The Direct Python API maps from CPP to Python directly, so any CPP function can be exposed to python users.
- A Trie data structure is used to map a fusion math definition to FusionExecutorCache.
- This FusionCache creates the CPP Fusion. The user cannot directly create nor modify the CPP fusion in python.
- RecordFunctor is created for each operation to handle equivalence, hashing, printing, and CPP interoperability.
- Caching fusions by definitions AND serialization is not supported with direct bindings.
- Supports using constant scalars with any operation.
- The python reproducers map directly to Fusion IR, so it will be more verbose. Direct bindings does not have
RecordFunctorobjects to map from Fusion IR to original python API.
- Create
py::class_for IR node. Statement,Expr,Val,IterDomain,TensorDomain,TensorView, andScalarexist inpython_direct/ir.cpp- All other nodes from
csrc/ir/internal_nodes.hgo topython_direct/internal_ir.cpp - Add functions to the IR node's
pyt::class_. All nodes map CPPtoStringto python__str__, so the node is printable. Other functions are usually added to support scheduling.
- Add operation to
python_direct/ops.cppwith numpy-style docstring. - If an operation corresponds with some new Expr nodes, add the appropriate
void handle(const SomeOp*) finaltopython_direct/python_translate.cpp. For example,broadcast_in_dimis a composite operations usingbroadcastandexpand, so you would need to add twohandlefunctions toPythonTranslator.
- Create handle function for expression.
- Check if IR node pointer is not nullptr.
- Add output values for Expr node to
visited_vals_. - Create dynamic scalar input arguments. This step is for view and expand operations. TensorView input arguments are handled via DAG traversal. Constant scalars are added directly to python defintion.
- Use
PythonPrinter::generateOperationif the operation only uses positional arguments. This is mainly used for unary and binary operations. - Use
PythonPrinter::generateKwargsOperationif the operation uses keyword arguments. If none of the keyword arguments have default arguments, create a static vector of strings. If some of the keyword arguments have default arguments, create a vector of KeywordArgument. The KeywordArgument struct hold default values for keyword arguments. Usestd::nulloptfor keyword arguments without default values.
- Recompile with debug symbols with
export NVFUSER_BUILD_BUILD_TYPE=RelwithDebInfo. - Run
gdb python. - Catch exception in gdb
(gdb) catch throw. - Run failing test with
r -m pytest test_python_frontend.py -k [your_failing_test]. - At gdb Catchpoint, get backtrace for call stack using
(gdb) bt. - Find and fix failure in PythonTranslate.