I have begun to experiment with writing a new library called `astgen` to 
replace the large quantity of boilerplate required by the AST today, and enable 
us to more flexibly evolve the node system, and its APIs.

The first version of this tool will take a Python file like this:
```python
import astgen
import tvm

class Expr:
    pass

@astgen.astgen
class Constant(Expr):
    """
    \\brief Constant tensor, backed by an NDArray on the cpu(0) device.
    \\note Scalar constants are represented by rank-0 constant tensors,
           enabling uniform constant folding over scalars and tensors.
    """

    """The data of the tensor."""
    data: tvm.ndarray.NDArray

astgen.generate_all("expr.h", "tvm::relay")

```

and produce this C++ file:

```cpp
namespace tvm {
namespace relay {

/*!
 * \brief Constant tensor, backed by an NDArray on the cpu(0) device.
 * \note Scalar constants are represented by rank-0 constant tensors,
 * enabling uniform constant folding over scalars and tensors.
 *
 */
class Constant;

/*!
 * \brief Constant container.
 *
 */
class ConstantNode : public ExprNode {
 public:
  void VisitAttrs(tvm::AttrVisitor* v) final {
    v->Visit("data", &data);
  }
  TVM_DLL static Constant make(runtime::NDArray data);

  static constexpr const char* _type_key = "relay.Constant";
  TVM_DECLARE_NODE_TYPE_INFO(ConstantNode, ExprNode);
};
}
RELAY_DEFINE_NODE_REF(Constant, ConstantNode, Expr);

} // relay
} // tvm
```

This compliments Tianqi's recent proposal to evolve the low level IR see #3474.

There are some open challenges left in this proposal, a goal of mine is to 
allow any language with a C ABI compatible FFI to construct and manipulate TVM 
ASTs. 

By supporting this we will allow users to build tools in languages of choice 
without having to change how we develop the core of TVM. Furthermore this will 
improve Python interop. as we will no longer have to deal with hidden C++ 
fields as is the case today.

Unfortunately we have heavily relied on C++ objects, and C++ datatypes such as 
`std::string`. 

I hope the community can help come up with a design for Relay's IR using this 
new tool that allows to flexibly share the AST.

I will follow up with more details over the next few days.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/dmlc/tvm/issues/3501

Reply via email to