You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2023/02/08 14:38:23 UTC
[tvm] branch unity-staging updated (6e5532ae06 -> c1ccd5bd07)
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a change to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
omit 6e5532ae06 [Unity] Basic StructInfo Analysis and Expr construction.
omit b1573d20ad [Unity][CI] Unity specific jenkins setup (do not upstream to main) (#13910)
omit 83e1f07d82 [Unity][IR] First-class StructInfo (#13907)
omit 5c38bef497 [Unity] Relax expressions and types (#13901)
omit 129b12f9b2 [Unity] Relax VM (#13878)
add 239edb5158 [microTVM] Allow multiple runners in tuning micro models with meta-schedule (#13811)
add 697fdb2cb7 [TVMScript] Comments and docstrings printing (#13839)
add f7dfef4cde [ONNX] QGemm support (#13747)
add 4ddb87631b [microTVM] Fix host-driven AOT memory workspaces (#13807)
add ec72ac6690 [ROCM] Fixes compiling on ROCM 5 and accuracy on dense op (#13847)
add 18b7dc1dd9 [MetaSchedule] Fix for RewriteLayout + AllocateConst when the rank of the rewritten weight doesn't change (#13851)
add 56771a87d1 [CLML][RUNTIME] Enable more ops in CLML runtime (#13834)
add 2bfdcbe07a [Relay] Convert negative axes to positive when importing ONNX Unsqueeze (#13846)
add 16b19582a2 [ETHOSN] Apply FoldConstant before NPU partitioning (#13848)
add 95fa22308b [Hexagon][CI] Updated sha for builder LLVM (#13418)
add c2cc01910c [microTVM] Update tutorials (#13845)
add 1bc8cf80d0 [ONNX] Support Bernoulli op on ONNX front-end (#13802)
add 0c2ab1bb42 [Arith] Support eq in detect_clip_bound (#13746)
add acde2c1fc3 [topi] remove comment redundancy in resize.py (#13860)
add 10d6c17238 [WASM][FIX] test tests/node/websock_rpc_test.py (#13862)
add 3c81d9bbac [RUNTIME][CLML] OpenCLML tuning and profiling enhanced (#13843)
add e6262bf985 [ONNX] Support SequenceLength op (#13863)
add 76c5186e15 Enable C++17 for cmake modules (#13869)
add 803207c256 [Hexagon][CI] Update the docker image ID to reflect newer LLVM (#13870)
add c81aaa852c [LLVM] Remove call to EmitDebugLocation from AddAliasInfo (#13872)
add 558c994d1a [TIR][FIX] check args size when creating prim_func by runtime::Registry (#13809)
add 4daf38f691 [TVMScript] Robustify the Highlight Printer (#13861)
add 0d5baacc02 [ONNX] Support SequenceErase op (#13865)
add d8833bdbbe [ONNX] Support SequenceEmpty op (#13866)
add 5e652c1a7a [CI] NNPACK build issue workaround (#13873)
add 73740385a9 [microTVM]Refactor test and add skip to current failing tests/boards (#13858)
add 206f085b03 [TIR][TOPI][CI] Fix number of arguments in calls of llvm_pure_intrin (#13881)
add c3fe08fb20 [ARITH] support floordiv in deduce bound (#13880)
add 7db77ada3d [Build] Fix misleading error messages (#13887)
add a1229f6fe4 [TIR] Handle nullptr returned by FindEntryFunc (#13852)
add d6f78b1acc [microTVM] Update Zephyr version and Zephyr SDK version (#13818)
add 9bbc2c0ab7 [MetaSchedule] Use `shared.dyn` for Tensor Core Schedule Rules (#13891)
add 2ced7a72a1 [microTVM] Clean-up test_crt.py and add to pylint (#13886)
add 2877c5a3cf [COMMUNITY] Wrongtest -> PMC (#13893)
add ba936e9547 [CUTLASS][Ansor] Combine CUTLASS and Ansor (#13879)
add 49849c8c3e Extend the USE_LIBBACKTRACE option (#13816)
add f0ea9e461a [RUNTIME] Fix the manual determination of cores in FillDataForMeasure (#13849)
add 9008ec21ba [VM][DMLC] Lower memory usage when loading and dumping weights (#13877)
add 7aecc1a44d [Torch] Fix advanced indexing with NoneType index arguments (#13826)
add 37e1a6862c [QNN][Relay][Topi] Add qnn.dense with weight layout (#13854)
add ea34e6eb0b [TOPHUB] use keys as a keyword for searching of existing statistics (#13874)
add 099ed94951 [OpenCL] Implement save/load pre-compiled programs (#13868)
add a89ff3e62f [tir] fix buffer_decl buffer allocation (#13906)
add 666006e926 [Doc] fix doc for tvm.te.const() (#13904)
add d35a8ab135 [CLML][CODEGEN] CLML native codegen utility (#13837)
add 209f074e96 [CI] Pass the 'path' parameter passed to cmake_build to the task_build.py script (#13905)
add 583f5ae37c [ONNX] Support Bitwise operations (#13888)
add 62a69a6c93 [TVMC] Stop printing a wall of warnings with tvmc tune (#13882)
add fb2315a266 [Fix][Arith] Analyzer simplification starts with canonical (#13875)
add e34506caaa Fix numpy version constraint (#13912)
add 98008c2d67 [Fix][TVMScript] Fix `LetStmt` printing logic (#13900)
add f7aeaf1d38 [TVMScript] Connect `assert_structural_equal` with new TVMScript printer (#13859)
add cec5f0bc10 [microNPU] Add rescale parameters for binary elementwise (#13890)
add 60fcb0dede Revert the changes for NNPACK build issue (#13913)
add 5456fae776 [TVMScript,Fix] Fix findsource when classes are indented (#13924)
add 282f17584b [Arith] Updated incorrect simplification rule (#13922)
add 7323e1d16f [OpenCL] Refactor OpenCL init function (#13919)
add c8b99a93a1 Fix build platform environment variable (#13914)
add 7149142781 [TVMScript] Schedule error reporting with new TVMScript printer (#13921)
add 6fb961a945 [CI] Include static builds of the runtime as part of CI (#13612)
add c36ae1cb3a [Arith][UnitTest] Parametrize tests of RewriteSimplifier (#13923)
new 486c617bfa [Unity] Relax VM (#13878)
new 8ee4b6f7c6 [Unity] Relax expressions and types (#13901)
new 8aaff909ea [Unity][IR] First-class StructInfo (#13907)
new 5d3a864e78 [Unity][CI] Unity specific jenkins setup (do not upstream to main) (#13910)
new 3963005a93 [Unity] Basic StructInfo Analysis and Expr construction (#13916)
new 284be6ee87 [Unity] Relax BlockBuilder and ExprMutator (#13926)
new c1ccd5bd07 [Unity] Relax TVMScript Parser. (#13932)
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (6e5532ae06)
\
N -- N -- N refs/heads/unity-staging (c1ccd5bd07)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.github/workflows/main.yml | 34 +
CMakeLists.txt | 26 +-
CONTRIBUTORS.md | 2 +-
apps/cpp_clml/CMakeLists.txt | 61 +
apps/cpp_clml/README.md | 145 ++
apps/cpp_clml/clml_runner.cc | 818 +++++++++
apps/cpp_clml/clml_runner.h | 262 +++
apps/cpp_clml/main.cc | 243 +++
apps/cpp_clml/scripts/clml_codegen.py | 64 +
apps/cpp_rtvm/README.md | 14 +
apps/cpp_rtvm/main.cc | 9 +
apps/cpp_rtvm/tvm_runner.cc | 29 +-
apps/cpp_rtvm/tvm_runner.h | 4 +
.../template_project/launch_microtvm_api_server.sh | 34 +-
.../template_project/microtvm_api_server.py | 13 +-
.../template_project/launch_microtvm_api_server.sh | 29 +-
.../zephyr/template_project/microtvm_api_server.py | 41 +-
.../src/aot_standalone_demo/main.c | 5 +-
.../src/aot_standalone_demo/zephyr_uart.c | 6 +-
.../zephyr/template_project/src/host_driven/main.c | 22 +-
.../src/mlperftiny/submitter_implemented.cc | 4 +-
.../template_project/src/mlperftiny/tvmruntime.cc | 4 +-
.../template_project/src/mlperftiny/zephyr_uart.cc | 6 +-
apps/microtvm/zephyr_cmsisnn/src/main.c | 4 +-
ci/jenkins/docker-images.ini | 4 +-
ci/jenkins/generated/arm_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/cortexm_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/cpu_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/docker_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/gpu_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/hexagon_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/i386_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/lint_jenkinsfile.groovy | 4 +-
.../generated/minimal_cross_isa_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/minimal_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/riscv_jenkinsfile.groovy | 4 +-
ci/jenkins/generated/wasm_jenkinsfile.groovy | 4 +-
ci/jenkins/templates/utils/Build.groovy.j2 | 2 +-
cmake/config.cmake | 15 +
cmake/libs/Libbacktrace.cmake | 4 -
cmake/modules/Arduino.cmake | 2 +
cmake/modules/Logging.cmake | 106 +-
cmake/modules/Zephyr.cmake | 2 +
cmake/modules/contrib/CLML.cmake | 2 +-
cmake/utils/FindLLVM.cmake | 12 +-
docker/Dockerfile.ci_adreno | 3 +
docker/build.sh | 13 +
docker/install/ubuntu_install_hexagon.sh | 5 +-
docker/install/ubuntu_install_nnpack.sh | 2 +-
docs/conf.py | 12 +-
docs/topic/microtvm/index.rst | 11 +-
.../how_to/work_with_microtvm/install_zephyr.rst | 14 +-
gallery/how_to/work_with_microtvm/micro_aot.py | 17 +-
.../how_to/work_with_microtvm/micro_autotune.py | 13 +-
gallery/how_to/work_with_microtvm/micro_ethosu.py | 6 +-
.../how_to/work_with_microtvm/micro_mlperftiny.py | 7 +-
gallery/how_to/work_with_microtvm/micro_pytorch.py | 18 +-
.../work_with_microtvm/micro_reference_vm.py | 159 --
gallery/how_to/work_with_microtvm/micro_tflite.py | 72 +-
gallery/how_to/work_with_microtvm/micro_train.py | 14 +-
gallery/how_to/work_with_microtvm/micro_tvmc.sh | 43 +-
include/tvm/ir/expr.h | 1 +
include/tvm/node/script_printer.h | 20 +-
include/tvm/node/structural_equal.h | 11 +-
include/tvm/relax/analysis.h | 13 +
include/tvm/relax/block_builder.h | 239 +++
include/tvm/relax/expr.h | 7 +-
include/tvm/relax/expr_functor.h | 138 +-
include/tvm/relax/op_attr_types.h | 75 +
include/tvm/relax/struct_info.h | 5 +-
include/tvm/relax/utils.h | 155 ++
include/tvm/script/ir_builder/ir/frame.h | 11 +-
include/tvm/script/ir_builder/ir/ir.h | 17 +
include/tvm/script/ir_builder/relax/frame.h | 293 +++
include/tvm/script/ir_builder/relax/ir.h | 137 ++
include/tvm/script/printer/doc.h | 44 +
include/tvm/script/printer/ir_docsifier.h | 43 +
include/tvm/te/operation.h | 2 +-
python/gen_requirements.py | 2 +-
python/tvm/auto_scheduler/relay_integration.py | 8 +-
python/tvm/autotvm/measure/measure_methods.py | 2 +-
python/tvm/autotvm/tophub.py | 10 +
.../micro/meta_schedule/rpc_runner_micro.py | 108 +-
python/tvm/contrib/rocm.py | 4 +-
python/tvm/driver/tvmc/autotuner.py | 3 +-
python/tvm/ir/expr.py | 50 +-
python/tvm/ir/function.py | 16 +
python/tvm/meta_schedule/utils.py | 50 +-
python/tvm/micro/build.py | 7 +
python/tvm/micro/project_api/client.py | 14 +-
python/tvm/micro/testing/pytest_plugin.py | 7 +-
python/tvm/micro/testing/utils.py | 8 +-
python/tvm/relax/__init__.py | 11 +
python/tvm/relax/analysis/analysis.py | 33 +-
python/tvm/relax/block_builder.py | 801 ++++++++
python/tvm/relax/expr_functor.py | 1530 ++++++++++++++++
.../{script/ir_builder/ir => relax/op}/__init__.py | 9 +-
.../ir/__init__.py => relax/op/_ffi_api.py} | 8 +-
python/tvm/relax/op/base.py | 358 ++++
python/tvm/relax/op/binary.py | 67 +
python/tvm/relax/utils.py | 278 +++
.../contrib/ethosu/op/binary_elementwise.py | 18 +
.../contrib/ethosu/te/binary_elementwise.py | 12 +
.../contrib/ethosu/tir/binary_elementwise.py | 6 +-
.../tvm/relay/backend/contrib/ethosu/tir/spec.py | 12 +
.../backend/contrib/ethosu/tir_to_cs_translator.py | 5 +
python/tvm/relay/frontend/onnx.py | 256 ++-
python/tvm/relay/frontend/pytorch.py | 40 +-
python/tvm/relay/op/contrib/clml.py | 788 +++++++-
python/tvm/relay/op/contrib/ethosn.py | 1 +
python/tvm/relay/op/strategy/cuda.py | 15 +-
python/tvm/relay/qnn/op/_qnn.py | 11 +-
python/tvm/relay/qnn/op/legalizations.py | 134 +-
python/tvm/relay/qnn/op/qnn.py | 64 +
python/tvm/relay/qnn/strategy/generic.py | 6 +
python/tvm/relay/qnn/strategy/hexagon.py | 18 +
python/tvm/runtime/object_path.py | 2 +
python/tvm/runtime/script_printer.py | 58 +-
python/tvm/script/highlight.py | 13 +-
python/tvm/script/ir_builder/base.py | 6 +-
python/tvm/script/ir_builder/ir/__init__.py | 2 +-
python/tvm/script/ir_builder/ir/ir.py | 45 +
.../{parser => ir_builder}/relax/__init__.py | 9 +-
.../__init__.py => ir_builder/relax/_ffi_api.py} | 7 +-
python/tvm/script/ir_builder/relax/frame.py | 55 +
python/tvm/script/ir_builder/relax/ir.py | 407 +++++
python/tvm/script/parser/__init__.py | 3 +-
python/tvm/script/parser/core/diagnostics.py | 7 +-
python/tvm/script/parser/core/entry.py | 5 +
python/tvm/script/parser/core/evaluator.py | 2 +-
python/tvm/script/parser/core/parser.py | 50 +-
python/tvm/script/parser/ir/parser.py | 15 +
python/tvm/script/parser/relax/__init__.py | 17 +-
python/tvm/script/parser/relax/entry.py | 327 ++++
python/tvm/script/parser/relax/parser.py | 276 +++
python/tvm/script/parser/tir/entry.py | 4 +-
python/tvm/script/parser/tir/operation.py | 12 +-
python/tvm/script/parser/tir/parser.py | 26 +
python/tvm/script/printer/doc.py | 20 +
python/tvm/script/printer/doc_printer.py | 4 +-
python/tvm/te/__init__.py | 1 +
python/tvm/te/operation.py | 68 +-
python/tvm/tir/tensor_intrin/cuda.py | 166 +-
python/tvm/tir/tensor_intrin/x86.py | 2 +-
python/tvm/topi/hexagon/qnn/__init__.py | 1 +
.../tvm/topi/hexagon/qnn/dense_alter_op.py | 28 +-
python/tvm/topi/hexagon/qnn/nn.py | 216 ++-
python/tvm/topi/image/resize.py | 6 -
python/tvm/topi/nn/qnn.py | 19 +
python/tvm/topi/x86/tensor_intrin.py | 12 +-
src/arith/analyzer.cc | 4 +
src/arith/bound_deducer.cc | 61 +-
src/arith/canonical_simplify.cc | 12 +-
src/arith/detect_linear_equation.cc | 38 +-
src/arith/rewrite_simplify.cc | 10 +-
src/ir/function.cc | 14 +
src/ir/module.cc | 16 +-
src/meta_schedule/module_equality.cc | 2 +-
src/meta_schedule/schedule_rule/schedule_rule.cc | 40 +-
src/node/script_printer.cc | 19 +-
src/node/structural_equal.cc | 73 +-
src/node/structural_hash.cc | 20 +-
src/relax/analysis/struct_info_analysis.cc | 149 ++
src/relax/ir/block_builder.cc | 969 ++++++++++
src/relax/ir/emit_te.cc | 78 +
src/relax/ir/emit_te.h | 68 +
src/relax/ir/expr_functor.cc | 244 +++
src/relax/ir/py_expr_functor.cc | 649 +++++++
src/relax/op/op.cc | 77 +
src/relax/op/op_common.cc | 122 ++
src/relax/op/op_common.h | 285 +++
src/relax/op/tensor/binary.cc | 87 +
src/relax/op/tensor/binary.h | 70 +
src/{script/ir_builder/ir/ir.cc => relax/utils.cc} | 31 +-
.../backend/contrib/codegen_json/codegen_json.h | 1 +
src/relay/backend/te_compiler_cache.cc | 41 +-
src/relay/op/contrib/ethosu/binary_elementwise.cc | 6 +-
src/relay/op/contrib/ethosu/op_attrs.h | 10 +
src/relay/op/nn/nn.h | 5 +
src/relay/qnn/op/dense.cc | 105 +-
src/runtime/const_loader_module.cc | 10 +
src/runtime/contrib/clml/clml_runtime.cc | 210 ++-
src/runtime/contrib/json/json_runtime.h | 3 +
src/runtime/contrib/random/mt_random_engine.cc | 10 +-
src/runtime/crt/aot_executor/aot_executor.c | 13 +-
src/runtime/file_utils.h | 37 +
src/runtime/opencl/opencl_common.h | 64 +-
src/runtime/opencl/opencl_device_api.cc | 155 +-
src/runtime/opencl/opencl_module.cc | 88 +-
.../opencl/opencl_wrapper/opencl_wrapper.cc | 12 +
src/runtime/vm/executable.cc | 22 +-
src/script/ir_builder/ir/frame.cc | 12 +-
src/script/ir_builder/ir/ir.cc | 41 +-
src/script/ir_builder/ir/{frame.cc => utils.h} | 30 +-
src/script/ir_builder/relax/frame.cc | 273 +++
src/script/ir_builder/relax/ir.cc | 236 +++
src/script/ir_builder/relax/utils.h | 119 ++
src/script/ir_builder/tir/frame.cc | 15 +-
src/script/ir_builder/tir/utils.h | 2 +-
src/script/printer/doc.cc | 22 +
src/script/printer/doc_printer/base_doc_printer.cc | 87 +-
src/script/printer/doc_printer/base_doc_printer.h | 24 +-
.../printer/doc_printer/python_doc_printer.cc | 67 +-
src/script/printer/ir/ir.cc | 3 +-
src/script/printer/ir/utils.h | 1 +
src/script/printer/tir/block.cc | 8 +-
src/script/printer/tir/for_loop.cc | 24 +-
src/script/printer/tir/function.cc | 15 +-
src/script/printer/tir/stmt.cc | 20 +-
src/script/printer/tir/utils.h | 5 +-
src/script/printer/utils.h | 20 +
src/target/llvm/codegen_llvm.cc | 1 -
src/target/source/source_module.cc | 16 +-
src/te/operation/create_primfunc.cc | 81 +
src/te/operation/create_primfunc.h | 17 +
src/tir/analysis/deep_equal.cc | 1 +
src/tir/analysis/stmt_finding.cc | 39 +-
src/tir/schedule/concrete_schedule.h | 2 +-
src/tir/schedule/error.cc | 20 +-
src/tir/schedule/utils.h | 10 +-
.../plan_update_buffer_allocation_location.cc | 7 +
tests/cpp-runtime/opencl/opencl_compile_to_bin.cc | 208 +++
tests/cpp-runtime/opencl/opencl_timer_test.cc | 11 +-
tests/lint/pylint.sh | 1 +
tests/micro/common/test_autotune.py | 3 +
tests/micro/common/test_tvmc.py | 3 +
.../test_arduino_microtvm_api_server.py | 200 --
tests/micro/project_api/test_project_api.py | 33 +-
tests/micro/zephyr/test_ms_tuning.py | 20 +-
tests/micro/zephyr/test_zephyr.py | 71 +-
tests/micro/zephyr/test_zephyr_aot_exec.py | 13 +-
.../zephyr/test_zephyr_aot_exec_standalone.py | 9 +-
tests/micro/zephyr/test_zephyr_armv7m.py | 21 +-
tests/micro/zephyr/utils.py | 28 +-
.../contrib/test_arm_compute_lib/test_dense.py | 6 +-
tests/python/contrib/test_clml/test_ops.py | 102 ++
tests/python/contrib/test_cutlass.py | 140 +-
tests/python/contrib/test_ethosn/test_addition.py | 68 +-
tests/python/contrib/test_ethosn/test_networks.py | 2 +-
.../test_ethosu_binary_elementwise_matcher.py | 3 +
tests/python/contrib/test_ethosu/infra.py | 6 +
tests/python/contrib/test_ethosu/test_codegen.py | 65 +
.../test_ethosu/test_replace_binary_elementwise.py | 2 +
tests/python/contrib/test_ethosu/test_scheduler.py | 2 +-
.../test_ethosu/test_tir_to_cs_translator.py | 30 +-
.../test_hexagon/test_wo_qnn_canonicalization.py | 172 +-
tests/python/contrib/test_random.py | 28 +
tests/python/frontend/onnx/test_forward.py | 579 +++++-
tests/python/frontend/pytorch/test_forward.py | 35 +
.../relax/test_analysis_struct_info_analysis.py | 143 ++
tests/python/relax/test_blockbuilder.py | 542 ++++++
tests/python/relax/test_expr.py | 4 +-
tests/python/relax/test_expr_functor.py | 746 ++++++++
tests/python/relax/test_tvmscript_ir_builder.py | 153 ++
tests/python/relax/test_tvmscript_parser.py | 1079 +++++++++++
tests/python/relay/test_pass_qnn_legalize.py | 92 +
.../unittest/test_arith_canonical_simplify.py | 14 +
tests/python/unittest/test_arith_deduce_bound.py | 38 +-
.../unittest/test_arith_detect_clip_bound.py | 13 +
tests/python/unittest/test_arith_intset.py | 11 +-
.../python/unittest/test_arith_rewrite_simplify.py | 1909 ++++++++++----------
..._dispatch_context.py => test_arith_simplify.py} | 27 +-
.../unittest/test_autotvm_dispatch_context.py | 16 +
tests/python/unittest/test_crt.py | 183 +-
.../python/unittest/test_meta_schedule_database.py | 34 +-
...est_meta_schedule_postproc_rewrite_tensorize.py | 2 +-
.../test_meta_schedule_relay_integration.py | 74 +
.../test_meta_schedule_schedule_rule_mlt_tc.py | 91 +-
.../unittest/test_meta_schedule_trace_apply.py | 18 +-
.../unittest/test_micro_model_library_format.py | 1 -
tests/python/unittest/test_tir_buffer.py | 2 +-
.../python/unittest/test_tir_schedule_analysis.py | 6 +-
.../unittest/test_tir_schedule_compute_inline.py | 4 +-
...sform_plan_update_buffer_allocation_location.py | 18 +
.../python/unittest/test_tvmscript_error_report.py | 22 +-
.../unittest/test_tvmscript_parser_source.py | 15 +
.../unittest/test_tvmscript_printer_annotation.py | 86 +
.../python/unittest/test_tvmscript_printer_doc.py | 28 +
.../unittest/test_tvmscript_printer_highlight.py | 29 +
tests/python/unittest/test_tvmscript_printer_ir.py | 3 +
.../test_tvmscript_printer_python_doc_printer.py | 62 +-
.../test_tvmscript_printer_structural_equal.py | 134 ++
.../python/unittest/test_tvmscript_printer_tir.py | 19 +-
.../unittest/test_tvmscript_printer_underlining.py | 127 +-
tests/python/unittest/test_tvmscript_roundtrip.py | 28 +
tests/scripts/request_hook/request_hook.py | 2 +-
tests/scripts/task_config_build_cpu.sh | 2 +-
tests/scripts/task_config_build_hexagon.sh | 5 +-
tests/scripts/task_config_build_minimal.sh | 2 +-
...uild_minimal.sh => task_config_build_static.sh} | 20 +-
tests/scripts/task_demo_microtvm.sh | 7 +-
web/emcc/tvmjs_support.cc | 2 +-
292 files changed, 20386 insertions(+), 2793 deletions(-)
create mode 100644 apps/cpp_clml/CMakeLists.txt
create mode 100644 apps/cpp_clml/README.md
create mode 100644 apps/cpp_clml/clml_runner.cc
create mode 100644 apps/cpp_clml/clml_runner.h
create mode 100644 apps/cpp_clml/main.cc
create mode 100644 apps/cpp_clml/scripts/clml_codegen.py
copy docker/install/ubuntu_install_nnpack.sh => apps/microtvm/arduino/template_project/launch_microtvm_api_server.sh (55%)
copy tests/python/unittest/test_autotvm_dispatch_context.py => apps/microtvm/zephyr/template_project/launch_microtvm_api_server.sh (67%)
mode change 100644 => 100755
delete mode 100644 gallery/how_to/work_with_microtvm/micro_reference_vm.py
create mode 100644 include/tvm/relax/block_builder.h
create mode 100644 include/tvm/relax/op_attr_types.h
create mode 100644 include/tvm/relax/utils.h
create mode 100644 include/tvm/script/ir_builder/relax/frame.h
create mode 100644 include/tvm/script/ir_builder/relax/ir.h
create mode 100644 python/tvm/relax/block_builder.py
create mode 100644 python/tvm/relax/expr_functor.py
copy python/tvm/{script/ir_builder/ir => relax/op}/__init__.py (85%)
copy python/tvm/{script/ir_builder/ir/__init__.py => relax/op/_ffi_api.py} (86%)
create mode 100644 python/tvm/relax/op/base.py
create mode 100644 python/tvm/relax/op/binary.py
create mode 100644 python/tvm/relax/utils.py
copy python/tvm/script/{parser => ir_builder}/relax/__init__.py (82%)
copy python/tvm/script/{parser/relax/__init__.py => ir_builder/relax/_ffi_api.py} (83%)
create mode 100644 python/tvm/script/ir_builder/relax/frame.py
create mode 100644 python/tvm/script/ir_builder/relax/ir.py
create mode 100644 python/tvm/script/parser/relax/entry.py
create mode 100644 python/tvm/script/parser/relax/parser.py
copy tests/python/unittest/test_autotvm_dispatch_context.py => python/tvm/topi/hexagon/qnn/dense_alter_op.py (59%)
create mode 100644 src/relax/ir/block_builder.cc
create mode 100644 src/relax/ir/emit_te.cc
create mode 100644 src/relax/ir/emit_te.h
create mode 100644 src/relax/ir/py_expr_functor.cc
create mode 100644 src/relax/op/op.cc
create mode 100644 src/relax/op/op_common.cc
create mode 100644 src/relax/op/op_common.h
create mode 100644 src/relax/op/tensor/binary.cc
create mode 100644 src/relax/op/tensor/binary.h
copy src/{script/ir_builder/ir/ir.cc => relax/utils.cc} (56%)
copy src/script/ir_builder/ir/{frame.cc => utils.h} (59%)
create mode 100644 src/script/ir_builder/relax/frame.cc
create mode 100644 src/script/ir_builder/relax/ir.cc
create mode 100644 src/script/ir_builder/relax/utils.h
create mode 100644 tests/cpp-runtime/opencl/opencl_compile_to_bin.cc
delete mode 100644 tests/micro/project_api/test_arduino_microtvm_api_server.py
create mode 100644 tests/python/relax/test_blockbuilder.py
create mode 100644 tests/python/relax/test_expr_functor.py
create mode 100644 tests/python/relax/test_tvmscript_ir_builder.py
create mode 100644 tests/python/relax/test_tvmscript_parser.py
copy tests/python/unittest/{test_autotvm_dispatch_context.py => test_arith_simplify.py} (63%)
create mode 100644 tests/python/unittest/test_tvmscript_printer_annotation.py
create mode 100644 tests/python/unittest/test_tvmscript_printer_structural_equal.py
copy tests/scripts/{task_config_build_minimal.sh => task_config_build_static.sh} (66%)
[tvm] 07/07: [Unity] Relax TVMScript Parser. (#13932)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit c1ccd5bd0737af0be025fe2f2aa9b4e2f849a1bf
Author: Siyuan Feng <Hz...@sjtu.edu.cn>
AuthorDate: Wed Feb 8 22:31:47 2023 +0800
[Unity] Relax TVMScript Parser. (#13932)
This PR adds the TVMScript parser/ir_builder support based on the blockbuilder.
Co-authored-by: Ruihang Lai <ru...@cs.cmu.edu>
Co-authored-by: Junru Shao <ju...@gmail.com>
Co-authored-by: Tianqi Chen <ti...@gmail.com>
Co-authored-by: Yuchen Jin <yu...@cs.washington.edu>
Co-authored-by: Steven S. Lyubomirsky <sl...@gmail.com>
Co-authored-by: Yong Wu <yo...@gmail.com>
---
include/tvm/ir/expr.h | 1 +
include/tvm/script/ir_builder/ir/frame.h | 11 +-
include/tvm/script/ir_builder/ir/ir.h | 17 +
include/tvm/script/ir_builder/relax/frame.h | 293 ++++++
include/tvm/script/ir_builder/relax/ir.h | 137 +++
python/tvm/ir/expr.py | 50 +-
python/tvm/script/ir_builder/base.py | 6 +-
python/tvm/script/ir_builder/ir/__init__.py | 2 +-
python/tvm/script/ir_builder/ir/ir.py | 45 +
.../script/ir_builder/{ir => relax}/__init__.py | 7 +-
.../__init__.py => ir_builder/relax/_ffi_api.py} | 7 +-
python/tvm/script/ir_builder/relax/frame.py | 55 +
python/tvm/script/ir_builder/relax/ir.py | 407 ++++++++
python/tvm/script/parser/__init__.py | 3 +-
python/tvm/script/parser/core/diagnostics.py | 2 +-
python/tvm/script/parser/core/entry.py | 5 +
python/tvm/script/parser/core/evaluator.py | 2 +-
python/tvm/script/parser/core/parser.py | 50 +-
python/tvm/script/parser/ir/parser.py | 15 +
python/tvm/script/parser/relax/__init__.py | 17 +-
python/tvm/script/parser/relax/entry.py | 327 ++++++
python/tvm/script/parser/relax/parser.py | 276 +++++
python/tvm/script/parser/tir/entry.py | 4 +-
python/tvm/script/parser/tir/operation.py | 12 +-
python/tvm/script/parser/tir/parser.py | 26 +
src/ir/module.cc | 16 +-
src/script/ir_builder/ir/frame.cc | 12 +-
src/script/ir_builder/ir/ir.cc | 41 +-
src/script/ir_builder/ir/{frame.cc => utils.h} | 30 +-
src/script/ir_builder/relax/frame.cc | 273 +++++
src/script/ir_builder/relax/ir.cc | 236 +++++
src/script/ir_builder/relax/utils.h | 119 +++
src/script/ir_builder/tir/frame.cc | 15 +-
src/script/ir_builder/tir/utils.h | 2 +-
tests/python/relax/test_tvmscript_ir_builder.py | 153 +++
tests/python/relax/test_tvmscript_parser.py | 1079 ++++++++++++++++++++
36 files changed, 3684 insertions(+), 69 deletions(-)
diff --git a/include/tvm/ir/expr.h b/include/tvm/ir/expr.h
index d4ba628d36..c662067a04 100644
--- a/include/tvm/ir/expr.h
+++ b/include/tvm/ir/expr.h
@@ -462,6 +462,7 @@ class GlobalVarNode : public RelayExprNode {
v->Visit("virtual_device_", &virtual_device_);
v->Visit("span", &span);
v->Visit("_checked_type_", &checked_type_);
+ v->Visit("struct_info_", &struct_info_);
}
bool SEqualReduce(const GlobalVarNode* other, SEqualReducer equal) const {
diff --git a/include/tvm/script/ir_builder/ir/frame.h b/include/tvm/script/ir_builder/ir/frame.h
index 887981ccff..dacfc361a6 100644
--- a/include/tvm/script/ir_builder/ir/frame.h
+++ b/include/tvm/script/ir_builder/ir/frame.h
@@ -38,12 +38,17 @@ namespace ir {
*/
class IRModuleFrameNode : public IRBuilderFrameNode {
public:
- Array<GlobalVar> global_vars;
- Array<BaseFunc> functions;
+ /*! \brief A map from string names to global variables that ensures global uniqueness. */
+ Map<String, GlobalVar> global_var_map;
+ /*!
+ * \brief A map from GlobalVar to all global functions.
+ * \note Only defined functions are in the map, while declared functions are not included.
+ */
+ Map<GlobalVar, BaseFunc> functions;
void VisitAttrs(tvm::AttrVisitor* v) {
IRBuilderFrameNode::VisitAttrs(v);
- v->Visit("global_vars", &global_vars);
+ v->Visit("global_vars", &global_var_map);
v->Visit("functions", &functions);
}
diff --git a/include/tvm/script/ir_builder/ir/ir.h b/include/tvm/script/ir_builder/ir/ir.h
index f0e7cc6f5c..49bdcf60e6 100644
--- a/include/tvm/script/ir_builder/ir/ir.h
+++ b/include/tvm/script/ir_builder/ir/ir.h
@@ -37,6 +37,23 @@ namespace ir {
*/
TVM_DLL IRModuleFrame IRModule();
+/*!
+ * \brief Declare a Function without given the specific function implementation.
+ * \note It is usually used in cross-function call. And we can specify the function by `DefFunction`
+ * \param func_name The function unique name.
+ * \param func_signature A Function w/o body, which used to specify the function signature
+ * (i.e. func params and func return type/shape).
+ * \return The corresponding GlobalVar.
+ */
+TVM_DLL GlobalVar DeclFunction(const String& func_name, const BaseFunc& func_signature);
+
+/*!
+ * \brief Define the function which is declared before.
+ * \param func_name The function unique name.
+ * \param func The given function implementation
+ */
+TVM_DLL void DefFunction(const String& func_name, const BaseFunc& func);
+
} // namespace ir
} // namespace ir_builder
} // namespace script
diff --git a/include/tvm/script/ir_builder/relax/frame.h b/include/tvm/script/ir_builder/relax/frame.h
new file mode 100644
index 0000000000..0f544d3abc
--- /dev/null
+++ b/include/tvm/script/ir_builder/relax/frame.h
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
+#define TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
+
+#include <tvm/relax/block_builder.h>
+#include <tvm/relax/expr.h>
+#include <tvm/script/ir_builder/base.h>
+#include <tvm/script/ir_builder/ir/frame.h>
+#include <tvm/script/ir_builder/ir/ir.h>
+
+namespace tvm {
+namespace script {
+namespace ir_builder {
+namespace relax {
+
+/*! \brief The base ir_builder frame for the relax dialect. */
+class RelaxFrameNode : public IRBuilderFrameNode {
+ public:
+ void VisitAttrs(tvm::AttrVisitor* v) { IRBuilderFrameNode::VisitAttrs(v); }
+
+ static constexpr const char* _type_key = "script.ir_builder.relax.RelaxFrame";
+ TVM_DECLARE_BASE_OBJECT_INFO(RelaxFrameNode, IRBuilderFrameNode);
+};
+
+class RelaxFrame : public IRBuilderFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(RelaxFrame, IRBuilderFrame, RelaxFrameNode);
+
+ protected:
+ RelaxFrame() = default;
+};
+
+/*! \brief The base ir_builder frame for frames with SeqExpr
+ i.e. Functions, If branches
+ */
+class SeqExprFrameNode : public RelaxFrameNode {
+ public:
+ /*! \brief The binding blocks inside the frame. */
+ Array<tvm::relax::BindingBlock> binding_blocks;
+ /*! \brief The frame output expr. `NullOpt` when undefined. */
+ Optional<tvm::relax::Expr> output;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ RelaxFrameNode::VisitAttrs(v);
+ v->Visit("binding_blocks", &binding_blocks);
+ v->Visit("output", &output);
+ }
+
+ static constexpr const char* _type_key = "script.ir_builder.relax.SeqExprFrame";
+ TVM_DECLARE_BASE_OBJECT_INFO(SeqExprFrameNode, RelaxFrameNode);
+
+ public:
+ void EnterWithScope() override;
+ void ExitWithScope() override;
+};
+
+class SeqExprFrame : public RelaxFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(SeqExprFrame, RelaxFrame, SeqExprFrameNode);
+};
+
+/*! \brief The ir_builder frame for the relax function. */
+class FunctionFrameNode : public SeqExprFrameNode {
+ public:
+ /*!
+ * \brief The function name.
+ * \note The name will not be specified in constructor, so it is "Optional",
+ * However, we must specify the name by `R.func_name` before exit this frame.
+ */
+ Optional<String> name;
+ /*! \brief The function params. */
+ Array<tvm::relax::Var> params;
+ /*!
+ * \brief The function return struct info.
+ * \note Usually the function return type can be deduced by the function body.
+ * But we can use this field to specify a more "accurate" return type.
+ * i.e. If the `ret_struct_info` is None, try to use the deduced type from body
+ * If the `ret_struct_info` is not None, we can still take body.struct_info
+ * if we ret_struct_info is base of body.struct_info. If not, we will
+ * take the specified `ret_struct_info`.
+ */
+ Optional<tvm::relax::StructInfo> ret_struct_info;
+
+ /*! \brief The function attributes. */
+ Map<String, ObjectRef> attrs;
+ /*! \brief The block builder to create Relax function. */
+ tvm::relax::BlockBuilder block_builder;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ SeqExprFrameNode::VisitAttrs(v);
+ v->Visit("name", &name);
+ v->Visit("params", ¶ms);
+ v->Visit("ret_struct_info", &ret_struct_info);
+ v->Visit("attrs", &attrs);
+ v->Visit("binding_blocks", &binding_blocks);
+ v->Visit("output", &output);
+ // `block_builder` is not visited.
+ }
+
+ static constexpr const char* _type_key = "script.ir_builder.relax.FunctionFrame";
+ TVM_DECLARE_FINAL_OBJECT_INFO(FunctionFrameNode, SeqExprFrameNode);
+
+ public:
+ void ExitWithScope() final;
+};
+
+class FunctionFrame : public SeqExprFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(FunctionFrame, SeqExprFrame, FunctionFrameNode);
+};
+
+/*! \brief The ir_builder frame for relax binding blocks. */
+class BlockFrameNode : public RelaxFrameNode {
+ public:
+ /*! \brief The flag that indicates whether the block is a dataflow block. */
+ bool is_dataflow;
+ /*! \brief The variables emitted in this block. */
+ Array<tvm::relax::Var> emitted_vars;
+ /*!
+ * \brief A boolean indicating if the dataflow block is ended of construction.
+ * If it is true, any new binding trying to be emitted into this block will cause an error.
+ * \note Only used for a dataflow block.
+ */
+ bool block_ended;
+ /*!
+ * \brief The output vars of the dataflow block.
+ * \note Only used for a dataflow block.
+ */
+ Array<tvm::relax::Var> output_vars;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ RelaxFrameNode::VisitAttrs(v);
+ v->Visit("is_dataflow", &is_dataflow);
+ v->Visit("emitted_vars", &emitted_vars);
+ v->Visit("output_vars", &output_vars);
+ // `block_ended` is not visited.
+ }
+
+ static constexpr const char* _type_key = "script.ir_builder.relax.BlockFrame";
+ TVM_DECLARE_FINAL_OBJECT_INFO(BlockFrameNode, RelaxFrameNode);
+
+ public:
+ void EnterWithScope() final;
+ void ExitWithScope() final;
+};
+
+class BlockFrame : public RelaxFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockFrame, RelaxFrame, BlockFrameNode);
+};
+
+/*!
+ * \brief A frame that represents if statement.
+ *
+ * \sa IfFrame
+ */
+class IfFrameNode : public RelaxFrameNode {
+ public:
+ /*! \brief The condition of the if statement. */
+ tvm::relax::Expr condition;
+ /*! \brief The Bindings in the true branch. */
+ Optional<tvm::relax::Expr> then_expr;
+ /*! \brief The Bindings in the false branch. */
+ Optional<tvm::relax::Expr> else_expr;
+ /*! \brief The Binding var. */
+ tvm::relax::Var var;
+ /*! \brief The binding var name. */
+ String var_name;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ RelaxFrameNode::VisitAttrs(v);
+ v->Visit("condition", &condition);
+ v->Visit("then_expr", &then_expr);
+ v->Visit("else_expr", &else_expr);
+ v->Visit("var", &var);
+ v->Visit("var_name", &var_name);
+ }
+
+ static constexpr const char* _type_key = "script.ir_builder.relax.IfFrame";
+ TVM_DECLARE_FINAL_OBJECT_INFO(IfFrameNode, RelaxFrameNode);
+
+ public:
+ /*!
+ * \brief The method called when entering RAII scope.
+ * \sa tvm::support::With
+ */
+ void EnterWithScope() final;
+ /*!
+ * \brief The method called when exiting RAII scope.
+ * \sa tvm::support::With
+ */
+ void ExitWithScope() final;
+};
+
+/*!
+ * \brief Managed reference to IfFrameNode.
+ *
+ * \sa IfFrameNode
+ */
+class IfFrame : public RelaxFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IfFrame, RelaxFrame, IfFrameNode);
+};
+
+/*!
+ * \brief A frame that represents then.
+ *
+ * \sa ThenFrame
+ */
+class ThenFrameNode : public SeqExprFrameNode {
+ public:
+ static constexpr const char* _type_key = "script.ir_builder.relax.ThenFrame";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ThenFrameNode, SeqExprFrameNode);
+
+ public:
+ /*!
+ * \brief The method called when entering RAII scope.
+ * \sa tvm::support::With
+ */
+ void EnterWithScope() final;
+ /*!
+ * \brief The method called when exiting RAII scope.
+ * \sa tvm::support::With
+ */
+ void ExitWithScope() final;
+};
+
+/*!
+ * \brief Managed reference to ThenFrameNode.
+ *
+ * \sa ThenFrameNode
+ */
+class ThenFrame : public SeqExprFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ThenFrame, SeqExprFrame, ThenFrameNode);
+};
+
+/*!
+ * \brief A frame that represents else.
+ *
+ * \sa ElseFrame
+ */
+class ElseFrameNode : public SeqExprFrameNode {
+ public:
+ static constexpr const char* _type_key = "script.ir_builder.relax.ElseFrame";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ElseFrameNode, SeqExprFrameNode);
+
+ public:
+ /*!
+ * \brief The method called when entering RAII scope.
+ * \sa tvm::support::With
+ */
+ void EnterWithScope() final;
+ /*!
+ * \brief The method called when exiting RAII scope.
+ * \sa tvm::support::With
+ */
+ void ExitWithScope() final;
+};
+
+/*!
+ * \brief Managed reference to ElseFrameNode.
+ *
+ * \sa ElseFrameNode
+ */
+class ElseFrame : public SeqExprFrame {
+ public:
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ElseFrame, SeqExprFrame, ElseFrameNode);
+};
+
+} // namespace relax
+} // namespace ir_builder
+} // namespace script
+} // namespace tvm
+
+#endif // TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
diff --git a/include/tvm/script/ir_builder/relax/ir.h b/include/tvm/script/ir_builder/relax/ir.h
new file mode 100644
index 0000000000..72aab6684e
--- /dev/null
+++ b/include/tvm/script/ir_builder/relax/ir.h
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_SCRIPT_IR_BUILDER_RELAX_IR_H_
+#define TVM_SCRIPT_IR_BUILDER_RELAX_IR_H_
+
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/script/ir_builder/base.h>
+#include <tvm/script/ir_builder/relax/frame.h>
+
+namespace tvm {
+namespace script {
+namespace ir_builder {
+namespace relax {
+
+/////////////////////////////// Function ////////////////////////////////
+
+/*!
+ * \brief Start a function frame.
+ * \return The created ir_builder Function frame.
+ */
+TVM_DLL FunctionFrame Function();
+
+/*!
+ * \brief Add a parameter to the last function frame.
+ * \param name The name of the parameter.
+ * \param struct_info The struct_info of the parameter.
+ * \return The created function parameter var.
+ */
+TVM_DLL tvm::relax::Var Arg(const String& name, const tvm::relax::StructInfo& struct_info);
+
+/*!
+ * \brief Specify the name of the last function frame.
+ * \param name The function name.
+ */
+TVM_DLL void FuncName(const String& name);
+
+/*!
+ * \brief Specify the attrs of the last function frame.
+ * \param attrs The function attrs.
+ */
+TVM_DLL void FuncAttrs(Map<String, ObjectRef> attrs);
+
+/*!
+ * \brief Specify the return struct info of the last function frame.
+ * \param ret_sinfo The return struct info.
+ */
+TVM_DLL void FuncRetStructInfo(const tvm::relax::StructInfo& ret_sinfo);
+
+/*!
+ * \brief Specify the return value of the last function frame.
+ * \param value The return value.
+ */
+TVM_DLL void FuncRetValue(const tvm::relax::Expr& value);
+
+///////////////////////////// BindingBlock //////////////////////////////
+
+/*!
+ * \brief Start a binding block frame.
+ * \return The created ir_builder Block frame.
+ */
+TVM_DLL BlockFrame BindingBlock();
+
+/*!
+ * \brief Start a dataflow binding block frame.
+ * \return The created ir_builder Block frame.
+ */
+TVM_DLL BlockFrame Dataflow();
+
+/*!
+ * \brief Expose the dataflow block output variables as global ones
+ * \param vars The output variables of a dataflow block
+ */
+TVM_DLL void DataflowBlockOutput(const Array<tvm::relax::Var>& vars);
+
+////////////////////////////// Bindings ////////////////////////////////
+
+/*!
+ * \brief Emit a binding to the last binding block frame.
+ * \param value The right side value of the bindings to be emitted.
+ * \param annotate_struct_info The optional struct info annotation for the emitted value.
+ * \return The left side var of the emitted binding.
+ */
+TVM_DLL tvm::relax::Var Emit(
+ const tvm::relax::Expr& value,
+ const Optional<tvm::relax::StructInfo>& annotate_struct_info = NullOpt);
+
+/*!
+ * \brief Emit a match_cast binding to the last binding block frame.
+ * \param value The value of the MatchCast to be emitted.
+ * \param struct_info The struct info of the MatchCast to be emitted.
+ * \return The left side var of the emitted binding.
+ */
+TVM_DLL tvm::relax::Var EmitMatchCast(const tvm::relax::Expr& value,
+ const tvm::relax::StructInfo& struct_info);
+
+///////////////////////////// If Then Else /////////////////////////////
+
+/*!
+ * \brief Create an if statement.
+ * \param condition The condition of if statement.
+ * \return The result IfFrame.
+ */
+IfFrame If(tvm::relax::Expr condition);
+/*!
+ * \brief Create a then.
+ * \return The result ThenFrame.
+ */
+ThenFrame Then();
+/*!
+ * \brief Create an else.
+ * \return The result ElseFrame.
+ */
+ElseFrame Else();
+
+} // namespace relax
+} // namespace ir_builder
+} // namespace script
+} // namespace tvm
+
+#endif // TVM_SCRIPT_IR_BUILDER_RELAX_IR_H_
diff --git a/python/tvm/ir/expr.py b/python/tvm/ir/expr.py
index f90468de66..721e12e7f8 100644
--- a/python/tvm/ir/expr.py
+++ b/python/tvm/ir/expr.py
@@ -93,10 +93,17 @@ class GlobalVar(RelayExpr):
A call taking the variable as a function.
"""
# pylint: disable=import-outside-toplevel
+
+ # TODO(@relax-team): replace with Relax base class after it's introduced
if all(isinstance(x, RelayExpr) for x in args):
- from tvm import relay
+ if all(is_relax_expr(x) for x in args):
+ from tvm import relax
+
+ return relax.Call(self, args)
+ else:
+ from tvm import relay
- return relay.Call(self, args)
+ return relay.Call(self, args)
arg_types = [type(x) for x in args]
raise RuntimeError(
"Do not know how to handle GlobalVar.__call__ for types {}".format(arg_types)
@@ -185,3 +192,42 @@ class Range(Node, Scriptable):
The constructed range.
"""
return _ffi_api.Range_from_min_extent(min_value, extent, span)
+
+
+# TODO(@relax-team): remove when we have a RelaxExpr base class
+def is_relax_expr(expr: RelayExpr) -> bool:
+ """check if a RelayExpr is a Relax expresssion.
+
+ Parameters
+ ----------
+ expr : RelayExpr
+ The expression to check.
+
+ Returns
+ -------
+ res : bool
+ If the expression is Relax expression, return True; otherwise return False.
+ """
+ from tvm import relax # pylint: disable=import-outside-toplevel
+
+ if isinstance(
+ expr,
+ (
+ relax.Call,
+ relax.Constant,
+ relax.Tuple,
+ relax.TupleGetItem,
+ relax.If,
+ relax.Var,
+ relax.DataflowVar,
+ relax.ShapeExpr,
+ relax.SeqExpr,
+ relax.Function,
+ relax.ExternFunc,
+ relax.PrimValue,
+ relax.StringImm,
+ relax.DataTypeImm,
+ ),
+ ):
+ return True
+ return False
diff --git a/python/tvm/script/ir_builder/base.py b/python/tvm/script/ir_builder/base.py
index 7aa33ee49c..b35bbd0a7d 100644
--- a/python/tvm/script/ir_builder/base.py
+++ b/python/tvm/script/ir_builder/base.py
@@ -64,8 +64,10 @@ class IRBuilderFrame(_Object):
_ffi_api.IRBuilderFrameEnter(self) # type: ignore[attr-defined] # pylint: disable=no-member
return self
- def __exit__(self, ptype, value, trace) -> None: # pylint: disable=unused-argument
- _ffi_api.IRBuilderFrameExit(self) # type: ignore[attr-defined] # pylint: disable=no-member
+ def __exit__(self, exc_type, exc_value, trace) -> None: # pylint: disable=unused-argument
+ if exc_type is None and exc_value is None:
+ # Do not execute `FrameExit` if the with scope exits because of exceptions
+ _ffi_api.IRBuilderFrameExit(self) # type: ignore[attr-defined] # pylint: disable=no-member
def add_callback(self, callback: Callable[[], None]) -> None:
"""Add a callback method invoked when exiting the with-scope.
diff --git a/python/tvm/script/ir_builder/ir/__init__.py b/python/tvm/script/ir_builder/ir/__init__.py
index ebb9728737..946be263a7 100644
--- a/python/tvm/script/ir_builder/ir/__init__.py
+++ b/python/tvm/script/ir_builder/ir/__init__.py
@@ -16,4 +16,4 @@
# under the License.
"""Package tvm.script.ir_builder.ir"""
from .frame import IRModuleFrame
-from .ir import ir_module
+from .ir import decl_function, def_function, ir_module
diff --git a/python/tvm/script/ir_builder/ir/ir.py b/python/tvm/script/ir_builder/ir/ir.py
index 213180463c..796d6f3aad 100644
--- a/python/tvm/script/ir_builder/ir/ir.py
+++ b/python/tvm/script/ir_builder/ir/ir.py
@@ -16,9 +16,54 @@
# under the License.
"""Package tvm.script.ir_builder.ir.ir"""
+from tvm.ir import BaseFunc, GlobalVar
+
from . import _ffi_api
from .frame import IRModuleFrame
def ir_module() -> IRModuleFrame:
+ """Start a ir_module frame.
+ Returns
+ -------
+ frame: IRModuleFrame
+ The constructed frame.
+ """
return _ffi_api.IRModule() # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def decl_function(func_name: str, func_signature: BaseFunc) -> GlobalVar:
+ """Declare a Function without given the specific function implementation.
+ Parameters
+ ----------
+ func_name : str
+ The function unique name.
+
+ func_signature: Optional[BaseFunc]
+ A Function w/o body, which used to specify the function signature
+ (i.e. func params and func return type/shape).
+
+ Note
+ ----
+ It is usually used in cross-function call. And we can specify the function by `DefFunction`
+ Returns
+ -------
+ gv : GlobalVar
+ The corresponding GlobalVar.
+ """
+
+ return _ffi_api.DeclFunction( # type: ignore[attr-defined] # pylint: disable=no-member
+ func_name, func_signature
+ )
+
+
+def def_function(func_name: str, func: BaseFunc) -> None:
+ """Define the function which is declared before.
+ Parameters
+ ----------
+ func_name : str
+ The function unique name.
+ func: BaseFunc
+ The given function implementation
+ """
+ return _ffi_api.DefFunction(func_name, func) # type: ignore[attr-defined] # pylint: disable=no-member
diff --git a/python/tvm/script/ir_builder/ir/__init__.py b/python/tvm/script/ir_builder/relax/__init__.py
similarity index 82%
copy from python/tvm/script/ir_builder/ir/__init__.py
copy to python/tvm/script/ir_builder/relax/__init__.py
index ebb9728737..f0905acf34 100644
--- a/python/tvm/script/ir_builder/ir/__init__.py
+++ b/python/tvm/script/ir_builder/relax/__init__.py
@@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""Package tvm.script.ir_builder.ir"""
-from .frame import IRModuleFrame
-from .ir import ir_module
+# pylint: disable=unused-import
+"""Package tvm.script.ir_builder.relax"""
+from . import frame
+from .ir import * # pylint: disable=wildcard-import,redefined-builtin
diff --git a/python/tvm/script/parser/relax/__init__.py b/python/tvm/script/ir_builder/relax/_ffi_api.py
similarity index 83%
copy from python/tvm/script/parser/relax/__init__.py
copy to python/tvm/script/ir_builder/relax/_ffi_api.py
index feb8e68340..6e2098cf88 100644
--- a/python/tvm/script/parser/relax/__init__.py
+++ b/python/tvm/script/ir_builder/relax/_ffi_api.py
@@ -14,8 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""Initial impl of relax parser for sugars"""
-from tvm.relax import TensorStructInfo, ShapeStructInfo
+"""FFI APIs for tvm.script.ir_builder.relax"""
+import tvm._ffi
-Tensor = TensorStructInfo
-Shape = ShapeStructInfo
+tvm._ffi._init_api("script.ir_builder.relax", __name__) # pylint: disable=protected-access
diff --git a/python/tvm/script/ir_builder/relax/frame.py b/python/tvm/script/ir_builder/relax/frame.py
new file mode 100644
index 0000000000..97e181fbe4
--- /dev/null
+++ b/python/tvm/script/ir_builder/relax/frame.py
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""IR Builder Frame for Relax dialect"""
+from tvm._ffi import register_object as _register_object
+
+from ..base import IRBuilderFrame
+
+
+@_register_object("script.ir_builder.relax.RelaxFrame")
+class RelaxFrame(IRBuilderFrame):
+ """The base ir_builder frame for the relax dialect."""
+
+
+@_register_object("script.ir_builder.relax.SeqExprFrame")
+class SeqExprFrame(RelaxFrame):
+ ...
+
+
+@_register_object("script.ir_builder.relax.FunctionFrame")
+class FunctionFrame(SeqExprFrame):
+ """The ir_builder frame for the relax function."""
+
+
+@_register_object("script.ir_builder.relax.BlockFrame")
+class BlockFrame(RelaxFrame):
+ """The ir_builder frame for relax binding blocks."""
+
+
+@_register_object("script.ir_builder.relax.IfFrame")
+class IfFrame(RelaxFrame):
+ ...
+
+
+@_register_object("script.ir_builder.relax.ThenFrame")
+class ThenFrame(SeqExprFrame):
+ ...
+
+
+@_register_object("script.ir_builder.relax.ElseFrame")
+class ElseFrame(SeqExprFrame):
+ ...
diff --git a/python/tvm/script/ir_builder/relax/ir.py b/python/tvm/script/ir_builder/relax/ir.py
new file mode 100644
index 0000000000..647ef8f25a
--- /dev/null
+++ b/python/tvm/script/ir_builder/relax/ir.py
@@ -0,0 +1,407 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=redefined-builtin, wrong-import-order, no-member, invalid-name
+"""IRBuilder for Relax dialect"""
+
+import builtins
+import functools
+import inspect
+from typing import Any, Dict, List, Optional, Tuple, Union
+
+import tvm
+from tvm import DataType, relax
+from tvm.ir import PrimExpr
+from tvm.relax import Call, Expr, ExternFunc, TupleGetItem, Var, const
+
+############################### Operators ###############################
+from tvm.relax.op import (
+ add,
+ assert_op,
+ call_builtin_with_ctx,
+ call_tir,
+ invoke_closure,
+ make_closure,
+ multiply,
+ null_value,
+ print,
+ shape_of,
+)
+from tvm.relax.struct_info import StructInfo
+from tvm.relax.utils import args_converter
+from tvm.runtime import Object as tvm_Object
+from tvm.runtime import ObjectGeneric
+
+from . import _ffi_api, frame
+
+##################### Python Native Function Alias ######################
+
+py_print = builtins.print
+py_tuple = tuple
+py_str = str
+
+
+############################### Function ################################
+
+
+def function() -> frame.FunctionFrame:
+ """Start a function frame.
+ Returns
+ -------
+ frame: FunctionFrame
+ The constructed function frame.
+ """
+ return _ffi_api.Function() # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def arg(name: py_str, struct_info: StructInfo) -> Var:
+ """Add a parameter to the last function frame.
+ Parameters
+ ----------
+ name: str
+ The name of the parameter.
+ struct_info: StructInfo
+ The Struct Info of the parameter
+
+ Returns
+ -------
+ var: Var
+ The created function parameter var.
+ """
+
+ return _ffi_api.Arg(name, struct_info) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def func_name(name: py_str) -> None:
+ """Specify the name of the last function frame.
+ Parameters
+ ----------
+ name: str
+ The function name.
+ """
+ return _ffi_api.FuncName(name) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def func_attr(attrs: Dict[py_str, tvm_Object]) -> None:
+ """Specify the attrs of the last function frame.
+ Parameters
+ ----------
+ attrs: Dict[str, Object]
+ The function attrs.
+ """
+ return _ffi_api.FuncAttrs(attrs) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def func_ret_struct_info(ret_sinfo: StructInfo) -> None:
+ """Specify the return struct info of the last function frame.
+ Parameters
+ ----------
+ ret_type: StructInfo
+ The function return struct info.
+ """
+ return _ffi_api.FuncRetStructInfo(ret_sinfo) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def func_ret_value(value: Expr) -> None:
+ """Specify the return value of the last function frame.
+ Parameters
+ ----------
+ value: Expr
+ The function return value.
+ """
+ return _ffi_api.FuncRetValue(value) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+############################# BindingBlock ##############################
+
+
+def dataflow() -> frame.BlockFrame:
+ """Start a dataflow binding block frame.
+ Returns
+ -------
+ frame: frame.BlockFrame
+ The created ir_builder Block frame.
+ """
+ return _ffi_api.Dataflow() # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def output(*vars: Tuple[Var]) -> None:
+ """Expose the dataflow block output variables as global ones.
+ Parameters
+ ----------
+ vars: Tuple[Var]
+ The output variables of a dataflow block.
+ """
+ return _ffi_api.DataflowBlockOutput(vars) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+################################## Ops #################################
+
+
+@args_converter.auto
+def call_packed(
+ func: py_str,
+ *args: Expr,
+ sinfo_args: Union[StructInfo, List[StructInfo]],
+ **kwargs: Any,
+) -> Call:
+ """Create a relax Call, which calls a packed function.
+ Parameters
+ ----------
+ func: str
+ The name of extern function.
+ *args : Expr
+ The arguments.
+ sinfo_args: Union[StructInfo, List[StructInfo]]
+ The list of structure info arguments.
+ kwargs: Expr
+ The keyword arguments.
+
+ Returns
+ -------
+ call: Call
+ The created Relax Call
+ """
+ op = ExternFunc(func)
+ if sinfo_args is None:
+ raise ValueError("R.call_packed is required to have type_args")
+ if isinstance(sinfo_args, py_tuple): # type: ignore
+ sinfo_args = list(sinfo_args)
+ elif not isinstance(sinfo_args, list):
+ sinfo_args = [sinfo_args]
+ for i, sinfo_arg in enumerate(sinfo_args):
+ if callable(sinfo_arg):
+ sinfo_arg = sinfo_arg()
+ # Convert possible StructInfoProxy to StructInfo
+ if isinstance(sinfo_arg, ObjectGeneric):
+ sinfo_arg = sinfo_arg.asobject()
+ sinfo_args[i] = sinfo_arg
+
+ is_default = False
+ if "attrs_type_key" in kwargs:
+ attrs_type_key = kwargs["attrs_type_key"]
+ kwargs.pop("attrs_type_key")
+ else:
+ attrs_type_key = "DictAttrs"
+ is_default = True
+ attrs = None
+ if kwargs or not is_default:
+ attrs = tvm.ir.attrs.make_node(attrs_type_key, **kwargs)
+
+ return Call(op, args, attrs=attrs, sinfo_args=sinfo_args)
+
+
+def _sinfo_arg_wrapper(func):
+ """A wrapper to convert StructInfoProxies to StructInfo for builtin operators with sinfo_args"""
+
+ def _convert_tensor_type(args):
+ if isinstance(args, (list, py_tuple)): # type: ignore
+ new_args = [_convert_tensor_type(x) for x in args]
+ return type(args)(new_args)
+ if isinstance(args, dict):
+ return {_convert_tensor_type(k): _convert_tensor_type(v) for k, v in args.items()}
+ if inspect.isfunction(args):
+ args = args()
+ if isinstance(args, ObjectGeneric):
+ args = args.asobject()
+ return args
+
+ @functools.wraps(func)
+ def wrapped(*args, **kwargs):
+ return func(*_convert_tensor_type(args), **_convert_tensor_type(kwargs))
+
+ return wrapped # type: ignore
+
+
+invoke_closure = _sinfo_arg_wrapper(invoke_closure) # pylint: disable=invalid-name
+
+call_builtin_with_ctx = _sinfo_arg_wrapper(call_builtin_with_ctx) # pylint: disable=invalid-name
+
+############################### Bindings ###############################
+
+
+def emit(value: Expr, annotate_struct_info: Optional[StructInfo] = None) -> Var:
+ """Emit a binding to the last binding block frame.
+ Parameters
+ ----------
+ value: Expr
+ The right side value of the bindings to be emitted.
+
+ annotate_struct_info: Optional[StructInfo]
+ The optional struct info annotation for the emitted value.
+
+ Returns
+ -------
+ var: Var
+ The left side var of the emitted binding.
+ """
+ return _ffi_api.Emit(value, annotate_struct_info) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def emit_match_cast(value: Expr, struct_info: StructInfo) -> Var:
+ """Emit a match_cast binding to the last binding block frame.
+ Parameters
+ ----------
+ value: Expr
+ The value of the MatchCast to be emitted.
+ struct_info: StructInfo
+ The struct_info of the MatchCast to be emitted.
+
+ Returns
+ -------
+ var: Var
+ The left side var of the emitted binding.
+ """
+ return _ffi_api.EmitMatchCast(value, struct_info) # type: ignore
+
+
+############################# If Then Else #############################
+
+
+def If(condition: Expr) -> frame.IfFrame: # pylint: disable=invalid-name
+ """Create an if frame.
+ Parameters
+ ----------
+ condition : Expr
+ The condition of if statement, executes the true branch if the condition is true,
+ otherwise jump into the false branch.
+ Returns
+ -------
+ res : frame.IfFrame
+ The result IfFrame.
+ """
+ return _ffi_api.If(condition) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def Then() -> frame.ThenFrame: # pylint: disable=invalid-name
+ """Create a then frame.
+ Returns
+ -------
+ res : frame.ThenFrame
+ The result ThenFrame.
+ """
+ return _ffi_api.Then() # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def Else() -> frame.ElseFrame: # pylint: disable=invalid-name
+ """Create an else frame.
+ Returns
+ -------
+ res : frame.ElseFrame
+ The result ElseFrame.
+ """
+ return _ffi_api.Else() # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+############################### R.tuple ################################
+
+
+def tuple(*fields: Expr) -> Expr:
+ """Create a tuple expression.
+ Parameters
+ ----------
+ *fields : Expr
+ The fields of the tuple.
+ Returns
+ -------
+ res : Expr
+ The result tuple.
+ """
+ if len(fields) == 0:
+ fields = py_tuple()
+
+ return relax.Tuple(fields) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+############################### PrimValue ##############################
+
+
+def prim_value(value: PrimExpr) -> Expr:
+ """Create a prim value expression.
+ Parameters
+ ----------
+ value : PrimExpr
+ The value of the prim value.
+ Returns
+ -------
+ res : Expr
+ The result prim value.
+ """
+ return relax.PrimValue(value) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def str(value: py_str) -> Expr:
+ """Create a string imm expression.
+ Parameters
+ ----------
+ value : str
+ The value of the str.
+ Returns
+ -------
+ res : Expr
+ The result str.
+ """
+ return relax.StringImm(value) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+def dtype(value: Union[py_str, DataType]) -> Expr:
+ """Create a dtype imm expression.
+ Parameters
+ ----------
+ value : dtype
+ The value of the dtype.
+ Returns
+ -------
+ res : Expr
+ The result dtype.
+ """
+ return relax.DataTypeImm(value) # type: ignore[attr-defined] # pylint: disable=no-member
+
+
+############################### Importer ###############################
+
+__all__ = [
+ "Else",
+ "If",
+ "Then",
+ "TupleGetItem",
+ "add",
+ "arg",
+ "assert_op",
+ "call_packed",
+ "call_tir",
+ "call_builtin_with_ctx",
+ "const",
+ "dataflow",
+ "dtype",
+ "emit",
+ "emit_match_cast",
+ "func_attr",
+ "func_name",
+ "func_ret_struct_info",
+ "func_ret_value",
+ "function",
+ "invoke_closure",
+ "make_closure",
+ "multiply",
+ "null_value",
+ "output",
+ "prim_value",
+ "print",
+ "shape_of",
+ "str",
+ "tuple",
+]
diff --git a/python/tvm/script/parser/__init__.py b/python/tvm/script/parser/__init__.py
index 5161a2601c..678297799e 100644
--- a/python/tvm/script/parser/__init__.py
+++ b/python/tvm/script/parser/__init__.py
@@ -15,7 +15,8 @@
# specific language governing permissions and limitations
# under the Licens.
"""The parser"""
-from . import _core, ir, tir
+from . import _core, ir, tir, relax
from ._core import parse
from .ir import ir_module
from .tir import prim_func
+from .relax import function
diff --git a/python/tvm/script/parser/core/diagnostics.py b/python/tvm/script/parser/core/diagnostics.py
index ad7ae50347..2767a97f60 100644
--- a/python/tvm/script/parser/core/diagnostics.py
+++ b/python/tvm/script/parser/core/diagnostics.py
@@ -220,7 +220,7 @@ class Diagnostics:
level : diagnostics.DiagnosticLevel
The diagnostic level.
"""
- lineno = node.lineno or self.source.start_line
+ lineno = node.lineno or 1
col_offset = node.col_offset or self.source.start_column
end_lineno = node.end_lineno or lineno
end_col_offset = node.end_col_offset or col_offset
diff --git a/python/tvm/script/parser/core/entry.py b/python/tvm/script/parser/core/entry.py
index bf6a118672..a98dba91a0 100644
--- a/python/tvm/script/parser/core/entry.py
+++ b/python/tvm/script/parser/core/entry.py
@@ -41,7 +41,9 @@ def parse(program: Union[doc.AST, Any, str], extra_vars: Dict[str, Any] = None)
The parsed TVMScript program.
"""
if extra_vars is None:
+ import tvm # pylint: disable=import-outside-toplevel
from tvm.script.parser import ir # pylint: disable=import-outside-toplevel
+ from tvm.script.parser import relax # pylint: disable=import-outside-toplevel
from tvm.script.parser import tir # pylint: disable=import-outside-toplevel
extra_vars = {
@@ -49,6 +51,9 @@ def parse(program: Union[doc.AST, Any, str], extra_vars: Dict[str, Any] = None)
"ir": ir,
"T": tir,
"tir": tir,
+ "relax": relax,
+ "R": relax,
+ "tvm": tvm,
}
source = Source(program)
diff --git a/python/tvm/script/parser/core/evaluator.py b/python/tvm/script/parser/core/evaluator.py
index 3a72a3c331..075aedd891 100644
--- a/python/tvm/script/parser/core/evaluator.py
+++ b/python/tvm/script/parser/core/evaluator.py
@@ -203,7 +203,7 @@ class ExprEvaluator:
else:
value = self._eval_expr(node.__class__(**fields))
except Exception as e: # pylint: disable=broad-except,invalid-name
- self.parser.report_error(node, str(e))
+ self.parser.report_error(node, e)
return self._add_intermediate_result(value)
def _eval_lambda(self, node: doc.Lambda) -> Any:
diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py
index 7c699c42ae..105164ed5f 100644
--- a/python/tvm/script/parser/core/parser.py
+++ b/python/tvm/script/parser/core/parser.py
@@ -60,6 +60,10 @@ def _deferred(exit_f: Callable[[], None]):
return context()
+def _do_nothing(*args, **kwargs): # pylint: disable=unused-argument
+ pass
+
+
class VarTableFrame:
"""The variable table frame.
A frame of variable table stores the variables created in one block or scope.
@@ -259,6 +263,17 @@ class Parser(doc.NodeVisitor):
node = self.diag.source.as_ast()
self.visit(node)
+ def get_dispatch_token(self, node: doc.FunctionDef) -> str:
+ if not isinstance(node, doc.FunctionDef):
+ self.report_error(node, "Only can get dispatch token for function.")
+ if not node.decorator_list:
+ self.report_error(node, "Function must be decorated")
+ # TODO: only the last decorator is parsed
+ decorator = self.eval_expr(node.decorator_list[-1])
+ if not hasattr(decorator, "dispatch_token"):
+ self.report_error(node, "The parser does not understand the decorator")
+ return decorator.dispatch_token
+
def with_dispatch_token(self, token: str):
"""Add a new dispatching token as with statement.
@@ -388,6 +403,8 @@ class Parser(doc.NodeVisitor):
# Only take the last line of the error message
if isinstance(err, TVMError):
msg = list(filter(None, str(err).split("\n")))[-1]
+ elif isinstance(err, KeyError):
+ msg = "KeyError: " + str(err)
else:
msg = str(err)
self.diag.error(node, msg)
@@ -457,30 +474,33 @@ class Parser(doc.NodeVisitor):
"""
return _dispatch(self, "tvm_annotation")(self, node)
- def visit_FunctionDef(self, node: doc.FunctionDef) -> Any: # pylint: disable=invalid-name
- """The general function definition visiting method.
+ def visit_FunctionDef(self, node: doc.FunctionDef) -> None: # pylint: disable=invalid-name
+ """The general function definition visit method.
Parameters
----------
node : doc.FunctionDef
- The doc AST function definition node.
-
- Returns
- -------
- res : Any
- The visiting result.
+ The doc FunctionDef node.
"""
- if not node.decorator_list:
- self.report_error(node, "Function must be decorated")
- # TODO: only the last decorator is parsed
- decorator = self.eval_expr(node.decorator_list[-1])
- if not hasattr(decorator, "dispatch_token"):
- self.report_error(node, "The parser does not understand the decorator")
- token = decorator.dispatch_token
+ token = self.get_dispatch_token(node)
+ current_token = self.dispatch_tokens[-1]
func = dispatch.get(token=token, type_name="FunctionDef", default=None)
if func is None:
self.report_error(node, "The parser does not understand the decorator")
+ pre_func = dispatch.get(
+ token=current_token, type_name="pre_token_switch", default=_do_nothing
+ )
+ post_func = dispatch.get(
+ token=current_token, type_name="post_token_switch", default=_do_nothing
+ )
+ pre_func(self, node)
_dispatch_wrapper(func)(self, node)
+ post_func(self, node)
+
+ def visit_tvm_declare_function(self, node: doc.FunctionDef) -> None:
+ token = self.get_dispatch_token(node)
+ with self.with_dispatch_token(token):
+ _dispatch(self, "tvm_declare_function")(self, node)
def visit_ClassDef(self, node: doc.ClassDef) -> Any: # pylint: disable=invalid-name
"""The general class definition visiting method.
diff --git a/python/tvm/script/parser/ir/parser.py b/python/tvm/script/parser/ir/parser.py
index 9532e7e32c..13b3e29859 100644
--- a/python/tvm/script/parser/ir/parser.py
+++ b/python/tvm/script/parser/ir/parser.py
@@ -32,8 +32,12 @@ def _visit_class_def(self: Parser, node: doc.ClassDef) -> None:
node : doc.ClassDef
The doc AST class definition node.
"""
+
with self.var_table.with_frame():
with I.ir_module():
+ for stmt in node.body:
+ if isinstance(stmt, doc.FunctionDef):
+ self.visit_tvm_declare_function(stmt)
with self.with_dispatch_token("ir"):
self.visit_body(node.body)
@@ -64,3 +68,14 @@ def _visit_expr(_self: Parser, _node: doc.Expr) -> None:
node : doc.ClassDef
The doc AST expression node.
"""
+
+
+@dispatch.register(token="default", type_name="Assign")
+def visit_assign(self: Parser, node: doc.Assign) -> None:
+ if len(node.targets) != 1:
+ self.report_error(node, "Consequential assignments like 'a = b = c' are not supported.")
+ lhs = node.targets[0]
+ rhs = self.eval_expr(node.value)
+ self.eval_assign(
+ target=lhs, source=rhs, bind_value=lambda _a, _b, _c, value: value, allow_shadowing=True
+ )
diff --git a/python/tvm/script/parser/relax/__init__.py b/python/tvm/script/parser/relax/__init__.py
index feb8e68340..04f3fea21c 100644
--- a/python/tvm/script/parser/relax/__init__.py
+++ b/python/tvm/script/parser/relax/__init__.py
@@ -15,7 +15,18 @@
# specific language governing permissions and limitations
# under the License.
"""Initial impl of relax parser for sugars"""
-from tvm.relax import TensorStructInfo, ShapeStructInfo
+from ...ir_builder.relax import * # pylint: disable=redefined-builtin
+from ...ir_builder.relax import ir as _relax
+from . import parser as _parser
+from .entry import Callable, Object, Prim, Shape, Tensor, Tuple, function, match_cast
-Tensor = TensorStructInfo
-Shape = ShapeStructInfo
+__all__ = _relax.__all__ + [
+ "Callable",
+ "Object",
+ "Prim",
+ "Shape",
+ "Tensor",
+ "Tuple",
+ "function",
+ "match_cast",
+]
diff --git a/python/tvm/script/parser/relax/entry.py b/python/tvm/script/parser/relax/entry.py
new file mode 100644
index 0000000000..d93f9a2826
--- /dev/null
+++ b/python/tvm/script/parser/relax/entry.py
@@ -0,0 +1,327 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=missing-docstring, invalid-name
+import inspect
+from typing import Any
+from typing import Callable as _Callable
+from typing import Dict, List, Optional, Set, TypeVar, Union
+
+from tvm.relax import (
+ Expr,
+ FuncStructInfo,
+ Function,
+ ObjectStructInfo,
+ PrimStructInfo,
+ ShapeStructInfo,
+ StructInfo,
+ TensorStructInfo,
+ TupleStructInfo,
+)
+from tvm.runtime import ObjectGeneric
+from tvm.tir import PrimExpr
+
+from .._core import parse, utils
+
+FType = TypeVar("FType", bound=_Callable)
+
+############################## R.function ##############################
+
+
+def function(f: FType) -> Union[Function, FType]:
+ if not inspect.isfunction(f):
+ raise TypeError(f"Expect a function, but got: {f}")
+ if utils.is_defined_in_class(inspect.stack(), f):
+ return f
+ return parse(f, utils.inspect_function_capture(f))
+
+
+setattr(function, "dispatch_token", "relax")
+
+
+############################# Struct Info ##############################
+
+
+class StructInfoProxy(ObjectGeneric):
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> StructInfo:
+ raise NotImplementedError()
+
+ def get_symbolic_vars(self) -> Set[str]:
+ return {}
+
+ def asobject(self):
+ return self.as_struct_info(None)
+
+
+############################### R.Tensor ###############################
+
+
+def _eval_shape(expr: Union[str, PrimExpr], dict_globals: Optional[Dict[str, Any]]) -> PrimExpr:
+ if isinstance(expr, str):
+ code = compile(expr, "<string>", "eval")
+ return eval(code, dict_globals or {}) # pylint: disable=eval-used
+ else:
+ return expr
+
+
+class TensorProxy(StructInfoProxy):
+ shape: Optional[List[Union[str, PrimExpr]]]
+ dtype: str
+ ndim: int
+
+ def __init__(
+ self,
+ shape: Optional[List[Union[PrimExpr, str]]] = None,
+ dtype: Optional[str] = None,
+ ndim: int = -1,
+ ) -> None:
+ self.shape = shape
+ self.dtype = dtype
+ self.ndim = ndim
+ super().__init__()
+
+ def get_symbolic_vars(self) -> Set[str]:
+ if self.shape is None:
+ return {}
+ else:
+ return {s for s in self.shape if isinstance(s, str) and s.isidentifier()}
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> TensorStructInfo:
+ if self.shape is None:
+ return TensorStructInfo(None, self.dtype, self.ndim)
+ else:
+ if dict_globals is None and any([isinstance(s, str) for s in self.shape]):
+ raise ValueError(
+ "String-defined shape expr is only allowed when parsing function parameters "
+ "and return annotations for TVMScript."
+ )
+ shape = [_eval_shape(s, dict_globals) for s in self.shape]
+ return TensorStructInfo(shape, self.dtype, self.ndim)
+
+
+def Tensor(
+ shape: Optional[List[Union[PrimExpr, str]]] = None,
+ dtype: Optional[str] = None,
+ ndim: int = -1,
+) -> TensorProxy:
+ # scalar tensor case
+ if shape is not None and len(shape) == 0:
+ shape = []
+ if isinstance(shape, str) and dtype is None:
+ dtype = shape
+ shape = None
+
+ if shape is not None and not isinstance(shape, (tuple, list)):
+ raise ValueError(f"shape must be a list or tuple, but got: {shape}")
+ return TensorProxy(shape, dtype, ndim)
+
+
+############################## R.Callable ##############################
+
+
+class CallableProxy(StructInfoProxy):
+ params: List[StructInfoProxy]
+ ret: StructInfoProxy
+ """Function type.
+
+ A function type consists of a list of type parameters to enable
+ the definition of generic functions,
+ a set of type constraints which we omit for the time being,
+ a sequence of argument types, and a return type.
+
+ Parameters
+ ----------
+ params : List[StructInfoProxy]
+ The argument StructInfoProxy
+
+ ret : StructInfoProxy
+ The return StructInfoProxy.
+
+ """
+
+ def __init__(
+ self,
+ params: Union[StructInfoProxy, List[StructInfoProxy]],
+ ret: StructInfoProxy,
+ ) -> None:
+ if not isinstance(params, (list, tuple)):
+ params = [params]
+ # convert `R.Tensor` to `R.Tensor()`
+ self.params = [param() if callable(param) else param for param in params]
+ self.ret = ret() if callable(ret) else ret
+
+ def get_symbolic_vars(self) -> Set[str]:
+ return set().union(*[p.get_symbolic_vars() for p in self.params])
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> FuncStructInfo:
+ params = [param.as_struct_info(dict_globals) for param in self.params]
+ ret = self.ret.as_struct_info(dict_globals)
+ return FuncStructInfo(params, ret)
+
+
+def Callable(
+ params: Union[StructInfoProxy, List[StructInfoProxy]],
+ ret: StructInfoProxy,
+) -> CallableProxy:
+ return CallableProxy(params, ret)
+
+
+############################### R.Tuple ################################
+
+
+class TupleProxy(StructInfoProxy):
+ fields: List[StructInfoProxy]
+ """The type of tuple values.
+
+ Parameters
+ ----------
+ fields : List[StructInfoProxy]
+ The fields in the tuple
+ """
+
+ def __init__(
+ self,
+ *fields: List[StructInfoProxy],
+ ) -> None:
+ if len(fields) == 1 and isinstance(fields[0], (tuple, list)):
+ fields = fields[0]
+ # convert `R.Tensor` to `R.Tensor()`
+ self.fields = [field() if callable(field) else field for field in fields]
+
+ def get_symbolic_vars(self) -> Set[str]:
+ return set().union(*[f.get_symbolic_vars() for f in self.fields])
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> TupleStructInfo:
+ fields = [field.as_struct_info(dict_globals) for field in self.fields]
+ return TupleStructInfo(fields)
+
+
+def Tuple(*fields: List[StructInfoProxy]) -> TupleProxy:
+ return TupleProxy(*fields)
+
+
+############################### R.Shape ################################
+
+
+class ShapeProxy(StructInfoProxy):
+ values: Optional[List[PrimExpr]]
+ ndim: int
+ """The type of shape values.
+
+ Parameters
+ ----------
+ values : Optional[List[PrimExpr]]
+ The symbolic shape values if known.
+
+ ndim : Optional[int]
+ The size of the shape.
+ """
+
+ def __init__(
+ self,
+ values: Optional[List[PrimExpr]] = None,
+ ndim: int = -1,
+ ) -> None:
+ self.values = values
+ self.ndim = ndim
+
+ def get_symbolic_vars(self) -> Set[str]:
+ if self.values is None:
+ return {}
+ else:
+ return {v for v in self.values if isinstance(v, str) and v.isidentifier()}
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> ShapeStructInfo:
+ values = [_eval_shape(v, dict_globals) for v in self.values] if self.values else None
+ return ShapeStructInfo(values, self.ndim)
+
+
+def Shape(values: Optional[List[PrimExpr]] = None, ndim: int = -1) -> ShapeProxy:
+ return ShapeProxy(values, ndim)
+
+
+############################### R.Object ################################
+
+
+class ObjectProxy(StructInfoProxy):
+ """The proxy fo ObjectStructInfo.
+
+ Parameters
+ ----------
+ values : Optional[List[PrimExpr]]
+ The symbolic shape values if known.
+
+ ndim : Optional[int]
+ The size of the shape.
+ """
+
+ def __init__(self) -> None:
+ pass
+
+ def get_symbolic_vars(self) -> Set[str]:
+ return set()
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> ShapeStructInfo:
+ return ObjectStructInfo()
+
+
+def Object() -> ObjectProxy:
+ return ObjectProxy()
+
+
+################################ R.Prim ################################
+
+
+class PrimProxy(StructInfoProxy):
+ dtype: str
+ """The type of shape values.
+
+ Parameters
+ ----------
+ dtype : str
+ The data type.
+ """
+
+ def __init__(self, dtype: str) -> None:
+ self.dtype = dtype
+
+ def get_symbolic_vars(self) -> Set[str]:
+ return set()
+
+ def as_struct_info(self, dict_globals: Optional[Dict[str, Any]] = None) -> ShapeStructInfo:
+ return PrimStructInfo(self.dtype)
+
+
+def Prim(dtype: str) -> PrimProxy:
+ return PrimProxy(dtype)
+
+
+############################ R.match_cast #############################
+class MatchCastPair:
+ value: Expr
+ struct_info: StructInfo
+
+ def __init__(self, value: Expr, struct_info: StructInfo) -> None:
+ self.value = value
+ self.struct_info = struct_info
+
+
+def match_cast(value: Expr, struct_info: StructInfo):
+ if value is None:
+ raise ValueError("value of match_cast cannot be None")
+ if struct_info is None:
+ raise ValueError("struct_info of match_cast cannot be None")
+ return MatchCastPair(value, struct_info)
diff --git a/python/tvm/script/parser/relax/parser.py b/python/tvm/script/parser/relax/parser.py
new file mode 100644
index 0000000000..ef26ddd6e9
--- /dev/null
+++ b/python/tvm/script/parser/relax/parser.py
@@ -0,0 +1,276 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=missing-docstring
+
+import functools
+import numbers
+from typing import Any, Optional
+
+from tvm import relax, tir
+from tvm.ir import structural_equal
+from tvm.relax import StructInfo
+from tvm.relax.utils import convert_to_expr
+from tvm.script.ir_builder.relax.frame import BlockFrame
+
+from ...ir_builder import ir as I
+from ...ir_builder import relax as R
+from ...ir_builder.base import IRBuilder
+from .._core import Parser, dispatch, doc
+from .entry import MatchCastPair, StructInfoProxy, TupleProxy
+
+
+def bind_assign_value(
+ self: Parser,
+ node: doc.expr,
+ var_name: str,
+ value: Any,
+ anno_sinfo: Optional[StructInfo] = None,
+) -> Any:
+ var_table = self.var_table.get()
+
+ if isinstance(value, tir.Var):
+ if value.name and var_name != value.name:
+ self.report_error(
+ node,
+ "Cannot define TIR variables with different names. The LHS of binding should "
+ "has the same name provided in RHS.",
+ )
+ if var_name in var_table:
+ prev_value = var_table[var_name]
+ if not isinstance(prev_value, tir.Var):
+ self.report_error(
+ node,
+ "Cannot redefine a non-TIR-variable object to a TIR variable. Please "
+ "define the TIR variable with another name.",
+ )
+ if prev_value.dtype != value.dtype:
+ self.report_error(
+ node,
+ "Expected the same dtype for TIR vars "
+ f"but got {value.dtype} vs {prev_value.dtype}",
+ )
+ return prev_value
+ IRBuilder.name(var_name, value)
+ return value
+
+ if isinstance(value, tuple):
+ value = convert_to_expr(value)
+ if isinstance(value, numbers.Number):
+ value = R.const(value)
+
+ if isinstance(value, relax.Expr):
+ var = R.emit(value, anno_sinfo)
+ elif isinstance(value, MatchCastPair):
+ if anno_sinfo is not None and not structural_equal(anno_sinfo, value.struct_info):
+ self.report_error(
+ node, "Cannot specify inconsistent annotation for a match cast pair. "
+ )
+ var = R.emit_match_cast(value.value, value.struct_info)
+ else:
+ raise TypeError(f"Unsupported type {type(value)} in assignment")
+
+ IRBuilder.name(var_name, var)
+ return var
+
+
+def eval_struct_info_proxy(self: Parser, node: doc.expr) -> StructInfoProxy:
+ try:
+ annotation = self.eval_expr(node)
+ if annotation is None:
+ return TupleProxy([])
+ if callable(annotation):
+ annotation = annotation()
+ if isinstance(annotation, StructInfoProxy):
+ return annotation
+ else:
+ raise TypeError(f"Expected StructInfoProxy but got {type(annotation)}.")
+ except Exception as err:
+ self.report_error(node, str(err))
+ raise err
+
+
+def eval_struct_info(self: Parser, node: doc.expr, eval_str: bool = False) -> StructInfo:
+ var_table = self.var_table.get() if eval_str else None
+ try:
+ return eval_struct_info_proxy(self, node).as_struct_info(var_table)
+ except Exception as err:
+ self.report_error(node, str(err))
+ raise err
+
+
+def collect_symbolic_var_from_params(self: Parser, node: doc.FunctionDef) -> None:
+ # Collect symbolic vars from parameters
+ symbolic_vars = set()
+ for arg in node.args.args:
+ if arg.annotation is None:
+ self.report_error(arg, "Type annotation is required for function parameters.")
+ param_sinfo_proxy = eval_struct_info_proxy(self, arg.annotation)
+ symbolic_vars.update(param_sinfo_proxy.get_symbolic_vars())
+
+ # Define symbolic vars to the current var_table frame
+ for var_name in symbolic_vars:
+ self.var_table.add(var_name, tir.Var(var_name, "int64"), allow_shadowing=False)
+
+
+@dispatch.register(token="relax", type_name="FunctionDef")
+def visit_function_def(self: Parser, node: doc.FunctionDef) -> None:
+ with self.var_table.with_frame():
+ with self.with_dispatch_token("relax"):
+ with R.function():
+ R.func_name(node.name)
+ collect_symbolic_var_from_params(self, node)
+
+ if node.returns is not None:
+ ann_sinfo = eval_struct_info(self, node.returns, eval_str=True)
+ R.func_ret_struct_info(ann_sinfo)
+
+ self.visit(node.args)
+ self.visit_body(node.body)
+
+
+@dispatch.register(token="relax", type_name="tvm_declare_function")
+def visit_tvm_declare_function(self: Parser, node: doc.FunctionDef) -> None:
+ with self.var_table.with_frame():
+ collect_symbolic_var_from_params(self, node)
+
+ if node.returns is None:
+ # Use ObjectStructInfo as unknown return type
+ # NOTE: Cannot use VoidStructInfo here because the return type can be refined later.
+ ret_sinfo = relax.ObjectStructInfo()
+ else:
+ ret_sinfo = eval_struct_info(self, node.returns, eval_str=True)
+ params = []
+ params_sinfo = []
+ for arg in node.args.args:
+ if arg.annotation is None:
+ self.report_error(arg, "Type annotation is required for function parameters.")
+ param_sinfo = eval_struct_info(self, arg.annotation, eval_str=True)
+ params_sinfo.append(param_sinfo)
+ params.append(relax.Var(arg.arg, param_sinfo))
+
+ func_signature = relax.Function.create_empty(params, ret_sinfo)
+ global_var = I.decl_function(node.name, func_signature)
+ self.var_table.add(node.name, global_var)
+
+
+@dispatch.register(token="relax", type_name="pre_token_switch")
+def pre_token_switch(self: Parser, node: doc.Expr) -> None: # pylint: disable=unused-argument
+ ir_builder = IRBuilder()
+ ir_builder.__enter__()
+
+
+@dispatch.register(token="relax", type_name="post_token_switch")
+def post_token_switch(self: Parser, node: doc.Expr) -> None:
+ ir_builder = IRBuilder.current()
+ result = ir_builder.get()
+ ir_builder.__exit__(None, None, None)
+ var = R.emit(result)
+ IRBuilder.name(node.name, var)
+ self.var_table.add(node.name, var, allow_shadowing=False)
+
+
+@dispatch.register(token="relax", type_name="Expr")
+def visit_expr_stmt(self: Parser, node: doc.Expr) -> None:
+ value = self.eval_expr(node.value)
+ if value is not None:
+ self.report_error(node, f"Unsupported Expr stmt type {value}.")
+
+
+@dispatch.register(token="relax", type_name="arguments")
+def visit_arguments(self: Parser, node: doc.arguments) -> None:
+ arg: doc.arg
+ for arg in node.args:
+ if arg.annotation is None:
+ self.report_error(arg, "Type annotation is required for function parameters.")
+ param_sinfo = eval_struct_info(self, arg.annotation, eval_str=True)
+ param = R.arg(arg.arg, param_sinfo)
+
+ self.var_table.add(arg.arg, param)
+
+
+@dispatch.register(token="relax", type_name="tvm_annotation")
+def visit_tvm_annotation(self: Parser, node: doc.expr) -> StructInfo:
+ return eval_struct_info(self, node, eval_str=False)
+
+
+@dispatch.register(token="relax", type_name="With")
+def visit_with(self: Parser, node: doc.With) -> None:
+ # Currently only `with R.dataflow()` is supported
+ if len(node.items) != 1:
+ self.report_error(node, "Only one item is allowed.")
+ item = node.items[0]
+ if item.optional_vars is not None:
+ self.report_error(
+ item.context_expr,
+ "Relax syntax doesn't allow binding expressions in `with` to variables",
+ )
+ frame = self.eval_expr(item.context_expr)
+ with self.var_table.with_frame():
+ with frame:
+ self.visit(node.body)
+ if isinstance(frame, BlockFrame) and frame.is_dataflow:
+ output_vars = frame.output_vars
+ for var in output_vars:
+ self.var_table.add(var.name_hint, var, allow_shadowing=True)
+
+
+@dispatch.register(token="relax", type_name="Assign")
+def visit_assign(self: Parser, node: doc.Assign) -> None:
+ if len(node.targets) != 1:
+ self.report_error(node, "Consequential assignments like 'a = b = c' are not supported.")
+ lhs = node.targets[0]
+ rhs = self.eval_expr(node.value)
+ self.eval_assign(
+ target=lhs,
+ source=rhs,
+ bind_value=bind_assign_value,
+ allow_shadowing=True,
+ )
+
+
+@dispatch.register(token="relax", type_name="AnnAssign")
+def visit_ann_assign(self: Parser, node: doc.AnnAssign) -> None:
+ lhs = node.target
+ rhs = self.eval_expr(node.value)
+ anno_sinfo = self.visit_tvm_annotation(node.annotation)
+ self.eval_assign(
+ target=lhs,
+ source=rhs,
+ bind_value=functools.partial(bind_assign_value, anno_sinfo=anno_sinfo),
+ allow_shadowing=True,
+ )
+
+
+@dispatch.register(token="relax", type_name="Return")
+def visit_return(self: Parser, node: doc.Assign) -> None:
+ value = self.eval_expr(node.value)
+ value = convert_to_expr(value)
+ R.func_ret_value(value)
+
+
+@dispatch.register(token="relax", type_name="If")
+def visit_if(self: Parser, node: doc.If) -> None:
+ if node.orelse is None:
+ raise ValueError("Else statements are required for relax dialect.")
+ with R.If(self.eval_expr(node.test)) as if_frame:
+ with self.var_table.with_frame():
+ with R.Then():
+ self.visit_body(node.body)
+ with self.var_table.with_frame():
+ with R.Else():
+ self.visit_body(node.orelse)
+ self.var_table.add(if_frame.var_name, if_frame.var, allow_shadowing=True)
diff --git a/python/tvm/script/parser/tir/entry.py b/python/tvm/script/parser/tir/entry.py
index e7ec7cf886..b38cbb09cf 100644
--- a/python/tvm/script/parser/tir/entry.py
+++ b/python/tvm/script/parser/tir/entry.py
@@ -83,7 +83,7 @@ class BufferProxy:
return self(keys)
if len(keys) >= 2 and not isinstance(keys[1], str):
return self(keys)
- return self(*keys) # pylint: disable=no-member # type: ignore
+ return self(*keys) # type: ignore[attr-defined] # pylint: disable=no-member
class PtrProxy:
@@ -94,7 +94,7 @@ class PtrProxy:
def __call__(self, dtype, storage_scope="global"):
if callable(dtype):
dtype = dtype().dtype
- return ptr(dtype, storage_scope) # pylint: disable=no-member # type: ignore
+ return ptr(dtype, storage_scope) # type: ignore[attr-defined] # pylint: disable=no-member
def __getitem__(self, keys):
if not isinstance(keys, tuple):
diff --git a/python/tvm/script/parser/tir/operation.py b/python/tvm/script/parser/tir/operation.py
index f0c04f47cd..ed8f07a063 100644
--- a/python/tvm/script/parser/tir/operation.py
+++ b/python/tvm/script/parser/tir/operation.py
@@ -46,12 +46,12 @@ def _register_expr_op(ty: Type): # pylint: disable=invalid-name
for i in [0, 1]:
# Case 1. binop
- r(doc.Add, i, tir.Add)
- r(doc.Sub, i, tir.Sub)
- r(doc.Mult, i, tir.Mul)
- r(doc.Div, i, tir.Div)
- r(doc.FloorDiv, i, tir.FloorDiv)
- r(doc.Mod, i, tir.FloorMod)
+ r(doc.Add, i, lambda a, b: a + b)
+ r(doc.Sub, i, lambda a, b: a - b)
+ r(doc.Mult, i, lambda a, b: a * b)
+ r(doc.Div, i, lambda a, b: a / b)
+ r(doc.FloorDiv, i, lambda a, b: a // b)
+ r(doc.Mod, i, lambda a, b: a % b)
r(doc.LShift, i, lambda a, b: a << b)
r(doc.RShift, i, lambda a, b: a >> b)
r(doc.BitOr, i, lambda a, b: a | b)
diff --git a/python/tvm/script/parser/tir/parser.py b/python/tvm/script/parser/tir/parser.py
index 0e74114ba2..07bf052d41 100644
--- a/python/tvm/script/parser/tir/parser.py
+++ b/python/tvm/script/parser/tir/parser.py
@@ -24,6 +24,7 @@ import tvm
from tvm.ir import PrimType
from tvm.tir import Buffer, IterVar, PrimExpr, Var
+from ...ir_builder import ir as I
from ...ir_builder import tir as T
from ...ir_builder.base import IRBuilder
from ...ir_builder.base import IRBuilderFrame as Frame
@@ -471,3 +472,28 @@ def visit_return(self: Parser, node: doc.Return) -> None:
The doc AST return node.
"""
self.report_error(node, "Return is not allowed.")
+
+
+@dispatch.register(token="tir", type_name="tvm_declare_function")
+def visit_tvm_declare_function(self: Parser, node: doc.FunctionDef) -> None:
+ """The function declaration step for tir
+
+ Parameters
+ ----------
+ self : Parser
+ The visiting parser.
+
+ node : doc.Return
+ The doc AST return node.
+ """
+
+ ret_type = None
+ if node.returns is not None:
+ ret_type = self.eval_expr(node.returns)
+ if callable(ret_type):
+ ret_type = PrimType(ret_type().dtype)
+
+ # Only ret_type is needed for func_signature.
+ func_signature = tvm.tir.PrimFunc([], None, ret_type=ret_type)
+ global_var = I.decl_function(node.name, func_signature)
+ self.var_table.add(node.name, global_var)
diff --git a/src/ir/module.cc b/src/ir/module.cc
index 22c6faf3d6..4a09bdaaf7 100644
--- a/src/ir/module.cc
+++ b/src/ir/module.cc
@@ -63,15 +63,25 @@ IRModule::IRModule(tvm::Map<GlobalVar, BaseFunc> functions,
}
bool IRModuleNode::SEqualReduce(const IRModuleNode* other, SEqualReducer equal) const {
- if (functions.size() != other->functions.size()) return false;
if (!equal(this->attrs, other->attrs)) return false;
+
+ if (functions.size() != other->functions.size()) return false;
+ // Update GlobalVar remap
+ for (const auto& gv : this->GetGlobalVars()) {
+ if (!other->ContainGlobalVar(gv->name_hint)) return false;
+ if (!equal.DefEqual(gv, other->GetGlobalVar(gv->name_hint))) return false;
+ }
for (const auto& kv : this->functions) {
- if (!other->ContainGlobalVar(kv.first->name_hint)) return false;
if (!equal(kv.second, other->Lookup(kv.first->name_hint))) return false;
}
+
if (type_definitions.size() != other->type_definitions.size()) return false;
+ // Update GlobalTypeVar remap
+ for (const auto& gtv : this->GetGlobalTypeVars()) {
+ if (!other->ContainGlobalTypeVar(gtv->name_hint)) return false;
+ if (!equal.DefEqual(gtv, other->GetGlobalTypeVar(gtv->name_hint))) return false;
+ }
for (const auto& kv : this->type_definitions) {
- if (!other->ContainGlobalTypeVar(kv.first->name_hint)) return false;
if (!equal(kv.second, other->LookupTypeDef(kv.first->name_hint))) return false;
}
return true;
diff --git a/src/script/ir_builder/ir/frame.cc b/src/script/ir_builder/ir/frame.cc
index a81c56922d..addf129284 100644
--- a/src/script/ir_builder/ir/frame.cc
+++ b/src/script/ir_builder/ir/frame.cc
@@ -26,11 +26,15 @@ namespace ir_builder {
namespace ir {
void IRModuleFrameNode::ExitWithScope() {
- ICHECK_EQ(functions.size(), global_vars.size());
- int n = functions.size();
Map<GlobalVar, BaseFunc> func_map;
- for (int i = 0; i < n; ++i) {
- func_map.Set(global_vars[i], functions[i]);
+ CHECK_EQ(functions.size(), global_var_map.size())
+ << "All functions must be defined in the IRModule. Got " << global_var_map.size()
+ << "declared function(s), but only " << functions.size() << "defined function(s).";
+ for (const auto& kv : functions) {
+ const GlobalVar& gv = kv.first;
+ const BaseFunc& func = kv.second;
+ CHECK(func.defined()) << "ValueError: function " << gv->name_hint << " is not defined";
+ func_map.Set(gv, func);
}
IRBuilder builder = IRBuilder::Current();
ICHECK(!builder->result.defined()) << "ValueError: Builder.result has already been set";
diff --git a/src/script/ir_builder/ir/ir.cc b/src/script/ir_builder/ir/ir.cc
index a8cc452e4f..da2330b577 100644
--- a/src/script/ir_builder/ir/ir.cc
+++ b/src/script/ir_builder/ir/ir.cc
@@ -17,9 +17,12 @@
* under the License.
*/
#include <tvm/ir/module.h>
+#include <tvm/relax/analysis.h>
#include <tvm/runtime/registry.h>
#include <tvm/script/ir_builder/ir/ir.h>
+#include "./utils.h"
+
namespace tvm {
namespace script {
namespace ir_builder {
@@ -27,12 +30,48 @@ namespace ir {
IRModuleFrame IRModule() {
ObjectPtr<IRModuleFrameNode> n = make_object<IRModuleFrameNode>();
- n->global_vars.clear();
+ n->global_var_map.clear();
n->functions.clear();
return IRModuleFrame(n);
}
+GlobalVar DeclFunction(const String& func_name, const BaseFunc& func_signature) {
+ IRModuleFrame frame = FindModuleFrame("I.DeclFunction");
+ CHECK(!frame->global_var_map.count(func_name))
+ << "ValueError: function " << func_name << " already exists";
+ GlobalVar gv = GlobalVar(func_name);
+ if (func_signature->struct_info_.defined()) {
+ gv->struct_info_ = tvm::relax::GetStructInfo(func_signature);
+ } else if (const auto* prim_func = func_signature.as<tvm::tir::PrimFuncNode>()) {
+ gv->struct_info_ =
+ tvm::relax::FuncStructInfo::OpaqueFunc(tvm::relax::StructInfoFromType(prim_func->ret_type));
+ } else {
+ LOG(FATAL) << "Unsupported function type: " << func_signature->GetTypeKey();
+ }
+ CHECK(frame->functions.find(gv) == frame->functions.end())
+ << "ValueError: function " << func_name << " has already been defined.";
+ frame->global_var_map.Set(func_name, gv);
+ if (func_signature.defined()) {
+ frame->functions.Set(gv, func_signature);
+ }
+ return gv;
+}
+
+void DefFunction(const String& func_name, const BaseFunc& func) {
+ IRModuleFrame frame = FindModuleFrame("I.DefFunction");
+ auto it = frame->global_var_map.find(func_name);
+ CHECK(it != frame->global_var_map.end())
+ << "ValueError: function " << func_name << " does not exist, please declare it first.";
+ const GlobalVar& gv = (*it).second;
+ frame->functions.Set(gv, func);
+ if (func->checked_type_.defined()) {
+ gv->checked_type_ = func->checked_type_;
+ }
+}
+
TVM_REGISTER_GLOBAL("script.ir_builder.ir.IRModule").set_body_typed(IRModule);
+TVM_REGISTER_GLOBAL("script.ir_builder.ir.DeclFunction").set_body_typed(DeclFunction);
+TVM_REGISTER_GLOBAL("script.ir_builder.ir.DefFunction").set_body_typed(DefFunction);
} // namespace ir
} // namespace ir_builder
diff --git a/src/script/ir_builder/ir/frame.cc b/src/script/ir_builder/ir/utils.h
similarity index 59%
copy from src/script/ir_builder/ir/frame.cc
copy to src/script/ir_builder/ir/utils.h
index a81c56922d..58d5e53f70 100644
--- a/src/script/ir_builder/ir/frame.cc
+++ b/src/script/ir_builder/ir/utils.h
@@ -16,8 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
-#include <tvm/ir/module.h>
-#include <tvm/runtime/registry.h>
+#ifndef TVM_SCRIPT_IR_BUILDER_IR_UTILS_H_
+#define TVM_SCRIPT_IR_BUILDER_IR_UTILS_H_
+
#include <tvm/script/ir_builder/ir/frame.h>
namespace tvm {
@@ -25,21 +26,24 @@ namespace script {
namespace ir_builder {
namespace ir {
-void IRModuleFrameNode::ExitWithScope() {
- ICHECK_EQ(functions.size(), global_vars.size());
- int n = functions.size();
- Map<GlobalVar, BaseFunc> func_map;
- for (int i = 0; i < n; ++i) {
- func_map.Set(global_vars[i], functions[i]);
- }
+inline IRModuleFrame FindModuleFrame(const String& method) {
IRBuilder builder = IRBuilder::Current();
- ICHECK(!builder->result.defined()) << "ValueError: Builder.result has already been set";
- builder->result = tvm::IRModule(func_map);
+ if (Optional<IRModuleFrame> frame = builder->FindFrame<IRModuleFrame>()) {
+ const Optional<IRModuleFrame>& last_module_frame = builder->GetLastFrame<IRModuleFrame>();
+ if (last_module_frame.defined() && last_module_frame.value() == frame) {
+ return frame.value();
+ }
+ } else {
+ LOG(FATAL) << "ValueError: IRModule frame not find. Please ensure '" << method
+ << "' is called under I.ir_module()";
+ }
+ LOG(FATAL) << "ValueError: '" << method << "' must be called immediately under I.ir_module()";
+ throw;
}
-TVM_REGISTER_NODE_TYPE(IRModuleFrameNode);
-
} // namespace ir
} // namespace ir_builder
} // namespace script
} // namespace tvm
+
+#endif // TVM_SCRIPT_IR_BUILDER_IR_UTILS_H_
diff --git a/src/script/ir_builder/relax/frame.cc b/src/script/ir_builder/relax/frame.cc
new file mode 100644
index 0000000000..c78b9e73c5
--- /dev/null
+++ b/src/script/ir_builder/relax/frame.cc
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/script/ir_builder/relax/frame.h>
+#include <tvm/script/ir_builder/relax/ir.h>
+
+#include "./utils.h"
+
+namespace tvm {
+namespace script {
+namespace ir_builder {
+namespace relax {
+
+void SeqExprFrameNode::ExitWithScope() {
+ // At this moment, there should be at most one BlockFrame which hasn't ended. In this case, call
+ // its `ExitBlockFrame` and check if there is any more unended BlockFrame.
+ if (Optional<BlockFrame> block_frame = IRBuilder::Current()->GetLastFrame<BlockFrame>()) {
+ block_frame.value()->ExitWithScope();
+ ICHECK(!IRBuilder::Current()->GetLastFrame<BlockFrame>().defined())
+ << "ValueError: There is some remaining BlockFrame that is not properly popped out.";
+ }
+ RelaxFrameNode::ExitWithScope();
+}
+
+void SeqExprFrameNode::EnterWithScope() {
+ RelaxFrameNode::EnterWithScope();
+ BindingBlock()->EnterWithScope();
+}
+
+void FunctionFrameNode::ExitWithScope() {
+ using ir::IRModuleFrame;
+ using tvm::relax::Expr;
+ IRBuilder builder = IRBuilder::Current();
+ SeqExprFrameNode::ExitWithScope();
+ // Step 1: Create the function.
+ CHECK(output.defined()) << "ValueError: A Relax function must have a return value. Please use "
+ "`return` to return an Expr";
+ this->block_builder->BeginScope(params);
+ Expr body = this->block_builder->Normalize(tvm::relax::SeqExpr(binding_blocks, output.value()));
+ auto dict_attrs = attrs.empty() ? NullValue<DictAttrs>() : DictAttrs(attrs);
+ this->block_builder->EndScope();
+ tvm::relax::Function func(/*params=*/params,
+ /*body=*/body,
+ /*ret_struct_info=*/ret_struct_info,
+ /*attrs=*/dict_attrs);
+ // Step 2: Update IRModule.
+ if (builder->frames.empty()) {
+ // Case 0. No outer frame, return function directly
+ ICHECK(!builder->result.defined()) << "ValueError: Builder.result has already been set";
+ builder->result = func;
+ } else if (Optional<IRModuleFrame> opt_frame = builder->FindFrame<IRModuleFrame>()) {
+ // Case 1. A global function of an IRModule
+ CHECK(name.defined()) << "ValueError: The function name must be defined before exiting the "
+ "function scope, if it's defined in a Module";
+ const IRModuleFrame& frame = opt_frame.value();
+ const String& func_name = name.value_or("");
+ if (!frame->global_var_map.count(func_name)) {
+ // First time visiting the function.
+ ir::DeclFunction(func_name, func);
+ }
+ // Define the function.
+ // Note we do checks to disallow redefinition of functions inside the `DefFunction`.
+ ir::DefFunction(func_name, func);
+ } else {
+ LOG(FATAL) << "ValueError: Cannot find where to insert Relax.Function";
+ }
+}
+
+void BlockFrameNode::EnterWithScope() {
+ // Step 1. If the last frame is a block frame. The start of a new block frame marks the end of the
+ // last block frame.
+ Optional<BlockFrame> block_frame = IRBuilder::Current()->GetLastFrame<BlockFrame>();
+ if (block_frame.defined()) {
+ block_frame.value()->ExitWithScope();
+ // Block frames cannot appear consecutively.
+ ICHECK(!IRBuilder::Current()->GetLastFrame<BlockFrame>());
+ }
+ // Step 2. Deal with the new block frame.
+ RelaxFrameNode::EnterWithScope();
+ Optional<FunctionFrame> func_frame = IRBuilder::Current()->FindFrame<FunctionFrame>();
+ CHECK(func_frame.defined())
+ << "ValueError: Cannot find FunctionFrame when creating BindingBlocks, Please ensure "
+ "creating the block under Relax function scope.";
+ const tvm::relax::BlockBuilder& block_builder = func_frame.value()->block_builder;
+ if (is_dataflow) {
+ block_builder->BeginDataflowBlock();
+ } else {
+ block_builder->BeginBindingBlock();
+ }
+}
+
+class DataflowBlockRewriter : public tvm::relax::ExprMutator {
+ public:
+ static tvm::relax::DataflowBlock Rewrite(const tvm::relax::DataflowBlock& block,
+ const Array<tvm::relax::Var>& output_vars) {
+ DataflowBlockRewriter rewriter(output_vars);
+ return Downcast<tvm::relax::DataflowBlock>(rewriter.VisitBindingBlock(block));
+ }
+
+ private:
+ explicit DataflowBlockRewriter(const Array<tvm::relax::Var>& output_vars) {
+ for (const tvm::relax::Var& var : output_vars) {
+ output_var_set_.insert(var.get());
+ }
+ }
+
+ tvm::relax::Var VisitVarDef_(const tvm::relax::DataflowVarNode* op) final {
+ auto it = output_var_set_.find(op);
+ if (it != output_var_set_.end()) {
+ // Rewrite dataflow vars to global vars
+ auto n = make_object<tvm::relax::VarNode>(*op);
+ tvm::relax::Var new_var(n);
+ this->var_remap_[op->vid] = new_var;
+ return new_var;
+ } else {
+ return GetRef<tvm::relax::Var>(op);
+ }
+ }
+
+ private:
+ std::unordered_set<const tvm::relax::VarNode*> output_var_set_;
+};
+
+void BlockFrameNode::ExitWithScope() {
+ // Step 1. Pop the current frame out of the frame stack.
+ RelaxFrameNode::ExitWithScope();
+
+ // Step 2. Get the constructed binding block from the block builder. The block should have at
+ // lease one binding - otherwise, the block is not supposed to be created.
+ const tvm::relax::BlockBuilder& block_builder = GetBlockBuilder();
+ tvm::relax::BindingBlock block = block_builder->EndBlock();
+ if (block->bindings.empty()) {
+ return;
+ }
+
+ // Step 3. Rewrite the dataflow block.
+ if (is_dataflow) {
+ // Step 3.1. Rewrite block binding
+ block = DataflowBlockRewriter::Rewrite(Downcast<tvm::relax::DataflowBlock>(block), output_vars);
+
+ // Step 3.2. Collect global vars' reference in bindings
+ Map<tvm::relax::Id, tvm::relax::Var> new_global_vars;
+ for (const tvm::relax::Binding& binding : block->bindings) {
+ if (!binding->var->IsInstance<tvm::relax::DataflowVarNode>()) {
+ new_global_vars.Set(binding->var->vid, binding->var);
+ }
+ }
+
+ // Step 3.3. Rewrite output vars
+ Array<tvm::relax::Var> new_output_vars;
+ for (const auto& var : output_vars) {
+ auto it = new_global_vars.find(var->vid);
+ ICHECK(it != new_global_vars.end());
+ new_output_vars.push_back((*it).second);
+ }
+ output_vars = std::move(new_output_vars);
+ }
+
+ // Step 3. Get the last frame from the IRBuilder frame stack.
+ Optional<RelaxFrame> opt_last_frame = IRBuilder::Current()->GetLastFrame<RelaxFrame>();
+ ICHECK(opt_last_frame.defined());
+ RelaxFrame last_frame = opt_last_frame.value();
+
+ // Step 4. Since we popped out any possible block frame when entering the "with" scope of the
+ // current frame, the last frame cannot be a block frame.
+ ICHECK(!last_frame->IsInstance<BlockFrameNode>());
+
+ // Step 5. Push the block frame into the corresponding field of the last frame.
+ if (const auto* seq_frame = last_frame.as<SeqExprFrameNode>()) {
+ ICHECK(!seq_frame->output.defined())
+ << "The function is not expected to have output values when emitting blocks.";
+ auto frame = GetRef<SeqExprFrame>(seq_frame);
+ frame->binding_blocks.push_back(block);
+ } else {
+ LOG(FATAL) << "ValueError: Currently the last frame is supposed to be either a function frame "
+ "or a block frame. However, the last frame is \""
+ << last_frame->GetTypeKey() << "\".";
+ }
+
+ // Step 6. Start another binding block when a dataflow block ended.
+ if (is_dataflow) {
+ BindingBlock()->EnterWithScope();
+ }
+}
+
+void IfFrameNode::EnterWithScope() {
+ const Array<IRBuilderFrame>& frames = IRBuilder::Current()->frames;
+ for (const IRBuilderFrame& frame : frames) {
+ const auto* block_frame = frame.as<BlockFrameNode>();
+ if (block_frame && block_frame->is_dataflow) {
+ LOG(FATAL) << "ValueError: Cannot create an IfFrame inside a dataflow block.";
+ }
+ }
+ RelaxFrameNode::EnterWithScope();
+}
+
+void IfFrameNode::ExitWithScope() {
+ RelaxFrameNode::ExitWithScope();
+ CHECK(then_expr.defined())
+ << "ValueError: The body of then part is expected to be defined before exiting.";
+ CHECK(then_expr.defined())
+ << "ValueError: The body of else part is expected to be defined before exiting.";
+ auto body = tvm::relax::If(condition, then_expr.value(), else_expr.value());
+ var = Emit(body);
+ IRBuilder::Name(var_name, var);
+}
+
+void ThenFrameNode::EnterWithScope() {
+ IfFrame frame = FindIfFrame("R.Then");
+ CHECK(!frame->then_expr.defined())
+ << "ValueError: Duplicate then branch declaration, previous one is "
+ << frame->then_expr.value();
+ SeqExprFrameNode::EnterWithScope();
+}
+
+void ThenFrameNode::ExitWithScope() {
+ SeqExprFrameNode::ExitWithScope();
+ String var_name;
+ output = GetSeqExprForBranch(GetRef<ThenFrame>(this), &var_name);
+ IfFrame frame = FindIfFrame("R.Then");
+ frame->then_expr = output;
+ frame->var_name = var_name;
+}
+
+void ElseFrameNode::EnterWithScope() {
+ IfFrame frame = FindIfFrame("R.Else");
+ CHECK(frame->then_expr.defined()) << "The else branch should follow then branch";
+ CHECK(!frame->else_expr.defined())
+ << "ValueError: Duplicate else branch declaration, previous one is "
+ << frame->else_expr.value();
+ SeqExprFrameNode::EnterWithScope();
+}
+
+void ElseFrameNode::ExitWithScope() {
+ SeqExprFrameNode::ExitWithScope();
+ String var_name;
+ output = GetSeqExprForBranch(GetRef<ElseFrame>(this), &var_name);
+ IfFrame frame = FindIfFrame("R.Else");
+ frame->else_expr = output;
+ CHECK(frame->var_name == var_name)
+ << "This last binding of both branches must have the same variable.";
+}
+
+TVM_REGISTER_NODE_TYPE(FunctionFrameNode);
+TVM_REGISTER_NODE_TYPE(SeqExprFrameNode);
+TVM_REGISTER_NODE_TYPE(BlockFrameNode);
+TVM_REGISTER_NODE_TYPE(IfFrameNode);
+TVM_REGISTER_NODE_TYPE(ThenFrameNode);
+TVM_REGISTER_NODE_TYPE(ElseFrameNode);
+
+} // namespace relax
+} // namespace ir_builder
+} // namespace script
+} // namespace tvm
diff --git a/src/script/ir_builder/relax/ir.cc b/src/script/ir_builder/relax/ir.cc
new file mode 100644
index 0000000000..ece645243c
--- /dev/null
+++ b/src/script/ir_builder/relax/ir.cc
@@ -0,0 +1,236 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/script/ir_builder/relax/ir.h>
+#include <tvm/tir/op.h>
+
+#include "./utils.h"
+
+namespace tvm {
+namespace script {
+namespace ir_builder {
+namespace relax {
+
+///////////////////////////////// Vars //////////////////////////////////
+
+using tvm::script::ir_builder::details::Namer;
+
+TVM_STATIC_IR_FUNCTOR(Namer, vtable)
+ .set_dispatch<tvm::relax::VarNode>([](const ObjectRef& node, String name) -> void {
+ using tvm::relax::VarNode;
+ using tvm::relax::IdNode;
+ const VarNode* var = node.as<VarNode>();
+ IdNode* vid = const_cast<IdNode*>(var->vid.get());
+ vid->name_hint = name;
+ });
+
+TVM_STATIC_IR_FUNCTOR(Namer, vtable)
+ .set_dispatch<tvm::relax::DataflowVarNode>([](const ObjectRef& node, String name) -> void {
+ using tvm::relax::DataflowVarNode;
+ using tvm::relax::IdNode;
+ const DataflowVarNode* var = node.as<DataflowVarNode>();
+ IdNode* vid = const_cast<IdNode*>(var->vid.get());
+ vid->name_hint = name;
+ });
+
+/////////////////////////////// Function ////////////////////////////////
+
+FunctionFrame Function() {
+ ObjectPtr<FunctionFrameNode> n = make_object<FunctionFrameNode>();
+ const IRBuilder& ir_builder = IRBuilder::Current();
+ Optional<tvm::IRModule> mod = NullOpt;
+ if (const Optional<ir::IRModuleFrame> mod_frame = ir_builder->GetLastFrame<ir::IRModuleFrame>()) {
+ mod = tvm::IRModule(mod_frame.value()->functions);
+ }
+ n->block_builder = tvm::relax::BlockBuilder::Create(/*mod=*/mod);
+ return FunctionFrame(n);
+}
+
+tvm::relax::Var Arg(const String& name, const tvm::relax::StructInfo& struct_info) {
+ FunctionFrame frame = FindFunctionFrame("R.Arg");
+ tvm::relax::Var var(name, struct_info);
+ frame->params.push_back(var);
+ return var;
+}
+
+void FuncName(const String& name) {
+ FunctionFrame frame = FindFunctionFrame("R.func_name");
+ if (frame->name.defined()) {
+ LOG(FATAL) << "ValueError: Duplicate function name, previous one is: \"" << frame->name.value()
+ << "\"";
+ }
+ frame->name = name;
+}
+
+void FuncAttrs(Map<String, ObjectRef> attrs) {
+ FunctionFrame frame = FindFunctionFrame("R.func_attr");
+ if (!frame->attrs.empty()) {
+ LOG(FATAL) << "ValueError: Duplicate function attrs, previous one is:\n" << frame->attrs;
+ }
+ frame->attrs = attrs;
+}
+
+void FuncRetStructInfo(const tvm::relax::StructInfo& ret_sinfo) {
+ FunctionFrame frame = FindFunctionFrame("R.func_ret_struct_info");
+ if (frame->ret_struct_info.defined()) {
+ LOG(FATAL) << "ValueError: Duplicate function return struct info, previous one is:\n "
+ << frame->ret_struct_info.value();
+ }
+ frame->ret_struct_info = ret_sinfo;
+}
+
+void FuncRetValue(const tvm::relax::Expr& value) {
+ // Step 0. Normalize the value.
+ const tvm::relax::BlockBuilder& block_builder = GetBlockBuilder();
+ tvm::relax::Expr normalized_value = block_builder->Normalize(value);
+
+ // Step 1. The current Relax TVMScript syntax only allows function return appearing at the end of
+ // a function body. Therefore if there is any unended block frame when dealing with function
+ // return, we should end the block frame.
+ Optional<BlockFrame> block_frame = IRBuilder::Current()->GetLastFrame<BlockFrame>();
+ if (block_frame.defined()) {
+ block_frame.value()->ExitWithScope();
+ ICHECK(!IRBuilder::Current()->FindFrame<BlockFrame>())
+ << "All block frame are supposed to be popped out already";
+ }
+ // Step 2. Add the output value to the function frame.
+ FunctionFrame frame = FindFunctionFrame("return");
+ CHECK(!frame->output.defined())
+ << "ValueError: Relax functions don't support multiple return statement. Please make sure "
+ "the return statement appears at the end of function.";
+
+ frame->output = std::move(normalized_value);
+}
+
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Function").set_body_typed(Function);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Arg").set_body_typed(Arg);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.FuncName").set_body_typed(FuncName);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.FuncAttrs").set_body_typed(FuncAttrs);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.FuncRetStructInfo").set_body_typed(FuncRetStructInfo);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.FuncRetValue").set_body_typed(FuncRetValue);
+
+///////////////////////////// BindingBlock //////////////////////////////
+
+BlockFrame Dataflow() {
+ ObjectPtr<BlockFrameNode> n = make_object<BlockFrameNode>();
+ n->is_dataflow = true;
+ n->block_ended = false;
+ return BlockFrame(n);
+}
+
+BlockFrame BindingBlock() {
+ ObjectPtr<BlockFrameNode> n = make_object<BlockFrameNode>();
+ n->is_dataflow = false;
+ n->block_ended = false;
+ return BlockFrame(n);
+}
+
+void DataflowBlockOutput(const Array<tvm::relax::Var>& vars) {
+ // Step 1. Check that we're in a Dataflow block that is not ended.
+ Optional<BlockFrame> block_frame = IRBuilder::Current()->GetLastFrame<BlockFrame>();
+ CHECK(block_frame.defined() && block_frame.value()->is_dataflow)
+ << "ValueError: `R.output` should appear inside a dataflow block. However, the current "
+ "innermost block is not a dataflow block.";
+ CHECK(!block_frame.value()->block_ended)
+ << "ValueError: It is not allowed for a dataflow block to have multiple output operation.";
+
+ // Step 2. Mark the block frame ended of construction, so that any followup binding after this
+ // mark in the dataflow block will lead to an error.
+ block_frame.value()->block_ended = true;
+
+ // Step 3. All the output variables must be global variables and must be emitted by this dataflow
+ // block.
+ const Array<tvm::relax::Var>& emitted_vars = block_frame.value()->emitted_vars;
+ for (const tvm::relax::Var& var : vars) {
+ CHECK(std::find(emitted_vars.begin(), emitted_vars.end(), var) != emitted_vars.end())
+ << "ValueError: An output variable is not emitted by this dataflow block. Please make sure "
+ "all dataflow block output variables are emitted exactly by this block.";
+ block_frame.value()->output_vars.push_back(var);
+ }
+}
+
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Dataflow").set_body_typed(Dataflow);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.BindingBlock").set_body_typed(BindingBlock);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.DataflowBlockOutput")
+ .set_body_typed(DataflowBlockOutput);
+
+/////////////////////////////// Bindings ///////////////////////////////
+
+tvm::relax::Var Emit(const tvm::relax::Expr& expr,
+ const Optional<tvm::relax::StructInfo>& annotate_struct_info) {
+ using tvm::relax::GetStructInfo;
+ BlockFrame block_frame = CheckBlockFrameExistAndUnended();
+ const tvm::relax::BlockBuilder& block_builder = GetBlockBuilder();
+ if (annotate_struct_info.defined()) {
+ const auto& sinfo = annotate_struct_info.value();
+ if (!expr->struct_info_.defined()) {
+ UpdateStructInfo(expr, sinfo);
+ } else {
+ CHECK(StructInfoBaseCheck(sinfo, GetStructInfo(expr)) != tvm::relax::BaseCheckResult::kFailL0)
+ << "Invalid annotation. Got rhs value struct info: " << GetStructInfo(expr)
+ << ", given struct info: " << sinfo;
+ }
+ }
+ tvm::relax::Var var = block_builder->Emit(expr);
+ block_frame->emitted_vars.push_back(var);
+ return var;
+}
+
+tvm::relax::Var EmitMatchCast(const tvm::relax::Expr& value,
+ const tvm::relax::StructInfo& struct_info) {
+ BlockFrame block_frame = CheckBlockFrameExistAndUnended();
+ const tvm::relax::BlockBuilder& block_builder = GetBlockBuilder();
+
+ tvm::relax::Var var = block_builder->EmitMatchCast(value, struct_info);
+ block_frame->emitted_vars.push_back(var);
+ return var;
+}
+
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Emit").set_body_typed(Emit);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.EmitMatchCast").set_body_typed(EmitMatchCast);
+
+///////////////////////////// If Then Else /////////////////////////////
+
+IfFrame If(tvm::relax::Expr condition) {
+ ObjectPtr<IfFrameNode> n = make_object<IfFrameNode>();
+ n->condition = condition;
+ n->then_expr = NullOpt;
+ n->else_expr = NullOpt;
+ return IfFrame(n);
+}
+
+ThenFrame Then() {
+ ObjectPtr<ThenFrameNode> n = make_object<ThenFrameNode>();
+ return ThenFrame(n);
+}
+
+ElseFrame Else() {
+ ObjectPtr<ElseFrameNode> n = make_object<ElseFrameNode>();
+ return ElseFrame(n);
+}
+
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.If").set_body_typed(If);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Then").set_body_typed(Then);
+TVM_REGISTER_GLOBAL("script.ir_builder.relax.Else").set_body_typed(Else);
+
+} // namespace relax
+} // namespace ir_builder
+} // namespace script
+} // namespace tvm
diff --git a/src/script/ir_builder/relax/utils.h b/src/script/ir_builder/relax/utils.h
new file mode 100644
index 0000000000..ae91d05769
--- /dev/null
+++ b/src/script/ir_builder/relax/utils.h
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_SCRIPT_IR_BUILDER_RELAX_UTILS_H_
+#define TVM_SCRIPT_IR_BUILDER_RELAX_UTILS_H_
+
+#include <tvm/relax/struct_info_functor.h>
+#include <tvm/script/ir_builder/relax/frame.h>
+#include <tvm/script/ir_builder/relax/ir.h>
+
+#include <string>
+
+namespace tvm {
+namespace script {
+namespace ir_builder {
+namespace relax {
+
+inline FunctionFrame FindFunctionFrame(const String& method) {
+ if (Optional<FunctionFrame> frame = IRBuilder::Current()->FindFrame<FunctionFrame>()) {
+ return frame.value();
+ }
+ LOG(FATAL) << "ValueError: Function frame not find. Please ensure '" << method
+ << "' is called under R.function()";
+ throw;
+}
+
+inline IfFrame FindIfFrame(const String& method) {
+ if (Optional<IfFrame> frame = IRBuilder::Current()->GetLastFrame<IfFrame>()) {
+ return frame.value();
+ } else {
+ LOG(FATAL) << "ValueError: IfThenElse frame not find. Please ensure '" << method
+ << "' is called under R.if_()";
+ }
+ throw;
+}
+
+inline tvm::relax::BlockBuilder GetBlockBuilder() {
+ Optional<FunctionFrame> frame = IRBuilder::Current()->FindFrame<FunctionFrame>();
+ CHECK(frame.defined()) << "ValueError: Relax Function frame not find. Please ensure "
+ "assignment is called under R.function()";
+ return frame.value()->block_builder;
+}
+
+inline BlockFrame CheckBlockFrameExistAndUnended() {
+ // We check if the current block is "ended" - if a block is ended, it is not allowed to emit new
+ // bindings into this block, and we should throw exceptions.
+
+ Optional<BlockFrame> block_frame = IRBuilder::Current()->GetLastFrame<BlockFrame>();
+ CHECK(block_frame.defined()) << "ValueError: Block frame not find";
+ CHECK(!block_frame.value()->block_ended)
+ << "ValueError: New binding is not allowed after dataflow block output.";
+ return block_frame.value();
+}
+
+inline tvm::relax::SeqExpr GetSeqExprForBranch(const SeqExprFrame& frame, String* var_name) {
+ // Step 0. Check frame type
+ std::string method;
+ if (frame->IsInstance<ThenFrameNode>()) {
+ method = "R.Then";
+ } else if (frame->IsInstance<ElseFrameNode>()) {
+ method = "R.Else";
+ } else {
+ ICHECK(false) << "TypeError: Unsupported frame type: " << frame->GetTypeKey();
+ }
+
+ // Step 1. Check non-empty block and last binding is non-dataflow
+ CHECK(!frame->binding_blocks.empty())
+ << "Empty body is not allowed for '" << method << "' statements.";
+ const tvm::relax::BindingBlock& last_block = frame->binding_blocks.back();
+ CHECK(!last_block->bindings.empty()) << "Blocks are expected to be non-empty.";
+
+ // Step 2. Collect body from the last binding.
+ tvm::relax::Expr body;
+ const tvm::relax::Binding& last_binding = last_block->bindings.back();
+ if (const auto* var_binding = last_binding.as<tvm::relax::VarBindingNode>()) {
+ CHECK(!var_binding->var->IsInstance<tvm::relax::DataflowVarNode>())
+ << "A non-dataflow var is expected in the last binding of '" << method << "'.";
+ body = var_binding->value;
+ *var_name = var_binding->var->name_hint();
+ } else if (const auto* match_cast = last_binding.as<tvm::relax::MatchCastNode>()) {
+ CHECK(!match_cast->var->IsInstance<tvm::relax::DataflowVarNode>())
+ << "A non-dataflow var is expected in the last binding of '" << method << "'.";
+ body = var_binding->value;
+ *var_name = match_cast->var->name_hint();
+ } else {
+ ICHECK(false) << "TypeError: Unsupported binding type: " << last_binding->GetTypeKey();
+ }
+
+ // Step 3. Re-collect binding blocks to remove the last binding.
+ Array<tvm::relax::BindingBlock> new_blocks(frame->binding_blocks.begin(),
+ frame->binding_blocks.end() - 1);
+ Array<tvm::relax::Binding> last_block_bindings(last_block->bindings.begin(),
+ last_block->bindings.end() - 1);
+ new_blocks.push_back(tvm::relax::BindingBlock(last_block_bindings));
+
+ return tvm::relax::SeqExpr(new_blocks, body);
+}
+
+} // namespace relax
+} // namespace ir_builder
+} // namespace script
+} // namespace tvm
+
+#endif // TVM_SCRIPT_IR_BUILDER_RELAX_UTILS_H_
diff --git a/src/script/ir_builder/tir/frame.cc b/src/script/ir_builder/tir/frame.cc
index 1e63201a40..dd8d3c2ed3 100644
--- a/src/script/ir_builder/tir/frame.cc
+++ b/src/script/ir_builder/tir/frame.cc
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+#include <tvm/script/ir_builder/ir/ir.h>
#include <tvm/script/ir_builder/tir/frame.h>
#include <tvm/tir/function.h>
@@ -41,9 +42,17 @@ void PrimFuncFrameNode::ExitWithScope() {
ICHECK(!builder->result.defined()) << "ValueError: Builder.result has already been set";
builder->result = func;
} else if (Optional<ir::IRModuleFrame> opt_frame = builder->FindFrame<ir::IRModuleFrame>()) {
- ir::IRModuleFrame frame = opt_frame.value();
- frame->global_vars.push_back(GlobalVar(name.value_or("")));
- frame->functions.push_back(func);
+ CHECK(name.defined()) << "ValueError: The function name must be defined before exiting the "
+ "function scope, if it's defined in a Module";
+ const ir::IRModuleFrame& frame = opt_frame.value();
+ const String& func_name = name.value_or("");
+ if (!frame->global_var_map.count(func_name)) {
+ // Case. First time visiting the function.
+ ir::DeclFunction(func_name, func);
+ }
+ // Define the function.
+ // Note we do checks to disallow redefinition of functions inside the `DefFunction`.
+ ir::DefFunction(func_name, func);
} else {
LOG(FATAL) << "ValueError: Cannot find where to insert PrimFunc";
}
diff --git a/src/script/ir_builder/tir/utils.h b/src/script/ir_builder/tir/utils.h
index 733c975fad..80a92f923c 100644
--- a/src/script/ir_builder/tir/utils.h
+++ b/src/script/ir_builder/tir/utils.h
@@ -80,7 +80,7 @@ inline PrimFuncFrame FindPrimFuncFrame(const String& method) {
* \return The top frame of BlockFrame.
*/
inline BlockFrame FindBlockFrame(const String& method) {
- if (Optional<BlockFrame> frame = IRBuilder::Current()->GetLastFrame<BlockFrame>()) {
+ if (Optional<BlockFrame> frame = IRBuilder::Current()->FindFrame<BlockFrame>()) {
return frame.value();
}
LOG(FATAL) << "ValueError: Block frame not find. Please ensure '" << method
diff --git a/tests/python/relax/test_tvmscript_ir_builder.py b/tests/python/relax/test_tvmscript_ir_builder.py
new file mode 100644
index 0000000000..12d8b114b8
--- /dev/null
+++ b/tests/python/relax/test_tvmscript_ir_builder.py
@@ -0,0 +1,153 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import tvm
+import tvm.testing
+from tvm import relax, tir
+from tvm.script.ir_builder import relax as R
+from tvm.script.ir_builder.base import IRBuilder
+
+
+def test_function_simple():
+ """
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ out = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ return out
+ """
+ # create with Script IRBuilder
+ with IRBuilder() as ir_builder:
+ with R.function():
+ R.func_name("foo")
+ R.func_attr({"Primitive": 1})
+ x = R.arg("x", relax.TensorStructInfo((128, 128), "float32"))
+ R.func_ret_struct_info(relax.TensorStructInfo(dtype="float32", ndim=2))
+ out = R.emit(
+ R.call_tir("extern_func", x, relax.TensorStructInfo((128, 128), dtype="float32"))
+ )
+ IRBuilder.name("out", out)
+ R.func_ret_value(out)
+ func = ir_builder.get()
+ # create with BlockBuilder
+ x = relax.Var("x", relax.TensorStructInfo((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,), attrs={"Primitive": 1}):
+ out = bb.emit(
+ relax.call_tir("extern_func", x, relax.TensorStructInfo((128, 128), dtype="float32"))
+ )
+ bb.emit_func_output(out)
+ mod = bb.get()
+
+ tvm.ir.assert_structural_equal(func, mod["foo"])
+ # check names
+ assert func.params[0].name_hint == "x"
+ assert func.body.body.name_hint == "out"
+
+
+def test_match_cast():
+ """
+ @R.function
+ def foo(x: R.Tensor(None, "float32"), y: R.Tensor(None, "float32")):
+ m = T.var("int64")
+ n = T.var("int64")
+ _ = R.match_cast(x, R.Tensor((m,), "float32"))
+ y1 = R.match_cast(x, R.Tensor((n,), "float32"))
+ return (m, n * 2)
+ """
+ # create with Script IRBuilder
+ with IRBuilder() as ir_builder:
+ with R.function():
+ R.func_name("foo")
+ x = R.arg("x", relax.TensorStructInfo(ndim=-1, dtype="float32"))
+ y = R.arg("y", relax.TensorStructInfo(ndim=-1, dtype="float32"))
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ _ = R.emit_match_cast(x, relax.TensorStructInfo((m,), "float32"))
+ y1 = R.emit_match_cast(y, relax.TensorStructInfo((n,), "float32"))
+ IRBuilder.name("y1", y1)
+ R.func_ret_value(relax.ShapeExpr([m, n * 2]))
+ func = ir_builder.get()
+
+ # create with BlockBuilder
+ x = relax.Var("x", relax.TensorStructInfo(dtype="float32", ndim=-1))
+ y = relax.Var("y", relax.TensorStructInfo(dtype="float32", ndim=-1))
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x, y)):
+ _ = bb.match_cast(x, relax.TensorStructInfo((m,), "float32"))
+ y1 = bb.match_cast(y, relax.TensorStructInfo((n,), "float32"))
+ bb.emit_func_output(relax.ShapeExpr([m, n * 2]))
+ mod = bb.get()
+
+ tvm.ir.assert_structural_equal(func, mod["foo"])
+
+
+def test_dataflow_block():
+ """
+ @R.function
+ def foo(x: Tensor((128, 128), "float32")) -> Tensor(None, "float32", ndim = 2):
+ # block 0
+ with R.dataflow():
+ lv0 = R.call_tir("extern_func", (x,), R.Tensor((128, 128), dtype="float32"))
+ gv: Tensor((128, 128), "float32") = lv0
+ R.output(gv)
+ return gv
+ """
+ # create with Script IRBuilder
+ with IRBuilder() as ir_builder:
+ with R.function():
+ R.func_name("foo")
+ x = R.arg("x", relax.TensorStructInfo((128, 128), "float32"))
+ with R.dataflow() as df:
+ lv0 = R.emit(
+ R.call_tir(
+ "extern_func", x, relax.TensorStructInfo((128, 128), dtype="float32")
+ )
+ )
+ IRBuilder.name("lv0", lv0)
+ gv = R.emit(lv0)
+ IRBuilder.name("gv", gv)
+ R.output(gv)
+ (gv,) = df.output_vars
+ R.func_ret_value(gv)
+ func = ir_builder.get()
+
+ # create with BlockBuilder
+ x = relax.Var("x", relax.TensorStructInfo((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ with bb.dataflow():
+ lv0 = bb.emit(
+ relax.call_tir(
+ "extern_func", x, relax.TensorStructInfo((128, 128), dtype="float32")
+ )
+ )
+ gv = bb.emit_output(lv0)
+ bb.emit_func_output(gv)
+
+ tvm.ir.assert_structural_equal(func, bb.get()["foo"])
+
+
+def test_regression_py_print():
+ # Test that the py_print directs to python builtin print
+ from tvm.script.ir_builder.relax.ir import py_print # pylint: disable=import-outside-toplevel
+
+ assert py_print == print
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
diff --git a/tests/python/relax/test_tvmscript_parser.py b/tests/python/relax/test_tvmscript_parser.py
new file mode 100644
index 0000000000..ca2da7ae58
--- /dev/null
+++ b/tests/python/relax/test_tvmscript_parser.py
@@ -0,0 +1,1079 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Optional, Union
+
+import pytest
+import tvm
+import tvm.script
+import tvm.testing
+from tvm import IRModule, relax, tir, topi
+from tvm.relax import DynTensorType
+from tvm.script import ir as I
+from tvm.script import relax as R
+from tvm.script import tir as T
+
+
+def _check(
+ parsed: Union[relax.Function, IRModule],
+ expect: Optional[Union[relax.Function, IRModule]] = None,
+):
+ # TODO(relax-team): enable roundtrip testing when printer is ready
+ # test = parsed.script(show_meta=True)
+ # roundtrip_mod = tvm.script.parse(test)
+ # tvm.ir.assert_structural_equal(parsed, roundtrip_mod)
+ if expect:
+ tvm.ir.assert_structural_equal(parsed, expect)
+
+
+def test_simple_func():
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor((128, 128), "float32"):
+ R.func_attr({"Primitive": 1})
+ gv0 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ return gv0
+
+ x = relax.Var("x", R.Tensor((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,), attrs={"Primitive": 1}):
+ out = bb.emit(relax.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32")))
+ bb.emit_func_output(out)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_error_report():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ # error: a = b = c is not allowed.
+ gv0 = gv1 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ return gv0
+
+
+def test_mismatch_cast_dims_and_ndim():
+ with pytest.raises(Exception):
+
+ @R.function
+ def f(
+ x: R.Tensor((2, 3), "float32", ndim=3)
+ ): # error: ndim and the shape dims are mismatch
+ return x
+
+
+def test_unexpected_num_kw_args():
+ with pytest.raises(Exception):
+
+ @R.function
+ def f(x: R.Tensor(dtype="float32", ndim=1, foo=2)): # error: unexpected kw args foo
+ return x
+
+
+def test_unexpected_ndim():
+ with pytest.raises(Exception):
+
+ @R.function
+ # error: dim is expected to be non-negative int or -1 for unknown
+ def f(x: R.Tensor(dtype="float32", ndim=-2)):
+ return x
+
+
+def test_unexpected_ndim_type():
+ with pytest.raises(Exception):
+
+ @R.function
+ def f(x: R.Tensor(dtype="float32", ndim="1")): # error: dim is expected to be int
+ return x
+
+
+def test_unexpected_tir_cast_args():
+
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(x: R.Tensor(("m",), "float32")):
+ m = T.var("int64")
+ # tir.cast expects 2 arguments, but got 3
+ return R.call_tir("foo", (x,), R.Tensor((T.cast("int32", m, 1),), dtype="float32"))
+
+
+def test_unexpected_tir_max_args():
+
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(x: R.Tensor(("m", "n"), "float32")):
+ m = T.var("int64")
+ # tir.max expects 2 arguments, but got 1
+ return relax.call_tir("foo", (x,), R.Tensor((T.max(m),), dtype="float32"))
+
+
+def test_func_type_annotation_fail():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(x, y): # error: the parameter type annotation is missing
+ z = R.add(x, y)
+ y = z
+ return y
+
+
+def test_if_mismatch_var_fail():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")):
+ if cond:
+ w = R.add(x, x)
+ y = R.multiply(w, w)
+ else:
+ w = R.multiply(x, x)
+ z = R.add(w, w) # error: The binding var is expected to `y`
+ return z
+
+
+def test_unassigned_call_fail():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(x: R.Tensor):
+ R.add(x, x)
+ return x
+
+
+def test_simple_module():
+ @I.ir_module
+ class TestModule:
+ @T.prim_func
+ def tir_func(
+ x: T.Buffer((T.int64(128), T.int64(128)), "float32"),
+ y: T.Buffer((T.int64(128), T.int64(128)), "float32"),
+ ):
+ T.func_attr({"tir.noalias": True})
+ for i, j in T.grid(T.int64(128), T.int64(128)):
+ with T.block():
+ vi, vj = T.axis.remap("SS", [i, j])
+ y[vi, vj] = x[vi, vj] + 1.0
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor((128, 128), "float32"):
+ # TODO(Siyuan): Need to change to `TestModule.tir_func`
+ gv0 = R.call_tir(tir_func, x, R.Tensor((128, 128), dtype="float32"))
+ return gv0
+
+ x = relax.Var("x", R.Tensor((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ out = bb.emit_te(lambda x: x + 1, x, primfunc_name_hint="tir_func")
+ bb.emit_func_output(out)
+
+ _check(TestModule, bb.get())
+
+
+def test_relax_tensor_op():
+ @R.function
+ def foo(x: R.Tensor((4, 4), "float32")) -> R.Tensor((4, 4), "float32"):
+ y = R.add(x, x)
+ z = R.multiply(x, y)
+ return z
+
+ x = relax.Var("x", R.Tensor((4, 4), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ y = bb.emit(relax.op.add(x, x))
+ z = bb.emit(relax.op.multiply(x, y))
+ bb.emit_func_output(z)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_symbolic_shape():
+ @R.function
+ def foo(x: R.Tensor(("m", "n"), "float32")) -> R.Tensor(("m", "n"), "float32"):
+ m = T.var("int64", "m")
+ n = T.var("int64", "n")
+ gv0 = R.call_tir("extern_func", x, R.Tensor((m, n), dtype="float32"))
+ return gv0
+
+ @R.function
+ def bar(x: R.Tensor(("m", "n"), "float32")) -> R.Tensor(("m", "n"), "float32"):
+ m = T.var("int64")
+ n = T.var("int64")
+ gv0 = R.call_tir("extern_func", x, R.Tensor((m, n), dtype="float32"))
+ return gv0
+
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def mismatch_dtype(x: R.Tensor(("m", "n"), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ m = T.var("int64")
+ n = T.var("int32") # The shape dtype should be int64
+ gv0 = R.call_tir("extern_func", x, R.Tensor((m, n), dtype="float32"))
+ return gv0
+
+ def _expected(name: str):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = relax.Var("x", R.Tensor([m, n], "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function(name, (x,)):
+ out = bb.emit(relax.call_tir("extern_func", x, R.Tensor((m, n), dtype="float32")))
+ bb.emit_func_output(out)
+ return bb.get()[name]
+
+ _check(foo, _expected("foo"))
+ _check(bar, _expected("bar"))
+
+
+def test_shadowing():
+ @R.function
+ def foo(x: R.Tensor((4, 4), "float32")):
+ y = R.add(x, x)
+ z = R.multiply(x, y)
+ y = R.add(x, y)
+ y = z
+ y = R.multiply(y, x)
+ z = y
+ return z
+
+ x = relax.Var("x", R.Tensor((4, 4), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ y = bb.emit(relax.op.add(x, x))
+ z = bb.emit(relax.op.multiply(x, y))
+ y = bb.emit(relax.op.add(x, y))
+ y = bb.emit(z)
+ y = bb.emit(relax.op.multiply(y, x))
+ z = bb.emit(y)
+ bb.emit_func_output(z)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_match_cast():
+ @R.function
+ def foo(x: R.Tensor("float32"), y: R.Tensor("float32")):
+ m = T.var("int64")
+ n = T.var("int64")
+ x0 = R.match_cast(x, R.Tensor([m], "float32"))
+ with R.dataflow():
+ y0 = R.match_cast(y, R.Tensor([n], "float32"))
+ gv = y0
+ R.output(gv)
+ return (x0, (m, n * 2))
+
+ x = relax.Var("x", R.Tensor("float32"))
+ y = relax.Var("y", R.Tensor("float32"))
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ y2 = relax.Var("y", R.Tensor([n], "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x, y)):
+ x0 = bb.match_cast(x, R.Tensor([m], "float32"))
+ with bb.dataflow():
+ y0 = bb.match_cast(y, R.Tensor([n], "float32"))
+ bb.emit_output(y0)
+ bb.emit_func_output(relax.Tuple([x0, relax.ShapeExpr([m, n * 2])]))
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_tuple_return():
+ @R.function
+ def foo(x: R.Tensor((4, 4), "float32")):
+ gv0 = R.call_tir("extern_func_0", x, R.Tensor((4, 4), dtype="float32"))
+ gv1 = R.call_tir("extern_func_1", x, R.Tensor((4, 4), dtype="float32"))
+ return (gv0, gv1)
+
+ x = relax.Var("x", R.Tensor((4, 4), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ gv0 = bb.emit(relax.call_tir("extern_func_0", x, R.Tensor((4, 4), dtype="float32")))
+ gv1 = bb.emit(relax.call_tir("extern_func_1", x, R.Tensor((4, 4), dtype="float32")))
+ bb.emit_func_output(relax.Tuple((gv0, gv1)))
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_tuple_return_2():
+ @R.function
+ def foo(x: R.Tensor("float32", ndim=2)):
+ n, m = T.var("int64"), T.var("int64")
+ x0 = R.match_cast(x, R.Tensor((n, m), "float32"))
+ return (x0, (n + 1, m, 1))
+
+ x = relax.Var("x", R.Tensor("float32", ndim=2))
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ x0 = bb.match_cast(x, R.Tensor((n, m), "float32"))
+ bb.emit_func_output(relax.Tuple([x0, relax.ShapeExpr([n + 1, m, 1])]))
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_tuple_binding():
+ @R.function
+ def foo(x: R.Tensor("float32", ndim=2)):
+ n, m = T.var("int64"), T.var("int64")
+ x0 = R.match_cast(x, R.Tensor((n, m), "float32"))
+ t0 = (x, x0)
+ t1 = (x, (n, m), t0)
+ return t1
+
+ x = relax.Var("x", R.Tensor("float32", ndim=2))
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ x0 = bb.match_cast(x, R.Tensor((n, m), "float32"))
+ t0 = bb.emit(relax.Tuple([x, x0]))
+ t1 = bb.emit(relax.Tuple([x, relax.ShapeExpr([n, m]), t0]))
+ bb.emit_func_output(t1)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_tuple_get_item():
+ @R.function
+ def foo(x: R.Tensor, y: R.Tensor):
+ t1 = R.tuple(x, y)
+ t2 = (x, y)
+ a = t1[0]
+ b = R.TupleGetItem(t2, 1)
+ c = R.add(a, b)
+ return c
+
+ x = relax.Var("x", R.Tensor())
+ y = relax.Var("y", R.Tensor())
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x, y)):
+ t1 = bb.emit(relax.Tuple([x, y]))
+ t2 = bb.emit(relax.Tuple([x, y]))
+ a = bb.emit(relax.TupleGetItem(t1, 0))
+ b = bb.emit(relax.TupleGetItem(t2, 1))
+ c = bb.emit(relax.op.add(a, b))
+ bb.emit_func_output(c)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_dataflow_block():
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ with R.dataflow():
+ lv0 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ lv1 = R.call_tir("extern_func", lv0, R.Tensor((128, 128), dtype="float32"))
+ gv = lv1
+ R.output(gv)
+ return gv
+
+ x = relax.Var("x", R.Tensor((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ with bb.dataflow():
+ lv0 = bb.emit(relax.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32")))
+ lv1 = bb.emit(relax.call_tir("extern_func", lv0, R.Tensor((128, 128), dtype="float32")))
+ gv = bb.emit_output(lv1)
+ bb.emit_func_output(gv)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_dataflow_block_advanced():
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ gv0 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ gv1 = R.call_tir("extern_func", gv0, R.Tensor((128, 128), dtype="float32"))
+ with R.dataflow():
+ m = T.var("int64")
+ n = T.var("int64")
+ lv0 = R.call_tir("extern_func", gv1, R.Tensor((128, 128), dtype="float32"))
+ lv1 = R.match_cast(lv0, R.Tensor((m, n), "float32"))
+ gv2 = R.call_tir("extern_func", lv0, R.Tensor((128, 128), dtype="float32"))
+ gv2 = R.call_tir("extern_func", gv2, R.Tensor((128, 128), dtype="float32"))
+ gv3 = R.match_cast(gv2, R.Tensor((m, n), "float32"))
+ gv3 = R.match_cast(lv0, R.Tensor((m, n), "float32"))
+ gv4 = gv3
+ gv5 = gv2
+ R.output(gv5, gv4)
+ gv6 = R.call_tir("extern_func", gv5, R.Tensor((128, 128), dtype="float32"))
+ gv7 = R.call_tir("extern_func", gv6, R.Tensor((128, 128), dtype="float32"))
+ return gv7
+
+ x = relax.Var("x", R.Tensor((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ with bb.function("foo", (x,)):
+ gv0 = bb.emit(relax.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32")))
+ gv1 = bb.emit(relax.call_tir("extern_func", gv0, R.Tensor((128, 128), dtype="float32")))
+ with bb.dataflow():
+ lv0 = bb.emit(relax.call_tir("extern_func", gv1, R.Tensor((128, 128), dtype="float32")))
+ lv1 = bb.match_cast(lv0, R.Tensor((m, n), "float32"))
+ gv2 = bb.emit(relax.call_tir("extern_func", lv0, R.Tensor((128, 128), dtype="float32")))
+ gv21 = bb.emit(
+ relax.call_tir("extern_func", gv2, R.Tensor((128, 128), dtype="float32"))
+ )
+ gv3 = bb.match_cast(gv21, R.Tensor((m, n), "float32"))
+ gv31 = bb.match_cast(lv0, R.Tensor((m, n), "float32"))
+ gv32 = bb.emit_output(gv31)
+ gv22 = bb.emit_output(gv21)
+ gv4 = bb.emit(relax.call_tir("extern_func", gv22, R.Tensor((128, 128), dtype="float32")))
+ gv5 = bb.emit(relax.call_tir("extern_func", gv4, R.Tensor((128, 128), dtype="float32")))
+ bb.emit_func_output(gv5)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_dataflow_binding_after_output():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ with R.dataflow():
+ gv = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ R.output(gv)
+ lv = R.call_tir("extern_func", gv, R.Tensor((128, 128), dtype="float32"))
+ return gv
+
+
+def test_dataflow_output_global_var():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ gv0 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ with R.dataflow():
+ gv1 = R.call_tir("extern_func", gv0, R.Tensor((128, 128), dtype="float32"))
+ R.output(gv0, gv1)
+ return gv1
+
+
+def test_dataflow_multiple_output():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ with R.dataflow():
+ gv = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ R.output(gv)
+ R.output(gv)
+ return gv
+
+
+def test_dataflow_output_outside_dataflow_block():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")) -> R.Tensor(None, "float32", ndim=2):
+ gv = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+ R.output(gv)
+ return gv
+
+
+def test_dataflow_scope_fail():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(x: R.Tensor(ndim=2)):
+ with R.dataflow():
+ y = R.add(x, x)
+ z = R.multiply(y, x)
+ w = R.add(z, x)
+ R.output(y, w)
+ t = R.multiply(y, z) # z is not in the outer scope
+ return t
+
+
+def test_return_without_binding():
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")):
+ return x
+
+ x = relax.Var("x", R.Tensor((128, 128), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ bb.emit_func_output(x)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_multiple_return():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")):
+ return x
+ return x
+
+
+def test_function_without_return():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor((128, 128), "float32")):
+ gv0 = R.call_tir("extern_func", x, R.Tensor((128, 128), dtype="float32"))
+
+
+def test_tensor_type_without_args():
+ @R.function
+ def foo(x: R.Tensor((32, 32), "float32")) -> R.Tensor:
+ v = R.call_tir("tir_relu", x, R.Tensor((32, 32), dtype="float32"))
+ return v
+
+ x = relax.Var("x", R.Tensor((32, 32), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x)):
+ v = bb.emit(relax.call_tir("tir_relu", x, R.Tensor((32, 32), dtype="float32")))
+ bb.emit_func_output(v)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_direct_return():
+ @R.function
+ def foo(x: R.Tensor((32, 32), "float32")) -> R.Tensor((32, 32), "float32"):
+ return x
+
+ x = relax.Var("x", R.Tensor((32, 32), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x)):
+ bb.emit_func_output(x)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_call_packed():
+ @R.function
+ def foo(x: R.Tensor((32, 32), "float32")) -> R.Tensor:
+ z = R.call_packed("vm.builtin.copy", x, sinfo_args=R.Tensor((32, 32), "float32"))
+ return z
+
+ x = relax.Var("x", R.Tensor((32, 32), "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x)):
+ z = bb.emit(
+ relax.Call(
+ relax.ExternFunc("vm.builtin.copy"),
+ (x,),
+ None,
+ sinfo_args=[R.Tensor((32, 32), "float32")],
+ )
+ )
+ bb.emit_func_output(z)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_annotation():
+ @R.function
+ def foo(
+ x: R.Tensor((32, "m"), "float32"),
+ y: R.Tensor(("m",), "float32"),
+ r: R.Tensor(dtype="int64"),
+ ) -> R.Object:
+ m = T.var("int64", "m")
+ z: R.Tensor((32, m), "float32") = R.multiply(x, y)
+ w: R.Tensor = R.multiply(z, z)
+ q: R.Tensor(ndim=2) = R.add(w, w)
+ t = R.add(w, z)
+ sh: R.Shape = R.call_packed("shape_of", x, sinfo_args=R.Shape)
+ o: R.Object = R.call_packed("contrib.tensor_array_stack", x, y, sinfo_args=R.Object)
+ return o
+
+ def _check_struct_info(binding, expected_sinfo):
+ tvm.ir.assert_structural_equal(binding.var.struct_info, expected_sinfo)
+ tvm.ir.assert_structural_equal(binding.value.struct_info, expected_sinfo)
+
+ # Cannot use block builder here because we need to check the annotated type,
+ # which may be inconsistent with deduced type.
+ assert isinstance(foo.ret_struct_info, relax.ObjectStructInfo)
+ m = relax.get_shape_of(foo.params[0])[1]
+ bindings = foo.body.blocks[0].bindings
+
+ _check_struct_info(bindings[0], relax.TensorStructInfo([32, m], "float32"))
+ _check_struct_info(bindings[1], relax.TensorStructInfo(dtype="", ndim=-1))
+ _check_struct_info(bindings[2], relax.TensorStructInfo(dtype="", ndim=2))
+ _check_struct_info(bindings[3], relax.TensorStructInfo(dtype="", ndim=-1))
+ _check_struct_info(bindings[4], relax.ShapeStructInfo(ndim=-1))
+ _check_struct_info(bindings[5], relax.ObjectStructInfo())
+
+
+def test_annotate_override():
+ @R.function
+ def foo(x: R.Tensor):
+ y = x
+ # z will be treated as object type even though it's a tensor
+ z: R.Object = R.add(x, y)
+ return z
+
+ assert isinstance(foo.ret_struct_info, relax.ObjectStructInfo)
+ y_bind, z_bind = foo.body.blocks[0].bindings
+ assert isinstance(y_bind.var.struct_info, relax.TensorStructInfo)
+ assert isinstance(z_bind.var.struct_info, relax.ObjectStructInfo)
+
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def test(x: R.Tensor):
+ # Error: x is of Tensor StructInfo, which can not annotate to R.Shape.
+ z: R.Shape = x
+ return z
+
+ @R.function
+ def bar(x: R.Tensor):
+ # x is of Tensor StructInfo, the annotation of `z` is ignored.
+ z: R.Object = x
+ return z
+
+ assert isinstance(bar.ret_struct_info, relax.TensorStructInfo)
+ (z_bind,) = bar.body.blocks[0].bindings
+ assert isinstance(z_bind.var.struct_info, relax.TensorStructInfo)
+
+
+def test_call_tir_empty_shape():
+ @R.function
+ def foo(x: R.Tensor((), "float32")):
+ z = R.call_tir("scalar_add", x, R.Tensor((), dtype="float32"))
+ return z
+
+ (z_bind,) = foo.body.blocks[0].bindings
+ shape_expr = z_bind.value.sinfo_args[0].shape
+
+ assert isinstance(shape_expr, relax.ShapeExpr)
+ assert len(shape_expr.values) == 0
+
+
+def test_call_tir_empty_tuple_arg():
+ bb = relax.BlockBuilder()
+ dummy_param = relax.Var("dummy_param", R.Tensor(()))
+ with bb.function("foo", [dummy_param]):
+ output = bb.emit_te(topi.full, shape=(16, 32), dtype="float32", fill_value=1.0)
+ bb.emit_func_output(output)
+
+ _check(bb.get())
+
+
+def test_call_tir_with_tir_var():
+ @I.ir_module
+ class Module:
+ @R.function
+ def main(
+ dumb_param: R.Tensor(("n",), "float32"), x: R.Tensor(("n * 2", "float32"))
+ ) -> R.Tensor(("n * 2",), "float32"):
+ n = T.var("int64")
+ y = R.call_tir(copy, (x,), R.Tensor(((n * 2,)), dtype="float32"), tir_vars=(n,))
+ return y
+
+ @T.prim_func
+ def copy(var_x: T.handle, var_y: T.handle, n: T.int64):
+ X = T.match_buffer(var_x, (n * 2,), dtype="float32")
+ Y = T.match_buffer(var_y, (n * 2,), dtype="float32")
+ for i in T.grid(n * 2):
+ with T.block("block"):
+ vi = T.axis.remap("S", [i])
+ Y[vi] = X[vi]
+
+ _check(Module)
+
+
+def test_local_function():
+ @R.function
+ def main(
+ x: R.Tensor((2, 3), "float32"), y: R.Tensor((2, 3), "float32")
+ ) -> R.Tensor((2, 3), "float32"):
+ @R.function
+ def outer_func(
+ c1: R.Tensor((2, 3), "float32")
+ ) -> R.Callable((R.Tensor(None, "float32", ndim=2),), R.Tensor(None, "float32", ndim=2)):
+ @R.function
+ def inner_func(x1: R.Tensor((2, 3), "float32")):
+ s: R.Tensor((2, 3), "float32") = R.add(x1, c1)
+ return s
+
+ return inner_func
+
+ in_call = outer_func(x)
+ res = in_call(y)
+ return res
+
+ main_bindings = main.body.blocks[0].bindings
+ assert len(main_bindings) == 3
+ outer_func = main_bindings[0].value
+ assert isinstance(outer_func, relax.Function)
+
+ outer_func_bindings = outer_func.body.blocks[0].bindings
+ assert len(outer_func_bindings) == 1
+ inner_func = outer_func_bindings[0].value
+ assert isinstance(inner_func, relax.Function)
+
+ @I.ir_module
+ class TestModule:
+ @R.function
+ def f(x: R.Tensor((128, 128), "float32"), y: R.Tensor((128, 128), "float32")):
+ @T.prim_func
+ def my_matmul(a: T.handle, b: T.handle, c: T.handle) -> None:
+ A = T.match_buffer(a, (128, 128))
+ B = T.match_buffer(b, (128, 128))
+ C = T.match_buffer(c, (128, 128))
+
+ for i, j, k in T.grid(128, 128, 128):
+ with T.block():
+ vi, vj, vk = T.axis.remap("SSR", [i, j, k])
+ with T.init():
+ C[vi, vj] = 0.0
+ C[vi, vj] += A[vi, vk] * B[vj, vk]
+
+ z = relax.call_tir(my_matmul, (x, y), R.Tensor((128, 128), dtype="float32"))
+ return z
+
+ bindings = TestModule["f"].body.blocks[0].bindings
+ assert len(bindings) == 2
+ tir_func = bindings[0].value
+ assert isinstance(tir_func, tir.PrimFunc)
+
+
+def test_cross_function_call():
+ @I.ir_module
+ class Mod0:
+ @R.function
+ def foo(x: R.Tensor((10, 5), "float32")):
+ s = R.add(x, x)
+ return s
+
+ @R.function
+ def main(x: R.Tensor((10, 5), "float32")):
+ inner = foo
+ gv1 = inner(x)
+ gv2 = foo(x)
+ return (inner, gv1, gv2)
+
+ @I.ir_module
+ class Mod1:
+ @R.function
+ def main(x: R.Tensor((10, 5), "float32")):
+ inner = foo
+ gv1 = inner(x)
+ gv2 = foo(x)
+ return (inner, gv1, gv2)
+
+ @R.function
+ def foo(x: R.Tensor((10, 5), "float32")) -> R.Tensor((10, 5), "float32"):
+ s = R.add(x, x)
+ return s
+
+ with pytest.raises(OSError):
+
+ @I.ir_module
+ class ErrorMod:
+ @R.function
+ def main(x: R.Tensor((10, 5), "float32")):
+ inner = foo
+ gv1 = inner(x)
+ gv2 = foo(x)
+ return (inner, gv1, gv2)
+
+ @R.function
+ def foo(
+ x: R.Tensor((10, 5), "float32")
+ ): # need function ret info since it is parse later than `main`
+ s = R.add(x, x)
+ return s
+
+
+def test_if_branch():
+ @R.function
+ def foo(cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")) -> R.Tensor((1,), "float32"):
+ if cond:
+ w = R.add(x, x)
+ y = R.multiply(w, w)
+ else:
+ w = R.multiply(x, x)
+ y = R.add(w, w)
+ return y
+
+ cond, x = foo.params
+ y_bind = foo.body.blocks[0].bindings[0]
+ y, ite = y_bind.var, y_bind.value
+
+ assert isinstance(y, relax.Var)
+ assert y.name_hint == "y"
+
+ assert isinstance(ite, relax.If)
+ assert isinstance(ite.true_branch, relax.SeqExpr)
+ assert isinstance(ite.false_branch, relax.SeqExpr)
+
+ def check_call(call, op, args):
+ assert isinstance(call, relax.Call)
+ if isinstance(op, str):
+ assert call.op.name == op
+ else:
+ assert call.op == op
+ tvm.ir.assert_structural_equal(call.args, args)
+
+ w_bind = ite.true_branch.blocks[0].bindings[0]
+ # the seq exprts in the branches are normalized to bind any call
+ # in the seq expr "body" to a var
+ y_bind = ite.true_branch.blocks[-1].bindings[-1]
+ assert w_bind.var.name_hint == "w"
+ check_call(w_bind.value, "relax.add", [x, x])
+ check_call(y_bind.value, "relax.multiply", [w_bind.var, w_bind.var])
+
+ w_bind = ite.false_branch.blocks[0].bindings[0]
+ y_bind = ite.false_branch.blocks[-1].bindings[-1]
+ assert w_bind.var.name_hint == "w"
+ check_call(w_bind.value, "relax.multiply", [x, x])
+ check_call(y_bind.value, "relax.add", [w_bind.var, w_bind.var])
+
+
+def test_if_inside_dataflow():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")):
+ with R.dataflow():
+ if cond:
+ w = R.add(x, x)
+ y = R.multiply(w, w)
+ else:
+ w = R.multiply(x, x)
+ y = R.add(w, w)
+ R.output(y)
+ return y
+
+
+def test_var_if_scoping_fail():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def f(cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")):
+ if cond:
+ w = R.add(x, x)
+ y = R.multiply(w, w)
+ else:
+ w = R.multiply(x, x)
+ y = R.add(w, w)
+ return w # error: The w is not defined in the outer scope
+
+
+def test_if_branch_var_scope():
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(cond: R.Tensor((), "bool"), x: R.Tensor((1,), "float32")):
+ if cond:
+ w = R.add(x, x)
+ y = R.multiply(w, w)
+ else:
+ w = R.multiply(x, x)
+ y = R.add(w, w)
+ return w
+
+
+def test_erase_to_well_defined():
+ @R.function
+ def foo(x: R.Tensor):
+ q = x
+ m, n = T.var("int64"), T.var("int64")
+ z = R.match_cast(q, R.Tensor((m, n)))
+ w = z
+ return w
+
+ tvm.ir.assert_structural_equal(foo.ret_struct_info, R.Tensor(ndim=2))
+ _check(foo)
+
+
+def test_empty_tuple():
+ @R.function
+ def foo(x: R.Tuple()):
+ y: R.Tuple() = R.tuple()
+ return y
+
+ x = relax.Var("x", relax.TupleStructInfo([]))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x,)):
+ y = bb.emit(relax.Tuple([]))
+ bb.emit_func_output(y)
+
+ _check(foo, bb.get()["foo"])
+
+
+def test_symbolic_shape_computing():
+ # Tensor Case 1
+ @R.function
+ def foo(x: R.Tensor(("m + 1",), "float32"), y: R.Tensor(("m", 1), "float32")):
+ z = R.add(x, y)
+ return z
+
+ m = tir.Var("m", "int64")
+ x = relax.Var("x", relax.TensorStructInfo([m + 1], "float32"))
+ y = relax.Var("y", relax.TensorStructInfo([m, 1], "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("foo", (x, y)):
+ z = bb.emit(relax.op.add(x, y))
+ bb.emit_func_output(z)
+
+ _check(foo, bb.get()["foo"])
+
+ # Tensor Case 2
+ @R.function
+ def bar(
+ x: R.Tensor(("m",), "float32"), y: R.Tensor(("T.max(m, 20)",), "float32")
+ ) -> R.Tensor(("T.max(m, 20) + 1",), "float32"):
+ m = T.var("int64")
+ z = R.call_tir("test_intrin", (x, y), R.Tensor((T.max(m, 20) + 1,), dtype="float32"))
+ return z
+
+ m = tir.Var("m", "int64")
+ x = relax.Var("x", relax.TensorStructInfo([m], "float32"))
+ y = relax.Var("y", relax.TensorStructInfo([tir.max(m, 20)], "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("bar", (x, y)):
+ z = bb.emit(
+ relax.call_tir("test_intrin", (x, y), R.Tensor((tir.max(m, 20) + 1,), dtype="float32"))
+ )
+ bb.emit_func_output(z)
+
+ _check(bar, bb.get()["bar"])
+
+ # Shape Case
+ @R.function
+ def baz(x: R.Shape(("m",)), y: R.Tensor(("m * 2",), "float32")):
+ m = T.var("int64")
+ z = R.call_tir("test_intrin", y, R.Tensor((m * 2,), dtype="float32"))
+ return z
+
+ m = tir.Var("m", "int64")
+ x = relax.Var("x", relax.ShapeStructInfo([m]))
+ y = relax.Var("y", relax.TensorStructInfo([m * 2], "float32"))
+ bb = relax.BlockBuilder()
+ with bb.function("baz", (x, y)):
+ z = bb.emit(relax.call_tir("test_intrin", (y), R.Tensor((m * 2,), dtype="float32")))
+ bb.emit_func_output(z)
+
+ _check(baz, bb.get()["baz"])
+
+ # Error Case
+ with pytest.raises(tvm.error.DiagnosticError):
+
+ @R.function
+ def foo(x: R.Tensor(("m + 1", "m * 2"), "float32")): # name 'm' is not defined
+ z = R.add(x, x)
+ return z
+
+
+# TODO(relax-team): enable this when vm ops are ready
+@pytest.mark.xfail
+def test_vm_ops():
+ @R.function
+ def foo(x: R.Tensor(("m", "n"), dtype="float32")):
+ m = T.var("int64")
+ n = T.var("int64")
+ storage = R.vm.alloc_storage((4 * m * n,), dtype="float32", runtime_device_index=0)
+ alloc = R.vm.alloc_tensor(storage, (m, n), offset=0, dtype="float32")
+ tensor = R.builtin.alloc_tensor((m, n), dtype="float32", runtime_device_index=0)
+ _ = R.vm.call_tir_dyn("te_func", (x, tensor, (m, n)))
+ gv = tensor
+ return alloc, gv
+
+
+def test_prim_value():
+ @R.function
+ def foo():
+ gv = R.call_packed("test", 1, sinfo_args=R.Tensor((32, 32), "float32"))
+ return gv
+
+ _check(foo)
+
+
+def test_string_imm():
+ @R.function
+ def foo():
+ gv = R.call_packed("test", "hello", sinfo_args=R.Tensor((32, 32), "float32"))
+ return gv
+
+ _check(foo)
+
+
+def test_datatype_imm():
+ @R.function
+ def foo():
+ gv = R.call_packed("test", R.dtype("float32"), sinfo_args=R.Tensor((32, 32), "float32"))
+ return gv
+
+ _check(foo)
+
+
+def test_function_void_return_type():
+ @tvm.script.ir_module
+ class Foo:
+ @R.function
+ def main(x: R.Tensor((3, 3), dtype="float32")):
+ res = mul(x)
+ return res
+
+ @R.function
+ def mul(x: R.Tensor((3, 3), dtype="float32")):
+ res = R.multiply(x, x)
+ return res
+
+ _check(Foo)
+ # Since the return type of function `mul` is not annotated,
+ # the function `main` regards it as a generic return type.
+ assert isinstance(Foo["main"].ret_struct_info, relax.ObjectStructInfo)
+ assert isinstance(Foo["mul"].ret_struct_info, relax.TensorStructInfo)
+
+ @tvm.script.ir_module
+ class Bar:
+ @R.function
+ def main(x1: R.Tensor((3, 3), dtype="float32")):
+ res1 = mul(x1)
+ return res1
+
+ @R.function
+ def mul(x: R.Tensor((3, 3), dtype="float32")) -> None:
+ res = R.multiply(x, x)
+ return res
+
+ # Since the return type of function `mul` is not annotated,
+ # the function `main` regards it as a generic return type.
+ _check(Bar)
+ tvm.ir.assert_structural_equal(Bar["main"].ret_struct_info, relax.TupleStructInfo([]))
+ tvm.ir.assert_structural_equal(Bar["mul"].ret_struct_info, relax.TupleStructInfo([]))
+
+
+def test_class_normalize():
+ @tvm.script.ir_module
+ class InputModule:
+ @R.function
+ def mul_add(x: R.Tensor) -> R.Tensor:
+ return R.multiply(R.add(x, x), R.add(x, x))
+
+ # The parser automatically normalizes the input AST to the following ANF form
+ @tvm.script.ir_module
+ class OutputModule:
+ @R.function
+ def mul_add(x: R.Tensor) -> R.Tensor:
+ gv = R.add(x, x)
+ gv1 = R.add(x, x)
+ return R.multiply(gv, gv1)
+
+ _check(InputModule, OutputModule)
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
[tvm] 01/07: [Unity] Relax VM (#13878)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 486c617bfac25e507447ebd5282de71f9a24ffb8
Author: Yuchen Jin <yu...@cs.washington.edu>
AuthorDate: Wed Feb 1 08:52:16 2023 -0500
[Unity] Relax VM (#13878)
This PR implements a flexible register-based VM to execute relax programs with dynamic shape and control flow. Design: https://github.com/tlc-pack/relax/wiki/Relax-VM-Design.
Co-Authored-by: Ziheng Jiang <zi...@apache.org>
Co-Authored-by: Ruihang Lai <ru...@cs.cmu.edu>
Co-Authored-by: Sunghyun Park <49...@users.noreply.github.com>
Co-Authored-by: Junru Shao <ju...@gmail.com>
Co-Authored-by: Prakalp Srivastava <pr...@octoml.ai>
Co-Authored-by: Yong Wu <yo...@gmail.com>
Co-Authored-by: Steven S. Lyubomirsky <sl...@octoml.ai>
Co-Authored-by: Tianqi Chen <ti...@gmail.com>
Co-Authored-by: Hongyi Jin <32...@qq.com>
---
CMakeLists.txt | 2 +
include/tvm/relax/exec_builder.h | 181 ++++++
include/tvm/runtime/relax_vm/builtin.h | 89 +++
include/tvm/runtime/relax_vm/bytecode.h | 223 +++++++
include/tvm/runtime/relax_vm/executable.h | 209 +++++++
include/tvm/runtime/relax_vm/memory_manager.h | 142 +++++
include/tvm/runtime/relax_vm/vm.h | 148 +++++
python/tvm/relax/__init__.py | 24 +
python/tvm/relax/_ffi_api.py | 20 +
python/tvm/relax/exec_builder.py | 147 +++++
python/tvm/relax/testing/vm.py | 85 +++
python/tvm/relax/vm.py | 609 +++++++++++++++++++
src/relax/backend/vm/exec_builder.cc | 399 +++++++++++++
src/runtime/relax_vm/builtin.cc | 445 ++++++++++++++
src/runtime/relax_vm/bytecode.cc | 68 +++
src/runtime/relax_vm/executable.cc | 576 ++++++++++++++++++
src/runtime/relax_vm/memory_manager.cc | 181 ++++++
src/runtime/relax_vm/naive_allocator.h | 65 +++
src/runtime/relax_vm/pooled_allocator.h | 111 ++++
src/runtime/relax_vm/vm.cc | 811 ++++++++++++++++++++++++++
tests/python/relax/test_vm_execbuilder.py | 262 +++++++++
21 files changed, 4797 insertions(+)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index abe415957e..a354662734 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -289,6 +289,7 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
src/driver/*.cc
src/support/*.cc
src/script/*.cc
+ src/relax/backend/vm/*.cc
)
tvm_file_glob(GLOB CODEGEN_SRCS
@@ -335,6 +336,7 @@ tvm_file_glob(GLOB RUNTIME_SRCS
src/runtime/*.cc
src/runtime/vm/*.cc
src/runtime/minrpc/*.cc
+ src/runtime/relax_vm/*.cc
)
if(BUILD_FOR_HEXAGON)
diff --git a/include/tvm/relax/exec_builder.h b/include/tvm/relax/exec_builder.h
new file mode 100644
index 0000000000..03e58392c2
--- /dev/null
+++ b/include/tvm/relax/exec_builder.h
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/exec_builder.h
+ */
+#ifndef TVM_RELAX_EXEC_BUILDER_H_
+#define TVM_RELAX_EXEC_BUILDER_H_
+
+#include <tvm/ir/expr.h>
+#include <tvm/node/reflection.h>
+#include <tvm/node/repr_printer.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/runtime/relax_vm/bytecode.h>
+#include <tvm/runtime/relax_vm/executable.h>
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace relax {
+
+namespace vm = tvm::runtime::relax_vm;
+
+class ExecBuilder;
+
+/*!
+ * \brief A builder provides api to build VM executable with instructions.
+ */
+class ExecBuilderNode : public Object {
+ public:
+ /*!
+ * \brief Declare a function, it is OK to have multiple declarations.
+ * \param func The function name.
+ * \param kind The kind of the function.
+ */
+ void DeclareFunction(const std::string& func, vm::VMFuncInfo::FuncKind kind);
+ /*!
+ * \brief To annotate the start of a vm function.
+ * \param func The function name.
+ * \param num_inputs The number of inputs.
+ * \param param_names The function parameter names.
+ * \param kind The kind of the function.
+ * \param init_register_size Initial setting of register file size.
+ */
+ void EmitFunction(const std::string& func, int64_t num_inputs,
+ Optional<Array<String>> param_names,
+ vm::VMFuncInfo::FuncKind kind = vm::VMFuncInfo::FuncKind::kVMFunc,
+ int64_t init_register_size = 0);
+ /*!
+ * \brief Annotate the end of a vm function.
+ * \param func The function name.
+ */
+ void EndFunction(const std::string& func);
+ /*!
+ * \brief Emit a call instruction for a packed function.
+ * \param func The packed function name.
+ * \param args The arguments of the function.
+ * \param ret The return register.
+ */
+ void EmitCall(const std::string& func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
+ /*!
+ * \brief Emit a call instruction with func as argument.
+ * \param func The packed function index.
+ * \param args The arguments of the function.
+ * \param ret The return register.
+ */
+ void EmitCall(vm::Instruction::Arg func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
+ /*!
+ * \brief Emit a ret instruction.
+ * \param result The return result.
+ * \note result must be a register.
+ */
+ void EmitRet(vm::Instruction::Arg result);
+ /*!
+ * \brief Emit a goto instruction.
+ * \param pc_offset The program counter offset as the jump offset.
+ */
+ void EmitGoto(vm::Index pc_offset);
+ /*!
+ * \brief Emit an If instruction.
+ * \param cond The register containing the cond value.
+ * \param false_offset The program counter offset for the false branch.
+ * \note result must be a register.
+ */
+ void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset);
+ /*!
+ * \brief Get function index by its name.
+ * \param name The name of the function.
+ * \return The argument corresponding to the function index.
+ */
+ vm::Instruction::Arg GetFunction(const std::string& name);
+ /*!
+ * \brief Convert a constant value something that exec builder can understand.
+ *
+ * This function may update the constant pool to include the obj value.
+ *
+ * \param value The input constant value
+ * \return An Arg that represents the result of constant argument.
+ */
+ template <typename T>
+ vm::Instruction::Arg ConvertConstant(T value) {
+ TVMRetValue rv;
+ rv = value;
+ return ConvertConstant_(rv);
+ }
+ /*!
+ * \brief Raw access to underlying executable build in progress.
+ */
+ vm::Executable* exec() const;
+ /*!
+ * \brief Finalize the build, run formalize and get the final result.
+ * \note This function should not be called during construction.
+ */
+ ObjectPtr<vm::Executable> Get();
+ /*!
+ * \brief Create an ExecBuilder.
+ * \return The ExecBuilder.
+ */
+ TVM_DLL static ExecBuilder Create();
+
+ void VisitAttrs(AttrVisitor* v) {}
+
+ static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
+ static constexpr const char* _type_key = "relax.ExecBuilder";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ExecBuilderNode, Object);
+
+ private:
+ /*!
+ * \brief Convert a constant value something that exec builder can understand.
+ *
+ * This function may update the constant pool to include the obj value.
+ *
+ * \param obj The constant value to be emitted
+ * \return An Arg that represents the result of constant argument.
+ */
+ vm::Instruction::Arg ConvertConstant_(TVMRetValue obj);
+
+ /*!
+ * \brief A helper function to check if an executable is legal by checking if registers are used
+ * properly
+ */
+ void CheckExecutable();
+ /*!
+ * \brief Formalize the executable.
+ */
+ void Formalize();
+
+ /*! \brief The mutable internal executable. */
+ ObjectPtr<vm::Executable> exec_; // mutable
+ /*! \brief internal dedup map when creating index for a new constant */
+ std::unordered_map<ObjectRef, vm::Index, StructuralHash, StructuralEqual> const_dedup_map_;
+};
+
+class ExecBuilder : public ObjectRef {
+ public:
+ TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ExecBuilder, ObjectRef, ExecBuilderNode);
+};
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_EXEC_BUILDER_H_
diff --git a/include/tvm/runtime/relax_vm/builtin.h b/include/tvm/runtime/relax_vm/builtin.h
new file mode 100644
index 0000000000..b994e44ae8
--- /dev/null
+++ b/include/tvm/runtime/relax_vm/builtin.h
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/builtin.h
+ * \brief Builtin runtime APIs.
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_BUILTIN_H_
+#define TVM_RUNTIME_RELAX_VM_BUILTIN_H_
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+/*!
+ * \brief Op code used in built-in match-shape function.
+ *
+ * The function takes the following signature:
+
+ * MatchShape(input_shape, shape_heap, n, c[0], r[0], c[1], r[1], ... c[n], r[n], err_ctx)
+ *
+ * This function provides runtime shape population and checking support for match-cast.
+ * When a shape variable appears in the first time, we should load the shape and
+ * populate the variable. When a shape variable already appears, we should
+ * assert that it already equals an existing shape value.
+ *
+ * NOTE: It is OK to pass nullptr shape_heap if all code are AssertEqualToImm.
+ */
+enum class MatchShapeCode : int {
+ /*!
+ * \brief Perform an assertion that shape equals immediate.
+ *
+ * assert input_shape[i] == r[i]
+ */
+ kAssertEqualToImm = 0,
+ /*!
+ * \brief This is the first time we see a symbolic shape variable, store to heap.
+ *
+ * shape_heap[r[i]] = input_shape[i]
+ */
+ kStoreToHeap = 1,
+ /*!
+ * \brief skip and do not do anything.
+ */
+ kNoOp = 2,
+ /*!
+ * \brief Peform an assertion that the shape equals a loaded value.
+ *
+ * assert input_shape[i] == shape_heap[r[i]]
+ */
+ kAssertEqualToLoad = 3,
+};
+
+/*!
+ * \brief Op code used in builtin function MakeShape.
+ *
+ * MakeShape(shape_heap, n, c[0], r[0], c[1], r[1], ... c[n], r[n]).
+ *
+ * \note It is OK to pass nullptr to shape_heap if all code are UseImm.
+ */
+enum class MakeShapeCode : int {
+ /*! \brief Use the following r[i] as immediate shape value. */
+ kUseImm = 0,
+ /*!
+ * \brief Load shape value from the shape_heap[[r[i]].
+ */
+ kLoadShape = 1,
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+#endif // TVM_RUNTIME_RELAX_VM_BUILTIN_H_
diff --git a/include/tvm/runtime/relax_vm/bytecode.h b/include/tvm/runtime/relax_vm/bytecode.h
new file mode 100644
index 0000000000..91d1823258
--- /dev/null
+++ b/include/tvm/runtime/relax_vm/bytecode.h
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/bytecode.h
+ * \brief The bytecode for the virtual machine.
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_BYTECODE_H_
+#define TVM_RUNTIME_RELAX_VM_BYTECODE_H_
+
+#include <tvm/runtime/data_type.h>
+#include <tvm/runtime/logging.h>
+
+#include <iostream>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+/*!
+ * \brief The storage type for the bytecode in the VM.
+ */
+using ExecWord = int64_t;
+
+/*! \brief A register name. */
+using RegName = ExecWord;
+
+/*!
+ * \brief An alias for the integer type used ubiquitously in the VM.
+ */
+using Index = ExecWord;
+
+/*!
+ * \brief An enumeration of Relax's opcodes.
+ *
+ * The opcode is used to implement instruction
+ * as a tagged union.
+ */
+enum class Opcode {
+ Call = 1U,
+ Ret = 2U,
+ Goto = 3U,
+ If = 4U,
+};
+
+/*! \brief A single virtual machine instruction.
+ *
+ * The representation of the instruction is as
+ * a tagged union.
+ *
+ * The first field represents which instruction,
+ * and by extension which field of the union
+ * is active.
+ */
+struct Instruction {
+ /*! \brief The number of bit for storing value. */
+ static constexpr ExecWord kKindBit = 8;
+ /*! \brief The number of bit for storing value. */
+ static constexpr ExecWord kValueBit = sizeof(ExecWord) * 8 - kKindBit;
+ /*! \brief The bit mask of the value part. */
+ static constexpr ExecWord kValueMask = (static_cast<ExecWord>(1) << kValueBit) - 1;
+ /*! \brief Maximum possible value, use 1 bit for sign. */
+ static constexpr ExecWord kValueMaxLimit = (static_cast<ExecWord>(1) << (kValueBit - 1)) - 1;
+ /*! \brief Minimum possible value, remove 1 slot to keep things symmetric. */
+ static constexpr ExecWord kValueMinLimit = -kValueMaxLimit;
+ /*! \brief Begining of special register section. */
+ static constexpr RegName kBeginSpecialReg = static_cast<ExecWord>(1) << 54;
+ /*! \brief Random magic number that represents void argument, indicate null value */
+ static constexpr RegName kVoidRegister = kBeginSpecialReg + 0;
+ /*! \brief Random magic number that represents the VM context */
+ static constexpr RegName kVMRegister = kBeginSpecialReg + 1;
+ /*!
+ * \brief The kind of instruction's argument.
+ */
+ enum class ArgKind : int { kRegister = 0, kImmediate = 1, kConstIdx = 2, kFuncIdx = 3 };
+ /*!
+ * \brief The auxiliary data structure for instruction argument.
+ */
+ class Arg {
+ public:
+ /*! \brief Construct a void argument. */
+ Arg() : data_(Instruction::kVoidRegister) {}
+ /*!
+ * \brief construct Arg from data.
+ * \param data The data repr.
+ */
+ static Arg FromData(ExecWord data) { return Arg(data); }
+ /*!
+ * \brief construct a register Arg.
+ * \param reg The register number.
+ * \return The constructed arg.
+ */
+ static Arg Register(RegName reg) { return Arg(ArgKind::kRegister, reg); }
+ /*!
+ * \brief construct a ConstIdx arg.
+ * \param index The constant index.
+ * \return The constructed arg.
+ */
+ static Arg ConstIdx(Index index) { return Arg(ArgKind::kConstIdx, index); }
+ /*!
+ * \brief construct a immediate arg.
+ * \param imm_value The immediate value.
+ * \return The constructed arg.
+ */
+ static Arg Immediate(int64_t imm_value) { return Arg(ArgKind::kImmediate, imm_value); }
+ /*!
+ * \brief construct a FuncIdx arg.
+ * \param index The func index in the function table.
+ * \return The constructed arg.
+ */
+ static Arg FuncIdx(Index index) { return Arg(ArgKind::kFuncIdx, index); }
+ /*!
+ * \brief Get the kind of argument..
+ * \return The kind of argument.
+ */
+ ArgKind kind() const {
+ uint8_t kind = (data_ >> kValueBit) & 0xFF;
+ return Instruction::ArgKind(kind);
+ }
+ /*!
+ * \brief Get the value of argument.
+ * \return The value of argument.
+ * \note We store both positive and negative values by sign extension.
+ */
+ ExecWord value() const { return ((data_ & kValueMask) << kKindBit) >> kKindBit; }
+ /*!
+ * \brief Get the raw data repr of the arg.
+ * \return The raw data.
+ */
+ ExecWord data() const { return data_; }
+
+ private:
+ /*! \brief Construct from the data. */
+ explicit Arg(ExecWord data) : data_(data) {}
+ /*! \brief Construct from the kind and value. */
+ Arg(ArgKind kind, Index value) {
+ ICHECK_LE(value, kValueMaxLimit);
+ ICHECK_GE(value, kValueMinLimit);
+ data_ = (static_cast<ExecWord>(kind) << kValueBit) | (value & kValueMask);
+ }
+ /*! \brief The underlying stored data. */
+ ExecWord data_;
+ };
+ /*! \brief The instruction opcode. */
+ Opcode op;
+ union {
+ struct /* Call */ {
+ /*! \brief The destination register. */
+ RegName dst;
+ /*! \brief The index into the packed function table. */
+ Index func_idx;
+ /*! \brief The number of arguments to the packed function. */
+ Index num_args;
+ /*! \brief The arguments of the packed function. */
+ Arg* args;
+ };
+ struct /* Ret */ {
+ /*! \brief The return result. */
+ RegName result;
+ };
+ struct /* Goto */ {
+ /*! \brief The jump offset. */
+ Index pc_offset;
+ };
+ struct /* If */ {
+ /*! \brief The register containing the cond value. */
+ RegName cond;
+ /*! \brief The program counter offset for the false branch. */
+ Index false_offset;
+ };
+ };
+ /*!
+ * \brief Construct a Call instruction.
+ * \param func_idx The index of the function to call.
+ * \param num_args The number of arguments.
+ * \param args The input arguments.
+ * \param dst The destination register.
+ * \return The call instruction.
+ */
+ static Instruction Call(Index func_idx, Index num_args, Arg* args, RegName dst);
+ /*!
+ * \brief Construct a return instruction.
+ * \param result The register containing the return value.
+ * \return The return instruction.
+ */
+ static Instruction Ret(RegName result);
+ /*!
+ * \brief Construct a goto instruction.
+ * \param pc_offset The register containing the jump offset.
+ * \return The goto instruction.
+ */
+ static Instruction Goto(RegName pc_offset);
+ /*!
+ * \brief Construct an If instruction.
+ * \param cond The register containing the cond value.
+ * \param false_offset The program counter offset for the false branch.
+ * \return The If instruction.
+ */
+ static Instruction If(RegName cond, Index false_offset);
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+#endif // TVM_RUNTIME_RELAX_VM_BYTECODE_H_
diff --git a/include/tvm/runtime/relax_vm/executable.h b/include/tvm/runtime/relax_vm/executable.h
new file mode 100644
index 0000000000..316a03a3b8
--- /dev/null
+++ b/include/tvm/runtime/relax_vm/executable.h
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/executable.h
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_EXECUTABLE_H_
+#define TVM_RUNTIME_RELAX_VM_EXECUTABLE_H_
+
+#include <tvm/runtime/container/closure.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "./bytecode.h"
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+/*!
+ * \brief Information entry in executable function table.
+ *
+ * Contains metadata about the compiled function, as
+ * well as the compiled VM instructions.
+ */
+struct VMFuncInfo {
+ /*! \brief kind of the function. */
+ enum class FuncKind : int {
+ /*! \brief system level packed function */
+ kPackedFunc = 0,
+ /*! \brief VM function. */
+ kVMFunc = 1,
+ /*! \brief VMTIR function. */
+ kVMTIRFunc = 2,
+ };
+ /*! \brief The kind of function. */
+ FuncKind kind;
+ /*! \brief The function's name, global symbol */
+ std::string name;
+ /*! \brief The start instruction index of the function. */
+ Index start_instr = 0;
+ /*! \brief The end instruction index of the function. */
+ Index end_instr = 0;
+ /*! \brief The number of arguments of the function. */
+ Index num_args = 0;
+ /*! \brief The register file size of the function. */
+ Index register_file_size = 0;
+ /*! \brief The function parameter names.*/
+ std::vector<std::string> param_names;
+
+ // defined customized loader save
+ void Save(dmlc::Stream* writer) const;
+ bool Load(dmlc::Stream* reader);
+};
+
+/*!
+ * \brief The executable emitted by the VM compiler.
+ *
+ * The executable contains information (e.g. data in different memory regions)
+ * to run in a virtual machine.
+ */
+class Executable : public runtime::ModuleNode {
+ public:
+ /*!
+ * \brief Get a PackedFunc from the executable module.
+ * \param name the name of the function.
+ * \param sptr_to_self The shared_ptr that points to this module node.
+ * \return PackedFunc or nullptr when it is not available.
+ */
+ PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final;
+ /*!
+ * \brief Print the detailed statistics of the given code, i.e. number of
+ * globals and constants, etc.
+ * \return The statistics represented by a string.
+ */
+ std::string Stats() const;
+ /*!
+ * \brief Get the i-th instruction from the executable.
+ * \param i The index of the instruction to be fetched.
+ * \return The instruction.
+ */
+ Instruction GetInstruction(Index i) const;
+ /*!
+ * \brief Set j-th byte data of i-th instruction to val.
+ * \param i The index of the instruction to be updated.
+ * \param j The index of the byte data of the instruction to be updated.
+ * \param val The value to be set
+ */
+ void SetInstructionData(Index i, Index j, ExecWord val);
+ /*!
+ * \brief Print the instructions as text format.
+ * \return The text format of the instructions.
+ */
+ String AsText() const;
+ /*!
+ * \brief Print the instructions as python program.
+ * \return The python program of the instructions, represented by a string.
+ */
+ String AsPython() const;
+ /*!
+ * \brief Write the Executable to the binary stream in serialized form.
+ * \param stream The binary stream to save the executable to.
+ */
+ void SaveToBinary(dmlc::Stream* stream) final;
+ /*!
+ * \brief Load Executable from the binary stream in serialized form.
+ * \param stream The binary stream that load the executable from.
+ * \return The loaded executable, in the form of a `runtime::Module`.
+ */
+ static Module LoadFromBinary(void* stream);
+ /*!
+ * \brief Write the Executable to the provided path as a file containing its serialized content.
+ * \param file_name The name of the file to write the serialized data to.
+ * \param format The target format of the saved file.
+ */
+ void SaveToFile(const std::string& file_name, const std::string& format) final;
+ /*!
+ * \brief Load Executable from the file.
+ * \param file_name The path of the file that load the executable from.
+ * \return The loaded executable, in the form of a `runtime::Module`.
+ */
+ static Module LoadFromFile(const std::string& file_name);
+
+ /*! \brief The virtual machine's function table. */
+ std::vector<VMFuncInfo> func_table;
+ /*! \brief A map from globals (as strings) to their index in the function map. */
+ std::unordered_map<std::string, Index> func_map;
+ /*! \brief The global constant pool. */
+ std::vector<TVMRetValue> constants;
+ /*! \brief The offset of instruction. */
+ std::vector<Index> instr_offset;
+ /*! \brief The byte data of instruction. */
+ std::vector<ExecWord> instr_data;
+
+ virtual ~Executable() {}
+
+ const char* type_key() const final { return "relax.Executable"; }
+
+ private:
+ /*!
+ * \brief Save the globals.
+ * \param strm The input stream.
+ */
+ void SaveGlobalSection(dmlc::Stream* strm);
+ /*!
+ * \brief Save the constant pool.
+ * \param strm The input stream.
+ */
+ void SaveConstantSection(dmlc::Stream* strm);
+ /*!
+ * \brief Save the instructions.
+ * \param strm The input stream.
+ */
+ void SaveCodeSection(dmlc::Stream* strm);
+ /*!
+ * \brief Save the packed functions.
+ * \param strm The input stream.
+ */
+ void SavePackedFuncNames(dmlc::Stream* strm);
+ /*!
+ * \brief Load the globals.
+ * \param strm The input stream.
+ */
+ void LoadGlobalSection(dmlc::Stream* strm);
+ /*!
+ * \brief Load the constant pool.
+ * \param strm The input stream.
+ */
+ void LoadConstantSection(dmlc::Stream* strm);
+ /*!
+ * \brief Load the instructions.
+ * \param strm The input stream.
+ */
+ void LoadCodeSection(dmlc::Stream* strm);
+ /*!
+ * \brief Save the packed functions.
+ * \param strm The input stream.
+ */
+ void LoadPackedFuncNames(dmlc::Stream* strm);
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+namespace dmlc {
+DMLC_DECLARE_TRAITS(has_saveload, ::tvm::runtime::relax_vm::VMFuncInfo, true);
+} // namespace dmlc
+#endif // TVM_RUNTIME_RELAX_VM_EXECUTABLE_H_
diff --git a/include/tvm/runtime/relax_vm/memory_manager.h b/include/tvm/runtime/relax_vm/memory_manager.h
new file mode 100644
index 0000000000..e5ae8cfcfb
--- /dev/null
+++ b/include/tvm/runtime/relax_vm/memory_manager.h
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/memory_manager.h
+ * \brief Abstract device memory management API
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_MEMORY_MANAGER_H_
+#define TVM_RUNTIME_RELAX_VM_MEMORY_MANAGER_H_
+
+#include <tvm/runtime/c_runtime_api.h>
+#include <tvm/runtime/ndarray.h>
+
+#include <functional>
+#include <memory>
+#include <mutex>
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+struct Buffer {
+ /*! \brief The pointer to the allocated block of memory. */
+ void* data{nullptr};
+ /*! \brief The size of the block. */
+ size_t size{0};
+ /*! \brief The device of the allocated buffers. */
+ Device device;
+};
+
+enum AllocatorType {
+ kNaive = 1,
+ kPooled,
+};
+
+class Allocator {
+ public:
+ explicit Allocator(AllocatorType type) : type_(type) {}
+ virtual ~Allocator() = default;
+ /*! \brief Allocate an empty NDArray using from the allocator.
+ * \param shape The shape of the NDArray.
+ * \param dtype The datatype of the NDArray.
+ * \param dev The device where the array is allocated.
+ * \return The empty NDArray.
+ */
+ runtime::NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, Device dev);
+ /*! \brief Return the allocator type. */
+ inline AllocatorType type() const { return type_; }
+ /*! \brief Allocate a buffer given a size, alignment and type.
+ * \param nbytes The size of the buffer.
+ * \param alignment The alignment of the buffer.
+ * \param type_hint A type hint to the allocator.
+ * \return A sized allocation in the form of a buffer.
+ */
+ virtual Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) = 0;
+ /*! \brief Free a buffer allocated by the allocator.
+ * \param buffer The buffer to free.
+ */
+ virtual void Free(const Buffer& buffer) = 0;
+
+ private:
+ AllocatorType type_;
+};
+
+class MemoryManager {
+ public:
+ static MemoryManager* Global();
+ /*!
+ * \brief Get or create an allocator given the device and allocator type.
+ * \param dev The TVM device
+ * \param type The allocator type
+ * \return The memory allocator.
+ */
+ static Allocator* GetOrCreateAllocator(Device dev, AllocatorType type);
+ /*!
+ * \brief Get an allocator given the device.
+ * \param dev The TVM device
+ * \return The memory allocator.
+ */
+ static Allocator* GetAllocator(Device dev);
+
+ private:
+ MemoryManager() {}
+
+ private:
+ std::mutex mutex_;
+ std::unordered_map<Device, std::unique_ptr<Allocator>> allocators_;
+};
+
+/*! \brief An object representing a storage allocation. */
+class StorageObj : public Object {
+ public:
+ /*! \brief The index into the VM function table. */
+ Buffer buffer;
+
+ /*! \brief Allocate an NDArray from a given piece of storage. */
+ runtime::NDArray AllocNDArray(uint64_t offset, ShapeTuple shape, DLDataType dtype);
+
+ /*! \brief The deleter for an NDArray when allocated from underlying storage. */
+ static void Deleter(Object* ptr);
+
+ ~StorageObj() {
+ auto alloc = MemoryManager::Global()->GetAllocator(buffer.device);
+ alloc->Free(buffer);
+ }
+
+ static constexpr const uint32_t _type_index = runtime::TypeIndex::kDynamic;
+ static constexpr const char* _type_key = "relax.Storage";
+ TVM_DECLARE_FINAL_OBJECT_INFO(StorageObj, Object);
+};
+
+/*! \brief reference to storage. */
+class Storage : public ObjectRef {
+ public:
+ explicit Storage(Buffer buffer);
+
+ TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Storage, ObjectRef, StorageObj);
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+#endif // TVM_RUNTIME_RELAX_VM_MEMORY_MANAGER_H_
diff --git a/include/tvm/runtime/relax_vm/vm.h b/include/tvm/runtime/relax_vm/vm.h
new file mode 100644
index 0000000000..cfe3880904
--- /dev/null
+++ b/include/tvm/runtime/relax_vm/vm.h
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/vm.h
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_VM_H_
+#define TVM_RUNTIME_RELAX_VM_VM_H_
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "./bytecode.h"
+#include "./executable.h"
+#include "./memory_manager.h"
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+/*!
+ * \brief An object representing a vm closure.
+ */
+class VMClosureObj : public ClosureObj {
+ public:
+ /*!
+ * \brief The function name. The function could be any
+ * function object that is compatible to the VM runtime.
+ */
+ String func_name;
+
+ /*!
+ * \brief The implementation of the Closure.
+ * \note This function takes context pointer(VirtualMachine*)
+ * as the first argument. The rest of arguments follows
+ * the same arguments as the normal function call.
+ */
+ PackedFunc impl;
+
+ static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
+ static constexpr const char* _type_key = "relax.vm.Closure";
+ TVM_DECLARE_FINAL_OBJECT_INFO(VMClosureObj, ClosureObj);
+};
+
+/*! \brief reference to closure. */
+class VMClosure : public Closure {
+ public:
+ VMClosure(String func_name, PackedFunc impl);
+ TVM_DEFINE_OBJECT_REF_METHODS(VMClosure, Closure, VMClosureObj);
+
+ /*!
+ * \brief Create another PackedFunc with last arguments already bound to last_args.
+ *
+ * This is a helper function to create captured closures.
+ * \param func The input func, can be a VMClosure or PackedFunc.
+ * \param last_args The arguments to bound to in the end of the function.
+ * \note The new function takes in arguments and append the last_args in the end.
+ */
+ static PackedFunc BindLastArgs(PackedFunc func, std::vector<TVMRetValue> last_args);
+};
+
+/*!
+ * \brief The virtual machine.
+ *
+ * The virtual machine contains all the current execution state,
+ * as well as the executable.
+ *
+ * The goal is to have a single self-contained object,
+ * enabling one to easily pass around VMs, execute them on
+ * multiple threads, or serialize them to disk or over the
+ * wire.
+ */
+class VirtualMachine : public runtime::ModuleNode {
+ public:
+ /*!
+ * \brief Initialize the virtual machine for a set of devices.
+ * \param devices The set of TVM devices.
+ * \param alloc_types The allocator types for each device.
+ */
+ virtual void Init(const std::vector<Device>& devices,
+ const std::vector<AllocatorType>& alloc_types) = 0;
+ /*!
+ * \brief Load the executable for the virtual machine.
+ * \param exec The executable.
+ */
+ virtual void LoadExecutable(ObjectPtr<Executable> exec) = 0;
+ /*!
+ * \brief Get global function in the VM.
+ * \param func_name The name of the function.
+ * \return The closure
+ */
+ virtual VMClosure GetClosure(const String& func_name) = 0;
+ /*!
+ * \brief Invoke closure or packed function using PackedFunc convention.
+ * \param closure_or_packedfunc A VM closure or a packed_func.
+ * \param args The input arguments.
+ * \param rv The return value.
+ */
+ virtual void InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, TVMArgs args,
+ TVMRetValue* rv) = 0;
+ /*!
+ * \brief Create a specific instance of VM.
+ * \return Created VM
+ */
+ static ObjectPtr<VirtualMachine> Create();
+ /*!
+ * \brief Helper function for vm closure functions to get the context ptr
+ * \param arg The argument value.
+ */
+ static VirtualMachine* GetContextPtr(TVMArgValue arg) {
+ return static_cast<VirtualMachine*>(arg.operator void*());
+ }
+
+ ~VirtualMachine() {}
+
+ const char* type_key() const final { return "relax.VirtualMachine"; }
+
+ //--------------------------------------------------------------------------
+ // The following section contains states that other builtin can depend on
+ //--------------------------------------------------------------------------
+ /*! \brief The memory allocators. */
+ std::vector<Allocator*> allocators;
+ /*! \brief Runtime physical device list. */
+ std::vector<Device> devices;
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+#endif // TVM_RUNTIME_RELAX_VM_VM_H_
diff --git a/python/tvm/relax/__init__.py b/python/tvm/relax/__init__.py
new file mode 100644
index 0000000000..c070fa4791
--- /dev/null
+++ b/python/tvm/relax/__init__.py
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name, wrong-import-position
+"""The Relax IR namespace containing the IR, type, operator, builder, vm, etc."""
+from . import exec_builder
+from . import vm
+
+# VM
+from .exec_builder import ExecBuilder
+from .vm import VirtualMachine
diff --git a/python/tvm/relax/_ffi_api.py b/python/tvm/relax/_ffi_api.py
new file mode 100644
index 0000000000..a127e1c813
--- /dev/null
+++ b/python/tvm/relax/_ffi_api.py
@@ -0,0 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""FFI API for Relax."""
+import tvm._ffi
+
+tvm._ffi._init_api("relax", __name__)
diff --git a/python/tvm/relax/exec_builder.py b/python/tvm/relax/exec_builder.py
new file mode 100644
index 0000000000..1e28c967d1
--- /dev/null
+++ b/python/tvm/relax/exec_builder.py
@@ -0,0 +1,147 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name
+"""A builder to build Relax VM executable."""
+from enum import IntEnum
+from typing import Optional, Union, List
+import tvm
+from tvm.runtime import Object
+from tvm.runtime.container import ShapeTuple
+from .vm import Executable
+from . import _ffi_api
+
+
+class SpecialReg(IntEnum):
+ """Magic numbers that represent special registers in vm."""
+
+ VOID_ARG = (1 << 54) + 0
+ VM_STATE = (1 << 54) + 1
+
+
+class VMFuncKind(IntEnum):
+ """VM function kind code."""
+
+ PACKED_FUNC = 0
+ VM_FUNC = 1
+
+
+class VMFuncScope(object):
+ """An object corresponds to each VM function, working as a context manager."""
+
+ stack: List["VMFuncScope"] = []
+
+ def __init__(self, exit_callback):
+ self.exit_callback = exit_callback
+
+ def __enter__(self):
+ VMFuncScope.stack.append(self)
+ return self
+
+ def __exit__(self, ptype, value, trace):
+ VMFuncScope.stack.pop()
+ self.exit_callback()
+
+
+@tvm._ffi.register_object("relax.ExecBuilder")
+class ExecBuilder(Object):
+ """A builder to emit instructions and build executable for the virtual machine."""
+
+ def __init__(self) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.ExecBuilderCreate) # type: ignore
+
+ def r(self, idx: int) -> int:
+ """set instruction's argument as a register."""
+ return _ffi_api.ExecBuilderR(self, idx) # type: ignore
+
+ def imm(self, value: int) -> int:
+ """set instruction's argument as an immediate."""
+ return _ffi_api.ExecBuilderImm(self, value) # type: ignore
+
+ def c(self, idx: int) -> int:
+ """set instruction's argument as a constant."""
+ return _ffi_api.ExecBuilderC(self, idx) # type: ignore
+
+ def f(self, name: str) -> int:
+ """set instruction's argument as a function."""
+ return _ffi_api.ExecBuilderF(self, name) # type: ignore
+
+ def void_arg(self) -> int:
+ return self.r(SpecialReg.VOID_ARG)
+
+ def vm_state(self) -> int:
+ return self.r(SpecialReg.VM_STATE)
+
+ def declare_function(self, func_name: str, kind: VMFuncKind = VMFuncKind.PACKED_FUNC) -> None:
+ """Declare a function"""
+ _ffi_api.ExecBuilderDecalreFunction(self, func_name, kind) # type: ignore
+
+ def function(
+ self, func_name: str, num_inputs: Optional[int] = 0, param_names: List[str] = None
+ ) -> VMFuncScope:
+ """annotate a VM function."""
+ _ffi_api.ExecBuilderEmitFunction(self, func_name, num_inputs, param_names) # type: ignore
+ return VMFuncScope(lambda: _ffi_api.ExecBuilderEndFunction(self, func_name)) # type: ignore
+
+ def _check_scope(self) -> None:
+ if len(VMFuncScope.stack) == 0:
+ raise ValueError("emit should happen in a function scope")
+
+ def convert_constant(self, const: object) -> int:
+ return _ffi_api.ExecBuilderConvertConstant(self, const) # type: ignore
+
+ def emit_call(
+ self,
+ name: str,
+ args: Optional[List[Union[tvm.nd.NDArray, tvm.DataType]]] = None,
+ dst: int = None,
+ ) -> None:
+ """emit a call instruction which calls a packed function."""
+ self._check_scope()
+ if dst is None:
+ dst = SpecialReg.VOID_ARG
+ args_ = []
+ if args is not None:
+ for arg in args:
+ if isinstance(arg, tuple):
+ shape_tuple = ShapeTuple(arg)
+ new_arg = self.convert_constant(shape_tuple)
+ args_.append(new_arg)
+ elif isinstance(arg, (tvm.nd.NDArray, tvm.DataType, ShapeTuple)):
+ new_arg = self.convert_constant(arg)
+ args_.append(new_arg)
+ else:
+ args_.append(arg)
+ _ffi_api.ExecBuilderEmitCall(self, name, args_, dst) # type: ignore
+
+ def emit_ret(self, result: int) -> None:
+ """emit a return instruction"""
+ self._check_scope()
+ _ffi_api.ExecBuilderEmitRet(self, result) # type: ignore
+
+ def emit_goto(self, pc_offset):
+ """emit a goto instruction"""
+ self._check_scope()
+ _ffi_api.ExecBuilderEmitGoto(self, pc_offset) # type: ignore
+
+ def emit_if(self, cond, false_offset):
+ """emit an if instruction"""
+ self._check_scope()
+ _ffi_api.ExecBuilderEmitIf(self, cond, false_offset) # type: ignore
+
+ def get(self) -> Executable:
+ """return the executable"""
+ return Executable(_ffi_api.ExecBuilderGet(self)) # type: ignore
diff --git a/python/tvm/relax/testing/vm.py b/python/tvm/relax/testing/vm.py
new file mode 100644
index 0000000000..79da54be10
--- /dev/null
+++ b/python/tvm/relax/testing/vm.py
@@ -0,0 +1,85 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name
+"""Testing utilities for relax VM"""
+from typing import Any, List
+import numpy as np # type: ignore
+
+import tvm
+from tvm import relax
+from tvm.runtime.object import Object
+
+
+@tvm.register_func("test.vm.move")
+def move(src):
+ return src
+
+
+@tvm.register_func("test.vm.add")
+def add(a, b):
+ ret = a.numpy() + b.numpy()
+ return tvm.nd.array(ret)
+
+
+@tvm.register_func("test.vm.mul")
+def mul(a, b):
+ ret = a.numpy() * b.numpy()
+ return tvm.nd.array(ret)
+
+
+@tvm.register_func("test.vm.equal_zero")
+def equal_zero(a):
+ ret = np.all((a.numpy() == 0))
+ return tvm.nd.array(ret)
+
+
+@tvm.register_func("test.vm.subtract_one")
+def subtract_one(a):
+ ret = np.subtract(a.numpy(), 1)
+ return tvm.nd.array(ret)
+
+
+@tvm.register_func("test.vm.identity")
+def identity_packed(a, b):
+ b[:] = tvm.nd.array(a.numpy())
+
+
+@tvm.register_func("test.vm.tile")
+def tile_packed(a, b):
+ b[:] = tvm.nd.array(np.tile(a.numpy(), (1, 2)))
+
+
+@tvm.register_func("test.vm.add_scalar")
+def add_scalar(a, b):
+ return a + b
+
+
+@tvm.register_func("test.vm.get_device_id")
+def get_device_id(device):
+ return device.device_id
+
+
+def check_saved_func(vm: relax.VirtualMachine, func_name: str, *inputs: List[Any]) -> Object:
+ # uses save_function to create a closure with the given inputs
+ # and ensure the result is the same
+ # (assumes the functions return tensors and that they're idempotent)
+ saved_name = f"{func_name}_saved"
+ vm.save_function(func_name, saved_name, *inputs)
+ res1 = vm[func_name](*inputs)
+ res2 = vm[saved_name]()
+ tvm.testing.assert_allclose(res1.numpy(), res2.numpy(), rtol=1e-7, atol=1e-7)
+ return res1
diff --git a/python/tvm/relax/vm.py b/python/tvm/relax/vm.py
new file mode 100644
index 0000000000..ba16dfb079
--- /dev/null
+++ b/python/tvm/relax/vm.py
@@ -0,0 +1,609 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name, redefined-builtin, no-else-return
+"""The Relax virtual machine"""
+from typing import Callable, List, Optional, Union, Dict, Tuple, Any
+import numpy as np # type: ignore
+
+from tvm._ffi import base as _base
+import tvm
+from tvm import relax
+from tvm.ir.module import IRModule
+from tvm.runtime import Device, Module, PackedFunc, container
+from tvm.runtime.object import Object
+from tvm.tir.function import PrimFunc
+from . import _ffi_api
+from ..rpc.base import RPC_SESS_MASK
+
+
+class Executable(object):
+ """The executable object emitted by the VM compiler or the ExecBuilder."""
+
+ def __init__(self, mod: Module):
+ self.mod = mod
+ self._stats = self.mod["stats"]
+ self._as_text = self.mod["as_text"]
+ self._as_python = self.mod["as_python"]
+
+ def stats(self) -> str:
+ """print the detailed statistics of the executable."""
+ return self._stats()
+
+ def as_text(self) -> str:
+ """print the instructions as text format."""
+ return self._as_text()
+
+ def as_python(self) -> str:
+ """print the instructions as python program."""
+ return self._as_python()
+
+
+class VirtualMachine(object):
+ """Relax VM runtime."""
+
+ NAIVE_ALLOCATOR = 1
+ POOLED_ALLOCATOR = 2
+
+ def __init__(
+ self,
+ exec: Union[Executable, Module],
+ device: Union[Device, List[Device]],
+ memory_cfg: Optional[Union[str, Dict[Device, str]]] = None,
+ ) -> None:
+ """
+ Construct a VirtualMachine wrapper object.
+
+ Parameters
+ ----------
+ exec: Union[Executable, Module]
+ The VM executable or Runtime Module
+
+ device : Union[Device, List[Device]]
+ The device to deploy the module.
+
+ memory_cfg : Optional[Union[str, Dict[Device, str]]]
+ Config the type of memory allocator. The allocator type can be ["naive",
+ "pooled"]. If memory_cfg is None, all devices will use pooled allocator
+ by default. If memory_cfg is string, all devices will use the specified
+ allocator type. If memory_cfg is a dict, each device uses the allocator
+ type specified in the dict, or pooled allocator if not specified in the
+ dict.
+ """
+ self.module = (
+ exec.mod["vm_load_executable"]()
+ if isinstance(exec, Executable)
+ else exec["vm_load_executable"]()
+ )
+ self._invoke_closure = self.module["invoke_closure"]
+ self._save_function = self.module["save_function"]
+ self._set_input = self.module["set_input"]
+ self._invoke_stateful = self.module["invoke_stateful"]
+ self._get_output = self.module["get_output"]
+ self._get_output_arity = self.module["get_output_arity"]
+ self._get_function_arity = self.module["get_function_arity"]
+ self._get_function_param_name = self.module["get_function_param_name"]
+ self._setup_device(device, memory_cfg)
+
+ def _setup_device(self, dev: Device, memory_cfg: Union[str, Dict[Device, str]]) -> None:
+ """init devices and allocators."""
+ devs = dev
+ if not isinstance(dev, (list, tuple)):
+ if not isinstance(dev, tvm.runtime.Device):
+ raise TypeError(
+ "dev is expected to be Device or \
+ List[Device]"
+ )
+ devs = [dev]
+
+ if any(dev.device_type % RPC_SESS_MASK == tvm.cpu().device_type for dev in devs[:-1]):
+ raise RuntimeError(
+ "CPU host is required to be the last element of the device list if provided."
+ )
+
+ # CPU is required for executing shape functions
+ if devs[-1].device_type % RPC_SESS_MASK != tvm.cpu().device_type:
+ devs.append(tvm.cpu())
+
+ default_alloc_type = VirtualMachine.POOLED_ALLOCATOR
+ if memory_cfg is None:
+ memory_cfg = {}
+ elif isinstance(memory_cfg, str):
+ assert memory_cfg in ["naive", "pooled"]
+ if memory_cfg == "naive":
+ default_alloc_type = VirtualMachine.NAIVE_ALLOCATOR
+ memory_cfg = {}
+ elif not isinstance(memory_cfg, dict):
+ raise TypeError(
+ "memory_cfg is expected be string or dictionary, "
+ + "but received {}".format(type(memory_cfg))
+ )
+ init_args = []
+ for device in devs:
+ init_args.append(device.device_type % RPC_SESS_MASK)
+ init_args.append(device.device_id)
+ alloc_type = memory_cfg[device] if device in memory_cfg else default_alloc_type
+ init_args.append(alloc_type)
+ self.module["vm_initialization"](*init_args)
+
+ def __getitem__(self, key: str) -> PackedFunc:
+ return self.module[key]
+
+ def invoke_closure(self, closure: Object, *args: Any) -> Object:
+ """Invoke a closure.
+
+ Parameters
+ ----------
+ closure : Object
+ The VMClosure Object.
+
+ args : list[tvm.runtime.NDArray] or list[np.ndarray]
+ The arguments to the closure.
+
+ Returns
+ -------
+ result : Object
+ The output.
+ """
+ return self._invoke_closure(closure, *args)
+
+ def save_function(
+ self,
+ func_name: str,
+ saved_name: str,
+ *args: List[Any],
+ include_return: bool = True,
+ **kwargs: Dict[str, Any],
+ ) -> None:
+ """
+ Convenience function. Takes a function from the module and saves
+ a `PackedFunc` that, when called, will invoke the function with the given arguments.
+ The `PackedFunc` can be accessed from the module using `saved_name`.
+ This is included to facilitate timing trials:
+ Invoking the returned `PackedFunc` will have less overhead from dictionary lookups
+ than normally running through the VM.
+
+ If the saved name is taken, it can be overridden, though it cannot override
+ the name of a function defined in the Relax source.
+
+ This is really creating a closure, but the function has a different name
+ to avoid confusion with `invoke_closure` (they are not meant to be used together).
+
+ Parameters
+ ----------
+ func_name : str
+ The function that should be packaged up.
+
+ saved_name : str
+ The name that the resulting closure should be saved under.
+
+ include_return : bool
+ Whether the saved PackedFunc should return its output.
+ If timing over RPC, it may not be desirable to send output
+ between machines.
+
+ args : List[Any]
+ The arguments to package up with the function.
+
+ kwargs : Dict[str, Any]
+ Any named arguments to package up with the function
+ """
+ cargs: List[Any] = []
+ if kwargs:
+ args = self._convert_func_named_args(func_name, args, **kwargs)
+ for arg in args:
+ self._convert(arg, cargs)
+ self._save_function(func_name, saved_name, int(include_return), *cargs)
+
+ def _convert(self, arg: Any, cargs: List) -> None:
+ """helper function to convert arguments to vm function."""
+
+ def _gettype(arg):
+ if isinstance(arg, np.float16):
+ return "float16"
+ elif isinstance(arg, (_base.integer_types, bool)):
+ return "int32"
+ else:
+ return "float32"
+
+ if isinstance(arg, Object):
+ cargs.append(arg)
+ elif isinstance(arg, np.ndarray):
+ nd_arr = tvm.nd.array(arg, device=tvm.cpu(0))
+ cargs.append(nd_arr)
+ elif isinstance(arg, tvm.runtime.NDArray):
+ cargs.append(arg)
+ elif isinstance(arg, (tuple, list)):
+ field_args: List[Any] = []
+ for field in arg:
+ self._convert(field, field_args)
+ cargs.append(container.tuple_object(field_args))
+ elif isinstance(arg, (_base.numeric_types, bool)):
+ dtype = _gettype(arg)
+ value = tvm.nd.array(np.array(arg, dtype=dtype), device=tvm.cpu(0))
+ cargs.append(value)
+ elif isinstance(arg, str):
+ cargs.append(arg)
+ else:
+ raise TypeError("Unsupported type: %s" % (type(arg)))
+
+ def _convert_func_named_args(self, func_name: str, args: Any, **kwargs: Any) -> Any:
+ """
+ Takes named function parameters and returns a list of those needed,
+ in the order they should appear
+ """
+ # kwargs can be a super set of the required function parameters.
+ # We only find the ones that are needed.
+ func_arity = self._get_function_arity(func_name)
+ func_params = [self._get_function_param_name(func_name, i) for i in range(func_arity)]
+ new_args = [None] * len(func_params)
+ cnt = 0
+ for k in kwargs:
+ if k in func_params:
+ idx = func_params.index(k)
+ new_args[idx] = kwargs[k]
+ cnt += 1
+ else:
+ print(f'Warning: Keyword argument "{k}" is unused in {func_name}')
+ assert len(args) + cnt == len(func_params)
+ idx = 0
+ for i, arg in enumerate(new_args):
+ if arg is None:
+ new_args[i] = args[idx]
+ idx += 1
+ return new_args
+
+ def set_input(self, func_name: str, *args: Any, **kwargs: Any) -> None:
+ """Set the inputs to a function.
+ This interface works when using VM over RPC by internally converting NDArray in
+ the arguments to DLTensor, which is supported in RPC where remote could only
+ have a minimal C runtime.
+
+ Note: If `set_input` is used, the function *must* be called using `invoke_stateful`
+ and the results must be obtained using `get_outputs`.
+
+ Parameters
+ ----------
+ func_name : str
+ The name of the function.
+ args: List[tvm.runtime.NDArray] or List[np.ndarray]
+ The arguments to the function.
+ kwargs: dict of str to tvm.runtime.NDArray or np.ndarray
+ Named arguments to the function.
+ """
+ cargs: List[Any] = []
+
+ if kwargs:
+ args = self._convert_func_named_args(func_name, args, **kwargs)
+
+ for arg in args:
+ self._convert(arg, cargs)
+
+ self._set_input(func_name, *cargs)
+
+ def invoke_stateful(self, func_name: str) -> None:
+ """
+ Call the named function from the VM module using the arguments set using `set_input`.
+ It is an error to call `invoke_stateful` without using `set_input` first
+ (even if it's to set 0 inputs); conversely, if `set_input` has been called,
+ it is an error to call the function without using `invoke_stateful`.
+
+ The results of the call can be obtained by calling `get_outputs`.
+
+ Parameters
+ ----------
+ func_name: str
+ The name of the function to call.
+ """
+ self._invoke_stateful(func_name)
+
+ def get_outputs(self, func_name: str) -> Union[tvm.Object, Tuple[Any]]:
+ """
+ Get the value output by the function by the given name
+ after a call of `invoke_stateful`.
+
+ It is an error to call this function without first calling `invoke_stateful`.
+
+ Parameters
+ ----------
+ func_name: str
+ The name of the function whose output should be fetched.
+
+ Returns
+ -------
+ ret: Union[tvm.Object, Tuple[Any]]
+ The result of the earlier call to the function via `invoke_stateful`.
+ If the result is a tuple, it returns a list of the fields.
+ The fields are potentially also tuples, so these can be arbitrily nested.
+ """
+ # to deal with potentially nested tuples, we need to query for arity recursively
+ def get_output_rec(func_name, *idx):
+ arity = self._get_output_arity(func_name, *idx)
+ if arity == -1:
+ return self._get_output(func_name, *idx)
+ # otherwise we need to specify more indices
+ idx_list = list(idx)
+ return tuple(get_output_rec(func_name, *(idx_list + [i])) for i in range(arity))
+
+ return get_output_rec(func_name)
+
+ def time_evaluator(
+ self,
+ func_name,
+ dev,
+ number=10,
+ repeat=1,
+ min_repeat_ms=0,
+ cooldown_interval_ms=0,
+ repeats_to_cooldown=1,
+ f_preproc="",
+ ) -> Callable[..., tvm.runtime.module.BenchmarkResult]:
+ """
+ Returns an evaluator that times a function in the module.
+ This follows the same convention as time_evaluator in tvm.runtime.module.
+ This can be used in combination with save_function() so that the
+ timings avoid extra dictionary lookups.
+
+ Parameters
+ ----------
+ func_name: str
+ The name of the function in the module.
+
+ dev: Device
+ The device we should run this function on.
+
+ number: int
+ The number of times to run this function for taking average.
+ We call these runs as one `repeat` of measurement.
+
+ repeat: int, optional
+ The number of times to repeat the measurement.
+ In total, the function will be invoked (1 + number x repeat) times,
+ where the first one is warm up and will be discarded.
+ The returned result contains `repeat` costs,
+ each of which is an average of `number` costs.
+
+ min_repeat_ms: int, optional
+ The minimum duration of one `repeat` in milliseconds.
+ By default, one `repeat` contains `number` runs. If this parameter is set,
+ the parameters `number` will be dynamically adjusted to meet the
+ minimum duration requirement of one `repeat`.
+ i.e., When the run time of one `repeat` falls below this time, the `number` parameter
+ will be automatically increased.
+
+ cooldown_interval_ms: int, optional
+ The cooldown interval in milliseconds between the number of repeats defined by
+ `repeats_to_cooldown`.
+
+ repeats_to_cooldown: int, optional
+ The number of repeats before the cooldown is activated.
+
+ f_preproc: str, optional
+ The preprocess function name we want to execute before executing the time evaluator.
+
+ Note
+ ----
+ The function will be invoked (1 + number x repeat) times,
+ with the first call discarded in case there is lazy initialization.
+
+ Example
+ -------
+ Normal use with a VM function (may not work over RPC if the function returns a tuple):
+
+ .. code-block:: python
+
+ target = tvm.target.Target("llvm", host="llvm")
+ ex = relax.vm.build(TestTimeEvaluator, target)
+ vm = relax.VirtualMachine(mod, tvm.cpu())
+ timing_res = vm.time_evaluator("func_name", tvm.cpu())(arg0, arg1, ..., argn)
+
+ Use with the stateful API:
+
+ .. code-block:: python
+
+ target = tvm.target.Target("llvm", host="llvm")
+ ex = relax.vm.build(TestTimeEvaluator, target)
+ vm = relax.VirtualMachine(mod, tvm.cpu())
+ vm.set_input("func_name", arg0, arg1, ..., argn)
+ timing_res = vm.time_evaluator("invoke_stateful", tvm.cpu())("func_name")
+
+ With saved closures via `save_function` (this results in
+ fewer dictionary lookups in the timed portion):
+
+ .. code-block:: python
+
+ target = tvm.target.Target("llvm", host="llvm")
+ ex = relax.vm.build(TestTimeEvaluator, target)
+ vm = relax.VirtualMachine(mod, tvm.cpu())
+ vm.save_function("func_name", "func_name_saved", arg0, arg1, ..., argn)
+ timing_res = vm.time_evaluator("func_name_saved", tvm.cpu())()
+
+ Returns
+ -------
+ ftimer : function
+ The function that takes same argument as func and returns a BenchmarkResult.
+ The ProfileResult reports `repeat` time costs in seconds.
+
+ """
+ return self.module.time_evaluator(
+ func_name,
+ dev,
+ number=number,
+ repeat=repeat,
+ min_repeat_ms=min_repeat_ms,
+ cooldown_interval_ms=cooldown_interval_ms,
+ repeats_to_cooldown=repeats_to_cooldown,
+ f_preproc=f_preproc,
+ )
+
+
+def _vmcodegen(
+ builder: "relax.ExecBuilder",
+ mod: tvm.IRModule,
+ exec_mode: str = "bytecode",
+) -> tvm.IRModule:
+ """Running VM codegen.
+
+ Parameters
+ ----------
+ builder: relax.ExecBuilder
+ ExecBuilder to collect the vm executable.
+
+ mod: IRModule
+ The input IRModule to be built.
+
+ exec_mode: {"bytecode", "compiled"}
+ The execution mode.
+
+ Return
+ ------
+ leftover: IRModule
+ Left over IRModule that may contain extra functions.
+ """
+
+ if exec_mode == "bytecode":
+ return _ffi_api.VMCodeGen(builder, mod) # type:ignore
+ if exec_mode == "compiled":
+ return _ffi_api.VMTIRCodeGen(builder, mod) # type: ignore
+ raise ValueError("Unknown exec_mode %s" % exec_mode)
+
+
+def _vmlink(
+ builder: "relax.ExecBuilder",
+ target: Union[str, tvm.target.Target],
+ tir_mod: Optional[tvm.IRModule] = None,
+ ext_libs: List[tvm.runtime.Module] = None,
+ params: Optional[Dict[str, list]] = None,
+):
+ """
+ Internal codegen function to make executable.
+
+ This function is only used for unit-testing purpoes.
+
+ Use build instead.
+
+ Parameters
+ ----------
+ builder: relax.ExecBuilder
+ Builder used to collect executables.
+
+ target : Union[str, tvm.target.Target]
+ A build target which can have optional host side compilation target.
+
+ tir_mod: IRModule
+ The input TIR IRModule to be linked together.
+
+ ext_libs: List[tvm.runtime.Module]
+ List of compiled external modules.
+
+ params: Optional[Dict[str, list]]
+ Extra parameter mappings.
+
+ Returns
+ -------
+ ex: tvm.relax.vm.Executable
+ An executable that can be loaded by virtual machine.
+ """
+ if isinstance(target, str):
+ target = tvm.target.Target(target)
+ if params is None:
+ params = {}
+ if ext_libs is None:
+ ext_libs = []
+ lib = None
+ if tir_mod is not None:
+ lib = tvm.build(tir_mod, target=target)
+ return Executable(_ffi_api.VMLink(builder, target, lib, ext_libs, params)) # type: ignore
+
+
+def build(
+ mod: tvm.IRModule,
+ target: Union[str, tvm.target.Target],
+ params: Optional[Dict[str, list]] = None,
+ exec_mode: str = "bytecode",
+) -> Executable:
+ """
+ Build an IRModule to VM executable.
+
+ Parameters
+ ----------
+ mod: IRModule
+ The input IRModule to be built.
+
+ target : Union[str, tvm.target.Target]
+ A build target which can have optional host side compilation target.
+
+ When TVM compiles device specific program such as CUDA,
+ we also need host(CPU) side code to interact with the driver
+ to setup the dimensions and parameters correctly.
+ host is used to specify the host side codegen target.
+ By default, llvm is used if it is enabled,
+ otherwise a stackvm interpreter is used.
+
+ params: Optional[Dict[str, list]]
+ Parameters for the input IRModule that will be bound.
+
+ exec_mode: {"bytecode", "compiled"}
+ The execution mode.
+
+ Returns
+ -------
+ ex: tvm.relax.vm.Executable
+ An executable that can be loaded by virtual machine.
+
+ Example
+ -------
+
+ .. code-block:: python
+ class InputModule:
+ @R.function
+ def foo(x: Tensor((3, 4), "float32"), y: Tensor((3, 4), "float32")):
+ z = R.add(x, y)
+ return z
+
+ mod = InputModule
+ target = tvm.target.Target("llvm", host="llvm")
+ ex = relax.vm.build(mod, target)
+ """
+ if isinstance(target, str):
+ target = tvm.target.Target(target)
+
+ passes = [relax.transform.ToNonDataflow()]
+ passes.append(relax.transform.CallTIRRewrite())
+ passes.append(relax.transform.VMBuiltinLower())
+ passes.append(relax.transform.VMShapeLower())
+ passes.append(relax.transform.AttachGlobalSymbol())
+ seq = tvm.transform.Sequential(passes)
+ new_mod = seq(mod)
+
+ # Extract external runtime modules if exist.
+ ext_libs = []
+ if mod.attrs and "external_mods" in mod.attrs:
+ ext_libs = mod.attrs["external_mods"]
+
+ # builder collects the executable
+ builder = relax.ExecBuilder()
+ leftover_mod = _vmcodegen(builder, new_mod, exec_mode=exec_mode)
+ tir_mod = _filter_tir(leftover_mod)
+ return _vmlink(builder, target, tir_mod, ext_libs, params)
+
+
+def _filter_tir(mod: tvm.IRModule) -> tvm.IRModule:
+ tir_mod = IRModule({})
+ for gv in mod.get_global_vars():
+ if isinstance(mod[gv], PrimFunc):
+ tir_mod[gv] = mod[gv]
+ return tir_mod
diff --git a/src/relax/backend/vm/exec_builder.cc b/src/relax/backend/vm/exec_builder.cc
new file mode 100644
index 0000000000..b5d932137b
--- /dev/null
+++ b/src/relax/backend/vm/exec_builder.cc
@@ -0,0 +1,399 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/backend/vm/exec_builder.cc
+ */
+#include <tvm/relax/exec_builder.h>
+
+#include <sstream>
+
+namespace tvm {
+namespace relax {
+
+using namespace vm;
+
+TVM_REGISTER_NODE_TYPE(ExecBuilderNode);
+
+ExecBuilder ExecBuilderNode::Create() {
+ ExecBuilder ret(make_object<ExecBuilderNode>());
+ ret->exec_ = make_object<Executable>();
+ return ret;
+}
+
+Executable* ExecBuilderNode::exec() const { return exec_.get(); }
+
+ObjectPtr<Executable> ExecBuilderNode::Get() {
+ this->Formalize();
+ this->CheckExecutable();
+ return exec_;
+}
+
+vm::Instruction::Arg ExecBuilderNode::ConvertConstant_(TVMRetValue cvalue) {
+ // emit constant immediate as immediate.
+ if (cvalue.type_code() == kDLInt) {
+ int64_t val = cvalue.operator int64_t();
+ if (val <= vm::Instruction::kValueMaxLimit && val >= vm::Instruction::kValueMinLimit) {
+ return vm::Instruction::Arg::Immediate(val);
+ }
+ }
+ // convert string to object string
+ if (cvalue.type_code() == kTVMStr) {
+ cvalue = cvalue.operator String();
+ }
+
+ // run dedup for object with structural equality
+ if (cvalue.IsObjectRef<ObjectRef>()) {
+ ObjectRef obj = cvalue.operator ObjectRef();
+ auto it = const_dedup_map_.find(obj);
+ if (it != const_dedup_map_.end()) {
+ return vm::Instruction::Arg::ConstIdx(it->second);
+ }
+ vm::Index idx = exec_->constants.size();
+ exec_->constants.push_back(cvalue);
+ const_dedup_map_[obj] = idx;
+ return vm::Instruction::Arg::ConstIdx(idx);
+ } else {
+ // emit normal constant
+ vm::Index idx = exec_->constants.size();
+ exec_->constants.push_back(cvalue);
+ return vm::Instruction::Arg::ConstIdx(idx);
+ }
+}
+
+void ExecBuilderNode::DeclareFunction(const std::string& func_name, VMFuncInfo::FuncKind kind) {
+ auto it = exec_->func_map.find(func_name);
+ if (it != exec_->func_map.end()) {
+ ICHECK(kind == exec_->func_table[it->second].kind)
+ << "Function " << func_name << "already declared in a different kind";
+ return;
+ }
+ VMFuncInfo vmfunc;
+ vmfunc.kind = kind;
+ vmfunc.name = func_name;
+ // use num args to mark undefined.
+ vmfunc.start_instr = 0;
+ vmfunc.num_args = -2;
+ vmfunc.register_file_size = 0;
+ exec_->func_map[func_name] = exec_->func_table.size();
+ exec_->func_table.push_back(vmfunc);
+}
+
+vm::Instruction::Arg ExecBuilderNode::GetFunction(const std::string& func_name) {
+ auto it = exec_->func_map.find(func_name);
+ ICHECK(it != exec_->func_map.end()) << "Cannot find function " << func_name;
+ return vm::Instruction::Arg::FuncIdx(it->second);
+}
+
+void ExecBuilderNode::EmitFunction(const std::string& func_name, int64_t num_inputs,
+ Optional<Array<String>> param_names,
+ vm::VMFuncInfo::FuncKind kind, int64_t init_register_size) {
+ auto it = exec_->func_map.find(func_name);
+ if (it == exec_->func_map.end()) {
+ this->DeclareFunction(func_name, kind);
+ }
+ auto& vmfunc = exec_->func_table.at(exec_->func_map.at(func_name));
+ ICHECK_EQ(vmfunc.name, func_name);
+ ICHECK_EQ(vmfunc.num_args, -2) << "Function " << func_name << " already defined";
+ vmfunc.num_args = num_inputs;
+ if (param_names.defined()) {
+ std::vector<std::string> names;
+ for (auto name : param_names.value()) {
+ names.push_back(name);
+ }
+ vmfunc.param_names = names;
+ }
+ vmfunc.register_file_size = init_register_size;
+ if (kind == vm::VMFuncInfo::FuncKind::kVMFunc) {
+ vmfunc.start_instr = exec_->instr_offset.size();
+ }
+}
+
+void ExecBuilderNode::EndFunction(const std::string& func_name) {
+ auto it = exec_->func_map.find(func_name);
+ ICHECK(it != exec_->func_map.end());
+ VMFuncInfo& vmfunc = exec_->func_table.at(it->second);
+ ICHECK_EQ(vmfunc.end_instr, 0) << "EndFuncton can only be called once";
+
+ if (vmfunc.kind == vm::VMFuncInfo::FuncKind::kVMFunc) {
+ vmfunc.end_instr = exec_->instr_offset.size();
+ }
+}
+
+void ExecBuilderNode::EmitCall(vm::Instruction::Arg func, std::vector<vm::Instruction::Arg> args,
+ vm::RegName dst) {
+ ICHECK(func.kind() == vm::Instruction::ArgKind::kFuncIdx);
+ // store instruction
+ exec_->instr_offset.push_back(exec_->instr_data.size());
+ exec_->instr_data.push_back(static_cast<ExecWord>(Opcode::Call));
+ exec_->instr_data.push_back(dst);
+ exec_->instr_data.push_back(func.value());
+ exec_->instr_data.push_back(args.size());
+ for (Instruction::Arg arg : args) {
+ exec_->instr_data.push_back(arg.data());
+ }
+}
+
+void ExecBuilderNode::EmitCall(const std::string& func, std::vector<Instruction::Arg> args,
+ RegName dst) {
+ auto it = exec_->func_map.find(func);
+ if (it == exec_->func_map.end()) {
+ this->DeclareFunction(func, VMFuncInfo::FuncKind::kPackedFunc);
+ }
+ Index func_idx = exec_->func_map.at(func);
+ EmitCall(vm::Instruction::Arg::FuncIdx(func_idx), args, dst);
+}
+
+void ExecBuilderNode::EmitRet(vm::Instruction::Arg result) {
+ ICHECK(result.kind() == vm::Instruction::ArgKind::kRegister);
+ exec_->instr_offset.push_back(exec_->instr_data.size());
+ exec_->instr_data.push_back(static_cast<ExecWord>(Opcode::Ret));
+ exec_->instr_data.push_back(result.value());
+}
+
+void ExecBuilderNode::EmitGoto(Index pc_offset) {
+ exec_->instr_offset.push_back(exec_->instr_data.size());
+ exec_->instr_data.push_back(static_cast<ExecWord>(Opcode::Goto));
+ exec_->instr_data.push_back(pc_offset);
+}
+
+void ExecBuilderNode::EmitIf(vm::Instruction::Arg cond, vm::Index false_offset) {
+ ICHECK(cond.kind() == vm::Instruction::ArgKind::kRegister);
+ exec_->instr_offset.push_back(exec_->instr_data.size());
+ exec_->instr_data.push_back(static_cast<ExecWord>(Opcode::If));
+ exec_->instr_data.push_back(cond.value());
+ exec_->instr_data.push_back(false_offset);
+}
+
+void ExecBuilderNode::CheckExecutable() {
+ for (auto it = exec_->func_table.cbegin(); it != exec_->func_table.cend(); ++it) {
+ if (it->kind == VMFuncInfo::FuncKind::kPackedFunc) continue;
+ if (it->kind == VMFuncInfo::FuncKind::kVMTIRFunc) {
+ ICHECK_GE(it->register_file_size, it->num_args + 1)
+ << "Function " << it->name << " do not meet register file constraint.";
+ continue;
+ }
+ Index num_inputs = it->num_args;
+ std::unordered_set<RegName> dst_registers;
+ std::unordered_set<RegName> arg_registers;
+ size_t start_instr = it->start_instr;
+ size_t end_instr = it->end_instr;
+
+ CHECK_LT(start_instr, end_instr)
+ << "Function " << it->name << " EndFunction has not be been called";
+
+ auto check_reg_defined = [&](Instruction::Arg arg) {
+ if (arg.kind() != Instruction::ArgKind::kRegister) return;
+ if (arg.value() >= Instruction::kBeginSpecialReg) return;
+ if (arg.value() < num_inputs) return;
+
+ if (dst_registers.find(arg.value()) == dst_registers.end()) {
+ LOG(FATAL) << "register r(" << arg.value() << ") in VM function \"" << it->name
+ << "\" is used as input while it is never defined"
+ << " as a destination. Dump:\n"
+ << exec_->AsText();
+ }
+ };
+
+ auto check_const_defined = [&](Instruction::Arg arg) {
+ if (arg.kind() != Instruction::ArgKind::kConstIdx) return;
+ CHECK_LT(arg.value(), exec_->constants.size())
+ << "Constant index " << arg.value() << " exceed size of constant pool. Dump:\n"
+ << exec_->AsText();
+ };
+
+ auto check_func_defined = [&](Instruction::Arg arg) {
+ if (arg.kind() != Instruction::ArgKind::kFuncIdx) return;
+ CHECK_LT(arg.value(), exec_->func_table.size())
+ << "Func index " << arg.value() << " exceed size of fun_table. Dump:\n"
+ << exec_->AsText();
+ };
+
+ for (size_t idx = start_instr; idx < end_instr; ++idx) {
+ Instruction instr = exec_->GetInstruction(idx);
+ switch (instr.op) {
+ case Opcode::Call: {
+ check_func_defined(Instruction::Arg::FuncIdx(instr.func_idx));
+ for (int i = 0; i < instr.num_args; ++i) {
+ check_reg_defined(instr.args[i]);
+ check_const_defined(instr.args[i]);
+ check_func_defined(instr.args[i]);
+ arg_registers.emplace(instr.args[i].value());
+ }
+ if (instr.dst != Instruction::kVoidRegister) {
+ dst_registers.emplace(instr.dst);
+ }
+ break;
+ }
+ case Opcode::Ret: {
+ arg_registers.emplace(instr.result);
+ check_reg_defined(Instruction::Arg::Register(instr.result));
+ break;
+ }
+ case Opcode::Goto: {
+ ICHECK_NE(instr.pc_offset, 0);
+ break;
+ }
+ case Opcode::If: {
+ ICHECK_GT(instr.false_offset, 1);
+ check_reg_defined(Instruction::Arg::Register(instr.cond));
+ arg_registers.emplace(instr.cond);
+ break;
+ }
+ default:
+ LOG(FATAL) << "should never hit this case: " << static_cast<int>(instr.op);
+ break;
+ }
+ }
+ }
+}
+
+void ExecBuilderNode::Formalize() {
+ // a pass to formalize user-specified register indexes in the order of use
+ // and decide the number of registers to allocate for each VMFunction in the Executable
+ for (auto it = this->exec_->func_table.begin(); it != this->exec_->func_table.end(); ++it) {
+ if (it->kind == VMFuncInfo::FuncKind::kPackedFunc) continue;
+ if (it->kind == VMFuncInfo::FuncKind::kVMTIRFunc) continue;
+
+ Index num_inputs = it->num_args;
+ RegName register_idx = num_inputs;
+ std::unordered_map<RegName, RegName> register_map;
+ size_t start_instr = it->start_instr;
+ size_t end_instr = it->end_instr;
+
+ for (size_t idx = start_instr; idx < end_instr; ++idx) {
+ Instruction instr = this->exec_->GetInstruction(idx);
+ switch (instr.op) {
+ case Opcode::Call: {
+ // rewrite args
+ for (int i = 0; i < instr.num_args; ++i) {
+ if (instr.args[i].kind() == Instruction::ArgKind::kRegister &&
+ instr.args[i].value() >= num_inputs &&
+ instr.args[i].value() < Instruction::kBeginSpecialReg &&
+ register_map.find(instr.args[i].value()) != register_map.end()) {
+ this->exec_->instr_data[this->exec_->instr_offset[idx] + 4 + i] =
+ register_map[instr.args[i].value()];
+ }
+ }
+ if (instr.dst >= num_inputs && instr.dst < Instruction::kBeginSpecialReg) {
+ auto it = register_map.find(instr.dst);
+ if (it != register_map.end()) {
+ this->exec_->instr_data[this->exec_->instr_offset[idx] + 1] = it->second;
+ } else {
+ this->exec_->instr_data[this->exec_->instr_offset[idx] + 1] = register_idx;
+ register_map[instr.dst] = register_idx++;
+ }
+ }
+ break;
+ }
+ case Opcode::Ret: {
+ if (register_map.find(instr.result) != register_map.end()) {
+ this->exec_->instr_data[this->exec_->instr_offset[idx] + 1] =
+ register_map[instr.result];
+ }
+ break;
+ }
+ case Opcode::Goto: {
+ break;
+ }
+ case Opcode::If: {
+ if (register_map.find(instr.cond) != register_map.end()) {
+ this->exec_->instr_data[this->exec_->instr_offset[idx] + 1] = register_map[instr.cond];
+ }
+ break;
+ }
+ default:
+ LOG(FATAL) << "should never hit this case: " << static_cast<int>(instr.op);
+ break;
+ }
+ }
+ it->register_file_size = register_idx;
+ }
+}
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderCreate").set_body_typed(ExecBuilderNode::Create);
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderConvertConstant")
+ .set_body([](TVMArgs args, TVMRetValue* ret) {
+ ExecBuilder builder = args[0];
+ TVMRetValue rt;
+ rt = args[1];
+ *ret = builder->ConvertConstant(rt).data();
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEmitFunction")
+ .set_body_typed([](ExecBuilder builder, String func, int64_t num_inputs,
+ Optional<Array<String>> param_names) {
+ builder->EmitFunction(func, num_inputs, param_names);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEndFunction")
+ .set_body_method<ExecBuilder>(&ExecBuilderNode::EndFunction);
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderDeclareFunction")
+ .set_body_typed([](ExecBuilder builder, String name, int32_t kind) {
+ builder->DeclareFunction(name, static_cast<VMFuncInfo::FuncKind>(kind));
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEmitCall")
+ .set_body_typed([](ExecBuilder builder, String name, Array<IntImm> args, int64_t dst) {
+ std::vector<Instruction::Arg> args_;
+ for (size_t i = 0; i < args.size(); ++i) {
+ args_.push_back(Instruction::Arg::FromData(args[i]->value));
+ }
+ auto dst_ = Instruction::Arg::Register(dst);
+ builder->EmitCall(name, args_, dst_.value());
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEmitRet")
+ .set_body_typed([](ExecBuilder builder, int64_t data) {
+ builder->EmitRet(Instruction::Arg::FromData(data));
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEmitGoto")
+ .set_body_method<ExecBuilder>(&ExecBuilderNode::EmitGoto);
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderEmitIf")
+ .set_body_typed([](ExecBuilder builder, int64_t data, vm::Index false_offset) {
+ builder->EmitIf(Instruction::Arg::FromData(data), false_offset);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderR").set_body_typed([](ExecBuilder builder, int64_t value) {
+ return Instruction::Arg::Register(value).data();
+});
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderImm").set_body_typed([](ExecBuilder builder, int64_t value) {
+ return Instruction::Arg::Immediate(value).data();
+});
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderC").set_body_typed([](ExecBuilder builder, int64_t value) {
+ return Instruction::Arg::ConstIdx(value).data();
+});
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderF").set_body_typed([](ExecBuilder builder, String value) {
+ return builder->GetFunction(value).data();
+});
+
+TVM_REGISTER_GLOBAL("relax.ExecBuilderGet").set_body_typed([](ExecBuilder builder) {
+ ObjectPtr<Executable> p_exec = builder->Get();
+ return runtime::Module(p_exec);
+});
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/runtime/relax_vm/builtin.cc b/src/runtime/relax_vm/builtin.cc
new file mode 100644
index 0000000000..0ef63c8a41
--- /dev/null
+++ b/src/runtime/relax_vm/builtin.cc
@@ -0,0 +1,445 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * \file src/runtime/relax_vm/builtin.cc
+ */
+#include <tvm/runtime/container/adt.h>
+#include <tvm/runtime/container/shape_tuple.h>
+#include <tvm/runtime/data_type.h>
+#include <tvm/runtime/device_api.h>
+#include <tvm/runtime/logging.h>
+#include <tvm/runtime/memory.h>
+#include <tvm/runtime/ndarray.h>
+#include <tvm/runtime/packed_func.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/runtime/relax_vm/builtin.h>
+#include <tvm/runtime/relax_vm/bytecode.h>
+#include <tvm/runtime/relax_vm/memory_manager.h>
+#include <tvm/runtime/relax_vm/vm.h>
+
+#include "../runtime_base.h"
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+using tvm::runtime::NDArray;
+
+//-------------------------------------------------
+// Shape/StructInfo handling.
+//-------------------------------------------------
+/*!
+ * \brief Builtin function to allocate shape heap.
+ * \param ctx_ptr The context module pointer.
+ * \param size the size of the heap.
+ * \return An allocate NDArray as shape heap.
+ */
+NDArray AllocShapeHeap(void* ctx_ptr, int64_t size) {
+ VirtualMachine* vm = static_cast<VirtualMachine*>(ctx_ptr);
+ // use host allocator, which is always last element.
+ size_t host_device_index = vm->devices.size() - 1;
+ // specialy handle hexagon on-device RT.
+ // TODO(relax-team): visit and consider other possible choices.
+ if (vm->devices[0].device_type == kDLHexagon) {
+ host_device_index = 0;
+ }
+ auto* alloc = vm->allocators[host_device_index];
+ return alloc->Empty({size}, DLDataType{kDLInt, 64, 1}, vm->devices[host_device_index]);
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.alloc_shape_heap").set_body_typed(AllocShapeHeap);
+
+/*!
+ * \brief Builtin match shape function.
+ * \param args The packed function arguments.
+ * \param rv The return value.
+ *
+ * \sa MatchShapeCode
+ */
+void MatchShape(TVMArgs args, TVMRetValue* rv) {
+ // input shape the first argument can take in tensor or shape.
+ ShapeTuple input_shape;
+ if (args[0].IsObjectRef<NDArray>()) {
+ input_shape = args[0].operator NDArray().Shape();
+ } else {
+ input_shape = args[0];
+ }
+ DLTensor* heap = args[1];
+ int64_t* heap_data = heap == nullptr ? nullptr : static_cast<int64_t*>(heap->data);
+ int64_t size = args[2];
+ const int64_t kBeginCode = 3;
+ ICHECK_LE(kBeginCode + size * 2, args.size());
+ // a function that lazily get context for error reporting
+ const int64_t kErrorContextOffset = kBeginCode + size * 2;
+ Optional<String> err_ctx = args[kErrorContextOffset];
+
+ CHECK_EQ(input_shape.size(), size)
+ << "RuntimeError: " << err_ctx.value_or("") << " match_cast shape size mismatch.";
+
+ for (int64_t i = 0; i < size; ++i) {
+ MatchShapeCode code = static_cast<MatchShapeCode>(args[kBeginCode + i * 2].operator int());
+ int64_t reg = args[kBeginCode + i * 2 + 1];
+
+ if (code == MatchShapeCode::kAssertEqualToImm) {
+ CHECK_EQ(input_shape[i], reg)
+ << "RuntimeError: " << err_ctx.value_or("") << " match_cast error, "
+ << " shape[" << i << "]"
+ << " mismatch to specified constant.";
+ } else if (code == MatchShapeCode::kStoreToHeap) {
+ heap_data[reg] = input_shape[i];
+ } else if (code == MatchShapeCode::kNoOp) {
+ } else {
+ ICHECK(code == MatchShapeCode::kAssertEqualToLoad);
+ CHECK_EQ(input_shape[i], heap_data[reg])
+ << "RuntimeError: " << err_ctx.value_or("") << " match_cast error, "
+ << " shape[" << i << "]"
+ << " mismatch to a previous populated value.";
+ }
+ }
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.match_shape").set_body(MatchShape);
+
+/*!
+ * \brief Builtin make shape function.
+ * \param args The packed function arguments.
+ * \param rv The return value.
+ *
+ * \sa MakeShapeCode
+ */
+void MakeShape(TVMArgs args, TVMRetValue* rv) {
+ // NOTE: heap can be nullptr
+ DLTensor* heap = args[0];
+ int64_t* heap_data = heap == nullptr ? nullptr : static_cast<int64_t*>(heap->data);
+ int64_t size = args[1];
+ const int64_t kBeginCode = 2;
+
+ std::vector<int64_t> shape(size);
+
+ for (int64_t i = 0; i < size; ++i) {
+ MakeShapeCode code = static_cast<MakeShapeCode>(args[kBeginCode + i * 2].operator int());
+ int64_t reg = args[kBeginCode + i * 2 + 1];
+ if (code == MakeShapeCode::kUseImm) {
+ shape[i] = reg;
+ } else {
+ ICHECK(code == MakeShapeCode::kLoadShape);
+ shape[i] = heap_data[reg];
+ }
+ }
+ *rv = ShapeTuple(std::move(shape));
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.make_shape").set_body(MakeShape);
+
+/*!
+ * \brief Builtin function to check if arg is Tensor(dtype, ndim)
+ * \param arg The input argument.
+ * \param ndim Expected ndim of the Tensor, can be -1 (indicate unknown).
+ * \param dtype The expected content data type.
+ * \param err_ctx Additional context if error occurs.
+ */
+void CheckTensorInfo(TVMArgs args, TVMRetValue* rv) {
+ ObjectRef arg = args[0];
+ int ndim = args[1];
+ DataType dtype;
+ Optional<String> err_ctx;
+
+ if (args.size() == 3) {
+ dtype = DataType::Void();
+ err_ctx = args[2].operator Optional<String>();
+ } else {
+ dtype = args[2];
+ err_ctx = args[3].operator Optional<String>();
+ }
+
+ auto* ptr = arg.as<NDArray::ContainerType>();
+ CHECK(ptr != nullptr) << "TypeError: " << err_ctx.value_or("") << " expect a Tensor but get "
+ << arg->GetTypeKey();
+
+ if (ndim != -1) {
+ CHECK(ptr->dl_tensor.ndim == ndim)
+ << "ValueError: " << err_ctx.value_or("") << " expect Tensor with ndim " << ndim
+ << " but get " << ptr->dl_tensor.ndim;
+ }
+
+ if (dtype != DataType::Void()) {
+ CHECK(DataType(ptr->dl_tensor.dtype) == dtype)
+ << "ValueError: " << err_ctx.value_or("") << " expect Tensor with dtype " << dtype
+ << " but get " << ptr->dl_tensor.dtype;
+ }
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.check_tensor_info").set_body(CheckTensorInfo);
+
+/*!
+ * \brief Builtin function to check if arg is Shape(ndim)
+ * \param arg The input argument.
+ * \param ndim Expected size of the shape, can be -1 (indicate unknown).
+ * \param err_ctx Additional context if error occurs.
+ */
+void CheckShapeInfo(ObjectRef arg, int ndim, Optional<String> err_ctx) {
+ // a function that lazily get context for error reporting
+ auto* ptr = arg.as<ShapeTuple::ContainerType>();
+ CHECK(ptr != nullptr) << "TypeError: " << err_ctx.value_or("") << " expect a Shape but get "
+ << arg->GetTypeKey();
+ if (ndim != -1) {
+ CHECK(ptr->size == static_cast<uint64_t>(ndim))
+ << "ValueError: " << err_ctx.value_or("") << " expect Shape with ndim " << ndim
+ << " but get " << ptr->size;
+ }
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.check_shape_info").set_body_typed(CheckShapeInfo);
+
+/*!
+ * \brief Builtin function to check if arg is Tuple with size elements.
+ * \param arg The input argument.
+ * \param size The expected size of the tuple.
+ * \param err_ctx Additional context if error occurs.
+ */
+void CheckTupleInfo(ObjectRef arg, int64_t size, Optional<String> err_ctx) {
+ using Tuple = runtime::ADT;
+ // a function that lazily get context for error reporting
+ auto* ptr = arg.as<Tuple::ContainerType>();
+ CHECK(ptr != nullptr) << "TypeError: " << err_ctx.value_or("") << " expect a Tuple but get "
+ << arg->GetTypeKey();
+ CHECK(static_cast<int64_t>(ptr->size) == size)
+ << "ValueError: " << err_ctx.value_or("") << " expect a Tuple with " << size << " elements, "
+ << " but get a Tuple with " << ptr->size << " elements.";
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.check_tuple_info").set_body_typed(CheckTupleInfo);
+
+/*!
+ * \brief Builtin function to check if arg is a callable function.
+ * \param arg The input argument.
+ * \param err_ctx Additional context if error occurs.
+ */
+void CheckFuncInfo(ObjectRef arg, Optional<String> err_ctx) {
+ // a function that lazily get context for error reporting
+ bool is_func = arg.as<PackedFunc::ContainerType>() || arg.as<VMClosure::ContainerType>();
+ CHECK(is_func) << "TypeError: " << err_ctx.value_or("") << " expect a Function but get "
+ << arg->GetTypeKey();
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.check_func_info").set_body_typed(CheckFuncInfo);
+
+//-------------------------------------------------
+// Storage management.
+//-------------------------------------------------
+Storage VMAllocStorage(void* ctx_ptr, ShapeTuple buffer_size, Index device_index,
+ DLDataType dtype_hint) {
+ VirtualMachine* vm = static_cast<VirtualMachine*>(ctx_ptr);
+
+ ICHECK_EQ(buffer_size.size(), 1);
+ int alignment = runtime::kAllocAlignment;
+ ICHECK_LT(device_index, vm->devices.size())
+ << "The device index is out of VM physical devices list";
+
+ if (device_index == -1) {
+ // Allocate on host. Host is always the last element of vm->devices.
+ device_index = vm->devices.size() - 1;
+ }
+
+ int64_t size_imm = buffer_size[0];
+
+ auto storage_obj = runtime::SimpleObjAllocator().make_object<StorageObj>();
+ auto* alloc = vm->allocators[device_index];
+ ICHECK(alloc) << "Did you forget to init the VirtualMachine with devices?";
+ storage_obj->buffer = alloc->Alloc(size_imm, alignment, dtype_hint);
+ Storage storage(storage_obj);
+ return storage;
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.alloc_storage").set_body_typed(VMAllocStorage);
+
+TVM_REGISTER_GLOBAL("vm.builtin.alloc_tensor").set_body_method<Storage>(&StorageObj::AllocNDArray);
+
+//-------------------------------------------------
+// Closure function handling, calling convention
+//-------------------------------------------------
+TVM_REGISTER_GLOBAL("vm.builtin.make_closure").set_body([](TVMArgs args, TVMRetValue* rv) {
+ VMClosure clo = args[0];
+ std::vector<TVMRetValue> saved_args;
+ saved_args.resize(args.size() - 1);
+ for (size_t i = 0; i < saved_args.size(); ++i) {
+ saved_args[i] = args[i + 1];
+ }
+ auto impl = VMClosure::BindLastArgs(clo->impl, saved_args);
+ *rv = VMClosure(clo->func_name, impl);
+});
+
+TVM_REGISTER_GLOBAL("vm.builtin.invoke_closure").set_body([](TVMArgs args, TVMRetValue* rv) {
+ // args[0]: vm; args[1]: closure; args[2, 3, ...]: function arguments
+ VirtualMachine* vm = VirtualMachine::GetContextPtr(args[0]);
+ ObjectRef vm_closure = args[1];
+ vm->InvokeClosurePacked(vm_closure,
+ TVMArgs(args.values + 2, args.type_codes + 2, args.size() - 2), rv);
+});
+
+TVM_REGISTER_GLOBAL("vm.builtin.call_tir_dyn").set_body([](TVMArgs args, TVMRetValue* rv) {
+ PackedFunc func = args[0];
+ ShapeTuple to_unpack = args[args.size() - 1];
+ size_t num_tensor_args = args.size() - 2;
+
+ std::vector<TVMValue> values(num_tensor_args + to_unpack.size());
+ std::vector<int> tcodes(num_tensor_args + to_unpack.size());
+ runtime::TVMArgsSetter setter(values.data(), tcodes.data());
+
+ std::copy(args.values + 1, args.values + args.size() - 1, values.data());
+ std::copy(args.type_codes + 1, args.type_codes + args.size() - 1, tcodes.data());
+
+ for (size_t i = 0; i < to_unpack.size(); ++i) {
+ setter(i + num_tensor_args, to_unpack[i]);
+ }
+ TVMArgs func_args(values.data(), tcodes.data(), values.size());
+ func.CallPacked(func_args, rv);
+});
+
+//-------------------------------------
+// Builtin runtime operators.
+//-------------------------------------
+TVM_REGISTER_GLOBAL("vm.builtin.shape_of").set_body_method(&NDArray::Shape);
+
+TVM_REGISTER_GLOBAL("vm.builtin.copy").set_body([](TVMArgs args, TVMRetValue* rv) {
+ *rv = args[0];
+});
+
+/*!
+ * \brief Load the scalar value in cond and return the result value.
+ * \param cond The condition
+ * \return Bool
+ */
+bool ReadIfCond(TVMArgValue cond) {
+ if (cond.type_code() == kDLInt) return cond.operator bool();
+ NDArray arr = cond.operator tvm::runtime::NDArray();
+ if (arr->device.device_type != kDLCPU) {
+ arr = arr.CopyTo(DLDevice{kDLCPU, 0});
+ }
+ ICHECK(arr->dtype.code == kDLInt || arr->dtype.code == kDLUInt);
+ int64_t result;
+ switch (arr->dtype.bits) {
+ case 1: {
+ result = reinterpret_cast<int8_t*>(arr->data)[0];
+ break;
+ }
+ case 8: {
+ result = reinterpret_cast<int8_t*>(arr->data)[0];
+ break;
+ }
+ case 16: {
+ result = reinterpret_cast<int16_t*>(arr->data)[0];
+ break;
+ }
+ case 32: {
+ result = reinterpret_cast<int32_t*>(arr->data)[0];
+ break;
+ }
+ case 64: {
+ result = reinterpret_cast<int64_t*>(arr->data)[0];
+ break;
+ }
+ default:
+ LOG(FATAL) << "Unknown scalar int type: " << DLDataType2String(arr->dtype);
+ throw;
+ }
+ return result != 0;
+}
+
+TVM_REGISTER_GLOBAL("vm.builtin.read_if_cond").set_body_typed(ReadIfCond);
+
+//-------------------------------------
+// Data structure API
+//-------------------------------------
+TVM_REGISTER_GLOBAL("vm.builtin.tuple_getitem").set_body_typed([](runtime::ADT arr, int64_t index) {
+ return arr[index];
+});
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+//-------------------------------------------------
+// AnyList C runtime API: keep in relax for now.
+//--------------------------------------------------
+extern "C" {
+/*!
+ * \brief Backend function to get anylist item and set into Packed Func call arg stack.
+ *
+ * \param anylist The handle to the anylist, backed by TVMRetValue*
+ * \param int The index.
+ * \param args The args stack.
+ * \param type_codes The type codes stack.
+ * \param arg_offset The offset of argument.
+ * \return 0 when no error is thrown, -1 when failure happens
+ */
+TVM_DLL int TVMBackendAnyListSetPackedArg(void* anylist, int index, TVMValue* args, int* type_codes,
+ int arg_offset);
+/*!
+ * \brief Backend function to get anylist item and set into Packed Func call arg stack.
+ *
+ * \param anylist The handle to the anylist, backed by TVMRetValue*
+ * \param int The index.
+ */
+TVM_DLL int TVMBackendAnyListResetItem(void* anylist, int index);
+
+/*!
+ * \brief Backend function to set anylist item by moving from packed func return.
+ *
+ * \param anylist The handle to the anylist, backed by TVMRetValue*
+ * \param int The index.
+ * \param args The args stack.
+ * \param type_codes The type codes stack.
+ * \param arg_offset The offset of argument.
+ * \return 0 when no error is thrown, -1 when failure happens.
+ */
+TVM_DLL int TVMBackendAnyListMoveFromPackedReturn(void* anylist, int index, TVMValue* args,
+ int* type_codes, int ret_offset);
+
+int TVMBackendAnyListSetPackedArg(void* anylist, int index, TVMValue* args, int* type_codes,
+ int arg_offset) {
+ using namespace tvm::runtime;
+ API_BEGIN();
+ auto* list = static_cast<TVMRetValue*>(anylist);
+ TVMArgsSetter setter(args, type_codes);
+ setter(arg_offset, list[index]);
+ API_END();
+}
+
+int TVMBackendAnyListResetItem(void* anylist, int index) {
+ using namespace tvm::runtime;
+ API_BEGIN();
+ auto* list = static_cast<TVMRetValue*>(anylist);
+ list[index] = nullptr;
+ API_END();
+}
+
+int TVMBackendAnyListMoveFromPackedReturn(void* anylist, int index, TVMValue* args, int* type_codes,
+ int ret_offset) {
+ using namespace tvm::runtime;
+ API_BEGIN();
+ auto* list = static_cast<TVMRetValue*>(anylist);
+ if (type_codes[ret_offset] == kTVMStr || type_codes[ret_offset] == kTVMBytes) {
+ list[index] = TVMArgValue(args[ret_offset], type_codes[ret_offset]);
+ } else {
+ list[index] = TVMRetValue::MoveFromCHost(args[ret_offset], type_codes[ret_offset]);
+ }
+ API_END();
+}
+} // extern "C"
diff --git a/src/runtime/relax_vm/bytecode.cc b/src/runtime/relax_vm/bytecode.cc
new file mode 100644
index 0000000000..9084207848
--- /dev/null
+++ b/src/runtime/relax_vm/bytecode.cc
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/runtime/relax_vm/bytecode.cc
+ * \brief The bytecode for Relax virtual machine.
+ */
+
+#include <tvm/runtime/logging.h>
+#include <tvm/runtime/relax_vm/bytecode.h>
+
+#include <functional>
+#include <sstream>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+Instruction Instruction::Call(Index func_idx, Index num_args, Instruction::Arg* args, RegName dst) {
+ Instruction instr;
+ instr.op = Opcode::Call;
+ instr.dst = dst;
+ instr.func_idx = func_idx;
+ instr.num_args = num_args;
+ instr.args = args;
+ return instr;
+}
+
+Instruction Instruction::Ret(RegName result) {
+ Instruction instr;
+ instr.op = Opcode::Ret;
+ instr.result = result;
+ return instr;
+}
+
+Instruction Instruction::Goto(Index pc_offset) {
+ Instruction instr;
+ instr.op = Opcode::Goto;
+ instr.pc_offset = pc_offset;
+ return instr;
+}
+
+Instruction Instruction::If(RegName cond, Index false_offset) {
+ Instruction instr;
+ instr.op = Opcode::If;
+ instr.cond = cond;
+ instr.false_offset = false_offset;
+ return instr;
+}
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
diff --git a/src/runtime/relax_vm/executable.cc b/src/runtime/relax_vm/executable.cc
new file mode 100644
index 0000000000..b7915d7978
--- /dev/null
+++ b/src/runtime/relax_vm/executable.cc
@@ -0,0 +1,576 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/runtime/relax_vm/executable.cc
+ */
+
+#include <dmlc/memory_io.h>
+#include <tvm/runtime/logging.h>
+#include <tvm/runtime/relax_vm/executable.h>
+#include <tvm/runtime/relax_vm/vm.h>
+
+#include <functional>
+#include <sstream>
+
+#include "../file_utils.h"
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+/*! \brief The magic number for the serialized VM bytecode file */
+constexpr uint64_t kTVMVMBytecodeMagic = 0xD225DE2F4214151D;
+
+/*! \brief Possible types in the constant pool */
+enum ConstantType : int {
+ kNDArray = 0,
+ kDLDataType = 1,
+ kShapeTuple = 2,
+ kString = 3,
+ kInt = 4,
+};
+
+#define STREAM_CHECK(val, section) \
+ ICHECK(val) << "Invalid VM file format in the " << section << " section." \
+ << "\n";
+
+PackedFunc Executable::GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) {
+ if (name == "stats") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->Stats(); });
+ } else if (name == "as_text") {
+ return PackedFunc(
+ [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->AsText(); });
+ } else if (name == "as_python") {
+ return PackedFunc(
+ [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->AsPython(); });
+ } else if (name == "vm_load_executable") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ ObjectPtr<VirtualMachine> vm = VirtualMachine::Create();
+ ICHECK(sptr_to_self.get() == this);
+ vm->LoadExecutable(GetObjectPtr<Executable>(this));
+ *rv = Module(vm);
+ });
+ }
+ return nullptr;
+}
+
+std::string Executable::Stats() const {
+ std::ostringstream oss;
+ oss << "Relax VM executable statistics:" << std::endl;
+
+ // Get the number of constants.
+ // If the constant is an NDArray, get the shape of each of them.
+ // If the constant is an DLDataType, get the data type of each of them.
+ oss << " Constant pool (# " << constants.size() << "): [";
+ for (const auto& it : constants) {
+ if (it.IsObjectRef<runtime::NDArray>()) {
+ const auto ndarray = it.operator tvm::runtime::NDArray();
+ const auto& shape = ndarray.Shape();
+ // Scalar
+ if (shape.empty()) {
+ oss << "scalar, ";
+ continue;
+ }
+ oss << "[";
+ for (auto s : shape) {
+ oss << s << ", ";
+ }
+ oss.seekp(-2, oss.cur);
+ oss << "], ";
+ } else if (it.IsObjectRef<ShapeTuple>()) {
+ ShapeTuple shape = it.operator ShapeTuple();
+ oss << "shapetuple[";
+ for (size_t i = 0; i < shape.size(); ++i) {
+ oss << shape.at(i) << ", ";
+ }
+ oss.seekp(-2, oss.cur);
+ oss << "], ";
+ } else if (it.IsObjectRef<String>()) {
+ std::string f = it.AsObjectRef<tvm::runtime::String>().operator std::string();
+ oss << "\"";
+ oss << f;
+ oss << "\", ";
+ } else if (it.type_code() == kDLInt) {
+ oss << static_cast<int64_t>(it);
+ oss << ", ";
+ } else {
+ try {
+ DLDataType dtype = it.operator DLDataType();
+ oss << dtype;
+ oss << ", ";
+ } catch (std::exception& exc) {
+ LOG(FATAL) << "Constant pool can only contain NDArray and DLDataType, but got "
+ << ArgTypeCode2Str(it.type_code());
+ }
+ }
+ }
+ if (!constants.empty()) oss.seekp(-2, oss.cur);
+ oss << "]" << std::endl;
+
+ // Get the number of globals and the name of each of them.
+ oss << " Globals (#" << func_table.size() << "): [";
+ for (const auto& it : func_table) {
+ oss << it.name << ", ";
+ }
+ if (!func_map.empty()) oss.seekp(-2, oss.cur);
+ oss << "]" << std::endl;
+
+ return oss.str();
+}
+
+void Executable::SetInstructionData(Index i, Index j, ExecWord val) {
+ ICHECK_LT(i, instr_offset.size());
+ Index instr_idx = instr_offset[i];
+ ICHECK_LT(instr_idx + j, instr_data.size());
+ instr_data[instr_idx + j] = val;
+}
+
+Instruction Executable::GetInstruction(Index i) const {
+ Index offset = instr_offset[i];
+ Opcode op = static_cast<Opcode>(instr_data[offset]);
+ switch (op) {
+ case Opcode::Call: {
+ RegName dst = instr_data[offset + 1];
+ Index func_idx = instr_data[offset + 2];
+ Index num_args = instr_data[offset + 3];
+ ExecWord* args = const_cast<ExecWord*>(&instr_data[offset + 4]);
+ return Instruction::Call(func_idx, num_args, reinterpret_cast<Instruction::Arg*>(args), dst);
+ }
+ case Opcode::Ret: {
+ RegName result = instr_data[offset + 1];
+ return Instruction::Ret(result);
+ }
+ case Opcode::Goto: {
+ Index pc_offset = instr_data[offset + 1];
+ return Instruction::Goto(pc_offset);
+ }
+ case Opcode::If: {
+ RegName cond = instr_data[offset + 1];
+ Index false_offset = instr_data[offset + 2];
+ return Instruction::If(cond, false_offset);
+ }
+ default:
+ LOG(FATAL) << "should never hit this case: " << static_cast<int>(op);
+ break;
+ }
+ return Instruction();
+}
+
+void SaveHeader(dmlc::Stream* strm) {
+ uint64_t header = kTVMVMBytecodeMagic;
+ strm->Write(header);
+ std::string version = TVM_VERSION;
+ strm->Write(version);
+}
+
+void LoadHeader(dmlc::Stream* strm) {
+ // Check header.
+ uint64_t header;
+ STREAM_CHECK(strm->Read(&header), "header");
+ STREAM_CHECK(header == kTVMVMBytecodeMagic, "header");
+
+ // Check version.
+ std::string version;
+ STREAM_CHECK(strm->Read(&version), "version");
+ STREAM_CHECK(version == TVM_VERSION, "version");
+}
+
+void Executable::SaveToBinary(dmlc::Stream* stream) {
+ std::string code;
+ // Initialize the stream object.
+ dmlc::MemoryStringStream strm(&code);
+
+ // Save header
+ SaveHeader(&strm);
+
+ // Global section.
+ SaveGlobalSection(&strm);
+
+ // Constant section.
+ SaveConstantSection(&strm);
+
+ // Code section.
+ SaveCodeSection(&strm);
+
+ stream->Write(code);
+}
+
+void Executable::SaveToFile(const std::string& file_name, const std::string& format) {
+ std::string data;
+ dmlc::MemoryStringStream writer(&data);
+ dmlc::SeekStream* strm = &writer;
+ Executable::SaveToBinary(strm);
+ runtime::SaveBinaryToFile(file_name, data);
+}
+
+Module Executable::LoadFromBinary(void* stream) {
+ std::string code;
+ static_cast<dmlc::Stream*>(stream)->Read(&code);
+ dmlc::MemoryStringStream strm(&code);
+
+ ObjectPtr<Executable> exec = make_object<Executable>();
+
+ // Load header.
+ LoadHeader(&strm);
+
+ // Global section.
+ exec->LoadGlobalSection(&strm);
+
+ // Constant section.
+ exec->LoadConstantSection(&strm);
+
+ // Code section.
+ exec->LoadCodeSection(&strm);
+
+ return Module(exec);
+}
+
+TVM_REGISTER_GLOBAL("runtime.module.loadbinary_relax.Executable")
+ .set_body_typed(Executable::LoadFromBinary);
+
+Module Executable::LoadFromFile(const std::string& file_name) {
+ std::string data;
+ runtime::LoadBinaryFromFile(file_name, &data);
+ dmlc::MemoryStringStream reader(&data);
+ dmlc::Stream* strm = &reader;
+ return Executable::LoadFromBinary(reinterpret_cast<void*>(strm));
+}
+
+TVM_REGISTER_GLOBAL("runtime.module.loadfile_relax.Executable")
+ .set_body_typed(Executable::LoadFromFile);
+
+void VMFuncInfo::Save(dmlc::Stream* strm) const {
+ int32_t temp_kind = static_cast<int32_t>(kind);
+ strm->Write(temp_kind);
+ strm->Write(name);
+ strm->Write(start_instr);
+ strm->Write(end_instr);
+ strm->Write(num_args);
+ strm->Write(register_file_size);
+ strm->Write(param_names);
+}
+
+bool VMFuncInfo::Load(dmlc::Stream* strm) {
+ int32_t temp_kind;
+ if (!strm->Read(&temp_kind)) return false;
+ this->kind = static_cast<VMFuncInfo::FuncKind>(temp_kind);
+ if (!strm->Read(&name)) return false;
+ if (!strm->Read(&start_instr)) return false;
+ if (!strm->Read(&end_instr)) return false;
+ if (!strm->Read(&num_args)) return false;
+ if (!strm->Read(®ister_file_size)) return false;
+ if (!strm->Read(¶m_names)) return false;
+ return true;
+}
+
+void Executable::SaveGlobalSection(dmlc::Stream* strm) { strm->Write(func_table); }
+
+void Executable::SaveConstantSection(dmlc::Stream* strm) {
+ strm->Write(static_cast<uint64_t>(this->constants.size()));
+ for (const auto& it : this->constants) {
+ if (it.IsObjectRef<runtime::NDArray>()) {
+ strm->Write(ConstantType::kNDArray);
+ runtime::SaveDLTensor(strm, it.operator DLTensor*());
+ } else if (it.IsObjectRef<ShapeTuple>()) {
+ ShapeTuple shape = it.operator ShapeTuple();
+ strm->Write(ConstantType::kShapeTuple);
+ strm->Write(shape.size());
+ for (size_t i = 0; i < shape.size(); ++i) {
+ strm->Write(shape.at(i));
+ }
+ } else if (it.IsObjectRef<String>()) {
+ String str = it.operator String();
+ strm->Write(ConstantType::kString);
+ strm->Write(str.size());
+ for (size_t i = 0; i < str.size(); ++i) {
+ strm->Write(str.at(i));
+ }
+ } else if (it.type_code() == kDLInt) {
+ strm->Write(ConstantType::kInt);
+ strm->Write(it.value());
+ } else {
+ try {
+ strm->Write(ConstantType::kDLDataType);
+ strm->Write(it.operator DLDataType());
+ } catch (std::exception& exc) {
+ LOG(FATAL) << "Constant pool can only contain NDArray, DLDataType, and Integers but got "
+ << ArgTypeCode2Str(it.type_code());
+ }
+ }
+ }
+}
+
+void Executable::SaveCodeSection(dmlc::Stream* strm) {
+ strm->Write(instr_offset);
+ strm->Write(instr_data);
+}
+
+void Executable::LoadGlobalSection(dmlc::Stream* strm) {
+ STREAM_CHECK(strm->Read(&func_table), "Global Section");
+ // setup func map
+ for (size_t i = 0; i < func_table.size(); ++i) {
+ this->func_map[func_table[i].name] = i;
+ }
+}
+
+void Executable::LoadConstantSection(dmlc::Stream* strm) {
+ uint64_t sz;
+ // Load the number of constants.
+ STREAM_CHECK(strm->Read(&sz, sizeof(sz)), "constant");
+
+ size_t size = static_cast<size_t>(sz);
+ runtime::NDArray ndarray;
+ DLDataType dtype;
+ // Load each of the constants.
+ for (size_t i = 0; i < size; i++) {
+ int constant_type;
+ STREAM_CHECK(strm->Read(&constant_type, sizeof(constant_type)), "constant");
+ if (constant_type == ConstantType::kNDArray) {
+ ndarray.Load(strm);
+ TVMRetValue cell;
+ cell = ndarray;
+ this->constants.push_back(cell);
+ } else if (constant_type == ConstantType::kShapeTuple) {
+ uint64_t size;
+ strm->Read(&size);
+ std::vector<ShapeTuple::index_type> data(size);
+ for (size_t i = 0; i < size; ++i) {
+ strm->Read(&(data[i]));
+ }
+ TVMRetValue cell;
+ cell = ShapeTuple(data);
+ this->constants.push_back(cell);
+ } else if (constant_type == ConstantType::kDLDataType) {
+ strm->Read(&dtype);
+ TVMRetValue cell;
+ cell = dtype;
+ this->constants.push_back(cell);
+ } else if (constant_type == ConstantType::kString) {
+ uint64_t size;
+ strm->Read(&size);
+ std::vector<char> data(size);
+ for (size_t i = 0; i < size; ++i) {
+ strm->Read(&(data[i]));
+ }
+ TVMRetValue cell;
+ cell = String(std::string(data.begin(), data.end()));
+ this->constants.push_back(cell);
+ } else if (constant_type == ConstantType::kInt) {
+ int64_t value;
+ strm->Read(&value);
+ TVMRetValue cell;
+ cell = value;
+ this->constants.push_back(cell);
+ } else {
+ LOG(FATAL) << "Constant pool can only contain NDArray and DLDataType, but got "
+ << ArgTypeCode2Str(constant_type) << " when loading the VM constant pool.";
+ }
+ }
+}
+
+void Executable::LoadCodeSection(dmlc::Stream* strm) {
+ STREAM_CHECK(strm->Read(&(this->instr_offset)), "instr offset");
+ STREAM_CHECK(strm->Read(&(this->instr_data)), "instr data");
+}
+
+template <typename T>
+std::string StrJoin(T* items, int offset, int cnt, std::string delim = ", ",
+ std::function<std::string(T)> repr = std::to_string) {
+ if (cnt == 0) {
+ return "";
+ }
+ std::ostringstream oss;
+ oss << repr(items[offset]);
+ for (int i = 1; i < cnt; ++i) {
+ oss << delim << repr(items[offset + i]);
+ }
+ return oss.str();
+}
+
+std::string RegNameToStr(RegName reg) {
+ if (reg == Instruction::kVoidRegister) {
+ return "%void";
+ }
+ if (reg == Instruction::kVMRegister) {
+ return "%vm";
+ }
+ return "%" + std::to_string(reg);
+}
+
+String Executable::AsText() const {
+ auto get_func_name = [&](Index index) -> std::string {
+ if (static_cast<size_t>(index) < func_table.size()) {
+ return func_table[index].name;
+ } else {
+ return "unknown_func_index(" + std::to_string(index) + ")";
+ }
+ };
+
+ auto instr_to_str = [&](Instruction::Arg arg) -> std::string {
+ // only for argument
+ switch (arg.kind()) {
+ case Instruction::ArgKind::kRegister:
+ return RegNameToStr(arg.value());
+ case Instruction::ArgKind::kImmediate:
+ return "i" + std::to_string(arg.value());
+ case Instruction::ArgKind::kConstIdx:
+ return "c[" + std::to_string(arg.value()) + "]";
+ case Instruction::ArgKind::kFuncIdx:
+ return "f[" + get_func_name(arg.value()) + "]";
+ default:
+ LOG(FATAL) << "Wrong instruction kind: " << static_cast<int>(arg.kind());
+ return "";
+ }
+ };
+
+ // print the text format
+ std::ostringstream os;
+ for (size_t fidx = 0; fidx < this->func_table.size(); ++fidx) {
+ const VMFuncInfo& gfunc = this->func_table[fidx];
+ if (gfunc.kind == VMFuncInfo::FuncKind::kPackedFunc) {
+ os << "@" << gfunc.name << " packed_func;\n\n";
+ continue;
+ }
+ if (gfunc.kind == VMFuncInfo::FuncKind::kVMTIRFunc) {
+ os << "@" << gfunc.name << " num_inputs=" << gfunc.num_args << " vm_tir_func;\n\n";
+ continue;
+ }
+ ICHECK(gfunc.kind == VMFuncInfo::FuncKind::kVMFunc);
+ os << "@" << gfunc.name << ":\n";
+ size_t start_instr = gfunc.start_instr;
+ size_t end_instr = gfunc.end_instr;
+
+ for (size_t idx = start_instr; idx < end_instr; ++idx) {
+ os << " ";
+ Instruction instr = this->GetInstruction(idx);
+ switch (instr.op) {
+ case Opcode::Call: {
+ os << std::setw(6) << std::left << "call" << std::setw(16) << std::left
+ << get_func_name(instr.func_idx) << " in: " << std::setw(12) << std::left
+ << StrJoin<Instruction::Arg>(instr.args, 0, instr.num_args, ", ", instr_to_str)
+ << " dst: " << RegNameToStr(instr.dst) << "\n";
+ break;
+ }
+ case Opcode::Ret: {
+ os << std::setw(6) << std::left << "ret " << RegNameToStr(instr.result) << "\n";
+ break;
+ }
+ case Opcode::Goto: {
+ os << std::setw(6) << std::left << "goto" << instr.pc_offset << "\n";
+ break;
+ }
+ case Opcode::If: {
+ os << std::setw(6) << std::left << "If" << RegNameToStr(instr.cond) << ", "
+ << instr.false_offset << "\n";
+ break;
+ }
+ default:
+ LOG(FATAL) << "should never hit this case: " << static_cast<int>(instr.op);
+ break;
+ }
+ }
+ os << "\n";
+ }
+ return String(os.str());
+}
+
+String Executable::AsPython() const {
+ auto get_func_name = [&](Index index) -> std::string {
+ if (static_cast<size_t>(index) < func_table.size()) {
+ return "\"" + func_table[index].name + "\"";
+ } else {
+ return "ib.unknown_func_index(" + std::to_string(index) + ")";
+ }
+ };
+
+ auto arg_to_py_str = [&](Instruction::Arg arg) -> std::string {
+ switch (arg.kind()) {
+ case Instruction::ArgKind::kRegister:
+ if (arg.value() == Instruction::kVMRegister) {
+ return "ib.r(vm)";
+ }
+ return "ib.r(" + std::to_string(arg.value()) + ")";
+ case Instruction::ArgKind::kImmediate:
+ return "ib.imm(" + std::to_string(arg.value()) + ")";
+ case Instruction::ArgKind::kConstIdx:
+ return "ib.c(" + std::to_string(arg.value()) + ")";
+ case Instruction::ArgKind::kFuncIdx: {
+ return "ib.f(" + get_func_name(arg.value()) + ")";
+ }
+ default:
+ LOG(FATAL) << "Wrong instruction kind: " << static_cast<int>(arg.kind());
+ return "";
+ }
+ };
+
+ // print the python format
+ std::ostringstream os;
+ os << "ib = rx.Builder()\n";
+ for (size_t fidx = 0; fidx < this->func_table.size(); ++fidx) {
+ const VMFuncInfo& gfunc = this->func_table[fidx];
+ if (gfunc.kind == VMFuncInfo::FuncKind::kPackedFunc) {
+ continue;
+ }
+ if (gfunc.kind == VMFuncInfo::FuncKind::kVMTIRFunc) {
+ continue;
+ }
+ ICHECK(gfunc.kind == VMFuncInfo::FuncKind::kVMFunc);
+
+ os << "with ib.function(\"" << gfunc.name << "\", num_inputs=" << gfunc.num_args << "):\n";
+ size_t start_instr = gfunc.start_instr;
+ size_t end_instr = gfunc.end_instr;
+
+ for (size_t idx = start_instr; idx < end_instr; ++idx) {
+ Instruction instr = this->GetInstruction(idx);
+ switch (instr.op) {
+ case Opcode::Call: {
+ os << " ib.emit_call(" << get_func_name(instr.func_idx) << ", args=["
+ << StrJoin<Instruction::Arg>(instr.args, 0, instr.num_args, ", ", arg_to_py_str)
+ << "]";
+ if (instr.dst != Instruction::kVoidRegister) os << ", dst=ib.r(" << instr.dst << ")";
+ os << ")\n";
+ break;
+ }
+ case Opcode::Ret: {
+ os << " ib.emit_ret(ib.r(" << instr.result << "))\n";
+ break;
+ }
+ case Opcode::Goto: {
+ os << " ib.emit_goto(" << instr.pc_offset << ")\n";
+ break;
+ }
+ case Opcode::If: {
+ os << " ib.emit_if(ib.r(" << instr.cond << "), " << instr.false_offset << ")\n";
+ break;
+ }
+ default:
+ LOG(FATAL) << "should never hit this case: " << static_cast<int>(instr.op);
+ break;
+ }
+ }
+ }
+ return String(os.str());
+}
+
+TVM_REGISTER_GLOBAL("relax.ExecutableLoadFromFile").set_body_typed(Executable::LoadFromFile);
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
diff --git a/src/runtime/relax_vm/memory_manager.cc b/src/runtime/relax_vm/memory_manager.cc
new file mode 100644
index 0000000000..a017b9c6d9
--- /dev/null
+++ b/src/runtime/relax_vm/memory_manager.cc
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/memory_manager.cc
+ * \brief Allocate and manage memory for the Relay VM.
+ */
+#include <tvm/runtime/relax_vm/memory_manager.h>
+
+#include <memory>
+#include <utility>
+
+#include "naive_allocator.h"
+#include "pooled_allocator.h"
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+static void BufferDeleter(Object* obj) {
+ auto* ptr = static_cast<runtime::NDArray::Container*>(obj);
+ ICHECK(ptr->manager_ctx != nullptr);
+ Buffer* buffer = reinterpret_cast<Buffer*>(ptr->manager_ctx);
+ MemoryManager::GetAllocator(buffer->device)->Free(*(buffer));
+ delete buffer;
+ delete ptr;
+}
+
+void StorageObj::Deleter(Object* obj) {
+ auto* ptr = static_cast<runtime::NDArray::Container*>(obj);
+ // When invoking AllocNDArray we don't own the underlying allocation
+ // and should not delete the buffer, but instead let it be reclaimed
+ // by the storage object's destructor.
+ //
+ // We did bump the reference count by 1 to keep alive the StorageObj
+ // allocation in case this NDArray is the sole owner.
+ //
+ // We decrement the object allowing for the buffer to release our
+ // reference count from allocation.
+ StorageObj* storage = reinterpret_cast<StorageObj*>(ptr->manager_ctx);
+ storage->DecRef();
+ delete ptr;
+}
+
+inline void VerifyDataType(DLDataType dtype) {
+ ICHECK_GE(dtype.lanes, 1);
+ if (dtype.code == kDLFloat) {
+ ICHECK_EQ(dtype.bits % 8, 0);
+ } else {
+ // allow uint1 as a special flag for bool.
+ if (dtype.bits == 1 && dtype.code == kDLUInt) return;
+ ICHECK_EQ(dtype.bits % 8, 0);
+ }
+ ICHECK_EQ(dtype.bits & (dtype.bits - 1), 0);
+}
+
+inline size_t GetDataAlignment(const DLTensor& arr) {
+ size_t align = (arr.dtype.bits / 8) * arr.dtype.lanes;
+ if (align < runtime::kAllocAlignment) return runtime::kAllocAlignment;
+ return align;
+}
+
+runtime::NDArray StorageObj::AllocNDArray(uint64_t offset, ShapeTuple shape, DLDataType dtype) {
+ VerifyDataType(dtype);
+
+ // critical zone: allocate header, cannot throw
+ runtime::NDArray::Container* container =
+ new runtime::NDArray::Container(nullptr, shape, dtype, this->buffer.device);
+
+ container->SetDeleter(StorageObj::Deleter);
+ size_t needed_size = runtime::GetDataSize(container->dl_tensor);
+ this->IncRef();
+ // The manager context pointer must continue to point to the storage object
+ // which owns the backing memory, and keeps track of the reference count.
+ //
+ // When we free a container we extract the storage object, decrement its
+ // reference count, then destroy the container, but leave the underlying
+ // buffer intact.
+ container->manager_ctx = reinterpret_cast<void*>(this);
+
+ // is this UB?
+ // The only change we make w.r.t offset is modifying the data pointer
+ // of the backing tensor to point into the buffer instead of its start.
+ auto offset_ptr = reinterpret_cast<uint8_t*>(this->buffer.data) + offset;
+ container->dl_tensor.data = reinterpret_cast<void*>(offset_ptr);
+
+ runtime::NDArray ret(runtime::GetObjectPtr<Object>(container));
+ // RAII in effect, now run the check.
+
+ ICHECK(offset + needed_size <= this->buffer.size)
+ << "storage allocation failure, attempted to allocate " << needed_size << " at offset "
+ << offset << " in region that is " << this->buffer.size << "bytes";
+
+ return ret;
+}
+
+MemoryManager* MemoryManager::Global() {
+ // NOTE: explicitly use new to avoid exit-time destruction of global state
+ // Global state will be recycled by OS as the process exits.
+ static auto* inst = new MemoryManager();
+ return inst;
+}
+
+Allocator* MemoryManager::GetOrCreateAllocator(Device dev, AllocatorType type) {
+ MemoryManager* m = MemoryManager::Global();
+ std::lock_guard<std::mutex> lock(m->mutex_);
+ if (m->allocators_.find(dev) == m->allocators_.end()) {
+ std::unique_ptr<Allocator> alloc;
+ switch (type) {
+ case kNaive: {
+ DLOG(INFO) << "New naive allocator for " << runtime::DeviceName(dev.device_type) << "("
+ << dev.device_id << ")";
+ alloc.reset(new NaiveAllocator(dev));
+ break;
+ }
+ case kPooled: {
+ DLOG(INFO) << "New pooled allocator for " << runtime::DeviceName(dev.device_type) << "("
+ << dev.device_id << ")";
+ alloc.reset(new PooledAllocator(dev));
+ break;
+ }
+ default:
+ LOG(FATAL) << "Unknown allocator type: " << type;
+ }
+ auto ret = alloc.get();
+ m->allocators_.emplace(dev, std::move(alloc));
+ return ret;
+ }
+ auto alloc = m->allocators_.at(dev).get();
+ if (alloc->type() != type) {
+ LOG(WARNING) << "The type of existing allocator for " << runtime::DeviceName(dev.device_type)
+ << "(" << dev.device_id << ") is different from the request type ("
+ << alloc->type() << " vs " << type << ")";
+ }
+ return alloc;
+}
+
+Allocator* MemoryManager::GetAllocator(Device dev) {
+ MemoryManager* m = MemoryManager::Global();
+ std::lock_guard<std::mutex> lock(m->mutex_);
+ auto it = m->allocators_.find(dev);
+ if (it == m->allocators_.end()) {
+ LOG(FATAL) << "Allocator for " << runtime::DeviceName(dev.device_type) << "(" << dev.device_id
+ << ") has not been created yet.";
+ }
+ return it->second.get();
+}
+
+runtime::NDArray Allocator::Empty(std::vector<int64_t> shape, DLDataType dtype, DLDevice dev) {
+ VerifyDataType(dtype);
+ runtime::NDArray::Container* container =
+ new runtime::NDArray::Container(nullptr, shape, dtype, dev);
+ container->SetDeleter(BufferDeleter);
+ size_t size = runtime::GetDataSize(container->dl_tensor);
+ size_t alignment = GetDataAlignment(container->dl_tensor);
+ Buffer* buffer = new Buffer;
+ *buffer = this->Alloc(size, alignment, dtype);
+ container->manager_ctx = reinterpret_cast<void*>(buffer);
+ container->dl_tensor.data = buffer->data;
+ return runtime::NDArray(runtime::GetObjectPtr<Object>(container));
+}
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
diff --git a/src/runtime/relax_vm/naive_allocator.h b/src/runtime/relax_vm/naive_allocator.h
new file mode 100644
index 0000000000..843a559602
--- /dev/null
+++ b/src/runtime/relax_vm/naive_allocator.h
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/naive_allocator.h
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_NAIVE_ALLOCATOR_H_
+#define TVM_RUNTIME_RELAX_VM_NAIVE_ALLOCATOR_H_
+
+#include <tvm/runtime/device_api.h>
+#include <tvm/runtime/relax_vm/memory_manager.h>
+
+#include <atomic>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+class NaiveAllocator final : public Allocator {
+ public:
+ explicit NaiveAllocator(Device dev) : Allocator(kNaive), used_memory_(0), device_(dev) {}
+
+ Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) override {
+ Buffer buf;
+ buf.device = device_;
+ buf.size = nbytes;
+ buf.data =
+ runtime::DeviceAPI::Get(device_)->AllocDataSpace(device_, nbytes, alignment, type_hint);
+ used_memory_.fetch_add(nbytes, std::memory_order_relaxed);
+ DLOG(INFO) << "allocate " << nbytes << " B, used memory " << used_memory_ << " B";
+ return buf;
+ }
+
+ void Free(const Buffer& buffer) override {
+ runtime::DeviceAPI::Get(device_)->FreeDataSpace(buffer.device, buffer.data);
+ used_memory_.fetch_sub(buffer.size, std::memory_order_relaxed);
+ DLOG(INFO) << "free " << buffer.size << " B, used memory " << used_memory_ << " B";
+ }
+
+ private:
+ std::atomic<size_t> used_memory_;
+ Device device_;
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+#endif // TVM_RUNTIME_RELAX_VM_NAIVE_ALLOCATOR_H_
diff --git a/src/runtime/relax_vm/pooled_allocator.h b/src/runtime/relax_vm/pooled_allocator.h
new file mode 100644
index 0000000000..0dd7d8b027
--- /dev/null
+++ b/src/runtime/relax_vm/pooled_allocator.h
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/relax_vm/pooled_allocator.h
+ */
+#ifndef TVM_RUNTIME_RELAX_VM_POOLED_ALLOCATOR_H_
+#define TVM_RUNTIME_RELAX_VM_POOLED_ALLOCATOR_H_
+
+#include <tvm/runtime/device_api.h>
+#include <tvm/runtime/relax_vm/memory_manager.h>
+
+#include <atomic>
+#include <mutex>
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+class PooledAllocator final : public Allocator {
+ public:
+ static constexpr size_t kDefaultPageSize = 4096;
+
+ explicit PooledAllocator(Device dev, size_t page_size = kDefaultPageSize)
+ : Allocator(kPooled), page_size_(page_size), used_memory_(0), device_(dev) {}
+
+ ~PooledAllocator() { ReleaseAll(); }
+
+ Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) override {
+ std::lock_guard<std::recursive_mutex> lock(mu_);
+ size_t size = ((nbytes + page_size_ - 1) / page_size_) * page_size_;
+ auto&& it = memory_pool_.find(size);
+ if (it != memory_pool_.end() && !it->second.empty()) {
+ auto&& pool = it->second;
+ auto ret = pool.back();
+ pool.pop_back();
+ return ret;
+ }
+ Buffer buf;
+ buf.device = device_;
+ buf.size = size;
+ try {
+ buf.data =
+ runtime::DeviceAPI::Get(device_)->AllocDataSpace(device_, size, alignment, type_hint);
+ } catch (InternalError& err) {
+ LOG(WARNING) << "PooledAllocator got InternalError during allocation: " << err.message();
+ LOG(WARNING) << "Trying to release all unused memory and reallocate...";
+ ReleaseAll();
+ buf.data =
+ runtime::DeviceAPI::Get(device_)->AllocDataSpace(device_, size, alignment, type_hint);
+ }
+
+ used_memory_.fetch_add(size, std::memory_order_relaxed);
+ DLOG(INFO) << "allocate " << size << " B, used memory " << used_memory_ << " B";
+ return buf;
+ }
+
+ void Free(const Buffer& buffer) override {
+ std::lock_guard<std::recursive_mutex> lock(mu_);
+ if (memory_pool_.find(buffer.size) == memory_pool_.end()) {
+ memory_pool_.emplace(buffer.size, std::vector<Buffer>{});
+ }
+ memory_pool_.at(buffer.size).push_back(buffer);
+ DLOG(INFO) << "reclaim buffer " << buffer.size;
+ }
+
+ private:
+ void ReleaseAll() {
+ std::lock_guard<std::recursive_mutex> lock(mu_);
+ for (auto const& it : memory_pool_) {
+ auto const& pool = it.second;
+ for (auto const& buf : pool) {
+ runtime::DeviceAPI::Get(buf.device)->FreeDataSpace(buf.device, buf.data);
+ }
+ }
+ memory_pool_.clear();
+ used_memory_ = 0;
+ DLOG(INFO) << "release all buffers";
+ }
+
+ private:
+ size_t page_size_;
+ std::atomic<size_t> used_memory_;
+ std::unordered_map<size_t, std::vector<Buffer> > memory_pool_;
+ std::recursive_mutex mu_;
+ Device device_;
+};
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
+
+#endif // TVM_RUNTIME_RELAX_VM_POOLED_ALLOCATOR_H_
diff --git a/src/runtime/relax_vm/vm.cc b/src/runtime/relax_vm/vm.cc
new file mode 100644
index 0000000000..3cf65faaa8
--- /dev/null
+++ b/src/runtime/relax_vm/vm.cc
@@ -0,0 +1,811 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/runtime/relax_vm/vm.cc
+ */
+
+#include <tvm/runtime/container/adt.h>
+#include <tvm/runtime/packed_func.h>
+#include <tvm/runtime/relax_vm/vm.h>
+
+namespace tvm {
+namespace runtime {
+namespace relax_vm {
+
+//---------------------------------------------
+// VM Closure object
+//---------------------------------------------
+TVM_REGISTER_OBJECT_TYPE(VMClosureObj);
+
+VMClosure::VMClosure(String func_name, PackedFunc impl) {
+ auto ptr = make_object<VMClosureObj>();
+ ptr->func_name = func_name;
+ ptr->impl = std::move(impl);
+ data_ = std::move(ptr);
+}
+
+/*!
+ * \brief Create another PackedFunc with last arguments already bound to last_args.
+ * \param func The input func, can be a VMClosure or PackedFunc.
+ * \param last_args The arguments to bound to in the end of the function.
+ * \note The new function takes in arguments and append the last_args in the end.
+ */
+PackedFunc VMClosure::BindLastArgs(PackedFunc func, std::vector<TVMRetValue> last_args) {
+ return PackedFunc([func, last_args](TVMArgs args, TVMRetValue* rv) {
+ std::vector<TVMValue> values(args.size() + last_args.size());
+ std::vector<int> tcodes(args.size() + last_args.size());
+ runtime::TVMArgsSetter setter(values.data(), tcodes.data());
+ std::copy(args.values, args.values + args.size(), values.data());
+ std::copy(args.type_codes, args.type_codes + args.size(), tcodes.data());
+ for (size_t i = 0; i < last_args.size(); ++i) {
+ setter(i + args.size(), last_args[i]);
+ }
+ func.CallPacked(TVMArgs(values.data(), tcodes.data(), values.size()), rv);
+ });
+}
+
+//-----------------------------------------------------------
+// Utility functions.
+//-----------------------------------------------------------
+// Use the args after `starting_arg_idx` as a series of indices into `obj`,
+// indexing into nested ADTs and returning the final indexed object.
+ObjectRef IndexIntoNestedObject(ObjectRef obj, TVMArgs args, int starting_arg_idx) {
+ for (int i = starting_arg_idx; i < args.size(); i++) {
+ // the object must be an ADT to be able to index into it
+ if (!obj.as<ADTObj>()) {
+ LOG(FATAL) << "ValueError: Attempted to index into an object that is not an ADT.";
+ }
+ int index = args[i];
+ auto adt = Downcast<ADT>(obj);
+ // make sure the index is in bounds
+ if (index >= static_cast<int>(adt.size())) {
+ LOG(FATAL) << "IndexError: Invalid index (" << index << " >= " << adt.size() << ").";
+ }
+ obj = adt[index];
+ }
+ return obj;
+}
+
+NDArray ConvertNDArrayToDevice(NDArray src, const DLDevice& dev) {
+ if (src->device.device_type == dev.device_type && src->device.device_id == dev.device_id) {
+ return src;
+ }
+ return src.CopyTo(dev);
+}
+
+ObjectRef ConvertObjectToDevice(ObjectRef src, const Device& dev) {
+ if (src->IsInstance<NDArray::ContainerType>()) {
+ return ConvertNDArrayToDevice(Downcast<NDArray>(src), dev);
+ } else if (src->IsInstance<ADTObj>()) {
+ std::vector<ObjectRef> ret;
+ ADT adt = Downcast<ADT>(src);
+ for (size_t i = 0; i < adt.size(); i++) {
+ ret.push_back(ConvertObjectToDevice(adt[i], dev));
+ }
+ return ADT(adt->tag, ret.begin(), ret.end());
+ } else {
+ return src;
+ }
+}
+
+TVMRetValue ConvertArgToDevice(TVMArgValue input, Device dev) {
+ // NOTE: NDArray::FromExternalDLTensor is not safe
+ // in terms of memory-behavior.
+ // To be extra careful, we copy DLTensor.
+ // The developer can still explicitly allocate NDArray
+ // in TVM Native API or NDArray::FromDLPack to regain zero copy behavior.
+ TVMRetValue ret;
+
+ if (input.type_code() == kTVMDLTensorHandle) {
+ ret = NDArray::NewFromDLTensor(input, dev);
+ } else if (input.IsObjectRef<ObjectRef>()) {
+ ret = ConvertObjectToDevice(input.operator ObjectRef(), dev);
+ } else {
+ ret = input;
+ }
+ return ret;
+}
+
+TVMRetValue ConvertRegToDevice(TVMRetValue input, Device dev) {
+ TVMRetValue ret;
+ if (input.IsObjectRef<ObjectRef>()) {
+ ret = ConvertObjectToDevice(input.operator ObjectRef(), dev);
+ } else {
+ ret = input;
+ }
+ return ret;
+}
+
+//-----------------------------------------------------------
+// VM implementations.
+//-----------------------------------------------------------
+/*!
+ * \brief The register type.
+ */
+using RegType = TVMRetValue;
+
+/*!
+ * \brief A representation of a stack frame.
+ *
+ * A stack frame is a record containing the information needed
+ * to restore the caller's virtual machine state after returning
+ * from a function call.
+ */
+struct VMFrame {
+ /*! \brief The return program counter. */
+ Index return_pc;
+ /*! \brief Statically allocated space for objects */
+ std::vector<RegType> register_file;
+ /*! \brief Register in caller's frame to put return value */
+ RegName caller_return_register;
+ // The following fields are used for PackedFunc call within
+ // a single function scope. The space is reused across multiple
+ // packed func calls to increase cache locality and avoid re-allocation
+ /*! \brief Temporary argument value stack for packed func call. */
+ std::vector<TVMValue> call_arg_values;
+ /*! \brief Temporary argument tcode stack for packed func call. */
+ std::vector<int> call_arg_tcodes;
+
+ VMFrame(Index pc, Index register_file_size)
+ : return_pc(pc), register_file(register_file_size), caller_return_register(0) {}
+};
+
+class VirtualMachineImpl : public VirtualMachine {
+ public:
+ //---------------------------------------------------
+ // Public facing functions overloading
+ //---------------------------------------------------
+ void LoadExecutable(ObjectPtr<Executable> exec) final;
+
+ void Init(const std::vector<Device>& devices,
+ const std::vector<AllocatorType>& alloc_types) final;
+
+ PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final;
+
+ VMClosure GetClosure(const String& func_name) final;
+
+ void InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, TVMArgs args,
+ TVMRetValue* rv) final;
+
+ //--------------------------------------------------
+ // Additional support arguments functions for VM
+ //--------------------------------------------------
+ /*!
+ * \brief Set inputs to a function.
+ * \param func_name The function name.
+ * \param args args[offset:] are arguments to the function. If the arguments are not of the
+ * correct device for the function, they will be copied to the device.
+ * \param offset Starting offset of the arguments in \p args.
+ * \note This interface works when using VM over RPC by internally converting NDArray in
+ * the arguments to DLTensor, which is supported in RPC where remote could only have a minimal C
+ * runtime.
+ */
+ void SetInput(std::string func_name, TVMArgs args, int offset);
+
+ /*!
+ * \brief Look up whether the VM has a function by the given name.
+ * \param func_name the function's name
+ * \return The function, if it exists. Logs a fatal error if not.
+ */
+ VMFuncInfo LookupVMFuncInfo(const std::string& func_name);
+
+ /*!
+ * \brief Look up whether the VM has outputs for the given function.
+ * \param func_name the function's name
+ * \return The output, if it exists. Logs a fatal error if not.
+ */
+ RegType LookupVMOutput(const std::string& func_name);
+
+ /*!
+ * \brief Fully bind the argument of a global function and save it in the env.
+ * \param func_name The global function name to be saved.
+ * \param save_name The saved name of the function.
+ * \param include_return Whether forward the return value, set it to false allows
+ * us to ignore forwarding return value, which can be helpful to do benchmarking
+ * in RPC environment when return value is complicated ADT.
+ *
+ * \param args The arguments to bound to the function.
+ * \note This function is used by RPC server to help benchmarking.
+ */
+ void SaveClosure(const String& func_name, const String& save_name, bool include_return,
+ TVMArgs args);
+ /*!
+ * \brief Internal function to invoke a closure.
+ * \param closure_or_packed The closure to be invoked.
+ * \param args The arguments to the function.
+ * \return The result value.
+ */
+ RegType InvokeClosureInternal(const ObjectRef& closure_or_packed,
+ const std::vector<RegType>& args);
+ /*!
+ * \brief Invoke a VM function by interpreting bytecode.
+ * \param fidx The function index.
+ * \param args The arguments to the function.
+ * \return The object representing the result.
+ */
+ RegType InvokeBytecode(Index fidx, const std::vector<RegType>& args);
+
+ protected:
+ /*!
+ * \brief Get function by querying all of the current module's imports.
+ * \param name The name of the function.
+ * \return The result function, can return PackedFunc(nullptr) if nothing is found.
+ */
+ PackedFunc GetFuncFromImports(const String& name) {
+ for (auto& lib : this->imports_) {
+ PackedFunc func = lib->GetFunction(name, true);
+ if (func.defined()) return func;
+ }
+ return PackedFunc(nullptr);
+ }
+ /*!
+ * \brief Initialize function pool.
+ */
+ void InitFuncPool();
+ //-------------------------------------------------
+ // Instruction interpretations.
+ //-------------------------------------------------
+ /*!
+ * \brief Push a call frame onto the call stack.
+ * \param ret_pc The program counter to return to.
+ * \param vm_func The function to be pushed to the call stack.
+ */
+ void PushFrame(Index ret_pc, const VMFuncInfo& vm_func) {
+ frames_.emplace_back(std::make_unique<VMFrame>(ret_pc, vm_func.register_file_size));
+ }
+ /*!
+ * \brief Pop a frame off the call stack.
+ */
+ void PopFrame() {
+ ICHECK_GT(frames_.size(), 0);
+ pc_ = frames_.back()->return_pc;
+ frames_.pop_back();
+ }
+ /*!
+ * \brief Write to a VM register.
+ * \param frame current vm frame.
+ * \param reg The register to write to.
+ * \param obj The object to write to.
+ */
+ void WriteRegister(VMFrame* frame, RegName reg, const RegType& obj) {
+ ICHECK_LT(reg, frame->register_file.size());
+ frame->register_file[reg] = obj;
+ }
+ /*!
+ * \brief Read a VM register.
+ * \param frame current vm frame.
+ * \param reg The register to read from.
+ * \return The value of the register.
+ */
+ RegType ReadRegister(VMFrame* frame, RegName reg) {
+ if (reg < Instruction::kBeginSpecialReg) {
+ return frame->register_file[reg];
+ }
+ RegType ret;
+ if (reg == Instruction::kVoidRegister) {
+ ret = nullptr;
+ } else {
+ ICHECK_EQ(reg, Instruction::kVMRegister);
+ // per convention, ctx ptr must be VirtualMachine* casted to void.
+ // this and VirtualMachine* may or maynot be the same
+ // do first cast to VirtualMachine* then to void*
+ ret = static_cast<void*>(static_cast<VirtualMachine*>(this));
+ }
+ return ret;
+ }
+ /*!
+ * \brief Run call instruction.
+ * \param curr_frame The current frame.
+ * \param inst The call instruction.
+ */
+ inline void RunInstrCall(VMFrame* curr_frame, Instruction inst);
+
+ /*! \brief Run VM dispatch loop. */
+ void RunLoop();
+
+ private:
+ //--------------------------------------------------------
+ // Internal states for execution.
+ //--------------------------------------------------------
+ /*! \brief The loaded executable. */
+ ObjectPtr<Executable> exec_;
+ /*! \brief The global constant pool */
+ std::vector<TVMRetValue> const_pool_;
+ /*!
+ * \brief Function pool to cache functions in func_table
+ */
+ std::vector<TVMRetValue> func_pool_;
+ //--------------------------------------------------------
+ // Executor interface support
+ //--------------------------------------------------------
+ /*! \brief The function name to input register mapping. */
+ std::unordered_map<std::string, std::vector<RegType>> inputs_;
+ /*! \brief The function name to output register. */
+ std::unordered_map<std::string, RegType> outputs_;
+ /*! \brief A store of closures created by `save_function`. */
+ std::unordered_map<std::string, VMClosure> saved_closures_;
+ //------------------------------------------------------------
+ // VM Instruction execution.
+ //------------------------------------------------------------
+ /*!
+ * \brief The current stack of call frames.
+ * \note: Use unique ptr to avoid re-allocation and copy when frames_ get resized.
+ */
+ std::vector<std::unique_ptr<VMFrame>> frames_;
+ /*! \brief The virtual machine PC. */
+ Index pc_{0};
+ /*! \brief The special return register. */
+ RegType return_value_;
+};
+
+void VirtualMachineImpl::LoadExecutable(ObjectPtr<Executable> exec) {
+ this->exec_ = exec;
+ this->imports_ = exec_->imports();
+}
+
+void VirtualMachineImpl::Init(const std::vector<Device>& devices,
+ const std::vector<AllocatorType>& alloc_types) {
+ // TODO(@yuchen): support multi-device heterogeneous execution
+ ICHECK_LT(devices.size(), 3)
+ << "Currently relax vm only supports at most 2 devices (host + device)";
+ ICHECK_EQ(devices.size(), alloc_types.size());
+
+ this->devices.reserve(devices.size());
+ this->allocators.reserve(alloc_types.size());
+ for (size_t i = 0; i < devices.size(); i++) {
+ auto alloc = MemoryManager::GetOrCreateAllocator(devices[i], alloc_types[i]);
+ this->devices.push_back(devices[i]);
+ this->allocators.push_back(alloc);
+ }
+ // Setup constant sections.
+ this->const_pool_.reserve(exec_->constants.size());
+ for (const auto& constant : exec_->constants) {
+ if (constant.type_code() != kTVMNDArrayHandle) {
+ this->const_pool_.push_back(constant);
+ } else {
+ this->const_pool_.push_back(ConvertRegToDevice(constant, devices[0]));
+ }
+ }
+ // Setup function sections.
+ this->InitFuncPool();
+}
+
+VMFuncInfo VirtualMachineImpl::LookupVMFuncInfo(const std::string& func_name) {
+ ICHECK(exec_) << "The executable is not created yet.";
+ auto it = this->exec_->func_map.find(func_name);
+ CHECK(it != this->exec_->func_map.end()) << "ValueError: Unknown function: " << func_name;
+
+ return exec_->func_table[it->second];
+}
+
+RegType VirtualMachineImpl::LookupVMOutput(const std::string& func_name) {
+ if (!outputs_.count(func_name)) {
+ LOG(FATAL) << "ValueError: No output saved for call of \"" << func_name
+ << "\"; use `invoke_stateful` to call it first.";
+ }
+ return outputs_[func_name];
+}
+
+PackedFunc VirtualMachineImpl::GetFunction(const std::string& name,
+ const ObjectPtr<Object>& sptr_to_self) {
+ if (name == "vm_initialization") {
+ // initialize the VirtualMachine, takes variable-length arguments
+ // first argument is a runtime::Module, followed by one or more device_type, device_id,
+ // and the AllocatorType associated with the device.
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ ICHECK_EQ(args.size() % 3, 0);
+ std::vector<Device> devices;
+ std::vector<AllocatorType> alloc_types;
+ for (int i = 0; i < args.size(); i += 3) {
+ Device dev;
+ int device_type = args[i];
+ dev.device_type = DLDeviceType(device_type);
+ dev.device_id = args[i + 1];
+ int type = args[i + 2];
+ devices.push_back(dev);
+ alloc_types.push_back(AllocatorType(type));
+ }
+ this->Init(devices, alloc_types);
+ });
+ } else if (name == "save_function") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ ICHECK_GE(args.size(), 3);
+ this->SaveClosure(args[0], args[1], args[2],
+ TVMArgs(args.values + 3, args.type_codes + 3, args.size() - 3));
+ });
+ } else if (name == "invoke_closure") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ VMClosure clo = args[0];
+ this->InvokeClosurePacked(clo, TVMArgs(args.values + 1, args.type_codes + 1, args.size() - 1),
+ rv);
+ });
+ } else if (name == "invoke_stateful") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ std::string func_name = args[0];
+ const auto& m = this->exec_->func_map;
+ if (m.find(func_name) == m.end()) {
+ LOG(FATAL) << "ValueError: Unknown function: " << func_name;
+ }
+ Index gf_idx = m.at(func_name);
+ if (!inputs_.count(func_name)) {
+ LOG(FATAL) << "ValueError: No inputs set for stateful call of " << func_name
+ << "; use `set_input` first.";
+ return;
+ }
+ outputs_[func_name] = this->InvokeClosureInternal(func_pool_[gf_idx], inputs_[func_name]);
+ });
+ } else if (name == "get_output_arity") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ std::string func_name = args[0];
+ RegType out = LookupVMOutput(func_name);
+ // use remaining args as indices
+ ObjectRef obj = IndexIntoNestedObject(out.AsObjectRef<ObjectRef>(), args, 1);
+ // after chasing through the indices, examine the final object
+ if (const auto* adt = obj.as<ADTObj>()) {
+ *rv = static_cast<int>(adt->size);
+ } else {
+ *rv = -1;
+ }
+ });
+ } else if (name == "get_output") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ std::string func_name = args[0];
+ RegType out = LookupVMOutput(func_name);
+ // use remaining args as indices
+ ObjectRef obj = IndexIntoNestedObject(out.AsObjectRef<ObjectRef>(), args, 1);
+ if (obj.as<ADTObj>()) {
+ LOG(FATAL) << "ValueError: `get_output` cannot return a tuple for RPC compatibility. "
+ "Please specify another index argument.";
+ return;
+ }
+ *rv = obj;
+ });
+ } else if (name == "set_input") {
+ return PackedFunc(
+ [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { SetInput(args[0], args, 1); });
+ } else if (name == "get_function_arity") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ std::string func_name = args[0];
+ const VMFuncInfo& vm_func = LookupVMFuncInfo(func_name);
+ *rv = static_cast<int>(vm_func.param_names.size());
+ });
+ } else if (name == "get_function_param_name") {
+ return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+ std::string func_name = args[0];
+ int index = args[1];
+ const VMFuncInfo& vm_func = LookupVMFuncInfo(func_name);
+ if (static_cast<size_t>(index) >= vm_func.param_names.size()) {
+ LOG(FATAL) << "ValueError: Invalid index for " << func_name << " (" << index << " out of "
+ << vm_func.param_names.size() << ")";
+ }
+ *rv = vm_func.param_names[index];
+ });
+ } else {
+ // default case, look up closure in VM.
+ VMClosure clo = this->GetClosure(name);
+ return PackedFunc([sptr_to_self, this, clo](TVMArgs args, TVMRetValue* rv) {
+ this->InvokeClosurePacked(clo, args, rv);
+ });
+ }
+}
+
+void VirtualMachineImpl::SetInput(std::string func_name, TVMArgs args, int offset) {
+ const auto& m = exec_->func_map;
+ if (m.find(func_name) != m.end()) {
+ Index gf_idx = m.at(func_name);
+ const VMFuncInfo& vm_func = exec_->func_table[gf_idx];
+ size_t params_num = vm_func.num_args;
+ ICHECK_EQ(args.size() - offset, params_num)
+ << "The number of provided parameters doesn't match the number of arguments for";
+ std::vector<RegType> func_args(params_num);
+ for (int i = offset; i < args.size(); ++i) {
+ int index = i - offset;
+ func_args[index] = ConvertArgToDevice(args[i], devices[0]);
+ }
+ inputs_.emplace(func_name, func_args);
+ } else {
+ LOG(FATAL) << "ValueError: Unknown function: " << func_name;
+ }
+}
+
+//------------------------------------------
+// Closure handling
+//------------------------------------------
+void VirtualMachineImpl::InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, TVMArgs args,
+ TVMRetValue* rv) {
+ // run packed call if it is a packed func.
+ if (auto* packed = closure_or_packedfunc.as<PackedFunc::ContainerType>()) {
+ packed->CallPacked(args, rv);
+ return;
+ }
+ // run closure call.
+ auto* clo = closure_or_packedfunc.as<VMClosureObj>();
+ ICHECK(clo != nullptr) << "Function expects a closure or PackedFunc ";
+
+ std::vector<TVMValue> values(args.size() + 1);
+ std::vector<int> tcodes(args.size() + 1);
+ runtime::TVMArgsSetter setter(values.data(), tcodes.data());
+ // per convention, ctx ptr must be VirtualMachine* casted to void.
+ // this and VirtualMachine* may or maynot be the same
+ // do first cast to VirtualMachine* then to void*
+ setter(0, static_cast<void*>(static_cast<VirtualMachine*>(this)));
+ std::copy(args.values, args.values + args.size(), values.begin() + 1);
+ std::copy(args.type_codes, args.type_codes + args.size(), tcodes.begin() + 1);
+ clo->impl.CallPacked(TVMArgs(values.data(), tcodes.data(), args.size() + 1), rv);
+}
+
+// internal variant version of invoke closurepacked
+RegType VirtualMachineImpl::InvokeClosureInternal(const ObjectRef& closure_or_packed,
+ const std::vector<RegType>& args) {
+ RegType ret;
+ auto* packed = closure_or_packed.as<PackedFunc::ContainerType>();
+ auto* clo = closure_or_packed.as<VMClosureObj>();
+ int clo_offset = clo != nullptr ? 1 : 0;
+ std::vector<TVMValue> values(args.size() + clo_offset);
+ std::vector<int> tcodes(args.size() + clo_offset);
+ runtime::TVMArgsSetter setter(values.data(), tcodes.data());
+
+ if (clo != nullptr) {
+ setter(0, static_cast<void*>(static_cast<VirtualMachine*>(this)));
+ }
+ for (size_t i = 0; i < args.size(); ++i) {
+ setter(i + clo_offset, args[i]);
+ }
+
+ if (packed != nullptr) {
+ packed->CallPacked(TVMArgs(values.data(), tcodes.data(), values.size()), &ret);
+ } else {
+ ICHECK(clo != nullptr);
+ clo->impl.CallPacked(TVMArgs(values.data(), tcodes.data(), values.size()), &ret);
+ }
+ return ret;
+}
+
+void VirtualMachineImpl::SaveClosure(const String& func_name, const String& save_name,
+ bool include_return, TVMArgs args) {
+ VMClosure clo = this->GetClosure(func_name);
+ std::vector<RegType> inputs(args.size());
+ for (int i = 0; i < args.size(); ++i) {
+ inputs[i] = ConvertArgToDevice(args[i], this->devices[0]);
+ }
+ PackedFunc impl = VMClosure::BindLastArgs(clo->impl, inputs);
+ if (!include_return) {
+ impl = PackedFunc([impl](TVMArgs args, TVMRetValue* rv) {
+ TVMRetValue temp;
+ impl.CallPacked(args, &temp);
+ });
+ }
+ saved_closures_[save_name] = VMClosure(save_name, impl);
+}
+
+VMClosure VirtualMachineImpl::GetClosure(const String& func_name) {
+ // look up saved closures.
+ auto saved_it = saved_closures_.find(func_name);
+ if (saved_it != saved_closures_.end()) {
+ return saved_it->second;
+ }
+ auto it = exec_->func_map.find(func_name);
+ CHECK(it != exec_->func_map.end()) << "ValueError: Unknown function: " << func_name;
+
+ Index gf_idx = it->second;
+ const VMFuncInfo& finfo = exec_->func_table[gf_idx];
+
+ if (finfo.kind == VMFuncInfo::FuncKind::kVMFunc) {
+ // NOTE: should not capture strong ref to self and avoid cyclic ref.
+ auto impl = PackedFunc([gf_idx](TVMArgs args, TVMRetValue* rv) {
+ // Per convention, ctx ptr is a VirtualMachine*
+ VirtualMachine* ctx_ptr = static_cast<VirtualMachine*>(args[0].operator void*());
+
+ std::vector<RegType> inputs(args.size() - 1);
+ for (size_t i = 0; i < inputs.size(); ++i) {
+ inputs[i] = args[i + 1];
+ }
+ *rv = static_cast<VirtualMachineImpl*>(ctx_ptr)->InvokeBytecode(gf_idx, inputs);
+ });
+ return VMClosure(func_name, impl);
+ } else {
+ ICHECK(finfo.kind == VMFuncInfo::FuncKind::kVMTIRFunc)
+ << "Cannot support closure with function kind " << static_cast<int>(finfo.kind);
+ PackedFunc tir_func = GetFuncFromImports("__vmtir__" + finfo.name);
+ ICHECK(tir_func != nullptr) << "Cannot find underlying compiled tir function of VMTIRFunc "
+ << finfo.name;
+ auto impl = PackedFunc([this, finfo, tir_func](TVMArgs args, TVMRetValue* rv) {
+ // Per convention, ctx ptr is a VirtualMachine*
+ VirtualMachine* ctx_ptr = static_cast<VirtualMachine*>(args[0].operator void*());
+ ICHECK(ctx_ptr == this);
+ ICHECK_EQ(args.size() - 1, finfo.num_args)
+ << "Function " << finfo.name << " expects " << finfo.num_args << " arguments";
+ ICHECK_GE(finfo.register_file_size, finfo.num_args + 1);
+ std::vector<TVMRetValue> reg_file(finfo.register_file_size);
+ for (int64_t i = 0; i < finfo.num_args; ++i) {
+ reg_file[i] = args[i + 1];
+ }
+ void* reg_anylist_handle = reg_file.data();
+ void* const_anylist_handle = this->const_pool_.data();
+ void* func_anylist_handle = this->func_pool_.data();
+ tir_func(static_cast<void*>(ctx_ptr), reg_anylist_handle, const_anylist_handle,
+ func_anylist_handle);
+ // Return value always stored after inputs.
+ *rv = reg_file[finfo.num_args];
+ });
+ return VMClosure(func_name, impl);
+ }
+}
+
+//--------------------------------------------------------------------
+// Instruction interpretations.
+//--------------------------------------------------------------------
+RegType VirtualMachineImpl::InvokeBytecode(Index gf_idx, const std::vector<RegType>& args) {
+ const VMFuncInfo& gfunc = exec_->func_table[gf_idx];
+ ICHECK(gfunc.kind == VMFuncInfo::FuncKind::kVMFunc);
+
+ // Get the curr instr which might be a potential caller.
+ Instruction curr_instr = exec_->GetInstruction(pc_);
+ PushFrame(this->pc_, gfunc);
+ // Get new frame and set the caller info.
+ VMFrame* curr_frame = frames_.back().get();
+ if (curr_instr.op == Opcode::Call) {
+ curr_frame->caller_return_register = curr_instr.dst;
+ }
+
+ // load arguments to the register file
+ ICHECK_EQ(static_cast<size_t>(gfunc.num_args), args.size())
+ << "ValueError: Invoking function " << gfunc.name << " requires " << gfunc.num_args
+ << " inputs but only " << args.size() << " inputs are provided.";
+ for (size_t i = 0; i < args.size(); ++i) {
+ WriteRegister(frames_.back().get(), i, args[i]);
+ }
+ // set program counter
+ pc_ = gfunc.start_instr;
+ RunLoop();
+ return return_value_;
+}
+
+void VirtualMachineImpl::InitFuncPool() {
+ func_pool_.resize(exec_->func_table.size());
+
+ for (size_t func_index = 0; func_index < exec_->func_table.size(); ++func_index) {
+ const VMFuncInfo& info = exec_->func_table[func_index];
+ if (info.kind == VMFuncInfo::FuncKind::kPackedFunc) {
+ // only look through imports first
+ PackedFunc func = GetFuncFromImports(info.name);
+ if (!func.defined()) {
+ const PackedFunc* p_func = Registry::Get(info.name);
+ if (p_func != nullptr) func = *(p_func);
+ }
+ ICHECK(func.defined())
+ << "Error: Cannot find PackedFunc " << info.name
+ << " in either Relax VM kernel library, or in TVM runtime PackedFunc registry, or in "
+ "global Relax functions of the VM executable";
+ func_pool_[func_index] = func;
+
+ } else {
+ ICHECK(info.kind == VMFuncInfo::FuncKind::kVMFunc ||
+ info.kind == VMFuncInfo::FuncKind::kVMTIRFunc);
+ auto clo = this->GetClosure(info.name);
+ func_pool_[func_index] = clo;
+ }
+ }
+}
+
+void VirtualMachineImpl::RunInstrCall(VMFrame* curr_frame, Instruction instr) {
+ DLOG(INFO) << "\n pc = " << pc_ << ", execute: " << exec_->func_table[instr.func_idx].name;
+
+ // Use the call arg stack from the current frame to increase reuse
+ // and avoid re-allocation
+ curr_frame->call_arg_values.resize(instr.num_args);
+ curr_frame->call_arg_tcodes.resize(instr.num_args);
+
+ // NOTE: no changes and resize to those vector ref(otherwise can leads to segfault)
+ // in the remainder part of the function.
+ std::vector<TVMValue>& values = curr_frame->call_arg_values;
+ std::vector<int>& tcodes = curr_frame->call_arg_tcodes;
+
+ runtime::TVMArgsSetter setter(values.data(), tcodes.data());
+ for (Index i = 0; i < instr.num_args; ++i) {
+ Instruction::Arg arg = instr.args[i];
+ switch (arg.kind()) {
+ case Instruction::ArgKind::kRegister: {
+ setter(i, ReadRegister(curr_frame, arg.value()));
+ break;
+ }
+ case Instruction::ArgKind::kImmediate: {
+ setter(i, arg.value());
+ break;
+ }
+ case Instruction::ArgKind::kConstIdx: {
+ setter(i, this->const_pool_[arg.value()]);
+ break;
+ }
+ case Instruction::ArgKind::kFuncIdx: {
+ ICHECK_LT(static_cast<size_t>(arg.value()), this->func_pool_.size());
+ setter(i, this->func_pool_[arg.value()]);
+ break;
+ }
+ default: {
+ LOG(FATAL) << "ValueError: Unknown argument kind: " << int(arg.kind());
+ }
+ }
+ }
+ TVMArgs args(values.data(), tcodes.data(), values.size());
+ TVMRetValue ret;
+
+ ICHECK_LT(static_cast<size_t>(instr.func_idx), this->func_pool_.size());
+ this->InvokeClosurePacked(func_pool_[instr.func_idx], args, &ret);
+ // save the return value to the register
+ // saving to special register is a NOP
+ if (instr.dst < Instruction::kBeginSpecialReg) {
+ WriteRegister(curr_frame, instr.dst, ret);
+ }
+ // increment pc
+ pc_++;
+}
+
+void VirtualMachineImpl::RunLoop() {
+ VMFrame* curr_frame = frames_.back().get();
+
+ while (true) {
+ ICHECK_LT(static_cast<size_t>(pc_), exec_->instr_offset.size()) << "run into invalide section";
+ Instruction instr = exec_->GetInstruction(pc_);
+ switch (instr.op) {
+ case Opcode::Call: {
+ this->RunInstrCall(curr_frame, instr);
+ break;
+ }
+ case Opcode::Ret: {
+ // If we have hit the point from which we started
+ // running, we should return to the caller breaking
+ // the dispatch loop.
+ return_value_ = ReadRegister(curr_frame, instr.result);
+ RegName caller_return_register = curr_frame->caller_return_register;
+ PopFrame();
+ if (frames_.size() == 0) {
+ // directly return if no frame in the call stack.
+ } else {
+ // return from a local call.
+ // Update the current frame to be the parent frame.
+ curr_frame = frames_.back().get();
+ WriteRegister(curr_frame, caller_return_register, return_value_);
+ }
+ return;
+ }
+ case Opcode::Goto: {
+ pc_ += instr.pc_offset;
+ break;
+ }
+ case Opcode::If: {
+ int64_t cond_val = ReadRegister(curr_frame, instr.cond);
+ if (cond_val != 0) {
+ pc_++;
+ } else {
+ ICHECK_GT(instr.false_offset, 1);
+ pc_ += instr.false_offset;
+ }
+ break;
+ }
+ }
+ }
+}
+
+ObjectPtr<VirtualMachine> VirtualMachine::Create() { return make_object<VirtualMachineImpl>(); }
+
+} // namespace relax_vm
+} // namespace runtime
+} // namespace tvm
diff --git a/tests/python/relax/test_vm_execbuilder.py b/tests/python/relax/test_vm_execbuilder.py
new file mode 100644
index 0000000000..9a7cd0c879
--- /dev/null
+++ b/tests/python/relax/test_vm_execbuilder.py
@@ -0,0 +1,262 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Lowest level testing VM. Test execbuilder and execution."""
+import tvm
+import pytest
+import numpy as np
+from tvm import relax, TVMError
+from tvm.relax.testing.vm import check_saved_func
+
+
+def test_vm_execute():
+ ib = relax.ExecBuilder()
+ with ib.function("func0", num_inputs=2):
+ ib.emit_call("test.vm.add", args=[ib.r(0), ib.r(1)], dst=ib.r(2))
+ ib.emit_ret(ib.r(2))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ a = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ b = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+
+ add_res = check_saved_func(vm, "func0", a, b)
+ tvm.testing.assert_allclose(add_res.numpy(), a.numpy() + b.numpy(), rtol=1e-7, atol=1e-7)
+
+
+def test_vm_multiple_func():
+ ib = relax.ExecBuilder()
+ with ib.function("func0", num_inputs=2):
+ ib.emit_call("test.vm.add", args=[ib.r(0), ib.r(1)], dst=ib.r(2))
+ ib.emit_ret(ib.r(2))
+ with ib.function("func1", num_inputs=2):
+ ib.emit_call("test.vm.mul", args=[ib.r(0), ib.r(1)], dst=ib.r(2))
+ ib.emit_ret(ib.r(2))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ a = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ b = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ mul_res = check_saved_func(vm, "func1", a, b)
+ add_res = check_saved_func(vm, "func0", a, b)
+ tvm.testing.assert_allclose(add_res.numpy(), a.numpy() + b.numpy(), rtol=1e-7, atol=1e-7)
+ tvm.testing.assert_allclose(mul_res.numpy(), a.numpy() * b.numpy(), rtol=1e-7, atol=1e-7)
+
+
+def test_vm_checker():
+ ib = relax.ExecBuilder()
+ with pytest.raises(TVMError):
+ with ib.function("func0", num_inputs=2):
+ ib.emit_call("test.vm.add", args=[ib.r(0), ib.r(2)], dst=ib.r(2))
+ ib.emit_ret(ib.r(2))
+ ib.get()
+
+
+def test_neg_imm():
+ ib = relax.ExecBuilder()
+
+ with ib.function("func0", num_inputs=1):
+ ib.emit_call("test.vm.add_scalar", args=[ib.imm(-3), ib.r(0)], dst=ib.r(1))
+ ib.emit_ret(ib.r(1))
+ ib.get()
+
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ assert vm["func0"](1) == -2
+ assert vm["func0"](-3) == -6
+
+
+def test_emit_cache():
+ ib = relax.ExecBuilder()
+
+ with ib.function("func0", num_inputs=1):
+ x0 = ib.convert_constant("str0")
+ x1 = ib.convert_constant("str0")
+ # cache constant str
+ assert x0 == x1
+ s0 = ib.convert_constant(tvm.runtime.container.ShapeTuple([1, 2]))
+ s1 = ib.convert_constant(tvm.runtime.container.ShapeTuple([1, 2]))
+ s2 = ib.convert_constant(tvm.runtime.container.ShapeTuple([1, 3]))
+ assert s0 == s1
+ assert s1 != s2
+ y0 = ib.convert_constant(tvm.nd.array(np.array([1, 2, 3]).astype("int32")))
+ y1 = ib.convert_constant(tvm.nd.array(np.array([1, 2, 3]).astype("int32")))
+ assert y0 == y1
+ ib.emit_ret(ib.r(0))
+
+
+def test_vm_formalize():
+ ib0 = relax.ExecBuilder()
+ ib1 = relax.ExecBuilder()
+ with ib0.function("func0", num_inputs=2):
+ ib0.emit_call("test.vm.add", args=[ib0.r(0), ib0.r(1)], dst=ib0.r(100))
+ ib0.emit_call("test.vm.mul", args=[ib0.r(1), ib0.r(100)], dst=ib0.r(50))
+ ib0.emit_ret(ib0.r(50))
+ with ib1.function("func0", num_inputs=2):
+ ib1.emit_call("test.vm.add", args=[ib1.r(0), ib1.r(1)], dst=ib1.r(2))
+ ib1.emit_call("test.vm.mul", args=[ib1.r(1), ib1.r(2)], dst=ib1.r(3))
+ ib1.emit_ret(ib1.r(3))
+ exec0 = ib0.get()
+ exec1 = ib1.get()
+ assert exec0.as_text() == exec1.as_text()
+
+
+def test_vm_operand():
+ ib0 = relax.ExecBuilder()
+ with ib0.function("func0", num_inputs=2):
+ ib0.emit_call("test.vm.add_scalar", args=[ib0.r(0), ib0.r(1)], dst=ib0.r(2))
+ ib0.emit_ret(ib0.r(2))
+ exec0 = ib0.get()
+ vm = relax.VirtualMachine(exec0, tvm.cpu())
+ res = vm["func0"](2, 3)
+ assert res == 5
+
+ ib1 = relax.ExecBuilder()
+ with ib1.function("func1", num_inputs=1):
+ ib1.emit_call("test.vm.get_device_id", args=[ib1.r(0)], dst=ib1.r(1))
+ ib1.emit_ret(ib1.r(1))
+ exec1 = ib1.get()
+ vm = relax.VirtualMachine(exec1, tvm.cpu())
+ res = vm["func1"](tvm.cpu(3))
+ assert res == 3
+
+
+def test_vm_shapeof():
+ ib = relax.ExecBuilder()
+ shape = (32, 16)
+ arr = tvm.nd.array(np.random.rand(*shape))
+ with ib.function("main", num_inputs=0):
+ ib.emit_call("vm.builtin.shape_of", args=[arr], dst=ib.r(0))
+ ib.emit_ret(ib.r(0))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ res = vm["main"]()
+ for i, s in enumerate(res):
+ assert s == shape[i]
+
+
+def test_vm_storage():
+ dtype = tvm.DataType("float32")
+ shape = (4, 6)
+ ib = relax.ExecBuilder()
+ with ib.function("main", num_inputs=0):
+ ib.emit_call(
+ "vm.builtin.alloc_storage",
+ args=[ib.vm_state(), (24,), ib.convert_constant(0), dtype],
+ dst=ib.r(1),
+ )
+ ib.emit_call(
+ "vm.builtin.alloc_tensor", args=[ib.r(1), ib.imm(0), shape, dtype], dst=ib.r(2)
+ )
+ ib.emit_ret(ib.r(2))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ res = vm["main"]()
+ assert res.device == tvm.cpu()
+ assert res.shape == shape
+
+
+def test_vm_goto():
+ ib = relax.ExecBuilder()
+ with ib.function("main", num_inputs=2):
+ ib.emit_call("test.vm.add", args=[ib.r(0), ib.r(1)], dst=ib.r(2))
+ ib.emit_goto(2)
+ ib.emit_call("test.vm.mul", args=[ib.r(2), ib.r(1)], dst=ib.r(2))
+ ib.emit_ret(ib.r(2))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ a = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ b = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ res = check_saved_func(vm, "main", a, b)
+ tvm.testing.assert_allclose(res.numpy(), a.numpy() + b.numpy(), rtol=1e-7, atol=1e-7)
+
+
+def test_vm_if():
+ ib = relax.ExecBuilder()
+ with ib.function("main", num_inputs=3):
+ ib.emit_if(ib.r(0), 3)
+ ib.emit_call("test.vm.add", args=[ib.r(1), ib.r(2)], dst=ib.r(3))
+ ib.emit_goto(2)
+ ib.emit_call("test.vm.mul", args=[ib.r(1), ib.r(2)], dst=ib.r(3))
+ ib.emit_ret(ib.r(3))
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ a = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ b = tvm.nd.array(
+ np.random.rand(
+ 4,
+ )
+ )
+ res = vm["main"](0, a, b)
+ tvm.testing.assert_allclose(res.numpy(), a.numpy() * b.numpy(), rtol=1e-7, atol=1e-7)
+ res = vm["main"](1, a, b)
+ tvm.testing.assert_allclose(res.numpy(), a.numpy() + b.numpy(), rtol=1e-7, atol=1e-7)
+
+
+def test_vm_invoke_closure():
+ ib = relax.ExecBuilder()
+ with ib.function("lifted_func_1", num_inputs=4):
+ ib.emit_call("test.vm.add", args=[ib.r(0), ib.r(1)], dst=ib.r(4))
+ ib.emit_call("test.vm.add", args=[ib.r(2), ib.r(4)], dst=ib.r(5))
+ ib.emit_call("test.vm.add", args=[ib.r(3), ib.r(5)], dst=ib.r(6))
+ ib.emit_ret(ib.r(6))
+ with ib.function("main", num_inputs=2):
+ ib.emit_call(
+ "vm.builtin.make_closure", args=[ib.f("lifted_func_1"), ib.r(0), ib.r(1)], dst=ib.r(2)
+ )
+ ib.emit_ret(ib.r(2))
+
+ ex = ib.get()
+ vm = relax.VirtualMachine(ex, tvm.cpu())
+ w_inp = tvm.nd.array(np.random.rand(2, 3))
+ x_inp = tvm.nd.array(np.random.rand(2, 3))
+ y_inp = tvm.nd.array([[3.1, 4.0, 5.0], [6.0, 7.1, 9.0]])
+ z_inp = tvm.nd.array(np.random.rand(2, 3))
+ clo = vm["main"](w_inp, x_inp)
+ res = vm.invoke_closure(clo, y_inp, z_inp)
+ tvm.testing.assert_allclose(
+ res.numpy(), w_inp.numpy() + x_inp.numpy() + y_inp.numpy() + z_inp.numpy()
+ )
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
[tvm] 03/07: [Unity][IR] First-class StructInfo (#13907)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 8aaff909ea739e3aed7e7b25fe2391ea960a29b0
Author: Yuchen Jin <yu...@cs.washington.edu>
AuthorDate: Thu Feb 2 23:32:15 2023 -0500
[Unity][IR] First-class StructInfo (#13907)
* [Unity][IR] First-class StructInfo
Relax tracks structural information (such as tensor shape) via `StructInfo` about the values in Relax.
* Fix rust build
---------
Co-authored-by: Junru Shao <ju...@gmail.com>
---
CMakeLists.txt | 1 +
include/tvm/relax/struct_info.h | 430 ++++++++++++++++++++++++++++++++++++++++
rust/tvm/src/ir/relay/mod.rs | 2 +
src/relax/ir/struct_info.cc | 238 ++++++++++++++++++++++
4 files changed, 671 insertions(+)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a354662734..42170c82bb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -289,6 +289,7 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
src/driver/*.cc
src/support/*.cc
src/script/*.cc
+ src/relax/ir/*.cc
src/relax/backend/vm/*.cc
)
diff --git a/include/tvm/relax/struct_info.h b/include/tvm/relax/struct_info.h
new file mode 100644
index 0000000000..d21c8db86b
--- /dev/null
+++ b/include/tvm/relax/struct_info.h
@@ -0,0 +1,430 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_RELAX_STRUCT_INFO_H_
+#define TVM_RELAX_STRUCT_INFO_H_
+
+#include <tvm/ir/env_func.h>
+#include <tvm/ir/source_map.h>
+#include <tvm/node/node.h>
+// #include <tvm/relax/block_builder.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/type.h>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief Opaque object.
+ */
+class ObjectStructInfoNode : public StructInfoNode {
+ public:
+ void VisitAttrs(AttrVisitor* v) { v->Visit("span", &span); }
+
+ bool SEqualReduce(const ObjectStructInfoNode* other, SEqualReducer equal) const { return true; }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(0); }
+
+ static constexpr const char* _type_key = "relax.ObjectStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ObjectStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to ObjectStructInfoNode.
+ * \sa ObjectStructInfoNode
+ */
+class ObjectStructInfo : public StructInfo {
+ public:
+ TVM_DLL ObjectStructInfo(Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ObjectStructInfo, StructInfo, ObjectStructInfoNode);
+};
+
+/*!
+ * \brief Primitive value.
+ */
+class PrimStructInfoNode : public StructInfoNode {
+ public:
+ /*! \brief Underlying data type of the primitive value */
+ DataType dtype;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("dtype", &dtype);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const PrimStructInfoNode* other, SEqualReducer equal) const {
+ return equal(dtype, other->dtype);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(dtype); }
+
+ static constexpr const char* _type_key = "relax.PrimStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(PrimStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to PrimStructInfoNode.
+ * \sa PrimStructInfoNode
+ */
+class PrimStructInfo : public StructInfo {
+ public:
+ TVM_DLL PrimStructInfo(DataType dtype, Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(PrimStructInfo, StructInfo, PrimStructInfoNode);
+};
+
+/*!
+ * \brief StructInfo of shape value.
+ */
+class ShapeStructInfoNode : public StructInfoNode {
+ public:
+ /*! \brief optionally stores the symbolic value patterns of the shape */
+ Optional<Array<PrimExpr>> values;
+ /*!
+ * \brief The number of dimension of the shape, can be unknown.
+ * \sa kUnknownNDim
+ */
+ int ndim;
+
+ /*! \return Whether the struct info contains unknown ndim. */
+ bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("values", &values);
+ v->Visit("ndim", &ndim);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const ShapeStructInfoNode* other, SEqualReducer equal) const {
+ return equal(values, other->values) && equal(ndim, other->ndim);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(values);
+ hash_reduce(ndim);
+ }
+
+ static constexpr const char* _type_key = "relax.ShapeStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ShapeStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to ShapeStructInfoNode.
+ * \sa ShapeStructInfoNode
+ */
+class ShapeStructInfo : public StructInfo {
+ public:
+ /*!
+ * \brief Construction with known symbolic shape patterns
+ * \param values The symbolic shape values
+ * \param span The span of the AST.
+ */
+ TVM_DLL ShapeStructInfo(Array<PrimExpr> values, Span span = Span());
+ /*!
+ * \brief Construction with known unknown symbolic shape patterns.
+ * \param ndim Number of dimensions -- can be kUnknownNDim
+ * \param span The span of the AST.
+ */
+ TVM_DLL ShapeStructInfo(int ndim, Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ShapeStructInfo, StructInfo, ShapeStructInfoNode);
+};
+
+/*!
+ * \brief StructInfo of Tensor.
+ */
+class TensorStructInfoNode : public StructInfoNode {
+ public:
+ /*!
+ * \brief optionally store the shape expression of the tensor.
+ * \note shape must be normalized: it can only be NullOpt or ShapeExpr or Var.
+ */
+ Optional<Expr> shape;
+ /*! \brief The content data type, use void to denote the dtype is unknown. */
+ DataType dtype;
+ /*!
+ * \brief The number of dimension of the tensor, can be unknown.
+ * \sa kUnknownNDim
+ */
+ int ndim;
+
+ /*! \return Whether the struct info contains unknown ndim. */
+ bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
+
+ /*! \return Whether the struct info contains unknown dtype. */
+ bool IsUnknownDtype() const { return dtype.is_void(); }
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("shape", &shape);
+ v->Visit("dtype", &dtype);
+ v->Visit("ndim", &ndim);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const TensorStructInfoNode* other, SEqualReducer equal) const {
+ return equal(shape, other->shape) && equal(ndim, other->ndim) && equal(dtype, other->dtype);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(shape);
+ hash_reduce(dtype);
+ hash_reduce(ndim);
+ }
+
+ static constexpr const char* _type_key = "relax.TensorStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(TensorStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to TensorStructInfoNode.
+ * \sa TensorStructInfoNode
+ */
+class TensorStructInfo : public StructInfo {
+ public:
+ /*!
+ * \brief Construction with a known shape expression.
+ * \param shape The shape of the tensor.
+ * \param dtype The data type of tensor's elements.
+ * \param span The span of the AST.
+ *
+ * \note shape must already be normalized.
+ */
+ TVM_DLL TensorStructInfo(Expr shape, DataType dtype, Span span = Span());
+
+ /*!
+ * \brief Construction with an unknown shape expression.
+ * \param dtype The data type of tensor's elements.
+ * \param ndim The number of dimensions
+ * \param span The span of the AST.
+ */
+ TVM_DLL TensorStructInfo(DataType dtype, int ndim, Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TensorStructInfo, StructInfo, TensorStructInfoNode);
+};
+
+/*!
+ * \brief StructInfo of Tuple.
+ */
+class TupleStructInfoNode : public StructInfoNode {
+ public:
+ /*! \brief The struct info of tuple fields. */
+ Array<StructInfo> fields;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("fields", &fields);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const TupleStructInfoNode* other, SEqualReducer equal) const {
+ return equal(fields, other->fields);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(fields); }
+
+ static constexpr const char* _type_key = "relax.TupleStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(TupleStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to TupleStructInfoNode.
+ * \sa TupleStructInfoNode
+ */
+class TupleStructInfo : public StructInfo {
+ public:
+ /*!
+ * \brief Constructor
+ * \param fields Struct info of tuple fields.
+ * \param span The span of the AST.
+ */
+ TVM_DLL TupleStructInfo(Array<StructInfo> fields, Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TupleStructInfo, StructInfo, TupleStructInfoNode);
+};
+
+class BlockBuilder;
+
+/*!
+ * \brief custom-defined StructInfo derivation function.
+ * \param call The call expression to be derived.
+ * \param ctx The builder context.
+ * \return The derived struct info of the call.
+ */
+using StructInfoDeriveFunc = TypedEnvFunc<StructInfo(const Call& call, const BlockBuilder& ctx)>;
+
+/*!
+ * \brief Structure information about function.
+ *
+ * This data structure contains enough information for us to
+ * do best-effort structure information deduction.
+ */
+class FuncStructInfoNode : public StructInfoNode {
+ public:
+ /*!
+ * \brief The parameter struct info of the function.
+ * \note When params is NullOpt means the function can take arbitrary number of arguments.
+ * We define such functions as Opaque function.
+ */
+ Optional<Array<StructInfo>> params;
+ /*!
+ * \brief The struct info of the function's return value.
+ */
+ StructInfo ret;
+ /*!
+ * \brief Derivation function of opaque functions that may take any number of parameters.
+ * \note When derive_func is not empty, then params should be NullOpt,
+ * ret should be ObjectStructInfo()
+ */
+ Optional<StructInfoDeriveFunc> derive_func;
+
+ /*!
+ * \return Whether the func struct info is opaque.
+ * \note We define a function as opaque we have no constraints on params.
+ */
+ bool IsOpaque() const { return !params.defined(); }
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("params", ¶ms);
+ v->Visit("ret", &ret);
+ v->Visit("derive_func", &derive_func);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const FuncStructInfoNode* other, SEqualReducer equal) const {
+ return equal.DefEqual(params, other->params) && equal(ret, other->ret) &&
+ equal(derive_func, other->derive_func);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce.DefHash(params);
+ hash_reduce(ret);
+ hash_reduce(derive_func);
+ }
+
+ static constexpr const char* _type_key = "relax.FuncStructInfo";
+ TVM_DECLARE_FINAL_OBJECT_INFO(FuncStructInfoNode, StructInfoNode);
+};
+
+/*!
+ * \brief Managed reference to FuncStructInfoNode.
+ * \sa FuncStructInfoNode
+ */
+class FuncStructInfo : public StructInfo {
+ public:
+ /*!
+ * \brief Constructor from parameter struct info and return value struct info.
+ * \param params The struct info of function parameters.
+ * \param ret The return value struct info.
+ * \param span The span of the AST.
+ *
+ * \note If the ret contains variables(tir::Var and relax::Var), they must be deducible from
+ * params. If you are unsure, you can always erase ret to static.
+ */
+ TVM_DLL FuncStructInfo(Array<StructInfo> params, StructInfo ret, Span span = Span());
+
+ /*!
+ * \brief Constructing an opaque function struct info using derive_func.
+ *
+ * \param derive_func Derivation function.
+ * \param span The span of the AST.
+ *
+ * \return The FuncStructInfo for opaque packedfunc.
+ * \note Defaults to an derive func that always return ObjectStructInfo if not specified.
+ */
+ TVM_DLL static FuncStructInfo OpaqueFunc(StructInfoDeriveFunc derive_func, Span span = Span());
+
+ /*!
+ * \brief Construct an opaque function using from return struct info.
+ *
+ * \param ret The struct info of the return value.
+ * \param span The span of the AST.
+ *
+ * \return The FuncStructInfo for opaque packedfunc.
+ * \note Defaults to an derive func that always return ObjectStructInfo if not specified.
+ */
+ TVM_DLL static FuncStructInfo OpaqueFunc(StructInfo ret = ObjectStructInfo(), Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(FuncStructInfo, StructInfo, FuncStructInfoNode);
+};
+
+/*!
+ * \brief Match and check if expr have StructInfo T and return it.
+ *
+ * \param expr The input expression.
+ * \return The result of match.
+ * \tparam T the underlying structure info type
+ */
+template <typename T>
+inline Optional<T> MatchStructInfo(const Expr& expr) {
+ using TNode = typename T::ContainerType;
+ if (const TNode* ptr = expr->struct_info_.as<TNode>()) {
+ return GetRef<T>(ptr);
+ } else {
+ return NullOpt;
+ }
+}
+
+/*!
+ * \brief Get the structure info of a given expr and try to cast it as const T*.
+ *
+ * \param expr The input expression.
+ * \return The pointer. Returns nullptr if the type does not match
+ * \tparam T the underlying structure info type
+ */
+template <typename T>
+inline const T* GetStructInfoAs(const Expr& expr) {
+ ICHECK(expr->struct_info_.defined())
+ << "The struct_info is not populated, check if you have normalized the expr";
+ return expr->struct_info_.as<T>();
+}
+
+/*!
+ * \brief Get the underlying structure info of expr.
+ *
+ * \param expr The input expression.
+ * \return underlying struct info.
+ */
+inline StructInfo GetStructInfo(const Expr& expr) {
+ auto* ptr = expr->struct_info_.as<StructInfoNode>();
+ ICHECK(ptr) << "The struct_info is not populated, check if you have normalized the expr";
+ return GetRef<StructInfo>(ptr);
+}
+
+/*!
+ * \brief Whether the expr has void struct info.
+ *
+ * \param expr The input expression.
+ * \return Whether the expr has void struct info.
+ */
+inline bool HasVoidStructInfo(const Expr& expr) {
+ auto* ptr = expr->struct_info_.as<TupleStructInfoNode>();
+ return ptr != nullptr && ptr->fields.size() == 0;
+}
+
+/*!
+ * \brief Update the struct info of an Expr.
+ * \param expr The Expr whose struct info to be updated.
+ * \param struct_info The struct_info assigned.
+ * \note We ensure idempotence, that is we can only update the struct_info of an Expr only
+ * if the original one is nullptr.
+ */
+TVM_DLL void UpdateStructInfo(Expr expr, StructInfo struct_info);
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_STRUCT_INFO_H_
diff --git a/rust/tvm/src/ir/relay/mod.rs b/rust/tvm/src/ir/relay/mod.rs
index abc25e89c4..08ce082c45 100644
--- a/rust/tvm/src/ir/relay/mod.rs
+++ b/rust/tvm/src/ir/relay/mod.rs
@@ -40,6 +40,7 @@ pub mod attrs;
pub struct ExprNode {
pub base: BaseExprNode,
pub checked_type: Type,
+ pub struct_info: ObjectRef,
pub virtual_device: ObjectRef,
}
@@ -48,6 +49,7 @@ impl ExprNode {
ExprNode {
base: BaseExprNode::base::<T>(span.clone()),
checked_type: Type::null(),
+ struct_info: ObjectRef::null(),
virtual_device: ObjectRef::null(),
}
}
diff --git a/src/relax/ir/struct_info.cc b/src/relax/ir/struct_info.cc
new file mode 100644
index 0000000000..88046ed81f
--- /dev/null
+++ b/src/relax/ir/struct_info.cc
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/ir/struct_info.cc
+ * \brief Relax struct info.
+ */
+#include <tvm/relax/struct_info.h>
+#include <tvm/runtime/registry.h>
+
+namespace tvm {
+namespace relax {
+
+ObjectStructInfo::ObjectStructInfo(Span span) {
+ ObjectPtr<ObjectStructInfoNode> n = make_object<ObjectStructInfoNode>();
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(ObjectStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.ObjectStructInfo").set_body_typed([](Span span) {
+ return ObjectStructInfo(span);
+});
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<ObjectStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ p->stream << "ObjectStructInfo()";
+ });
+
+// Prim
+PrimStructInfo::PrimStructInfo(DataType dtype, Span span) {
+ ObjectPtr<PrimStructInfoNode> n = make_object<PrimStructInfoNode>();
+ n->dtype = dtype;
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(PrimStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.PrimStructInfo").set_body_typed([](DataType dtype, Span span) {
+ return PrimStructInfo(dtype, span);
+});
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<PrimStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const PrimStructInfoNode*>(ref.get());
+ p->stream << "PrimStructInfo(" << node->dtype << ")";
+ });
+
+// Shape
+ShapeStructInfo::ShapeStructInfo(Array<PrimExpr> values, Span span) {
+ ObjectPtr<ShapeStructInfoNode> n = make_object<ShapeStructInfoNode>();
+ n->ndim = static_cast<int>(values.size());
+ n->values = values.Map([](PrimExpr value) {
+ if (value->IsInstance<IntImmNode>()) {
+ return tvm::cast(DataType::Int(64), value);
+ }
+ ICHECK(value.dtype() == DataType::Int(64))
+ << "the value in ShapeStructInfo can only have dtype of int64";
+ return value;
+ });
+ n->span = span;
+ data_ = std::move(n);
+}
+
+ShapeStructInfo::ShapeStructInfo(int ndim, Span span) {
+ ObjectPtr<ShapeStructInfoNode> n = make_object<ShapeStructInfoNode>();
+ CHECK_GE(ndim, -1) << "ndim of ShapeStructInfo must be >= -1, but got " << ndim;
+ n->ndim = ndim;
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(ShapeStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.ShapeStructInfo")
+ .set_body_typed([](Optional<Array<PrimExpr>> values, int ndim, Span span) {
+ if (values.defined()) {
+ CHECK_EQ(ndim, kUnknownNDim) << "ValueError: Cannot both specify values and ndim";
+ return ShapeStructInfo(values.value(), span);
+ } else {
+ return ShapeStructInfo(ndim, span);
+ }
+ });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<ShapeStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const ShapeStructInfoNode*>(ref.get());
+ if (node->values.defined()) {
+ p->stream << "ShapeStructInfo(" << node->values.value() << ")";
+ } else {
+ p->stream << "ShapeStructInfo(ndim=" << node->ndim << ")";
+ }
+ });
+
+// Tensor
+TensorStructInfo::TensorStructInfo(Expr shape, DataType dtype, Span span) {
+ ObjectPtr<TensorStructInfoNode> n = make_object<TensorStructInfoNode>();
+ // assign ndim before move
+ Optional<ShapeStructInfo> sinfo = MatchStructInfo<ShapeStructInfo>(shape);
+ ICHECK(sinfo) << "We expect shape to contain pre-set shape struct info";
+ ICHECK(shape.defined()) << "Must provide a shape in this constructor";
+ ICHECK(shape->IsInstance<ShapeExprNode>() || shape->IsInstance<VarNode>())
+ << "We require shape to be normalized when constructing TensorStructInfo";
+ n->ndim = sinfo.get()->ndim;
+ // assign rest of the fields.
+ n->shape = std::move(shape);
+ n->dtype = dtype;
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TensorStructInfo::TensorStructInfo(DataType dtype, int ndim, Span span) {
+ ObjectPtr<TensorStructInfoNode> n = make_object<TensorStructInfoNode>();
+ CHECK_GE(ndim, -1) << "ndim of TensorStructInfo must be >= -1, but got " << ndim;
+ n->ndim = ndim;
+ n->dtype = dtype;
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(TensorStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.TensorStructInfo")
+ .set_body_typed([](Optional<Expr> shape, DataType dtype, int ndim, Span span) {
+ if (shape.defined()) {
+ CHECK_EQ(ndim, kUnknownNDim) << "ValueError: Cannot both specify shape and ndim";
+ return TensorStructInfo(shape.value(), dtype, span);
+ } else {
+ return TensorStructInfo(dtype, ndim, span);
+ }
+ });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<TensorStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const TensorStructInfoNode*>(ref.get());
+ if (node->shape.defined()) {
+ p->stream << "TensorStructInfo(" << node->shape.value() << ", " << node->dtype << ")";
+ } else {
+ p->stream << "TensorStructInfo(" << node->dtype << ", ndim=" << node->ndim << ")";
+ }
+ });
+
+// Tuple
+TupleStructInfo::TupleStructInfo(Array<StructInfo> fields, Span span) {
+ ObjectPtr<TupleStructInfoNode> n = make_object<TupleStructInfoNode>();
+ n->fields = std::move(fields);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(TupleStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.TupleStructInfo")
+ .set_body_typed([](Array<StructInfo> fields, Span span) {
+ return TupleStructInfo(fields, span);
+ });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<TupleStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const TupleStructInfoNode*>(ref.get());
+ p->stream << "TupleStructInfo(" << node->fields << ")";
+ });
+
+// Func
+FuncStructInfo::FuncStructInfo(Array<StructInfo> params, StructInfo ret, Span span) {
+ ObjectPtr<FuncStructInfoNode> n = make_object<FuncStructInfoNode>();
+ n->params = std::move(params);
+ n->ret = std::move(ret);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+FuncStructInfo FuncStructInfo::OpaqueFunc(StructInfoDeriveFunc derive_func, Span span) {
+ ObjectPtr<FuncStructInfoNode> n = make_object<FuncStructInfoNode>();
+ n->derive_func = std::move(derive_func);
+ n->ret = ObjectStructInfo();
+ n->span = span;
+ return FuncStructInfo(n);
+}
+
+FuncStructInfo FuncStructInfo::OpaqueFunc(StructInfo ret, Span span) {
+ ObjectPtr<FuncStructInfoNode> n = make_object<FuncStructInfoNode>();
+ n->ret = std::move(ret);
+ n->span = span;
+ return FuncStructInfo(n);
+}
+
+TVM_REGISTER_NODE_TYPE(FuncStructInfoNode);
+
+TVM_REGISTER_GLOBAL("relax.FuncStructInfo")
+ .set_body_typed([](Array<StructInfo> params, StructInfo ret, Span span) {
+ return FuncStructInfo(params, ret, span);
+ });
+
+TVM_REGISTER_GLOBAL("relax.FuncStructInfoOpaqueFunc")
+ .set_body_typed([](Optional<StructInfo> ret, Optional<StructInfoDeriveFunc> derive_func,
+ Span span) {
+ if (derive_func.defined()) {
+ ICHECK(!ret.defined()) << "ValueError: Cannot specify both ret and derive_func";
+ return FuncStructInfo::OpaqueFunc(derive_func.value(), span);
+ } else {
+ return FuncStructInfo::OpaqueFunc(ret.value_or(ObjectStructInfo()), span);
+ }
+ });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<FuncStructInfoNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const FuncStructInfoNode*>(ref.get());
+ p->stream << "FuncStructInfo(" << node->params << ", " << node->ret << ")";
+ });
+
+// Helper functions
+// TODO(unity-team): add UpdateStructInfo once analysis.cc is upstreamed
+
+TVM_REGISTER_GLOBAL("ir.ExprStructInfo").set_body_typed([](Expr expr) {
+ return GetStructInfo(expr);
+});
+
+} // namespace relax
+} // namespace tvm
[tvm] 04/07: [Unity][CI] Unity specific jenkins setup (do not upstream to main) (#13910)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 5d3a864e78199d73bc3fde7d733d34fad8873b0f
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Thu Feb 2 23:33:15 2023 -0500
[Unity][CI] Unity specific jenkins setup (do not upstream to main) (#13910)
This PR setup a unity specific jenkins with minimum jenkinsfile
without sharding and disables most of the tests to reduce overall
cost. We can add tests of unty branch by configuring the specific
groovy file.
---
ci/jenkins/generated/arm_jenkinsfile.groovy | 5 +
ci/jenkins/generated/cortexm_jenkinsfile.groovy | 5 +
ci/jenkins/generated/cpu_jenkinsfile.groovy | 5 +
ci/jenkins/generated/docker_jenkinsfile.groovy | 5 +
ci/jenkins/generated/gpu_jenkinsfile.groovy | 5 +
ci/jenkins/generated/hexagon_jenkinsfile.groovy | 5 +
ci/jenkins/generated/i386_jenkinsfile.groovy | 5 +
ci/jenkins/generated/lint_jenkinsfile.groovy | 5 +
.../generated/minimal_cross_isa_jenkinsfile.groovy | 5 +
ci/jenkins/generated/minimal_jenkinsfile.groovy | 5 +
ci/jenkins/generated/riscv_jenkinsfile.groovy | 5 +
ci/jenkins/generated/wasm_jenkinsfile.groovy | 5 +
ci/jenkins/unity_jenkinsfile.groovy | 337 +++++++++++++++++++++
tests/scripts/task_lint.sh | 4 +-
tests/scripts/unity/README | 2 +
tests/scripts/unity/task_extra_lint.sh | 23 ++
tests/scripts/unity/task_python_relax.sh | 37 +++
tests/scripts/unity/task_python_relax_gpuonly.sh | 25 ++
18 files changed, 486 insertions(+), 2 deletions(-)
diff --git a/ci/jenkins/generated/arm_jenkinsfile.groovy b/ci/jenkins/generated/arm_jenkinsfile.groovy
index 4c830dce2c..ffcfa9b842 100644
--- a/ci/jenkins/generated/arm_jenkinsfile.groovy
+++ b/ci/jenkins/generated/arm_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/cortexm_jenkinsfile.groovy b/ci/jenkins/generated/cortexm_jenkinsfile.groovy
index d8a4d4671e..c1a6273670 100644
--- a/ci/jenkins/generated/cortexm_jenkinsfile.groovy
+++ b/ci/jenkins/generated/cortexm_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/cpu_jenkinsfile.groovy b/ci/jenkins/generated/cpu_jenkinsfile.groovy
index cdd2564e05..e689cbb655 100644
--- a/ci/jenkins/generated/cpu_jenkinsfile.groovy
+++ b/ci/jenkins/generated/cpu_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/docker_jenkinsfile.groovy b/ci/jenkins/generated/docker_jenkinsfile.groovy
index 32dec7863b..74e3ddfabe 100644
--- a/ci/jenkins/generated/docker_jenkinsfile.groovy
+++ b/ci/jenkins/generated/docker_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/gpu_jenkinsfile.groovy b/ci/jenkins/generated/gpu_jenkinsfile.groovy
index 390c8ddc3d..f14e8f541b 100644
--- a/ci/jenkins/generated/gpu_jenkinsfile.groovy
+++ b/ci/jenkins/generated/gpu_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/hexagon_jenkinsfile.groovy b/ci/jenkins/generated/hexagon_jenkinsfile.groovy
index 58fe4d14c9..7d5bd3309e 100644
--- a/ci/jenkins/generated/hexagon_jenkinsfile.groovy
+++ b/ci/jenkins/generated/hexagon_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/i386_jenkinsfile.groovy b/ci/jenkins/generated/i386_jenkinsfile.groovy
index b5bf5cb1fe..98e09c393a 100644
--- a/ci/jenkins/generated/i386_jenkinsfile.groovy
+++ b/ci/jenkins/generated/i386_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/lint_jenkinsfile.groovy b/ci/jenkins/generated/lint_jenkinsfile.groovy
index ed5aa8d679..1a3120efb0 100644
--- a/ci/jenkins/generated/lint_jenkinsfile.groovy
+++ b/ci/jenkins/generated/lint_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/minimal_cross_isa_jenkinsfile.groovy b/ci/jenkins/generated/minimal_cross_isa_jenkinsfile.groovy
index 479a4d2f8d..3a17885100 100644
--- a/ci/jenkins/generated/minimal_cross_isa_jenkinsfile.groovy
+++ b/ci/jenkins/generated/minimal_cross_isa_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/minimal_jenkinsfile.groovy b/ci/jenkins/generated/minimal_jenkinsfile.groovy
index 72864ec4ca..ff10d01670 100644
--- a/ci/jenkins/generated/minimal_jenkinsfile.groovy
+++ b/ci/jenkins/generated/minimal_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/riscv_jenkinsfile.groovy b/ci/jenkins/generated/riscv_jenkinsfile.groovy
index 2dfeb35612..df1160b3c1 100644
--- a/ci/jenkins/generated/riscv_jenkinsfile.groovy
+++ b/ci/jenkins/generated/riscv_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/generated/wasm_jenkinsfile.groovy b/ci/jenkins/generated/wasm_jenkinsfile.groovy
index 27e8f6570e..37b50f97ad 100644
--- a/ci/jenkins/generated/wasm_jenkinsfile.groovy
+++ b/ci/jenkins/generated/wasm_jenkinsfile.groovy
@@ -54,6 +54,11 @@
// - Periodically cleanup the old versions on local workers
//
+// unity: Skip less relevant tests
+// to reduce ci time and resource cost
+// (DO NOT UPSTREAM TO MAIN)
+return
+
// ============================= IMPORTANT NOTE =============================
// This file is generated by 'jenkins/generate.py'. Do not edit this file directly!
// Make edits to 'jenkins/Jenkinsfile.j2' and regenerate this with
diff --git a/ci/jenkins/unity_jenkinsfile.groovy b/ci/jenkins/unity_jenkinsfile.groovy
new file mode 100644
index 0000000000..714260a283
--- /dev/null
+++ b/ci/jenkins/unity_jenkinsfile.groovy
@@ -0,0 +1,337 @@
+#!groovy
+// -*- mode: groovy -*-
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Jenkins pipeline
+// See documents at https://jenkins.io/doc/book/pipeline/jenkinsfile/
+
+// ============================= IMPORTANT NOTE =============================
+// To keep things simple
+// This file is manually updated to maintain unity branch specific builds.
+// Please do not send this file to main
+
+
+import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
+
+// NOTE: these lines are scanned by docker/dev_common.sh. Please update the regex as needed. -->
+ci_lint = 'tlcpack/ci-lint:20221025-182121-e41d0ed6e'
+ci_gpu = 'tlcpack/ci-gpu:20221128-070141-ae4fd7df7'
+ci_cpu = 'tlcpack/ci-cpu:20230110-070003-d00168ffb'
+ci_wasm = 'tlcpack/ci-wasm:v0.72'
+ci_i386 = 'tlcpack/ci-i386:v0.75'
+ci_qemu = 'tlcpack/ci-qemu:v0.11'
+ci_arm = 'tlcpack/ci-arm:v0.08'
+ci_hexagon = 'tlcpack/ci-hexagon:20221025-182121-e41d0ed6e'
+// <--- End of regex-scanned config.
+
+// Parameters to allow overriding (in Jenkins UI), the images
+// to be used by a given build. When provided, they take precedence
+// over default values above.
+properties([
+ parameters([
+ string(name: 'ci_lint_param', defaultValue: ''),
+ string(name: 'ci_cpu_param', defaultValue: ''),
+ string(name: 'ci_gpu_param', defaultValue: ''),
+ string(name: 'ci_wasm_param', defaultValue: ''),
+ string(name: 'ci_i386_param', defaultValue: ''),
+ string(name: 'ci_qemu_param', defaultValue: ''),
+ string(name: 'ci_arm_param', defaultValue: ''),
+ string(name: 'ci_hexagon_param', defaultValue: '')
+ ])
+])
+
+// tvm libraries
+tvm_runtime = 'build/libtvm_runtime.so, build/config.cmake'
+tvm_lib = 'build/libtvm.so, ' + tvm_runtime
+// LLVM upstream lib
+tvm_multilib = 'build/libtvm.so, ' +
+ 'build/libvta_fsim.so, ' +
+ tvm_runtime
+
+tvm_multilib_tsim = 'build/libvta_tsim.so, ' +
+ tvm_multilib
+
+// command to start a docker container
+docker_run = 'docker/bash.sh'
+// timeout in minutes
+max_time = 240
+
+def per_exec_ws(folder) {
+ return "workspace/exec_${env.EXECUTOR_NUMBER}/" + folder
+}
+
+// initialize source codes
+def init_git() {
+ checkout scm
+ // Add more info about job node
+ sh (
+ script: './tests/scripts/task_show_node_info.sh',
+ label: 'Show executor node info',
+ )
+ retry(5) {
+ timeout(time: 2, unit: 'MINUTES') {
+ sh (script: 'git submodule update --init -f', label: 'Update git submodules')
+ }
+ }
+}
+
+def should_skip_slow_tests(pr_number) {
+ withCredentials([string(
+ credentialsId: 'tvm-bot-jenkins-reader',
+ variable: 'GITHUB_TOKEN',
+ )]) {
+ // Exit code of 1 means run slow tests, exit code of 0 means skip slow tests
+ result = sh (
+ returnStatus: true,
+ script: "./tests/scripts/should_run_slow_tests.py --pr '${pr_number}'",
+ label: 'Check if CI should run slow tests',
+ )
+ }
+ return result == 0
+}
+
+def cancel_previous_build() {
+ // cancel previous build if it is not on main.
+ if (env.BRANCH_NAME != 'main') {
+ def buildNumber = env.BUILD_NUMBER as int
+ // Milestone API allows us to cancel previous build
+ // with the same milestone number
+ if (buildNumber > 1) milestone(buildNumber - 1)
+ milestone(buildNumber)
+ }
+}
+
+def should_skip_ci(pr_number) {
+ withCredentials([string(
+ credentialsId: 'tvm-bot-jenkins-reader',
+ variable: 'TOKEN',
+ )]) {
+ // Exit code of 1 means run full CI (or the script had an error, so run
+ // full CI just in case). Exit code of 0 means skip CI.
+ git_skip_ci_code = sh (
+ returnStatus: true,
+ script: "./tests/scripts/git_skip_ci.py --pr '${pr_number}'",
+ label: 'Check if CI should be skipped',
+ )
+ }
+ return git_skip_ci_code == 0
+}
+
+cancel_previous_build()
+
+def lint() {
+stage('Prepare') {
+ node('CPU-SMALL') {
+ // When something is provided in ci_*_param, use it, otherwise default with ci_*
+ ci_lint = params.ci_lint_param ?: ci_lint
+ ci_cpu = params.ci_cpu_param ?: ci_cpu
+ ci_gpu = params.ci_gpu_param ?: ci_gpu
+ ci_wasm = params.ci_wasm_param ?: ci_wasm
+ ci_i386 = params.ci_i386_param ?: ci_i386
+ ci_qemu = params.ci_qemu_param ?: ci_qemu
+ ci_arm = params.ci_arm_param ?: ci_arm
+ ci_hexagon = params.ci_hexagon_param ?: ci_hexagon
+
+ sh (script: """
+ echo "Docker images being used in this build:"
+ echo " ci_lint = ${ci_lint}"
+ echo " ci_cpu = ${ci_cpu}"
+ echo " ci_gpu = ${ci_gpu}"
+ echo " ci_wasm = ${ci_wasm}"
+ echo " ci_i386 = ${ci_i386}"
+ echo " ci_qemu = ${ci_qemu}"
+ echo " ci_arm = ${ci_arm}"
+ echo " ci_hexagon = ${ci_hexagon}"
+ """, label: 'Docker image names')
+ }
+}
+
+stage('Sanity Check') {
+ timeout(time: max_time, unit: 'MINUTES') {
+ node('CPU') {
+ ws(per_exec_ws('tvm/sanity')) {
+ init_git()
+ is_docs_only_build = sh (
+ returnStatus: true,
+ script: './tests/scripts/git_change_docs.sh',
+ label: 'Check for docs only changes',
+ )
+ skip_ci = should_skip_ci(env.CHANGE_ID)
+ skip_slow_tests = should_skip_slow_tests(env.CHANGE_ID)
+ sh (
+ script: "${docker_run} ${ci_lint} ./tests/scripts/task_lint.sh",
+ label: 'Run lint',
+ )
+ sh (
+ script: "${docker_run} ${ci_lint} ./tests/scripts/unity/task_extra_lint.sh",
+ label: 'Run extra lint',
+ )
+ }
+ }
+ }
+}
+}
+
+lint()
+
+// Run make. First try to do an incremental make from a previous workspace in hope to
+// accelerate the compilation. If something is wrong, clean the workspace and then
+// build from scratch.
+def make(docker_type, path, make_flag) {
+ timeout(time: max_time, unit: 'MINUTES') {
+ try {
+ cmake_build(docker_type, path, make_flag)
+ // always run cpp test when build
+ // sh "${docker_run} ${docker_type} ./tests/scripts/task_cpp_unittest.sh"
+ } catch (hudson.AbortException ae) {
+ // script exited due to user abort, directly throw instead of retry
+ if (ae.getMessage().contains('script returned exit code 143')) {
+ throw ae
+ }
+ echo 'Incremental compilation failed. Fall back to build from scratch'
+ sh (
+ script: "${docker_run} ${docker_type} ./tests/scripts/task_clean.sh ${path}",
+ label: 'Clear old cmake workspace',
+ )
+ cmake_build(docker_type, path, make_flag)
+ cpp_unittest(docker_type)
+ }
+ }
+}
+
+// Specifications to Jenkins "stash" command for use with various pack_ and unpack_ functions.
+tvm_runtime = 'build/libtvm_runtime.so, build/config.cmake' // use libtvm_runtime.so.
+tvm_lib = 'build/libtvm.so, ' + tvm_runtime // use libtvm.so to run the full compiler.
+// LLVM upstream lib
+tvm_multilib = 'build/libtvm.so, ' +
+ 'build/libvta_fsim.so, ' +
+ tvm_runtime
+
+tvm_multilib_tsim = 'build/libvta_tsim.so, ' +
+ tvm_multilib
+
+microtvm_tar_gz = 'build/microtvm_template_projects.tar.gz'
+
+// pack libraries for later use
+def pack_lib(name, libs) {
+ sh (script: """
+ echo "Packing ${libs} into ${name}"
+ echo ${libs} | sed -e 's/,/ /g' | xargs md5sum
+ """, label: 'Stash libraries and show md5')
+ stash includes: libs, name: name
+}
+
+// unpack libraries saved before
+def unpack_lib(name, libs) {
+ unstash name
+ sh (script: """
+ echo "Unpacked ${libs} from ${name}"
+ echo ${libs} | sed -e 's/,/ /g' | xargs md5sum
+ """, label: 'Unstash libraries and show md5')
+}
+
+// compress microtvm template projects and pack the tar.
+def pack_microtvm_template_projects(name) {
+ sh(
+ script: 'cd build && tar -czvf microtvm_template_projects.tar.gz microtvm_template_projects/',
+ label: 'Compress microtvm_template_projects'
+ )
+ pack_lib(name + '-microtvm-libs', microtvm_tar_gz)
+}
+
+def unpack_microtvm_template_projects(name) {
+ unpack_lib(name + '-microtvm-libs', microtvm_tar_gz)
+ sh(
+ script: 'cd build && tar -xzvf microtvm_template_projects.tar.gz',
+ label: 'Unpack microtvm_template_projects'
+ )
+}
+
+def ci_setup(image) {
+ sh (
+ script: "${docker_run} ${image} ./tests/scripts/task_ci_setup.sh",
+ label: 'Set up CI environment',
+ )
+}
+
+def python_unittest(image) {
+ sh (
+ script: "${docker_run} ${image} ./tests/scripts/task_python_unittest.sh",
+ label: 'Run Python unit tests',
+ )
+}
+
+def fsim_test(image) {
+ sh (
+ script: "${docker_run} ${image} ./tests/scripts/task_python_vta_fsim.sh",
+ label: 'Run VTA tests in FSIM',
+ )
+}
+
+def cmake_build(image, path, make_flag) {
+ sh (
+ script: "${docker_run} ${image} ./tests/scripts/task_build.py --sccache-bucket tvm-sccache-prod",
+ label: 'Run cmake build',
+ )
+}
+
+def cpp_unittest(image) {
+ sh (
+ script: "${docker_run} ${image} ./tests/scripts/task_cpp_unittest.sh",
+ label: 'Build and run C++ tests',
+ )
+}
+
+def add_hexagon_permissions() {
+ sh(
+ script: 'find build/hexagon_api_output -type f | xargs chmod +x',
+ label: 'Add execute permissions for hexagon files',
+ )
+}
+
+// NOTE: limit tests to relax folder for now to allow us to skip some of the tests
+// that are mostly related to changes in main.
+// This helps to speedup CI time and reduce CI cost.
+stage('Build and Test') {
+ if (is_docs_only_build != 1) {
+ parallel 'BUILD: GPU': {
+ node('GPU') {
+ ws(per_exec_ws('tvm/build-gpu')) {
+ init_git()
+ sh "${docker_run} ${ci_gpu} nvidia-smi"
+ sh "${docker_run} ${ci_gpu} ./tests/scripts/task_config_build_gpu.sh build"
+ make("${ci_gpu}", 'build', '-j2')
+ sh "${docker_run} ${ci_gpu} ./tests/scripts/unity/task_python_relax_gpuonly.sh"
+ }
+ }
+ },
+ 'BUILD: CPU': {
+ node('CPU') {
+ ws(per_exec_ws('tvm/build-cpu')) {
+ init_git()
+ sh "${docker_run} ${ci_cpu} ./tests/scripts/task_config_build_cpu.sh build"
+ make(ci_cpu, 'build', '-j2')
+ sh "${docker_run} ${ci_cpu} ./tests/scripts/unity/task_python_relax.sh"
+ }
+ }
+ }
+ } else {
+ Utils.markStageSkippedForConditional('BUILD: CPU')
+ }
+}
diff --git a/tests/scripts/task_lint.sh b/tests/scripts/task_lint.sh
index 83ea86eccc..9ca83ece5c 100755
--- a/tests/scripts/task_lint.sh
+++ b/tests/scripts/task_lint.sh
@@ -31,8 +31,8 @@ function shard1 {
echo "Convert scripts to Python..."
tests/scripts/task_convert_scripts_to_python.sh
- echo "Check Jenkinsfile generation"
- python3 ci/jenkins/generate.py --check
+ # echo "Check Jenkinsfile generation"
+ # python3 ci/jenkins/generate.py --check
echo "Checking file types..."
python3 tests/lint/check_file_type.py
diff --git a/tests/scripts/unity/README b/tests/scripts/unity/README
new file mode 100644
index 0000000000..42f8c3e040
--- /dev/null
+++ b/tests/scripts/unity/README
@@ -0,0 +1,2 @@
+This folder contains CI task scripts that are specialized
+to unity branch, please do not send to other places.
diff --git a/tests/scripts/unity/task_extra_lint.sh b/tests/scripts/unity/task_extra_lint.sh
new file mode 100755
index 0000000000..989f4df738
--- /dev/null
+++ b/tests/scripts/unity/task_extra_lint.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set -euxo pipefail
+
+source tests/scripts/setup-pytest-env.sh
+
+# place extra lint here.
diff --git a/tests/scripts/unity/task_python_relax.sh b/tests/scripts/unity/task_python_relax.sh
new file mode 100755
index 0000000000..8869c318fa
--- /dev/null
+++ b/tests/scripts/unity/task_python_relax.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set -euxo pipefail
+
+source tests/scripts/setup-pytest-env.sh
+export PYTHONPATH=${PYTHONPATH}:${TVM_PATH}/apps/extension/python
+export LD_LIBRARY_PATH="build:${LD_LIBRARY_PATH:-}"
+
+# to avoid CI CPU thread throttling.
+export TVM_BIND_THREADS=0
+export TVM_NUM_THREADS=2
+
+make cython3
+
+# Run Relax tests
+TVM_TEST_TARGETS="${TVM_RELAY_TEST_TARGETS:-llvm}" pytest tests/python/relax
+
+# Run Relax examples
+# python3 ./apps/relax_examples/mlp.py
+# python3 ./apps/relax_examples/nn_module.py
+# python3 ./apps/relax_examples/resnet.py
diff --git a/tests/scripts/unity/task_python_relax_gpuonly.sh b/tests/scripts/unity/task_python_relax_gpuonly.sh
new file mode 100755
index 0000000000..acbcce44f2
--- /dev/null
+++ b/tests/scripts/unity/task_python_relax_gpuonly.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+export TVM_TEST_TARGETS="llvm;cuda"
+export PYTEST_ADDOPTS="-m gpu $PYTEST_ADDOPTS"
+export TVM_RELAY_TEST_TARGETS="cuda"
+export TVM_INTEGRATION_TESTSUITE_NAME=python-integration-gpu
+export TVM_INTEGRATION_GPU_ONLY=1
+
+./tests/scripts/unity/task_python_relax.sh
[tvm] 02/07: [Unity] Relax expressions and types (#13901)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 8ee4b6f7c66ad4baf7a695f1202f188d612b46fd
Author: Yuchen Jin <yu...@cs.washington.edu>
AuthorDate: Thu Feb 2 09:35:41 2023 -0500
[Unity] Relax expressions and types (#13901)
---
include/tvm/ir/expr.h | 8 +
include/tvm/relax/expr.h | 1003 ++++++++++++++++++++++++++++++++++++++++++++++
include/tvm/relax/type.h | 166 ++++++++
3 files changed, 1177 insertions(+)
diff --git a/include/tvm/ir/expr.h b/include/tvm/ir/expr.h
index c8531c8846..d4ba628d36 100644
--- a/include/tvm/ir/expr.h
+++ b/include/tvm/ir/expr.h
@@ -367,6 +367,14 @@ class RelayExprNode : public BaseExprNode {
* This value is discarded during serialization.
*/
mutable Type checked_type_ = Type(nullptr);
+
+ /*!
+ * \brief Stores the result of structure information of the
+ * expression that encapsulate both static shape and
+ * runtime information such as shape.
+ */
+ mutable Optional<ObjectRef> struct_info_ = Optional<ObjectRef>();
+
/*!
* \return The checked_type
*/
diff --git a/include/tvm/relax/expr.h b/include/tvm/relax/expr.h
new file mode 100644
index 0000000000..8154b1dd86
--- /dev/null
+++ b/include/tvm/relax/expr.h
@@ -0,0 +1,1003 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_RELAX_EXPR_H_
+#define TVM_RELAX_EXPR_H_
+
+#include <tvm/ir/expr.h>
+#include <tvm/ir/source_map.h>
+#include <tvm/node/node.h>
+#include <tvm/relax/type.h>
+#include <tvm/relay/expr.h>
+#include <tvm/runtime/container/array.h>
+#include <tvm/runtime/container/map.h>
+#include <tvm/runtime/object.h>
+#include <tvm/tir/expr.h>
+#include <tvm/tir/op.h>
+
+namespace tvm {
+namespace relax {
+
+using Expr = RelayExpr;
+using ExprNode = RelayExprNode;
+using relay::Id;
+
+/*!
+ * \brief Base type of all structure information.
+ *
+ * StructInfo stores possible structure information
+ * deduced during compile-time. It encapsulates
+ * both static type and runtime information such
+ * as shape.
+ *
+ * StructInfo of each non-primitive Expr can be
+ * deduced during compilation in a "best-effort" manner.
+ *
+ * When struct_info appears in function parameter and return
+ * signatures. They will imply a runtime check that matches
+ * the structure information with the value.
+ *
+ * When it appears in Expr, they follow "assume-semantics",
+ * which means the compiler will take the deduced information as it is
+ * and only do best effort prove and checks.
+ *
+ * Each struct info can be uniquely erased to a static-type.
+ * The compiler will still compile the code(with less information)
+ * when we erase to the static type.
+ *
+ * If an StructInfo contains an Expr field, then that field
+ * must be normalized already through NormalizeArg.
+ * This invariant will be checked in constructors
+ * and help us to simplify our assumption
+ * during struct info deduction.
+ */
+class StructInfoNode : public Object {
+ public:
+ /*!
+ * \brief Span that points to the original source code.
+ * Reserved debug information.
+ */
+ mutable Span span;
+
+ static constexpr const char* _type_key = "StructInfo";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ static constexpr const uint32_t _type_child_slots = 5;
+ TVM_DECLARE_BASE_OBJECT_INFO(StructInfoNode, Object);
+};
+
+/*!
+ * \brief Managed reference to StructInfoNode.
+ * \sa StructInfoNode
+ */
+class StructInfo : public ObjectRef {
+ public:
+ TVM_DEFINE_OBJECT_REF_METHODS(StructInfo, ObjectRef, StructInfoNode);
+};
+
+/*!
+ * \brief Call corresponds to callable invocation.
+ * Corresponds to operation in computational graph terminology.
+ */
+class CallNode : public ExprNode {
+ public:
+ /*!
+ * \brief The operator(function) being invoked
+ *
+ * - It can be tvm::Op which corresponds to the primitive operators.
+ * - It can also be user defined functions (Function, GlobalVar, Var).
+ */
+ Expr op;
+
+ /*! \brief The arguments(inputs) of the call */
+ tvm::Array<Expr> args;
+
+ /*! \brief The additional attributes */
+ Attrs attrs;
+
+ /*!
+ * \brief The structure info arguments of a CallNode.
+ * sinfo_args is designed to be non-empty only for intrinsic op (e.g.,
+ * call_tir, call_builtin_with_ctx, etc.) and calls to ExternFuncs, with the main
+ * usage of structure info inference.
+ */
+ Array<StructInfo> sinfo_args;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("op", &op);
+ v->Visit("args", &args);
+ v->Visit("attrs", &attrs);
+ v->Visit("sinfo_args", &sinfo_args);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const CallNode* other, SEqualReducer equal) const {
+ // skip sinfo_args check for primitive ops.
+ equal->MarkGraphNode();
+ return equal(op, other->op) && equal(args, other->args) && equal(attrs, other->attrs) &&
+ (IsPrimitiveOp(op) || equal(sinfo_args, other->sinfo_args)) &&
+ equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce->MarkGraphNode();
+ hash_reduce(op);
+ hash_reduce(args);
+ hash_reduce(attrs);
+ if (!IsPrimitiveOp(op)) {
+ hash_reduce(sinfo_args);
+ }
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.Call";
+ TVM_DECLARE_FINAL_OBJECT_INFO(CallNode, ExprNode);
+};
+
+class Call : public Expr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param op The operator to be invoked.
+ * \param args The arguments of the call.
+ * \param attrs The attributes of the call node.
+ * \param sinfo_args The structure info arguments passed to a function.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL Call(Expr op, Array<Expr> args, Attrs attrs = Attrs(),
+ Array<StructInfo> sinfo_args = Array<StructInfo>(), Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(Call, Expr, CallNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(CallNode);
+};
+
+/*!
+ * \brief Returns \p call with the given properties. A null property denotes 'no change'.
+ * Returns \p call if all properties are unchanged. Otherwise, returns a copy with the new
+ * fields.
+ */
+Call WithFields(Call call, Optional<Expr> opt_op = Optional<Expr>(),
+ Optional<Array<Expr>> opt_args = Optional<Array<Expr>>(),
+ Optional<Attrs> opt_attrs = Optional<Attrs>(),
+ Optional<Array<StructInfo>> opt_sinfo_args = Optional<Array<StructInfo>>(),
+ Optional<Span> opt_span = Optional<Span>());
+
+/*!
+ * \brief Condition expression
+ *
+ * Unlike traditional statement `if`s, the if evalutes
+ * to the result of the branch taken.
+ *
+ * x = if (true) { 1 } else { 0 }; // x is 1
+ * y = if (false) { 1 } else { 0 }; // y is 0
+ *
+ * \note This is similar to C's ternary operator.
+ */
+class IfNode : public ExprNode {
+ public:
+ /*! \brief The condition. */
+ Expr cond;
+ /*! \brief The expression evaluated when condition is true. */
+ Expr true_branch;
+ /*! \brief The expression evaluated when condition is false */
+ Expr false_branch;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("cond", &cond);
+ v->Visit("true_branch", &true_branch);
+ v->Visit("false_branch", &false_branch);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const IfNode* other, SEqualReducer equal) const {
+ equal->MarkGraphNode();
+ return equal(cond, other->cond) && equal(true_branch, other->true_branch) &&
+ equal(false_branch, other->false_branch) && equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce->MarkGraphNode();
+ hash_reduce(cond);
+ hash_reduce(true_branch);
+ hash_reduce(false_branch);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.If";
+ TVM_DECLARE_FINAL_OBJECT_INFO(IfNode, ExprNode);
+};
+
+class If : public Expr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param cond The condition of a if node.
+ * \param true_branch The fall through branch
+ * \param false_branch The branch for execution when condition is false.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL If(Expr cond, Expr true_branch, Expr false_branch, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(If, Expr, IfNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(IfNode);
+};
+
+/*!
+ * \brief Returns \p if_expr with the given properties. A null property denotes 'no change'.
+ * Returns \p if_expr if all properties are unchanged. Otherwise, returns a copy with the new
+ * fields.
+ */
+If WithFields(If if_expr, Optional<Expr> opt_cond = Optional<Expr>(),
+ Optional<Expr> opt_true_branch = Optional<Expr>(),
+ Optional<Expr> opt_false_branch = Optional<Expr>(),
+ Optional<Span> opt_span = Optional<Span>());
+
+/*! \brief Tuple container */
+class TupleNode : public ExprNode {
+ public:
+ /*! \brief the fields of the tuple */
+ tvm::Array<Expr> fields;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("fields", &fields);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const TupleNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from fields.
+ return equal(fields, other->fields);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(fields); }
+
+ static constexpr const char* _type_key = "relax.expr.Tuple";
+ TVM_DECLARE_FINAL_OBJECT_INFO(TupleNode, ExprNode);
+};
+
+class Tuple : public Expr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param fields The fields of a tuple.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL explicit Tuple(tvm::Array<Expr> fields, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(Tuple, Expr, TupleNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleNode);
+};
+
+/*!
+ * \brief Returns \p tuple with the given properties. A null property denotes 'no change'.
+ * Returns \p tuple if all properties are unchanged. Otherwise, returns a copy with the new
+ * fields.
+ */
+Tuple WithFields(Tuple tuple, Optional<Array<Expr>> opt_fields = Optional<Array<Expr>>(),
+ Optional<Span> opt_span = Optional<Span>());
+
+/*! \brief Get index-th field out of a tuple. */
+class TupleGetItemNode : public ExprNode {
+ public:
+ /*! \brief The tuple Expression */
+ Expr tuple;
+ /*! \brief which value to get */
+ int index;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("tuple_value", &tuple);
+ v->Visit("index", &index);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const TupleGetItemNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically tuple and index.
+ return equal(tuple, other->tuple) && equal(index, other->index);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(tuple);
+ hash_reduce(index);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.TupleGetItem";
+ TVM_DECLARE_FINAL_OBJECT_INFO(TupleGetItemNode, ExprNode);
+};
+
+class TupleGetItem : public Expr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param tuple The tuple to get an element from.
+ * \param index The index for extracting a value in the tuple.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL TupleGetItem(Expr tuple, int index, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(TupleGetItem, Expr, TupleGetItemNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleGetItemNode);
+};
+
+/*!
+ * \brief Returns \p tuple_get_item with the given properties. A null property denotes 'no change'.
+ * Returns \p tuple_get_item if all properties are unchanged. Otherwise, returns a copy with the new
+ * fields.
+ */
+TupleGetItem WithFields(TupleGetItem tuple_get_item, Optional<Expr> opt_tuple = Optional<Expr>(),
+ Optional<Integer> opt_index = Optional<Integer>(),
+ Optional<Span> opt_span = Optional<Span>());
+
+/*!
+ * \brief Base type of all (non-function) leaf Exprs.
+ * \sa Expr
+ */
+class LeafExprNode : public ExprNode {
+ public:
+ static constexpr const char* _type_key = "relax.expr.LeafExpr";
+ static constexpr const uint32_t _type_child_slots = 7;
+ TVM_DECLARE_BASE_OBJECT_INFO(LeafExprNode, ExprNode);
+};
+
+/*!
+ * \brief Managed reference to BaseExprNode.
+ * \sa LeafExprNode
+ */
+class LeafExpr : public Expr {
+ public:
+ TVM_DEFINE_OBJECT_REF_METHODS(LeafExpr, Expr, LeafExprNode);
+};
+
+/*! \brief A shape expression which allows users to construct a shape containing PrimExpr.
+ */
+class ShapeExprNode : public LeafExprNode {
+ public:
+ /*! The values of the shape expression. */
+ Array<PrimExpr> values;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("values", &values);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const ShapeExprNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from values.
+ return equal(values, other->values);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(values); }
+
+ static constexpr const char* _type_key = "relax.expr.ShapeExpr";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(ShapeExprNode, LeafExprNode);
+};
+
+class ShapeExpr : public LeafExpr {
+ public:
+ TVM_DLL explicit ShapeExpr(Array<PrimExpr> values, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(ShapeExpr, LeafExpr, ShapeExprNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(ShapeExprNode);
+};
+
+/*! \brief The variable class for all Relax bindings. */
+class VarNode : public LeafExprNode {
+ public:
+ /*! \brief The identifier of the variable, which is used for comparing stable equality across
+ * transformations. */
+ Id vid;
+
+ /*! \return The name hint of the variable */
+ const String& name_hint() const { return vid->name_hint; }
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("vid", &vid);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const VarNode* other, SEqualReducer equal) const {
+ equal->MarkGraphNode();
+ return equal(vid, other->vid) && equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(vid);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.Var";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ static constexpr const uint32_t _type_child_slots = 2;
+ TVM_DECLARE_BASE_OBJECT_INFO(VarNode, LeafExprNode);
+};
+
+class Var : public LeafExpr {
+ public:
+ TVM_DLL explicit Var(String name_hint, Optional<StructInfo> struct_info_annotation,
+ Span span = Span())
+ : Var(Id(name_hint), struct_info_annotation, span) {}
+
+ TVM_DLL explicit Var(Id vid, Optional<StructInfo> struct_info_annotation, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(Var, LeafExpr, VarNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(VarNode);
+};
+
+/*! \brief A sub-type of the variable node used to mark dataflow variables from
+ * normal visible "function local" bindings.
+ */
+class DataflowVarNode : public VarNode {
+ public:
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("vid", &vid);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const DataflowVarNode* other, SEqualReducer equal) const {
+ equal->MarkGraphNode();
+ return equal(vid, other->vid) && equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(vid);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.DataflowVar";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(DataflowVarNode, VarNode);
+};
+
+class DataflowVar : public Var {
+ public:
+ TVM_DLL explicit DataflowVar(String name_hint, Optional<StructInfo> struct_info_annotation,
+ Span span = Span())
+ : DataflowVar(Id(name_hint), struct_info_annotation, span) {}
+
+ TVM_DLL explicit DataflowVar(Id vid, Optional<StructInfo> struct_info_annotation,
+ Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(DataflowVar, Var, DataflowVarNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(DataflowVarNode);
+};
+
+/*!
+ * \brief Constant tensor.
+ *
+ * \note Scalar constants are represented by ndim-0 constant tensors.
+ */
+class ConstantNode : public LeafExprNode {
+ public:
+ /*! \brief The data of the tensor */
+ runtime::NDArray data;
+
+ /*! \return The corresponding tensor type of the data */
+ TensorType tensor_type() const;
+
+ /*! \return Whether it is scalar(ndim-0 tensor) */
+ bool is_scalar() const { return data->ndim == 0; }
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("data", &data);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const ConstantNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from data.
+ return equal(data, other->data);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(data); }
+
+ static constexpr const char* _type_key = "relax.expr.Constant";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ConstantNode, LeafExprNode);
+};
+
+class Constant : public LeafExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param data The data of the constant tensor.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL explicit Constant(runtime::NDArray data, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(Constant, LeafExpr, ConstantNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(ConstantNode);
+};
+
+/*!
+ * \brief PrimValue.
+ *
+ * Expression representing a TIR POD expression.
+ */
+class PrimValueNode : public LeafExprNode {
+ public:
+ /*! \brief The prim expr representing the value */
+ PrimExpr value;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("value", &value);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const PrimValueNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from data.
+ return equal(value, other->value);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(value); }
+
+ static constexpr const char* _type_key = "relax.expr.PrimValue";
+ TVM_DECLARE_FINAL_OBJECT_INFO(PrimValueNode, LeafExprNode);
+};
+
+/*!
+ * \brief Managed reference to PrimValueNode
+ * \sa PrimValeNode
+ */
+class PrimValue : public LeafExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param value The value input.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL explicit PrimValue(PrimExpr value, Span span = Span());
+
+ /*!
+ * \brief Create a int64 prim value.
+ * \param value The input value.
+ * \param span The source span of the expression.
+ * \return The created prim value.
+ */
+ TVM_DLL static PrimValue Int64(int64_t value, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(PrimValue, LeafExpr, PrimValueNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(PrimValueNode);
+};
+
+/*!
+ * \brief Represent a string literal constant.
+ */
+class StringImmNode : public LeafExprNode {
+ public:
+ /*! \brief The data value. */
+ String value;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("value", &value);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const StringImmNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from data.
+ return equal(value, other->value);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(value); }
+
+ static constexpr const char* _type_key = "relax.expr.StringImm";
+ TVM_DECLARE_FINAL_OBJECT_INFO(StringImmNode, LeafExprNode);
+};
+
+/*!
+ * \brief Managed reference to StringImm
+ * \sa StringImmNode
+ */
+class StringImm : public LeafExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param value The value input.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL explicit StringImm(String value, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(StringImm, LeafExpr, StringImmNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(StringImmNode);
+};
+
+/*!
+ * \brief Represent a data type constant.
+ */
+class DataTypeImmNode : public LeafExprNode {
+ public:
+ /*! \brief The data value. */
+ DataType value;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("value", &value);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const DataTypeImmNode* other, SEqualReducer equal) const {
+ // struct info can be deterministically derived from data.
+ return equal(value, other->value);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(value); }
+
+ static constexpr const char* _type_key = "relax.expr.DataTypeImm";
+ TVM_DECLARE_FINAL_OBJECT_INFO(DataTypeImmNode, LeafExprNode);
+};
+
+/*!
+ * \brief Managed reference to DataTypeImm
+ * \sa DataTypeImmNode
+ */
+class DataTypeImm : public LeafExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param value The value input.
+ * \param span The source span of the expression.
+ */
+ TVM_DLL explicit DataTypeImm(DataType value, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(DataTypeImm, LeafExpr, DataTypeImmNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(DataTypeImmNode);
+};
+
+/*! \brief The base class of a variable binding in Relax. */
+class BindingNode : public Object {
+ public:
+ /*! \brief The return variable to bound to. */
+ Var var;
+ mutable Span span;
+
+ static constexpr const char* _type_key = "relax.expr.Binding";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_BASE_OBJECT_INFO(BindingNode, Object);
+};
+
+class Binding : public ObjectRef {
+ protected:
+ Binding() = default;
+
+ public:
+ explicit Binding(ObjectPtr<Object> n) : ObjectRef(n) {}
+ TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(Binding);
+ const BindingNode* operator->() const { return static_cast<const BindingNode*>(data_.get()); }
+ const BindingNode* get() const { return operator->(); }
+ using ContainerType = BindingNode;
+};
+
+/*!
+ * \brief Runtime-match the value to the struct info.
+ *
+ * This operation does runtime check, populates the un-defined symbolic shape vars
+ * and vars in struct_info in first occurance, and insert equality assertions in
+ * other cases.
+ */
+class MatchCastNode : public BindingNode {
+ public:
+ /*! \brief The input value to match cast. */
+ Expr value;
+ /*! \brief The struct info pattern to match to. */
+ StructInfo struct_info;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("var", &var);
+ v->Visit("value", &value);
+ v->Visit("struct_info", &struct_info);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const MatchCastNode* other, SEqualReducer equal) const {
+ // NOTE: pattern can contain ShapeExpr which defines the vars
+ return equal.DefEqual(var, other->var) && equal.DefEqual(struct_info, other->struct_info) &&
+ equal(value, other->value);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ // NOTE: pattern can contain ShapeExpr which defines the vars
+ hash_reduce.DefHash(var);
+ hash_reduce.DefHash(struct_info);
+ hash_reduce(value);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.MatchCast";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(MatchCastNode, BindingNode);
+};
+
+/*!
+ * \brief Managed reference to MatchCastNode.
+ * \sa MatchCastNode
+ */
+class MatchCast : public Binding {
+ public:
+ TVM_DLL explicit MatchCast(Var var, Expr value, StructInfo struct_info, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(MatchCast, Binding, MatchCastNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(MatchCastNode);
+};
+
+class VarBindingNode : public BindingNode {
+ public:
+ /*! \brief The binding value. */
+ Expr value;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("var", &var);
+ v->Visit("value", &value);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const VarBindingNode* other, SEqualReducer equal) const {
+ return equal.DefEqual(var, other->var) && equal(value, other->value);
+ }
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce.DefHash(var);
+ hash_reduce(value);
+ }
+ static constexpr const char* _type_key = "relax.expr.VarBinding";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(VarBindingNode, BindingNode);
+};
+
+class VarBinding : public Binding {
+ public:
+ TVM_DLL explicit VarBinding(Var var, Expr value, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(VarBinding, Binding, VarBindingNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(VarBindingNode);
+};
+
+class BindingBlockNode : public Object {
+ public:
+ mutable Span span;
+ Array<Binding> bindings;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("span", &span);
+ v->Visit("bindings", &bindings);
+ }
+
+ bool SEqualReduce(const BindingBlockNode* other, SEqualReducer equal) const {
+ return equal(bindings, other->bindings);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(bindings); }
+
+ static constexpr const char* _type_key = "relax.expr.BindingBlock";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_BASE_OBJECT_INFO(BindingBlockNode, Object);
+};
+
+class BindingBlock : public ObjectRef {
+ public:
+ TVM_DLL explicit BindingBlock(Array<Binding> bindings, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(BindingBlock, ObjectRef, BindingBlockNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(BindingBlockNode);
+};
+
+class DataflowBlock;
+class DataflowBlockNode : public BindingBlockNode {
+ public:
+ bool SEqualReduce(const DataflowBlockNode* other, SEqualReducer equal) const {
+ return equal(bindings, other->bindings);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(bindings); }
+
+ static constexpr const char* _type_key = "relax.expr.DataflowBlock";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(DataflowBlockNode, BindingBlockNode);
+};
+
+class DataflowBlock : public BindingBlock {
+ public:
+ TVM_DLL explicit DataflowBlock(Array<Binding> bindings, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(DataflowBlock, BindingBlock, DataflowBlockNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(DataflowBlockNode);
+};
+
+/*! \brief A sequence of blocks followed by an expression.
+ *
+ * The order of blocks enforces scoping and ordering.
+ */
+class SeqExprNode : public ExprNode {
+ public:
+ Array<BindingBlock> blocks;
+ Expr body;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("blocks", &blocks);
+ v->Visit("body", &body);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const SeqExprNode* other, SEqualReducer equal) const {
+ return equal(blocks, other->blocks) && equal(body, other->body) &&
+ equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(blocks);
+ hash_reduce(body);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.SeqExpr";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(SeqExprNode, ExprNode);
+};
+
+class SeqExpr : public Expr {
+ public:
+ TVM_DLL explicit SeqExpr(Array<BindingBlock> blocks, Expr body, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(SeqExpr, Expr, SeqExprNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(SeqExprNode);
+};
+
+/*! \brief A Relax function. */
+class FunctionNode : public BaseFuncNode {
+ public:
+ /*! \brief The parameters to the function. */
+ Array<Var> params;
+ /*! \brief The body of the function. */
+ Expr body;
+ /*! \brief The return type of the function. */
+ StructInfo ret_struct_info;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("params", ¶ms);
+ v->Visit("body", &body);
+ v->Visit("ret_struct_info", &ret_struct_info);
+ v->Visit("attrs", &attrs);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const FunctionNode* other, SEqualReducer equal) const {
+ equal->MarkGraphNode();
+ return equal.DefEqual(params, other->params) && equal(body, other->body) &&
+ equal(ret_struct_info, other->ret_struct_info) && equal(attrs, other->attrs) &&
+ equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce->MarkGraphNode();
+ hash_reduce.DefHash(params);
+ hash_reduce(body);
+ hash_reduce(ret_struct_info);
+ hash_reduce(attrs);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.Function";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(FunctionNode, BaseFuncNode);
+};
+
+class Function : public BaseFunc {
+ public:
+ TVM_DLL explicit Function(Array<Var> params, Expr body, Optional<StructInfo> ret_struct_info,
+ DictAttrs attrs = NullValue<DictAttrs>(), Span span = Span());
+
+ /*!
+ * \brief Mimics the constructor but without body Expr.
+ * \note ret_struct_info is required, since it can not deduced by the body
+ */
+ TVM_DLL static Function CreateEmpty(Array<Var> params, StructInfo ret_struct_info,
+ DictAttrs attrs = NullValue<DictAttrs>(), Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(Function, BaseFunc, FunctionNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionNode);
+};
+
+// TODO(@sunggg): Investigate the exact usage of kComposite, kPartitionedFromPattern, and
+// kPrimitive.
+namespace attr {
+/*! \brief Mark the function as a primitive function. */
+constexpr const char* kPrimitive = "Primitive";
+/*!
+ * \brief Indicate the codegen that should be used for building this function.
+ * When this is unset or set to "default", the default compilation pipeline will be used.
+ */
+constexpr const char* kCodegen = "Codegen";
+/*! \brief Treat the function as a composite operator. */
+constexpr const char* kComposite = "Composite";
+/*! \brief Indicate the function was created by the Pattern Partitioning Pass. */
+constexpr const char* kPartitionedFromPattern = "PartitionedFromPattern";
+} // namespace attr
+
+/*! \brief The extern function, which can represent packed function. */
+class ExternFuncNode : public BaseFuncNode {
+ public:
+ /*! \brief The name of global symbol. */
+ String global_symbol;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("global_symbol", &global_symbol);
+ v->Visit("struct_info_", &struct_info_);
+ v->Visit("_checked_type_", &checked_type_);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const ExternFuncNode* other, SEqualReducer equal) const {
+ return equal(global_symbol, other->global_symbol) && equal(struct_info_, other->struct_info_);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(global_symbol);
+ hash_reduce(struct_info_);
+ }
+
+ static constexpr const char* _type_key = "relax.expr.ExternFunc";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(ExternFuncNode, BaseFuncNode);
+};
+
+class ExternFunc : public BaseFunc {
+ public:
+ TVM_DLL ExternFunc(String global_symbol, Span span = Span());
+ TVM_DEFINE_OBJECT_REF_METHODS(ExternFunc, BaseFunc, ExternFuncNode);
+ TVM_DEFINE_OBJECT_REF_COW_METHOD(ExternFuncNode);
+};
+
+/*!
+ * \brief Get the shape of Expr.
+ * \param expr The input expr.
+ * \return The corresonding shape.
+ *
+ * \note This function requires expr to be normalized.
+ * The function will report an error if expr's StructInfo is not TensorStructInfo.
+ * It will try to return symbolic function when possible. If the tensor do not
+ * have a compile-time symbolic shape, the function will then choose to return
+ * Call(relax.op.shape_of, [expr]).
+ */
+TVM_DLL Expr GetShapeOf(const Expr& expr);
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_EXPR_H_
diff --git a/include/tvm/relax/type.h b/include/tvm/relax/type.h
new file mode 100644
index 0000000000..9c20a52435
--- /dev/null
+++ b/include/tvm/relax/type.h
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/type.h
+ * \brief Relax Types.
+ */
+#ifndef TVM_RELAX_TYPE_H_
+#define TVM_RELAX_TYPE_H_
+
+#include <tvm/ir/attrs.h>
+#include <tvm/ir/env_func.h>
+#include <tvm/ir/tensor_type.h>
+#include <tvm/ir/type.h>
+#include <tvm/ir/type_relation.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/tir/expr.h>
+
+#include <string>
+
+namespace tvm {
+namespace relax {
+
+/*! \brief Indicates the number of dimensions of a tensor is unknown at compile time. */
+static constexpr int kUnknownNDim = -1;
+
+class ShapeTypeNode : public TypeNode {
+ public:
+ /*! \brief size of the shape. */
+ int ndim;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("ndim", &ndim);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const ShapeTypeNode* other, SEqualReducer equal) const {
+ return equal(ndim, other->ndim);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(ndim); }
+
+ static constexpr const char* _type_key = "relax.ShapeType";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTypeNode, TypeNode);
+};
+
+class ShapeType : public Type {
+ public:
+ // TODO(relax-team): remove the default value later.
+ TVM_DLL ShapeType(int ndim = kUnknownNDim, Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ShapeType, Type, ShapeTypeNode);
+};
+
+class ObjectTypeNode : public TypeNode {
+ public:
+ void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("span", &span); }
+
+ bool SEqualReduce(const ObjectTypeNode* other, SEqualReducer equal) const { return true; }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(0); }
+
+ static constexpr const char* _type_key = "relax.ObjectType";
+ TVM_DECLARE_FINAL_OBJECT_INFO(ObjectTypeNode, TypeNode);
+};
+
+class ObjectType : public Type {
+ public:
+ TVM_DLL ObjectType(Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ObjectType, Type, ObjectTypeNode);
+};
+
+class DynTensorTypeNode : public BaseTensorTypeNode {
+ public:
+ /*!
+ * \brief The number of dimensions of the tensor, use -1 to denote tensor with unknwon number of
+ * dimensions.
+ */
+ int ndim;
+ /*! \brief The content data type, use void to denote the dtype is unknown. */
+ DataType dtype;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {
+ v->Visit("ndim", &ndim);
+ v->Visit("dtype", &dtype);
+ v->Visit("span", &span);
+ }
+
+ bool SEqualReduce(const DynTensorTypeNode* other, SEqualReducer equal) const {
+ return equal(ndim, other->ndim) && equal(dtype, other->dtype);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const {
+ hash_reduce(ndim);
+ hash_reduce(dtype);
+ }
+
+ inline bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
+
+ inline bool IsUnknownDtype() const { return dtype.is_void(); }
+
+ static constexpr const char* _type_key = "relax.DynTensorType";
+ TVM_DECLARE_FINAL_OBJECT_INFO(DynTensorTypeNode, BaseTensorTypeNode);
+};
+
+/*!
+ * \brief Managed reference to DynTensorTypeNode.
+ * \sa DynTensorTypeNode.
+ */
+class DynTensorType : public Type {
+ public:
+ /*!
+ * \brief Constructor.
+ * \param ndim The number of dimensions of the tensor.
+ * \param dtype The runtime dtype of the tensor's elements.
+ * \param span The span.
+ */
+ TVM_DLL DynTensorType(int ndim, DataType dtype, Span span = Span());
+
+ /*!
+ * \brief Create a DynTensorType with unknown ndim.
+ */
+ TVM_DLL static DynTensorType CreateUnknownNDim(DataType dtype, Span span = Span());
+
+ TVM_DEFINE_OBJECT_REF_METHODS(DynTensorType, Type, DynTensorTypeNode);
+};
+
+class PackedFuncTypeNode : public TypeNode {
+ public:
+ void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("span", &span); }
+
+ bool SEqualReduce(const PackedFuncTypeNode* other, SEqualReducer equal) const { return true; }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(0); }
+
+ static constexpr const char* _type_key = "relax.PackedFuncType";
+ TVM_DECLARE_FINAL_OBJECT_INFO(PackedFuncTypeNode, TypeNode);
+};
+
+class PackedFuncType : public Type {
+ public:
+ TVM_DLL PackedFuncType(Span span = Span());
+
+ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(PackedFuncType, Type, PackedFuncTypeNode);
+};
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_TYPE_H_
[tvm] 06/07: [Unity] Relax BlockBuilder and ExprMutator (#13926)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 284be6ee87cd17c5cc197295b68a29093aecde8f
Author: Yuchen Jin <yu...@cs.washington.edu>
AuthorDate: Tue Feb 7 01:30:11 2023 -0500
[Unity] Relax BlockBuilder and ExprMutator (#13926)
This PR adds BlockBuilder: the core data structure to construct Relax AST, and ExprMutator: performs AST mutation for implementing transformation passes.
Co-Authored-by: Tianqi Chen <ti...@gmail.com>
Co-Authored-by: Altan Haan <al...@cs.washington.edu>
Co-Authored-by: Andrew Liu <an...@gmail.com>
Co-Authored-by: Hongyi Jin <32...@qq.com>
Co-Authored-by: Jiawei Liu <ja...@gmail.com>
Co-Authored-by: Junru Shao <ju...@gmail.com>
Co-Authored-by: Lesheng Jin <34...@users.noreply.github.com>
Co-Authored-by: masahi <ma...@gmail.com>
Co-Authored-by: Prakalp Srivastava <pr...@octoml.ai>
Co-Authored-by: Ruihang Lai <ru...@cs.cmu.edu>
Co-Authored-by: Siyuan Feng <Hz...@sjtu.edu.cn>
Co-Authored-by: Steven S. <Lyubomirsky slyubomirsky@octoml.ai>
Co-Authored-by: Sunghyun Park <49...@users.noreply.github.com>
Co-Authored-by: Yixin Dong <ub...@gmail.com>
Co-Authored-by: Yong Wu <yo...@gmail.com>
Co-Authored-by: Ziheng Jiang <zi...@apache.org>
---
CMakeLists.txt | 2 +
include/tvm/relax/analysis.h | 13 +
include/tvm/relax/block_builder.h | 239 +++
include/tvm/relax/expr.h | 7 +-
include/tvm/relax/expr_functor.h | 138 +-
include/tvm/relax/op_attr_types.h | 75 +
include/tvm/relax/struct_info.h | 5 +-
include/tvm/relax/utils.h | 155 ++
include/tvm/te/operation.h | 2 +-
python/tvm/ir/function.py | 16 +
python/tvm/meta_schedule/utils.py | 50 +-
python/tvm/relax/__init__.py | 11 +
python/tvm/relax/analysis/analysis.py | 33 +-
python/tvm/relax/block_builder.py | 801 ++++++++++
python/tvm/relax/expr_functor.py | 1530 ++++++++++++++++++++
python/tvm/relax/op/__init__.py | 22 +
python/tvm/relax/op/_ffi_api.py | 19 +
python/tvm/relax/op/base.py | 358 +++++
python/tvm/relax/op/binary.py | 67 +
python/tvm/relax/utils.py | 278 ++++
python/tvm/te/__init__.py | 1 +
python/tvm/te/operation.py | 54 +-
src/ir/function.cc | 14 +
src/relax/analysis/struct_info_analysis.cc | 149 ++
src/relax/ir/block_builder.cc | 969 +++++++++++++
src/relax/ir/emit_te.cc | 78 +
src/relax/ir/emit_te.h | 68 +
src/relax/ir/expr_functor.cc | 244 ++++
src/relax/ir/py_expr_functor.cc | 649 +++++++++
src/relax/op/op.cc | 77 +
src/relax/op/op_common.cc | 122 ++
src/relax/op/op_common.h | 285 ++++
src/relax/op/tensor/binary.cc | 87 ++
src/relax/op/tensor/binary.h | 70 +
src/relax/utils.cc | 41 +
src/te/operation/create_primfunc.cc | 80 +
src/te/operation/create_primfunc.h | 17 +
.../relax/test_analysis_struct_info_analysis.py | 143 ++
tests/python/relax/test_blockbuilder.py | 542 +++++++
tests/python/relax/test_expr.py | 4 +-
tests/python/relax/test_expr_functor.py | 746 ++++++++++
41 files changed, 8233 insertions(+), 28 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f6d5db3098..ab21d90e00 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -290,8 +290,10 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
src/support/*.cc
src/script/*.cc
src/relax/ir/*.cc
+ src/relax/op/*.cc
src/relax/analysis/*.cc
src/relax/backend/vm/*.cc
+ src/relax/utils.cc
)
tvm_file_glob(GLOB CODEGEN_SRCS
diff --git a/include/tvm/relax/analysis.h b/include/tvm/relax/analysis.h
index 82145032f4..ad2bd19aa4 100644
--- a/include/tvm/relax/analysis.h
+++ b/include/tvm/relax/analysis.h
@@ -85,6 +85,19 @@ TVM_DLL Type GetStaticType(const StructInfo& info);
*/
TVM_DLL StructInfo StructInfoFromType(const Type& type);
+/*!
+ * \return Derive the call's ret value struct info from inputs.
+ * \param finfo The function struct info.
+ * \param call The call expression to be derived.
+ * \param ctx The builder context.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ * \return The derived struct info of the call.
+ * \note call->op field is ignored during derivation and we only rely on information
+ * presented by func_sinfo.
+ */
+TVM_DLL StructInfo DeriveCallRetStructInfo(const FuncStructInfo& finfo, const Call& call,
+ const BlockBuilder& ctx, arith::Analyzer* ana = nullptr);
+
/*!
* \brief Erase the info to a corresponding more coarse grained
* struct info that is still well-defined(with all the vars in scope).
diff --git a/include/tvm/relax/block_builder.h b/include/tvm/relax/block_builder.h
new file mode 100644
index 0000000000..7222ae08f9
--- /dev/null
+++ b/include/tvm/relax/block_builder.h
@@ -0,0 +1,239 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/block_builder.h
+ * \brief The utility for constructing Relax binding blocks.
+ */
+#ifndef TVM_RELAX_BLOCK_BUILDER_H_
+#define TVM_RELAX_BLOCK_BUILDER_H_
+
+#include <tvm/arith/analyzer.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/utils.h>
+#include <tvm/runtime/object.h>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief A builder to build Relax binding blocks.
+ *
+ * BlockBuilder provides the following three categories
+ * of main functionalities for IR building and transformations:
+ *
+ * - Global context management: manages the IRModule,
+ * allowing query, update the surrounding global context.
+ * Provide context tools for analysis.
+ * - Scope management:
+ * - Manages block scopes for bulding nested blocks.
+ * - Emit bindings to the current scope.
+ * - Construct blocks by calling EndScope.
+ * - Normalization: Take an Expr, normalize it
+ * to deduce shape/type, turn things into normal forms.
+ *
+ * Importantly, these three categories of features can be dependent
+ * on each other. For example, when we emit into scope we will call
+ * normalize to ensure the code is in normal form. Similarly, when we
+ * normalize we could choose to emit into the current context.
+ *
+ * We would encourage the developers to keep these three category
+ * in mind when using and developing BlockBuilder, we can group
+ * the code in a logically clean way.
+ *
+ * BlockBuilderNode is implemented as a virtual interface to
+ * allow logically grouped implementation and internal data
+ * structures that are hidden from the users.
+ */
+class BlockBuilderNode : public Object {
+ public:
+ //-------------------------------
+ // Global Context management
+ //-------------------------------
+ /*!
+ * \brief Get the name table for generating unique names.
+ *
+ * \return The name table.
+ */
+ virtual NameTable* name_table() = 0;
+
+ /*!
+ * \brief Get the context IRModule in this builder.
+ *
+ * \note The context
+ * \return The IRModule in this BlockBuilder.
+ */
+ virtual IRModule GetContextIRModule() const = 0;
+
+ /*!
+ * \brief Add a Relax function or a TIR PrimFunc to internal context module.
+ * \param func The function to be added.
+ * \param func_name_hint The name hint of the function to be added.
+ * \note If the function to be added already exists, return its
+ * GlobalVar directly.
+ * \return The global var bound to the added function.
+ */
+ virtual GlobalVar AddFunction(const BaseFunc& func, String func_name_hint) = 0;
+
+ /*!
+ * \brief Update a Relax function or a TIR PrimFunc in the internal context module.
+ * \param gv The global var referring the function to be updated.
+ * \param function The updated function.
+ */
+ virtual void UpdateFunction(const GlobalVar& gv, BaseFunc function) = 0;
+
+ /*!
+ * \brief Report an error during transformation construction.
+ * \param diagnostic The diagnostic information.
+ */
+ virtual void ReportFatal(const Diagnostic& diagnostic) = 0;
+
+ //-------------------------------
+ // Scope management
+ //-------------------------------
+ /*!
+ * \brief Lookup the binding value that var binds to in the current emitted sequences.
+ * \param var The input var.
+ * \return The Expr bound to the input \p var.
+ * \note For function parameters, this function returns NullOpt.
+ */
+ virtual Optional<Expr> LookupBinding(const Var& var) = 0;
+
+ /*!
+ * \brief Begin a new scope, with optional parameters that
+ * are visible within the scope.
+ *
+ * \param params Parameters that are visible within the scope.
+ *
+ * \note This function should be called when new scope is introduced
+ * (function, seq) to properly track the variable availability
+ * and help the best effort deduction.
+ *
+ * \sa EndScope
+ */
+ virtual void BeginScope(Optional<Array<Var>> params) = 0;
+
+ /*! \brief End the previously defined scope. */
+ virtual void EndScope() = 0;
+
+ /*! \brief Begin to build a DataflowBlock. */
+ virtual void BeginDataflowBlock() = 0;
+
+ /*! \brief Begin to build a BindingBlock. */
+ virtual void BeginBindingBlock() = 0;
+ /*!
+ * \brief End building a BindingBlock.
+ * \return The BindingBlock being built.
+ */
+ virtual BindingBlock EndBlock() = 0;
+
+ /*!
+ * \brief Check if the block being built is DataflowBlock or not.
+ * \return A boolean that indicates if the block being built is DataflowBlock or not.
+ */
+ virtual bool CurrentBlockIsDataFlow() = 0;
+
+ /*!
+ * \brief Emits an Expr, and returns the variable it is bound to.
+ * \param expr The Expr to be emitted.
+ * \param name_hint Name hint for the bound variable.
+ * \return The new variable that \p expr is bound to.
+ *
+ * \note This Emit function normalizes the \p expr, and
+ * performs shape and type deductions by calling Normalize.
+ */
+ virtual Var Emit(Expr expr, String name_hint = "") = 0;
+
+ /*!
+ * \brief Emit a MatchCast.
+ * \param value The input value.
+ * \param struct_info The struct info to be matched.
+ * \param name_hint Name hint for the bound variable.
+ * \return The variable bound to the MatchCast.
+ */
+ virtual Var EmitMatchCast(Expr value, StructInfo struct_info, String name_hint = "") = 0;
+
+ /*!
+ * \brief Generate an output for the current dataflow block.
+ * \param output The output variable of the block.
+ * \param name_hint Name hint for the bound variable.
+ * \return The variable bound to \p output.
+ */
+ virtual Var EmitOutput(Expr output, String name_hint = "") = 0;
+
+ /*!
+ * \brief Emit a binding that is already normalized.
+ *
+ * \param normalized_binding A binding whose value is already normalized.
+ *
+ * \note This function requires binding to be pre-normalized.
+ */
+ virtual void EmitNormalized(Binding normalized_binding) = 0;
+
+ /*!
+ * \brief Convert an expression to normal form, and try to eagerly infer types and shapes.
+ * \param expr The input expression.
+ * \return The normalized expression.
+ *
+ * \note Invariant: If any of the sub expr have struct_info field.
+ * they must have already been normalized.
+ */
+ virtual Expr Normalize(const Expr& expr) = 0;
+
+ /*!
+ * \brief Normalize argument to a call or another IRNode.
+ * \param expr The input expression.
+ * \return The normalized expression.
+ *
+ * \note This function will create a binding var for non-leaf expressions such as Call.
+ */
+ virtual Expr NormalizeArgument(const Expr& expr) = 0;
+
+ /*!
+ * \brief Get the analyzer of the BlockBuilder.
+ * \return The BlockBuilder's arithmetic analyzer.
+ */
+ virtual arith::Analyzer* GetAnalyzer() = 0;
+
+ static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
+ static constexpr const char* _type_key = "relax.BlockBuilder";
+ TVM_DECLARE_BASE_OBJECT_INFO(BlockBuilderNode, Object);
+};
+
+class BlockBuilder : public ObjectRef {
+ public:
+ /*!
+ * \brief Create a BlockBuilder.
+ *
+ * \param ctx_mod Optional before-transformation context module for rewriting.
+ * \return The created BlockBuilder.
+ *
+ * \note When rewriting an existing IRModule, it is important to pass it in as
+ * ctx_mod so you can lookup the context functions for cross function
+ * call analysis.
+ */
+ TVM_DLL static BlockBuilder Create(Optional<IRModule> ctx_mod);
+
+ TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(BlockBuilder, ObjectRef, BlockBuilderNode);
+};
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_BLOCK_BUILDER_H_
diff --git a/include/tvm/relax/expr.h b/include/tvm/relax/expr.h
index 9e563c7061..0788193ee7 100644
--- a/include/tvm/relax/expr.h
+++ b/include/tvm/relax/expr.h
@@ -171,8 +171,7 @@ class CallNode : public ExprNode {
// skip sinfo_args check for primitive ops.
equal->MarkGraphNode();
return equal(op, other->op) && equal(args, other->args) && equal(attrs, other->attrs) &&
- (IsPrimitiveOp(op) || equal(sinfo_args, other->sinfo_args)) &&
- equal(struct_info_, other->struct_info_);
+ equal(sinfo_args, other->sinfo_args) && equal(struct_info_, other->struct_info_);
}
void SHashReduce(SHashReducer hash_reduce) const {
@@ -180,9 +179,7 @@ class CallNode : public ExprNode {
hash_reduce(op);
hash_reduce(args);
hash_reduce(attrs);
- if (!IsPrimitiveOp(op)) {
- hash_reduce(sinfo_args);
- }
+ hash_reduce(sinfo_args);
hash_reduce(struct_info_);
}
diff --git a/include/tvm/relax/expr_functor.h b/include/tvm/relax/expr_functor.h
index 5735e8661f..655ecc52b6 100644
--- a/include/tvm/relax/expr_functor.h
+++ b/include/tvm/relax/expr_functor.h
@@ -26,15 +26,18 @@
#define TVM_RELAX_EXPR_FUNCTOR_H_
#include <tvm/node/functor.h>
+#include <tvm/relax/block_builder.h>
#include <tvm/relax/expr.h>
#include <tvm/relax/struct_info.h>
#include <tvm/relax/struct_info_functor.h>
#include <tvm/relay/op.h>
#include <tvm/tir/function.h>
+#include <deque>
#include <string>
+#include <unordered_map>
#include <utility>
-
+#include <vector>
namespace tvm {
namespace relax {
@@ -410,6 +413,139 @@ class ExprMutatorBase : public ExprFunctor<Expr(const Expr&)> {
DefaultStructInfoFieldMutator default_struct_info_field_mutator_{this};
};
+/*!
+ * \brief A mutator works in normal form.
+ *
+ * ExprMutator expects input AST to be in the normal form, i.e., the expressions are normalized(no
+ * nesting and hence the AST is in ANF), and all checked_type_ and shape_ of expressions are
+ * available.
+ */
+class ExprMutator : public ExprMutatorBase {
+ public:
+ using ExprMutatorBase::VisitExpr_;
+
+ ExprMutator(Optional<IRModule> mod = NullOpt) { builder_ = BlockBuilder::Create(mod); }
+ Expr VisitExpr(const Expr& expr) override;
+ Expr VisitExpr_(const VarNode* op) override;
+ Expr VisitExpr_(const DataflowVarNode* op) override;
+ Expr VisitExpr_(const FunctionNode* op) override;
+ Expr VisitExpr_(const SeqExprNode* op) override;
+ Expr VisitExpr_(const IfNode* op) override;
+
+ /*!
+ * \brief Generic dispatcher for bindings.
+ * \param binding The binding to be visited.
+ */
+ virtual void VisitBinding(const Binding& binding);
+ // specific leaf level visitor functions
+ virtual void VisitBinding_(const VarBindingNode* binding);
+ virtual void VisitBinding_(const MatchCastNode* binding);
+ // second level dispatching based on binding value type.
+ // these dispatching functions get called from first-level dispatch on VarBinding
+ virtual void VisitBinding_(const VarBindingNode* binding, const ConstantNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const TupleNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const VarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const DataflowVarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const ShapeExprNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const ExternFuncNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const GlobalVarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const FunctionNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const CallNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const SeqExprNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const IfNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const OpNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const TupleGetItemNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const PrimValueNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const StringImmNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const DataTypeImmNode* val);
+ /*!
+ * \brief Generic dispatcher for binding blocks.
+ * \param block The binding block to be visited.
+ * \return The binding block after transformation.
+ */
+ virtual BindingBlock VisitBindingBlock(const BindingBlock& block) override; // NOLINT(*)
+ // specific leaf level visitor functions
+ virtual BindingBlock VisitBindingBlock_(const BindingBlockNode* block);
+ virtual BindingBlock VisitBindingBlock_(const DataflowBlockNode* block);
+
+ /*!
+ * \brief Generic dispatcher for rewriting the var definition site.
+ * \param var The var to be visited.
+ * \return The var after post-order rewritten.
+ * \note VisitExpr_(const VarNode*) will only visit the usage site of an Var
+ */
+ virtual Var VisitVarDef(const Var& var);
+ // specific leaf level visitor functions
+ virtual Var VisitVarDef_(const VarNode* var);
+ virtual Var VisitVarDef_(const DataflowVarNode* var);
+
+ protected:
+ /*!
+ * \brief Try to remit binding and bind it to a new_value
+ *
+ * This function is called after VisitExpr(binding->value) in
+ * VisitBinding_(const VarBinding*).
+ * It will try to reuse the current binding when the new value's shape/type
+ * matches the original binding and no changes in var is needed.
+ *
+ * Otherwise, a new binding will be emitted to replace the var specified in
+ * the current binding.
+ */
+ void ReEmitBinding(const VarBindingNode* binding, Expr new_value);
+
+ /*!
+ * \brief Rewrite the expr with a new scope, used in a Function's body and the branches of If.
+ *
+ * \param body_expr The body to be visited.
+ * \param params Optional parameters that are visible within the scope.
+ * \return The expr after visiting.
+ *
+ * \note The body_expr must be an SeqExpr in the normal form.
+ */
+ Expr VisitWithNewScope(const Expr& body_expr, Optional<Array<Var>> params = NullOpt);
+
+ /*!
+ * \brief Look up the value bound to a variable.
+ * \param var The var to be looked up.
+ * \return The value bound to the input \p var.
+ * \note For function parameters, this function returns NullOpt.
+ */
+ Optional<Expr> LookupBinding(const Var& var);
+
+ /*!
+ * \brief Post-order rewrite a node and normalize.
+ * \tparam T The node type to be rewritten.
+ * \param op The node to be rewritten.
+ * \return The node after post rewritten.
+ */
+ template <typename T>
+ Expr VisitExprPostOrder_(const T* op) {
+ return builder_->Normalize(ExprMutator::VisitExpr_(op));
+ }
+
+ /*!
+ * \brief Create a new var with specified struct_info if the original var's shape or type does
+ * not match with the specified ones.
+ * \param var The var to be updated.
+ * \param struct_info The struct info to be updated.
+ * \return The var filled with struct_info
+ */
+ Var WithStructInfo(Var var, StructInfo struct_info);
+
+ /*! \brief Internal block builder to emit bindings during rewriting. */
+ BlockBuilder builder_;
+
+ /*! \brief Remap a var to a new var in use-site. */
+ std::unordered_map<Id, Var, ObjectPtrHash, ObjectPtrEqual> var_remap_;
+
+ private:
+ using TSelf = ExprMutator;
+ using VisitBindingVTable =
+ tvm::NodeFunctor<void(const ObjectRef& n, ExprMutator* self, const VarBindingNode* binding)>;
+ // initialize the vtable.
+ static VisitBindingVTable InitVisitBindingVTable();
+};
+
} // namespace relax
} // namespace tvm
#endif // TVM_RELAX_EXPR_FUNCTOR_H_
diff --git a/include/tvm/relax/op_attr_types.h b/include/tvm/relax/op_attr_types.h
new file mode 100644
index 0000000000..e171a8d47b
--- /dev/null
+++ b/include/tvm/relax/op_attr_types.h
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/op_attr_types.h
+ * \brief Data structures that can appear in operator attributes.
+ */
+#ifndef TVM_RELAX_OP_ATTR_TYPES_H_
+#define TVM_RELAX_OP_ATTR_TYPES_H_
+
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/te/schedule.h>
+#include <tvm/te/tensor.h>
+
+#include <string>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief Infer output struct info given the call
+ *
+ * \param call The call expression to be derived.
+ * \param ctx The builder context.
+ */
+using FInferStructInfo =
+ runtime::TypedPackedFunc<StructInfo(const Call& call, const BlockBuilder& ctx)>;
+
+/*!
+ * \brief Packed function implementation for operators. The relax operator will be lowered to
+ * this packed function call during codegen.
+ */
+using FCallPacked = String;
+
+struct PrintAttrs : public tvm::AttrsNode<PrintAttrs> {
+ std::string format;
+ TVM_DECLARE_ATTRS(PrintAttrs, "relax.attrs.PrintAttrs") {
+ TVM_ATTR_FIELD(format)
+ .describe("Python-style format string to use for displaying the input. Ignored if empty.")
+ .set_default("");
+ }
+};
+
+struct AssertOpAttrs : public tvm::AttrsNode<AssertOpAttrs> {
+ std::string format;
+ TVM_DECLARE_ATTRS(AssertOpAttrs, "relax.attrs.AssertOpAttrs") {
+ TVM_ATTR_FIELD(format)
+ .describe(
+ "Python-style format string to use for displaying "
+ "an error message if the assert fails. "
+ "Ignored if empty.")
+ .set_default("");
+ }
+};
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_OP_ATTR_TYPES_H_
diff --git a/include/tvm/relax/struct_info.h b/include/tvm/relax/struct_info.h
index f38a32f6bb..b9aebc5494 100644
--- a/include/tvm/relax/struct_info.h
+++ b/include/tvm/relax/struct_info.h
@@ -22,16 +22,13 @@
#include <tvm/ir/env_func.h>
#include <tvm/ir/source_map.h>
#include <tvm/node/node.h>
+#include <tvm/relax/block_builder.h>
#include <tvm/relax/expr.h>
#include <tvm/relax/type.h>
namespace tvm {
namespace relax {
-// TODO(relax-team) replace with real BlockBuilder
-// once it is ready.
-using BlockBuilder = ObjectRef;
-
/*!
* \brief Opaque object.
*/
diff --git a/include/tvm/relax/utils.h b/include/tvm/relax/utils.h
new file mode 100644
index 0000000000..1457a16427
--- /dev/null
+++ b/include/tvm/relax/utils.h
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/utils.h
+ * \brief Utility classes and functions for working with the Relax IR.
+ */
+#ifndef TVM_RELAX_UTILS_H_
+#define TVM_RELAX_UTILS_H_
+
+#include <tvm/ir/module.h>
+#include <tvm/runtime/logging.h>
+
+#include <algorithm>
+#include <cctype>
+#include <string>
+#include <type_traits>
+#include <unordered_map>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief Utility data structure for generating unique names for IR construction.
+ */
+class NameTable {
+ public:
+ /*!
+ * \brief Generate a unique name with a specified prefix.
+ * \param prefix The name prefix.
+ * \return The generated name.
+ */
+ inline std::string GetUniqueName(std::string prefix) {
+ std::replace(prefix.begin(), prefix.end(), '.', '_');
+ std::string unique_prefix = prefix;
+ auto it = alloc_map_.find(prefix);
+ if (it != alloc_map_.end()) {
+ while (alloc_map_.count(unique_prefix = prefix + std::to_string(++it->second)) > 0) {
+ }
+ }
+ alloc_map_[unique_prefix] = 0;
+ return unique_prefix;
+ }
+
+ NameTable() = default;
+
+ template <typename Iter, typename Lambda>
+ explicit NameTable(Iter begin, Iter end, Lambda f) {
+ // static_assert is more reader-friendly than SFINAE when template specialization is not needed.
+ static_assert(std::is_convertible<decltype(f(*begin)), std::string>::value,
+ "Lambda f must has a signature of [?](*it) -> string {}");
+ for (auto it = begin; it != end; ++it) {
+ const std::string& name = f(*it);
+ const size_t idx_last_first_num = std::distance(
+ std::find_if(name.rbegin(), name.rend(), [](char c) { return !std::isdigit(c); }),
+ name.rend());
+ // name = {O = others}{D = consecutive digits}
+ // let O -> prefix;
+ std::string prefix = name.substr(0, idx_last_first_num);
+ ICHECK(prefix.size() > 0 && std::isalpha(prefix[0])) << "Invalid variable name: " << name;
+ if (0 == alloc_map_.count(prefix)) alloc_map_[prefix] = 0;
+ if (idx_last_first_num < name.size()) { // has some digits.
+ // let D's nearest natural number -> idx;
+ // note: stoul("000123") = 123;
+ alloc_map_[prefix] =
+ std::max(alloc_map_[prefix], std::stoi(name.substr(idx_last_first_num)));
+ }
+ }
+ }
+
+ template <typename Iter>
+ explicit NameTable(Iter begin, Iter end)
+ : NameTable(begin, end, [](const decltype(*begin)& v) { return v; }) {}
+
+ private:
+ std::unordered_map<std::string, int> alloc_map_;
+};
+
+/*!
+ * \brief Bind the variables to a Relax expression. This is a helper
+ * function usually called by other pass functions to help optimizations.
+ * If any free variables are introduced into a function, those are added
+ * to the function parameters.
+ * Additionally this may change the order of parameters if you map a variable
+ * to a variable.
+ *
+ * \param expr The input expression.
+ * \param binds The variable to expression map that will be used to help the
+ * binding.
+ *
+ * \return The updated expression.
+ */
+TVM_DLL Expr Bind(const Expr& expr, const tvm::Map<Var, Expr>& binds);
+
+/*!
+ * \brief Check if the given type is a boolean scalar type (tensor of rank 0 with a boolean dtype).
+ *
+ * \param ty The input type.
+ * \param permit_unknown_rank If true, it will permit the input type to have unknown rank
+ * (ndim of -1), which will require a dynamic check.
+ * \param permit_unknown_dtype If true, it will permit the input type to have an unknown dtype
+ * (namely, void), which will require a dynamic check.
+ *
+ * \return True iff the input type is a boolean scalar type (or, depending on options, has unknown
+ * rank or dtype)
+ */
+TVM_DLL bool IsBoolScalarType(const Type& ty, bool permit_unknown_rank = true,
+ bool permit_unknown_dtype = true);
+
+/*!
+ * \brief Check if the given expression is a "leaf" node or tuple node for normalization purposes.
+ *
+ * The following expressions are defined as leaf nodes: Var, Constant, ShapeExpr,
+ * GlobalVar, Op, ExternFunc.
+ *
+ * Tuples are included in this list mainly for convenience in grouping operator arguments.
+ * *Note*: Since tuples can contain nested expressions, it is necessary to ensure that
+ * values nested inside them are also leaves.
+ *
+ * \param expr The input expression
+ *
+ * \return True iff the input expression is a "leaf" node (a value allowed to appear
+ * inline without being bound to a var during normalization).
+ */
+TVM_DLL bool IsLeafOrTuple(const Expr& expr);
+
+/*!
+ * \brief Copy the given function. The parameters of the original function would be copied to
+ * satisfy the restriction in the well-formed check: any two functions cannot share the same
+ * parameter variable.
+ * \param func The relax function to copy.
+ * \return The copied function.
+ */
+TVM_DLL Function CopyWithNewParams(Function func);
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_UTILS_H_
diff --git a/include/tvm/te/operation.h b/include/tvm/te/operation.h
index 2c50f3c315..f5753afa56 100644
--- a/include/tvm/te/operation.h
+++ b/include/tvm/te/operation.h
@@ -182,7 +182,7 @@ class PlaceholderOpNode : public OperationNode {
}
static constexpr const char* _type_key = "PlaceholderOp";
- TVM_DECLARE_FINAL_OBJECT_INFO(PlaceholderOpNode, OperationNode);
+ TVM_DECLARE_BASE_OBJECT_INFO(PlaceholderOpNode, OperationNode);
};
/*!
diff --git a/python/tvm/ir/function.py b/python/tvm/ir/function.py
index c3f1bf5f56..d02698edb5 100644
--- a/python/tvm/ir/function.py
+++ b/python/tvm/ir/function.py
@@ -66,3 +66,19 @@ class BaseFunc(RelayExpr):
return _ffi_api.BaseFuncWithAttr(
res._move(), attr_key_or_dict, tvm.runtime.convert(attr_value)
)
+
+ def without_attr(self, attr_key: str) -> "BaseFunc":
+ """Create a new copy of the function with an attribute without provided key.
+
+ Parameters
+ ----------
+ attr_key : str
+ The attribute key to delete from the attrubte pairs.
+
+
+ Returns
+ -------
+ func : BaseFunc
+ A new copy of the function
+ """
+ return _ffi_api.BaseFuncWithoutAttr(self, attr_key)
diff --git a/python/tvm/meta_schedule/utils.py b/python/tvm/meta_schedule/utils.py
index 401fdab08a..1f2cfd3401 100644
--- a/python/tvm/meta_schedule/utils.py
+++ b/python/tvm/meta_schedule/utils.py
@@ -75,14 +75,27 @@ def derived_object(cls: type) -> type:
def method(*args, **kwargs):
return getattr(inst, name)(*args, **kwargs)
- if getattr(base, name) is getattr(cls, name) and name != "__str__":
- # for task scheduler return None means calling default function
- # otherwise it will trigger a TVMError of method not implemented
- # on the c++ side when you call the method, __str__ not required
- return None
- return method
+ for inherit_cls, base_cls in zip(cls.__mro__, cls.__mro__[1:]):
+ # extract functions that differ from the base class
+ if not hasattr(base_cls, name):
+ continue
+ if getattr(base_cls, name) is getattr(inherit_cls, name) and name != "__str__":
+ continue
+ return method
+
+ # for task scheduler return None means calling default function
+ # otherwise it will trigger a TVMError of method not implemented
+ # on the c++ side when you call the method, __str__ not required
+ return None
assert isinstance(cls.__base__, type)
+ if hasattr(cls, "_type") and cls._type == "TVMDerivedObject": # type: ignore
+ raise TypeError(
+ (
+ f"Inheritance from a decorated object `{cls.__name__}` is not allowed. "
+ f"Please inherit from `{cls.__name__}._cls`."
+ )
+ )
assert hasattr(
cls, "_tvm_metadata"
), "Please use the user-facing method overriding class, i.e., PyRunner."
@@ -95,6 +108,9 @@ def derived_object(cls: type) -> type:
class TVMDerivedObject(metadata["cls"]): # type: ignore
"""The derived object to avoid cyclic dependency."""
+ _cls = cls
+ _type = "TVMDerivedObject"
+
def __init__(self, *args, **kwargs):
"""Constructor."""
self.handle = None
@@ -111,12 +127,22 @@ def derived_object(cls: type) -> type:
# using weakref to avoid cyclic dependency
self._inst._outer = weakref.ref(self)
- def __getattr__(self, name: str):
- """Bridge the attribute function."""
- try:
- return self._inst.__getattribute__(name)
- except AttributeError:
- return super(TVMDerivedObject, self).__getattr__(name)
+ def __getattr__(self, name):
+ # fall back to instance attribute if there is not any
+ # return self._inst.__getattribute__(name)
+ import inspect # pylint: disable=import-outside-toplevel
+
+ result = self._inst.__getattribute__(name)
+ if inspect.ismethod(result):
+
+ def method(*args, **kwargs):
+ return result(*args, **kwargs)
+
+ # set __own__ to aviod implicit deconstruction
+ setattr(method, "__own__", self)
+ return method
+
+ return result
def __setattr__(self, name, value):
if name not in ["_inst", "key", "handle"]:
diff --git a/python/tvm/relax/__init__.py b/python/tvm/relax/__init__.py
index 01310f6455..ce175354d0 100644
--- a/python/tvm/relax/__init__.py
+++ b/python/tvm/relax/__init__.py
@@ -21,6 +21,8 @@ from . import expr
from . import ty
from . import analysis
from . import vm
+from . import block_builder
+from . import op
from . import struct_info
# Expr
@@ -60,6 +62,15 @@ from .ty import Type, ObjectType, ShapeType, DynTensorType, TupleType, FuncType,
from .exec_builder import ExecBuilder
from .vm import VirtualMachine
+# Operator
+from .op.base import call_tir
+
+# BlockBuilder
+from .block_builder import BlockBuilder
+
+# ExprFunctor
+from .expr_functor import ExprFunctor, PyExprVisitor, PyExprMutator
+
# StructInfo
from .struct_info import (
StructInfo,
diff --git a/python/tvm/relax/analysis/analysis.py b/python/tvm/relax/analysis/analysis.py
index 301f3ecc72..d81c477145 100644
--- a/python/tvm/relax/analysis/analysis.py
+++ b/python/tvm/relax/analysis/analysis.py
@@ -26,8 +26,8 @@ from enum import IntEnum
from tvm import tir
from tvm.relax.ty import Type
-from tvm.relax.struct_info import StructInfo
-from tvm.relax.expr import Var, Expr
+from tvm.relax.struct_info import StructInfo, FuncStructInfo
+from tvm.relax.expr import Var, Expr, Call
from . import _ffi_api
@@ -116,6 +116,35 @@ def struct_info_base_check(base: StructInfo, derived: StructInfo) -> BaseCheckRe
return _ffi_api.StructInfoBaseCheck(base, derived) # type: ignore
+def derive_call_ret_struct_info(
+ func_sinfo: FuncStructInfo, call: Call, ctx: "tvm.relax.BlockBuilder"
+) -> StructInfo:
+ """Derive the call's ret value struct info from inputs.
+
+ Parameters
+ ----------
+ func_sinfo: FuncStructInfo
+ The call's function signature.
+
+ call: Call
+ The call expression
+
+ ctx: tvm.relax.BlockBuilder
+ The context block builder.
+
+ Returns
+ -------
+ ret : StructInfo
+ The derived return value struct info.
+
+ Note
+ ----
+ This is an internal derivation function, call.op field is
+ ignored in this case and the derivation only depends on func_sinfo.
+ """
+ return _ffi_api.DeriveCallRetStructInfo(func_sinfo, call, ctx) # type: ignore
+
+
def struct_info_lca(lhs: StructInfo, rhs: StructInfo) -> StructInfo:
"""Unify the two struct info to their least common ancestor.
diff --git a/python/tvm/relax/block_builder.py b/python/tvm/relax/block_builder.py
new file mode 100644
index 0000000000..77b45fdf55
--- /dev/null
+++ b/python/tvm/relax/block_builder.py
@@ -0,0 +1,801 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=no-else-return, invalid-name
+"""Developer API of constructing Relax AST."""
+import typing
+
+from typing import Dict, List, Optional, Union, Any, Callable
+from tvm.ir.module import IRModule
+from tvm.runtime import Object
+from tvm import relax as rx, tir
+import tvm
+from .expr import (
+ Expr,
+ te_tensor,
+ Var,
+ ShapeExpr,
+ GlobalVar,
+ BindingBlock,
+ Tuple,
+ BaseFunc,
+ Binding,
+)
+from .struct_info import ShapeStructInfo, StructInfo, TensorStructInfo
+from .op.base import call_tir
+from . import _ffi_api
+
+
+class FunctionScope(object):
+ """Auxiliary scope for function"""
+
+ def __init__(self, block_builder, name, params, attrs):
+ self._bb = block_builder
+ self._name = name
+ self._params = params
+ self._attrs = attrs
+
+ def __enter__(self):
+ self._bb._enter_function_scope(self._name, self._params, self._attrs)
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ # __exit__ should properly handle the case where the with block exits with an exception
+ # when handling error case in exit, always check if there is already an exception
+ # been thrown in the with block
+ self._bb._exit_function_scope(exc_type, exc_val, exc_tb)
+
+
+class DataflowScope(object):
+ """Auxiliary scope for Dataflow block"""
+
+ def __init__(self, block_builder):
+ self._bb = block_builder
+
+ def __enter__(self):
+ block = self._bb._end_block()
+ if len(block.bindings) > 0:
+ self._bb._blocks.append(block)
+ self._bb._begin_dataflow_block()
+
+ def __exit__(self, ptype, value, trace):
+ block = self._bb._end_block()
+ if len(block.bindings) > 0:
+ self._bb._blocks.append(block)
+ self._bb._begin_binding_block()
+
+
+class TestingScope(object):
+ """Auxiliary scope for testing purposes"""
+
+ def __init__(self, block_builder, def_vars):
+ self._bb = block_builder
+ shape_vars = []
+ for var in def_vars:
+ if isinstance(var, tvm.tir.Var):
+ shape_vars.append(var)
+ else:
+ raise ValueError("def_vars only can take tir.Var")
+ # setup a dummy var so shape is in scope.
+ sparam = rx.Var("sparam", rx.ShapeStructInfo(shape_vars))
+ self._scope_params = [sparam]
+
+ def __enter__(self):
+ self._bb.begin_scope(self._scope_params)
+ self._bb._begin_dataflow_block()
+
+ def __exit__(self, ptype, value, trace):
+ self._bb._end_block()
+ self._bb.end_scope()
+
+
+@tvm._ffi.register_object("relax.BlockBuilder")
+class BlockBuilder(Object):
+ """A builder to build Relax IR for testing and dev.
+
+ Examples
+ --------
+ .. code-block:: python
+
+ m = tir.Var("m", "int32")
+ n = tir.Var("n", "int32")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16")
+ bb = rx.BlockBuilder()
+ with bb.function([x, y], "func"):
+ with bb.dataflow() as df:
+ lv0 = bb.emit(rx.add(x, y))
+ lv1 = bb.emit(rx.multiply(lv0, y))
+ gv0 = bb.emit_output(lv1)
+ bb.emit_func_output(gv0)
+ mod = bb.get()
+
+ BlockBuilder can also be used to construct neural networks with nn.Module API
+
+ .. code-block:: python
+
+ from tvm.relax.testing import nn
+
+ n = tir.Var("n", "int64")
+ input_size = 784
+ hidden_sizes = [128, 32]
+ output_size = 10
+ bb = rx.BlockBuilder()
+
+ with bb.function("main"):
+ model = nn.Sequential(
+ nn.Linear(input_size, hidden_sizes[0]),
+ nn.ReLU(),
+ nn.Linear(hidden_sizes[0], hidden_sizes[1]),
+ nn.ReLU(),
+ nn.Linear(hidden_sizes[1], output_size),
+ nn.LogSoftmax(),
+ )
+ data = nn.Placeholder((n, input_size), name="data")
+ output = model(data)
+ params = [data] + model.parameters()
+ builder.emit_func_output(output, params=params)
+ mod = bb.get()
+ """
+
+ _current = None
+
+ @staticmethod
+ def current():
+ """Returns the current BlockBuilder."""
+ return BlockBuilder._current
+
+ def __init__(self, mod: IRModule = None):
+ self._blocks: List[BindingBlock] = []
+ # a boolean flag that tracks if emit_func_output has been called
+ self._is_emit_func_output_called = False
+ self.__init_handle_by_constructor__(_ffi_api.BlockBuilderCreate, mod) # type: ignore
+
+ def _begin_dataflow_block(self) -> None:
+ _ffi_api.BlockBuilderBeginDataflowBlock(self) # type: ignore
+
+ def _begin_binding_block(self) -> None:
+ _ffi_api.BlockBuilderBeginBindingBlock(self) # type: ignore
+
+ def _end_block(self) -> BindingBlock:
+ return _ffi_api.BlockBuilderEndBlock(self) # type: ignore
+
+ def _enter_function_scope(self, name, params, attrs):
+ if BlockBuilder.current() is not None:
+ raise RuntimeError("BlockBuilder does not allow nested functions.")
+ BlockBuilder._current = self
+ self._func_name = name
+ self._func_params = params
+ self._func_attrs = attrs
+ self.begin_scope(params)
+ self._begin_binding_block()
+
+ def _exit_function_scope(self, exc_type, exc_val, exc_tb):
+ # record
+ is_emit_func_output_called = self._is_emit_func_output_called
+ # recover to default state
+ self._blocks = []
+ self._is_emit_func_output_called = False
+ BlockBuilder._current = None
+
+ # NOTE: we must raise after we recover the state so future
+ # block builder scoping functions correctly
+ if exc_type is None:
+ if not is_emit_func_output_called:
+ raise RuntimeError("emit_func_output must be called in a relax function.")
+
+ def _convert_te_arg(
+ self, te_args: Any, tir_var_map: Dict[tir.Var, tir.PrimExpr]
+ ) -> typing.Tuple[Any, List[tvm.te.Tensor]]:
+ """Helper function used by `call_te` to convert Relax expressions to TE tensor.
+
+ In the common case, the type of te_args is a Relax expression and is converted
+ into a TE tensor.
+ If te_args is a nested or recursive datatype (i.e list, dict, tvm.ir.Map, tvm.ir.Array),
+ we recursive and convert any value of type Relax expression into a TE tensor.
+ Common values of type int, float, and str are preserved.
+
+ In dynamic shape cases, the passed in arguments may contain TIR variable.
+ For example, the argument can be a Relax Var with TensorStructInfo, which
+ has symbolic shape, or the argument can be a ShapeExpr with symbolic variables.
+ To make the PrimFunc generated by `call_te` has independent variables with
+ the caller Relax function, we will substitute the TIR variables in the input
+ arguments with fresh ones, which is done by maintaining a TIR variable mapping.
+
+ Parameters
+ ----------
+ te_args : Any
+ Argument to convert to TE
+
+ tir_var_map : Dict[tir.Var, tir.PrimExpr]
+ The TIR variable mapping, which maps TIR variables on the Relax function
+ side to the new set of variables used on the PrimFunc side.
+
+ Returns
+ -------
+ ret : (Any, [tvm.te.Tensor])
+ A tuple of the converted te_args, and a list of te tensors for each converted
+ Relax expression
+ """
+ te_args_list = []
+
+ def _copy_undefined_var(expr: tir.PrimExpr):
+ def _visit_expr(e: tir.PrimExpr):
+ if isinstance(e, tir.Var) and e not in tir_var_map:
+ new_var = tir.Var(e.name, e.dtype)
+ tir_var_map[e] = new_var
+
+ tir.stmt_functor.post_order_visit(expr, _visit_expr)
+
+ def _convert_te_arg_helper(arg):
+ if isinstance(arg, Expr): # type: ignore
+ if isinstance(arg.struct_info, TensorStructInfo):
+ assert isinstance(
+ arg.struct_info.shape, ShapeExpr
+ ), "emit_te now only supports Tensor that has ShapeExpr shape"
+ for shape_value in arg.struct_info.shape.values:
+ _copy_undefined_var(shape_value)
+
+ arg = te_tensor(arg, tir_var_map)
+ te_args_list.append(arg)
+ return arg
+ elif isinstance(arg.struct_info, ShapeStructInfo):
+ assert isinstance(
+ arg, ShapeExpr
+ ), "For Expr having ShapeStructInfo, emit_te now only supports ShapeExpr"
+ return [_convert_te_arg_helper(val) for val in arg.values]
+ elif isinstance(arg, (list, tvm.ir.Array)):
+ return [_convert_te_arg_helper(x) for x in arg]
+ elif isinstance(arg, tuple):
+ return tuple([_convert_te_arg_helper(x) for x in arg])
+ elif isinstance(arg, (dict, tvm.ir.Map)):
+ for key in arg:
+ assert isinstance(
+ key, str
+ ), "emit_te only supports dict with string as the key currently"
+ return {k: _convert_te_arg_helper(arg[k]) for k in arg}
+ elif isinstance(arg, tir.PrimExpr):
+ _copy_undefined_var(arg)
+ return tir.stmt_functor.substitute(arg, tir_var_map)
+ elif isinstance(arg, (int, float, str, tvm.ir.Type, tvm.ir.Attrs)) or arg is None:
+ return arg
+ raise TypeError("not supported type in emit_te: {}".format(type(arg)))
+
+ new_arg = _convert_te_arg_helper(te_args)
+ return new_arg, te_args_list
+
+ def _get_unbound_tir_vars(self, args: List[tvm.te.Tensor]) -> List[tvm.tir.Var]:
+ """get unbound TIR vars (i.e TIR vars used in the shape but is not
+ itself a dimension of a shape)"""
+ bound_vars = set()
+ used_vars = set()
+
+ def _populate_used_vars(expr):
+ if isinstance(expr, tvm.tir.Var):
+ used_vars.add(expr)
+
+ for x in args:
+ for s in x.shape:
+ tvm.tir.stmt_functor.post_order_visit(s, _populate_used_vars)
+ if isinstance(s, tir.Var):
+ bound_vars.add(s)
+
+ diff = used_vars - bound_vars
+ return list(diff)
+
+ def function(
+ self,
+ name: str,
+ params: Optional[Union[Var, Tuple, List[Var]]] = None,
+ attrs: Optional[Dict[str, Object]] = None,
+ ) -> FunctionScope:
+ """Annotate a Relax function.
+
+ Parameters
+ ----------
+ name : str, optional
+ The name of the function
+
+ params : tvm.relax.Var | Tuple | List[tvm.relax.Var], optional
+ The parameters of the function.
+ If params is None, it means deferring initialization of function parameters
+ until emit_func_output.
+
+ attrs : Dict[str, Object], optional
+ The function attrs
+
+ Returns
+ -------
+ ret: FunctionScope
+ A FunctionScope for building a Relax function node.
+ """
+ if not params:
+ params = None
+ elif isinstance(params, rx.Var):
+ params = [params]
+ elif isinstance(params, (list, tuple)):
+ for param in params:
+ if not isinstance(param, rx.Var):
+ raise TypeError(
+ "each element of function parameters must be of type tvm.relax.Var,\
+ but got: {}".format(
+ type(param)
+ )
+ )
+ if attrs is None:
+ attrs = {}
+ return FunctionScope(self, name, params, attrs)
+
+ def testing_scope(self, def_vars: List[tir.Var]) -> TestingScope:
+ """Start a scope for unit-testing purposes.
+
+ Parameters
+ ----------
+ def_vars: List[tir.Var]
+ List of symbolic variables that are marked as defined in scope.
+
+ Returns
+ -------
+ ret: TestingScope
+ A TestingScope to setup builder for emit and other purposes.
+ """
+ return TestingScope(self, def_vars)
+
+ def dataflow(self) -> DataflowScope:
+ """Annotate a Relax dataflow block.
+
+ Returns
+ -------
+ ret: DataflowScope
+ A DataflowScope for building a Relax dataflow block.
+ """
+ return DataflowScope(self)
+
+ def emit(self, expr: Expr) -> Var:
+ """Emit an expr.
+ This infers the shape and type of the expr, create a variable,
+ and bind the expr to the variable.
+
+ Parameters
+ ----------
+ expr : tvm.relax.Expr
+ The Expr to be emitted.
+
+ Returns
+ -------
+ ret : tvm.relax.Var
+ A newly created variable that gets bound to the input expr.
+ """
+ return _ffi_api.BlockBuilderEmit(self, expr) # type: ignore
+
+ def call_te(self, func: Callable, *args: Any, **kwargs: Any) -> Expr:
+ """Generate a call node according to the te function.
+ This function converts arguments from relax expression to te tensor,
+ The callback func should return a te tensor or a list of te tensors.
+ Please see detailed example in emit_te
+
+ Parameters
+ ----------
+ func : Callable
+ A function that returns a te tensor or a list of te tensors.
+
+ args : Any, optional
+ arguments passed to the function.
+
+ kwargs : Any, optional
+ The keyword arguments passed to the function.
+ Note that the key "primfunc_name_hint" is reserved for passing name hint
+ to the PrimFunc that gets generated.
+
+ Returns
+ -------
+ ret : tvm.relax.Call
+ A newly created call node
+ """
+
+ primfunc_name_hint = kwargs.pop("primfunc_name_hint", None)
+ tir_var_map: Dict[tir.Var, tir.PrimExpr] = dict()
+ new_args, te_arg_list = self._convert_te_arg(args, tir_var_map)
+ new_kwargs, te_kwarg_list = self._convert_te_arg(kwargs, tir_var_map)
+
+ te_args = te_arg_list + te_kwarg_list
+
+ te_out = func(*new_args, **new_kwargs)
+ assert isinstance(te_out, tvm.te.tensor.Tensor) or (
+ isinstance(te_out, (tuple, list, tvm.ir.Array))
+ and all(isinstance(t, tvm.te.tensor.Tensor) for t in te_out)
+ ), "only support te.tensor or tuple/list/Array of te.tensor as function output"
+
+ outs = [te_out] if isinstance(te_out, tvm.te.tensor.Tensor) else list(te_out)
+ unbound_tir_vars = self._get_unbound_tir_vars(te_args + outs)
+
+ inputs = [*te_args] + outs
+ tir_func = tvm.te.create_relax_prim_func(inputs, unbound_tir_vars, "int64")
+
+ tir_func = tir_func.without_attr("global_symbol")
+
+ if primfunc_name_hint:
+ gvar = self.add_func(tir_func, primfunc_name_hint)
+ else:
+ gvar = self.add_func(tir_func, func.__name__)
+
+ call_args = [x.op.value for x in te_args]
+
+ def _shape_with_old_tir_var(
+ shape_values: List[tir.PrimExpr], tir_var_inverse_map: Dict[tir.Var, tir.PrimExpr]
+ ):
+ return ShapeExpr(
+ [tir.stmt_functor.substitute(value, tir_var_inverse_map) for value in shape_values]
+ )
+
+ # Invert the TIR variable mapping, to convert the output shape back
+ # with old set of variables.
+ tir_var_inverse_map = {v: k for k, v in tir_var_map.items()}
+
+ output_sinfo = [
+ TensorStructInfo(_shape_with_old_tir_var(out.shape, tir_var_inverse_map), out.dtype)
+ for out in outs
+ ]
+
+ # add arguments for extra parameters from unbound var
+ if len(unbound_tir_vars) > 0:
+ call = call_tir(
+ gvar,
+ call_args,
+ output_sinfo,
+ tir_vars=_shape_with_old_tir_var(unbound_tir_vars, tir_var_inverse_map),
+ )
+ else:
+ call = call_tir(gvar, call_args, output_sinfo)
+ return call
+
+ def emit_te(self, func: Callable, *args: Any, **kwargs: Any) -> Var:
+ """Emit a call node according to the te function.
+ This function converts arguments from relax expression to te tensor,
+ The callback func should return a te tensor or a list of te tensors.
+
+ Parameters
+ ----------
+ func : Callable
+ A function that returns a te tensor or a list of te tensors.
+
+ args : Any, optional
+ arguments passed to the function.
+
+ kwargs : Any, optional
+ The keyword arguments passed to the function.
+ Note that the key "primfunc_name_hint" is reserved for passing name hint
+ to the PrimFunc that gets generated.
+
+ Returns
+ -------
+ ret : tvm.relax.Var
+ A newly created variable that gets bound to the call code.
+
+ Example
+ -------
+
+ .. code-block:: python
+
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([n, m], "float32"))
+
+ def te_func(args, args_dict, msg):
+ A = args[0]
+ B = args_dict["B"]
+ return te.compute((128, 128), lambda i, j: A[i, j] + B[i, j])
+
+ with bb.function([x, y], "rx_func"):
+ out = bb.emit_te(te_func, [x], {"B": y}, msg="hello")
+ bb.emit_func_output(out)
+
+ will result in TVMScript
+
+ .. code-block:: python
+
+ @tvm.script.ir_module
+ class Module:
+ @T.prim_func
+ def te_func(var_rxplaceholder: T.handle, var_rxplaceholder_1: T.handle,
+ var_compute: T.handle) -> None:
+ # function attr dict
+ T.func_attr({"tir.noalias": True})
+ m = T.var("int64")
+ n = T.var("int64")
+ rxplaceholder = T.match_buffer(var_rxplaceholder, [n, m], dtype="float32")
+ rxplaceholder_1 = T.match_buffer(var_rxplaceholder_1, [n, m], dtype="float32")
+ compute = T.match_buffer(var_compute, [128, 128], dtype="float32")
+ # body
+ # with T.block("root")
+ for i0, i1 in T.grid(128, 128):
+ with T.block("compute"):
+ i, j = T.axis.remap("SS", [i0, i1])
+ T.reads([rxplaceholder[i, j], rxplaceholder_1[i, j]])
+ T.writes([compute[i, j]])
+ compute[i, j] = rxplaceholder[i, j] + rxplaceholder_1[i, j]
+
+ @R.function
+ def rx_func(x: Tensor((n, m), "float32"), y: Tensor((n, m), "float32")) -> Tensor:
+ # block 0
+ gv = relax.call_tir("te_func", (x, y), R.Tensor((128, 128), "float32"))
+ return gv
+
+ Example
+ -------
+
+ .. code-block:: python
+
+ bb = relax.BlockBuilder()
+ n = tir.Var("n", "int64")
+ x = relax.Var("x", relax.TensorStructInfo([n], "float32"))
+ y = relax.Var("y", relax.TensorStructInfo([n + 1], "float32"))
+
+ def te_func(A):
+ C = te.compute((n + 1), lambda i: A[i])
+ return C
+
+ with bb.function("rx_func", [x, y]):
+ x1 = bb.emit_te(te_func, y)
+ bb.emit_func_output(x1)
+
+ will result in TVMScript
+
+ .. code-block:: python
+
+ @tvm.script.ir_module
+ class Module:
+ @T.prim_func
+ def te_func(var_rxplaceholder: T.handle, var_compute: T.handle, n: T.int64) -> None:
+ rxplaceholder = T.match_buffer(var_rxplaceholder, [n + T.int64(1)],
+ dtype="float32")
+ compute = T.match_buffer(var_compute, [n + T.int64(1)], dtype="float32")
+ # body
+ # with T.block("root")
+ for i0 in T.serial(0, n + T.int64(1)):
+ with T.block("compute"):
+ i = T.axis.spatial(n + T.int64(1), i0)
+ T.reads([rxplaceholder[i]])
+ T.writes([compute[i]])
+ compute[i] = rxplaceholder[i]
+
+ @R.function
+ def rx_func(x: Tensor((n,), "float32"), y: Tensor(((n + 1),), "float32"))
+ -> Tensor(None, "float32", ndim=-1):
+ # block 0
+ gv = relax.call_tir(te_func, (y,), R.Tensor((n + 1,), "float32"), (n,))
+ return gv
+ """
+ return self.emit(self.call_te(func, *args, **kwargs))
+
+ def match_cast(self, value: Expr, struct_info: StructInfo) -> Var:
+ """Emit a MatchCast.
+
+ Parameters
+ ----------
+ value : tvm.relax.Expr
+ The value of the MatchCast to be emitted.
+
+ struct_info : StructInfo
+ The struct info to be matched.
+
+ Returns
+ -------
+ ret : tvm.relax.Var
+ A newly created variable that get bounds to be the casted result.
+ """
+ return _ffi_api.BlockBuilderEmitMatchCast(self, value, struct_info) # type: ignore
+
+ def emit_output(self, output: Union[Expr, Tuple, List[Expr]]) -> None:
+ """Emit output for the current dataflow block or function.
+
+ Parameters
+ ----------
+ output : Expr | Tuple | List[Expr]
+ The output of the current block/function.
+
+ Returns
+ -------
+ ret : tvm.relax.Var
+ The return variable which gets bound to the output.
+ """
+ if isinstance(output, (list, tuple)):
+ output = Tuple(output)
+ return _ffi_api.BlockBuilderEmitOutput(self, output) # type: ignore
+
+ def emit_func_output(
+ self,
+ output: Union[Expr, Tuple, List[Expr]],
+ params: Optional[Union[Var, Tuple, List[Var]]] = None,
+ ) -> None:
+ """Emit output for the function.
+
+ Parameters
+ ----------
+ output : Expr | Tuple | List[Expr]
+ The output of the current block/function.
+
+ params : tvm.relax.Var | Tuple | List[tvm.relax.Var], optional
+ The parameters of the function to be built.
+ If params is None, it means the params have been initialized in the function with scope.
+
+ Returns
+ -------
+ ret : tvm.relax.Var
+ The return variable which gets bound to the output.
+ """
+ if self._is_emit_func_output_called:
+ raise RuntimeError("emit_func_output must be called exactly once in a relax function.")
+ self._is_emit_func_output_called = True
+
+ if self._func_params is not None and params is not None:
+ raise RuntimeError(
+ "function parameters have been initialized in the function with scope."
+ )
+
+ if self._func_params is None and params is None:
+ raise RuntimeError("Relax function must have parameter.")
+
+ if self._func_params is None:
+ self._func_params = params
+
+ if BlockBuilder.current() is not self:
+ raise RuntimeError("BlockBuilder._current must be self.")
+
+ if isinstance(output, (list, tuple)):
+ output = Tuple(output)
+
+ block = self._end_block()
+ if len(block.bindings) > 0:
+ self._blocks.append(block)
+ seqe = self.normalize(rx.SeqExpr(self._blocks, output))
+
+ # do not specify ret_struct_info and let constructor deduce
+ # from seqe.struct_info
+ func = rx.Function(self._func_params, seqe)
+ for key, value in self._func_attrs.items():
+ func = func.with_attr(key, value)
+ self.end_scope()
+ self.add_func(func, self._func_name)
+
+ def normalize(self, expr: Expr) -> Expr:
+ """Normalize an Expr to complete its shape and type.
+
+ Parameters
+ ----------
+ expr : Expr
+ The input expr.
+
+ Returns
+ -------
+ ret : Expr
+ The expr with normalized shape and type.
+ """
+ return _ffi_api.BlockBuilderNormalize(self, expr) # type: ignore
+
+ def get(self) -> tvm.IRModule:
+ """Return the IRModule being built.
+
+ Returns
+ -------
+ ret : tvm.IRModule
+ An IRModule with Relax and TIR functions being built.
+ """
+ return _ffi_api.BlockBuilderGetContextIRModule(self) # type: ignore
+
+ def get_unique_name(self, name_prefix: str) -> str:
+ """Generate a unique name with a specified prefix.
+
+ Parameters
+ ----------
+ name_hint : str
+ The name prefix.
+
+ Returns
+ -------
+ ret : str
+ The generated name.
+ """
+ return _ffi_api.BlockBuilderGetUniqueName(self, name_prefix) # type: ignore
+
+ def add_func(self, func: BaseFunc, func_name: str) -> GlobalVar:
+ """Add a Relax function or a TIR PrimFunc to the IRModule being built.
+
+ Parameters
+ ----------
+ func : BaseFunc
+ The function to be added.
+
+ func_name : str
+ The name of the function to be added.
+
+ Returns
+ -------
+ gvar : GlobalVar
+ The global var bound to the added function.
+ """
+ return _ffi_api.BlockBuilderAddFunction(self, func, func_name) # type: ignore
+
+ def update_func(self, gv: GlobalVar, updated_func: BaseFunc) -> None:
+ """Add a Relax function or a TIR PrimFunc to the IRModule being built.
+
+ Parameters
+ ----------
+ gv : GlobalVar
+ The global var referring the function to be updated.
+
+ updated_func : BaseFunc
+ The updated function.
+ """
+ return _ffi_api.BlockBuilderUpdateFunction(self, gv, updated_func) # type: ignore
+
+ def current_block_is_dataflow(self) -> bool:
+ """Check if the block being built is DataflowBlock or not.
+
+ Returns
+ -------
+ ret : bool
+ A boolean that indicates if the block being built is DataflowBlock or not.
+ """
+ return _ffi_api.BlockBuilderCurrentBlockIsDataFlow(self) # type: ignore
+
+ def emit_normalized(self, binding: Binding) -> None:
+ """Emit an already normalized binding.
+
+ Parameters
+ ----------
+ binding: Binding
+ The binding to be emitted.
+ """
+ _ffi_api.BlockBuilderEmitNormalized(self, binding) # type: ignore
+
+ def lookup_binding(self, var: Var) -> Optional[Expr]:
+ """Lookup a var in the binding table binding_table_.
+
+ Parameters
+ ----------
+ var: Var
+ The input var.
+
+ Returns
+ -------
+ expr: Expr
+ The Expr bound to the input var.
+ """
+ return _ffi_api.BlockBuilderLookupBinding(self, var) # type: ignore
+
+ def begin_scope(self, params: Optional[List[Var]] = None) -> None:
+ """Begin a new scope, with optional parameters that
+ are visible within the scope.
+
+ Parameters
+ ----------
+ params: Optional[List[Var]]
+ Parameters that are visible within the scope.
+
+ Note
+ ----
+ This function should be called when new scope is introduced
+ (function, seq) to properly track the variable availability
+ and help the best effort deduction.
+ """
+
+ return _ffi_api.BlockBuilderBeginScope(self, params) # type: ignore
+
+ def end_scope(self) -> None:
+ """End the current scope. Please see `begin_scope` for details"""
+
+ return _ffi_api.BlockBuilderEndScope(self) # type: ignore
diff --git a/python/tvm/relax/expr_functor.py b/python/tvm/relax/expr_functor.py
new file mode 100644
index 0000000000..0252720f6e
--- /dev/null
+++ b/python/tvm/relax/expr_functor.py
@@ -0,0 +1,1530 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=no-else-return, unidiomatic-typecheck, invalid-name, arguments-differ
+"""The expression functor of Relax."""
+from typing import Callable, Optional
+
+import tvm
+from tvm.ir import Op
+from tvm.meta_schedule.utils import derived_object
+from tvm.runtime import Object
+
+from ..ir.module import IRModule
+from . import _ffi_api
+from .block_builder import BlockBuilder
+from .expr import (
+ Binding,
+ BindingBlock,
+ Call,
+ Constant,
+ Id,
+ DataflowBlock,
+ DataflowVar,
+ DataTypeImm,
+ Expr,
+ ExternFunc,
+ Function,
+ GlobalVar,
+ If,
+ MatchCast,
+ PrimValue,
+ SeqExpr,
+ ShapeExpr,
+ Span,
+ StringImm,
+ Tuple,
+ TupleGetItem,
+ Var,
+ VarBinding,
+)
+from .struct_info import StructInfo
+
+visitor = derived_object
+"""
+A decorator to wrap user-customized PyExprVisitor as TVM object _PyExprVisitor.
+
+Parameters
+----------
+visitor_cls : PyExprVisitor
+ The user-customized PyExprVisitor.
+
+Returns
+-------
+cls : _PyExprVisitor
+ The decorated TVM object _PyExprVisitor(ExprVisitor on the C++ side).
+
+Example
+-------
+.. code-block:: python
+
+ @relax.expr_functor.visitor
+ class MyExprVisitor(PyExprVisitor):
+ # customize visit function
+ def visit_call_(self, op: Call) -> None:
+ # just for demo purposes
+ ...
+ # myvisitor is now a special visitor that visit every Call with
+ # user-customized visit_call_
+ myvisitor = MyExprVisitor()
+ # apply myvisitor to Expr/Binding/BindingBlock/VarDef
+ myvisitor.visit_expr(expr)
+ myvisitor.visit_binding(binding)
+ myvisitor.visit_binding_block(bindingblock)
+ myvisitor.visit_var_def(var)
+"""
+
+mutator = derived_object
+"""
+A decorator to wrap user-customized PyExprMutator as TVM object _PyExprMutator.
+Note: Cannot override visit function and post-order rewrite at the same time.
+
+Parameters
+----------
+mutator_cls : PyExprMutator
+ The user-customized PyExprMutator.
+
+Returns
+-------
+cls : _PyExprMutator
+ The decorated TVM object _PyExprMutator(ExprMutator on the C++ side).
+
+Example
+-------
+.. code-block:: python
+
+ @relax.expr_functor.mutator
+ class MyExprMutator(PyExprMutator):
+ # customize rewrite function
+ def visit_tuple_(self, op: Tuple) -> Expr:
+ # just for demo purposes
+ ...
+
+ # mymutator is now a special mutator that rewrite every Tuple with
+ # user-customized visit_tuple_
+ mymutator = MyExprMutator()
+ # apply mymutator to Expr/Binding/BindingBlock/VarDef
+ mymutator.visit_expr(expr)
+ mymutator.visit_binding(binding)
+ mymutator.visit_binding_block(bindingblock)
+ mymutator.visit_var_def(var)
+"""
+
+
+class ExprFunctor:
+ """
+ An abstract visitor defined over Expr.
+ Defines the default dispatch over expressions, and
+ implements memoization.
+ """
+
+ def visit_expr(self, expr: Expr) -> Expr:
+ """Apply the visitor to an expression."""
+ if isinstance(expr, Constant): # type: ignore
+ ret = self.visit_constant_(expr)
+ elif isinstance(expr, Tuple):
+ ret = self.visit_tuple_(expr)
+ elif isinstance(expr, DataflowVar):
+ ret = self.visit_dataflow_var_(expr)
+ elif isinstance(expr, Var):
+ ret = self.visit_var_(expr)
+ elif isinstance(expr, ShapeExpr):
+ ret = self.visit_shape_expr_(expr)
+ elif isinstance(expr, ExternFunc):
+ ret = self.visit_extern_func_(expr)
+ elif isinstance(expr, GlobalVar): # type: ignore
+ ret = self.visit_global_var_(expr)
+ elif isinstance(expr, Function):
+ ret = self.visit_function_(expr)
+ elif isinstance(expr, Call): # type: ignore
+ ret = self.visit_call_(expr)
+ elif isinstance(expr, SeqExpr):
+ ret = self.visit_seq_expr_(expr)
+ elif isinstance(expr, If): # type: ignore
+ ret = self.visit_if_(expr)
+ elif isinstance(expr, Op):
+ ret = self.visit_op_(expr)
+ elif isinstance(expr, TupleGetItem):
+ ret = self.visit_tuple_getitem_(expr)
+ elif isinstance(expr, PrimValue):
+ ret = self.visit_prim_value_(expr)
+ elif isinstance(expr, StringImm):
+ ret = self.visit_string_imm_(expr)
+ elif isinstance(expr, DataTypeImm):
+ ret = self.visit_data_type_imm_(expr)
+ else:
+ raise TypeError("Invalid type: {0}".format(type(expr)))
+
+ return ret
+
+ def visit_constant_(self, op: Constant):
+ raise NotImplementedError()
+
+ def visit_tuple_(self, op: Tuple):
+ raise NotImplementedError()
+
+ def visit_dataflow_var_(self, op: DataflowVar):
+ raise NotImplementedError()
+
+ def visit_var_(self, op: Var):
+ raise NotImplementedError()
+
+ def visit_shape_expr_(self, op: ShapeExpr):
+ raise NotImplementedError()
+
+ def visit_extern_func_(self, op: ExternFunc):
+ raise NotImplementedError()
+
+ def visit_global_var_(self, op: GlobalVar):
+ raise NotImplementedError()
+
+ def visit_function_(self, op: Function):
+ raise NotImplementedError()
+
+ def visit_call_(self, op: Call):
+ raise NotImplementedError()
+
+ def visit_seq_expr_(self, op: SeqExpr):
+ raise NotImplementedError()
+
+ def visit_if_(self, op: If):
+ raise NotImplementedError()
+
+ def visit_op_(self, op: Op):
+ raise NotImplementedError()
+
+ def visit_tuple_getitem_(self, op: TupleGetItem):
+ raise NotImplementedError()
+
+ def visit_prim_value_(self, op: PrimValue):
+ raise NotImplementedError()
+
+ def visit_string_imm_(self, op: StringImm):
+ raise NotImplementedError()
+
+ def visit_data_type_imm_(self, op: DataTypeImm):
+ raise NotImplementedError()
+
+ def visit_var_binding_(self, binding: VarBinding):
+ raise NotImplementedError()
+
+ def visit_match_cast_(self, binding: MatchCast):
+ raise NotImplementedError()
+
+ def visit_binding_block_(self, block: BindingBlock):
+ raise NotImplementedError()
+
+ def visit_dataflow_block_(self, block: DataflowBlock):
+ raise NotImplementedError()
+
+ def visit_var_def_(self, var: Var):
+ raise NotImplementedError()
+
+ def visit_dataflow_var_def_(self, var: DataflowVar):
+ raise NotImplementedError()
+
+ def visit_binding(self, binding: Binding):
+ if isinstance(binding, MatchCast):
+ self.visit_match_cast_(binding)
+ elif isinstance(binding, VarBinding):
+ self.visit_var_binding_(binding)
+ else:
+ raise TypeError("Invalid type: {0}".format(type(binding)))
+
+ def visit_binding_block(self, block: BindingBlock):
+ if isinstance(block, DataflowBlock):
+ self.visit_dataflow_block_(block)
+ elif isinstance(block, BindingBlock):
+ self.visit_binding_block_(block)
+ else:
+ raise TypeError("Invalid type: {0}".format(type(block)))
+
+ def visit_var_def(self, var: Var):
+ if isinstance(var, DataflowVar):
+ self.visit_dataflow_var_def_(var)
+ elif isinstance(var, Var):
+ self.visit_var_def_(var)
+ else:
+ raise TypeError("Invalid type: {0}".format(type(var)))
+
+
+@tvm._ffi.register_object("expr_functor.PyExprVisitor")
+class _PyExprVisitor(Object):
+ """
+ A TVM object to support customization of ExprVisitor on the python side.
+ This is the decorated result returned from visitor decorator.
+
+ WARNING: This is NOT the user facing class for method overwriting inheritance.
+
+ See also: visitor, PyExprVisitor
+ """
+
+ def __init__(
+ self,
+ f_visit_expr: Callable = None,
+ f_visit_constant_: Callable = None,
+ f_visit_tuple_: Callable = None,
+ f_visit_var_: Callable = None,
+ f_visit_dataflow_var_: Callable = None,
+ f_visit_shape_expr_: Callable = None,
+ f_visit_extern_func_: Callable = None,
+ f_visit_global_var_: Callable = None,
+ f_visit_function_: Callable = None,
+ f_visit_call_: Callable = None,
+ f_visit_seq_expr_: Callable = None,
+ f_visit_if_: Callable = None,
+ f_visit_op_: Callable = None,
+ f_visit_tuple_getitem_: Callable = None,
+ f_visit_prim_value_: Callable = None,
+ f_visit_string_imm_: Callable = None,
+ f_visit_data_type_imm_: Callable = None,
+ f_visit_binding: Callable = None,
+ f_visit_var_binding_: Callable = None,
+ f_visit_match_cast_: Callable = None,
+ f_visit_binding_block: Callable = None,
+ f_visit_binding_block_: Callable = None,
+ f_visit_dataflow_block_: Callable = None,
+ f_visit_var_def: Callable = None,
+ f_visit_var_def_: Callable = None,
+ f_visit_dataflow_var_def_: Callable = None,
+ f_visit_span: Callable = None,
+ ) -> None:
+ """Constructor."""
+
+ self.__init_handle_by_constructor__(
+ _ffi_api.MakePyExprVisitor, # type: ignore
+ f_visit_expr,
+ f_visit_constant_,
+ f_visit_tuple_,
+ f_visit_var_,
+ f_visit_dataflow_var_,
+ f_visit_shape_expr_,
+ f_visit_extern_func_,
+ f_visit_global_var_,
+ f_visit_function_,
+ f_visit_call_,
+ f_visit_seq_expr_,
+ f_visit_if_,
+ f_visit_op_,
+ f_visit_tuple_getitem_,
+ f_visit_prim_value_,
+ f_visit_string_imm_,
+ f_visit_data_type_imm_,
+ f_visit_binding,
+ f_visit_var_binding_,
+ f_visit_match_cast_,
+ f_visit_binding_block,
+ f_visit_binding_block_,
+ f_visit_dataflow_block_,
+ f_visit_var_def,
+ f_visit_var_def_,
+ f_visit_dataflow_var_def_,
+ f_visit_span,
+ )
+
+ def visit_expr(self, expr: Expr) -> None:
+ """Generic dispatcher for Expr.
+
+ Parameters
+ ----------
+ expr : Expr
+ The expr to be visited.
+ """
+ return _ffi_api.PyExprVisitorVisitExpr(self, expr) # type: ignore
+
+ def visit_binding(self, binding: Binding) -> None:
+ """Generic dispatcher for Binding.
+
+ Parameters
+ ----------
+ binding : Binding
+ The binding to be visited.
+ """
+ return _ffi_api.PyExprVisitorVisitBinding(self, binding) # type: ignore
+
+ def visit_binding_block(self, block: BindingBlock) -> None:
+ """Generic dispatcher for BindingBlock.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The block to be visited.
+ """
+ return _ffi_api.PyExprVisitorVisitBindingBlock(self, block) # type: ignore
+
+ def visit_var_def(self, var: Var) -> None:
+ """Generic dispatcher for visiting the var definition site.
+ Note that visit_var_() will only visit the usage site of an Var.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be visited.
+ """
+ return _ffi_api.PyExprVisitorVisitVarDef(self, var) # type: ignore
+
+
+class PyExprVisitor:
+ """
+ An abstract ExprVisitor with customized methods on the python-side.
+ This is the user facing class for method overwriting inheritance.
+ _tvm_metadata discribes the class to inherit("cls"), the methods
+ that users can overwrite("methods").
+
+ Note: @relax.expr_functor.visitor is required for proper usage of any inherited class.
+
+ See also: visitor, _PyExprVisitor
+
+ Example:
+ @relax.expr_functor.visitor
+ def MyExprVisitor(PyExprVisitor):
+ ...
+ """
+
+ _tvm_metadata = {
+ "cls": _PyExprVisitor,
+ "methods": [
+ "visit_expr",
+ "visit_constant_",
+ "visit_tuple_",
+ "visit_var_",
+ "visit_dataflow_var_",
+ "visit_shape_expr_",
+ "visit_extern_func_",
+ "visit_global_var_",
+ "visit_function_",
+ "visit_call_",
+ "visit_seq_expr_",
+ "visit_if_",
+ "visit_op_",
+ "visit_tuple_getitem_",
+ "visit_prim_value_",
+ "visit_string_imm_",
+ "visit_data_type_imm_",
+ "visit_binding",
+ "visit_var_binding_",
+ "visit_match_cast_",
+ "visit_binding_block",
+ "visit_binding_block_",
+ "visit_dataflow_block_",
+ "visit_var_def",
+ "visit_var_def_",
+ "visit_dataflow_var_def_",
+ "visit_span",
+ ],
+ }
+
+ def visit_expr(self, expr: Expr) -> None:
+ """Generic dispatcher for Expr.
+ Users can customized this function to overwrite VisitExpr(const Expr& expr) on the C++ side.
+
+ Parameters
+ ----------
+ expr : Expr
+ The expr to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.PyExprVisitorVisitExpr(self._outer(), expr) # type: ignore
+
+ def visit_binding(self, binding: Binding) -> None:
+ """Generic dispatcher for Binding.
+ Users can customized this function to overwrite VisitBinding(const Binding& binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : Binding
+ The binding to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.PyExprVisitorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_binding_block(self, block: BindingBlock) -> None:
+ """Generic dispatcher for BindingBlock.
+ Users can customized this function to overwrite VisitBindingBlock(const BindingBlock& block)
+ on the C++ side.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The block to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.PyExprVisitorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_var_def(self, var: Var) -> None:
+ """Generic dispatcher for visiting the var definition site.
+ Users can customized this function to overwrite VisitVarDef(const Var& var) on the C++ side.
+ Note that visit_var_() will only visit the usage site of an Var.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.PyExprVisitorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_constant_(self, op: Constant) -> None:
+ """Visit Constant.
+ Users can customized this function to overwrite VisitExpr_(const ConstantNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Constant
+ The Constant to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_tuple_(self, op: Tuple) -> None:
+ """Visit Tuple.
+ Users can customized this function to overwrite VisitExpr_(const TupleNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Tuple
+ The Tuple to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_var_(self, op: Var) -> None:
+ """Visit Var.
+ Users can customized this function to overwrite VisitExpr_(const VarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Var
+ The Var to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_dataflow_var_(self, op: DataflowVar) -> None:
+ """Visit DataflowVar.
+ Users can customized this function to overwrite VisitExpr_(const DataflowVarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : DataflowVar
+ The DataflowVar to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_shape_expr_(self, op: ShapeExpr) -> None:
+ """Visit ShapeExpr.
+ Users can customized this function to overwrite VisitExpr_(const ShapeExprNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : ShapeExpr
+ The ShapeExpr to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_extern_func_(self, op: ExternFunc) -> None:
+ """Visit ExternFunc.
+ Users can customized this function to overwrite VisitExpr_(const ExternFuncNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : ExternFunc
+ The ExternFunc to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_global_var_(self, op: GlobalVar) -> None:
+ """Visit GlobalVar.
+ Users can customized this function to overwrite VisitExpr_(const GlobalVarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : GlobalVar
+ The GlobalVar to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_function_(self, op: Function) -> None:
+ """Visit Function.
+ Users can customized this function to overwrite VisitExpr_(const FunctionNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Function
+ The Function to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_call_(self, op: Call) -> None:
+ """Visit Call.
+ Users can customized this function to overwrite VisitExpr_(const CallNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Call
+ The Call to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_seq_expr_(self, op: SeqExpr) -> None:
+ """Visit SeqExpr.
+ Users can customized this function to overwrite VisitExpr_(const SeqExprNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : SeqExpr
+ The SeqExpr to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_if_(self, op: If) -> None:
+ """Visit If.
+ Users can customized this function to overwrite VisitExpr_(const IfNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : If
+ The If to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_op_(self, op: Op) -> None:
+ """Visit Op.
+ Users can customized this function to overwrite VisitExpr_(const OpNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Op
+ The Op to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_tuple_getitem_(self, op: TupleGetItem) -> None:
+ """Visit TupleGetItem.
+ Users can customized this function to overwrite VisitExpr_(const TupleGetItemNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : TupleGetItem
+ The TupleGetItem to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_prim_value_(self, op: PrimValue) -> None:
+ """Visit PrimValue.
+ Users can customized this function to overwrite VisitExpr_(const PrimValueNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : PrimValue
+ The PrimValue to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_string_imm_(self, op: StringImm) -> None:
+ """Visit StringImm.
+ Users can customized this function to overwrite VisitExpr_(const StringImmNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : StringImm
+ The StringImm to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_data_type_imm_(self, op: DataTypeImm) -> None:
+ """Visit DataTypeImm.
+ Users can customized this function to overwrite VisitExpr_(const DataTypeImmNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : DataTypeImm
+ The DataTypeImm to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_var_binding_(self, binding: VarBinding) -> None:
+ """Visit VarBinding.
+ Users can customized this function to overwrite VisitBinding_(const VarBindingNode* binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : VarBinding
+ The VarBinding to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_match_cast_(self, binding: MatchCast) -> None:
+ """Visit MatchCast.
+ Users can customized this function to overwrite VisitBinding_(const MatchCastNode* binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : MatchCast
+ The MatchCast to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_binding_block_(self, block: BindingBlock) -> None:
+ """Visit BindingBlock.
+ Users can customized this function to overwrite VisitBindingBlock_(const BindingBlockNode*
+ block) on the C++ side.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The BindingBlock to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_dataflow_block_(self, block: DataflowBlock) -> None:
+ """Visit DataflowBlock.
+ Users can customized this function to overwrite VisitBindingBlock_(const DataflowBlockNode*
+ block) on the C++ side.
+
+ Parameters
+ ----------
+ block : DataflowBlock
+ The DataflowBlock to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_var_def_(self, var: Var) -> None:
+ """Visit the Var definition site.
+ Users can customized this function to overwrite VisitVarDef_(const VarNode* var)
+ on the C++ side.
+
+ Parameters
+ ----------
+ var : Var
+ The Var to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_dataflow_var_def_(self, var: DataflowVar) -> None:
+ """Visit the DataflowVar definition site.
+ Users can customized this function to overwrite VisitVarDef_(const DataflowVarNode* var)
+ on the C++ side.
+
+ Parameters
+ ----------
+ var : DataflowVar
+ The DataflowVar to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_span(self, span: Span) -> None:
+ """Visit Span.
+ Users can customized this function to overwrite VisitSpan(const Span& span) on the C++ side.
+
+ Parameters
+ ----------
+ span : Span
+ The Span to be visited.
+ """
+ # Using self._outer() to ref _PyExprVisitor
+ return _ffi_api.ExprVisitorVisitSpan(self._outer(), span) # type: ignore
+
+
+@tvm._ffi.register_object("expr_functor.PyExprMutator")
+class _PyExprMutator(Object):
+ """
+ A TVM object to support customization of ExprMutator on the python side.
+ This is the decorated result returned from mutator decorator.
+
+ WARNING: This is NOT the user facing class for method overwriting inheritance.
+
+ See also: mutator, PyExprmutator
+ """
+
+ def __init__(
+ self,
+ builder: BlockBuilder = None,
+ f_visit_expr: Callable = None,
+ f_visit_constant_: Callable = None,
+ f_visit_tuple_: Callable = None,
+ f_visit_var_: Callable = None,
+ f_visit_dataflow_var_: Callable = None,
+ f_visit_shape_expr_: Callable = None,
+ f_visit_extern_func_: Callable = None,
+ f_visit_global_var_: Callable = None,
+ f_visit_function_: Callable = None,
+ f_visit_call_: Callable = None,
+ f_visit_seq_expr_: Callable = None,
+ f_visit_if_: Callable = None,
+ f_visit_op_: Callable = None,
+ f_visit_tuple_getitem_: Callable = None,
+ f_visit_prim_value_: Callable = None,
+ f_visit_string_imm_: Callable = None,
+ f_visit_data_type_imm_: Callable = None,
+ f_visit_binding: Callable = None,
+ f_visit_var_binding_: Callable = None,
+ f_visit_match_cast_: Callable = None,
+ f_visit_binding_block: Callable = None,
+ f_visit_binding_block_: Callable = None,
+ f_visit_dataflow_block_: Callable = None,
+ f_visit_var_def: Callable = None,
+ f_visit_var_def_: Callable = None,
+ f_visit_dataflow_var_def_: Callable = None,
+ f_visit_span: Callable = None,
+ ) -> None:
+ """Constructor."""
+
+ self.__init_handle_by_constructor__(
+ _ffi_api.MakePyExprMutator, # type: ignore
+ builder,
+ f_visit_expr,
+ f_visit_constant_,
+ f_visit_tuple_,
+ f_visit_var_,
+ f_visit_dataflow_var_,
+ f_visit_shape_expr_,
+ f_visit_extern_func_,
+ f_visit_global_var_,
+ f_visit_function_,
+ f_visit_call_,
+ f_visit_seq_expr_,
+ f_visit_if_,
+ f_visit_op_,
+ f_visit_tuple_getitem_,
+ f_visit_prim_value_,
+ f_visit_string_imm_,
+ f_visit_data_type_imm_,
+ f_visit_binding,
+ f_visit_var_binding_,
+ f_visit_match_cast_,
+ f_visit_binding_block,
+ f_visit_binding_block_,
+ f_visit_dataflow_block_,
+ f_visit_var_def,
+ f_visit_var_def_,
+ f_visit_dataflow_var_def_,
+ f_visit_span,
+ )
+
+ def visit_expr(self, expr: Expr) -> Expr:
+ """Generic dispatcher for Expr.
+
+ Parameters
+ ----------
+ expr : Expr
+ The expr to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation.
+ """
+ return _ffi_api.PyExprMutatorVisitExpr(self, expr) # type: ignore
+
+ def visit_binding(self, binding: Binding) -> None:
+ """Generic dispatcher for Binding.
+
+ Parameters
+ ----------
+ binding : Binding
+ The binding to be visited.
+ """
+ return _ffi_api.PyExprMutatorVisitBinding(self, binding) # type: ignore
+
+ def visit_binding_block(self, block: BindingBlock) -> BindingBlock:
+ """Generic dispatcher for BindingBlock.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The block to be visited.
+
+ Returns
+ -------
+ result : BindingBlock
+ The binding block after transformation.
+ """
+ return _ffi_api.PyExprMutatorVisitBindingBlock(self, block) # type: ignore
+
+ def visit_var_def(self, var: Var) -> Var:
+ """Generic dispatcher for visiting the var definition site.
+ Note that visit_var_() will only visit the usage site of an Var.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be visited.
+
+ Returns
+ -------
+ result : Var
+ The var after post-order rewritten.
+ """
+ return _ffi_api.PyExprMutatorVisitVarDef(self, var) # type: ignore
+
+
+class PyExprMutator:
+ """
+ An abstract ExprMutator with customized methods on the python-side.
+ This is the user facing class for method overwriting inheritance.
+ _tvm_metadata discribes the class to inherit("cls"), the methods that users can
+ overwrite("methods"), the constructor's parameters("fields")
+
+ Note: @relax.expr_functor.mutator is required for proper usage of any inherited class.
+
+ See also: visitor, _PyExprVisitor
+
+ Example:
+ @relax.expr_functor.mutator
+ def MyExprMutator(PyExprMutator):
+ ...
+ """
+
+ _tvm_metadata = {
+ "cls": _PyExprMutator,
+ "fields": ["builder_"],
+ "methods": [
+ "visit_expr",
+ "visit_constant_",
+ "visit_tuple_",
+ "visit_var_",
+ "visit_dataflow_var_",
+ "visit_shape_expr_",
+ "visit_extern_func_",
+ "visit_global_var_",
+ "visit_function_",
+ "visit_call_",
+ "visit_seq_expr_",
+ "visit_if_",
+ "visit_op_",
+ "visit_tuple_getitem_",
+ "visit_prim_value_",
+ "visit_string_imm_",
+ "visit_data_type_imm_",
+ "visit_binding",
+ "visit_var_binding_",
+ "visit_match_cast_",
+ "visit_binding_block",
+ "visit_binding_block_",
+ "visit_dataflow_block_",
+ "visit_var_def",
+ "visit_var_def_",
+ "visit_dataflow_var_def_",
+ "visit_span",
+ ],
+ }
+
+ def __init__(self, mod: Optional[IRModule] = None) -> None:
+ """Constructor"""
+ self.builder_ = BlockBuilder(mod)
+
+ def visit_expr(self, expr: Expr) -> Expr:
+ """Generic dispatcher for Expr.
+ Users can customized this function to overwrite VisitExpr(const Expr& expr) on the C++ side.
+
+ Parameters
+ ----------
+ expr : Expr
+ The expr to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorVisitExpr(self._outer(), expr) # type: ignore
+
+ def visit_binding(self, binding: Binding) -> None:
+ """Generic dispatcher for Binding.
+ Users can customized this function to overwrite VisitBinding(const Binding& binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : Binding
+ The binding to be visited.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_binding_block(self, block: BindingBlock) -> BindingBlock:
+ """Generic dispatcher for BindingBlock.
+ Users can customized this function to overwrite VisitBindingBlock(const BindingBlock& block)
+ on the C++ side.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The block to be visited.
+
+ Returns
+ -------
+ result : BindingBlock
+ The binding block after transformation.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_var_def(self, var: Var) -> Var:
+ """Generic dispatcher for visiting the var definition site.
+ Users can customized this function to overwrite VisitVarDef(const Var& var) on the C++ side.
+ Note that visit_var_() will only visit the usage site of an Var.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be visited.
+
+ Returns
+ -------
+ result: Var
+ The var after post-order rewritten.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_constant_(self, op: Constant) -> Expr:
+ """Visit Constant.
+ Users can customized this function to overwrite VisitExpr_(const ConstantNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Constant
+ The Constant to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_tuple_(self, op: Tuple) -> Expr:
+ """Visit Tuple.
+ Users can customized this function to overwrite VisitExpr_(const TupleNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Tuple
+ The Tuple to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_var_(self, op: Var) -> Expr:
+ """Visit Var.
+ Users can customized this function to overwrite VisitExpr_(const VarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Var
+ The Var to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_dataflow_var_(self, op: DataflowVar) -> Expr:
+ """Visit DataflowVar.
+ Users can customized this function to overwrite VisitExpr_(const DataflowVarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : DataflowVar
+ The DataflowVar to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_shape_expr_(self, op: ShapeExpr) -> Expr:
+ """Visit ShapeExpr.
+ Users can customized this function to overwrite VisitExpr_(const ShapeExprNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : ShapeExpr
+ The ShapeExpr to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_extern_func_(self, op: ExternFunc) -> Expr:
+ """Visit ExternFunc.
+ Users can customized this function to overwrite VisitExpr_(const ExternFuncNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : ExternFunc
+ The ExternFunc to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_global_var_(self, op: GlobalVar) -> Expr:
+ """Visit GlobalVar.
+ Users can customized this function to overwrite VisitExpr_(const GlobalVarNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : GlobalVar
+ The GlobalVar to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_function_(self, op: Function) -> Expr:
+ """Visit Function.
+ Users can customized this function to overwrite VisitExpr_(const FunctionNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Function
+ The Function to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_call_(self, op: Call) -> Expr:
+ """Visit Call.
+ Users can customized this function to overwrite VisitExpr_(const CallNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Call
+ The Call to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_seq_expr_(self, op: SeqExpr) -> Expr:
+ """Visit SeqExpr.
+ Users can customized this function to overwrite VisitExpr_(const SeqExprNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : SeqExpr
+ The SeqExpr to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_if_(self, op: If) -> Expr:
+ """Visit If.
+ Users can customized this function to overwrite VisitExpr_(const IfNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : If
+ The If to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_op_(self, op: Op) -> Expr:
+ """Visit Op.
+ Users can customized this function to overwrite VisitExpr_(const OpNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : Op
+ The Op to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_tuple_getitem_(self, op: TupleGetItem) -> Expr:
+ """Visit TupleGetItem.
+ Users can customized this function to overwrite VisitExpr_(const TupleGetItemNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : TupleGetItem
+ The TupleGetItem to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_prim_value_(self, op: PrimValue) -> Expr:
+ """Visit PrimValue.
+ Users can customized this function to overwrite VisitExpr_(const PrimValueNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : PrimValue
+ The PrimValue to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_string_imm_(self, op: StringImm) -> Expr:
+ """Visit StringImm.
+ Users can customized this function to overwrite VisitExpr_(const StringImmNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : StringImm
+ The StringImm to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_data_type_imm_(self, op: DataTypeImm) -> Expr:
+ """Visit DataTypeImm.
+ Users can customized this function to overwrite VisitExpr_(const DataTypeImmNode* op)
+ on the C++ side.
+
+ Parameters
+ ----------
+ op : DataTypeImm
+ The DataTypeImm to be visited.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitExpr(self._outer(), op) # type: ignore
+
+ def visit_var_binding_(self, binding: VarBinding) -> None:
+ """Visit VarBinding.
+ Users can customized this function to overwrite VisitBinding_(const VarBindingNode* binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : VarBinding
+ The VarBinding to be visited.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_match_cast_(self, binding: MatchCast) -> None:
+ """Visit MatchCast.
+ Users can customized this function to overwrite VisitBinding_(const MatchCastNode* binding)
+ on the C++ side.
+
+ Parameters
+ ----------
+ binding : MatchCast
+ The MatchCast to be visited.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitBinding(self._outer(), binding) # type: ignore
+
+ def visit_binding_block_(self, block: BindingBlock) -> BindingBlock:
+ """Visit BindingBlock.
+ Users can customized this function to overwrite VisitBindingBlock_(const BindingBlockNode*
+ block) on the C++ side.
+
+ Parameters
+ ----------
+ block : BindingBlock
+ The BindingBlock to be visited.
+
+ Returns
+ -------
+ result : BindingBlock
+ The binding block after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_dataflow_block_(self, block: DataflowBlock) -> BindingBlock:
+ """Visit DataflowBlock.
+ Users can customized this function to overwrite VisitBindingBlock_(const DataflowBlockNode*
+ block) on the C++ side.
+
+ Parameters
+ ----------
+ block : DataflowBlock
+ The DataflowBlock to be visited.
+
+ Returns
+ -------
+ result : BindingBlock
+ The binding block after transformation
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitBindingBlock(self._outer(), block) # type: ignore
+
+ def visit_var_def_(self, var: Var) -> Var:
+ """Visit the Var definition site.
+ Users can customized this function to overwrite VisitVarDef_(const VarNode* var)
+ on the C++ side.
+
+ Parameters
+ ----------
+ var : Var
+ The Var to be visited.
+
+ Returns
+ -------
+ result : Var
+ The var after post-order rewritten.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_dataflow_var_def_(self, var: DataflowVar) -> Var:
+ """Visit the DataflowVar definition site.
+ Users can customized this function to overwrite VisitVarDef_(const DataflowVarNode* var)
+ on the C++ side.
+
+ Parameters
+ ----------
+ var : DataflowVar
+ The DataflowVar to be visited.
+
+ Returns
+ -------
+ result : Var
+ The var after post-order rewritten.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.ExprMutatorVisitVarDef(self._outer(), var) # type: ignore
+
+ def visit_span(self, span: Span) -> Span:
+ """Visit Span.
+ Users can customized this function to overwrite VisitSpan(const Span& span) on the C++ side.
+
+ Parameters
+ ----------
+ span : Span
+ The Span to be visited.
+
+ Returns
+ -------
+ result : Span
+ The span after transformation.
+ """
+ raise NotImplementedError
+
+ def visit_expr_post_order(self, expr: Expr) -> Expr:
+ """Post-order rewrite an Expr and normalize.
+
+ Parameters
+ ----------
+ expr : Expr
+ The Expr to be rewritten.
+
+ Returns
+ -------
+ result : Expr
+ The Expr after post-order rewritten.
+ """
+ return _ffi_api.PyExprMutatorVisitExprPostOrder(self._outer(), expr) # type: ignore
+
+ def set_var_remap(self, vid: Id, var: Var) -> None:
+ """Remap a var to a new var in use-site.
+
+ Parameters
+ ----------
+ vid : Id
+ The vid of the old var.
+ var : Var
+ The new var.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorSetVarRemap(self._outer(), vid, var) # type: ignore
+
+ def get_var_remap(self, vid: Id) -> Var:
+ """Remap a var to a new var in use-site.
+
+ Parameters
+ ----------
+ vid : Id
+ The vid of the old var
+
+ Returns
+ -------
+ var : Var
+ The remapped var.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorGetVarRemap(self._outer(), vid) # type: ignore
+
+ def visit_with_new_scope(self, expr: Expr) -> Expr:
+ """Rewrite the expr with a new scope, used in a Function's body and the branches of If.
+
+ Parameters
+ ----------
+ expr : Expr
+ The expr to be visited.
+
+ Returns
+ -------
+ var : Var
+ The expr after visiting.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorVisitWithNewScope(self._outer(), expr) # type: ignore
+
+ def lookup_binding(self, var: Var) -> Optional[Expr]:
+ """Look up the value bound to a variable.
+ Note: For function parameters, this function returns NullOpt.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be looked up.
+
+ Returns
+ -------
+ var : Var
+ The value bound to the input var.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorLookupBinding(self._outer(), var) # type: ignore
+
+ def with_struct_info(self, var: Var, struct_info: StructInfo) -> Var:
+ """Create a new var with specified shape and type if the original var's shape or type does
+ not match with the specified ones.
+
+ Parameters
+ ----------
+ var : Var
+ The var to be updated.
+ struct_info : StructInfo
+ The struct info.
+
+ Returns
+ -------
+ var : Var
+ The var filled with shape and type.
+ """
+ # Using self._outer() to ref _PyExprMutator
+ return _ffi_api.PyExprMutatorWithStructInfo(self._outer(), var, struct_info) # type: ignore
diff --git a/python/tvm/relax/op/__init__.py b/python/tvm/relax/op/__init__.py
new file mode 100644
index 0000000000..101b0827d6
--- /dev/null
+++ b/python/tvm/relax/op/__init__.py
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=wildcard-import, redefined-builtin
+"""Relax core operators."""
+
+# Operators
+from .base import *
+from .binary import *
diff --git a/python/tvm/relax/op/_ffi_api.py b/python/tvm/relax/op/_ffi_api.py
new file mode 100644
index 0000000000..8dc6a1b4fb
--- /dev/null
+++ b/python/tvm/relax/op/_ffi_api.py
@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+"""FFI APIs for tvm.relax.op"""
+import tvm._ffi
+
+tvm._ffi._init_api("relax.op", __name__)
diff --git a/python/tvm/relax/op/base.py b/python/tvm/relax/op/base.py
new file mode 100644
index 0000000000..d76b155beb
--- /dev/null
+++ b/python/tvm/relax/op/base.py
@@ -0,0 +1,358 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# pylint: disable=redefined-builtin
+"""The base Relax operators."""
+from typing import Union, List, Tuple, Optional
+
+
+import tvm
+from tvm.runtime.object import Object
+
+from . import _ffi_api
+from ..expr import Expr, ShapeExpr, Call, ExternFunc
+from ..expr import Tuple as RxTuple
+from ..struct_info import StructInfo, TensorStructInfo
+from ...ir import PrimExpr
+from ..utils import args_converter
+
+
+py_print = print # pylint: disable=invalid-name
+
+
+def null_value() -> Call:
+ """Create a call node that represents a null value object.
+
+ Returns
+ -------
+ ret: Call
+ The created call node.
+ """
+ return _ffi_api.null_value() # type: ignore
+
+
+@args_converter.auto
+def call_tir(
+ func: Union[str, Expr],
+ args: Expr,
+ out_sinfo: Union[TensorStructInfo, List[TensorStructInfo]],
+ tir_vars: Optional[Union[ShapeExpr, Tuple[PrimExpr], List[PrimExpr]]] = None,
+) -> Call:
+ """
+ Call a destination-passing-style function and return the output.
+
+ Parameters
+ ----------
+ func : Union[str, Expr]
+ The destination-passing-style function, can be ExternFunc or PrimFunc.
+
+ args : Expr
+ The input arguments.
+
+ out_sinfo : Union[TensorStructInfo, List[TensorStructInfo]]
+ The structure info of the call_tir output.
+ It should be a single or a list of TensorStructInfo. Each one denotes the
+ structure info of a returned tensor.
+
+ tir_vars : Optional[Union[ShapeExpr, Tuple[PrimExpr], List[PrimExpr]]]
+ ShapeExpr representing a tuple of integers to unpack when calling func. Is null if not used
+
+ Returns
+ -------
+ ret: Call
+ A call node for the call_tir operator.
+ """
+ if isinstance(func, str):
+ func = ExternFunc(func)
+
+ if isinstance(args, Expr) and not isinstance(args, RxTuple): # type: ignore
+ args = RxTuple((args,))
+
+ if not isinstance(out_sinfo, list):
+ out_sinfo = [out_sinfo]
+
+ if isinstance(tir_vars, (list, tuple)):
+ tir_vars = ShapeExpr(tir_vars)
+
+ return _ffi_api.call_tir(func, args, out_sinfo, tir_vars) # type: ignore
+
+
+@args_converter.auto
+def call_builtin_with_ctx(
+ func: Union[str, Expr],
+ args: Expr,
+ *,
+ sinfo_args: Optional[Union[StructInfo, List[StructInfo]]] = None,
+) -> Call:
+ """Call a builtin function func.
+
+ Parameters
+ ----------
+ func : Expr
+ The builtin function to be called.
+
+ args : Expr
+ The input arguments.
+
+ sinfo_args: Optional[Union[StructInfo, List[StructInfo]]]
+ The struct info arguments to the call node.
+
+ Returns
+ -------
+ ret: Call
+ The created call node.
+ """
+ if isinstance(func, str):
+ func = ExternFunc(func)
+
+ if sinfo_args is not None and not isinstance(sinfo_args, (list, tuple)):
+ sinfo_args = [sinfo_args]
+
+ return _ffi_api.call_builtin_with_ctx( # type: ignore
+ func,
+ args,
+ sinfo_args, # type: ignore
+ )
+
+
+@args_converter.auto
+def make_closure(
+ func: Expr,
+ args: Expr,
+) -> Object:
+ """
+ Create a closure with free variables and return the closure.
+
+ Parameters
+ ----------
+ func : Expr
+ The closure, can be ExternFunc or PrimFunc.
+
+ args : Expr
+ The input arguments.
+
+
+ Returns
+ -------
+ ret: Object
+ The VMClosure.
+ """
+
+ return _ffi_api.make_closure(func, args) # type: ignore
+
+
+@args_converter.auto
+def invoke_closure(
+ closure: Expr,
+ args: Expr,
+ sinfo_args: Union[List[StructInfo], StructInfo],
+) -> Object:
+ """
+ Invoke a closure.
+
+ Parameters
+ ----------
+ closure : Expr
+ The VMClosure object.
+
+ args : Expr
+ The input arguments.
+
+ type_args: Union[List[StructInfo], StructInfo]
+ The structure info arguments of the CallNode
+
+ Returns
+ -------
+ ret: Object
+ The result.
+ """
+
+ if not isinstance(sinfo_args, (list, tuple)):
+ sinfo_args = [sinfo_args]
+
+ return _ffi_api.invoke_closure(closure, args, sinfo_args) # type: ignore
+
+
+def render_object(val: tvm.Object) -> str:
+ """
+ Given a TVM Object, renders it in string form. Used for Relax printing and assertions.
+
+ Parameters
+ ----------
+ val: tvm.Object
+ An object to render
+
+ Returns
+ -------
+ ret: str
+ A string representing the value, ideally human-readable
+ """
+ if isinstance(val, tvm.runtime.ndarray.NDArray):
+ return str(val)
+ # no pretty-printer by default, so if we don't handle this,
+ # then we can't look inside tuples
+ if isinstance(val, tvm.runtime.container.ADT):
+ # the fields array of an ADT cannot be directly accessed in Python
+ # so we have to get the length and index into the fields separately
+ fields = ", ".join([render_object(val[i]) for i in range(len(val))])
+ # special case: tag = 0 is a tuple
+ if val.tag == 0:
+ return f"({fields})"
+ return f"ADT(tag={val.tag}, fields=[{fields}])"
+ return str(val)
+
+
+@tvm.register_func("relax.run.print")
+def relax_print(format_str: str, *format_args: tvm.Object) -> None:
+ """
+ Takes a list of values to print, formats with the given format string.
+ If the format string is empty, simply prints.
+
+ Call from TVM script like this:
+ `relax.print(value1, value2, ..., valueN, format=format_str)`
+ or
+ `relax.print(value1, value2, ..., valueN) # format_str defaults to ""`
+
+ Parameters
+ ----------
+ format_str: str
+ The last argument is a Python-style format string for printing the value
+
+ format_args: List[Object]
+ The values to print.
+ """
+ val_strs = map(render_object, format_args)
+ if format_str == "":
+ py_print(*val_strs)
+ else:
+ py_print(format_str.format(*val_strs))
+
+
+def print(*values: List[Expr], format: str = "") -> Expr:
+ """Print op to print the values
+
+ Parameters
+ ----------
+ values : List[Expr]
+ The values to print.
+
+ format_str: str
+ The format string.
+
+ Returns
+ -------
+ result : Expr
+ A relax Call, which will print the value during runtime.
+ """
+ return _ffi_api.print(values, format) # type: ignore # pylint: disable=no-member
+
+
+@tvm.register_func("relax.run.assert_op")
+def relax_assert_op(condition: tvm.Object, format_str: str, *format_args: tvm.Object) -> None:
+ """
+ A variadic function. The first value serves as the assertion condition:
+ If the condition is true, then the operator does nothing.
+ If the condition is false, then the operator raises an assertion error.
+
+ Arguments after the first value serve as format arguments for the error message;
+ the last argument must be a format string for the error message (empty by default).
+ If the format string is the empty string, then the error message will simply include
+ a comma-separated list of the format arguments.
+ The condition argument is not included in the format string.
+
+ Parameters
+ ----------
+ condition: tvm.Object
+ The assertion condition. Must be a boolean scalar.
+
+ format_str: str
+ The last argument is a Python-style format string for printing the value
+
+ format_args: List[tvm.Object]
+ Values used for formatting the string.
+ """
+ if not isinstance(format_str, str):
+ raise ValueError(
+ f"The format string argument to assert must be a string, given {type(format_str)})"
+ )
+
+ # should be guaranteed by the type system
+ if not isinstance(condition, tvm.runtime.ndarray.NDArray):
+ raise ValueError(f"The condition must be an NDArray, but given a {type(condition)}.")
+
+ # may happen if the original program had unknown shape or dtype for the tensor's type
+ dtype = condition.dtype
+ if dtype != "bool":
+ raise ValueError(f"The condition must be a bool scalar, but given a {dtype} tensor")
+ shape = condition.shape
+ if len(shape) != 0:
+ raise ValueError(f"The condition must be a scalar, but it has a shape of {shape}")
+
+ val = condition.numpy()
+ if not val:
+ error_message = "Assertion Failed"
+ if format_args or format_str != "":
+ rendered = map(render_object, format_args)
+ if format_str != "":
+ error_message = format_str.format(*rendered)
+ else:
+ error_message = ", ".join(rendered)
+ raise AssertionError(error_message)
+
+
+def assert_op(
+ condition: Expr, format_args: Optional[Union[Expr, List[Expr]]] = None, format: str = ""
+) -> Expr:
+ """
+ Create a call to Relax's assert_op operation (`assert` is reserved in Python,
+ so the name must be distinct).
+
+ Parameters
+ ----------
+ condition: Expr
+ The assertion condition.
+
+ format_args: Optional[Union[Expr, List[Expr]]]
+ Format arguments for the error message if the condition fails.
+
+ format_str: str
+ The format string for the error message.
+
+ Returns
+ -------
+ result : Expr
+ A Call to the Relax assert operation.
+ """
+ if format_args is None:
+ format_args = []
+ if isinstance(format_args, Expr): # type: ignore
+ format_args = [format_args]
+ return _ffi_api.assert_op(condition, format_args, format) # type: ignore
+
+
+def shape_of(expr: Expr) -> Expr:
+ """Get shape of a tensor.
+
+ Parameters
+ ----------
+ expr : Expr
+ The input Expr.
+
+ Returns
+ -------
+ result : Expr
+ A relax Call, which gets the shape of the input
+ """
+ return _ffi_api.shape_of(expr) # type: ignore # pylint: disable=no-member
diff --git a/python/tvm/relax/op/binary.py b/python/tvm/relax/op/binary.py
new file mode 100644
index 0000000000..eee0b6f336
--- /dev/null
+++ b/python/tvm/relax/op/binary.py
@@ -0,0 +1,67 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=redefined-builtin, invalid-name
+"""Relax binary arithmetic and comparison operators."""
+from . import _ffi_api
+from ..expr import Expr
+
+###################### Arithmetic operators ######################
+
+
+def add(x1: Expr, x2: Expr) -> Expr:
+ """Addition with numpy-style broadcasting.
+
+ Parameters
+ ----------
+ x1 : Expr
+ The first input tensor.
+ x2 : Expr
+ The second input tensor.
+
+ Returns
+ -------
+ result : Expr
+ The computed result.
+
+ Examples
+ --------
+ .. code:: python
+
+ bb = relax.BlockBuilder()
+ a = relax.Var("a", relax.TensorStructInfo(shape=(2, 3), dtype="float32"))
+ b = relax.Var("b", relax.TensorStructInfo(shape=(2, 1), dtype="float32"))
+ c = bb.normalize(relax.op.add(a, b)) # c has TensorStructInfo(shape=(2, 3), dtype="float32")
+ """
+ return _ffi_api.add(x1, x2) # type: ignore
+
+
+def multiply(x1: Expr, x2: Expr) -> Expr:
+ """Multiplication with numpy-style broadcasting.
+
+ Parameters
+ ----------
+ x1 : Expr
+ The first input tensor.
+ x2 : Expr
+ The second input tensor.
+
+ Returns
+ -------
+ result : Expr
+ The computed result.
+ """
+ return _ffi_api.multiply(x1, x2) # type: ignore
diff --git a/python/tvm/relax/utils.py b/python/tvm/relax/utils.py
new file mode 100644
index 0000000000..5bfb0d87bf
--- /dev/null
+++ b/python/tvm/relax/utils.py
@@ -0,0 +1,278 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Utility functions for Relax"""
+import functools
+import inspect
+from typing import Any, Callable, List, Optional, TypeVar
+
+from .. import tir
+from ..runtime import String, convert_to_object
+from ..tir import PrimExpr
+from . import _ffi_api
+from .expr import Expr, Function, PrimValue, ShapeExpr, StringImm
+from .expr import Tuple as rx_Tuple
+
+
+def metadata_partitioner(rx_txt: str) -> List[str]:
+ """Extract Relax program and metadata section.
+
+ Parameters
+ ----------
+ rx_txt : str
+ The input relax text.
+
+ Returns
+ -------
+ output : List[str]
+ The result list of partitioned text, the first element
+ is the relax program, and the second is metadata section.
+ """
+ partitions = []
+ left_curly = 0
+ meta_start = 0
+ meta_end = 0
+ for i, char in enumerate(rx_txt):
+ if i < 0:
+ raise ValueError("The program is invalid.")
+ if char == "{":
+ if meta_start == 0:
+ meta_start = i
+ left_curly += 1
+ elif char == "}":
+ left_curly -= 1
+ if left_curly == 0:
+ meta_end = i + 1
+ break
+
+ if meta_end == 0:
+ raise ValueError("The metadata section was not found.")
+ metadata = rx_txt[meta_start:meta_end]
+ rx_program = rx_txt[meta_end:-1]
+
+ partitions.append(rx_program)
+ partitions.append(metadata)
+
+ return partitions
+
+
+def convert_to_expr(value: Any) -> Expr:
+ """Helper function to convert the input to Expr, which follows the rules:
+ 1. Return the input itself if it's already a `relax.Expr`;
+ 2. Return `relax.PrimValue` if the input is a `PrimExpr`;
+ 3. Return `relax.StringImm` if the input is `tvm.String` or `str`;
+ 4. Return `relax.ShapeExpr` if the input is a tuple/list of `PrimExpr` w/ int dtype;
+ 5. Return `relax.Tuple` if the input is a tuple/list of `Expr`.
+
+ Notes
+ -----
+ 1. `tvm.tir.StringImm` is not allowed because of ambiguity,
+ which can be either `relax.StringImm` or `relax.PrimValue`.
+ 2. We regard empty tuple/list as `relax.Tuple` instead of `relax.ShapeExpr`
+ """
+ if isinstance(value, int):
+ return PrimValue(tir.IntImm("int64", value))
+
+ tvm_value = convert_to_object(value)
+ # Case 1
+ if isinstance(tvm_value, Expr): # type: ignore
+ return tvm_value
+ # Note`` 1
+ if isinstance(tvm_value, tir.StringImm):
+ raise TypeError(
+ "Cannot convert `tir.StringImm` to `relax.Expr` because of ambiguity,"
+ "which can be either `relax.StringImm` or `relax.PrimValue` "
+ )
+ # Case 2
+ if isinstance(tvm_value, PrimExpr):
+ return PrimValue(value)
+ # Case 3
+ if isinstance(tvm_value, String):
+ return StringImm(value)
+ # Case 4 & 5
+ if isinstance(value, (tuple, list)):
+ # Note 2
+ if len(value) == 0:
+ return rx_Tuple([])
+ # Case 4
+ opt_prim_value = [convert_to_object(v) for v in value]
+ if all([isinstance(v, PrimExpr) and v.dtype.startswith("int") for v in opt_prim_value]):
+ return ShapeExpr(value)
+ # Case 5
+ # `convert_to_expr` ensures that all elements are `Expr` if no exception raises
+ return rx_Tuple([convert_to_expr(v) for v in value])
+ raise TypeError(f"Cannot convert {value} with type {type(value)} to `relax.Expr`")
+
+
+FType = TypeVar("FType", bound=Callable[..., Expr])
+
+
+class _ArgsConverter:
+ """A helper class to convert the arguments to Expr."""
+
+ @staticmethod
+ def convert(args_to_expr: List[str], args_to_list_expr: List[str]):
+ """Convert the arguments to Expr.
+
+ Parameters
+ ----------
+ args_to_expr : List[str]
+ The argument names to be converted to Expr.
+
+ args_to_list_expr : List[str]
+ The argument names to be converted to List[Expr].
+
+ Returns
+ -------
+ output : Callable[[FType], FType]
+ The decorator.
+ """
+
+ if any([x in args_to_list_expr for x in args_to_expr]):
+ raise ValueError(f"`args_to_expr` and `args_to_list_expr` should be disjoint.")
+
+ def _convert(name: str, value: Any) -> Any:
+ if value is None:
+ return value
+ if name in args_to_expr:
+ try:
+ return convert_to_expr(value)
+ except:
+ raise TypeError(
+ f"Argument `{name}` is expected to be converted to `Expr`, "
+ f"but failed with input value: {value}"
+ )
+ elif name in args_to_list_expr:
+ try:
+ return [convert_to_expr(x) for x in value]
+ except:
+ raise TypeError(
+ f"Argument `{name}` is expected to be converted to `List[Expr]`, "
+ f"but failed with input value: {value}"
+ )
+ else:
+ return value
+
+ def inner(func: FType) -> FType:
+ sig = inspect.signature(func)
+ param_names = list(sig.parameters.keys())
+ for name in args_to_expr + args_to_list_expr:
+ if name not in param_names:
+ raise ValueError(f"Argument `{name}` is not found in function signature.")
+
+ @functools.wraps(func)
+ def wrapper(*args, **kwargs):
+ bound = sig.bind(*args, **kwargs)
+ bound.apply_defaults()
+ for param in sig.parameters.values():
+ if param.kind == param.VAR_POSITIONAL:
+ # *args case
+ values = [_convert(param.name, x) for x in bound.arguments[param.name]]
+ bound.arguments[param.name] = tuple(values)
+ elif param.kind == param.VAR_KEYWORD:
+ # **kwargs case
+ key_value = {
+ key: _convert(param.name, value)
+ for key, value in bound.arguments[param.name].items()
+ }
+ bound.arguments[param.name] = key_value
+ else:
+ bound.arguments[param.name] = _convert(
+ param.name, bound.arguments[param.name]
+ )
+ return func(*bound.args, **bound.kwargs)
+
+ return wrapper # type: ignore
+
+ return inner
+
+ @staticmethod
+ def to_expr(*arg_names: str) -> Callable:
+ """Convert the arguments to Expr.
+
+ Parameters
+ ----------
+ *arg_names: str
+ The list of argument names that need to be converted to Expr.
+
+ Returns
+ -------
+ output: Callable
+ The decorator.
+ """
+
+ return _ArgsConverter.convert(args_to_expr=list(arg_names), args_to_list_expr=[])
+
+ @staticmethod
+ def to_list_expr(*arg_names: str) -> Callable:
+ """Convert the arguments to List of Expr.
+
+ Parameters
+ ----------
+ *arg_names: str
+ The list of argument names that need to be converted to List of Expr.
+
+ Returns
+ -------
+ output: Callable
+ The decorator.
+ """
+
+ return _ArgsConverter.convert(args_to_expr=[], args_to_list_expr=list(arg_names))
+
+ @staticmethod
+ def auto(func: FType) -> FType:
+ """Decorator for automatically convert the arguments to Expr according to type annotation.
+ Only two patterns are supported:
+
+ 1. The argument is Expr or Optional[Expr].
+
+ 2. The argument is List[Expr] or Optional[List[Expr]].
+
+ """
+ sig = inspect.signature(func)
+ args_to_expr = []
+ args_to_list_expr = []
+
+ for param in sig.parameters.values():
+ anno = param.annotation
+ if anno in (Expr, Optional[Expr]):
+ args_to_expr.append(param.name)
+ if anno in (List[Expr], Optional[List[Expr]]):
+ args_to_list_expr.append(param.name)
+
+ return _ArgsConverter.convert(args_to_expr, args_to_list_expr)(func)
+
+
+args_converter = _ArgsConverter() # pylint: disable=invalid-name
+
+
+def copy_with_new_params(func: Function) -> Function:
+ """Copy the given function. The parameters of the original function would be copied to
+ satisfy the restriction in the well-formed check: any two functions cannot share the same
+ parameter variable.
+
+ Parameters
+ ----------
+ func : Function
+ The relax function to copy.
+
+ Returns
+ -------
+ ret : Function
+ The copied function.
+ """
+ return _ffi_api.CopyWithNewParams(func) # type: ignore
diff --git a/python/tvm/te/__init__.py b/python/tvm/te/__init__.py
index 0907ea2ebf..40fac0f92f 100644
--- a/python/tvm/te/__init__.py
+++ b/python/tvm/te/__init__.py
@@ -41,6 +41,7 @@ from .tag import tag_scope
from .operation import placeholder, compute, scan, extern, var, size_var, const
from .operation import thread_axis, reduce_axis
from .operation import create_prim_func
+from .operation import create_relax_prim_func
from .operation import extern_primfunc
from .tensor import PlaceholderOp, ComputeOp, TensorComputeOp, ScanOp, ExternOp, HybridOp
diff --git a/python/tvm/te/operation.py b/python/tvm/te/operation.py
index 59bc76f504..cfe5e073ba 100644
--- a/python/tvm/te/operation.py
+++ b/python/tvm/te/operation.py
@@ -571,12 +571,64 @@ def create_prim_func(
ops: List[_tensor.Tensor], index_dtype_override: Optional[str] = None
) -> tvm.tir.PrimFunc:
"""Create a TensorIR PrimFunc from tensor expression
+ Parameters
+ ----------
+ ops : List[Tensor]
+ The source expression.
+ Example
+ -------
+ We define a matmul kernel using following code:
+ .. code-block:: python
+ import tvm
+ from tvm import te
+ from tvm.te import create_prim_func
+ import tvm.script
+ A = te.placeholder((128, 128), name="A")
+ B = te.placeholder((128, 128), name="B")
+ k = te.reduce_axis((0, 128), "k")
+ C = te.compute((128, 128), lambda x, y: te.sum(A[x, k] * B[y, k], axis=k), name="C")
+ func = create_prim_func([A, B, C])
+ print(func.script())
+ If we want to use TensorIR schedule to do transformations on such kernel,
+ we need to use `create_prim_func([A, B, C])` to create a schedulable PrimFunc.
+ The generated function looks like:
+ .. code-block:: python
+ @T.prim_func
+ def tir_matmul(a: T.handle, b: T.handle, c: T.handle) -> None:
+ A = T.match_buffer(a, (128, 128))
+ B = T.match_buffer(b, (128, 128))
+ C = T.match_buffer(c, (128, 128))
+ for i, j, k in T.grip(128, 128, 128):
+ with T.block():
+ vi, vj, vk = T.axis.remap("SSR", [i, j, k])
+ with T.init():
+ C[vi, vj] = 0.0
+ C[vi, vj] += A[vi, vk] * B[vj, vk]
+ Returns
+ -------
+ func : tir.PrimFunc
+ The created function.
+ """
+ if not isinstance(ops, (list, tuple, Array)):
+ ops = [ops]
+ return _ffi_api.CreatePrimFunc(ops, index_dtype_override)
+
+
+def create_relax_prim_func(
+ ops: List[_tensor.Tensor],
+ tir_var_list: List[tvm.tir.Var] = None,
+ index_dtype_override: Optional[str] = None,
+) -> tvm.tir.PrimFunc:
+ """Create a TensorIR PrimFunc from tensor expression
Parameters
----------
ops : List[Tensor]
The source expression.
+ tir_var_list: List[Var]
+ TIR variables to add as parameters to generated PrimFunc
+
Example
-------
We define a matmul kernel using following code:
@@ -621,4 +673,4 @@ def create_prim_func(
"""
if not isinstance(ops, (list, tuple, Array)):
ops = [ops]
- return _ffi_api.CreatePrimFunc(ops, index_dtype_override)
+ return _ffi_api.CreateRelaxPrimFunc(ops, tir_var_list, index_dtype_override)
diff --git a/src/ir/function.cc b/src/ir/function.cc
index 69752f529a..6a7ccc7cf2 100644
--- a/src/ir/function.cc
+++ b/src/ir/function.cc
@@ -46,4 +46,18 @@ TVM_REGISTER_GLOBAL("ir.BaseFuncWithAttr")
}
});
+TVM_REGISTER_GLOBAL("ir.BaseFuncWithoutAttr")
+ .set_body_typed([](BaseFunc func, String key) -> BaseFunc {
+ if (func->IsInstance<tir::PrimFuncNode>()) {
+ return WithoutAttr(Downcast<tir::PrimFunc>(std::move(func)), key);
+ } else if (func->IsInstance<relay::FunctionNode>()) {
+ return WithoutAttr(Downcast<relay::Function>(std::move(func)), key);
+ } else if (func->IsInstance<relax::FunctionNode>()) {
+ return WithoutAttr(Downcast<relax::Function>(std::move(func)), key);
+ } else {
+ LOG(FATAL) << "Do not support function type " << func->GetTypeKey();
+ return func;
+ }
+ });
+
} // namespace tvm
diff --git a/src/relax/analysis/struct_info_analysis.cc b/src/relax/analysis/struct_info_analysis.cc
index d9b1397534..2de06fe5d6 100644
--- a/src/relax/analysis/struct_info_analysis.cc
+++ b/src/relax/analysis/struct_info_analysis.cc
@@ -533,6 +533,155 @@ TVM_REGISTER_GLOBAL("relax.StructInfoIsBaseOf")
return IsBaseOf(base, derived);
});
+//--------------------------
+// DeriveStructInfo
+//--------------------------
+
+// NOTE: we are reusing StructInfoBaseChecker here to populate a mapping
+// from the expressions in arg(rhs) to var in param.
+class CallRetStructInfoDeriver : public StructInfoBaseChecker {
+ public:
+ explicit CallRetStructInfoDeriver(arith::Analyzer* ana) : StructInfoBaseChecker(ana) {}
+
+ // No short cut, so we can recursively populate all pairs.
+ BaseCheckResult VisitStructInfo(const StructInfo& lhs, const StructInfo& other) final {
+ return StructInfoFunctor::VisitStructInfo(lhs, other);
+ }
+
+ StructInfo Derive(const FuncStructInfo& finfo, const Call& call, const BlockBuilder& ctx) {
+ // opaque derivation
+ if (finfo->IsOpaque()) {
+ if (finfo->derive_func.defined()) {
+ // derive using custom derivation function.
+ return finfo->derive_func.value()(call, ctx);
+ } else {
+ // directly return the normal value.
+ return finfo->ret;
+ }
+ }
+
+ // Normal function signature derivation.
+ auto params = finfo->params.value();
+ if (params.size() != call->args.size()) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "number of arguments and parameters mismatch:"
+ << " expected " << params.size() << ", given " << call->args.size());
+ }
+ // Visit each param arg pair, check and populate the var map
+ for (size_t i = 0; i < params.size(); ++i) {
+ auto arg_sinfo = GetStructInfo(call->args[i]);
+ BaseCheckResult res = this->VisitStructInfo(params[i], arg_sinfo);
+ // Report error if we find L1 level failure
+ // L2 level is best effort so we don't report.
+ // The behavior of L2 can be customized later.
+ if (res == BaseCheckResult::kFailL0 || res == BaseCheckResult::kFailL1) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "Argument " << i << " type mismatch:"
+ << " expected " << params[i] << ", given " << arg_sinfo);
+ }
+ }
+ // map the ret using the populated var map.
+ return EraseToWellDefined(finfo->ret, shape_var_map_, var_map_);
+ }
+
+ protected:
+ // Whether to populate map in params.
+ bool populate_mapping_{true};
+ // for simplicity, we make these fields public so the user can access them.
+ Map<tir::Var, PrimExpr> shape_var_map_;
+ Map<Var, Expr> var_map_;
+
+ using StructInfoBaseChecker::ShapeMatchCheck;
+
+ // Match shape values in between param(lhs) and arg(rhs)
+ BaseCheckResult PrimValueMatchCheck(const PrimExpr& param, const PrimExpr& arg) final {
+ if (!populate_mapping_) {
+ return StructInfoBaseChecker::PrimValueMatchCheck(param, arg);
+ }
+
+ if (auto* ptr = param.as<tir::VarNode>()) {
+ auto var = GetRef<tir::Var>(ptr);
+ auto it = shape_var_map_.find(var);
+ // not populated
+ if (it == shape_var_map_.end()) {
+ shape_var_map_.Set(var, arg);
+ return BaseCheckResult::kPass;
+ } else {
+ // Best effort prove.
+ PrimExpr mapped_value = (*it).second;
+ if (analyzer_->CanProveEqual(mapped_value, arg)) return BaseCheckResult::kPass;
+ return BaseCheckResult::kFailL2;
+ }
+ } else {
+ // Best effort
+ // Do not attempt to do prove when param contains a symbolic expr.
+ // such expression might depends on a later defined var in params created by dyn fusion.
+ // example: f(a: Tensor[(n+1)], s: Shape[(n,)]), the (n+1) case here.
+ return StructInfoBaseChecker::PrimValueMatchCheck(param, arg);
+ }
+ }
+
+ BaseCheckResult ShapeMatchCheck(const Expr& lhs, const Expr& rhs) final {
+ if (!populate_mapping_) {
+ return StructInfoBaseChecker::ShapeMatchCheck(lhs, rhs);
+ }
+
+ if (auto* ptr = lhs.as<VarNode>()) {
+ auto var = GetRef<Var>(ptr);
+ auto it = var_map_.find(var);
+ // not populated
+ if (it == var_map_.end()) {
+ var_map_.Set(var, rhs);
+ return BaseCheckResult::kPass;
+ } else {
+ // Best effort prove.
+ Expr mapped_value = (*it).second;
+ if (CanProveShapeEqual(mapped_value, rhs, analyzer_)) return BaseCheckResult::kPass;
+ return BaseCheckResult::kFailL2;
+ }
+ }
+ auto lhs_shape = lhs.as<ShapeExprNode>();
+ auto rhs_shape = rhs.as<ShapeExprNode>();
+ ICHECK(lhs_shape) << "lhs must have a shape";
+ if (!rhs_shape) return BaseCheckResult::kFailL2;
+ return ShapeMatchCheck(lhs_shape->values, rhs_shape->values);
+ }
+
+ BaseCheckResult FuncParamsCheck(const Array<StructInfo>& lhs,
+ const Array<StructInfo>& rhs) final {
+ // Set populate mapping to false
+ // so we do not pick up symbolic vars in params with function type.
+ //
+ // @R.function
+ // def f(g: R.Func([R.Tensor[(n,)]], R.Tensor[(n+1,)]),
+ // x: R.Tensor[(m,)]) -> R.Tensor[(m,)]:
+ // ...
+ //
+ // For example, in the above function f, we should avoid
+ // pick up n in g's signature.
+ bool populate_mapping = false;
+ std::swap(populate_mapping_, populate_mapping);
+ auto ret = StructInfoBaseChecker::FuncParamsCheck(lhs, rhs);
+ std::swap(populate_mapping_, populate_mapping);
+ return ret;
+ }
+};
+
+StructInfo DeriveCallRetStructInfo(const FuncStructInfo& finfo, const Call& call,
+ const BlockBuilder& ctx, arith::Analyzer* ana) {
+ if (ana == nullptr) {
+ arith::Analyzer inst;
+ return CallRetStructInfoDeriver(&inst).Derive(finfo, call, ctx);
+ } else {
+ return CallRetStructInfoDeriver(ana).Derive(finfo, call, ctx);
+ }
+}
+
+TVM_REGISTER_GLOBAL("relax.analysis.DeriveCallRetStructInfo")
+ .set_body_typed([](const FuncStructInfo& finfo, const Call& call, const BlockBuilder& ctx) {
+ return DeriveCallRetStructInfo(finfo, call, ctx);
+ });
+
//--------------------------
// UnifyToLCA
//--------------------------
diff --git a/src/relax/ir/block_builder.cc b/src/relax/ir/block_builder.cc
new file mode 100644
index 0000000000..6a2d7ea5c5
--- /dev/null
+++ b/src/relax/ir/block_builder.cc
@@ -0,0 +1,969 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/block_builder.cc
+ */
+#include <tvm/arith/analyzer.h>
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/block_builder.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/op_attr_types.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/relax/struct_info_functor.h>
+#include <tvm/relax/type.h>
+#include <tvm/relax/utils.h>
+#include <tvm/relay/op.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/tir/function.h>
+
+#include <memory>
+#include <unordered_map>
+#include <vector>
+
+// Block builder have three categories of logics that are interdependent with each other.
+//
+// The logics are somewhat interdependent with each other.
+// To help us implement a block builder in two parts:
+//
+// - BlockBuilderImpl: implements ctx and scope management, with no normalization.
+// - BlockBuilderImplWithNormalize: subclasses BlockBuilderImpl and implements normalization.
+//
+// The final blockbuilder create will be backed by BlockBuilderWithNormalize
+
+namespace tvm {
+namespace relax {
+
+//---------------------------------------
+// ctx and scope management.
+//---------------------------------------
+class BlockBuilderImpl : public BlockBuilderNode {
+ public:
+ explicit BlockBuilderImpl(IRModule context_mod) : context_mod_(std::move(context_mod)) {}
+
+ ~BlockBuilderImpl() {
+ if (!block_stack_.empty()) {
+ LOG(WARNING) << "BlockBuilder destroyed with remaining blocks!";
+ }
+ }
+
+ //-------------------------------
+ // Global Context management
+ //-------------------------------
+ NameTable* name_table() final { return name_table_.get(); }
+
+ IRModule GetContextIRModule() const final { return context_mod_; }
+
+ GlobalVar AddFunction(const BaseFunc& func, String func_name_hint) final {
+ LazyInitCtxFuncDedupMap();
+ auto it = ctx_func_dedup_map_->find(func);
+ if (it == ctx_func_dedup_map_->end()) {
+ context_mod_.CopyOnWrite();
+
+ String func_name = name_table_->GetUniqueName(func_name_hint);
+ while (context_mod_->ContainGlobalVar(func_name)) {
+ func_name = name_table_->GetUniqueName(func_name_hint);
+ }
+ GlobalVar gvar = GlobalVar(func_name);
+
+ StructInfo finfo;
+ if (func->struct_info_.defined()) {
+ finfo = GetStructInfo(func);
+ } else if (auto* prim_func = func.as<tir::PrimFuncNode>()) {
+ // NOTE: use a slightly different struct info than checked type
+ // in PrimFunc so handle can turn into Tensor.
+ // TODO(relax-team): add fine-grained PrimFunc struct info signature generation.
+ finfo = FuncStructInfo::OpaqueFunc(StructInfoFromType(prim_func->ret_type));
+ } else {
+ finfo = StructInfoFromType(func->checked_type());
+ }
+ UpdateStructInfo(gvar, finfo);
+
+ context_mod_->Add(gvar, func);
+
+ ctx_func_dedup_map_->emplace(func, gvar);
+ return gvar;
+ } else {
+ return it->second;
+ }
+ }
+
+ void UpdateFunction(const GlobalVar& gv, BaseFunc function) final {
+ context_mod_.CopyOnWrite();
+
+ // invalidate old dedup map
+ if (ctx_func_dedup_map_ != nullptr) {
+ auto it = context_mod_->functions.find(gv);
+ if (it != context_mod_->functions.end()) {
+ BaseFunc old_func = (*it).second;
+ auto ptr = ctx_func_dedup_map_->find(old_func);
+ ICHECK(ptr != ctx_func_dedup_map_->end());
+ ctx_func_dedup_map_->erase(ptr);
+ }
+ }
+
+ context_mod_->Update(gv, function);
+
+ // add new dedup map item.
+ if (ctx_func_dedup_map_ != nullptr) {
+ ctx_func_dedup_map_->emplace(function, gv);
+ }
+ }
+
+ void ReportFatal(const Diagnostic& diagnostic) final {
+ // TODO(relax-team): Print more context information by looking
+ // into the diagnostic->loc and surrounding IRModule.
+ // We do not materialzie DiagnosticContext to avoid double referencing to
+ // the change IRModule in COW. Additionally, we need to be able to
+ // continue use the builder after an error is thrown to avoid state building up.
+ // in an interactive environment.
+ LOG(FATAL) << diagnostic->message;
+ }
+
+ //-------------------------------
+ // Scope management
+ //-------------------------------
+ Optional<Expr> LookupBinding(const Var& var) final {
+ auto it = binding_table_.find(var->vid);
+ if (it == binding_table_.end()) return NullOpt;
+ return it->second;
+ }
+
+ void BeginDataflowBlock() final { block_stack_.emplace_back(BlockFrame{{}, true}); }
+
+ void BeginBindingBlock() final { block_stack_.emplace_back(BlockFrame{{}, false}); }
+
+ void BeginScope(Optional<Array<Var>> params) final {
+ // The current implementation handles the collection of shape var
+ // defined in parameter struct info annotations. The implementation
+ // is correct (since we will simply erase all relax Vars in EraseToWellDefined),
+ // but can be further improved.
+ //
+ // TODO(relax-team): Add support for relax Var in struct info annotations.
+ Map<tir::Var, PrimExpr> shape_var_map;
+ for (const Var& var : params.value_or(Array<Var>())) {
+ const Map<tir::Var, PrimExpr>& var_map = StructInfoVarCollector::Collect(GetStructInfo(var));
+ for (const auto& kv : var_map) {
+ const tir::Var& shape_var = kv.first;
+ const PrimExpr& shape_expr = kv.second;
+ auto it = shape_var_map.find(shape_var);
+ if (it == shape_var_map.end()) {
+ shape_var_map.Set(shape_var, shape_expr);
+ } else {
+ const PrimExpr& old_shape_expr = (*it).second;
+ CHECK(analyzer_.CanProveEqual(old_shape_expr, shape_expr))
+ << "Inconsistent shape var " << shape_var << " in scope: " << old_shape_expr << " vs "
+ << shape_expr;
+ }
+ shape_var_map.Set(shape_var, shape_expr);
+ }
+ }
+ scope_stack_.emplace_back(ScopeFrame({std::move(shape_var_map)}));
+ }
+
+ void EndScope() final { scope_stack_.pop_back(); }
+
+ BindingBlock EndBlock() final {
+ BlockFrame* cur_frame = CurrentBlockFrame();
+ BindingBlock ret = cur_frame->is_dataflow ? DataflowBlock(cur_frame->bindings)
+ : BindingBlock(cur_frame->bindings);
+ block_stack_.pop_back();
+ return ret;
+ }
+
+ bool CurrentBlockIsDataFlow() final { return CurrentBlockFrame()->is_dataflow; }
+
+ Var Emit(Expr expr, String name_hint) final {
+ return this->Emit(expr, CurrentBlockFrame()->is_dataflow, name_hint);
+ }
+
+ Var EmitMatchCast(Expr value, StructInfo struct_info, String name_hint) final {
+ value = this->Normalize(value);
+
+ CHECK(StructInfoBaseCheck(GetStructInfo(value), struct_info) != BaseCheckResult::kFailL0)
+ << "It is impossible to match cast any value into the target struct_info. "
+ "But got value struct info: "
+ << GetStructInfo(value) << ", given struct info: " << struct_info;
+
+ // NOTE: do match cast checking later in a pass.
+ BlockFrame* cur_frame = CurrentBlockFrame();
+ Var var = CreateVar(cur_frame->is_dataflow, name_hint);
+ UpdateStructInfo(var, struct_info);
+
+ MatchCast match_cast(var, value, struct_info);
+ cur_frame->bindings.push_back(match_cast);
+ // NOTE match shape do not follow simple binding rule
+ // as a result should not appear in binding table.
+ return var;
+ }
+
+ Var EmitOutput(Expr output, String name_hint) final {
+ BlockFrame* cur_frame = CurrentBlockFrame();
+
+ ICHECK(cur_frame->is_dataflow) << "EmitOutput has to be called inside dataflow block.";
+
+ return Emit(output, false, name_hint);
+ }
+
+ void EmitNormalized(Binding binding) final {
+ BlockFrame* cur_frame = CurrentBlockFrame();
+
+ if (const auto* var_binding = binding.as<VarBindingNode>()) {
+ if (!cur_frame->is_dataflow) {
+ ICHECK(!var_binding->var.as<DataflowVarNode>())
+ << "Cannot emit dataflow var in non-dataflow block";
+ }
+ // normalized check
+ ICHECK(var_binding->var->struct_info_.defined());
+ ICHECK(var_binding->value->struct_info_.defined());
+ cur_frame->bindings.push_back(binding);
+ binding_table_[var_binding->var->vid] = var_binding->value;
+ } else if (const auto* match_cast = binding.as<MatchCastNode>()) {
+ if (!cur_frame->is_dataflow) {
+ ICHECK(!match_cast->var.as<DataflowVarNode>())
+ << "Cannot emit dataflow var in non-dataflow block";
+ }
+ // normalized check
+ ICHECK(match_cast->var->struct_info_.defined());
+ ICHECK(match_cast->value->struct_info_.defined());
+ // NOTE match shape do not follow simple binding rule
+ // as a result should not appear in binding table.
+ cur_frame->bindings.push_back(binding);
+ } else {
+ LOG(FATAL) << "Unsupported binding type: " << binding->GetTypeKey();
+ }
+ }
+
+ arith::Analyzer* GetAnalyzer() final { return &analyzer_; }
+
+ protected:
+ /*!
+ * \brief A representation of a block frame.
+ *
+ * A block frame is a record containing the bindings needed
+ * to build a binding block, and a boolean to indicate if the
+ * block being built is a DataflowBlock or not.
+ */
+ struct BlockFrame {
+ /*!
+ * \brief List of bindings
+ */
+ Array<Binding> bindings;
+ /*! \brief Whether current block is dataflow block. */
+ bool is_dataflow;
+ /*!
+ * \brief Binding map used by normalizer.
+ *
+ * \note The normalizer only caches reuse in the current block scope
+ * and will not cache bindings from parent scope.
+ */
+ std::unordered_map<Expr, Var, ObjectPtrHash, ObjectPtrEqual> normalize_binding_map;
+ };
+ /*!
+ * \brief A representation of a scope frame.
+ *
+ * A scope frame records tracks the context of current scope.
+ */
+ struct ScopeFrame {
+ // NOTE: for simplicity, only tracks symbolic var for now
+ // the scope is only used for erasure, so less information means
+ // more conservative analysis.
+ // Consider impl alternative: merge with block frame if we have more frame kinds.
+ //
+ // TODO(relax-team) tracks the var defined also through match-cast.
+ /*! \brief set of defined symbolic vars, value as themself. */
+ Map<tir::Var, PrimExpr> shape_var_map;
+ };
+
+ /*! \brief A stack to store block frames. */
+ std::vector<BlockFrame> block_stack_;
+
+ /*! \brief A stack to store scope frames. */
+ std::vector<ScopeFrame> scope_stack_;
+
+ /*! \brief A binding table that maps var to value. */
+ std::unordered_map<Id, Expr, ObjectPtrHash, ObjectPtrEqual> binding_table_;
+
+ /*! \brief A name table to get unique names for IR construction. */
+ std::unique_ptr<NameTable> name_table_ = std::make_unique<NameTable>();
+
+ /*! \brief The IRModule being built by the BlockBuilder. */
+ IRModule context_mod_;
+
+ /*! \brief Internal analzyer */
+ arith::Analyzer analyzer_;
+
+ /*!
+ * \return The current frame.
+ * \note Never hold the value of current frame between Normalize
+ * or other scope calls this value can change if the block stack get updated,
+ * then the block frame is no longer valid.
+ */
+ BlockFrame* CurrentBlockFrame() {
+ ICHECK(!block_stack_.empty()) << "no block is being built";
+ return &block_stack_.back();
+ }
+
+ /*!
+ * \return The current scope frame.
+ * \note only use this value
+ */
+ ScopeFrame* CurrentScopeFrame() {
+ ICHECK(!scope_stack_.empty()) << "no scope is being opened";
+ return &scope_stack_.back();
+ }
+
+ /*!
+ * \brief Emits an Expr, and returns the variable it is bound to.
+ * \param expr The Expr to be emitted.
+ * \param is_dataflow Is the bound variable a DataflowVar or not(i.e. Var).
+ * \param name_hint Name hint for the bound variable.
+ * \note This Emit function normalizes the \p expr,
+ * and performs shape/type deductions by calling Normalize.
+ * \return The new variable that \p expr is bound to.
+ */
+ Var Emit(Expr expr, bool is_dataflow, String name_hint) {
+ expr = this->Normalize(expr);
+
+ Var var = CreateVar(is_dataflow, name_hint);
+
+ // set the values
+ UpdateStructInfo(var, Downcast<StructInfo>(expr->struct_info_.value()));
+
+ CurrentBlockFrame()->bindings.push_back(VarBinding(var, expr));
+
+ // update the binding table
+ binding_table_[var->vid] = expr;
+
+ return var;
+ }
+
+ /*!
+ * \brief Create var for bindings
+ * \param is_dataflow Is the bound variable a DataflowVar or not(i.e. Var).
+ * \param name_hint Name hint for the bound variable.
+ * \return The created var.
+ */
+ Var CreateVar(bool is_dataflow, String name_hint) {
+ if (name_hint.empty()) {
+ name_hint = is_dataflow ? "lv" : "gv";
+ }
+ Id vid = Id(name_table_->GetUniqueName(name_hint));
+ return is_dataflow ? DataflowVar(vid, /*struct_info_annotation=*/NullOpt)
+ : Var(vid, /*struct_info_annotation=*/NullOpt);
+ }
+
+ private:
+ /*!
+ * \brief A hashmap to store the mapping of Relax functions and TIR PrimFuncs
+ * in context_mod to their GlobalVar to avoid generating duplicated functions.
+ */
+ std::unique_ptr<std::unordered_map<BaseFunc, GlobalVar, StructuralHash, StructuralEqual>>
+ ctx_func_dedup_map_ = nullptr;
+
+ /*!
+ * \brief lazily initialize function dedeup map.
+ */
+ void LazyInitCtxFuncDedupMap() {
+ if (ctx_func_dedup_map_ != nullptr) return;
+ ctx_func_dedup_map_ = std::make_unique<
+ std::unordered_map<BaseFunc, GlobalVar, StructuralHash, StructuralEqual>>();
+ for (const auto& kv : context_mod_->functions) {
+ const GlobalVar gv = kv.first;
+ const BaseFunc func = kv.second;
+ ctx_func_dedup_map_->emplace(func, gv);
+ }
+ }
+
+ // Collect all the variables that a parameter var can define.
+ // The collector is used to making sure that we record the
+ // shape vars as defined when calling BeginScope(params)
+ class StructInfoVarCollector : public StructInfoVisitor {
+ public:
+ static Map<tir::Var, PrimExpr> Collect(const StructInfo& struct_info) {
+ StructInfoVarCollector collector;
+ collector(struct_info);
+ return collector.shape_var_map_;
+ }
+
+ private:
+ void VisitStructInfo_(const TensorStructInfoNode* op) final {
+ if (const auto* shape_expr = op->shape.as<ShapeExprNode>()) {
+ for (const PrimExpr& s : shape_expr->values) {
+ // Only collect single var defined shape. Ignore something like `R.Tensor((m + 1, n + 1))
+ if (const auto* var = s.as<tir::VarNode>()) {
+ shape_var_map_.Set(GetRef<tir::Var>(var), s);
+ }
+ }
+ }
+ }
+
+ void VisitStructInfo_(const ShapeStructInfoNode* op) final {
+ for (const PrimExpr& s : op->values.value_or(Array<PrimExpr>())) {
+ // Only collect single var defined shape. Ignore something like `R.Tensor((m + 1, n + 1))
+ if (const auto* var = s.as<tir::VarNode>()) {
+ shape_var_map_.Set(GetRef<tir::Var>(var), s);
+ }
+ }
+ }
+
+ private:
+ Map<tir::Var, PrimExpr> shape_var_map_;
+ };
+};
+
+//---------------------------------------
+// Normalization
+//---------------------------------------
+#define RELAX_EXPR_NORMALIZER_LEAF(OP) \
+ Expr VisitExpr_(const OP* op) final { return GetRef<Expr>(op); }
+
+// TODO(relax-team): Check normalize logic after struct info.
+
+// Normalizer on struct info:
+//
+// We take benefit of the following invariants(that are checked in constructor):
+// - If an expr appears in StructInfo, then it is already normalized.
+// As a result, we do not need to peek into StructInfo in Normalization.
+// - Constant, ShapeExpr, already have their StructInfo populated in constructing time.
+class Normalizer : public BlockBuilderImpl, private ExprFunctor<Expr(const Expr&)> {
+ public:
+ explicit Normalizer(IRModule context_mod) : BlockBuilderImpl(context_mod) {}
+
+ Expr Normalize(const Expr& expr) final {
+ Expr normalized = this->VisitExpr(expr);
+ // Invariant:
+ // After Normalize: an Expr always have
+ // struct_info (with the exception of Op).
+ if (!normalized->IsInstance<OpNode>()) {
+ ICHECK(normalized->struct_info_.defined())
+ << "The struct_info_ of an Expr except OpNode after "
+ "normalization must not be nullptr. However, this Expr does not have struct_info_: "
+ << normalized;
+ }
+
+ return normalized;
+ }
+
+ /*!
+ * \brief Normalize Argument values to call and other IR sub-fields.
+ * \param arg The argument.
+ * \return The normalized value.
+ *
+ * \note This function create a new binding for non-leaf expressions except for tuple.
+ */
+ Expr NormalizeArgument(const Expr& arg) final {
+ // Temp patch to ensure we handle inline PrimFunc case.
+ // TODO(relax-team) remove such cases from parser and testcases.
+ if (auto* prim_func = arg.as<tir::PrimFuncNode>()) {
+ return NormalizePrimFunc(GetRef<tir::PrimFunc>(prim_func));
+ }
+
+ if (!block_stack_.empty()) {
+ // cache lookup
+ BlockFrame* cur_frame = CurrentBlockFrame();
+ auto it = cur_frame->normalize_binding_map.find(arg);
+ if (it != cur_frame->normalize_binding_map.end()) {
+ return it->second;
+ }
+ }
+ // skip visit expr's cache, normalize arg
+ Expr post = ExprFunctor::VisitExpr(arg);
+
+ if (!IsLeafOrTuple(arg)) {
+ ICHECK(!block_stack_.empty()) << "Cannot normalize non-leaf without a scope";
+ Var var = this->Emit(post, "");
+ // NOTE: current frame addr can change due to underlying vector
+ // re-allocation, redo lookup
+ CurrentBlockFrame()->normalize_binding_map[arg] = var;
+ return var;
+ } else {
+ return post;
+ }
+ }
+
+ RELAX_EXPR_NORMALIZER_LEAF(ExternFuncNode);
+ RELAX_EXPR_NORMALIZER_LEAF(GlobalVarNode);
+ RELAX_EXPR_NORMALIZER_LEAF(OpNode);
+ RELAX_EXPR_NORMALIZER_LEAF(ConstantNode);
+ RELAX_EXPR_NORMALIZER_LEAF(ShapeExprNode);
+ RELAX_EXPR_NORMALIZER_LEAF(PrimValueNode);
+ RELAX_EXPR_NORMALIZER_LEAF(StringImmNode);
+ RELAX_EXPR_NORMALIZER_LEAF(DataTypeImmNode);
+
+ template <typename T>
+ Expr VisitVar_(const typename T::ContainerType* var) {
+ // Parameters and free-vars must be present with struct info
+ // Other vars must have already been normalized through binding
+ ICHECK(var->struct_info_.defined())
+ << "Var " << var->name_hint() << " does not have struct info.";
+ return GetRef<Var>(var);
+ }
+
+ Expr VisitExpr_(const VarNode* var) final { return VisitVar_<Var>(var); }
+
+ Expr VisitExpr_(const DataflowVarNode* var) final { return VisitVar_<DataflowVar>(var); }
+
+ // Temp patch to ensure we handle inline PrimFunc case.
+ // TODO(relax-team) remove such cases from parser and testcases.
+ Expr NormalizePrimFunc(tir::PrimFunc prim_func) {
+ if (!prim_func->struct_info_.defined()) {
+ auto finfo = FuncStructInfo::OpaqueFunc(StructInfoFromType(prim_func->ret_type));
+ UpdateStructInfo(prim_func, finfo);
+ }
+ return prim_func;
+ }
+
+ Expr VisitExpr(const Expr& expr) final {
+ // Temp patch to ensure we handle inline PrimFunc case.
+ // TODO(relax-team) remove such cases from parser and testcases.
+ if (auto* prim_func = expr.as<tir::PrimFuncNode>()) {
+ return NormalizePrimFunc(GetRef<tir::PrimFunc>(prim_func));
+ }
+
+ // lookup normalize map
+ if (!block_stack_.empty()) {
+ BlockFrame* cur_frame = CurrentBlockFrame();
+ auto it = cur_frame->normalize_binding_map.find(expr);
+ if (it != cur_frame->normalize_binding_map.end()) {
+ return it->second;
+ }
+ }
+ return ExprFunctor::VisitExpr(expr);
+ }
+
+ Expr VisitExpr_(const TupleNode* op) final {
+ bool unchanged = true;
+ Array<Expr> new_fields;
+
+ for (const Expr& field : op->fields) {
+ Expr new_field = this->NormalizeArgument(field);
+ new_fields.push_back(new_field);
+ unchanged &= new_field.same_as(field);
+ }
+
+ Tuple tuple = unchanged ? GetRef<Tuple>(op) : Tuple(new_fields, op->span);
+ // Update tuple fields.
+ if (!tuple->struct_info_.defined()) {
+ Array<StructInfo> tuple_sinfo;
+ for (Expr field : tuple->fields) {
+ tuple_sinfo.push_back(GetStructInfo(field));
+ }
+ UpdateStructInfo(tuple, TupleStructInfo(tuple_sinfo, op->span));
+ }
+ return tuple;
+ }
+
+ Expr VisitExpr_(const FunctionNode* op) final {
+ Expr new_body = this->VisitWithNewScope(op->body, op->params);
+
+ if (new_body.same_as(op->body)) {
+ return GetRef<Function>(op);
+ } else {
+ return Function(op->params, new_body, op->ret_struct_info, op->attrs);
+ }
+ }
+
+ Expr VisitExpr_(const CallNode* op) final {
+ Expr new_op = this->NormalizeArgument(op->op);
+ bool unchanged = new_op.same_as(op->op);
+
+ Array<Expr> new_args;
+
+ for (Expr arg : op->args) {
+ Expr new_arg = this->NormalizeArgument(arg);
+ new_args.push_back(new_arg);
+ unchanged &= new_arg.same_as(arg);
+ }
+
+ Call call;
+ if (unchanged) {
+ call = GetRef<Call>(op);
+ } else {
+ call = Call(new_op, new_args, op->attrs, op->sinfo_args);
+ }
+
+ if (!call->struct_info_.defined()) {
+ auto inferred_sinfo = InferStructInfo(call);
+ UpdateStructInfo(call, inferred_sinfo);
+ }
+
+ return call;
+ }
+
+ Expr VisitExpr_(const SeqExprNode* op) final {
+ bool unchanged = true;
+ Array<BindingBlock> new_blocks;
+ for (BindingBlock block : op->blocks) {
+ BindingBlock new_block = this->VisitBindingBlock(block);
+ new_blocks.push_back(new_block);
+ unchanged &= new_block.same_as(block);
+ }
+
+ this->BeginBindingBlock();
+ // the body may not be a leaf expression, so check for that
+ Expr new_body = this->NormalizeArgument(op->body);
+ unchanged &= new_body.same_as(op->body);
+ BindingBlock prologue = this->EndBlock();
+
+ if (!prologue->bindings.empty()) {
+ new_blocks.push_back(prologue);
+ unchanged = false;
+ }
+
+ // Combine nearby blocks if possible
+ Array<BindingBlock> normalized_blocks = NormalizeBlocks(new_blocks);
+ unchanged &= normalized_blocks.same_as(new_blocks);
+
+ SeqExpr seq_expr;
+ if (unchanged) {
+ seq_expr = GetRef<SeqExpr>(op);
+ } else {
+ seq_expr = SeqExpr(normalized_blocks, new_body, op->span);
+ }
+
+ // only do shape/type inference if the SeqExpr does not have shape/type
+ if (!seq_expr->struct_info_.defined()) {
+ UpdateStructInfo(seq_expr, EraseToWellDefinedInScope(GetStructInfo(seq_expr->body)));
+ }
+ return seq_expr;
+ }
+
+ Expr VisitExpr_(const IfNode* op) final {
+ Expr new_cond = this->NormalizeArgument(op->cond);
+ Expr new_true = this->VisitWithNewScope(op->true_branch);
+ Expr new_false = this->VisitWithNewScope(op->false_branch);
+
+ If if_node;
+ if (new_cond.same_as(op->cond) && new_true.same_as(op->true_branch) &&
+ new_false.same_as(op->false_branch)) {
+ if_node = GetRef<If>(op);
+ } else {
+ if_node = If(new_cond, new_true, new_false, op->span);
+ }
+ if (!if_node->struct_info_.defined()) {
+ auto true_info = EraseToWellDefinedInScope(GetStructInfo(new_true));
+ auto false_info = EraseToWellDefinedInScope(GetStructInfo(new_false));
+ UpdateStructInfo(if_node, StructInfoLCA(true_info, false_info));
+ }
+ return if_node;
+ }
+
+ Expr VisitExpr_(const TupleGetItemNode* op) final {
+ Expr new_tuple = this->NormalizeArgument(op->tuple);
+
+ TupleGetItem node = new_tuple.same_as(op->tuple) ? GetRef<TupleGetItem>(op)
+ : TupleGetItem(new_tuple, op->index);
+
+ if (!node->struct_info_.defined()) {
+ auto opt = MatchStructInfo<TupleStructInfo>(node->tuple);
+ ICHECK(opt) << "The struct info of Tuple must be TupleStructInfo.";
+ UpdateStructInfo(node, opt.value()->fields[node->index]);
+ }
+
+ return node;
+ }
+
+ Binding VisitBinding(const Binding& binding) {
+ if (auto* var_binding = binding.as<VarBindingNode>()) {
+ return this->VisitVarBinding(GetRef<VarBinding>(var_binding));
+ } else {
+ auto* match_cast = binding.as<MatchCastNode>();
+ ICHECK(match_cast) << "Unsupported binding type: " << binding->GetTypeKey();
+ return this->VisitMatchCast(GetRef<MatchCast>(match_cast));
+ }
+ }
+
+ VarBinding VisitVarBinding(VarBinding binding) {
+ Expr new_value = this->VisitExpr(binding->value);
+ if (!new_value.same_as(binding->value)) {
+ binding = VarBinding(binding->var, new_value, binding->span);
+ }
+ if (!binding->var->struct_info_.defined()) {
+ UpdateStructInfo(binding->var, GetStructInfo(new_value));
+ }
+ return binding;
+ }
+
+ MatchCast VisitMatchCast(MatchCast binding) {
+ Expr new_value = this->VisitExpr(binding->value);
+ if (!new_value.same_as(binding->value)) {
+ binding = MatchCast(binding->var, new_value, binding->struct_info, binding->span);
+ }
+ if (!binding->var->struct_info_.defined()) {
+ UpdateStructInfo(binding->var, binding->struct_info);
+ }
+ return binding;
+ }
+
+ BindingBlock VisitBindingBlock(const BindingBlock& block) {
+ if (block.as<DataflowBlockNode>()) {
+ this->BeginDataflowBlock();
+ } else {
+ this->BeginBindingBlock();
+ }
+
+ bool unchanged = true;
+ for (const Binding& binding : block->bindings) {
+ Binding new_binding = this->VisitBinding(binding);
+ unchanged &= new_binding.same_as(binding);
+
+ this->EmitNormalized(new_binding);
+ }
+ BindingBlock new_block = this->EndBlock();
+ unchanged &= new_block->bindings.size() == block->bindings.size();
+ if (unchanged) {
+ return block;
+ }
+ return new_block;
+ }
+
+ private:
+ // Helper function to infer the type of a Call.
+ StructInfo InferStructInfo(const Call& call) {
+ if (auto* op_ptr = call->op.as<OpNode>()) {
+ // Case 1: the op field is a primitive op, look up FInferStructInfo attribute
+ Op op = GetRef<Op>(op_ptr);
+ ICHECK(op_map_infer_struct_info_.count(op))
+ << " Cannot find the FInferStructInfo attribute registered to op: " << op->name;
+ return op_map_infer_struct_info_[op](call, GetRef<BlockBuilder>(this));
+ } else {
+ // derive using function parameters
+ ICHECK(call->op->struct_info_.defined());
+ auto opt = MatchStructInfo<FuncStructInfo>(call->op);
+ ICHECK(opt) << "Call->op must contains a function struct info";
+ FuncStructInfo finfo = opt.value();
+ return DeriveCallRetStructInfo(finfo, call, GetRef<BlockBuilder>(this), &analyzer_);
+ }
+ }
+
+ // erase to well defined within current scope.
+ StructInfo EraseToWellDefinedInScope(StructInfo info) {
+ if (scope_stack_.empty()) {
+ return EraseToWellDefined(info);
+ }
+ auto* curr_scope = CurrentScopeFrame();
+ auto f_shape_var_map = [curr_scope](tir::Var var) -> Optional<PrimExpr> {
+ auto it = curr_scope->shape_var_map.find(var);
+ if (it != curr_scope->shape_var_map.end()) return (*it).second;
+ return NullOpt;
+ };
+ return EraseToWellDefined(info, f_shape_var_map);
+ }
+
+ Expr VisitWithNewScope(const Expr& expr, Optional<Array<Var>> params = NullOpt) {
+ // SeqExpr do not need to prepare for normalization.
+ if (expr.as<SeqExprNode>()) {
+ this->BeginScope(params);
+ Expr ret = this->VisitExpr(expr);
+ this->EndScope();
+ return ret;
+ } else {
+ this->BeginScope(params);
+
+ this->BeginBindingBlock();
+ Expr post = this->NormalizeArgument(expr);
+ BindingBlock prologue = this->EndBlock();
+ // "New scopes" (function bodies, if/else clauses) must be wrapped in seq exprs.
+ // Don't wrap if it's already a seq and there are no bindings to add
+ if (post.as<SeqExprNode>() && prologue->bindings.empty()) {
+ return post;
+ }
+ Array<BindingBlock> bindings;
+ if (!prologue->bindings.empty()) {
+ bindings.push_back(prologue);
+ }
+
+ SeqExpr seq(bindings, post);
+ UpdateStructInfo(seq, EraseToWellDefinedInScope(GetStructInfo(seq->body)));
+
+ this->EndScope();
+ return seq;
+ }
+ }
+
+ Array<BindingBlock> FlattenBlocks(const Array<BindingBlock>& blocks) {
+ // If there is a binding that is a seq expr, split the current block,
+ // add the nested blocks prior to the seq expr, and bind the seq expr body
+ // to the var
+ Array<BindingBlock> ret;
+ bool changed = false;
+ for (const BindingBlock& block : blocks) {
+ bool is_dataflow = block->IsInstance<DataflowBlockNode>();
+ Array<Binding> current;
+ for (const Binding& binding : block->bindings) {
+ Expr value;
+ if (const auto* var_binding = binding.as<VarBindingNode>()) {
+ value = var_binding->value;
+ } else if (const auto* match_cast = binding.as<MatchCastNode>()) {
+ value = match_cast->value;
+ } else {
+ LOG(FATAL) << "Unknown binding type: " << binding->GetTypeKey();
+ }
+ // if we encounter a nested seq, we have to flatten it:
+ // 1. Append the binding block we've accumulated so far
+ // 2. Reset the current block
+ // 3. Append the inner blocks
+ // 4. Add a binding of the current var to the seq expr's body to the current block
+ // then continue
+ if (auto seq = value.as<SeqExprNode>()) {
+ changed = true;
+ ret.push_back(is_dataflow ? DataflowBlock(current) : BindingBlock(current));
+ current = {};
+ // We do not need to flatten recursively because the normalizer will have normalized
+ // and thus flattened the inner SeqExprs already
+ for (const BindingBlock& block : seq->blocks) {
+ if (is_dataflow && !block->IsInstance<DataflowBlockNode>()) {
+ LOG(WARNING) << "Malformed AST: Seq expr nested inside a dataflow block contains a "
+ "non-dataflow block! "
+ << seq;
+ }
+ ret.push_back(block);
+ }
+
+ if (const auto* var_binding = binding.as<VarBindingNode>()) {
+ current.push_back(VarBinding(var_binding->var, seq->body));
+ } else if (const auto* match_cast = binding.as<MatchCastNode>()) {
+ current.push_back(MatchCast(match_cast->var, seq->body, match_cast->struct_info));
+ } else {
+ LOG(FATAL) << "Unknown binding type: " << binding->GetTypeKey();
+ }
+ } else {
+ current.push_back(binding);
+ }
+ }
+ ret.push_back(is_dataflow ? DataflowBlock(current) : BindingBlock(current));
+ }
+ return changed ? ret : blocks;
+ }
+
+ Array<BindingBlock> NormalizeBlocks(const Array<BindingBlock>& blocks) {
+ bool changed = false;
+ Array<BindingBlock> ret;
+ auto flattened = FlattenBlocks(blocks);
+ if (!flattened.same_as(blocks)) {
+ changed = true;
+ }
+ for (const BindingBlock& block : flattened) {
+ if (block->bindings.empty()) {
+ // Case 1. Skip empty blocks
+ changed = true;
+ } else if (!ret.empty() && ret.back()->type_index() == block->type_index()) {
+ // Case 2. Merge with previous block if possible
+ BindingBlock merged;
+ // NOTE: should check DataflowBlockNode first.
+ if (const auto* dataflow_block = ret.back().as<DataflowBlockNode>()) {
+ auto n = make_object<DataflowBlockNode>(*dataflow_block);
+ n->bindings.insert(n->bindings.end(), block->bindings.begin(), block->bindings.end());
+ merged = DataflowBlock(n);
+ } else if (const auto* binding_block = ret.back().as<BindingBlockNode>()) {
+ auto n = make_object<BindingBlockNode>(*binding_block);
+ n->bindings.insert(n->bindings.end(), block->bindings.begin(), block->bindings.end());
+ merged = BindingBlock(n);
+ } else {
+ LOG(FATAL) << "Unknown block type: " << ret.back()->GetTypeKey();
+ }
+ ret.pop_back();
+ ret.push_back(merged);
+ changed = true;
+ } else {
+ // Case 3. Add to the result
+ ret.push_back(block);
+ }
+ }
+ return changed ? ret : blocks;
+ }
+
+ /*! \brief Operator struct info inference map. */
+ tvm::OpAttrMap<FInferStructInfo> op_map_infer_struct_info_ =
+ Op::GetAttrMap<FInferStructInfo>("FInferStructInfo");
+};
+
+BlockBuilder BlockBuilder::Create(Optional<IRModule> mod) {
+ ObjectPtr<BlockBuilderNode> n = make_object<Normalizer>(mod.value_or(IRModule()));
+ return BlockBuilder(n);
+}
+
+//---------------------------------------
+// User facing function registration.
+//---------------------------------------
+TVM_REGISTER_OBJECT_TYPE(BlockBuilderNode);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderCreate").set_body_typed([](Optional<IRModule> mod) {
+ return BlockBuilder::Create(mod);
+});
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderBeginDataflowBlock")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::BeginDataflowBlock);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderBeginBindingBlock")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::BeginBindingBlock);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEndBlock")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::EndBlock);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderNormalize")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::Normalize);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEmit").set_body_typed([](BlockBuilder builder, Expr expr) {
+ return builder->Emit(expr);
+});
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEmitMatchCast")
+ .set_body_typed([](BlockBuilder builder, Expr value, StructInfo struct_info) {
+ return builder->EmitMatchCast(value, struct_info);
+ });
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEmitOutput")
+ .set_body_typed([](BlockBuilder builder, const Expr& output) {
+ return builder->EmitOutput(output);
+ });
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEmitNormalized")
+ .set_body_typed([](BlockBuilder builder, Binding binding) {
+ return builder->EmitNormalized(binding);
+ });
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderGetUniqueName")
+ .set_body_typed([](BlockBuilder builder, String name_hint) {
+ return builder->name_table()->GetUniqueName(name_hint);
+ });
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderAddFunction")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::AddFunction);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderUpdateFunction")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::UpdateFunction);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderGetContextIRModule")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::GetContextIRModule);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderCurrentBlockIsDataFlow")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::CurrentBlockIsDataFlow);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderLookupBinding")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::LookupBinding);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderBeginScope")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::BeginScope);
+
+TVM_REGISTER_GLOBAL("relax.BlockBuilderEndScope")
+ .set_body_method<BlockBuilder>(&BlockBuilderNode::EndScope);
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/emit_te.cc b/src/relax/ir/emit_te.cc
new file mode 100644
index 0000000000..bfb5896c99
--- /dev/null
+++ b/src/relax/ir/emit_te.cc
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file relax/src/ir/emit_te.cc
+ */
+#include "./emit_te.h"
+
+#include <tvm/relax/struct_info.h>
+#include <tvm/tir/stmt_functor.h>
+
+namespace tvm {
+namespace relax {
+
+// RXPlaceholderOpNode
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<RXPlaceholderOpNode>([](const ObjectRef& node, ReprPrinter* p) {
+ auto* op = static_cast<const RXPlaceholderOpNode*>(node.get());
+ p->stream << "rxplaceholder(" << op->name << ", " << op << ")";
+ });
+
+TVM_REGISTER_NODE_TYPE(RXPlaceholderOpNode);
+
+te::Tensor TETensor(Expr value, Map<tir::Var, PrimExpr> tir_var_map, std::string name) {
+ auto n = make_object<RXPlaceholderOpNode>();
+ n->name = name;
+ n->value = value;
+
+ // If the value is a constant, it might come as an argument of EmitTE and thus its shape and
+ // checked-type might not be properly set. In this case we set the shape and dtype of the returned
+ // TE tensor.
+ if (const auto* constant = value.as<ConstantNode>()) {
+ n->dtype = DataType(constant->data->dtype);
+
+ int ndim = constant->data->ndim;
+ ShapeTuple shape_tuple = constant->data.Shape();
+ Array<PrimExpr> shape;
+ shape.reserve(ndim);
+ for (int i = 0; i < ndim; ++i) {
+ shape.push_back(IntImm(DataType::Int(64), shape_tuple[i]));
+ }
+ n->shape = std::move(shape);
+ return te::PlaceholderOp(n).output(0);
+ }
+ ICHECK(value->struct_info_.defined()) << "value must be normalized and contain StructInfo";
+ auto* tensor_sinfo = GetStructInfoAs<TensorStructInfoNode>(value);
+ ICHECK(tensor_sinfo) << "Value must be a tensor";
+ auto* shape_expr = tensor_sinfo->shape.as<ShapeExprNode>();
+ CHECK(shape_expr)
+ << "ValueError: Expression does not have an known symbolic shape, please consider use "
+ "match_cast "
+ << "to constrain the shape before passing into te_tensor";
+ n->shape = shape_expr->values.Map(
+ [&tir_var_map](const PrimExpr& e) { return tir::Substitute(e, tir_var_map); });
+ n->dtype = tensor_sinfo->dtype;
+ return te::PlaceholderOp(n).output(0);
+}
+
+TVM_REGISTER_GLOBAL("relax.TETensor").set_body_typed(TETensor);
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/emit_te.h b/src/relax/ir/emit_te.h
new file mode 100644
index 0000000000..46207479c7
--- /dev/null
+++ b/src/relax/ir/emit_te.h
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file relax/src/ir/emit_te.h
+ * \brief Tensor expression extension in Relax.
+ */
+#ifndef TVM_RELAX_IR_EMIT_TE_H_
+#define TVM_RELAX_IR_EMIT_TE_H_
+
+#include <tvm/relax/expr.h>
+#include <tvm/te/operation.h>
+
+#include <string>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief A placeholder op that represents a relax expression.
+ */
+class RXPlaceholderOpNode : public te::PlaceholderOpNode {
+ public:
+ /*! \brief The relax expression. */
+ Expr value;
+
+ void VisitAttrs(AttrVisitor* v) {
+ v->Visit("name", &name);
+ v->Visit("tag", &tag);
+ v->Visit("attrs", &attrs);
+ v->Visit("value", &value);
+ v->Visit("shape", &shape);
+ v->Visit("dtype", &dtype);
+ }
+
+ static constexpr const char* _type_key = "RXPlaceholderOp";
+ TVM_DECLARE_FINAL_OBJECT_INFO(RXPlaceholderOpNode, te::PlaceholderOpNode);
+};
+
+/*!
+ * \brief Create a TE tensor from relax expression, with TIR variables in the
+ * tensor shape substituted by the given mapping.
+ * \param value The relax expression, which is required to have TensorStructInfo.
+ * \param tir_var_map The mapping to substitute the TIR variables appeared in the
+ * shape of the input Expr.
+ * \param name The name of the created tensor.
+ */
+te::Tensor TETensor(Expr value, Map<tir::Var, PrimExpr> tir_var_map, std::string name);
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_IR_EMIT_TE_H_
diff --git a/src/relax/ir/expr_functor.cc b/src/relax/ir/expr_functor.cc
index 048de7950f..4c4b68f3d2 100644
--- a/src/relax/ir/expr_functor.cc
+++ b/src/relax/ir/expr_functor.cc
@@ -542,5 +542,249 @@ BindingBlock ExprMutatorBase::VisitBindingBlock(const BindingBlock& block) {
PrimExpr ExprMutatorBase::VisitPrimExpr(const PrimExpr& expr) { return expr; }
+// ==================
+// ExprMutator
+
+Expr ExprMutator::VisitExpr(const Expr& expr) {
+ return builder_->Normalize(ExprFunctor::VisitExpr(expr));
+}
+
+// Visit the use-site of a defined Var
+Expr ExprMutator::VisitExpr_(const VarNode* op) {
+ auto it = var_remap_.find(op->vid);
+ if (it != var_remap_.end()) {
+ return it->second;
+ }
+
+ // default case return self.
+ return GetRef<Expr>(op);
+}
+
+// Visit the use-site of a defined DataflowVar
+Expr ExprMutator::VisitExpr_(const DataflowVarNode* op) {
+ auto it = var_remap_.find(op->vid);
+ if (it != var_remap_.end()) {
+ return it->second;
+ }
+
+ // default case return self.
+ return GetRef<Expr>(op);
+}
+
+Expr ExprMutator::VisitExpr_(const FunctionNode* op) {
+ tvm::Array<Var> params;
+ bool all_params_unchanged = true;
+ for (Var param : op->params) {
+ Var new_param = this->VisitVarDef(param);
+ params.push_back(new_param);
+ all_params_unchanged &= param.same_as(new_param);
+ }
+
+ Expr body = this->VisitWithNewScope(op->body, params);
+
+ // FuncStructInfo does not depend on Expr
+ if (all_params_unchanged && body.same_as(op->body)) {
+ return GetRef<Expr>(op);
+ } else {
+ return Function(params, body, op->ret_struct_info, op->attrs);
+ }
+}
+
+Expr ExprMutator::VisitExpr_(const IfNode* op) {
+ Expr guard = this->VisitExpr(op->cond);
+ Expr true_b = this->VisitWithNewScope(op->true_branch);
+ Expr false_b = this->VisitWithNewScope(op->false_branch);
+ if (op->cond.same_as(guard) && op->true_branch.same_as(true_b) &&
+ op->false_branch.same_as(false_b) &&
+ VisitAndCheckStructInfoFieldUnchanged(op->struct_info_)) {
+ return GetRef<Expr>(op);
+ } else {
+ return If(guard, true_b, false_b, op->span);
+ }
+}
+
+Expr ExprMutator::VisitExpr_(const SeqExprNode* op) {
+ bool all_blocks_unchanged = true;
+ Array<BindingBlock> blocks;
+ for (auto block : op->blocks) {
+ BindingBlock new_block = this->VisitBindingBlock(block);
+ if (!new_block->bindings.empty()) {
+ blocks.push_back(new_block);
+ }
+ all_blocks_unchanged &= block.same_as(new_block);
+ }
+
+ builder_->BeginBindingBlock();
+ Expr body = this->VisitExpr(op->body);
+ BindingBlock prologue = builder_->EndBlock();
+ if (!prologue->bindings.empty()) {
+ blocks.push_back(prologue);
+ all_blocks_unchanged = false;
+ }
+
+ if (all_blocks_unchanged && body.same_as(op->body) &&
+ VisitAndCheckStructInfoFieldUnchanged(op->struct_info_)) {
+ return GetRef<Expr>(op);
+ } else {
+ return SeqExpr(blocks, body);
+ }
+}
+
+RELAX_VAR_BINDING_DISPATCH_IMPL(ExprMutator);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(ConstantNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(TupleNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(VarNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(DataflowVarNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(ShapeExprNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(ExternFuncNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(GlobalVarNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(FunctionNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(CallNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(SeqExprNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(IfNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(OpNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(TupleGetItemNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(PrimValueNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(StringImmNode);
+RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(DataTypeImmNode);
+
+void ExprMutator::ReEmitBinding(const VarBindingNode* binding, Expr new_value) {
+ Var new_var = this->VisitVarDef(binding->var);
+
+ // fast path: reemit binding if nothing changes
+ if (new_var.same_as(binding->var) && new_value.same_as(binding->value)) {
+ builder_->EmitNormalized(GetRef<VarBinding>(binding));
+ return;
+ }
+
+ Var temp = WithStructInfo(new_var, GetStructInfo(new_value));
+ if (!temp.same_as(new_var)) {
+ new_var = temp;
+ this->var_remap_[binding->var->vid] = new_var;
+ }
+
+ builder_->EmitNormalized(VarBinding(new_var, new_value));
+}
+
+void ExprMutator::VisitBinding_(const MatchCastNode* binding) {
+ Var new_var = this->VisitVarDef(binding->var);
+ Expr new_value = this->VisitExpr(binding->value);
+
+ // re-emit old binding if nothing changes
+ if (new_var.same_as(binding->var) && new_value.same_as(binding->value)) {
+ builder_->EmitNormalized(GetRef<MatchCast>(binding));
+ } else {
+ new_value = builder_->NormalizeArgument(new_value);
+ builder_->EmitNormalized(MatchCast(new_var, new_value, binding->struct_info, binding->span));
+ }
+}
+
+BindingBlock ExprMutator::VisitBindingBlock_(const BindingBlockNode* block) {
+ builder_->BeginBindingBlock();
+ for (Binding binding : block->bindings) {
+ this->VisitBinding(binding);
+ }
+ return builder_->EndBlock();
+}
+
+BindingBlock ExprMutator::VisitBindingBlock_(const DataflowBlockNode* block) {
+ builder_->BeginDataflowBlock();
+ for (auto binding : block->bindings) {
+ this->VisitBinding(binding);
+ }
+ return builder_->EndBlock();
+}
+
+Var ExprMutator::VisitVarDef_(const DataflowVarNode* var) {
+ if (auto* sinfo = var->struct_info_.as<StructInfoNode>()) {
+ StructInfo struct_info = this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ if (struct_info.same_as(var->struct_info_)) {
+ return GetRef<DataflowVar>(var);
+ } else {
+ return DataflowVar(var->vid, struct_info, var->span);
+ }
+ } else {
+ return GetRef<DataflowVar>(var);
+ }
+}
+
+Var ExprMutator::VisitVarDef_(const VarNode* var) {
+ if (auto* sinfo = var->struct_info_.as<StructInfoNode>()) {
+ StructInfo struct_info = this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ if (struct_info.same_as(var->struct_info_)) {
+ return GetRef<Var>(var);
+ } else {
+ return Var(var->vid, struct_info, var->span);
+ }
+ } else {
+ return GetRef<Var>(var);
+ }
+}
+
+void ExprMutator::VisitBinding(const Binding& binding) {
+ if (const auto* node = binding.as<VarBindingNode>()) {
+ VisitBinding_(node);
+ } else if (const auto* node = binding.as<MatchCastNode>()) {
+ VisitBinding_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << binding->GetTypeKey();
+ }
+}
+
+BindingBlock ExprMutator::VisitBindingBlock(const BindingBlock& block) {
+ BindingBlock ret;
+ if (const auto* node = block.as<DataflowBlockNode>()) {
+ ret = VisitBindingBlock_(node);
+ } else if (const auto* node = block.as<BindingBlockNode>()) {
+ ret = VisitBindingBlock_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << block->GetTypeKey();
+ }
+ return ret;
+}
+
+Var ExprMutator::VisitVarDef(const Var& var) {
+ Var ret;
+ if (const auto* node = var.as<DataflowVarNode>()) {
+ ret = VisitVarDef_(node);
+ } else if (const auto* node = var.as<VarNode>()) {
+ ret = VisitVarDef_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << var->GetTypeKey();
+ }
+ return ret;
+}
+
+Expr ExprMutator::VisitWithNewScope(const Expr& expr, Optional<Array<Var>> params) {
+ ICHECK(expr->IsInstance<SeqExprNode>())
+ << "Normal form requires all new scope is stored as SeqExpr";
+ builder_->BeginScope(params);
+ Expr ret = this->VisitExpr(expr);
+ builder_->EndScope();
+ return ret;
+}
+
+Optional<Expr> ExprMutator::LookupBinding(const Var& var) { return builder_->LookupBinding(var); }
+
+Var ExprMutator::WithStructInfo(Var var, StructInfo struct_info) {
+ ICHECK(struct_info.defined());
+
+ // TODO(relax-team) add StructInfoEqual check
+ if (var->struct_info_.defined()) {
+ // use same-as as a quick path
+ if (var->struct_info_.same_as(struct_info) ||
+ StructuralEqual()(var->struct_info_, struct_info)) {
+ return var;
+ } else {
+ Var new_var = var.as<DataflowVarNode>() ? DataflowVar(var->vid, struct_info, var->span)
+ : Var(var->vid, struct_info, var->span);
+ return new_var;
+ }
+ } else {
+ UpdateStructInfo(var, struct_info);
+ return var;
+ }
+}
+
} // namespace relax
} // namespace tvm
diff --git a/src/relax/ir/py_expr_functor.cc b/src/relax/ir/py_expr_functor.cc
new file mode 100644
index 0000000000..7e86235aa6
--- /dev/null
+++ b/src/relax/ir/py_expr_functor.cc
@@ -0,0 +1,649 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/py_expr_functor.cc
+ * \brief The backbone of PyExprVisitor/PyExprMutator.
+ */
+#include <tvm/relax/expr_functor.h>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief The abstract interface of ExprVisitor.
+ */
+class PyExprVisitorNode : public Object, public ExprVisitor {
+ private:
+ using TSelf = PyExprVisitorNode;
+ using FType = tvm::NodeFunctor<void(const ObjectRef& n, TSelf* self)>;
+
+ public:
+ /*! \brief The packed function to the `VisitExpr(const Expr& expr)` function. */
+ PackedFunc f_visit_expr{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ConstantNode* op)` function. */
+ PackedFunc f_visit_constant_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const TupleNode* op)` function. */
+ PackedFunc f_visit_tuple_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const VarNode* op)` function. */
+ PackedFunc f_visit_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const DataflowVarNode* op)` function. */
+ PackedFunc f_visit_dataflow_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ShapeExprNode* op)` function. */
+ PackedFunc f_visit_shape_expr_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ExternFuncNode* op)` function. */
+ PackedFunc f_visit_extern_func_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const GlobalVarNode* op)` function. */
+ PackedFunc f_visit_global_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const FunctionNode* op)` function. */
+ PackedFunc f_visit_function_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const CallNode* op)` function. */
+ PackedFunc f_visit_call_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const SeqExprNode* op)` function. */
+ PackedFunc f_visit_seq_expr_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const IfNode* op)` function. */
+ PackedFunc f_visit_if_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const OpNode* op)` function. */
+ PackedFunc f_visit_op_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const TupleGetItemNode* op)` function. */
+ PackedFunc f_visit_tuple_getitem_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const PrimValueNode* op)` function. */
+ PackedFunc f_visit_prim_value_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const StringImmNode* op)` function. */
+ PackedFunc f_visit_string_imm_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const DataTypeImmNode* op)` function. */
+ PackedFunc f_visit_data_type_imm_{nullptr};
+ /*! \brief The packed function to the `VisitBinding(const Binding& binding)` function. */
+ PackedFunc f_visit_binding{nullptr};
+ /*! \brief The packed function to the `VisitBinding_(const VarBindingNode* binding)`
+ * function. */
+ PackedFunc f_visit_var_binding_{nullptr};
+ /*! \brief The packed function to the `VisitBinding_(const MatchCastNode* binding)`
+ * function. */
+ PackedFunc f_visit_match_cast_{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock(const BindingBlock& block)`
+ * function. */
+ PackedFunc f_visit_binding_block{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock_(const BindingBlockNode* block)`
+ * function. */
+ PackedFunc f_visit_binding_block_{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock_(const DataflowBlockNode* block)`
+ * function. */
+ PackedFunc f_visit_dataflow_block_{nullptr};
+ /*! \brief The packed function to the `VisitVarDef(const Var& var)` function. */
+ PackedFunc f_visit_var_def{nullptr};
+ /*! \brief The packed function to the `VisitVarDef_(const VarNode* var)` function. */
+ PackedFunc f_visit_var_def_{nullptr};
+ /*! \brief The packed function to the `VisitVarDef_(const DataflowVarNode* var)` function. */
+ PackedFunc f_visit_dataflow_var_def_{nullptr};
+ /*! \brief The packed function to the `VisitSpan(const Span& span)` function. */
+ PackedFunc f_visit_span{nullptr};
+
+ void VisitExpr(const Expr& expr) {
+ if (f_visit_expr != nullptr) {
+ f_visit_expr(expr);
+ } else {
+ // Need to init the overwrite VTable
+ static FType vtable = InitVTable();
+ vtable(expr, this);
+ }
+ }
+
+ void VisitBinding(const Binding& binding)
+ PY_EXPR_VISITOR_DEFAULT(binding, f_visit_binding, ExprVisitor::VisitBinding(binding));
+
+ void VisitBinding_(const VarBindingNode* binding)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<VarBinding>(binding), f_visit_var_binding_,
+ ExprVisitor::VisitBinding_(binding));
+ void VisitBinding_(const MatchCastNode* binding)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<MatchCast>(binding), f_visit_match_cast_,
+ ExprVisitor::VisitBinding_(binding));
+
+ void VisitBindingBlock(const BindingBlock& block)
+ PY_EXPR_VISITOR_DEFAULT(block, f_visit_binding_block, ExprVisitor::VisitBindingBlock(block));
+
+ void VisitBindingBlock_(const BindingBlockNode* block)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<BindingBlock>(block), f_visit_binding_block_,
+ ExprVisitor::VisitBindingBlock_(block));
+ void VisitBindingBlock_(const DataflowBlockNode* block)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<DataflowBlock>(block), f_visit_dataflow_block_,
+ ExprVisitor::VisitBindingBlock_(block));
+
+ void VisitVarDef(const Var& var)
+ PY_EXPR_VISITOR_DEFAULT(var, f_visit_var_def, ExprVisitor::VisitVarDef(var));
+ void VisitVarDef_(const VarNode* var)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<Var>(var), f_visit_var_def_, ExprVisitor::VisitVarDef_(var));
+ void VisitVarDef_(const DataflowVarNode* var)
+ PY_EXPR_VISITOR_DEFAULT(GetRef<DataflowVar>(var), f_visit_dataflow_var_def_,
+ ExprVisitor::VisitVarDef_(var));
+
+ void VisitSpan(const Span& span)
+ PY_EXPR_VISITOR_DEFAULT(span, f_visit_span, ExprVisitor::VisitSpan(span));
+
+ void VisitAttrs(AttrVisitor* v) {}
+ static constexpr const char* _type_key = "expr_functor.PyExprVisitor";
+ TVM_DECLARE_BASE_OBJECT_INFO(PyExprVisitorNode, Object);
+
+ private:
+ // initialize the vtable.
+ static FType InitVTable() {
+ FType vtable;
+ // Set dispatch
+ PY_EXPR_VISITOR_DISPATCH(ConstantNode, f_visit_constant_);
+ PY_EXPR_VISITOR_DISPATCH(TupleNode, f_visit_tuple_);
+ PY_EXPR_VISITOR_DISPATCH(VarNode, f_visit_var_);
+ PY_EXPR_VISITOR_DISPATCH(DataflowVarNode, f_visit_dataflow_var_);
+ PY_EXPR_VISITOR_DISPATCH(ShapeExprNode, f_visit_shape_expr_);
+ PY_EXPR_VISITOR_DISPATCH(ExternFuncNode, f_visit_extern_func_);
+ PY_EXPR_VISITOR_DISPATCH(GlobalVarNode, f_visit_global_var_);
+ PY_EXPR_VISITOR_DISPATCH(FunctionNode, f_visit_function_);
+ PY_EXPR_VISITOR_DISPATCH(CallNode, f_visit_call_);
+ PY_EXPR_VISITOR_DISPATCH(SeqExprNode, f_visit_seq_expr_);
+ PY_EXPR_VISITOR_DISPATCH(IfNode, f_visit_if_);
+ PY_EXPR_VISITOR_DISPATCH(OpNode, f_visit_op_);
+ PY_EXPR_VISITOR_DISPATCH(TupleGetItemNode, f_visit_tuple_getitem_);
+ PY_EXPR_VISITOR_DISPATCH(PrimValueNode, f_visit_prim_value_);
+ PY_EXPR_VISITOR_DISPATCH(StringImmNode, f_visit_string_imm_);
+ PY_EXPR_VISITOR_DISPATCH(DataTypeImmNode, f_visit_data_type_imm_);
+ return vtable;
+ }
+};
+
+TVM_REGISTER_NODE_TYPE(PyExprVisitorNode);
+
+/*!
+ * \brief Managed reference to PyExprVisitorNode.
+ * \sa PyExprVisitorNode
+ */
+class PyExprVisitor : public ObjectRef {
+ public:
+ /*!
+ * \brief Create a PyExprVisitor with customized methods on the python-side.
+ * \param f_visit_expr The packed function of `VisitExpr(const Expr& expr)`.
+ * \param f_visit_constant_ The packed function of `VisitExpr_(const ConstantNode* op)`.
+ * \param f_visit_tuple_ The packed function of `VisitExpr_(const TupleNode* op)`.
+ * \param f_visit_var_ The packed function of `VisitExpr_(const VarNode* op)`.
+ * \param f_visit_dataflow_var_ The packed function of `VisitExpr_(const DataflowVarNode* op)`.
+ * \param f_visit_shape_expr_ The packed function of `VisitExpr_(const ShapeExprNode* op)`.
+ * \param f_visit_extern_func_ The packed function of `VisitExpr_(const ExternFuncNode* op)`.
+ * \param f_visit_global_var_ The packed function of `VisitExpr_(const GlobalVarNode* op)`.
+ * \param f_visit_function_ The packed function of `VisitExpr_(const FunctionNode* op)`.
+ * \param f_visit_call_ The packed function of `VisitExpr_(const CallNode* op)`.
+ * \param f_visit_seq_expr_ The packed function of `VisitExpr_(const SeqExprNode* op)`.
+ * \param f_visit_if_ The packed function of `VisitExpr_(const IfNode* op)`.
+ * \param f_visit_op_ The packed function of `VisitExpr_(const OpNode* op)`.
+ * \param f_visit_tuple_getitem_ The packed function of `VisitExpr_(const TupleGetItemNode* op)`.
+ * \param f_visit_prim_value_ The packed function of `VisitExpr_(const PrimValueNode* op)`.
+ * \param f_visit_string_imm_ The packed function of `VisitExpr_(const StringImmNode* op)`.
+ * \param f_visit_data_type_imm_ The packed function of `VisitExpr_(const DataTypeImmNode* op)`.
+ * \param f_visit_binding The packed function of `VisitBinding(const Binding& binding)`.
+ * \param f_visit_var_binding_ The packed function of `VisitBinding_(const VarBindingNode*
+ * binding)`.
+ * \param f_visit_match_cast_ The packed function of `VisitBinding_(const MatchCastNode*
+ * binding)`.
+ * \param f_visit_binding_block The packed function of `VisitBindingBlock(const BindingBlock&
+ * block)`.
+ * \param f_visit_binding_block_ The packed function of `VisitBindingBlock_(const
+ * BindingBlockNode* block)`.
+ * \param f_visit_dataflow_block_ The packed function of `VisitBindingBlock_(const
+ * DataflowBlockNode* block)`.
+ * \param f_visit_var_def The packed function of `VisitVarDef(const Var& var)`.
+ * \param f_visit_var_def_ The packed function of `VisitVarDef_(const VarNode* var)`.
+ * \param f_visit_dataflow_var_def_ The packed function of `VisitVarDef_(const DataflowVarNode*
+ * var)`.
+ * \param f_visit_span The packed function of `VisitSpan(const Span& span)`.
+ * \return The PyVisitor created.
+ */
+ TVM_DLL static PyExprVisitor MakePyExprVisitor(
+ PackedFunc f_visit_expr, PackedFunc f_visit_constant_, PackedFunc f_visit_tuple_,
+ PackedFunc f_visit_var_, PackedFunc f_visit_dataflow_var_, PackedFunc f_visit_shape_expr_,
+ PackedFunc f_visit_extern_func_, PackedFunc f_visit_global_var_, PackedFunc f_visit_function_,
+ PackedFunc f_visit_call_, PackedFunc f_visit_seq_expr_, PackedFunc f_visit_if_,
+ PackedFunc f_visit_op_, PackedFunc f_visit_tuple_getitem_, PackedFunc f_visit_prim_value_,
+ PackedFunc f_visit_string_imm_, PackedFunc f_visit_data_type_imm_, PackedFunc f_visit_binding,
+ PackedFunc f_visit_var_binding_, PackedFunc f_visit_match_cast_,
+ PackedFunc f_visit_binding_block, PackedFunc f_visit_binding_block_,
+ PackedFunc f_visit_dataflow_block_, PackedFunc f_visit_var_def, PackedFunc f_visit_var_def_,
+ PackedFunc f_visit_dataflow_var_def_, PackedFunc f_visit_span) {
+ ObjectPtr<PyExprVisitorNode> n = make_object<PyExprVisitorNode>();
+ n->f_visit_expr = f_visit_expr;
+ n->f_visit_binding = f_visit_binding;
+ n->f_visit_binding_block = f_visit_binding_block;
+ n->f_visit_var_def = f_visit_var_def;
+ n->f_visit_span = f_visit_span;
+ n->f_visit_constant_ = f_visit_constant_;
+ n->f_visit_tuple_ = f_visit_tuple_;
+ n->f_visit_var_ = f_visit_var_;
+ n->f_visit_dataflow_var_ = f_visit_dataflow_var_;
+ n->f_visit_shape_expr_ = f_visit_shape_expr_;
+ n->f_visit_extern_func_ = f_visit_extern_func_;
+ n->f_visit_global_var_ = f_visit_global_var_;
+ n->f_visit_function_ = f_visit_function_;
+ n->f_visit_call_ = f_visit_call_;
+ n->f_visit_seq_expr_ = f_visit_seq_expr_;
+ n->f_visit_if_ = f_visit_if_;
+ n->f_visit_op_ = f_visit_op_;
+ n->f_visit_tuple_getitem_ = f_visit_tuple_getitem_;
+ n->f_visit_prim_value_ = f_visit_prim_value_;
+ n->f_visit_string_imm_ = f_visit_string_imm_;
+ n->f_visit_data_type_imm_ = f_visit_data_type_imm_;
+ n->f_visit_var_binding_ = f_visit_var_binding_;
+ n->f_visit_match_cast_ = f_visit_match_cast_;
+ n->f_visit_binding_block_ = f_visit_binding_block_;
+ n->f_visit_dataflow_block_ = f_visit_dataflow_block_;
+ n->f_visit_var_def_ = f_visit_var_def_;
+ n->f_visit_dataflow_var_def_ = f_visit_dataflow_var_def_;
+ return PyExprVisitor(n);
+ }
+
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PyExprVisitor, ObjectRef, PyExprVisitorNode);
+};
+
+/*!
+ * \brief The abstract interface of ExprMutator.
+ */
+class PyExprMutatorNode : public Object, public ExprMutator {
+ private:
+ using TSelf = PyExprMutatorNode;
+ using FType = tvm::NodeFunctor<Expr(const ObjectRef& n, TSelf* self)>;
+
+ public:
+ /*! \brief The packed function to the `VisitExpr(const Expr& expr)` function. */
+ PackedFunc f_visit_expr{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ConstantNode* op)` function. */
+ PackedFunc f_visit_constant_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const TupleNode* op)` function. */
+ PackedFunc f_visit_tuple_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const VarNode* op)` function. */
+ PackedFunc f_visit_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const DataflowVarNode* op)` function. */
+ PackedFunc f_visit_dataflow_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ShapeExprNode* op)` function. */
+ PackedFunc f_visit_shape_expr_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const ExternFuncNode* op)` function. */
+ PackedFunc f_visit_extern_func_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const GlobalVarNode* op)` function. */
+ PackedFunc f_visit_global_var_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const FunctionNode* op)` function. */
+ PackedFunc f_visit_function_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const CallNode* op)` function. */
+ PackedFunc f_visit_call_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const SeqExprNode* op)` function. */
+ PackedFunc f_visit_seq_expr_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const IfNode* op)` function. */
+ PackedFunc f_visit_if_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const OpNode* op)` function. */
+ PackedFunc f_visit_op_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const TupleGetItemNode* op)` function. */
+ PackedFunc f_visit_tuple_getitem_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const PrimValueNode* op)` function. */
+ PackedFunc f_visit_prim_value_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const StringImmNode* op)` function. */
+ PackedFunc f_visit_string_imm_{nullptr};
+ /*! \brief The packed function to the `VisitExpr_(const DataTypeImmNode* op)` function. */
+ PackedFunc f_visit_data_type_imm_{nullptr};
+ /*! \brief The packed function to the `VisitBinding(const Binding& binding)` function. */
+ PackedFunc f_visit_binding{nullptr};
+ /*! \brief The packed function to the `VisitBinding_(const VarBindingNode* binding)`
+ * function. */
+ PackedFunc f_visit_var_binding_{nullptr};
+ /*! \brief The packed function to the `VisitBinding_(const MatchCastNode* binding)`
+ * function. */
+ PackedFunc f_visit_match_cast_{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock(const BindingBlock& block)`
+ * function. */
+ PackedFunc f_visit_binding_block{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock_(const BindingBlockNode* block)`
+ * function. */
+ PackedFunc f_visit_binding_block_{nullptr};
+ /*! \brief The packed function to the `VisitBindingBlock_(const DataflowBlockNode* block)`
+ * function. */
+ PackedFunc f_visit_dataflow_block_{nullptr};
+ /*! \brief The packed function to the `VisitVarDef(const Var& var)` function. */
+ PackedFunc f_visit_var_def{nullptr};
+ /*! \brief The packed function to the `VisitVarDef_(const VarNode* var)` function. */
+ PackedFunc f_visit_var_def_{nullptr};
+ /*! \brief The packed function to the `VisitVarDef_(const DataflowVarNode* var)` function. */
+ PackedFunc f_visit_dataflow_var_def_{nullptr};
+ /*! \brief The packed function to the `VisitSpan(const Span& span)` function. */
+ PackedFunc f_visit_span{nullptr};
+
+ Expr VisitExpr(const Expr& expr) {
+ if (f_visit_expr != nullptr) {
+ return builder_->Normalize(f_visit_expr(expr));
+ } else {
+ static FType vtable = InitVTable();
+ return builder_->Normalize(vtable(expr, this));
+ }
+ }
+
+ void VisitBinding(const Binding& binding) {
+ if (f_visit_binding != nullptr)
+ f_visit_binding(binding);
+ else
+ ExprMutator::VisitBinding(binding);
+ }
+
+ void VisitBinding_(const VarBindingNode* binding) {
+ if (f_visit_var_binding_ != nullptr)
+ f_visit_var_binding_(GetRef<VarBinding>(binding));
+ else
+ ExprMutator::VisitBinding_(binding);
+ }
+
+ void VisitBinding_(const MatchCastNode* binding) {
+ if (f_visit_match_cast_ != nullptr)
+ f_visit_match_cast_(GetRef<MatchCast>(binding));
+ else
+ ExprMutator::VisitBinding_(binding);
+ }
+
+ BindingBlock VisitBindingBlock(const BindingBlock& block)
+ PY_EXPR_MUTATOR_DEFAULT(block, f_visit_binding_block, ExprMutator::VisitBindingBlock(block),
+ BindingBlock);
+
+ BindingBlock VisitBindingBlock_(const BindingBlockNode* block)
+ PY_EXPR_MUTATOR_DEFAULT(GetRef<BindingBlock>(block), f_visit_binding_block_,
+ ExprMutator::VisitBindingBlock_(block), BindingBlock);
+ BindingBlock VisitBindingBlock_(const DataflowBlockNode* block)
+ PY_EXPR_MUTATOR_DEFAULT(GetRef<DataflowBlock>(block), f_visit_dataflow_block_,
+ ExprMutator::VisitBindingBlock_(block), BindingBlock);
+
+ Var VisitVarDef(const Var& var)
+ PY_EXPR_MUTATOR_DEFAULT(var, f_visit_var_def, ExprMutator::VisitVarDef(var), Var);
+ Var VisitVarDef_(const VarNode* var) PY_EXPR_MUTATOR_DEFAULT(GetRef<Var>(var), f_visit_var_def_,
+ ExprMutator::VisitVarDef_(var), Var);
+ Var VisitVarDef_(const DataflowVarNode* var)
+ PY_EXPR_MUTATOR_DEFAULT(GetRef<DataflowVar>(var), f_visit_dataflow_var_def_,
+ ExprMutator::VisitVarDef_(var), Var);
+
+ /*!
+ * \brief Dispatcher for post-order rewrite.
+ * \param expr The Expr to be rewritten.
+ * \return The Expr after post-order rewritten.
+ */
+ Expr VisitExprPostOrder(const Expr& expr) {
+ static FType post_order_vtable = InitPostOrderVTable();
+ return post_order_vtable(expr, this);
+ }
+
+ using ExprMutator::builder_;
+ using ExprMutator::LookupBinding;
+ using ExprMutator::var_remap_;
+ using ExprMutator::VisitWithNewScope;
+ using ExprMutator::WithStructInfo;
+
+ void VisitAttrs(AttrVisitor* v) { v->Visit("builder_", &builder_); }
+ static constexpr const char* _type_key = "expr_functor.PyExprMutator";
+ TVM_DECLARE_BASE_OBJECT_INFO(PyExprMutatorNode, Object);
+
+ private:
+ // initialize the vtable.
+ static FType InitVTable() {
+ FType vtable;
+ // Set dispatch
+ PY_EXPR_MUTATOR_DISPATCH(ConstantNode, f_visit_constant_);
+ PY_EXPR_MUTATOR_DISPATCH(TupleNode, f_visit_tuple_);
+ PY_EXPR_MUTATOR_DISPATCH(VarNode, f_visit_var_);
+ PY_EXPR_MUTATOR_DISPATCH(DataflowVarNode, f_visit_dataflow_var_);
+ PY_EXPR_MUTATOR_DISPATCH(ShapeExprNode, f_visit_shape_expr_);
+ PY_EXPR_MUTATOR_DISPATCH(ExternFuncNode, f_visit_extern_func_);
+ PY_EXPR_MUTATOR_DISPATCH(GlobalVarNode, f_visit_global_var_);
+ PY_EXPR_MUTATOR_DISPATCH(FunctionNode, f_visit_function_);
+ PY_EXPR_MUTATOR_DISPATCH(CallNode, f_visit_call_);
+ PY_EXPR_MUTATOR_DISPATCH(SeqExprNode, f_visit_seq_expr_);
+ PY_EXPR_MUTATOR_DISPATCH(IfNode, f_visit_if_);
+ PY_EXPR_MUTATOR_DISPATCH(OpNode, f_visit_op_);
+ PY_EXPR_MUTATOR_DISPATCH(TupleGetItemNode, f_visit_tuple_getitem_);
+ PY_EXPR_MUTATOR_DISPATCH(PrimValueNode, f_visit_prim_value_);
+ PY_EXPR_MUTATOR_DISPATCH(StringImmNode, f_visit_string_imm_);
+ PY_EXPR_MUTATOR_DISPATCH(DataTypeImmNode, f_visit_data_type_imm_);
+ return vtable;
+ }
+
+ // initialize the vtable for post order visit.
+ static FType InitPostOrderVTable() {
+ FType post_order_vtable;
+ // Set dispatch
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(ConstantNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(TupleNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(VarNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(DataflowVarNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(ShapeExprNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(ExternFuncNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(GlobalVarNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(FunctionNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(CallNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(SeqExprNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(IfNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(OpNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(TupleGetItemNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(PrimValueNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(StringImmNode);
+ PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(DataTypeImmNode);
+ return post_order_vtable;
+ }
+};
+
+TVM_REGISTER_NODE_TYPE(PyExprMutatorNode);
+
+/*!
+ * \brief Managed reference to PyExprMutatorNode.
+ * \sa PyExprMutatorNode
+ */
+class PyExprMutator : public ObjectRef {
+ public:
+ /*!
+ * \brief Create a PyExprMutator with customized methods on the python-side.
+ * \param f_visit_expr The packed function of `VisitExpr(const Expr& expr)`.
+ * \param f_visit_constant_ The packed function of `VisitExpr_(const ConstantNode* op)`.
+ * \param f_visit_tuple_ The packed function of `VisitExpr_(const TupleNode* op)`.
+ * \param f_visit_var_ The packed function of `VisitExpr_(const VarNode* op)`.
+ * \param f_visit_dataflow_var_ The packed function of `VisitExpr_(const DataflowVarNode* op)`.
+ * \param f_visit_shape_expr_ The packed function of `VisitExpr_(const ShapeExprNode* op)`.
+ * \param f_visit_extern_func_ The packed function of `VisitExpr_(const ExternFuncNode* op)`.
+ * \param f_visit_global_var_ The packed function of `VisitExpr_(const GlobalVarNode* op)`.
+ * \param f_visit_function_ The packed function of `VisitExpr_(const FunctionNode* op)`.
+ * \param f_visit_call_ The packed function of `VisitExpr_(const CallNode* op)`.
+ * \param f_visit_seq_expr_ The packed function of `VisitExpr_(const SeqExprNode* op)`.
+ * \param f_visit_if_ The packed function of `VisitExpr_(const IfNode* op)`.
+ * \param f_visit_op_ The packed function of `VisitExpr_(const OpNode* op)`.
+ * \param f_visit_tuple_getitem_ The packed function of `VisitExpr_(const TupleGetItemNode* op)`.
+ * \param f_visit_prim_value_ The packed function of `VisitExpr_(const PrimValueNode* op)`.
+ * \param f_visit_string_imm_ The packed function of `VisitExpr_(const StringImmNode* op)`.
+ * \param f_visit_data_type_imm_ The packed function of `VisitExpr_(const DataTypeImmNode* op)`.
+ * \param f_visit_binding The packed function of `VisitBinding(const Binding& binding)`.
+ * \param f_visit_var_binding_ The packed function of `VisitBinding_(const VarBindingNode*
+ * binding)`.
+ * \param f_visit_match_cast_ The packed function of `VisitBinding_(const MatchCastNode*
+ * binding)`.
+ * \param f_visit_binding_block The packed function of `VisitBindingBlock(const BindingBlock&
+ * block)`.
+ * \param f_visit_binding_block_ The packed function of `VisitBindingBlock_(const
+ * BindingBlockNode* block)`.
+ * \param f_visit_dataflow_block_ The packed function of `VisitBindingBlock_(const
+ * DataflowBlockNode* block)`.
+ * \param f_visit_var_def The packed function of `VisitVarDef(const Var& var)`.
+ * \param f_visit_var_def_ The packed function of `VisitVarDef_(const VarNode* var)`.
+ * \param f_visit_dataflow_var_def_ The packed function of `VisitVarDef_(const DataflowVarNode*
+ * var)`.
+ * \param f_visit_span The packed function of `VisitSpan(const Span& span)`.
+ * \return The PyExprMutator created.
+ */
+ TVM_DLL static PyExprMutator MakePyExprMutator(
+ BlockBuilder builder_, PackedFunc f_visit_expr, PackedFunc f_visit_constant_,
+ PackedFunc f_visit_tuple_, PackedFunc f_visit_var_, PackedFunc f_visit_dataflow_var_,
+ PackedFunc f_visit_shape_expr_, PackedFunc f_visit_extern_func_,
+ PackedFunc f_visit_global_var_, PackedFunc f_visit_function_, PackedFunc f_visit_call_,
+ PackedFunc f_visit_seq_expr_, PackedFunc f_visit_if_, PackedFunc f_visit_op_,
+ PackedFunc f_visit_tuple_getitem_, PackedFunc f_visit_prim_value_,
+ PackedFunc f_visit_string_imm_, PackedFunc f_visit_data_type_imm_, PackedFunc f_visit_binding,
+ PackedFunc f_visit_var_binding_, PackedFunc f_visit_match_cast_,
+ PackedFunc f_visit_binding_block, PackedFunc f_visit_binding_block_,
+ PackedFunc f_visit_dataflow_block_, PackedFunc f_visit_var_def, PackedFunc f_visit_var_def_,
+ PackedFunc f_visit_dataflow_var_def_, PackedFunc f_visit_span) {
+ ObjectPtr<PyExprMutatorNode> n = make_object<PyExprMutatorNode>();
+ n->builder_ = builder_;
+ n->f_visit_expr = f_visit_expr;
+ n->f_visit_constant_ = f_visit_constant_;
+ n->f_visit_tuple_ = f_visit_tuple_;
+ n->f_visit_var_ = f_visit_var_;
+ n->f_visit_dataflow_var_ = f_visit_dataflow_var_;
+ n->f_visit_shape_expr_ = f_visit_shape_expr_;
+ n->f_visit_extern_func_ = f_visit_extern_func_;
+ n->f_visit_global_var_ = f_visit_global_var_;
+ n->f_visit_function_ = f_visit_function_;
+ n->f_visit_call_ = f_visit_call_;
+ n->f_visit_seq_expr_ = f_visit_seq_expr_;
+ n->f_visit_if_ = f_visit_if_;
+ n->f_visit_op_ = f_visit_op_;
+ n->f_visit_tuple_getitem_ = f_visit_tuple_getitem_;
+ n->f_visit_prim_value_ = f_visit_prim_value_;
+ n->f_visit_string_imm_ = f_visit_string_imm_;
+ n->f_visit_data_type_imm_ = f_visit_data_type_imm_;
+ n->f_visit_binding = f_visit_binding;
+ n->f_visit_var_binding_ = f_visit_var_binding_;
+ n->f_visit_match_cast_ = f_visit_match_cast_;
+ n->f_visit_binding_block = f_visit_binding_block;
+ n->f_visit_binding_block_ = f_visit_binding_block_;
+ n->f_visit_dataflow_block_ = f_visit_dataflow_block_;
+ n->f_visit_var_def = f_visit_var_def;
+ n->f_visit_var_def_ = f_visit_var_def_;
+ n->f_visit_dataflow_var_def_ = f_visit_dataflow_var_def_;
+ n->f_visit_span = f_visit_span;
+ return PyExprMutator(n);
+ }
+ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PyExprMutator, ObjectRef, PyExprMutatorNode);
+};
+
+TVM_REGISTER_GLOBAL("relax.MakePyExprVisitor").set_body_typed(PyExprVisitor::MakePyExprVisitor);
+
+TVM_REGISTER_GLOBAL("relax.PyExprVisitorVisitExpr")
+ .set_body_typed([](PyExprVisitor visitor, const Expr& expr) { visitor->VisitExpr(expr); });
+
+TVM_REGISTER_GLOBAL("relax.PyExprVisitorVisitBinding")
+ .set_body_typed([](PyExprVisitor visitor, const Binding& binding) {
+ visitor->VisitBinding(binding);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprVisitorVisitBindingBlock")
+ .set_body_typed([](PyExprVisitor visitor, const BindingBlock& block) {
+ visitor->VisitBindingBlock(block);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprVisitorVisitVarDef")
+ .set_body_typed([](PyExprVisitor visitor, const Var& var) { visitor->VisitVarDef(var); });
+
+TVM_REGISTER_GLOBAL("relax.ExprVisitorVisitExpr")
+ .set_body_typed([](PyExprVisitor visitor, const Expr& expr) {
+ visitor->ExprVisitor::VisitExpr(expr);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprVisitorVisitBinding")
+ .set_body_typed([](PyExprVisitor visitor, const Binding& binding) {
+ visitor->ExprVisitor::VisitBinding(binding);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprVisitorVisitBindingBlock")
+ .set_body_typed([](PyExprVisitor visitor, const BindingBlock& block) {
+ visitor->ExprVisitor::VisitBindingBlock(block);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprVisitorVisitVarDef")
+ .set_body_typed([](PyExprVisitor visitor, const Var& var) {
+ visitor->ExprVisitor::VisitVarDef(var);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprVisitorVisitSpan")
+ .set_body_typed([](PyExprVisitor visitor, const Span& span) {
+ visitor->ExprVisitor::VisitSpan(span);
+ });
+
+TVM_REGISTER_GLOBAL("relax.MakePyExprMutator").set_body_typed(PyExprMutator::MakePyExprMutator);
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitExpr")
+ .set_body_typed([](PyExprMutator mutator, const Expr& expr) {
+ return mutator->VisitExpr(expr);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitBinding")
+ .set_body_typed([](PyExprMutator mutator, const Binding& binding) {
+ mutator->VisitBinding(binding);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitBindingBlock")
+ .set_body_typed([](PyExprMutator mutator, const BindingBlock& block) {
+ return mutator->VisitBindingBlock(block);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitVarDef")
+ .set_body_typed([](PyExprMutator mutator, const Var& var) {
+ return mutator->VisitVarDef(var);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprMutatorVisitExpr")
+ .set_body_typed([](PyExprMutator mutator, const Expr& expr) {
+ return mutator->ExprMutator::VisitExpr(expr);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprMutatorVisitBinding")
+ .set_body_typed([](PyExprMutator mutator, const Binding& binding) {
+ return mutator->ExprMutator::VisitBinding(binding);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprMutatorVisitBindingBlock")
+ .set_body_typed([](PyExprMutator mutator, const BindingBlock& block) {
+ return mutator->ExprMutator::VisitBindingBlock(block);
+ });
+
+TVM_REGISTER_GLOBAL("relax.ExprMutatorVisitVarDef")
+ .set_body_typed([](PyExprMutator mutator, const Var& var) {
+ return mutator->ExprMutator::VisitVarDef(var);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitExprPostOrder")
+ .set_body_typed([](PyExprMutator mutator, const Expr& expr) {
+ return mutator->VisitExprPostOrder(expr);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorVisitWithNewScope")
+ .set_body_typed([](PyExprMutator mutator, const Expr& expr) {
+ return mutator->VisitWithNewScope(expr);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorLookupBinding")
+ .set_body_typed([](PyExprMutator mutator, const Var& var) {
+ return mutator->LookupBinding(var);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorWithStructInfo")
+ .set_body_typed([](PyExprMutator mutator, Var var, StructInfo sinfo) {
+ return mutator->WithStructInfo(var, sinfo);
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorSetVarRemap")
+ .set_body_typed([](PyExprMutator mutator, Id id, Var var) {
+ return mutator->var_remap_[id] = var;
+ });
+
+TVM_REGISTER_GLOBAL("relax.PyExprMutatorGetVarRemap")
+ .set_body_typed([](PyExprMutator mutator, Id id) { return mutator->var_remap_[id]; });
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/op/op.cc b/src/relax/op/op.cc
new file mode 100644
index 0000000000..8640ed79ad
--- /dev/null
+++ b/src/relax/op/op.cc
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/op_attr_types.h>
+#include <tvm/relax/utils.h>
+#include <tvm/relay/op.h>
+
+namespace tvm {
+namespace relax {
+
+// call_tir
+
+StructInfo InferStructInfoCallTIR(const Call& call, const BlockBuilder& ctx) {
+ if (call->sinfo_args.size() != 1) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "sinfo_args should have exact 1 output struct info.");
+ }
+ return call->sinfo_args[0];
+}
+
+RELAY_REGISTER_OP("relax.call_tir")
+ .set_num_inputs(3)
+ .add_argument("func", "Expr", "The destination-passing-style function.")
+ .add_argument("args", "Tuple", "The input arguments.")
+ .add_argument("packed_ints", "Expr",
+ "ShapeExpr representing a tuple of ints to unpack during runtime. Omitted from "
+ "args if unused")
+ .set_attr<FInferStructInfo>("FInferStructInfo", InferStructInfoCallTIR);
+
+Expr MakeCallTIR(Expr func, Tuple args, Array<TensorStructInfo> out_sinfo_list,
+ Optional<Expr> packed_ints) {
+ for (const TensorStructInfo& sinfo : out_sinfo_list) {
+ const auto* shape = sinfo->shape.as<ShapeExprNode>();
+ CHECK(shape != nullptr) << "out_sinfo of call_tir should have defined ShapeExpr as shape. "
+ "However, one given structure info is "
+ << sinfo;
+ }
+
+ StructInfo out_sinfo{nullptr};
+ if (out_sinfo_list.size() == 1) {
+ out_sinfo = out_sinfo_list[0];
+ } else {
+ out_sinfo = TupleStructInfo({out_sinfo_list.begin(), out_sinfo_list.end()});
+ }
+
+ static const Op& op = Op::Get("relax.call_tir");
+ Call call;
+ if (!packed_ints) {
+ // don't use additional optional argument
+ call = Call(op, {func, args}, {}, {out_sinfo});
+ } else {
+ call = Call(op, {func, args, packed_ints.value()}, {}, {out_sinfo});
+ }
+ return call;
+}
+
+TVM_REGISTER_GLOBAL("relax.op.call_tir").set_body_typed(MakeCallTIR);
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/op/op_common.cc b/src/relax/op/op_common.cc
new file mode 100644
index 0000000000..260f71e7bf
--- /dev/null
+++ b/src/relax/op/op_common.cc
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "op_common.h"
+
+#include <algorithm>
+
+namespace tvm {
+namespace relax {
+
+Array<TensorStructInfo> GetInputTensorStructInfo(const Call& call, const BlockBuilder& ctx) {
+ Op op = Downcast<Op>(call->op);
+ int n_input = op->arguments.size();
+ if (static_cast<int>(call->args.size()) != n_input) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << op << " op should have " << n_input << " arguments");
+ }
+ Array<TensorStructInfo> input_tensor_sinfo;
+ input_tensor_sinfo.reserve(n_input);
+ for (int i = 0; i < n_input; ++i) {
+ const auto* sinfo = GetStructInfoAs<TensorStructInfoNode>(call->args[i]);
+ if (sinfo == nullptr) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << op << " requires the input " << op->arguments[i]->name
+ << " to be Tensor. However, the given one is "
+ << call->args[i]->struct_info_->GetTypeKey());
+ }
+ input_tensor_sinfo.push_back(GetRef<TensorStructInfo>(sinfo));
+ }
+ return input_tensor_sinfo;
+}
+
+Optional<Array<PrimExpr>> InferBinaryBroadcastShape(const Call& call, const BlockBuilder& ctx,
+ const Array<PrimExpr>& x1_shape,
+ const Array<PrimExpr>& x2_shape) {
+ arith::Analyzer* analyzer = ctx->GetAnalyzer();
+ int x1_ndim = x1_shape.size();
+ int x2_ndim = x2_shape.size();
+ int max_ndim = std::max(x1_ndim, x2_ndim);
+
+ std::vector<PrimExpr> output_shape;
+ output_shape.reserve(max_ndim);
+
+ int i = 1;
+ for (; i <= std::min(x1_ndim, x2_ndim); ++i) {
+ const PrimExpr& dim0 = x1_shape[x1_ndim - i];
+ const PrimExpr& dim1 = x2_shape[x2_ndim - i];
+ const auto* int_dim0 = dim0.as<IntImmNode>();
+ const auto* int_dim1 = dim1.as<IntImmNode>();
+ if (int_dim0 != nullptr && int_dim0->value == 1) {
+ output_shape.push_back(dim1);
+ } else if (int_dim1 != nullptr && int_dim1->value == 1) {
+ output_shape.push_back(dim0);
+ } else if (analyzer->CanProveEqual(dim0, dim1)) {
+ output_shape.push_back(dim0);
+ } else if (int_dim0 && int_dim1 && int_dim0->value != int_dim1->value) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "In " << call->op << ", the first input shape at dim " << x1_ndim - i
+ << " is " << dim0 << " and the second input shape at dim " << x2_ndim - i
+ << " is " << dim1 << ", which are not broadcastable.");
+ } else {
+ // Use simple fallback when shape mismatch.
+ return NullOpt;
+ }
+ }
+ auto& longer_shape = (x1_ndim > x2_ndim) ? x1_shape : x2_shape;
+ for (; i <= max_ndim; ++i) {
+ output_shape.push_back(longer_shape[max_ndim - i]);
+ }
+ return Array<PrimExpr>(output_shape.rbegin(), output_shape.rend());
+}
+
+std::vector<int> NormalizeAxes(const Call& call, const BlockBuilder& ctx, int ndim,
+ const Array<Integer>& axes) {
+ ICHECK_NE(ndim, kUnknownNDim) << "The ndim is required to be known for this function.";
+ std::vector<bool> appeared_dims_set;
+ std::vector<int> axes_non_neg;
+ appeared_dims_set.resize(ndim, /*value=*/false);
+ axes_non_neg.reserve(axes.size());
+ for (const Integer& axis : axes) {
+ int _axis = axis->value;
+ if (_axis < -ndim || _axis >= ndim) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "In " << call->op << ", the input axis " << _axis
+ << " is out of range. The input tensor has " << ndim
+ << " dimensions, so axis should be in range [" << -ndim << ", " << ndim
+ << ").");
+ } else if (_axis < 0) {
+ _axis = ndim + _axis;
+ }
+
+ if (appeared_dims_set[_axis]) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "In " << call->op
+ << ", the input axes is required to be non-repetitive. However, there are "
+ "multiple given axes referring to axis "
+ << _axis);
+ }
+ appeared_dims_set[_axis] = true;
+ axes_non_neg.push_back(_axis);
+ }
+ return axes_non_neg;
+}
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/op/op_common.h b/src/relax/op/op_common.h
new file mode 100644
index 0000000000..8e362bb4d5
--- /dev/null
+++ b/src/relax/op/op_common.h
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file op_common.h
+ * \brief A set of utilities and common functionality
+ * for Relax ops.
+ */
+#ifndef TVM_RELAX_OP_OP_COMMON_H_
+#define TVM_RELAX_OP_OP_COMMON_H_
+
+#include <tvm/arith/analyzer.h>
+#include <tvm/relax/op_attr_types.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/op.h>
+#include <tvm/tir/data_layout.h>
+
+#include <utility>
+#include <vector>
+
+namespace tvm {
+namespace relax {
+
+/************ Op input struct info getter ************/
+
+/*!
+ * \brief Get the tensor struct info of the operator input.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \return The tensor struct info of each input.
+ * \note This function require every input to be Tensor. The number of call arguments is required
+ * to match the number of inputs of the op being called.
+ */
+Array<TensorStructInfo> GetInputTensorStructInfo(const Call& call, const BlockBuilder& ctx);
+
+/*!
+ * \brief Get the tensor struct info of the unary operator input.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \return The tensor struct info of the unary operator input.
+ * \throw Throw exception if the number of input is not one, or the struct info of the input is not
+ * a tensor struct info.
+ */
+inline TensorStructInfo GetUnaryInputTensorStructInfo(const Call& call, const BlockBuilder& ctx) {
+ return GetInputTensorStructInfo(call, ctx)[0];
+}
+
+/************ Op registration macro ************/
+
+/*!
+ * \brief Quick helper macro to register the operator to registry
+ * \param OpRegName The name of operator to register. The name passed in will
+ * be prepended with a prefix "relax." as the identifier string in the operator registry.
+ */
+#define RELAX_REGISTER_UNARY_OP(OpRegName) \
+ TVM_REGISTER_OP("relax." OpRegName) \
+ .set_num_inputs(1) \
+ .add_argument("x", "Tensor", "The input tensor.")
+
+/*!
+ * \brief Quick helper macro to expose a make-function to construct the operator.
+ * \param OpName The name of the operator as well as the make-function name, which will
+ * be prepended with a prefix "relax.op." as the FFI identifier string for the make function,
+ * \param OpRegName The identifier of the operator in the registry.
+ */
+#define RELAX_UNARY_OP_INTERFACE(OpName, OpRegName) \
+ Expr OpName(Expr x) { \
+ static const Op& op = Op::Get("relax." OpRegName); \
+ return Call(op, {std::move(x)}, Attrs(), {}); \
+ } \
+ TVM_REGISTER_GLOBAL("relax.op." OpRegName).set_body_typed(OpName)
+
+/************ Utilities ************/
+
+/*!
+ * \brief Infer the struct info for unary elementwise ops.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param f_compute_out_dtype The function to compute the output dtype, with
+ * signature DataType f_compute_out_dtype(const TensorStructInfo& input_sinfo).
+ * \tparam require_float_dtype whether this op requires the input dtype to be float
+ * \tparam Ftype the type of f_compute_out_dtype
+ * \return The inferred struct info.
+ */
+template <bool require_float_dtype, typename FType>
+inline StructInfo InferStructInfoUnary(const Call& call, const BlockBuilder& ctx,
+ FType f_compute_out_dtype) {
+ TensorStructInfo input_sinfo = GetUnaryInputTensorStructInfo(call, ctx);
+ if (require_float_dtype && !input_sinfo->IsUnknownDtype() && !input_sinfo->dtype.is_float()) {
+ ctx->ReportFatal(
+ Diagnostic::Error(call->span)
+ << call->op
+ << " requires the input tensor to have float dtype. However, the given input dtype is "
+ << input_sinfo->dtype);
+ }
+ auto output_sinfo = make_object<TensorStructInfoNode>(*input_sinfo.get());
+ output_sinfo->dtype = f_compute_out_dtype(input_sinfo);
+ return TensorStructInfo(output_sinfo);
+}
+
+/*!
+ * \brief Infer the struct info for unary arithmetic elementwise ops. It's also
+ * used in some NN operators.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \tparam require_float_dtype whether this op requires the input dtype to be float
+ * \return The inferred struct info.
+ */
+template <bool require_float_dtype>
+StructInfo InferStructInfoUnaryArith(const Call& call, const BlockBuilder& ctx) {
+ return InferStructInfoUnary<require_float_dtype>(
+ call, ctx, [](const TensorStructInfo& input_sinfo) { return input_sinfo->dtype; });
+}
+
+/************ Utilities ************/
+
+/*!
+ * \brief Infer the output datatype for binary arithmetic operators.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param x1_sinfo The struct info of the first operand
+ * \param x2_sinfo The struct info of the second operand
+ * \return The inferred output dtype.
+ * \throw Throw exception if the dtype of two input TensorStructInfo don’t match
+ */
+inline DataType InferBinaryArithOpOutDtype(const Call& call, const BlockBuilder& ctx,
+ const TensorStructInfo& x1_sinfo,
+ const TensorStructInfo& x2_sinfo) {
+ if (x1_sinfo->IsUnknownDtype() || x2_sinfo->IsUnknownDtype()) {
+ return DataType::Void();
+ } else if (x1_sinfo->dtype != x2_sinfo->dtype) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "Data types " << x1_sinfo->dtype << " and " << x2_sinfo->dtype
+ << " must be equal for binary operators");
+ }
+ return x1_sinfo->dtype;
+}
+
+/*!
+ * \brief Infer the output shape for binary broadcast operators.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param x1_shape The shape of the first operand.
+ * \param x2_shape The shape of the second operand.
+ * \return The inferred output shape after broadcasting. Or `NullOpt` if the output shape cannot be
+ * determined due to symbolic broadcast.
+ */
+Optional<Array<PrimExpr>> InferBinaryBroadcastShape(const Call& call, const BlockBuilder& ctx,
+ const Array<PrimExpr>& x1_shape,
+ const Array<PrimExpr>& x2_shape);
+
+/*!
+ * \brief Convert all axes to non-negative indices, and meanwhile check if the given array of axes
+ * are all in range and non-repetitive with regards to the given ndim.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param ndim The ndim constraint, which is required to be known already.
+ * \param axes The axis indices to be checked
+ * \return The input axes in non-negative indexing.
+ * \throw Throw exception if there exists out-of-range axis index or repetitive indices.
+ */
+std::vector<int> NormalizeAxes(const Call& call, const BlockBuilder& ctx, int ndim,
+ const Array<Integer>& axes);
+
+/*!
+ * \brief Convert the given axis to non-negative index. Meanwhile check if the axis is in range
+ * with regards to the given ndim.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param ndim The ndim constraint.
+ * \param axis The axis index to be checked
+ * \return The input axis in non-negative indexing.
+ * \throw Throw exception the given axis is out-of-range.
+ */
+inline int NormalizeAxis(const Call& call, const BlockBuilder& ctx, int ndim, int axis) {
+ return NormalizeAxes(call, ctx, ndim, {axis})[0];
+}
+
+/*!
+ * \brief Convert an array of integers to int64 dtype.
+ * \param int_imms The input IntImms to be converted.
+ * \return The conversion result, where every IntImm has dtype int64
+ */
+inline Array<IntImm> ConvertIntImmToInt64(const Array<IntImm>& int_imms) {
+ return int_imms.Map([](const IntImm& i) { return Downcast<IntImm>(cast(DataType::Int(64), i)); });
+}
+
+/************ Utilities for NN operators ************/
+
+/*!
+ * \brief Complete the padding to a 4-length array.
+ * - If the padding length is 1, the same padding is used on all top/left/bottom/right sides
+ * - If the padding length is 2, top/bottom sides use padding[0] and left/right use padding[1]
+ * - If the padding length is 4, padding is in the order of (top, left, bottom, right)
+ * \param padding The given padding to be completed
+ * \return The completed padding.
+ * \throws Throws error if the input padding length is neither 1, 2 or 4.
+ */
+inline Array<IntImm> GetCompletePadding2D(Array<IntImm> padding) {
+ if (padding.size() == 1) {
+ return {padding[0], padding[0], padding[0], padding[0]};
+ } else if (padding.size() == 2) {
+ return {padding[0], padding[1], padding[0], padding[1]};
+ } else if (padding.size() == 4) {
+ return padding;
+ }
+ LOG(FATAL) << "The input padding length is expected to be either 1, 2 or 4. However, the given "
+ "padding is "
+ << padding;
+ throw;
+}
+
+/*!
+ * \brief Check if the given tensor layout can be converted to the given target layout.
+ * If convertible, return the tensor layout and the bijective conversion in tir::Layout and
+ * tir::BijectiveLayout accordingly.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param tensor_layout The tensor layout to be checked
+ * \param tgt_layout The target layout to be matched
+ * \param tensor_name The name of the input tensor
+ * \return The tensor layout and the bijective conversion in tir::Layout and tir::BijectiveLayout
+ * accordingly.
+ */
+inline std::pair<tir::Layout, tir::BijectiveLayout> CheckTensorLayout(const Call& call,
+ const BlockBuilder& ctx,
+ const String& tensor_layout,
+ const String& tgt_layout,
+ const String& tensor_name) {
+ tir::Layout _tensor_layout(tensor_layout, DataType::Int(64));
+ tir::BijectiveLayout tensor2tgt(_tensor_layout, tir::Layout(tgt_layout, DataType::Int(64)));
+ if (!tensor2tgt.defined()) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << call->op << " requires the given " << tensor_name
+ << " layout to be convertible from " << tgt_layout
+ << " layout. However, the given layout " << tensor_layout
+ << " is not convertible.");
+ }
+ return {_tensor_layout, tensor2tgt};
+}
+
+/*!
+ * \brief Check if the given tensor struct info has expected ndim per the given layout (or the ndim
+ * is unknown), and try to cast the shape to ShapeExpr.
+ * \param call The context Call to the operator.
+ * \param ctx The error reporting context.
+ * \param sinfo The input tensor struct info to be checked.
+ * \param layout The layout that the given tensor is expected to have.
+ * \return The shape of the input tensor in ShapeExpr, or `NullOpt` if the shape is unknown.
+ */
+inline Optional<ShapeExpr> CheckNdimPerLayoutAndGetShape(const Call& call, const BlockBuilder& ctx,
+ const TensorStructInfo& sinfo,
+ const tir::Layout& layout) {
+ if (!sinfo->IsUnknownNdim() && sinfo->ndim != static_cast<int>(layout.ndim())) {
+ ctx->ReportFatal(Diagnostic::Error(call->span)
+ << "In " << call->op << ", layout " << layout << " requires the input to be "
+ << layout.ndim() << "-dim tensor. However, the given input has ndim "
+ << sinfo->ndim);
+ }
+ if (const auto* shape_expr = sinfo->shape.as<ShapeExprNode>()) {
+ return GetRef<ShapeExpr>(shape_expr);
+ }
+ return NullOpt;
+}
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_OP_OP_COMMON_H_
diff --git a/src/relax/op/tensor/binary.cc b/src/relax/op/tensor/binary.cc
new file mode 100644
index 0000000000..dd61091f7a
--- /dev/null
+++ b/src/relax/op/tensor/binary.cc
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file binary.cc
+ * \brief binary broadcast operators.
+ */
+
+#include "binary.h"
+
+#include <algorithm>
+
+namespace tvm {
+namespace relax {
+
+template <typename FType>
+StructInfo InferStructInfoBroadcast(const Call& call, const BlockBuilder& ctx,
+ FType f_compute_out_dtype) {
+ Array<TensorStructInfo> input_sinfo = GetInputTensorStructInfo(call, ctx);
+ TensorStructInfo x1_sinfo = input_sinfo[0];
+ TensorStructInfo x2_sinfo = input_sinfo[1];
+
+ // DateType
+ DataType output_dtype = f_compute_out_dtype(call, ctx, x1_sinfo, x2_sinfo);
+
+ // ndims
+ int output_ndim;
+ if (x1_sinfo->IsUnknownNdim() || x2_sinfo->IsUnknownNdim()) {
+ output_ndim = kUnknownNDim;
+ } else {
+ output_ndim = std::max(x1_sinfo->ndim, x2_sinfo->ndim);
+ }
+
+ const auto* x1_shape = x1_sinfo->shape.as<ShapeExprNode>();
+ const auto* x2_shape = x2_sinfo->shape.as<ShapeExprNode>();
+ // Shapes and ndims
+ if (x1_shape && x2_shape) {
+ // If all inputs have shapes, directly infer shapes
+ Optional<Array<PrimExpr>> output_shape =
+ InferBinaryBroadcastShape(call, ctx, x1_shape->values, x2_shape->values);
+ if (!output_shape.defined()) {
+ return TensorStructInfo(output_dtype, /*ndim=*/output_ndim);
+ } else {
+ ICHECK_EQ(static_cast<int>(output_shape.value().size()), output_ndim);
+ return TensorStructInfo(ShapeExpr(output_shape.value()), output_dtype);
+ }
+ } else if (x1_sinfo->shape.defined() && x1_sinfo->shape.same_as(x2_sinfo->shape)) {
+ return TensorStructInfo(x1_sinfo->shape.value(), output_dtype);
+ } else {
+ return TensorStructInfo(output_dtype, /*ndim=*/output_ndim);
+ }
+}
+
+StructInfo InferStructInfoBroadcastArith(const Call& call, const BlockBuilder& ctx) {
+ return InferStructInfoBroadcast(call, ctx, InferBinaryArithOpOutDtype);
+}
+
+StructInfo InferStructInfoBroadcastCMP(const Call& call, const BlockBuilder& ctx) {
+ return InferStructInfoBroadcast(
+ call, ctx,
+ [](const Call& call, const BlockBuilder& ctx, const TensorStructInfo& x1_sinfo,
+ const TensorStructInfo& x2_sinfo) { return DataType::Bool(); });
+}
+
+/***************** Arithmetic operators *****************/
+
+RELAX_REGISTER_BINARY_BROADCAST_OP_AND_IMPL(add);
+RELAX_REGISTER_BINARY_BROADCAST_OP_AND_IMPL(multiply);
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/op/tensor/binary.h b/src/relax/op/tensor/binary.h
new file mode 100644
index 0000000000..a7aea576b6
--- /dev/null
+++ b/src/relax/op/tensor/binary.h
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file binary.h
+ * \brief The functions to make Relax binary arithmetic and comparison operator calls.
+ */
+#ifndef TVM_RELAX_OP_TENSOR_BINARY_H_
+#define TVM_RELAX_OP_TENSOR_BINARY_H_
+
+#include "../op_common.h"
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief Quick helper macro
+ * - Expose a make function to construct the node.
+ * - Register op to the registry.
+ * \param OpName The name of operator to register. The name passed in will
+ * 1. be prepended with a prefix "relax.op." as the FFI identifier string for the make function,
+ * 2. be prepended with a prefix "relax." as the identifier string in the operator registry.
+ */
+#define RELAX_REGISTER_BINARY_OP_AND_IMPL(OpName) \
+ Expr OpName(Expr x1, Expr x2) { \
+ static const Op& op = Op::Get("relax." #OpName); \
+ return Call(op, {x1, x2}, Attrs(), {}); \
+ } \
+ TVM_REGISTER_GLOBAL("relax.op." #OpName).set_body_typed(OpName); \
+ TVM_REGISTER_OP("relax." #OpName) \
+ .set_num_inputs(2) \
+ .add_argument("x1", "Tensor", "The first input tensor.") \
+ .add_argument("x2", "Tensor", "The second input tensor.")
+
+#define RELAX_REGISTER_BINARY_BROADCAST_OP_AND_IMPL(OpName) \
+ RELAX_REGISTER_BINARY_OP_AND_IMPL(OpName).set_attr<FInferStructInfo>( \
+ "FInferStructInfo", InferStructInfoBroadcastArith)
+
+#define RELAX_REGISTER_CMP_OP_AND_IMPL(OpName) \
+ RELAX_REGISTER_BINARY_OP_AND_IMPL(OpName).set_attr<FInferStructInfo>( \
+ "FInferStructInfo", InferStructInfoBroadcastCMP)
+
+/***************** Arithmetic operators *****************/
+
+/*! \brief Addition with numpy-style broadcasting. */
+Expr add(Expr x1, Expr x2);
+
+/*! \brief Multiplication with numpy-style broadcasting. */
+Expr multiply(Expr x1, Expr x2);
+
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_OP_TENSOR_BINARY_H_
diff --git a/src/relax/utils.cc b/src/relax/utils.cc
new file mode 100644
index 0000000000..5846f8116d
--- /dev/null
+++ b/src/relax/utils.cc
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tvm/relax/expr_functor.h>
+
+namespace tvm {
+namespace relax {
+
+bool IsBoolScalarType(const Type& ty, bool permit_unknown_rank, bool permit_unknown_dtype) {
+ const DynTensorTypeNode* tt = ty.as<DynTensorTypeNode>();
+ if (!tt) {
+ return false;
+ }
+ bool correct_dtype = tt->dtype.is_bool() || (permit_unknown_dtype && tt->dtype.is_void());
+ bool correct_rank = tt->ndim == 0 || (permit_unknown_rank && tt->ndim == -1);
+ return correct_dtype && correct_rank;
+}
+
+bool IsLeafOrTuple(const Expr& expr) {
+ return expr.as<LeafExprNode>() || expr.as<GlobalVarNode>() || expr.as<ExternFuncNode>() ||
+ expr.as<OpNode>() || expr.as<TupleNode>();
+}
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/te/operation/create_primfunc.cc b/src/te/operation/create_primfunc.cc
index bdcdbc023a..cca8b9e433 100644
--- a/src/te/operation/create_primfunc.cc
+++ b/src/te/operation/create_primfunc.cc
@@ -581,5 +581,85 @@ TVM_REGISTER_GLOBAL("te.CreatePrimFunc").set_body([](TVMArgs args, TVMRetValue*
*ret = CreatePrimFunc(arg_list, index_dtype_override);
});
+// Relax version impl
+PrimFunc GenerateAndCompletePrimFunc(const Array<te::Tensor>& arg_list,
+ const Array<Stmt>& root_stmts, CreateFuncInfo* info,
+ const Optional<Array<tir::Var>> tir_var_list) {
+ Array<Var> parameters;
+ Map<Var, Buffer> buffer_map;
+ for (const te::Tensor& tensor : arg_list) {
+ Var arg("var_" + tensor->GetNameHint(), PrimType(DataType::Handle()));
+ parameters.push_back(arg);
+ auto it = info->tensor2buffers.find(tensor);
+ ICHECK(it != info->tensor2buffers.end());
+ buffer_map.Set(arg, it->second);
+ }
+
+ // add additional arguments for tir vars that are left unbound by match buffer
+ if (tir_var_list) {
+ for (const Var& v : tir_var_list.value()) {
+ parameters.push_back(v);
+ }
+ }
+
+ PrimFunc func = WithAttrs(PrimFunc(/*params=*/std::move(parameters),
+ /*body=*/SeqStmt::Flatten(root_stmts),
+ /*ret_type=*/VoidType(),
+ /*buffer_map=*/std::move(buffer_map)),
+ {{"global_symbol", String("main")}, {"tir.noalias", Bool(true)}});
+
+ const auto* complete = runtime::Registry::Get("script.Complete");
+ ICHECK(complete);
+ func = (*complete)(std::move(func), info->root_alloc);
+ return func;
+}
+
+PrimFunc CreatePrimFuncWithConstants(const Array<te::Tensor>& arg_list,
+ const Array<runtime::NDArray>& constants,
+ const Optional<Array<tir::Var>>& tir_var_list,
+ std::optional<DataType> index_dtype_override) {
+ // Infomations used in CreatePrimFunc and its sub-functions.
+ CreateFuncInfo info(arg_list);
+ // Root body stmts.
+ Array<Stmt> root_stmts;
+ // Analyzer
+ arith::Analyzer analyzer;
+
+ // Step 1. Create ordered array of operations and validate they are supported.
+ Array<te::Operation> order = CollectOrderedOps(arg_list);
+
+ // Step 2. Initialize buffer binds map
+ InitializeBufferBinds(order, &info);
+
+ // Step 3. Rewrite compute stages into blocks.
+ for (const te::Operation& op : order) {
+ RewriteStageToBlock(op, &info, &root_stmts, &analyzer);
+ }
+ auto func = GenerateAndCompletePrimFunc(arg_list, root_stmts, &info, tir_var_list);
+ func = tir::BindParams(func, constants);
+ if (index_dtype_override.has_value()) {
+ func = IndexDataTypeNormalizer(index_dtype_override.value()).Rewrite(std::move(func));
+ }
+ auto result = LayoutFreePlaceholdersNormalizer().Process(std::move(func));
+ return result;
+}
+
+PrimFunc CreatePrimFunc(const Array<te::Tensor>& arg_list,
+ const Optional<Array<tir::Var>> tir_var_list,
+ std::optional<DataType> index_dtype_override) {
+ return CreatePrimFuncWithConstants(arg_list, {}, tir_var_list, index_dtype_override);
+}
+
+TVM_REGISTER_GLOBAL("te.CreateRelaxPrimFunc").set_body([](TVMArgs args, TVMRetValue* ret) {
+ Array<te::Tensor> arg_list = args[0];
+ Optional<Array<tir::Var>> tir_var_list = args[1];
+ std::optional<DataType> index_dtype_override{std::nullopt};
+ // Add conversion to make std::optional compatible with FFI.
+ if (args[2].type_code() != kTVMNullptr) {
+ index_dtype_override = args[2].operator DataType();
+ }
+ *ret = CreatePrimFunc(arg_list, tir_var_list, index_dtype_override);
+});
+
} // namespace tir
} // namespace tvm
diff --git a/src/te/operation/create_primfunc.h b/src/te/operation/create_primfunc.h
index 4246347a16..946f024849 100644
--- a/src/te/operation/create_primfunc.h
+++ b/src/te/operation/create_primfunc.h
@@ -42,6 +42,23 @@ PrimFunc CreatePrimFuncWithConstants(const Array<te::Tensor>& arg_list,
const Array<runtime::NDArray>& constants,
std::optional<DataType> index_dtype_override = std::nullopt);
+// Relax version
+// TODO(relax-team) combine with the relay version
+/*! \brief Use Tensor Expression to create a schedulable TensorIR func. */
+PrimFunc CreatePrimFunc(const Array<te::Tensor>& arg_list,
+ const Optional<Array<tir::Var>> tir_var_list,
+ std::optional<DataType> index_dtype_override);
+
+/*! \brief The same as above but create a PrimFunc with AllocateConstNode. If the size of the
+ * constants array is N, the last N tensors in arg_list will be treated as constant tensors.
+ * Constant tensors will not be part of the parameters of the created PrimFunc, instead constants
+ * will be embedded in the body as AllocateConstNode.
+ */
+PrimFunc CreatePrimFuncWithConstants(const Array<te::Tensor>& arg_list,
+ const Array<runtime::NDArray>& constants,
+ const Optional<Array<tir::Var>>& tir_var_list,
+ std::optional<DataType> index_dtype_override = std::nullopt);
+
} // namespace tir
} // namespace tvm
diff --git a/tests/python/relax/test_analysis_struct_info_analysis.py b/tests/python/relax/test_analysis_struct_info_analysis.py
index faf8fedcf4..03b98f8a56 100644
--- a/tests/python/relax/test_analysis_struct_info_analysis.py
+++ b/tests/python/relax/test_analysis_struct_info_analysis.py
@@ -319,6 +319,149 @@ def test_base_check():
assert fopaque.is_base_of(fn_info_shape(1))
+def _check_derive(ctx, finfo, args_sinfo, ret):
+ gv = rx.GlobalVar("test")
+ rx.expr._update_struct_info(gv, finfo)
+ args = []
+ for i, sinfo in enumerate(args_sinfo):
+ arg = rx.Var("arg%i" % i, sinfo)
+ args.append(arg)
+ call = rx.Call(gv, args)
+ derived_ret = rx.analysis.derive_call_ret_struct_info(finfo, call, ctx)
+ tvm.ir.assert_structural_equal(ret, derived_ret)
+
+
+def test_derive_call_ret_struct_info():
+ obj0 = rx.ObjectStructInfo()
+ prim0 = rx.PrimStructInfo("float32")
+
+ n, m = tir.Var("n0", "int64"), tir.Var("m0", "int64")
+ bb = rx.BlockBuilder()
+ # derivation cases
+ with bb.testing_scope(def_vars=[n, m]):
+
+ def func0(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([n, m], "float32")
+ z = rx.TensorStructInfo([m + c, n], "float32")
+ return rx.FuncStructInfo([x], z)
+
+ # Tensor => Tensor
+ _check_derive(
+ bb,
+ func0(1),
+ [rx.TensorStructInfo([10, 11], "float32")],
+ rx.TensorStructInfo([12, 10], "float32"),
+ )
+
+ _check_derive(
+ bb,
+ func0(2),
+ [rx.TensorStructInfo([n, m], "float32")],
+ rx.TensorStructInfo([m + 2, n], "float32"),
+ )
+
+ # passing in information that cannot deduce n, m
+ # it is still OK as type still matches, return an
+ # eriased output
+ _check_derive(
+ bb,
+ func0(2),
+ [rx.TensorStructInfo(ndim=2, dtype="float32")],
+ rx.TensorStructInfo(ndim=2, dtype="float32"),
+ )
+
+ # Error: wrong number of arguments
+ with pytest.raises(TVMError):
+ _check_derive(
+ bb,
+ func0(2),
+ [rx.TensorStructInfo(ndim=2, dtype="float32"), obj0],
+ rx.TensorStructInfo(ndim=2, dtype="float32"),
+ )
+
+ # Error:type mismatch
+ with pytest.raises(TVMError):
+ _check_derive(bb, func0(2), [obj0], obj0)
+
+ # opaque derivation
+ fopaque0 = lambda: rx.FuncStructInfo.opaque_func()
+ fopaque1 = lambda: rx.FuncStructInfo.opaque_func(ret=prim0)
+ _check_derive(bb, fopaque0(), [obj0, prim0], obj0)
+ _check_derive(bb, fopaque1(), [obj0, prim0], prim0)
+
+ # recursive tuple derivation
+ def func_tuple0(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x0 = rx.TensorStructInfo([n, c], "float32")
+ x1 = rx.TensorStructInfo([n + c, m], "float32")
+ z = rx.TupleStructInfo([rx.TensorStructInfo([m, n], "float32")])
+ return rx.FuncStructInfo([rx.TupleStructInfo([x0, x1])], z)
+
+ _check_derive(
+ bb,
+ func_tuple0(2),
+ [
+ rx.TupleStructInfo(
+ [
+ rx.TensorStructInfo([n, 2], "float32"),
+ rx.TensorStructInfo([n + 2, 10], "float32"),
+ ]
+ )
+ ],
+ rx.TupleStructInfo([rx.TensorStructInfo([10, n], "float32")]),
+ )
+
+ def func_tuple1(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x0 = rx.TensorStructInfo([n, m], "float32")
+ x1 = rx.TensorStructInfo([n + c, c], "float32")
+ z = rx.TupleStructInfo([rx.TensorStructInfo([m, n], "float32")])
+ return rx.FuncStructInfo([rx.TupleStructInfo([x0, x1])], z)
+
+ # Still OK, to pass erased tensor into n+2, n is captured by other argument.
+ _check_derive(
+ bb,
+ func_tuple1(4),
+ [
+ rx.TupleStructInfo(
+ [
+ rx.TensorStructInfo([n, 4], "float32"),
+ rx.TensorStructInfo(ndim=2, dtype="float32"),
+ ]
+ )
+ ],
+ rx.TupleStructInfo([rx.TensorStructInfo([4, n], "float32")]),
+ )
+
+ # tuple length mismatch is not causes an error
+ with pytest.raises(TVMError):
+ _check_derive(
+ bb,
+ func_tuple0(4),
+ [rx.TupleStructInfo([rx.TensorStructInfo([n, 4], "float32")])],
+ rx.TupleStructInfo([rx.TensorStructInfo([10, n], "float32")]),
+ )
+
+ # mixed shape types
+ def func_shape_mixed(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x0 = rx.ShapeStructInfo([n, m])
+ f0 = func_tuple0(c)
+ z = rx.ShapeStructInfo([m + n, c])
+ return rx.FuncStructInfo([x0, f0], z)
+
+ _check_derive(
+ bb,
+ func_shape_mixed(3),
+ [
+ rx.ShapeStructInfo([10, 20]),
+ rx.FuncStructInfo.opaque_func(ret=rx.ShapeStructInfo(ndim=2)),
+ ],
+ rx.ShapeStructInfo([30, 3]),
+ )
+
+
def _check_lca(lhs, rhs, target):
tvm.ir.assert_structural_equal(rx.analysis.struct_info_lca(lhs, rhs), target)
tvm.ir.assert_structural_equal(rx.analysis.struct_info_lca(rhs, lhs), target)
diff --git a/tests/python/relax/test_blockbuilder.py b/tests/python/relax/test_blockbuilder.py
new file mode 100644
index 0000000000..36a22f9712
--- /dev/null
+++ b/tests/python/relax/test_blockbuilder.py
@@ -0,0 +1,542 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+import tvm
+import tvm.testing
+
+from tvm import te, tir, topi
+from tvm import relax as rx, relay
+from tvm.ir.base import assert_structural_equal
+from tvm.relax import ExternFunc
+from tvm.tir.function import PrimFunc
+
+
+@tvm.register_func("test.blockbuilder.nop")
+def nop():
+ pass
+
+
+def test_block_builder():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ bb._begin_binding_block()
+ gv0 = bb.emit(rx.op.add(x, y))
+ bb._begin_dataflow_block()
+ lv0 = bb.emit(rx.op.multiply(gv0, y))
+ gv1 = bb.emit_output(rx.op.multiply(lv0, lv0))
+ b0 = bb._end_block()
+ bb._begin_dataflow_block()
+ lv1 = bb.emit(rx.op.multiply(gv0, y))
+ gv2 = bb.emit_output(rx.op.multiply(lv1, lv1))
+ b1 = bb._end_block()
+ gv3 = bb.emit(rx.op.add(x, y))
+ b2 = bb._end_block()
+
+ assert isinstance(b0, rx.DataflowBlock)
+ assert isinstance(b1, rx.DataflowBlock)
+ assert not isinstance(b2, rx.DataflowBlock)
+
+
+def test_function_single_block():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with bb.function("func", [x, y]):
+ with bb.dataflow():
+ lv0 = bb.emit(rx.op.add(x, y))
+ assert lv0.name_hint == "lv"
+ lv1 = bb.emit(rx.op.multiply(lv0, y))
+ assert lv1.name_hint == "lv1"
+ gv0 = bb.emit_output(lv1)
+ assert gv0.name_hint == "gv"
+ bb.emit_func_output(gv0)
+
+ func = bb.get()["func"]
+ assert func.params[0] == x
+ assert func.params[1] == y
+ assert func.body.body == gv0
+ assert_structural_equal(gv0.struct_info, rx.TensorStructInfo([m, n], "float16"))
+ assert len(func.body.blocks) == 1
+ assert len(func.body.blocks[0].bindings) == 3
+
+
+def test_function_multi_blocks():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with bb.function("func", [x, y]):
+ with bb.dataflow():
+ lv0 = bb.emit(rx.op.add(x, y))
+ assert lv0.name_hint == "lv"
+ gv0 = bb.emit_output(lv0)
+ assert gv0.name_hint == "gv"
+ gv1 = bb.emit(rx.op.add(gv0, gv0))
+ assert gv1.name_hint == "gv1"
+ with bb.dataflow():
+ lv1 = bb.emit(rx.op.add(gv1, gv1))
+ assert lv1.name_hint == "lv1"
+ gv2 = bb.emit_output(gv1)
+ bb.emit_func_output(gv2)
+
+ func = bb.get()["func"]
+
+ assert_structural_equal(gv2.struct_info, rx.TensorStructInfo([m, n], "float16"))
+ assert func.params[0] == x
+ assert func.params[1] == y
+ assert func.body.body == gv2
+ assert len(func.body.blocks) == 3
+ assert len(func.body.blocks[0].bindings) == 2
+ assert len(func.body.blocks[1].bindings) == 1
+ assert len(func.body.blocks[2].bindings) == 2
+
+
+def test_multi_functions():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with bb.function("func1", [x, y]):
+ with bb.dataflow():
+ lv0 = bb.emit(rx.op.add(x, y))
+ assert lv0.name_hint == "lv"
+ gv0 = bb.emit_output(lv0)
+ bb.emit_func_output(gv0)
+
+ with bb.function("func2", [x, y]):
+ with bb.dataflow():
+ lv0 = bb.emit(rx.op.add(y, x))
+ # TODO(@yuchen): enable block builder to reset local var unique name map
+ assert lv0.name_hint == "lv1"
+ gv0 = bb.emit_output(lv0)
+ bb.emit_func_output(gv0)
+
+ mod = bb.get()
+ func1 = mod["func1"]
+ assert func1.params[0] == x
+ assert func1.params[1] == y
+ assert len(func1.body.blocks) == 1
+ func2 = mod["func2"]
+ assert func2.params[0] == x
+ assert func2.params[1] == y
+ assert len(func2.body.blocks) == 1
+
+
+def test_binary_shape_type_deduction():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ k = tir.Var("k", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, 1], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ z = rx.Var("z", rx.TensorStructInfo([5], "float16"))
+ w = rx.Var("w", rx.TensorStructInfo([k], "float16"))
+ bb = rx.BlockBuilder()
+
+ with bb.function("func", [x, y, z, w]):
+ with bb.dataflow():
+ lv0 = bb.emit(rx.op.add(x, y))
+ assert_structural_equal(lv0.struct_info, rx.TensorStructInfo([m, n], "float16"))
+
+ lv1 = bb.emit(rx.op.multiply(x, z))
+ assert_structural_equal(lv1.struct_info, rx.TensorStructInfo([m, 5], "float16"))
+
+ lv2 = bb.emit(rx.op.multiply(z, w))
+ assert isinstance(lv2.struct_info, rx.TensorStructInfo)
+ assert lv2.struct_info.ndim == 1
+ assert lv2.struct_info.dtype == "float16"
+
+ lv3 = bb.emit(rx.op.multiply(y, w))
+ assert isinstance(lv3.struct_info, rx.TensorStructInfo)
+ assert lv3.struct_info.ndim == 1
+ assert lv3.struct_info.dtype == "float16"
+
+ gv0 = bb.emit_output(lv3)
+ bb.emit_func_output(gv0)
+
+ assert isinstance(gv0.checked_type, rx.DynTensorType)
+ assert gv0.checked_type.ndim == 1
+ assert gv0.checked_type.dtype == "float16"
+
+
+def test_emit_match_cast():
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ x = rx.Var("tensor_value", rx.TensorStructInfo(dtype="float32", ndim=-1))
+ y = rx.Var("shape_value", rx.ShapeStructInfo([16, 8]))
+ bb = rx.BlockBuilder()
+
+ with bb.function("func", [x, y]):
+ with bb.dataflow():
+ # lv0: Tensor((m, n), "float32") =
+ # match_cast(x: Tensor(_, "float32"], [m, n))
+ lv0 = bb.match_cast(x, rx.TensorStructInfo([m, n], "float32"))
+ assert isinstance(lv0, rx.DataflowVar)
+ assert_structural_equal(lv0.struct_info, rx.TensorStructInfo([m, n], "float32"))
+
+ # lv1: Shape = match_cast(shape, rx.ShapeStructInfo([m, n]))
+ lv1 = bb.match_cast(y, rx.ShapeStructInfo([m, n]))
+ assert lv1.struct_info == rx.ShapeStructInfo([m, n])
+ gv0 = bb.emit_output(lv1)
+
+ bb.emit_func_output(gv0)
+ func = bb.get()["func"]
+ block = func.body.blocks[0]
+ b0, b1 = block.bindings[:2]
+ assert isinstance(b0, rx.MatchCast)
+ assert isinstance(b1, rx.MatchCast)
+
+ assert b0.value == x
+ assert b0.struct_info == rx.TensorStructInfo([m, n], "float32")
+ assert b0.var == lv0
+
+ assert b1.value == y
+ assert b1.struct_info == rx.ShapeStructInfo([m, n])
+ assert b1.var == lv1
+
+
+def test_emit_match_cast_binding_in_dataflow_block():
+ bb = rx.BlockBuilder()
+
+ x = rx.Var("x", rx.TensorStructInfo(dtype="float32", ndim=-1))
+ m = tir.Var("m", dtype="int64")
+ gv = rx.Var("gv", rx.TensorStructInfo(dtype="float32", ndim=-1))
+ match_cast = rx.MatchCast(gv, x, rx.TensorStructInfo((m,), "float32"))
+
+ with bb.function("main", [x]):
+ with bb.dataflow():
+ bb.emit_normalized(match_cast)
+ bb.emit_output(gv)
+ bb.emit_func_output(x)
+
+ func = bb.get()["main"]
+ block = func.body.blocks[0]
+ b0 = block.bindings[0]
+ assert isinstance(b0, rx.MatchCast)
+
+ assert b0.value == x
+ assert isinstance(b0.struct_info, rx.TensorStructInfo)
+ assert b0.struct_info.shape[0] == m
+ assert b0.var == gv
+
+
+def test_normalize():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ # Call node
+ add_call = rx.op.multiply(x, y)
+
+ bb.normalize(add_call)
+ shape = rx.get_shape_of(add_call)
+
+ assert isinstance(shape, rx.ShapeExpr)
+ assert shape[0] == m
+ assert shape[1] == n
+
+ # Tuple node
+ tuple_1 = rx.Tuple([x, y])
+ bb.normalize(tuple_1)
+ assert isinstance(tuple_1.struct_info, rx.TupleStructInfo)
+ assert isinstance(tuple_1.struct_info.fields[0], rx.TensorStructInfo)
+ assert isinstance(tuple_1.struct_info.fields[1], rx.TensorStructInfo)
+
+ # Nested Tuple
+ tuple_2 = rx.Tuple([x, rx.Tuple([x, y])])
+ bb.normalize(tuple_2)
+ type_anno0 = x.checked_type
+ type_anno1 = y.checked_type
+ assert_structural_equal(
+ tuple_2.checked_type, rx.TupleType([type_anno0, rx.TupleType([type_anno0, type_anno1])])
+ )
+ assert isinstance(tuple_2.struct_info, rx.TupleStructInfo)
+ assert isinstance(tuple_2.struct_info.fields[0], rx.TensorStructInfo)
+ assert isinstance(tuple_2.struct_info.fields[1], rx.TupleStructInfo)
+ assert isinstance(tuple_2.struct_info.fields[1].fields[0], rx.TensorStructInfo)
+ assert isinstance(tuple_2.struct_info.fields[1].fields[1], rx.TensorStructInfo)
+
+
+def test_call_te():
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([n, m], "float32"))
+ z = rx.Var("z", rx.TensorStructInfo([n, m], "float32"))
+
+ def te_func(args, args_dict, msg):
+ A, B = args
+ C = args_dict["C"]
+ D = te.compute((128, 128), lambda i, j: A[i, j] + B[i, j])
+ E = te.compute((128, 128), lambda i, j: D[i, j] - C[i, j])
+ return E
+
+ with bb.function("rx_func", [x, y, z]):
+ with bb.dataflow():
+ out = bb.emit_output(bb.call_te(te_func, [x, y], {"C": z}, msg="hello"))
+ bb.emit_func_output(out)
+
+ mod = bb.get()
+ rx_func = mod["rx_func"]
+
+ assert rx_func.params[0] == x
+ assert rx_func.params[1] == y
+ assert rx_func.params[2] == z
+ assert rx_func.body.body == out
+ assert len(rx_func.body.blocks) == 1
+ assert len(rx_func.body.blocks[0].bindings) == 1
+
+
+def test_call_te_with_unsupported_shape_arg():
+ bb = rx.BlockBuilder()
+ x = rx.Var("x", rx.TensorStructInfo((200,), "float32"))
+ s = rx.Var("s", rx.ShapeStructInfo((200,)))
+
+ with pytest.raises(AssertionError):
+ with bb.function("rx_func", [x]):
+ out = bb.emit(bb.call_te(topi.reshape, x, s))
+ bb.emit_func_output(out)
+
+
+def test_emit_te():
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([n, m], "float32"))
+ z = rx.Var("z", rx.TensorStructInfo([n, m], "float32"))
+
+ def te_func(args, args_dict, msg):
+ A, B = args
+ C = args_dict["C"]
+ D = te.compute((128, 128), lambda i, j: A[i, j] + B[i, j])
+ E = te.compute((128, 128), lambda i, j: D[i, j] - C[i, j])
+ return E
+
+ with bb.function("rx_func", [x, y, z]):
+ out = bb.emit_te(te_func, [x, y], {"C": z}, msg="hello")
+ bb.emit_func_output(out)
+
+ mod = bb.get()
+ rx_func = mod["rx_func"]
+
+ def get_tir_func():
+ A = te.placeholder((n, m), dtype="float32", name="A")
+ B = te.placeholder((n, m), dtype="float32", name="B")
+ C = te.placeholder((n, m), dtype="float32", name="C")
+ out = te_func((A, B), {"C": C}, "")
+ return tvm.te.create_prim_func([A, B, C, out], index_dtype_override="int64")
+
+ # check TIR structure matches expected
+ assert_structural_equal(mod["te_func"].body, get_tir_func().body)
+
+ # check Relax function calls TIR function with call_tir call
+ assert rx_func.params[0] == x
+ assert rx_func.params[1] == y
+ assert rx_func.params[2] == z
+ assert rx_func.body.body == out
+ assert len(rx_func.body.blocks) == 1
+ assert len(rx_func.body.blocks[0].bindings) == 1
+
+ call_node = rx_func.body.blocks[0].bindings[0].value
+ assert isinstance(call_node, rx.Call)
+ assert call_node.op == relay.op.get("relax.call_tir")
+ assert len(call_node.args) == 2
+ assert call_node.args[0].name_hint == "te_func"
+ assert call_node.args[1][0] == x
+ assert call_node.args[1][1] == y
+ assert call_node.args[1][2] == z
+
+
+def test_emit_te_multiple():
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([n, m], "float32"))
+ z = rx.Var("z", rx.TensorStructInfo([128, m], "float32"))
+
+ def te_func(A):
+ B = te.compute((128, 128), lambda i, j: A[i, j] + 1)
+ return B
+
+ with bb.function("rx_func", [x, y]):
+ x1 = bb.emit_te(te_func, x)
+ y1 = bb.emit_te(te_func, y)
+ z1 = bb.emit_te(te_func, z)
+ bb.emit_func_output(z1)
+
+ mod = bb.get()
+ rx_func = mod["rx_func"]
+
+ prim_func = []
+ for gv in mod.get_global_vars():
+ if isinstance(mod[gv], PrimFunc):
+ prim_func.append(mod[gv])
+
+ # only two PrimFuncs were generated since two of them are equal so got deduped
+ assert len(prim_func) == 2
+ assert rx_func.body.blocks[0].bindings[0].value.args[0].name_hint == "te_func"
+ assert rx_func.body.blocks[0].bindings[1].value.args[0].name_hint == "te_func"
+ assert rx_func.body.blocks[0].bindings[2].value.args[0].name_hint == "te_func1"
+
+
+def test_emit_te_multiple_output():
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+
+ def te_func(A):
+ B0, B1 = te.compute((n, m), lambda i, j: (A[i, j] + 1, A[i, j] * 2), name="B")
+ return (B0, B1)
+
+ with bb.function("rx_func", [x]):
+ y = bb.emit_te(te_func, x)
+ z = rx.TupleGetItem(y, 0)
+ bb.emit_func_output([y, z])
+
+ rx_func = bb.get()["rx_func"]
+
+ # check call tir output shape is a Tuple of ShapeExpr
+ assert rx_func.params[0] == x
+ call_node = rx_func.body.blocks[0].bindings[0].value
+ assert call_node.op == relay.op.get("relax.call_tir")
+ assert call_node.args[0].name_hint == "te_func"
+ assert isinstance(call_node.sinfo_args[0], rx.TupleStructInfo)
+ assert len(call_node.sinfo_args[0].fields) == 2
+ assert isinstance(call_node.sinfo_args[0].fields[0].shape, rx.ShapeExpr)
+ assert isinstance(call_node.sinfo_args[0].fields[1].shape, rx.ShapeExpr)
+
+
+def test_emit_te_extern():
+ bb = rx.BlockBuilder()
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([m, n], "float32"))
+
+ with bb.function("rx_cblas_matmul", [x, y]):
+ out = bb.emit_te(tvm.contrib.cblas.matmul, x, y, transa=False, transb=False)
+ bb.emit_func_output(out)
+
+ mod = bb.get()
+ rx_func = mod["rx_cblas_matmul"]
+
+ # check Relax function calls TIR function with call_tir call
+ assert rx_func.params[0] == x
+ assert rx_func.params[1] == y
+ assert len(rx_func.body.blocks) == 1
+ call_node = rx_func.body.blocks[0].bindings[0].value
+ assert isinstance(call_node, rx.Call)
+ assert call_node.op == relay.op.get("relax.call_tir")
+ assert len(call_node.args) == 2
+ assert call_node.args[0].name_hint == "matmul"
+ assert call_node.args[1][0] == x
+ assert call_node.args[1][1] == y
+ assert call_node.sinfo_args[0].shape[0] == n
+ assert call_node.sinfo_args[0].shape[1] == n
+
+
+def test_nested_function_fail():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with pytest.raises(RuntimeError):
+ with bb.function("func", [x, y]):
+ gv0 = bb.emit(rx.op.add(x, x))
+ with bb.function("func1", [x, y]):
+ gv1 = bb.emit(rx.op.add(x, x))
+ bb.emit_func_output(gv0)
+
+
+def test_emit_func_output_twice_fail():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with pytest.raises(RuntimeError):
+ with bb.function("func", [x, y]):
+ gv0 = bb.emit(rx.op.add(x, y))
+ bb.emit_func_output(gv0)
+ bb.emit_func_output(gv0)
+
+
+def test_func_params_twice_fail():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with pytest.raises(RuntimeError):
+ with bb.function("func", [x, y]):
+ gv0 = bb.emit(rx.op.add(x, y))
+ bb.emit_func_output(gv0, [x])
+
+
+def test_no_func_params_fail():
+ m = tir.Var("m", "int64")
+ n = tir.Var("n", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([m, n], "float16"))
+ y = rx.Var("y", rx.TensorStructInfo([n], "float16"))
+ bb = rx.BlockBuilder()
+
+ with pytest.raises(RuntimeError):
+ with bb.function("func"):
+ gv0 = bb.emit(rx.Call(ExternFunc("test.blockbuilder.nop"), []))
+ bb.emit_func_output(gv0)
+
+
+def test_block_builder_scope_recovery():
+ bb = rx.BlockBuilder()
+
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.Var("x", rx.TensorStructInfo([n, m], "float32"))
+ y = rx.Var("y", rx.TensorStructInfo([m, n], "float32"))
+
+ with pytest.raises(RuntimeError):
+ # this line fails
+ with bb.function("func", [x, y]):
+ gv0 = bb.emit(rx.op.add(x, y))
+
+ # current should be recovered
+ assert rx.BlockBuilder.current() is None
+
+ # second attempt to do it correctly.
+ with bb.function("func", [x, y]):
+ gv0 = bb.emit(rx.op.add(x, y))
+ bb.emit_func_output(gv0)
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
diff --git a/tests/python/relax/test_expr.py b/tests/python/relax/test_expr.py
index 4eeaed1e0b..902c478561 100644
--- a/tests/python/relax/test_expr.py
+++ b/tests/python/relax/test_expr.py
@@ -15,11 +15,11 @@
# specific language governing permissions and limitations
# under the License.
import numpy as np
-import pytest
import tvm
from tvm import relax as rx
from tvm import tir
from tvm.script import relax as R
+import pytest
def _check_equal(x, y, map_free_vars=False):
@@ -255,4 +255,4 @@ def test_datatype_imm():
if __name__ == "__main__":
- pytest.main([__file__])
+ tvm.testing.main()
diff --git a/tests/python/relax/test_expr_functor.py b/tests/python/relax/test_expr_functor.py
new file mode 100644
index 0000000000..8165107394
--- /dev/null
+++ b/tests/python/relax/test_expr_functor.py
@@ -0,0 +1,746 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import tvm
+from tvm import relax, tir
+from tvm.ir import Op
+from tvm.ir.base import assert_structural_equal
+from tvm.relax import PyExprMutator, PyExprVisitor
+from tvm.relax.expr import (
+ BindingBlock,
+ Call,
+ Constant,
+ DataflowBlock,
+ DataflowVar,
+ Expr,
+ ExternFunc,
+ Function,
+ GlobalVar,
+ If,
+ MatchCast,
+ SeqExpr,
+ ShapeExpr,
+ Tuple,
+ TupleGetItem,
+ PrimValue,
+ StringImm,
+ DataTypeImm,
+ Var,
+ VarBinding,
+)
+from tvm.script import relax as R
+import pytest
+
+m, n = tir.Var("m", "int64"), tir.Var("n", "int64")
+x = relax.Var("x", R.Tensor([n], "float32"))
+y = relax.Var("y", R.Tensor([m, n], "float32"))
+bb = relax.BlockBuilder()
+
+
+@relax.expr_functor.visitor
+class BasicVisitor(PyExprVisitor):
+ """Default ExprVisitor"""
+
+
+class ASTLog:
+ """Helper class to log AST"""
+
+ def __init__(self) -> None:
+ self.log = []
+ self.indent = "\t"
+ self.level = 0
+
+ def push_scope(self):
+ self.level += 1
+
+ def pop_scope(self):
+ self.level -= 1
+
+ def add(self, s: str):
+ self.log.append(self.indent * self.level + s)
+
+ def __str__(self) -> str:
+ return "\n".join(self.log)
+
+
+@relax.expr_functor.visitor
+class ASTPrinter(PyExprVisitor):
+ """Print relax AST in structured format. The shape of Node is ignored."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_constant_(self, op: Constant) -> None:
+ self.log.add("Constant")
+
+ def visit_global_var_(self, op: GlobalVar) -> None:
+ self.log.add("GlobalVar")
+
+ def visit_tuple_(self, op: Tuple) -> None:
+ self.log.add("Tuple")
+ self.log.push_scope()
+ for field in op.fields:
+ self.visit_expr(field)
+ self.log.pop_scope()
+
+ def visit_var_(self, op: Var) -> None:
+ self.log.add("Var")
+
+ def visit_dataflow_var_(self, op: DataflowVar) -> None:
+ self.log.add("DataflowVar")
+
+ def visit_function_(self, op: Function) -> None:
+ self.log.add("Function")
+ self.log.push_scope()
+ for param in op.params:
+ self.visit_var_def(param)
+
+ self.visit_expr(op.body)
+ self.log.pop_scope()
+
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("Call")
+ self.log.push_scope()
+ self.visit_expr(op.op)
+
+ for arg in op.args:
+ self.visit_expr(arg)
+ self.log.pop_scope()
+
+ def visit_if_(self, op: If) -> None:
+ self.log.add("If")
+ self.log.push_scope()
+ self.visit_expr(op.cond)
+ self.visit_expr(op.true_branch)
+ self.visit_expr(op.false_branch)
+ self.log.pop_scope()
+
+ def visit_op_(self, op: Op) -> None:
+ self.log.add("Op")
+
+ def visit_tuple_getitem_(self, op: TupleGetItem) -> None:
+ self.log.add("TupleGetItem")
+ self.log.push_scope()
+ self.visit_expr(op.tuple_value)
+ self.log.pop_scope()
+
+ def visit_prim_value_(self, op: PrimValue) -> None:
+ self.log.add("PrimValue")
+
+ def visit_string_imm_(self, op: StringImm) -> None:
+ self.log.add("StringImm")
+
+ def visit_data_type_imm_(self, op: DataTypeImm) -> None:
+ self.log.add("DataTypeImm")
+
+ def visit_shape_expr_(self, op: ShapeExpr) -> None:
+ self.log.add("ShapeExpr")
+
+ def visit_extern_func_(self, op: ExternFunc) -> None:
+ self.log.add("ExternFunc")
+
+ def visit_seq_expr_(self, op: SeqExpr) -> None:
+ self.log.add("SeqExpr")
+ self.log.push_scope()
+ for block in op.blocks:
+ self.visit_binding_block(block)
+ self.visit_expr(op.body)
+ self.log.pop_scope()
+
+ def visit_var_binding_(self, binding: VarBinding) -> None:
+ self.log.add("VarBinding")
+ self.log.push_scope()
+ self.visit_expr(binding.value)
+ self.visit_var_def(binding.var)
+ self.log.pop_scope()
+
+ def visit_match_cast_(self, binding: MatchCast) -> None:
+ self.log.add("MatchCast")
+ self.log.push_scope()
+ self.visit_var_def(binding.var)
+ self.visit_expr(binding.value)
+ self.log.pop_scope()
+
+ def visit_binding_block_(self, block: BindingBlock) -> None:
+ self.log.add("BindingBlock")
+ self.log.push_scope()
+ for binding in block.bindings:
+ self.visit_binding(binding)
+ self.log.pop_scope()
+
+ def visit_dataflow_block_(self, block: DataflowBlock) -> None:
+ self.log.add("DataflowBlock")
+ self.log.push_scope()
+ for binding in block.bindings:
+ self.visit_binding(binding)
+ self.log.pop_scope()
+
+ def visit_var_def_(self, var: Var) -> None:
+ self.log.add("VarDef")
+
+ def visit_dataflow_var_def_(self, var: DataflowVar) -> None:
+ self.log.add("DataflowVarDef")
+
+
+@relax.expr_functor.mutator
+class BasicMutator(PyExprMutator):
+ """Default ExprMutator"""
+
+
+@relax.expr_functor.mutator
+class ASTPostPrinterMutator(PyExprMutator):
+ """Print relax AST in the post order format."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_constant_(self, op: Constant) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Constant")
+ return op
+
+ def visit_global_var_(self, op: GlobalVar) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("GlobalVar")
+ return op
+
+ def visit_tuple_(self, op: Tuple) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Tuple")
+ return op
+
+ def visit_var_(self, op: Var) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Var")
+ return op
+
+ def visit_dataflow_var_(self, op: DataflowVar) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("DataflowVar")
+ return op
+
+ def visit_function_(self, op: Function) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Function")
+ return op
+
+ def visit_call_(self, op: Call) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Call")
+ return op
+
+ def visit_if_(self, op: If) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("If")
+ return op
+
+ def visit_op_(self, op: Op) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("Op")
+ return op
+
+ def visit_tuple_getitem_(self, op: TupleGetItem) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("TupleGetItem")
+ return op
+
+ def visit_prim_value_(self, op: PrimValue) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("PrimValue")
+ return op
+
+ def visit_string_imm_(self, op: StringImm) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("StringImm")
+ return op
+
+ def visit_data_type_imm_(self, op: DataTypeImm) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("DataTypeImm")
+ return op
+
+ def visit_shape_expr_(self, op: ShapeExpr) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("ShapeExpr")
+ return op
+
+ def visit_extern_func_(self, op: ExternFunc) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("ExternFunc")
+ return op
+
+ def visit_seq_expr_(self, op: SeqExpr) -> Expr:
+ op = self.visit_expr_post_order(op)
+ self.log.add("SeqExpr")
+ return op
+
+ def visit_var_binding_(self, binding: VarBinding) -> None:
+ """Identical with ExprMutator::VisitBinding_(const VarBindingNode* binding) on the C++ side."""
+ new_value = self.visit_expr(binding.value)
+ new_var = self.visit_var_def(binding.var)
+
+ self.log.add("VarBinding")
+ if binding.var.same_as(new_var) and binding.value.same_as(new_value):
+ self.builder_.emit_normalized(binding)
+ return
+
+ temp = self.with_struct_info(new_var, new_value.struct_info)
+ if not temp.same_as(new_var):
+ new_var = temp
+ self.set_var_remap(binding.var.vid, new_var)
+
+ self.builder_.emit_normalized(VarBinding(new_var, new_value))
+
+ def visit_match_cast_(self, binding: MatchCast) -> None:
+ """Identical with ExprMutator::VisitBinding_(const MatchCastNode* binding) on the C++ side."""
+ new_var = self.visit_var_def(binding.var)
+ new_value = self.visit_expr(binding.value)
+
+ temp = self.with_struct_info(new_var, binding.struct_info)
+ if not temp.same_as(new_var):
+ new_var = temp
+ self.set_var_remap(binding.var.vid, new_var)
+
+ self.log.add("MatchCast")
+ self.builder_.emit_normalized(MatchCast(new_var, new_value, binding.struct_info))
+
+ def visit_binding_block_(self, block: BindingBlock) -> BindingBlock:
+ """Identical with ExprMutator::VisitBindingBlock_(const BindingBlockNode* block) on the C++ side."""
+ self.builder_._begin_binding_block()
+ for binding in block.bindings:
+ self.visit_binding(binding)
+ self.log.add("BindingBlock")
+ return self.builder_._end_block()
+
+ def visit_dataflow_block_(self, block: DataflowBlock) -> None:
+ """Identical with ExprMutator::VisitBindingBlock_(const DataflowBlockNode* block) on the C++ side."""
+ self.builder_._begin_dataflow_block()
+ for binding in block.bindings:
+ self.visit_binding(binding)
+ self.log.add("DataflowBlock")
+ return self.builder_._end_block()
+
+ def visit_var_def_(self, var: Var) -> None:
+ """Identical with ExprMutator::VisitVarDef_(const VarNode* var) on the C++ side."""
+ self.log.add("VarDef")
+ return var
+
+ def visit_dataflow_var_def_(self, var: DataflowVar) -> None:
+ """Identical with ExprMutator::VisitVarDef_(const DataflowVarNode* var) on the C++ side."""
+ self.log.add("DataflowVarDef")
+ return var
+
+
+def basic_check(expr, visitor_str, mutator_str):
+ def visit(f, expr):
+ if isinstance(expr, relax.Expr):
+ return f.visit_expr(expr)
+ elif isinstance(expr, relax.BindingBlock):
+ return f.visit_binding_block(expr)
+
+ # check no overloading case
+ basic_visitor = BasicVisitor()
+ visit(basic_visitor, expr)
+
+ # check the output log
+ log_visitor = ASTPrinter()
+ visit(log_visitor, expr)
+ assert str(log_visitor.log) == visitor_str
+
+ # check no overloading case
+ basic_mutator = BasicMutator()
+ # skip normalize GlobalVar since it requires context IRModule to get the checked_type_
+ if isinstance(expr, relax.Expr) and not isinstance(expr, relax.GlobalVar):
+ expr = bb.normalize(expr)
+ assert_structural_equal(visit(basic_mutator, expr), expr)
+
+ # check the output log and return value
+ post_log_mutator = ASTPostPrinterMutator()
+ if isinstance(expr, relax.Expr) and not isinstance(expr, relax.GlobalVar):
+ expr = bb.normalize(expr)
+ assert_structural_equal(visit(post_log_mutator, expr), expr)
+ assert str(post_log_mutator.log) == mutator_str
+
+
+def test_constant():
+ basic_check(relax.const(1.0), "Constant", "Constant")
+
+
+def test_var():
+ basic_check(x, "Var", "Var")
+
+
+def test_dataflow_var():
+ lv = relax.DataflowVar("lv", R.Tensor([n], "float32"))
+ basic_check(lv, "DataflowVar", "DataflowVar")
+
+
+def test_tuple():
+ t = relax.Tuple([x, y])
+ basic_check(t, "\n".join(["Tuple", "\tVar", "\tVar"]), "\n".join(["Var", "Var", "Tuple"]))
+
+
+def test_global_var():
+ gv = relax.GlobalVar("gv")
+ basic_check(gv, "GlobalVar", "GlobalVar")
+
+
+def test_seq_expr():
+ bindings = [relax.VarBinding(x, relax.const(1))]
+ blocks = [relax.BindingBlock(bindings)]
+ seq_expr = relax.SeqExpr(blocks, x)
+ basic_check(
+ seq_expr,
+ "\n".join(
+ [
+ "SeqExpr",
+ "\tBindingBlock",
+ "\t\tVarBinding",
+ "\t\t\tConstant",
+ "\t\t\tVarDef",
+ "\tVar",
+ ]
+ ),
+ "\n".join(["Constant", "VarDef", "VarBinding", "BindingBlock", "Var", "SeqExpr"]),
+ )
+
+
+def test_shape_expr():
+ x = relax.ShapeExpr([m, n])
+ basic_check(x, "ShapeExpr", "ShapeExpr")
+
+
+def test_call():
+ call_node = relax.op.add(x, y)
+ basic_check(
+ call_node,
+ "\n".join(["Call", "\tOp", "\tVar", "\tVar"]),
+ "\n".join(["Op", "Var", "Var", "ShapeExpr", "Call"]),
+ )
+
+
+def test_if():
+ if_node = relax.If(x, x, x)
+ basic_check(
+ if_node,
+ "\n".join(["If", "\tVar", "\tVar", "\tVar"]),
+ "\n".join(["Var", "Var", "SeqExpr", "Var", "SeqExpr", "If"]),
+ )
+
+
+def test_tuple_getitem():
+ tuple_getitem_node = relax.TupleGetItem(relax.Tuple([x, y]), 0)
+ basic_check(
+ tuple_getitem_node,
+ "\n".join(["TupleGetItem", "\tTuple", "\t\tVar", "\t\tVar"]),
+ "\n".join(["Var", "Var", "Tuple", "TupleGetItem"]),
+ )
+
+
+def test_binding_block():
+ bb._begin_binding_block()
+ gv0 = bb.emit(relax.op.add(x, y))
+ gv1 = bb.match_cast(y, R.Tensor([m, n], "float32"))
+ b0 = bb._end_block()
+ basic_check(
+ b0,
+ "\n".join(
+ [
+ "BindingBlock",
+ "\tVarBinding",
+ "\t\tCall",
+ "\t\t\tOp",
+ "\t\t\tVar",
+ "\t\t\tVar",
+ "\t\tVarDef",
+ "\tMatchCast",
+ "\t\tVarDef",
+ "\t\tVar",
+ ]
+ ),
+ "\n".join(
+ [
+ "Op",
+ "Var",
+ "Var",
+ "Call",
+ "ShapeExpr",
+ "VarDef",
+ "VarBinding",
+ "Var",
+ "ShapeExpr",
+ "ShapeExpr",
+ "VarDef",
+ "MatchCast",
+ "BindingBlock",
+ ]
+ ),
+ )
+
+
+def test_dataflow_block():
+ bb._begin_dataflow_block()
+ lv0 = bb.emit(relax.op.add(x, y))
+ gv1 = bb.match_cast(y, R.Tensor([m, n], "float32"))
+ b0 = bb._end_block()
+ basic_check(
+ b0,
+ "\n".join(
+ [
+ "DataflowBlock",
+ "\tVarBinding",
+ "\t\tCall",
+ "\t\t\tOp",
+ "\t\t\tVar",
+ "\t\t\tVar",
+ "\t\tDataflowVarDef",
+ "\tMatchCast",
+ "\t\tDataflowVarDef",
+ "\t\tVar",
+ ]
+ ),
+ "\n".join(
+ [
+ "Op",
+ "Var",
+ "Var",
+ "Call",
+ "ShapeExpr",
+ "DataflowVarDef",
+ "VarBinding",
+ "Var",
+ "ShapeExpr",
+ "ShapeExpr",
+ "DataflowVarDef",
+ "MatchCast",
+ "DataflowBlock",
+ ]
+ ),
+ )
+
+
+def test_function():
+ bindings = [relax.VarBinding(x, relax.const(1))]
+ blocks = [relax.BindingBlock(bindings)]
+ seq_expr = relax.SeqExpr(blocks, x)
+ func = relax.Function([x], seq_expr, R.Tensor([n], "float32"))
+ basic_check(
+ func,
+ "\n".join(
+ [
+ "Function",
+ "\tVarDef",
+ "\tSeqExpr",
+ "\t\tBindingBlock",
+ "\t\t\tVarBinding",
+ "\t\t\t\tConstant",
+ "\t\t\t\tVarDef",
+ "\t\tVar",
+ ]
+ ),
+ "\n".join(
+ [
+ "VarDef",
+ "Constant",
+ "VarDef",
+ "VarBinding",
+ "BindingBlock",
+ "Var",
+ "SeqExpr",
+ "Function",
+ ]
+ ),
+ )
+
+
+def test_extern_func():
+ func = relax.ExternFunc("f")
+ basic_check(func, "ExternFunc", "ExternFunc")
+
+
+def test_inherit():
+ # The internal class is not instantiated.
+ class InternalVisitor(PyExprVisitor):
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("InternalCall")
+ self.log.push_scope()
+ self.visit_expr(op.op)
+
+ for arg in op.args:
+ self.visit_expr(arg)
+ self.log.pop_scope()
+
+ def visit_var_(self, op: Var) -> None:
+ self.log.add("Var")
+
+ def visit_op_(self, op: Op) -> None:
+ self.log.add("Op")
+
+ @relax.expr_functor.visitor
+ class LeafVisitor(InternalVisitor):
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("LeafCall")
+ self.log.push_scope()
+ self.visit_expr(op.op)
+
+ for arg in op.args:
+ self.visit_expr(arg)
+ self.log.pop_scope()
+
+ call_node = relax.op.add(x, y)
+ lv = LeafVisitor()
+ lv.visit_expr(call_node)
+ assert str(lv.log) == "\n".join(["LeafCall", "\tOp", "\tVar", "\tVar"])
+
+
+def test_inherit_with_cls():
+ # The decorator converts `InternalVisitor` to a wrapper class.
+ @relax.expr_functor.visitor
+ class InternalVisitor(PyExprVisitor):
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("InternalCall")
+ self.log.push_scope()
+ self.visit_expr(op.op)
+
+ for arg in op.args:
+ self.visit_expr(arg)
+ self.log.pop_scope()
+
+ def visit_var_(self, op: Var) -> None:
+ self.log.add("Var")
+
+ def visit_op_(self, op: Op) -> None:
+ self.log.add("Op")
+
+ # `InternalVisitor._cls` refers to the original `InternalVisitor` users defined.
+ @relax.expr_functor.visitor
+ class LeafVisitor(InternalVisitor._cls):
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("LeafCall")
+ self.log.push_scope()
+ self.visit_expr(op.op)
+
+ for arg in op.args:
+ self.visit_expr(arg)
+ self.log.pop_scope()
+
+ call_node = relax.op.add(x, y)
+ iv = InternalVisitor()
+ iv.visit_expr(call_node)
+ assert str(iv.log) == "\n".join(["InternalCall", "\tOp", "\tVar", "\tVar"])
+
+ lv = LeafVisitor()
+ lv.visit_expr(call_node)
+ assert str(lv.log) == "\n".join(["LeafCall", "\tOp", "\tVar", "\tVar"])
+
+
+def test_wrong_inherit():
+ @relax.expr_functor.visitor
+ class InternalVisitor(PyExprVisitor):
+ def visit_call_(self, op: Call) -> None:
+ pass
+
+ with pytest.raises(
+ TypeError,
+ match="Inheritance from a decorated object `LeafVisitor` is not allowed. Please inherit from `LeafVisitor._cls`.",
+ ):
+
+ @relax.expr_functor.visitor
+ class LeafVisitor(InternalVisitor):
+ def visit_call_(self, op: Call) -> None:
+ pass
+
+
+def test_call_visitor_super():
+ @relax.expr_functor.visitor
+ class InternalVisitor(PyExprVisitor):
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("InternalCall")
+ super().visit_call_(op) # call PyExprVisitor.visit_call_
+
+ def visit_var_(self, op: Var) -> None:
+ self.log.add("Var")
+
+ def visit_op_(self, op: Op) -> None:
+ self.log.add("Op")
+
+ @relax.expr_functor.visitor
+ class LeafVisitor(InternalVisitor._cls):
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("LeafCall")
+ super().visit_call_(op) # call InternalVisit.visit_call_
+
+ call_node = relax.op.add(x, y)
+ iv = InternalVisitor()
+ iv.visit_expr(call_node)
+ assert str(iv.log) == "\n".join(["InternalCall", "Op", "Var", "Var"])
+
+ lv = LeafVisitor()
+ lv.visit_expr(call_node)
+ assert str(lv.log) == "\n".join(["LeafCall", "InternalCall", "Op", "Var", "Var"])
+
+
+def test_call_mutator_super():
+ @relax.expr_functor.mutator
+ class InternalMutator(PyExprMutator):
+ def __init__(self) -> None:
+ super().__init__()
+ self.log = ASTLog()
+
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("InternalCall")
+ return super().visit_call_(op) # call PyExprMutator.visit_call_
+
+ def visit_var_(self, op: Var) -> None:
+ self.log.add("Var")
+ return super().visit_var_(op) # call PyExprMutator.visit_var_
+
+ def visit_op_(self, op: Op) -> None:
+ self.log.add("Op")
+ return super().visit_op_(op) # call PyExprMutator.visit_op_
+
+ @relax.expr_functor.mutator
+ class LeafMutator(InternalMutator._cls):
+ def visit_call_(self, op: Call) -> None:
+ self.log.add("LeafCall")
+ return super().visit_call_(op) # call InternalMutator.visit_call_
+
+ call_node = relax.op.add(x, y)
+ im = InternalMutator()
+ im.visit_expr(call_node)
+ assert str(im.log) == "\n".join(["InternalCall", "Op", "Var", "Var"])
+
+ lm = LeafMutator()
+ lm.visit_expr(call_node)
+ assert str(lm.log) == "\n".join(["LeafCall", "InternalCall", "Op", "Var", "Var"])
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
[tvm] 05/07: [Unity] Basic StructInfo Analysis and Expr construction (#13916)
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity-staging
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 3963005a9352bad6fa2e32f16e32f0967f9ef4b3
Author: Yuchen Jin <yu...@cs.washington.edu>
AuthorDate: Sun Feb 5 00:51:08 2023 -0500
[Unity] Basic StructInfo Analysis and Expr construction (#13916)
[Unity] Basic StructInfo Analysis and Expr construction.
This PR adds struct info analysis and expr support.
These are logics to construct the IR node and perform
struct info related analysis.
Testcases are added to cover the IR node construction
and related struct info analysis checks.
Co-authored-by: Tianqi Chen <ti...@gmail.com>
Co-authored-by: Altan Haan <al...@cs.washington.edu>
Co-authored-by: Andrew Liu <an...@gmail.com>
Co-authored-by: Hongyi Jin <32...@qq.com>
Co-authored-by: Jiawei Liu <ja...@gmail.com>
Co-authored-by: Junru Shao <ju...@gmail.com>
Co-authored-by: Lesheng Jin <34...@users.noreply.github.com>
Co-authored-by: masahi <ma...@gmail.com>
Co-authored-by: Prakalp Srivastava <pr...@octoml.ai>
Co-authored-by: Ruihang Lai <ru...@cs.cmu.edu>
Co-authored-by: Siyuan Feng <Hz...@sjtu.edu.cn>
Co-authored-by: Steven S. <Lyubomirsky slyubomirsky@octoml.ai>
Co-authored-by: Sunghyun Park <49...@users.noreply.github.com>
Co-authored-by: Yixin Dong <ub...@gmail.com>
Co-authored-by: Yong Wu <yo...@gmail.com>
Co-authored-by: Ziheng Jiang <zi...@apache.org>
---
CMakeLists.txt | 1 +
include/tvm/ir/type.h | 3 +-
include/tvm/relax/analysis.h | 252 +++++++
include/tvm/relax/expr.h | 43 +-
include/tvm/relax/expr_functor.h | 415 ++++++++++++
include/tvm/relax/struct_info.h | 7 +-
include/tvm/relax/struct_info_functor.h | 151 +++++
python/tvm/ir/expr.py | 11 +
python/tvm/relax/__init__.py | 48 ++
python/tvm/{script => relax/analysis}/__init__.py | 8 +-
.../__init__.py => relax/analysis/_ffi_api.py} | 9 +-
python/tvm/relax/analysis/analysis.py | 135 ++++
python/tvm/relax/expr.py | 729 +++++++++++++++++++++
python/tvm/relax/struct_info.py | 197 ++++++
python/tvm/relax/ty.py | 75 +++
python/tvm/script/__init__.py | 1 +
python/tvm/script/{ => parser/relax}/__init__.py | 9 +-
src/ir/function.cc | 14 +-
src/ir/type.cc | 3 +-
src/relax/analysis/shape_analysis.cc | 55 ++
src/relax/analysis/struct_info_analysis.cc | 716 ++++++++++++++++++++
src/relax/ir/expr.cc | 601 +++++++++++++++++
src/relax/ir/expr_functor.cc | 546 +++++++++++++++
src/relax/ir/struct_info.cc | 14 +-
src/relax/ir/struct_info_functor.cc | 130 ++++
src/relax/ir/type.cc | 88 +++
.../relax/test_analysis_struct_info_analysis.py | 418 ++++++++++++
tests/python/relax/test_expr.py | 258 ++++++++
tests/python/relax/test_struct_info.py | 241 +++++++
29 files changed, 5151 insertions(+), 27 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 42170c82bb..f6d5db3098 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -290,6 +290,7 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
src/support/*.cc
src/script/*.cc
src/relax/ir/*.cc
+ src/relax/analysis/*.cc
src/relax/backend/vm/*.cc
)
diff --git a/include/tvm/ir/type.h b/include/tvm/ir/type.h
index c6baf5e08b..ec13635a26 100644
--- a/include/tvm/ir/type.h
+++ b/include/tvm/ir/type.h
@@ -131,8 +131,9 @@ class PrimType : public Type {
/*!
* \brief Constructor
* \param dtype The corresponding dtype.
+ * \param span The span
*/
- TVM_DLL explicit PrimType(runtime::DataType dtype);
+ TVM_DLL explicit PrimType(runtime::DataType dtype, Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(PrimType, Type, PrimTypeNode);
};
diff --git a/include/tvm/relax/analysis.h b/include/tvm/relax/analysis.h
new file mode 100644
index 0000000000..82145032f4
--- /dev/null
+++ b/include/tvm/relax/analysis.h
@@ -0,0 +1,252 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/analysis.h
+ * \brief The set of Relax specific analysis on IR.
+ */
+#ifndef TVM_RELAX_ANALYSIS_H_
+#define TVM_RELAX_ANALYSIS_H_
+
+#include <tvm/arith/analyzer.h>
+#include <tvm/ir/diagnostic.h>
+#include <tvm/ir/module.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/relay/op_attr_types.h>
+#include <tvm/tir/function.h>
+
+#include <functional>
+#include <utility>
+
+namespace tvm {
+namespace relax {
+//-----------------------------------
+// Shape expression analysis
+//----------------------------------
+/*!
+ * \brief Can prove the two symbolic shape arrays equals to each other.
+ *
+ * \param lhs The left operand.
+ * \param rhs The right operand.
+ * \param ana The analyzer used for integer analysis.
+ * \return The prove result.
+ *
+ * \note This function does best effort prove, which means
+ * if result is false, there is still possibility that
+ * two shapes equals to each other during runtime.
+ */
+TVM_DLL bool CanProveShapeEqual(const Array<PrimExpr>& lhs, const Array<PrimExpr>& rhs,
+ arith::Analyzer* ana);
+
+/*!
+ * \brief Can prove the two symbolic shape expressions equals to each other.
+ *
+ * \param lhs The left operand.
+ * \param rhs The right operand.
+ * \param ana The analyzer used for integer analysis.
+ *
+ * \note This function does best effort prove, which means
+ * if result is false, there is still possibility that
+ * two shapes equals to each other during runtime.
+ */
+TVM_DLL bool CanProveShapeEqual(const Expr& lhs, const Expr& rhs, arith::Analyzer* ana);
+
+//-----------------------------------
+// Foundational StructInfo analysis
+//-----------------------------------
+/*!
+ * \brief Get the corresponding static type from a given struct info.
+ * \param info The struct info.
+ * \return the corresponding static type.
+ */
+TVM_DLL Type GetStaticType(const StructInfo& info);
+
+/*!
+ * \brief Get the corresponding struct info from static type.
+ * \param type The input type
+ * \return the corresponding struct info.
+ */
+TVM_DLL StructInfo StructInfoFromType(const Type& type);
+
+/*!
+ * \brief Erase the info to a corresponding more coarse grained
+ * struct info that is still well-defined(with all the vars in scope).
+ *
+ * When we are returning a StructInfo to another scope,
+ * it is important to remember that StructInfo may carry
+ * dependencies on var that is not defined the other scope.
+ *
+ * In such cases, it is important to call EraseToWellDefined to get
+ * another StructInfo that **only** contains the vars that are defined
+ * in the target scope.
+ *
+ * For example, consider the following function
+ *
+ * \code
+ *
+ * @R.function
+ * def f(x: R.Tensor[(n, m)]):
+ * k = tir.Var("k", "int64")
+ * v0 = opaque_fn(x)
+ * v1 = match_cast(v0, R.Tensor[(n, k)])
+ * v2 : R.Tensor[(n + 1, k + 2)] = pad(v1)
+ * return v2
+ *
+ * \endcode
+ *
+ * In the above code, the return value y have shape `(n + 1, k + 2)`,
+ * However, at the level of function signature, only n, m are defined,
+ * k is undefined here.
+ *
+ * When we call EraseToWellDefined(R.Tensor[(n + 1, k + 2)], fshape_var_map={n: n, m: m}),
+ * we will obtain R.Tensor(ndim=2), which is an erased info that does not depend
+ * on k(which is undefined from parameter signature).
+ *
+ * However, if we call EraseToWellDefined(R.Tensor[(n + 1, m)], fshape_var_map={n: n, m: m}),
+ * Then the return value will be R.Tensor[(n + 1, m)], because both n and m are defined.
+ *
+ * We can also make these var map to return a different expression.
+ * For example, EraseToWellDefined(R.Tensor[(n + 1, m)], fshape_var_map={n: 2, m: m})
+ * will give us R.Tensor[(3, m)], where n get replaced by 2.
+ *
+ * Use this function in the following scenarios:
+ * - Decide the struct_info of expr with sub-scopes, such as If, SeqExpr
+ * - Decide the deduced return struct_info of a function that can be fully decided by params.
+ *
+ * \param info The struct info.
+ * \param f_shape_var_map callback function to specify
+ * whether a symbolic shape var is defined and the value it maps to,
+ * return nullopt if var is undefined.
+ * \param f_var_map callback function to specify
+ * whether a var is defined in the target scope and the value it maps to,
+ * return nullopt if var is undefined.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ *
+ * \return the corresponding erased struct info.
+ */
+TVM_DLL StructInfo
+EraseToWellDefined(const StructInfo& info,
+ std::function<Optional<PrimExpr>(const tir::Var& var)> f_shape_var_map = nullptr,
+ std::function<Optional<Expr>(const Var& var)> f_var_map = nullptr,
+ arith::Analyzer* ana = nullptr);
+
+/*!
+ * \brief EraseToWellDefined variant with map.
+ * \param info The struct info.
+ * \param shape_var_map map to specify
+ * whether a symbolic shape var is defined and the value it maps to,
+ * return nullopt if var is undefined.
+ * \param var_map map to specify
+ * whether a var is defined in the target scope and the value it maps to,
+ * return nullopt if var is undefined.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ *
+ * \return the corresponding erased struct info.
+ */
+TVM_DLL StructInfo EraseToWellDefined(const StructInfo& info, Map<tir::Var, PrimExpr> shape_var_map,
+ Map<Var, Expr> var_map, arith::Analyzer* ana = nullptr);
+
+/*!
+ * \brief Fine grained result of base check.
+ *
+ * This analysis comes with different levels of checking failures
+ * that can help to customize the compilation decisions.
+ *
+ * For a given pair of lhs_struct_info, rhs_struct_info. We adopt
+ * the following terminology:
+ * - LSet = {value | value matches lhs_struct_info}
+ * - RSet = {value | value matches rhs_struct_info}
+ *
+ * See the definition of each level below.
+ */
+enum class BaseCheckResult {
+ /*!
+ * \brief The two value sets have no intersection at all: Interset(LSet, RSet) = empty
+ */
+ kFailL0 = 0,
+ /*!
+ * \brief LSet is not superset of RSet by only looking at static information.
+ *
+ * \note This level will trigger static type checking error when lhs is param and rhs is arg.
+ */
+ kFailL1 = 1,
+ /*!
+ * \brief WLSet is not superset of RSet because of mismatch in value information.
+ *
+ * L1-level mismatches in params of FuncStructInfo is categorized as
+ * If lhs is FuncStructInfo, then L1-level mismatch in its params
+ * is categorized as L2-level mismatch for lhs.
+ *
+ * Design considerations for functions:
+ * - (a) We want to be able to erase type/value in function signature
+ * when we unify function struct info and preserve simpler representations.
+ * - (b) We automatically insert match_cast at function boundary, so
+ * we can erase (int)->int argument as (object)->int.
+ * The input shape/type mismatch will be detected by runtime checks at function boundary.
+ * This behavior is also consistent with the PackedFunc behavior.
+ *
+ * \note This level means there is no problem about static known information.
+ * It is OK for the checker to do best effort and return this value.
+ */
+ kFailL2 = 2,
+ /*! \brief LSet is superset of RSet. */
+ kPass = 3
+};
+
+/*!
+ * \brief Run a base check to see if base subsumes derived.
+ *
+ * This function returns fine-grained base-check result on reasons of failure.
+ *
+ * \param base The base struct info.
+ * \param derived The derived struct info.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ * \return Whether the relation holds.
+ *
+ * \sa BaseCheckResult
+ */
+TVM_DLL BaseCheckResult StructInfoBaseCheck(const StructInfo& base, const StructInfo& derived,
+ arith::Analyzer* ana = nullptr);
+
+/*!
+ * \brief Check the relation of two struct info to see if one subsumes another one.
+ *
+ * \param base The base struct info.
+ * \param derived The derived struct info.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ * \return Whether the relation holds.
+ */
+TVM_DLL bool IsBaseOf(const StructInfo& base, const StructInfo& derived,
+ arith::Analyzer* ana = nullptr);
+
+/*!
+ * \brief Unify the two struct info to their least common ancestor.
+ *
+ * \param lhs The left operand.
+ * \param rhs The right operand.
+ * \param ana Optional context analyzer to prove symbolic expression equality.
+ * \return The unified information.
+ */
+TVM_DLL StructInfo StructInfoLCA(const StructInfo& lhs, const StructInfo& rhs,
+ arith::Analyzer* ana = nullptr);
+} // namespace relax
+} // namespace tvm
+
+#endif // TVM_RELAX_ANALYSIS_H_
diff --git a/include/tvm/relax/expr.h b/include/tvm/relax/expr.h
index 8154b1dd86..9e563c7061 100644
--- a/include/tvm/relax/expr.h
+++ b/include/tvm/relax/expr.h
@@ -23,7 +23,6 @@
#include <tvm/ir/source_map.h>
#include <tvm/node/node.h>
#include <tvm/relax/type.h>
-#include <tvm/relay/expr.h>
#include <tvm/runtime/container/array.h>
#include <tvm/runtime/container/map.h>
#include <tvm/runtime/object.h>
@@ -35,7 +34,47 @@ namespace relax {
using Expr = RelayExpr;
using ExprNode = RelayExprNode;
-using relay::Id;
+/*!
+ * \brief The unique identifier of variables.
+ *
+ * Id is like name to the variables,
+ * except that id is unique for each Var.
+ *
+ * \note Do not create Id directly, they are created in Var.
+ */
+class IdNode : public Object {
+ public:
+ /*!
+ * \brief The name of the variable,
+ * this only acts as a hint to the user,
+ * and is not used for equality.
+ */
+ String name_hint;
+
+ void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("name_hint", &name_hint); }
+
+ bool SEqualReduce(const IdNode* other, SEqualReducer equal) const {
+ return equal.FreeVarEqualImpl(this, other);
+ }
+
+ void SHashReduce(SHashReducer hash_reduce) const { hash_reduce.FreeVarHashImpl(this); }
+
+ static constexpr const char* _type_key = "relax.Id";
+ static constexpr const bool _type_has_method_sequal_reduce = true;
+ static constexpr const bool _type_has_method_shash_reduce = true;
+ TVM_DECLARE_FINAL_OBJECT_INFO(IdNode, Object);
+};
+
+class Id : public ObjectRef {
+ public:
+ /*!
+ * \brief The constructor
+ * \param name_hint The name of the variable.
+ */
+ TVM_DLL explicit Id(String name_hint);
+
+ TVM_DEFINE_OBJECT_REF_METHODS(Id, ObjectRef, IdNode);
+};
/*!
* \brief Base type of all structure information.
diff --git a/include/tvm/relax/expr_functor.h b/include/tvm/relax/expr_functor.h
new file mode 100644
index 0000000000..5735e8661f
--- /dev/null
+++ b/include/tvm/relax/expr_functor.h
@@ -0,0 +1,415 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/expr_functor.h
+ * \brief A more powerful visitor which enables defining arbitrary function
+ * signatures with type based dispatch on first argument.
+ */
+#ifndef TVM_RELAX_EXPR_FUNCTOR_H_
+#define TVM_RELAX_EXPR_FUNCTOR_H_
+
+#include <tvm/node/functor.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/relax/struct_info_functor.h>
+#include <tvm/relay/op.h>
+#include <tvm/tir/function.h>
+
+#include <string>
+#include <utility>
+
+namespace tvm {
+namespace relax {
+
+/*!
+ * \brief A dynamical functor that dispatches on in the first Expr argument.
+ * You can use this as a more powerful Visitor, since it allows you to
+ * define function signatures of Visit Function.
+ *
+ * \sa tvm/ir_functor.h
+ *
+ * \tparam FType function signiture
+ * This type is only defined for FType with function signature R(const Expr&,
+ * Args...)
+ */
+template <typename FType>
+class ExprFunctor;
+
+// functions to be overriden.
+#define EXPR_FUNCTOR_DEFAULT \
+ { return VisitExprDefault_(op, std::forward<Args>(args)...); }
+
+#define RELAX_EXPR_FUNCTOR_DISPATCH(OP) \
+ vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self, Args... args) { \
+ return self->VisitExpr_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \
+ });
+
+#define PY_EXPR_VISITOR_DEFAULT(N, PY_FUNC, DEFAULT_FUNC) \
+ { \
+ if (PY_FUNC != nullptr) \
+ PY_FUNC(N); \
+ else \
+ DEFAULT_FUNC; \
+ }
+
+#define PY_EXPR_MUTATOR_DEFAULT(N, PY_FUNC, DEFAULT_FUNC, RET_TYPE) \
+ { \
+ if (PY_FUNC != nullptr) { \
+ RET_TYPE ret = PY_FUNC(N); \
+ return ret; \
+ } else { \
+ return DEFAULT_FUNC; \
+ } \
+ }
+
+#define PY_EXPR_VISITOR_DISPATCH(OP, PY_FUNC) \
+ vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self) { \
+ if (self->PY_FUNC != nullptr) \
+ self->PY_FUNC(n); \
+ else \
+ self->VisitExpr_(static_cast<const OP*>(n.get())); \
+ });
+
+#define PY_EXPR_MUTATOR_DISPATCH(OP, PY_FUNC) \
+ vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self) { \
+ if (self->PY_FUNC != nullptr) { \
+ Expr expr = self->PY_FUNC(n); \
+ return expr; \
+ } else { \
+ return self->VisitExpr_(static_cast<const OP*>(n.get())); \
+ } \
+ });
+
+#define PY_EXPR_MUTATOR_VISIT_EXPR_POST_ORDER_DISPATCH(OP) \
+ post_order_vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self) { \
+ return self->VisitExprPostOrder_(static_cast<const OP*>(n.get())); \
+ });
+
+template <typename R, typename... Args>
+class ExprFunctor<R(const Expr& n, Args...)> {
+ private:
+ using TSelf = ExprFunctor<R(const Expr& n, Args...)>;
+ using FType = tvm::NodeFunctor<R(const ObjectRef& n, TSelf* self, Args...)>;
+
+ public:
+ /*! \brief the result type of this functor */
+ using result_type = R;
+ /*! \brief virtual destructor */
+ virtual ~ExprFunctor() {}
+ /*!
+ * \brief Same as call.
+ * \param n The expression node.
+ * \param args Additional arguments.
+ * \return The result of the call
+ */
+ R operator()(const Expr& n, Args... args) { return VisitExpr(n, std::forward<Args>(args)...); }
+ /*!
+ * \brief The functor call.
+ * \param n The expression node.
+ * \param args Additional arguments.
+ * \return The result of the call
+ */
+ virtual R VisitExpr(const Expr& n, Args... args) {
+ ICHECK(n.defined()) << "Found null pointer node while traversing AST. The previous pass may "
+ "have generated invalid data.";
+ static FType vtable = InitVTable();
+ return vtable(n, this, std::forward<Args>(args)...);
+ }
+ // Functions that can be overriden by subclass
+ // NOTE: cross dialect calls are invoked through global var
+ // We do not expect inline PrimFunc to appear in relax IR.
+ virtual R VisitExpr_(const ConstantNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const TupleNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const VarNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const DataflowVarNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const ShapeExprNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const ExternFuncNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const GlobalVarNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const FunctionNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const CallNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const SeqExprNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const IfNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const OpNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const TupleGetItemNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const PrimValueNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const StringImmNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExpr_(const DataTypeImmNode* op, Args... args) EXPR_FUNCTOR_DEFAULT;
+ virtual R VisitExprDefault_(const Object* op, Args...) {
+ LOG(FATAL) << "Do not have a default for " << op->GetTypeKey();
+ throw;
+ }
+
+ private:
+ // initialize the vtable.
+ static FType InitVTable() {
+ FType vtable;
+ // Set dispatch
+ RELAX_EXPR_FUNCTOR_DISPATCH(ConstantNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(TupleNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(VarNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(DataflowVarNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(ShapeExprNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(ExternFuncNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(GlobalVarNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(FunctionNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(CallNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(SeqExprNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(IfNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(OpNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(TupleGetItemNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(PrimValueNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(StringImmNode);
+ RELAX_EXPR_FUNCTOR_DISPATCH(DataTypeImmNode);
+ return vtable;
+ }
+};
+
+/*!
+ * \brief A simple visitor wrapper around ExprFunctor.
+ * Recursively visit the content.
+ */
+class ExprVisitor : public ExprFunctor<void(const Expr&)> {
+ public:
+ /*!
+ * \brief Generic dispatcher for Expr.
+ * \param expr The expr to be visited.
+ */
+ void VisitExpr(const Expr& expr) override;
+ // specific leaf level visitor functions
+ void VisitExpr_(const ConstantNode* op) override;
+ void VisitExpr_(const TupleNode* op) override;
+ void VisitExpr_(const VarNode* op) override;
+ void VisitExpr_(const DataflowVarNode* op) override;
+ void VisitExpr_(const ShapeExprNode* op) override;
+ void VisitExpr_(const ExternFuncNode* op) override;
+ void VisitExpr_(const GlobalVarNode* op) override;
+ void VisitExpr_(const FunctionNode* op) override;
+ void VisitExpr_(const CallNode* op) override;
+ void VisitExpr_(const SeqExprNode* op) override;
+ void VisitExpr_(const IfNode* op) override;
+ void VisitExpr_(const OpNode* op) override;
+ void VisitExpr_(const TupleGetItemNode* op) override;
+ void VisitExpr_(const PrimValueNode* op) override;
+ void VisitExpr_(const StringImmNode* op) override;
+ void VisitExpr_(const DataTypeImmNode* op) override;
+
+ /*!
+ * \brief Generic dispatcher for bindings.
+ * \param binding The binding to be visited.
+ */
+ virtual void VisitBinding(const Binding& binding);
+ // specific leaf level visitor functions
+ virtual void VisitBinding_(const VarBindingNode* binding);
+ virtual void VisitBinding_(const MatchCastNode* binding);
+ // second level dispatching based on binding value type.
+ // these dispatching functions get called from first-level dispatch on VarBinding
+ virtual void VisitBinding_(const VarBindingNode* binding, const ConstantNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const TupleNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const VarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const DataflowVarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const ShapeExprNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const ExternFuncNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const GlobalVarNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const FunctionNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const CallNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const SeqExprNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const IfNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const OpNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const TupleGetItemNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const PrimValueNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const StringImmNode* val);
+ virtual void VisitBinding_(const VarBindingNode* binding, const DataTypeImmNode* val);
+ /*!
+ * \brief Generic dispatcher for binding blocks.
+ * \param block The binding block to be visited.
+ */
+ virtual void VisitBindingBlock(const BindingBlock& block);
+ // specific leaf level visitor functions
+ virtual void VisitBindingBlock_(const BindingBlockNode* block);
+ virtual void VisitBindingBlock_(const DataflowBlockNode* block);
+
+ /*!
+ * \brief Generic dispatcher for visiting the var definition site.
+ * \param var The var to be visited.
+ * \note VisitExpr_(const VarNode*) will only visit the usage site of an Var
+ */
+ virtual void VisitVarDef(const Var& var);
+
+ /*!
+ * \brief Visit struct_info may recursively contain Expr/PrimExpr.
+ *
+ * By default, this function recurse into struct info such as
+ * TensorStructInfo and ShapeStructInfo and call VisitExpr/VisitPrimExpr
+ * accordingly. It does not recurse into FunctionStructInfo as it does
+ * not contain Expr defined in the current scope.
+ *
+ * Pass writers can overload this function to change to other behaviors.
+ * For example, if we are not interested in Expr in StructInfo, we can
+ * override this function by a no-op.
+ *
+ * \param struct_info Input struct info field.
+ */
+ virtual void VisitExprDepStructInfoField(const StructInfo& struct_info);
+
+ // specific leaf level visitor functions
+ virtual void VisitVarDef_(const VarNode* var);
+ virtual void VisitVarDef_(const DataflowVarNode* var);
+
+ virtual void VisitSpan(const Span& span);
+ virtual void VisitPrimExpr(const PrimExpr& expr);
+
+ private:
+ using TSelf = ExprVisitor;
+ using VisitBindingVTable =
+ tvm::NodeFunctor<void(const ObjectRef& n, ExprVisitor* self, const VarBindingNode* binding)>;
+ // initialize the vtable.
+ static VisitBindingVTable InitVisitBindingVTable();
+ /*!
+ * \brief Private internal struct info field visitor.
+ *
+ * Support default visiting of struct info field and recursive into
+ * their Expr fields.
+ *
+ * We use component instead of sub-classing so there can be other
+ * joint inheritance between ExprVisitor and StructInfoVisitor.
+ */
+ class DefaultStructInfoFieldVisitor : public StructInfoVisitor {
+ public:
+ explicit DefaultStructInfoFieldVisitor(ExprVisitor* parent);
+
+ // Override defaults in struct info visitor.
+ void VisitStructInfoExprField(const Expr& expr) final;
+ void VisitStructInfoExprField(const PrimExpr& expr) final;
+ void VisitStructInfo_(const FuncStructInfoNode* op) final;
+
+ private:
+ ExprVisitor* parent_;
+ };
+ // This visitor is not visible to child classes and only
+ // used to supportd default visiting behavior.
+ DefaultStructInfoFieldVisitor default_struct_info_field_visitor_{this};
+};
+
+void PostOrderVisit(const Expr& node, std::function<void(const Expr&)> fvisit);
+
+/*!
+ * \brief A mutator works in unnormalized form.
+ *
+ * ExprMutatorBase expects input AST to be in the unnormalized form, i.e., checked_type_ and shape_
+ * of expressions can be nullptr, and the expressions may nest(and as a result the AST is not in
+ * ANF).
+ */
+
+class ExprMutatorBase : public ExprFunctor<Expr(const Expr&)> {
+ public:
+ Expr VisitExpr(const Expr& expr) override;
+ Expr VisitExpr_(const ConstantNode* op) override;
+ Expr VisitExpr_(const TupleNode* op) override;
+ Expr VisitExpr_(const VarNode* op) override;
+ Expr VisitExpr_(const DataflowVarNode* op) override;
+ Expr VisitExpr_(const ShapeExprNode* op) override;
+ Expr VisitExpr_(const ExternFuncNode* op) override;
+ Expr VisitExpr_(const GlobalVarNode* op) override;
+ Expr VisitExpr_(const FunctionNode* op) override;
+ Expr VisitExpr_(const CallNode* op) override;
+ Expr VisitExpr_(const SeqExprNode* op) override;
+ Expr VisitExpr_(const IfNode* op) override;
+ Expr VisitExpr_(const OpNode* op) override;
+ Expr VisitExpr_(const TupleGetItemNode* op) override;
+ Expr VisitExpr_(const PrimValueNode* op) override;
+ Expr VisitExpr_(const StringImmNode* op) override;
+ Expr VisitExpr_(const DataTypeImmNode* op) override;
+
+ /*!
+ * \brief Mutate BindingBlock.
+ * \param block The binding block to be visited.
+ * \return The binding block after transformation.
+ */
+ virtual BindingBlock VisitBindingBlock(const BindingBlock& block);
+
+ /*!
+ * \brief Used to visit the PrimExpr inside of expressions.
+ *
+ * Can be overloaded to transform the shape expressions.
+ */
+ virtual PrimExpr VisitPrimExpr(const PrimExpr& expr);
+
+ /*!
+ * \brief Visit struct_info that may recursively contain Expr/PrimExpr.
+ *
+ * By default, this function recurse into struct info such as
+ * TensorStructInfo and ShapeStructInfo and call VisitExpr/VisitPrimExpr
+ * accordingly. It does not recurse into FunctionStructInfo as it does
+ * not contain Expr defined in the current scope.
+ *
+ * Pass writers can overload this function to change to other behaviors.
+ * For example, if in Expr in StructInfo won't change, we can
+ * override this function by an identity function.
+ *
+ * \param struct_info Input struct info field.
+ * \return The updated struct info.
+ */
+ virtual StructInfo VisitExprDepStructInfoField(const StructInfo& struct_info);
+
+ protected:
+ /*!
+ * \brief Check whether VisitExprDepStructInfoField change struct_info.
+ * \return Whether struct info changed.
+ * \note This function is used by mutator implementations to check if
+ * previous Expr update will trigger a change in struct_info.
+ * If change is detected, the implementation can generate a fresh
+ * node without struct_info, and trigger normalizer to re-derive.
+ */
+ bool VisitAndCheckStructInfoFieldUnchanged(const ObjectRef& struct_info) {
+ if (const StructInfoNode* sinfo = struct_info.as<StructInfoNode>()) {
+ return this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo)).same_as(struct_info);
+ } else {
+ return true;
+ }
+ }
+
+ private:
+ /*!
+ * \brief Private internal struct info field visitor to support
+ * Default visiting of struct info field and recursive into their Expr fields.
+ *
+ * We use component instead of sub-classing so there can be other
+ * joint inheritance between ExprMutator and StructInfoMutator.
+ */
+ class DefaultStructInfoFieldMutator : public StructInfoMutator {
+ public:
+ explicit DefaultStructInfoFieldMutator(ExprMutatorBase* parent);
+
+ // Override defaults in struct info visitor.
+ Expr VisitStructInfoExprField(const Expr& expr) final;
+ PrimExpr VisitStructInfoExprField(const PrimExpr& expr) final;
+ StructInfo VisitStructInfo_(const FuncStructInfoNode* op) final;
+
+ private:
+ ExprMutatorBase* parent_;
+ };
+ // This visitor is not visible to child classes and only
+ // used to supportd default visiting behavior.
+ DefaultStructInfoFieldMutator default_struct_info_field_mutator_{this};
+};
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_EXPR_FUNCTOR_H_
diff --git a/include/tvm/relax/struct_info.h b/include/tvm/relax/struct_info.h
index d21c8db86b..f38a32f6bb 100644
--- a/include/tvm/relax/struct_info.h
+++ b/include/tvm/relax/struct_info.h
@@ -22,13 +22,16 @@
#include <tvm/ir/env_func.h>
#include <tvm/ir/source_map.h>
#include <tvm/node/node.h>
-// #include <tvm/relax/block_builder.h>
#include <tvm/relax/expr.h>
#include <tvm/relax/type.h>
namespace tvm {
namespace relax {
+// TODO(relax-team) replace with real BlockBuilder
+// once it is ready.
+using BlockBuilder = ObjectRef;
+
/*!
* \brief Opaque object.
*/
@@ -257,8 +260,6 @@ class TupleStructInfo : public StructInfo {
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TupleStructInfo, StructInfo, TupleStructInfoNode);
};
-class BlockBuilder;
-
/*!
* \brief custom-defined StructInfo derivation function.
* \param call The call expression to be derived.
diff --git a/include/tvm/relax/struct_info_functor.h b/include/tvm/relax/struct_info_functor.h
new file mode 100644
index 0000000000..382b4ab2c9
--- /dev/null
+++ b/include/tvm/relax/struct_info_functor.h
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relax/struct_info_functor.h
+ * \brief Functors and visitors for struct info.
+ */
+#ifndef TVM_RELAX_STRUCT_INFO_FUNCTOR_H_
+#define TVM_RELAX_STRUCT_INFO_FUNCTOR_H_
+
+#include <tvm/node/functor.h>
+#include <tvm/relax/struct_info.h>
+
+#include <utility>
+
+namespace tvm {
+namespace relax {
+
+template <typename FStructInfo>
+class StructInfoFunctor;
+
+// functions to be overriden.
+#define STRUCT_INFO_FUNCTOR_DEFAULT \
+ { return VisitStructInfoDefault_(op, std::forward<Args>(args)...); }
+
+#define TVM_STRUCT_INFO_FUNCTOR_DISPATCH(OP) \
+ vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self, Args... args) { \
+ return self->VisitStructInfo_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \
+ });
+
+template <typename R, typename... Args>
+class StructInfoFunctor<R(const StructInfo& n, Args...)> {
+ private:
+ using TSelf = StructInfoFunctor<R(const StructInfo& n, Args...)>;
+ using FStructInfo = tvm::NodeFunctor<R(const ObjectRef& n, TSelf* self, Args...)>;
+
+ public:
+ /*! \brief the result type of this functor */
+ using result_type = R;
+ /*! \brief virtual destructor */
+ virtual ~StructInfoFunctor() {}
+ /*!
+ * \brief Same as call.
+ * \param n The expression node.
+ * \param args Additional arguments.
+ * \return The result of the call
+ */
+ R operator()(const StructInfo& n, Args... args) {
+ return VisitStructInfo(n, std::forward<Args>(args)...);
+ }
+ /*!
+ * \brief The functor call.
+ * \param n The expression node.
+ * \param args Additional arguments.
+ * \return The result of the call
+ */
+ virtual R VisitStructInfo(const StructInfo& n, Args... args) {
+ ICHECK(n.defined());
+ static FStructInfo vtable = InitVTable();
+ return vtable(n, this, std::forward<Args>(args)...);
+ }
+ // Functions that can be overriden by subclass
+ virtual R VisitStructInfo_(const ObjectStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfo_(const PrimStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfo_(const ShapeStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfo_(const TensorStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfo_(const TupleStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfo_(const FuncStructInfoNode* op,
+ Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
+ virtual R VisitStructInfoDefault_(const Object* op, Args...) {
+ LOG(FATAL) << "Do not have a default for " << op->GetTypeKey();
+ throw; // unreachable, written to stop compiler warning
+ }
+
+ private:
+ // initialize the vtable.
+ static FStructInfo InitVTable() {
+ FStructInfo vtable;
+ // Set dispatch
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(ObjectStructInfoNode);
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(PrimStructInfoNode);
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(ShapeStructInfoNode);
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(TensorStructInfoNode);
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(TupleStructInfoNode);
+ TVM_STRUCT_INFO_FUNCTOR_DISPATCH(FuncStructInfoNode);
+ return vtable;
+ }
+};
+
+#undef TVM_STRUCT_INFO_FUNCTOR_DISPATCH
+
+/*!
+ * \brief A struct info visitor.
+ */
+class TVM_DLL StructInfoVisitor : public StructInfoFunctor<void(const StructInfo& n)> {
+ public:
+ void VisitStructInfo_(const ObjectStructInfoNode* op) override;
+ void VisitStructInfo_(const PrimStructInfoNode* op) override;
+ void VisitStructInfo_(const ShapeStructInfoNode* op) override;
+ void VisitStructInfo_(const TensorStructInfoNode* op) override;
+ void VisitStructInfo_(const TupleStructInfoNode* op) override;
+ void VisitStructInfo_(const FuncStructInfoNode* op) override;
+
+ protected:
+ // two functions to override when visit expr fields in struct info.
+ virtual void VisitStructInfoExprField(const Expr& expr) {}
+ virtual void VisitStructInfoExprField(const PrimExpr& expr) {}
+};
+
+/*!
+ * \brief StructInfoMutator that mutates struct info.
+ */
+class TVM_DLL StructInfoMutator : public StructInfoFunctor<StructInfo(const StructInfo& n)> {
+ public:
+ StructInfo VisitStructInfo_(const ObjectStructInfoNode* op) override;
+ StructInfo VisitStructInfo_(const PrimStructInfoNode* op) override;
+ StructInfo VisitStructInfo_(const ShapeStructInfoNode* op) override;
+ StructInfo VisitStructInfo_(const TensorStructInfoNode* op) override;
+ StructInfo VisitStructInfo_(const TupleStructInfoNode* op) override;
+ StructInfo VisitStructInfo_(const FuncStructInfoNode* op) override;
+
+ protected:
+ // two functions to override when visit expr fields in struct info.
+ virtual Expr VisitStructInfoExprField(const Expr& expr) { return expr; }
+ virtual PrimExpr VisitStructInfoExprField(const PrimExpr& expr) { return expr; }
+};
+
+} // namespace relax
+} // namespace tvm
+#endif // TVM_RELAX_STRUCT_INFO_FUNCTOR_H_
diff --git a/python/tvm/ir/expr.py b/python/tvm/ir/expr.py
index 3c3fefb6d6..f90468de66 100644
--- a/python/tvm/ir/expr.py
+++ b/python/tvm/ir/expr.py
@@ -51,6 +51,17 @@ class RelayExpr(BaseExpr):
raise ValueError("The type checker has not populated" " the checked_type for this node")
return ret
+ @property
+ def struct_info(self) -> "tvm.relax.StructInfo":
+ """Get the struct info field
+
+ Returns
+ -------
+ struct_info : tvm.relax.StructInfo
+ The struct info if available.
+ """
+ return _ffi_api.ExprStructInfo(self)
+
@tvm._ffi.register_object("GlobalVar")
class GlobalVar(RelayExpr):
diff --git a/python/tvm/relax/__init__.py b/python/tvm/relax/__init__.py
index c070fa4791..01310f6455 100644
--- a/python/tvm/relax/__init__.py
+++ b/python/tvm/relax/__init__.py
@@ -17,8 +17,56 @@
# pylint: disable=invalid-name, wrong-import-position
"""The Relax IR namespace containing the IR, type, operator, builder, vm, etc."""
from . import exec_builder
+from . import expr
+from . import ty
+from . import analysis
from . import vm
+from . import struct_info
+
+# Expr
+from .expr import (
+ Expr,
+ Span,
+ SourceName,
+ Id,
+ GlobalVar,
+ Var,
+ DataflowVar,
+ Binding,
+ MatchCast,
+ VarBinding,
+ BindingBlock,
+ DataflowBlock,
+ SeqExpr,
+ ShapeExpr,
+ Tuple,
+ TupleGetItem,
+ Function,
+ ExternFunc,
+ Call,
+ If,
+ Constant,
+ PrimValue,
+ DataTypeImm,
+ StringImm,
+)
+
+from .expr import const, extern, get_shape_of
+
+# Type
+from .ty import Type, ObjectType, ShapeType, DynTensorType, TupleType, FuncType, PackedFuncType
# VM
from .exec_builder import ExecBuilder
from .vm import VirtualMachine
+
+# StructInfo
+from .struct_info import (
+ StructInfo,
+ ObjectStructInfo,
+ PrimStructInfo,
+ ShapeStructInfo,
+ TensorStructInfo,
+ TupleStructInfo,
+ FuncStructInfo,
+)
diff --git a/python/tvm/script/__init__.py b/python/tvm/relax/analysis/__init__.py
similarity index 84%
copy from python/tvm/script/__init__.py
copy to python/tvm/relax/analysis/__init__.py
index 9283727ad4..cc0089ff31 100644
--- a/python/tvm/script/__init__.py
+++ b/python/tvm/relax/analysis/__init__.py
@@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""TVM Script APIs of TVM Python Package"""
-from .parser import ir, ir_module
-from .parser import parse as from_source
-from .parser import tir
+# pylint: disable=wildcard-import, redefined-builtin
+"""Relax IR analysis. """
+
+from .analysis import *
diff --git a/python/tvm/script/__init__.py b/python/tvm/relax/analysis/_ffi_api.py
similarity index 82%
copy from python/tvm/script/__init__.py
copy to python/tvm/relax/analysis/_ffi_api.py
index 9283727ad4..40ee05c396 100644
--- a/python/tvm/script/__init__.py
+++ b/python/tvm/relax/analysis/_ffi_api.py
@@ -13,8 +13,7 @@
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
-# under the License.
-"""TVM Script APIs of TVM Python Package"""
-from .parser import ir, ir_module
-from .parser import parse as from_source
-from .parser import tir
+"""FFI APIs"""
+import tvm._ffi
+
+tvm._ffi._init_api("relax.analysis", __name__)
diff --git a/python/tvm/relax/analysis/analysis.py b/python/tvm/relax/analysis/analysis.py
new file mode 100644
index 0000000000..301f3ecc72
--- /dev/null
+++ b/python/tvm/relax/analysis/analysis.py
@@ -0,0 +1,135 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=no-else-return
+# pylint: disable=unidiomatic-typecheck
+"""
+This file contains the set of passes for Relax, which exposes an interface for
+configuring the passes and scripting them in Python.
+"""
+
+from typing import Dict
+from enum import IntEnum
+
+from tvm import tir
+from tvm.relax.ty import Type
+from tvm.relax.struct_info import StructInfo
+from tvm.relax.expr import Var, Expr
+from . import _ffi_api
+
+
+def get_static_type(sinfo: StructInfo) -> Type:
+ """Get the corresponding static type from a StructInfo.
+
+ Parameters
+ ----------
+ sinfo : StructInfo
+ The input struct info.
+
+ Returns
+ -------
+ ret : Type
+ The corresponding static type.
+ """
+ return _ffi_api.GetStaticType(sinfo) # type: ignore
+
+
+def erase_to_well_defined(
+ sinfo: StructInfo,
+ shape_var_map: Dict[tir.Var, tir.PrimExpr] = None,
+ var_map: Dict[Var, Expr] = None,
+) -> StructInfo:
+ """Erase sinfo into a well defined form.
+
+ This function removes the StructInfo's dependencies on shape and vars that
+ are not defined in given maps.
+
+ Parameters
+ ----------
+ sinfo : StructInfo
+ The input struct info.
+
+ shape_var_map : Dict[tir.Var, tir.PrimExpr]
+ Specifies the defined shape vars and the values they should map to.
+
+ var_map : Dict[Var, Expr]
+ Specifies the defined vars and the values they should map to.
+
+ Returns
+ -------
+ ret : StructInfo
+ The corresponding erased struct info.
+ """
+ shape_var_map = {} if shape_var_map is None else shape_var_map
+ var_map = {} if var_map is None else var_map
+
+ return _ffi_api.EraseToWellDefined(sinfo, shape_var_map, var_map) # type: ignore
+
+
+class BaseCheckResult(IntEnum):
+ """Return result of fine-grained base check.
+
+ Note
+ ----
+ Base check comes with fine-grained fail levels.
+
+ - FAIL_L0: The lhs and rhs have no intersection at all.
+ - FAIL_L1: We get the failure by looking at static information.
+ - FAIL_L2: We get the failure due to unknown symbolic variable relations.
+ """
+
+ FAIL_L0 = 0
+ FAIL_L1 = 1
+ FAIL_L2 = 2
+ PASS = 3
+
+
+def struct_info_base_check(base: StructInfo, derived: StructInfo) -> BaseCheckResult:
+ """Run a base check to see if base subsumes derived.
+
+ Parameters
+ ----------
+ base: StructInfo
+ The base struct info.
+
+ derived: StructInfo
+ The derived struct info.
+
+ Returns
+ -------
+ ret : StructInfo
+ The derived return value struct info.
+ """
+ return _ffi_api.StructInfoBaseCheck(base, derived) # type: ignore
+
+
+def struct_info_lca(lhs: StructInfo, rhs: StructInfo) -> StructInfo:
+ """Unify the two struct info to their least common ancestor.
+
+ Parameters
+ ----------
+ lhs: StructInfo
+ The left operand.
+
+ rhs: StructInfo
+ The right operand.
+
+ Returns
+ -------
+ ret : StructInfo
+ The corresponding lca result.
+ """
+ return _ffi_api.StructInfoLCA(lhs, rhs) # type: ignore
diff --git a/python/tvm/relax/expr.py b/python/tvm/relax/expr.py
new file mode 100644
index 0000000000..138724ed06
--- /dev/null
+++ b/python/tvm/relax/expr.py
@@ -0,0 +1,729 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name, unused-import, super-init-not-called
+# pylint: disable=redefined-builtin
+"""The expression nodes of Relax."""
+import typing
+from numbers import Number
+from typing import Any, Callable, Dict, List, Optional, Union
+
+import numpy as _np # type: ignore
+import tvm
+import tvm._ffi
+import tvm.relax
+import tvm.ir
+from tvm import DataType
+from tvm._ffi import base as _base
+from tvm.runtime import ndarray as _nd, Object
+
+from ..ir import BaseFunc, Node, SourceName, Span
+from ..runtime import String
+from ..tir import PrimExpr
+from . import _ffi_api
+
+# It is a workaround for mypy: https://github.com/python/mypy/issues/7866#issuecomment-549454370
+# This feature is not supported until python 3.10:
+# https://docs.python.org/3.10/whatsnew/3.10.html#pep-613-typealias
+Expr = Union[tvm.ir.RelayExpr]
+Type = Union[tvm.ir.Type]
+GlobalVar = Union[tvm.ir.GlobalVar]
+
+
+@tvm._ffi.register_object("relax.Id")
+class Id(Object):
+ """Unique identifier(name) used in Var.
+ Guaranteed to be stable across all passes.
+ """
+
+ def __init__(self):
+ raise RuntimeError("Cannot directly construct Id")
+
+
+# NOTE: place base struct info in expr to avoid cyclic dep
+# from expr to struct info.
+class StructInfo(Node):
+ """The base class of all StructInfo.
+
+ StructInfo contains both the static type
+ and runtime structural information.
+ """
+
+ def __eq__(self, other):
+ """Compare two struct info for structural equivalence."""
+ return tvm.ir.structural_equal(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def same_as(self, other):
+ """Overload with structural equality."""
+ return super().__eq__(other)
+
+ def is_base_of(self, derived: "StructInfo") -> bool:
+ """Check if self is base of another derived struct info.
+
+ Parameters
+ ----------
+ derived : StructInfo
+ The derived struct info to be checked.
+
+ Returns
+ -------
+ result : bool
+ The check result.
+ """
+ return _ffi_api.StructInfoIsBaseOf(self, derived) # type: ignore
+
+
+# will be registered afterwards in python/tvm/relax/op/init.py
+_op_ffi_api = None
+
+
+def _binary_op_helper(lhs: "ExprWithOp", rhs: "ExprWithOp", op: Callable) -> "ExprWithOp":
+ if not isinstance(lhs, Expr): # type: ignore
+ raise ValueError("lhs must be Expr")
+ if isinstance(rhs, Expr): # type: ignore
+ return op(lhs, rhs)
+ elif isinstance(rhs, Number):
+ raise TypeError(f"Please convert {rhs} with `const` first")
+ else:
+ raise TypeError(f"type {type(rhs)} not supported")
+
+
+def _binary_rhs_helper(rhs: "ExprWithOp") -> "ExprWithOp":
+ if isinstance(rhs, Number):
+ raise TypeError(f"Please convert {rhs} with `const` first")
+ raise TypeError(f"type {type(rhs)} not supported")
+
+
+class ExprWithOp(Expr):
+ """Basetype of all relax expressions that defines op overloading."""
+
+ def astype(self, dtype: Union[str, DataType]) -> "ExprWithOp":
+ """Cast the content type of the current data to dtype.
+
+ Parameters
+ ----------
+ dtype : str
+ The target data type.
+
+ Note
+ ----
+ This function only works for TensorType Exprs.
+
+ Returns
+ -------
+ result : ExprWithOp
+ The result expression.
+ """
+ return _op_ffi_api.astype(self, dtype) # type: ignore
+
+ def __neg__(self) -> "ExprWithOp":
+ raise ValueError("relax.negative is not supported yet.")
+
+ def __lt__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.less) # type: ignore
+
+ def __gt__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.greater) # type: ignore
+
+ def __ge__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.greater_equal) # type: ignore
+
+ def __le__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.less_equal) # type: ignore
+
+ # NOTE: Cannot override __eq__ and __ne__, which will influence object equal
+
+ def __add__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.add) # type: ignore
+
+ def __radd__(self, other: Expr) -> "ExprWithOp":
+ return self.__add__(other)
+
+ def __sub__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.subtract) # type: ignore
+
+ def __rsub__(self, other: Expr) -> "ExprWithOp":
+ return _binary_rhs_helper(other)
+
+ def __mul__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.multiply) # type: ignore
+
+ def __rmul__(self, other: Expr) -> "ExprWithOp":
+ return self.__mul__(other)
+
+ def __truediv__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.divide) # type: ignore
+
+ def __rtruediv__(self, other: Expr) -> "ExprWithOp":
+ return _binary_rhs_helper(other)
+
+ def __floordiv__(self, other: Expr) -> "ExprWithOp":
+ return _binary_op_helper(self, other, _op_ffi_api.floor_divide) # type: ignore
+
+ def __rfloordiv__(self, other: Expr) -> "ExprWithOp":
+ return _binary_rhs_helper(other)
+
+ def __mod__(self, other: Expr) -> "ExprWithOp":
+ # TODO(siyuan): Support it after mod operator is supported in relax
+ raise ValueError("relax.mod is not supported yet.")
+
+ def __rmod__(self, other: Expr) -> "ExprWithOp":
+ return _binary_rhs_helper(other)
+
+ def __call__(self, *args: List[Expr], attrs: Optional[Dict[str, Any]] = None) -> "ExprWithOp":
+ """Call the variable (if it represents a function).
+
+ Parameters
+ ----------
+ args: List[Expr]
+ The arguments to the call.
+
+ attr: Optional[Dict[str, object]]
+ The additional attributes to the call.
+
+ Returns
+ -------
+ call: ExprWithOp
+ A call taking the variable as a function.
+ """
+ return Call(self, args, attrs=attrs)
+
+ def __getitem__(self, index: int) -> "ExprWithOp":
+ """Get the i-th element of the tuple or Expr with TupleType.
+
+ Parameters
+ ----------
+ index: int
+ The index of the element to be retrieved.
+
+ Note
+ ----
+ This function will be overridden by Tuple and ShapeExpr
+
+ Returns
+ -------
+ result: ExprWithOp
+ The result expression.
+ """
+ return TupleGetItem(self, index)
+
+
+@tvm._ffi.register_object("relax.expr.Call")
+class Call(ExprWithOp):
+ """Function call node in Relax.
+
+ Call node corresponds the operator application node
+ in computational graph terminology.
+
+ Parameters
+ ----------
+ op: tvm.ir.Op or any tvm.relax.Expr with function type.
+ The operation to be called.
+
+ args: Union[List[Expr], typing.Tuple[Expr, ...]]
+ The arguments to the call.
+
+ attrs: Optional[tvm.ir.Attrs]
+ Attributes to the call, can be None
+
+ sinfo_args: Optional[Union[List[StructInfo], typing.Tuple[StructInfo, ...]]]
+ The structure info arguments of a CallNode.
+ sinfo_args is designed to be non-empty only for intrinsic op (e.g.,
+ call_tir, call_builtin_with_ctx, etc.) and calls to ExternFuncs, with the main
+ usage of structure info inference.
+
+ span: Optional[Span]
+ Span that points to original source code
+ """
+
+ def __init__(
+ self,
+ op: Union[Expr, tvm.ir.Op],
+ args: Union[List[Expr], typing.Tuple[Expr, ...]],
+ attrs: Optional[tvm.ir.Attrs] = None,
+ sinfo_args: Optional[Union[List[StructInfo], typing.Tuple[StructInfo, ...]]] = None,
+ span: Optional[Span] = None,
+ ):
+ if not sinfo_args:
+ sinfo_args = []
+ self.__init_handle_by_constructor__(
+ _ffi_api.Call, op, args, attrs, sinfo_args, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.expr.If")
+class If(ExprWithOp):
+ """A conditional expression in Relax.
+
+ Parameters
+ ----------
+ cond: Expr
+ The condition.
+
+ true_branch: Expr
+ The expression evaluated when condition is true.
+
+ false_branch: Expr
+ The expression evaluated when condition is false.
+ """
+
+ def __init__(self, cond: Expr, true_branch: Expr, false_branch: Expr, span: Span = None):
+ self.__init_handle_by_constructor__(
+ _ffi_api.If, cond, true_branch, false_branch, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.expr.Tuple")
+class Tuple(ExprWithOp):
+ """Tuple expression that groups several fields together.
+
+ Parameters
+ ----------
+ fields : Union[List[Expr], typing.Tuple[Expr, ...]]
+ The fields in the tuple.
+
+ span: Optional[Span]
+ Span that points to original source code
+ """
+
+ def __init__(self, fields: Union[List[Expr], typing.Tuple[Expr, ...]], span: Span = None):
+ self.__init_handle_by_constructor__(_ffi_api.Tuple, fields, span) # type: ignore
+
+ def __getitem__(self, index: int) -> Expr:
+ if index >= len(self) or index < -len(self):
+ raise IndexError("Tuple index out of range")
+ return self.fields[index]
+
+ def __len__(self) -> int:
+ return len(self.fields)
+
+
+@tvm._ffi.register_object("relax.expr.TupleGetItem")
+class TupleGetItem(ExprWithOp):
+ """Get index-th item from a tuple.
+
+ Parameters
+ ----------
+ tuple_value: Expr
+ The input tuple expression.
+
+ index: int
+ The index.
+ """
+
+ def __init__(self, tuple_value: Expr, index: int):
+ self.__init_handle_by_constructor__(
+ _ffi_api.TupleGetItem, tuple_value, index # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.expr.ShapeExpr")
+class ShapeExpr(ExprWithOp):
+ """A shape expression which allows users to construct a shape containing PrimExpr."""
+
+ values: List[PrimExpr]
+
+ def __init__(
+ self,
+ values: Union[List[PrimExpr], typing.Tuple[PrimExpr, ...], tvm.ir.Array],
+ span: Span = None,
+ ) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.ShapeExpr, values, span) # type: ignore
+
+ def __getitem__(self, index):
+ if index >= len(self) or index < -len(self):
+ raise IndexError("ShapeExpr index out of range")
+ return self.values[index]
+
+ def __len__(self):
+ return len(self.values)
+
+
+def make_shape(shape: Union[List[Any], typing.Tuple[Any, ...]]) -> ShapeExpr:
+ if isinstance(shape, (list, tuple)):
+ return ShapeExpr(shape)
+ raise ValueError("Wrong type")
+
+
+@tvm._ffi.register_object("relax.expr.Constant")
+class Constant(ExprWithOp):
+ def __init__(self, data: tvm.nd.NDArray, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.Constant, data, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.Var")
+class Var(ExprWithOp):
+ """The variable class for all Relax bindings."""
+
+ vid: Id
+ struct_info: Optional[StructInfo]
+
+ def __init__(
+ self,
+ name_hint: Union[str, Id],
+ struct_info: Optional[StructInfo] = None,
+ span: Span = None,
+ ) -> None:
+ if struct_info is not None:
+ struct_info = tvm.runtime.convert_to_object(struct_info)
+ if not isinstance(struct_info, StructInfo):
+ raise TypeError(
+ "struct_info needs to be an instance of StructInfo. "
+ "If you attempt to pass in shape, "
+ "use relax.TensorStructInfo(shape, dtype)."
+ )
+ self.__init_handle_by_constructor__(
+ _ffi_api.Var if isinstance(name_hint, str) else _ffi_api.VarFromId, # type: ignore
+ name_hint,
+ struct_info,
+ span,
+ )
+
+ @property
+ def name_hint(self):
+ """Get name hint of the current var."""
+ name = str(self.vid.name_hint)
+ return name
+
+
+@tvm._ffi.register_object("relax.expr.DataflowVar")
+class DataflowVar(Var):
+ """A sub-type of the variable node used to mark dataflow variables from
+ normal visible "function local" bindings."""
+
+ vid: Id
+ struct_info: Optional[StructInfo]
+
+ def __init__(
+ self,
+ name_hint: Union[str, Id],
+ struct_info: Optional[StructInfo] = None,
+ span: Span = None,
+ ) -> None:
+ if struct_info is not None:
+ struct_info = tvm.runtime.convert_to_object(struct_info)
+ if not isinstance(struct_info, StructInfo):
+ raise TypeError(
+ "struct_info needs to be an instance of StructInfo. "
+ "If you attempt to pass in shape, "
+ "use relax.TensorStructInfo(shape, dtype)."
+ )
+
+ self.__init_handle_by_constructor__(
+ _ffi_api.DataflowVar # type: ignore
+ if isinstance(name_hint, str)
+ else _ffi_api.DataflowVarFromId, # type: ignore
+ name_hint,
+ struct_info,
+ span,
+ )
+
+
+@tvm._ffi.register_object("relax.expr.PrimValue")
+class PrimValue(Expr):
+ """The prim expr representing the value."""
+
+ value: PrimExpr
+
+ def __init__(self, value: Union[PrimExpr, int], span: Span = None) -> None:
+ if isinstance(value, int):
+ value = tvm.tir.IntImm("int64", value)
+ self.__init_handle_by_constructor__(_ffi_api.PrimValue, value, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.StringImm")
+class StringImm(Expr):
+ """Represent a string literal constant."""
+
+ value: str
+
+ def __init__(self, value: str, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.StringImm, value, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.DataTypeImm")
+class DataTypeImm(Expr):
+ """Represent a data type constant."""
+
+ value: DataType
+
+ def __init__(self, value: Union[DataType, str], span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.DataTypeImm, value, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.Binding")
+class Binding(Node):
+ """The base class of a binding in Relax."""
+
+ ...
+
+
+@tvm._ffi.register_object("relax.expr.MatchCast")
+class MatchCast(Binding):
+ """Runtime-match the value to the struct info.
+
+ This operation does runtime check, populates the un-defined symbolic shape vars
+ and vars in struct_info in the first occurrence, and insert equality assertions in
+ other cases.
+
+ Parameters
+ ----------
+ var: Var
+ The return variable that the match cast bind to.
+
+ value: Expr
+ The input value expression.
+
+ struct_info: tvm.relax.StructInfo
+ The struct info to match cast to.
+ """
+
+ var: Var
+ struct_info: "tvm.relax.StructInfo"
+ value: Expr
+
+ def __init__(
+ self, var: Var, value: Expr, struct_info: "tvm.relax.StructInfo", span: Span = None
+ ) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.MatchCast, var, value, struct_info, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.expr.VarBinding")
+class VarBinding(Binding):
+ """Variable binding, bind he variable of the lhs with the rhs."""
+
+ var: Var
+ value: Expr
+
+ def __init__(self, var: Var, value: Expr, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.VarBinding, var, value, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.BindingBlock")
+class BindingBlock(Node):
+ """base class of binding block, bindings inside can be impure
+ (with side effect or control flow)"""
+
+ bindings: List[Binding]
+
+ def __init__(self, bindings: List[Binding], span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.BindingBlock, bindings, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.DataflowBlock")
+class DataflowBlock(BindingBlock):
+ """dataflow block, bindings inside are pure (no side effect and no control flow)"""
+
+ def __init__(self, bindings: List[Binding], span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.DataflowBlock, bindings, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.SeqExpr")
+class SeqExpr(ExprWithOp):
+ """A sequence of binding blocks followed by an expression."""
+
+ blocks: List[BindingBlock]
+ body: Expr
+
+ def __init__(self, blocks: List[BindingBlock], body: Expr, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.SeqExpr, blocks, body, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.expr.Function")
+class Function(BaseFunc):
+ """A Relax function."""
+
+ params: List[Var]
+ body: Expr
+ ret_struct_info: StructInfo
+ attrs: Optional[tvm.ir.DictAttrs]
+
+ def __init__(
+ self,
+ params: List[Var],
+ body: Expr,
+ ret_struct_info: Optional[StructInfo] = None,
+ attrs: Optional[tvm.ir.DictAttrs] = None,
+ span: Optional[Span] = None,
+ ) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.Function, params, body, ret_struct_info, attrs, span # type: ignore
+ )
+
+ @staticmethod
+ def create_empty(
+ params: List[Var],
+ ret_struct_info: StructInfo,
+ attrs: Optional[tvm.ir.DictAttrs] = None,
+ span: Optional[Span] = None,
+ ):
+ """Construct a relax.Function but without body"""
+ return _ffi_api.FunctionCreateEmpty(params, ret_struct_info, attrs, span) # type: ignore
+
+ def __call__(self, *args):
+ """Invoke the global function.
+
+ Parameters
+ ----------
+ args: List[relax.Expr]
+ Arguments.
+ """
+ return Call(self, args, None, None)
+
+ def script(self, show_meta: bool = False) -> str:
+ """Print relax.Function into TVMScript
+
+ Parameters
+ ----------
+ show_meta : bool
+ Whether to show meta information
+
+ Returns
+ -------
+ script : str
+ The TVM Script of the relax.Function
+ """
+ return tvm._ffi.get_global_func("script.AsRelaxScript")(self, show_meta) # type: ignore
+
+ def show(self, style: str = "light") -> None:
+ """
+ A sugar for print highlighted TVM script.
+
+ Parameters
+ ----------
+ style : str, optional
+ Pygments styles extended by "light" (default) and "dark", by default "light"
+ """
+ from tvm.script.highlight import cprint # pylint: disable=import-outside-toplevel
+
+ # Use deferred import to avoid circular import while keeping cprint under tvm/script
+ cprint(self, style=style)
+
+
+@tvm._ffi.register_object("relax.expr.ExternFunc")
+class ExternFunc(BaseFunc):
+ """extern function, which can represent a TIR PrimFunc or a PackedFunc."""
+
+ global_symbol: String
+
+ def __init__(self, global_symbol: String, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.ExternFunc, global_symbol, span # type: ignore
+ )
+
+
+def extern(name: str, span: Span = None):
+ """Create extern function."""
+ return ExternFunc(name, span)
+
+
+def const(
+ value: Union[bool, int, float, _np.ndarray, tvm.nd.NDArray], dtype: Optional[str] = None
+) -> Constant:
+ """Create a constant value.
+
+ Parameters
+ ----------
+ value: Union[bool, int, float, numpy.ndarray, tvm.nd.NDArray]
+ The constant value.
+
+ dtype: Optional[str]
+ The data type of the resulting constant.
+
+ Note
+ ----
+ When dtype is None, we use the following rule:
+
+ - int maps to "int32"
+ - float maps to "float32"
+ - bool maps to "bool"
+ - other using the same default rule as numpy.
+ """
+ if isinstance(value, (_base.numeric_types, (bool, list))):
+ value = _np.array(value, dtype=dtype)
+
+ if not dtype:
+ # when dtype is None: int maps to "int32", float maps to "float32"
+ dtype = { # type: ignore
+ _np.dtype("int64"): _np.int32, # type: ignore
+ _np.dtype("float64"): _np.float32, # type: ignore
+ }.get(
+ value.dtype, None # type: ignore
+ )
+
+ if isinstance(value, (_np.ndarray, _np.generic)):
+ if dtype is not None:
+ value = value.astype(dtype)
+ value = _nd.array(value)
+
+ if not isinstance(value, _nd.NDArray):
+ raise ValueError("value has to be scalar or NDArray")
+
+ return Constant(value)
+
+
+def te_tensor(
+ value: Expr, tir_var_map: Dict[tvm.tir.Var, tvm.tir.PrimExpr], name: str = "rxplaceholder"
+):
+ """Create a TE tensor from relax expression, with TIR variables in the
+ tensor shape substituted by the given mapping
+
+ Parameters
+ ----------
+ value : Expr
+ The relax expression, which is required to have TensorStructInfo.
+
+ tir_var_map : Dict[tvm.tir.Var, tvm.tir.PrimExpr]
+ The mapping to substitute the TIR variables appeared in the
+ shape of the input Expr.
+
+ name : str
+ The name of the created tensor.
+ """
+ return _ffi_api.TETensor(value, tir_var_map, name) # type: ignore
+
+
+def get_shape_of(expr: Expr) -> Expr:
+ """Get shape of expr.
+
+ Parameters
+ ----------
+ expr: Expr
+ The input expr.
+
+ Returns
+ -------
+ shape: Expr
+ The shape expression
+
+ Note
+ ----
+ This function requires expr to be normalized.
+ The function will report an error if expr's StructInfo is not TensorStructInfo.
+ It will try to return symbolic function when possible. If the tensor do not
+ have a compile-time symbolic shape, the function will then choose to return
+ `Call(relax.op.shape_of, [expr])`.
+ """
+ return _ffi_api.GetShapeOf(expr) # type: ignore
+
+
+def _update_struct_info(expr: Expr, struct_info: Optional[StructInfo]) -> None:
+ _ffi_api.UpdateStructInfo(expr, struct_info) # type: ignore
diff --git a/python/tvm/relax/struct_info.py b/python/tvm/relax/struct_info.py
new file mode 100644
index 0000000000..2ff027b229
--- /dev/null
+++ b/python/tvm/relax/struct_info.py
@@ -0,0 +1,197 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name, unused-import
+"""The struct info nodes of the Relax language."""
+from typing import List, Optional, Tuple, Union
+
+import tvm._ffi
+import tvm
+
+from tvm.ir import Span, Node, EnvFunc, Array, Type
+from tvm.tir import PrimExpr
+from .expr import StructInfo, Var, Expr, ShapeExpr
+
+from . import _ffi_api, ty, expr
+
+
+@tvm._ffi.register_object("relax.ObjectStructInfo")
+class ObjectStructInfo(StructInfo):
+ """StructInfo of an Object."""
+
+ def __init__(self, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.ObjectStructInfo, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.PrimStructInfo")
+class PrimStructInfo(StructInfo):
+ """StructInfo of a primitive POD value.
+
+ Parameters
+ ----------
+ dtype : str
+ The data type of the prim value.
+ """
+
+ dtype: str
+
+ def __init__(self, dtype: str, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.PrimStructInfo, dtype, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.ShapeStructInfo")
+class ShapeStructInfo(StructInfo):
+ """StructInfo of a shape value.
+
+ Parameters
+ ----------
+ values : Optional[List[PrimExpr]]
+ The symbolic shape values if known.
+
+ ndim : Optional[int]
+ The size of the shape.
+
+ Note
+ ----
+ Do not specify values and ndim at the same time.
+ """
+
+ values: Optional[List[PrimExpr]]
+ ndim: int
+ span: Span
+
+ def __init__(
+ self, values: Optional[List[PrimExpr]] = None, ndim: int = -1, span: Span = None
+ ) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.ShapeStructInfo, values, ndim, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.TensorStructInfo")
+class TensorStructInfo(StructInfo):
+ """StructInfo of a Tensor value.
+
+ Parameters
+ ----------
+ shape : Optional[Expr]
+ The shape expression.
+
+ dtype : Optional[str]
+ The content data type.
+
+ ndim : Optional[int]
+ The number of dimensions of the tensor.
+
+ Note
+ ----
+ Do not specify shape and ndim at the same time.
+ """
+
+ shape: Optional[Expr]
+ dtype: str
+ ndim: int
+ span: Span
+
+ def __init__(
+ self,
+ shape: Union[Optional[Expr], List[PrimExpr]] = None,
+ dtype: str = "float32",
+ ndim: int = -1,
+ span: Span = None,
+ ) -> None:
+ if isinstance(shape, (list, tuple, Array)):
+ shape = ShapeExpr(shape)
+
+ self.__init_handle_by_constructor__(
+ _ffi_api.TensorStructInfo, shape, dtype, ndim, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.TupleStructInfo")
+class TupleStructInfo(StructInfo):
+ """StructInfo of a Tuple value.
+
+ Parameters
+ ----------
+ fields: List[StructInfo]
+ The struct info of the fields.
+ """
+
+ fields: List[StructInfo]
+ span: Span
+
+ def __init__(self, fields: List[StructInfo], span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.TupleStructInfo, fields, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.FuncStructInfo")
+class FuncStructInfo(StructInfo):
+ """StructInfo of a function value.
+
+ Parameters
+ ----------
+ params: List[StructInfo]
+ The struct info of the fields.
+
+ ret: StructInfo
+ The struct info of return value
+ """
+
+ params: Optional[List[StructInfo]]
+ ret: StructInfo
+ derive_func: Optional[EnvFunc]
+ span: Span
+
+ def __init__(self, params: List[StructInfo], ret: StructInfo, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.FuncStructInfo, params, ret, span # type: ignore
+ )
+
+ @staticmethod
+ def opaque_func(
+ *,
+ ret: Optional[StructInfo] = None,
+ derive_func: Optional[EnvFunc] = None,
+ span: Span = None,
+ ) -> "FuncStructInfo":
+ """
+ Create an opaque FuncStructInfo.
+
+ The opaque function takes either a ret
+ that specificies the struct info of the return value
+ or a derive_func that provides a customized derivation rule.
+
+ Parameters
+ ----------
+ ret: Optional[StructInfo]
+ The struct info of the the function return value.
+
+ derive_func: Optional[EnvFunc]
+ The environment function used for derivation
+
+ span: Optional[Span]
+ Optional span information of the ast.
+
+ Returns
+ -------
+ info: FuncStructInfo
+
+ Note
+ ----
+ We cannot specify ret and derive_func simultaneously.
+ """
+ return _ffi_api.FuncStructInfoOpaqueFunc(ret, derive_func, span) # type: ignore
diff --git a/python/tvm/relax/ty.py b/python/tvm/relax/ty.py
new file mode 100644
index 0000000000..05492d6a9c
--- /dev/null
+++ b/python/tvm/relax/ty.py
@@ -0,0 +1,75 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=invalid-name, unused-import
+"""The type nodes of the Relax language."""
+import tvm._ffi
+from tvm.ir import Type, TensorType, TupleType, FuncType, Span
+
+from . import _ffi_api
+
+
+@tvm._ffi.register_object("relax.ShapeType")
+class ShapeType(Type):
+ """The type of shape in Relax.
+
+ Parameters
+ ----------
+ ndim : Optional[int]
+ The size of the shape.
+ """
+
+ # TODO(relax-team): consider make ndim mandatory
+ def __init__(self, ndim: int = -1, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.ShapeType, ndim, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.ObjectType")
+class ObjectType(Type):
+ """A type that corresponds to tvm::runtime::Object, is base of all possible object
+ values in TVM."""
+
+ def __init__(self, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.ObjectType, span) # type: ignore
+
+
+@tvm._ffi.register_object("relax.DynTensorType")
+class DynTensorType(Type):
+ """A dynamic tensor type in Relax.
+
+ This is the type assigned to tensors with a known dtype and unknown shape.
+
+ Parameters
+ ----------
+ ndim : Optional[int]
+ The ndim of the Tensor
+
+ dtype : Optional[str]
+ The content data type.
+ """
+
+ def __init__(self, ndim=-1, dtype="float32", span: Span = None) -> None:
+ self.__init_handle_by_constructor__(
+ _ffi_api.DynTensorType, ndim, dtype, span # type: ignore
+ )
+
+
+@tvm._ffi.register_object("relax.PackedFuncType")
+class PackedFuncType(Type):
+ """The type of ExternFunc in Relax."""
+
+ def __init__(self, span: Span = None) -> None:
+ self.__init_handle_by_constructor__(_ffi_api.PackedFuncType, span) # type: ignore
diff --git a/python/tvm/script/__init__.py b/python/tvm/script/__init__.py
index 9283727ad4..6d92c68367 100644
--- a/python/tvm/script/__init__.py
+++ b/python/tvm/script/__init__.py
@@ -18,3 +18,4 @@
from .parser import ir, ir_module
from .parser import parse as from_source
from .parser import tir
+from .parser import relax
diff --git a/python/tvm/script/__init__.py b/python/tvm/script/parser/relax/__init__.py
similarity index 83%
copy from python/tvm/script/__init__.py
copy to python/tvm/script/parser/relax/__init__.py
index 9283727ad4..feb8e68340 100644
--- a/python/tvm/script/__init__.py
+++ b/python/tvm/script/parser/relax/__init__.py
@@ -14,7 +14,8 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""TVM Script APIs of TVM Python Package"""
-from .parser import ir, ir_module
-from .parser import parse as from_source
-from .parser import tir
+"""Initial impl of relax parser for sugars"""
+from tvm.relax import TensorStructInfo, ShapeStructInfo
+
+Tensor = TensorStructInfo
+Shape = ShapeStructInfo
diff --git a/src/ir/function.cc b/src/ir/function.cc
index ce294708b2..69752f529a 100644
--- a/src/ir/function.cc
+++ b/src/ir/function.cc
@@ -22,6 +22,8 @@
* \brief The function data structure.
*/
#include <tvm/ir/function.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relay/function.h>
#include <tvm/runtime/registry.h>
#include <tvm/tir/function.h>
@@ -35,13 +37,13 @@ TVM_REGISTER_GLOBAL("ir.BaseFuncWithAttr")
.set_body_typed([](BaseFunc func, String key, ObjectRef value) -> BaseFunc {
if (func->IsInstance<tir::PrimFuncNode>()) {
return WithAttr(Downcast<tir::PrimFunc>(std::move(func)), key, value);
+ } else if (func->IsInstance<relay::FunctionNode>()) {
+ return WithAttr(Downcast<relay::Function>(std::move(func)), key, value);
+ } else if (func->IsInstance<relax::FunctionNode>()) {
+ return WithAttr(Downcast<relax::Function>(std::move(func)), key, value);
+ } else {
+ LOG(FATAL) << "Do not support function type " << func->GetTypeKey();
}
- if (const auto* f = runtime::Registry::Get("relay.ir.FuncWithAttr")) {
- if (Optional<BaseFunc> ret = (*f)(func, key, value)) {
- return ret.value();
- }
- }
- LOG(FATAL) << "Do not support function type " << func->GetTypeKey();
});
} // namespace tvm
diff --git a/src/ir/type.cc b/src/ir/type.cc
index d965406e8b..b61a3df091 100644
--- a/src/ir/type.cc
+++ b/src/ir/type.cc
@@ -25,9 +25,10 @@
#include <tvm/runtime/registry.h>
namespace tvm {
-PrimType::PrimType(runtime::DataType dtype) {
+PrimType::PrimType(runtime::DataType dtype, Span span) {
ObjectPtr<PrimTypeNode> n = make_object<PrimTypeNode>();
n->dtype = dtype;
+ n->span = std::move(span);
data_ = std::move(n);
}
diff --git a/src/relax/analysis/shape_analysis.cc b/src/relax/analysis/shape_analysis.cc
new file mode 100644
index 0000000000..70ce5ac06e
--- /dev/null
+++ b/src/relax/analysis/shape_analysis.cc
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file shape_analysis.cc
+ *
+ * \brief Utilities for shape analysis.
+ */
+
+#include <tvm/arith/analyzer.h>
+#include <tvm/relax/analysis.h>
+
+namespace tvm {
+namespace relax {
+
+bool CanProveShapeEqual(const Array<PrimExpr>& lhs, const Array<PrimExpr>& rhs,
+ arith::Analyzer* ana) {
+ if (lhs.same_as(rhs)) return true;
+ if (lhs.size() != rhs.size()) return false;
+ for (size_t i = 0; i < lhs.size(); ++i) {
+ if (!ana->CanProveEqual(lhs[i], rhs[i])) return false;
+ }
+ return true;
+}
+
+bool CanProveShapeEqual(const Expr& lhs, const Expr& rhs, arith::Analyzer* ana) {
+ if (lhs.same_as(rhs)) return true;
+ auto* lhs_shape = lhs.as<ShapeExprNode>();
+ auto* rhs_shape = rhs.as<ShapeExprNode>();
+
+ if (lhs_shape && rhs_shape) {
+ return CanProveShapeEqual(lhs_shape->values, rhs_shape->values, ana);
+ } else {
+ return false;
+ }
+}
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/analysis/struct_info_analysis.cc b/src/relax/analysis/struct_info_analysis.cc
new file mode 100644
index 0000000000..d9b1397534
--- /dev/null
+++ b/src/relax/analysis/struct_info_analysis.cc
@@ -0,0 +1,716 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file struct_info_analysis.cc
+ * \brief Implementations of foundation struct info analysis
+ *
+ * \note Update this file when you added a new StructInfo.
+ */
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/struct_info_functor.h>
+#include <tvm/tir/expr_functor.h>
+
+namespace tvm {
+namespace relax {
+
+//--------------------------
+// GetStaticType
+//--------------------------
+class StaticTypeDeriver : public StructInfoFunctor<Type(const StructInfo&)> {
+ public:
+ Type VisitStructInfo_(const ObjectStructInfoNode* op) final { return ObjectType(op->span); }
+
+ Type VisitStructInfo_(const PrimStructInfoNode* op) final {
+ return PrimType(op->dtype, op->span);
+ }
+
+ Type VisitStructInfo_(const ShapeStructInfoNode* op) final {
+ return ShapeType(op->ndim, op->span);
+ }
+
+ Type VisitStructInfo_(const TensorStructInfoNode* op) final {
+ return DynTensorType(op->ndim, op->dtype);
+ }
+
+ Type VisitStructInfo_(const TupleStructInfoNode* op) final {
+ Array<Type> fields =
+ op->fields.Map([this](const StructInfo& sinfo) { return this->VisitStructInfo(sinfo); });
+ return TupleType(fields, op->span);
+ }
+
+ Type VisitStructInfo_(const FuncStructInfoNode* op) final {
+ if (op->IsOpaque()) return PackedFuncType(op->span);
+ Array<Type> params = op->params.value().Map(
+ [this](const StructInfo& sinfo) { return this->VisitStructInfo(sinfo); });
+ Type ret = this->VisitStructInfo(op->ret);
+ return FuncType(params, ret, {}, {}, op->span);
+ }
+};
+
+Type GetStaticType(const StructInfo& info) { return StaticTypeDeriver()(info); }
+
+TVM_REGISTER_GLOBAL("relax.analysis.GetStaticType").set_body_typed([](const StructInfo& info) {
+ return GetStaticType(info);
+});
+
+//--------------------------
+// StructInfoFromType
+//--------------------------
+
+StructInfo StructInfoFromType(const Type& type) {
+ if (type.as<ObjectTypeNode>()) {
+ return ObjectStructInfo(type->span);
+ } else if (const PrimTypeNode* prim_type = type.as<PrimTypeNode>()) {
+ return PrimStructInfo(prim_type->dtype, prim_type->span);
+ } else if (const ShapeTypeNode* shape_type = type.as<ShapeTypeNode>()) {
+ return ShapeStructInfo(shape_type->ndim, type->span);
+ } else if (const DynTensorTypeNode* tensor_type = type.as<DynTensorTypeNode>()) {
+ return TensorStructInfo(tensor_type->dtype, tensor_type->ndim);
+ } else if (const TupleTypeNode* tuple_type = type.as<TupleTypeNode>()) {
+ Array<StructInfo> fields;
+ for (const Type& field : tuple_type->fields) {
+ fields.push_back(StructInfoFromType(field));
+ }
+ return TupleStructInfo(fields, type->span);
+ } else if (const FuncTypeNode* func_type = type.as<FuncTypeNode>()) {
+ Array<StructInfo> params =
+ func_type->arg_types.Map([](const Type& param) { return StructInfoFromType(param); });
+ StructInfo ret = StructInfoFromType(func_type->ret_type);
+ return FuncStructInfo(params, ret, func_type->span);
+ } else {
+ LOG(FATAL) << "Unsupported type: " << type;
+ return StructInfo();
+ }
+}
+
+//--------------------------
+// EraseToWellDefined
+//--------------------------
+class WellDefinedEraser : public StructInfoMutator,
+ public ExprMutatorBase,
+ public tir::ExprMutator {
+ public:
+ WellDefinedEraser(std::function<Optional<PrimExpr>(const tir::Var& var)> f_shape_var_map,
+ std::function<Optional<Expr>(const Var& var)> f_var_map, arith::Analyzer* ana)
+ : f_shape_var_map_(f_shape_var_map), f_var_map_(f_var_map), ana_(ana) {}
+
+ StructInfo VisitStructInfo_(const ShapeStructInfoNode* op) final {
+ bool has_undefined = false;
+ Optional<Array<PrimExpr>> values;
+
+ if (op->values.defined()) {
+ std::swap(has_undefined_, has_undefined);
+ values = op->values.value().Map([&](PrimExpr val) { return this->VisitPrimExpr(val); });
+ std::swap(has_undefined_, has_undefined);
+ }
+ // erase symbolic shape if we have undefined.
+ if (!has_undefined) {
+ if (values.same_as(op->values)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ return ShapeStructInfo(values.value(), op->span);
+ }
+ } else {
+ return ShapeStructInfo(op->ndim, op->span);
+ }
+ }
+
+ StructInfo VisitStructInfo_(const TensorStructInfoNode* op) final {
+ bool has_undefined = false;
+ Optional<Expr> shape;
+
+ if (op->shape.defined()) {
+ std::swap(has_undefined_, has_undefined);
+ shape = relax::ExprMutatorBase::VisitExpr(op->shape.value());
+ std::swap(has_undefined_, has_undefined);
+ }
+
+ // erase symbolic shape if we have undefined.
+ if (!has_undefined) {
+ if (shape.same_as(op->shape)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ if (shape.defined()) {
+ return TensorStructInfo(shape.value(), op->dtype, op->span);
+ } else {
+ return TensorStructInfo(op->dtype, op->ndim, op->span);
+ }
+ }
+ } else {
+ return TensorStructInfo(op->dtype, op->ndim, op->span);
+ }
+ }
+
+ StructInfo VisitStructInfo_(const FuncStructInfoNode* op) final {
+ // NOTE: we always require func struct info to be well-defined.
+ //
+ // All the occuring symbolic variables are defined in parameters'
+ // struct info annotations. So there is no needed to erase.
+ return GetRef<StructInfo>(op);
+ }
+
+ using relax::ExprMutatorBase::VisitExpr_;
+ using tir::ExprMutator::VisitExpr_;
+
+ // connect things up
+ PrimExpr VisitPrimExpr(const PrimExpr& expr) {
+ // apply eager simplification
+ PrimExpr val = tir::ExprMutator::VisitExpr(expr);
+ if (!val.same_as(expr)) {
+ return ana_->Simplify(val);
+ } else {
+ return val;
+ }
+ }
+
+ Expr VisitExpr_(const DataflowVarNode* var) final {
+ return VisitExpr_(static_cast<const VarNode*>(var));
+ }
+
+ Expr VisitExpr_(const VarNode* var) final {
+ Optional<Expr> ret;
+ if (f_var_map_ != nullptr) {
+ ret = f_var_map_(GetRef<Var>(var));
+ }
+ has_undefined_ = has_undefined_ || !ret.defined();
+ if (ret.defined()) {
+ ICHECK(ret.as<VarNode>() || ret.as<ShapeExprNode>())
+ << "Only allow Expr in StructInfo to be ShapeExpr or Var";
+ }
+ return ret.value_or(GetRef<Expr>(var));
+ }
+
+ PrimExpr VisitExpr_(const tir::VarNode* var) final {
+ Optional<PrimExpr> ret;
+ if (f_shape_var_map_ != nullptr) {
+ ret = f_shape_var_map_(GetRef<tir::Var>(var));
+ }
+ has_undefined_ = has_undefined_ || !ret.defined();
+
+ if (ret.defined()) {
+ PrimExpr value = ret.value();
+ if (value->IsInstance<IntImmNode>()) {
+ return tvm::cast(DataType::Int(64), value);
+ }
+ ICHECK(value.dtype() == DataType::Int(64)) << "Can only provide i64 expressions in shape";
+ return value;
+ } else {
+ return GetRef<PrimExpr>(var);
+ }
+ }
+
+ private:
+ bool has_undefined_ = false;
+ std::function<Optional<PrimExpr>(const tir::Var& var)> f_shape_var_map_;
+ std::function<Optional<Expr>(const Var& var)> f_var_map_;
+ arith::Analyzer* ana_;
+};
+
+StructInfo EraseToWellDefined(
+ const StructInfo& info, std::function<Optional<PrimExpr>(const tir::Var& var)> f_shape_var_map,
+ std::function<Optional<Expr>(const Var& var)> f_var_map, arith::Analyzer* ana) {
+ if (ana == nullptr) {
+ arith::Analyzer inst;
+ return WellDefinedEraser(f_shape_var_map, f_var_map, &inst).VisitStructInfo(info);
+ } else {
+ return WellDefinedEraser(f_shape_var_map, f_var_map, ana).VisitStructInfo(info);
+ }
+}
+
+StructInfo EraseToWellDefined(const StructInfo& info, Map<tir::Var, PrimExpr> shape_var_map,
+ Map<Var, Expr> var_map, arith::Analyzer* ana) {
+ std::function<Optional<PrimExpr>(const tir::Var& var)> f_shape_var_map = nullptr;
+ std::function<Optional<Expr>(const Var& var)> f_var_map = nullptr;
+
+ if (!shape_var_map.empty()) {
+ f_shape_var_map = [&](const tir::Var& var) -> Optional<PrimExpr> {
+ auto it = shape_var_map.find(var);
+ if (it != shape_var_map.end()) return (*it).second;
+ return NullOpt;
+ };
+ }
+
+ if (!var_map.empty()) {
+ f_var_map = [&](const Var& var) -> Optional<Expr> {
+ auto it = var_map.find(var);
+ if (it != var_map.end()) return (*it).second;
+ return NullOpt;
+ };
+ }
+
+ return EraseToWellDefined(info, f_shape_var_map, f_var_map, ana);
+}
+
+TVM_REGISTER_GLOBAL("relax.analysis.EraseToWellDefined")
+ .set_body_typed([](const StructInfo& info, Map<tir::Var, PrimExpr> shape_var_map,
+ Map<Var, Expr> var_map) {
+ return EraseToWellDefined(info, shape_var_map, var_map);
+ });
+
+//--------------------------
+// IsBaseOf
+//--------------------------
+class StructInfoBaseChecker
+ : public StructInfoFunctor<BaseCheckResult(const StructInfo&, const StructInfo&)> {
+ public:
+ explicit StructInfoBaseChecker(arith::Analyzer* ana) : analyzer_(ana) {}
+
+ BaseCheckResult VisitStructInfo(const StructInfo& lhs, const StructInfo& other) override {
+ // quick path
+ // Note: subclass may disable this quick path if we need to go over all struct info.
+ if (lhs.same_as(other)) return BaseCheckResult::kPass;
+ return StructInfoFunctor::VisitStructInfo(lhs, other);
+ }
+
+ // Object is base of everything
+ BaseCheckResult VisitStructInfo_(const ObjectStructInfoNode* lhs, const StructInfo& other) final {
+ return BaseCheckResult::kPass;
+ }
+
+ BaseCheckResult VisitStructInfo_(const PrimStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<PrimStructInfoNode>();
+ if (rhs == nullptr) {
+ if (other.as<ObjectStructInfoNode>()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+ return lhs->dtype == rhs->dtype ? BaseCheckResult::kPass : BaseCheckResult::kFailL0;
+ }
+
+ BaseCheckResult VisitStructInfo_(const ShapeStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<ShapeStructInfoNode>();
+ if (rhs == nullptr) {
+ if (other.as<ObjectStructInfoNode>()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+ // lhs have unknown ndim
+ if (lhs->IsUnknownNdim()) return BaseCheckResult::kPass;
+
+ // ndim must match
+ if (lhs->ndim != rhs->ndim) {
+ if (rhs->IsUnknownNdim()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+
+ // lhs does not have symbolic value
+ if (!lhs->values.defined()) return BaseCheckResult::kPass;
+ // rhs does not have symbolic value but lhs do.
+ if (!rhs->values.defined()) return BaseCheckResult::kFailL2;
+
+ // shape match check
+ return ShapeMatchCheck(lhs->values.value(), rhs->values.value());
+ }
+
+ BaseCheckResult VisitStructInfo_(const TensorStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<TensorStructInfoNode>();
+ if (rhs == nullptr) {
+ if (other.as<ObjectStructInfoNode>()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+ // dtype mismatch
+ if (!lhs->IsUnknownDtype() && lhs->dtype != rhs->dtype) {
+ if (rhs->IsUnknownDtype()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+
+ // ndim msiamtch
+ if (!lhs->IsUnknownNdim() && lhs->ndim != rhs->ndim) {
+ if (rhs->IsUnknownNdim()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+ // lhs does not have defined shape and everything else matches
+ if (!lhs->shape.defined()) return BaseCheckResult::kPass;
+ // rhs does not have symbolic value but lhs don't
+ if (!rhs->shape.defined()) return BaseCheckResult::kFailL2;
+
+ // shape match check
+ return ShapeMatchCheck(lhs->shape.value(), rhs->shape.value());
+ }
+
+ BaseCheckResult VisitStructInfo_(const TupleStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<TupleStructInfoNode>();
+ if (rhs == nullptr) {
+ if (other.as<ObjectStructInfoNode>()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+ return ArrayCheck(lhs->fields, rhs->fields);
+ }
+
+ BaseCheckResult VisitStructInfo_(const FuncStructInfoNode* lhs,
+ const StructInfo& other) override {
+ auto* rhs = other.as<FuncStructInfoNode>();
+ if (rhs == nullptr) {
+ if (other.as<ObjectStructInfoNode>()) return BaseCheckResult::kFailL1;
+ return BaseCheckResult::kFailL0;
+ }
+
+ // lhs opaque handling
+ if (lhs->IsOpaque()) {
+ if (lhs->derive_func.defined()) {
+ // function proving is best effort.
+ return lhs->derive_func.same_as(rhs->derive_func) ? BaseCheckResult::kPass
+ : BaseCheckResult::kFailL2;
+ }
+ // no derivation function, only depends on ret
+ return this->VisitStructInfo(lhs->ret, rhs->ret);
+ }
+
+ // Function check is best effort.
+ // rhs is opaque but lhs is not
+ if (rhs->IsOpaque()) return BaseCheckResult::kFailL2;
+
+ // NOTE: lhs->params, rhs->params may contain different symbolic
+ // vars that needs to be re-mapped to each other.
+ // This can only be done through structural equality check and not ArrayCheck.
+ //
+ // So we check structural equality here and if two are structurally
+ // equal return true.
+ //
+ // otherwise we do best effort BaseArrayCheck.
+ //
+ // This still does not handle cases where some arguments are sub of another
+ // while other parameters needs to get remapped.
+ //
+ // Given we only do best effort checking in these cases, and such cases
+ // are likely not a primary concern atm, we take this approach here.
+ if (struct_equal_(GetRef<StructInfo>(lhs), other)) return BaseCheckResult::kPass;
+
+ auto param_check = FuncParamsCheck(lhs->params.value(), rhs->params.value());
+ auto ret_check = this->VisitStructInfo(lhs->ret, rhs->ret);
+ return CombineCheck(param_check, ret_check);
+ }
+
+ protected:
+ // analyzer
+ arith::Analyzer* analyzer_;
+ // struct equal checker
+ StructuralEqual struct_equal_;
+
+ // customizable functions.
+ /*!
+ * \brief Check symbolic shape value equivalence.
+ * \param lhs The left hand shape.
+ * \param rhs The right hand shape.
+ * \return CheckResult.
+ */
+ virtual BaseCheckResult PrimValueMatchCheck(const PrimExpr& lhs, const PrimExpr& rhs) {
+ // get static shape checking right.
+ auto* int_lhs = lhs.as<IntImmNode>();
+ auto* int_rhs = rhs.as<IntImmNode>();
+ if (int_lhs && int_rhs) {
+ if (int_lhs->value == int_rhs->value) {
+ return BaseCheckResult::kPass;
+ } else {
+ return BaseCheckResult::kFailL0;
+ }
+ }
+ return analyzer_->CanProveEqual(lhs, rhs) ? BaseCheckResult::kPass : BaseCheckResult::kFailL2;
+ }
+ /*!
+ * \brief CheckShape value.
+ * \param lhs The left hand shape.
+ * \param rhs The right hand shape.
+ * \return CheckResult.
+ */
+ virtual BaseCheckResult ShapeMatchCheck(const Array<PrimExpr>& lhs, const Array<PrimExpr>& rhs) {
+ if (lhs.size() != rhs.size()) return BaseCheckResult::kFailL0;
+
+ BaseCheckResult ret = BaseCheckResult::kPass;
+ for (size_t i = 0; i < lhs.size(); ++i) {
+ auto cmp_ret = PrimValueMatchCheck(lhs[i], rhs[i]);
+ if (ret == BaseCheckResult::kFailL0) return ret;
+ ret = CombineCheck(cmp_ret, ret);
+ }
+ return ret;
+ }
+
+ /*!
+ * \brief CheckShape value.
+ * \param lhs The left hand shape.
+ * \param rhs The right hand shape.
+ * \return Check result.
+ */
+ virtual BaseCheckResult ShapeMatchCheck(const Expr& lhs, const Expr& rhs) {
+ if (lhs.same_as(rhs)) return BaseCheckResult::kPass;
+ auto* lhs_shape = lhs.as<ShapeExprNode>();
+ auto* rhs_shape = rhs.as<ShapeExprNode>();
+ if (lhs_shape && rhs_shape) {
+ return ShapeMatchCheck(lhs_shape->values, rhs_shape->values);
+ } else {
+ return BaseCheckResult::kFailL2;
+ }
+ }
+
+ /*!
+ * \brief CheckShape function parameters.
+ * \param lhs The left hand params.
+ * \param rhs The right hand params.
+ * \return Check result.
+ */
+ virtual BaseCheckResult FuncParamsCheck(const Array<StructInfo>& lhs,
+ const Array<StructInfo>& rhs) {
+ auto res = ArrayCheck(lhs, rhs);
+ // treat L1 failures in params checking as L2.
+ if (res == BaseCheckResult::kFailL1) res = BaseCheckResult::kFailL2;
+ return res;
+ }
+ // helper functions
+ /*!
+ * \brief Combine check results.
+ * \param lhs The left operand.
+ * \param rhs The righr operand.
+ * \return The check result.
+ */
+ static BaseCheckResult CombineCheck(BaseCheckResult lhs, BaseCheckResult rhs) {
+ if (lhs == BaseCheckResult::kFailL0 || rhs == BaseCheckResult::kFailL0) {
+ return BaseCheckResult::kFailL0;
+ }
+ if (lhs == BaseCheckResult::kFailL1 || rhs == BaseCheckResult::kFailL1) {
+ return BaseCheckResult::kFailL1;
+ }
+ if (lhs == BaseCheckResult::kFailL2 || rhs == BaseCheckResult::kFailL2) {
+ return BaseCheckResult::kFailL2;
+ }
+ return BaseCheckResult::kPass;
+ }
+
+ /*!
+ * \brief Generic helper function to check arrays.
+ * \param lhs The left operand.
+ * \param rhs The right operand.
+ */
+ BaseCheckResult ArrayCheck(const Array<StructInfo>& lhs, const Array<StructInfo>& rhs) {
+ if (lhs.size() != rhs.size()) return BaseCheckResult::kFailL0;
+ BaseCheckResult ret = BaseCheckResult::kPass;
+
+ for (size_t i = 0; i < lhs.size(); ++i) {
+ auto cmp_ret = this->VisitStructInfo(lhs[i], rhs[i]);
+ if (ret == BaseCheckResult::kFailL0) return ret;
+ ret = CombineCheck(cmp_ret, ret);
+ }
+ return ret;
+ }
+};
+
+BaseCheckResult StructInfoBaseCheck(const StructInfo& base, const StructInfo& derived,
+ arith::Analyzer* ana) {
+ if (ana == nullptr) {
+ arith::Analyzer inst;
+ return StructInfoBaseChecker(&inst)(base, derived);
+ } else {
+ return StructInfoBaseChecker(ana)(base, derived);
+ }
+}
+
+TVM_REGISTER_GLOBAL("relax.analysis.StructInfoBaseCheck")
+ .set_body_typed([](const StructInfo& base, const StructInfo& derived) -> int {
+ return static_cast<int>(StructInfoBaseCheck(base, derived));
+ });
+
+bool IsBaseOf(const StructInfo& base, const StructInfo& derived, arith::Analyzer* ana) {
+ return StructInfoBaseCheck(base, derived, ana) == BaseCheckResult::kPass;
+}
+
+TVM_REGISTER_GLOBAL("relax.StructInfoIsBaseOf")
+ .set_body_typed([](const StructInfo& base, const StructInfo& derived) {
+ return IsBaseOf(base, derived);
+ });
+
+//--------------------------
+// UnifyToLCA
+//--------------------------
+class StructInfoLCAFinder
+ : public StructInfoFunctor<StructInfo(const StructInfo&, const StructInfo&)> {
+ public:
+ explicit StructInfoLCAFinder(arith::Analyzer* ana) : analyzer_(ana) {}
+
+ StructInfo VisitStructInfo(const StructInfo& lhs, const StructInfo& other) final {
+ // quick path
+ if (lhs.same_as(other)) return lhs;
+ return StructInfoFunctor::VisitStructInfo(lhs, other);
+ }
+
+ // Object is based of everything, unify to object.
+ StructInfo VisitStructInfo_(const ObjectStructInfoNode* lhs, const StructInfo& other) final {
+ return GetRef<StructInfo>(lhs);
+ }
+
+ StructInfo VisitStructInfo_(const PrimStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<PrimStructInfoNode>();
+ if (rhs == nullptr) return ObjectStructInfo(lhs->span);
+ if (lhs->dtype == rhs->dtype) return GetRef<StructInfo>(lhs);
+ // PrimType will be treated as their boxed(object) values
+ // as a result we can unify to object.
+ return ObjectStructInfo(lhs->span);
+ }
+
+ StructInfo VisitStructInfo_(const ShapeStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<ShapeStructInfoNode>();
+ if (rhs == nullptr) return ObjectStructInfo(lhs->span);
+
+ int ndim = lhs->ndim == rhs->ndim ? lhs->ndim : kUnknownNDim;
+ if (lhs->ndim != rhs->ndim || !lhs->values.defined() || !rhs->values.defined() ||
+ !CanProveShapeEqual(lhs->values.value(), rhs->values.value(), analyzer_)) {
+ // prefers return same when possible
+ if (!lhs->values.defined() && lhs->ndim == ndim) {
+ return GetRef<StructInfo>(lhs);
+ } else {
+ return ShapeStructInfo(ndim, lhs->span);
+ }
+ }
+ // equals to each other
+ return GetRef<StructInfo>(lhs);
+ }
+
+ StructInfo VisitStructInfo_(const TensorStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<TensorStructInfoNode>();
+ if (rhs == nullptr) return ObjectStructInfo(lhs->span);
+
+ // find the target dtype and ndim.
+ DataType dtype = lhs->dtype == rhs->dtype ? lhs->dtype : DataType::Void();
+ int ndim = lhs->ndim == rhs->ndim ? lhs->ndim : kUnknownNDim;
+ // if ndim mismatch or one side of shape is missing
+ // then we cannot keep in symbolic shape
+ if (lhs->ndim != rhs->ndim || !lhs->shape.defined() || !rhs->shape.defined() ||
+ !CanProveShapeEqual(lhs->shape.value(), rhs->shape.value(), analyzer_)) {
+ // reuse lhs when possible
+ if (!lhs->shape.defined() && lhs->dtype == dtype && lhs->ndim == ndim) {
+ return GetRef<StructInfo>(lhs);
+ } else {
+ return TensorStructInfo(dtype, ndim, lhs->span);
+ }
+ }
+ // symbolic shape match but dtype mismatch
+ if (lhs->dtype != dtype) {
+ return TensorStructInfo(lhs->shape.value(), dtype, lhs->span);
+ } else {
+ return GetRef<StructInfo>(lhs);
+ }
+ }
+
+ StructInfo VisitStructInfo_(const TupleStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<TupleStructInfoNode>();
+ if (rhs == nullptr) return ObjectStructInfo(lhs->span);
+ Optional<Array<StructInfo>> fields = UnifyArray(lhs->fields, rhs->fields);
+ // tuple length not the same.
+ if (!fields.defined()) return ObjectStructInfo(lhs->span);
+
+ // same length tuple.
+ if (!fields.same_as(lhs->fields)) {
+ return TupleStructInfo(fields.value(), lhs->span);
+ } else {
+ return GetRef<StructInfo>(lhs);
+ }
+ }
+
+ StructInfo VisitStructInfo_(const FuncStructInfoNode* lhs, const StructInfo& other) final {
+ auto* rhs = other.as<FuncStructInfoNode>();
+ if (rhs == nullptr) return ObjectStructInfo(lhs->span);
+
+ // lhs opaque handling
+ if (lhs->IsOpaque()) {
+ if (lhs->derive_func.defined()) {
+ if (lhs->derive_func.same_as(rhs->derive_func)) {
+ return GetRef<StructInfo>(lhs);
+ } else {
+ // Create a new opaque with object return
+ return FuncStructInfo::OpaqueFunc(ObjectStructInfo(), lhs->span);
+ }
+ } else {
+ // no derivation function, only depends on ret
+ StructInfo ret = this->VisitStructInfo(lhs->ret, rhs->ret);
+ if (ret.same_as(lhs->ret)) return GetRef<StructInfo>(lhs);
+ return FuncStructInfo::OpaqueFunc(ret, lhs->span);
+ }
+ }
+ // rhs is opaque, lhs is not
+ if (rhs->IsOpaque()) {
+ // unify ret value, note that rhs's ret is context free(because it is opaque)
+ // so result of the unify is also context-free.
+ StructInfo ret = this->VisitStructInfo(lhs->ret, rhs->ret);
+ return FuncStructInfo::OpaqueFunc(ret, lhs->span);
+ }
+
+ // Both lhs and rhs are not opaque
+ // NOTE: lhs->params, rhs->params may contain different symbolic
+ // vars that needs to be re-mapped to each other.
+ // This can only be done through structural equality check.
+ //
+ // So we check structural equality here and if two are structurally
+ // equal return true.
+ //
+ // otherwise we do best effort of unify types without considering var remap.
+ //
+ // This still does not handle cases where some arguments are sub of another
+ // while other parameters needs to get remapped.
+ //
+ // Given we only do best effort checking in these cases, and such cases
+ // are likely not a primary concern atm, we take this approach here.
+ if (struct_equal_(GetRef<StructInfo>(lhs), GetRef<StructInfo>(rhs))) {
+ return GetRef<StructInfo>(lhs);
+ }
+
+ auto params = UnifyArray(lhs->params.value(), rhs->params.value());
+ auto ret = this->VisitStructInfo(lhs->ret, rhs->ret);
+
+ if (params.same_as(lhs->params) && ret.same_as(lhs->ret)) {
+ return GetRef<StructInfo>(lhs);
+ } else {
+ // fail to unify the params
+ if (!params.defined()) {
+ return FuncStructInfo::OpaqueFunc(ret, lhs->span);
+ } else {
+ return FuncStructInfo(params.value(), ret, lhs->span);
+ }
+ }
+ }
+
+ private:
+ // analyzer
+ arith::Analyzer* analyzer_;
+ // struct equal checker
+ StructuralEqual struct_equal_;
+
+ // check arrays
+ Optional<Array<StructInfo>> UnifyArray(const Array<StructInfo>& lhs,
+ const Array<StructInfo>& rhs) {
+ if (lhs.same_as(rhs)) return lhs;
+ if (lhs.size() != rhs.size()) return NullOpt;
+ size_t index = 0;
+ return lhs.Map([&](const StructInfo& a) { return this->VisitStructInfo(a, rhs[index++]); });
+ }
+};
+
+StructInfo StructInfoLCA(const StructInfo& lhs, const StructInfo& rhs, arith::Analyzer* ana) {
+ if (ana == nullptr) {
+ arith::Analyzer inst;
+ return StructInfoLCAFinder(&inst)(lhs, rhs);
+ } else {
+ return StructInfoLCAFinder(ana)(lhs, rhs);
+ }
+}
+
+TVM_REGISTER_GLOBAL("relax.analysis.StructInfoLCA")
+ .set_body_typed([](const StructInfo& lhs, const StructInfo& rhs) {
+ return StructInfoLCA(lhs, rhs);
+ });
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/expr.cc b/src/relax/ir/expr.cc
new file mode 100644
index 0000000000..45868a488a
--- /dev/null
+++ b/src/relax/ir/expr.cc
@@ -0,0 +1,601 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/relax/type.h>
+
+namespace tvm {
+namespace relax {
+
+using tvm::ReprPrinter;
+using tvm::runtime::Optional;
+
+TVM_REGISTER_NODE_TYPE(IdNode);
+
+Id::Id(String name_hint) {
+ ObjectPtr<IdNode> n = make_object<IdNode>();
+ n->name_hint = std::move(name_hint);
+ data_ = std::move(n);
+}
+
+Call::Call(Expr op, Array<Expr> args, Attrs attrs, Array<StructInfo> sinfo_args, Span span) {
+ ObjectPtr<CallNode> n = make_object<CallNode>();
+ n->op = std::move(op);
+ n->args = std::move(args);
+ n->attrs = std::move(attrs);
+ n->sinfo_args = std::move(sinfo_args);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+Call WithFields(Call call, Optional<Expr> opt_op, Optional<Array<Expr>> opt_args,
+ Optional<Attrs> opt_attrs, Optional<Array<StructInfo>> opt_sinfo_args,
+ Optional<Span> opt_span) {
+ // Collect new values for fields.
+ Expr op = opt_op.value_or(call->op);
+ Array<Expr> args = opt_args.value_or(call->args);
+ Attrs attrs = opt_attrs.value_or(call->attrs);
+ Array<StructInfo> sinfo_args = opt_sinfo_args.value_or(call->sinfo_args);
+ Span span = opt_span.value_or(call->span);
+
+ // Check if anything changed.
+ bool unchanged = op.same_as(call->op) && attrs.same_as(call->attrs) && span.same_as(call->span);
+ if (unchanged) {
+ if (args.size() == call->args.size()) {
+ for (size_t i = 0; i < args.size(); i++) {
+ unchanged &= args[i].same_as(call->args[i]);
+ }
+ } else {
+ unchanged = false;
+ }
+ }
+ if (unchanged) {
+ if (sinfo_args.size() == call->sinfo_args.size()) {
+ for (size_t i = 0; i < sinfo_args.size(); i++) {
+ unchanged &= sinfo_args[i].same_as(call->sinfo_args[i]);
+ }
+ } else {
+ unchanged = false;
+ }
+ }
+
+ if (!unchanged) {
+ // If call is only references, update it in place. Otherwise copy and update.
+ CallNode* cow_call_node = call.CopyOnWrite();
+ cow_call_node->op = op;
+ cow_call_node->args = args;
+ cow_call_node->attrs = attrs;
+ cow_call_node->sinfo_args = sinfo_args;
+ cow_call_node->span = span;
+ }
+ return call;
+}
+
+TVM_REGISTER_NODE_TYPE(CallNode);
+
+TVM_REGISTER_GLOBAL("relax.Call")
+ .set_body_typed([](Expr op, Array<Expr> args, Attrs attrs, Array<StructInfo> sinfo_args,
+ Span span) { return Call(op, args, attrs, sinfo_args, span); });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<CallNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ auto* node = static_cast<const CallNode*>(ref.get());
+ p->stream << "CallNode(" << node->op << ", " << node->args << ", " << node->attrs << ", "
+ << node->sinfo_args << ")";
+ });
+
+If::If(Expr cond, Expr true_branch, Expr false_branch, Span span) {
+ ObjectPtr<IfNode> n = make_object<IfNode>();
+ n->cond = std::move(cond);
+ n->true_branch = std::move(true_branch);
+ n->false_branch = std::move(false_branch);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+If WithFields(If if_expr, Optional<Expr> opt_cond, Optional<Expr> opt_true_branch,
+ Optional<Expr> opt_false_branch, Optional<Span> opt_span) {
+ Expr cond = opt_cond.value_or(if_expr->cond);
+ Expr true_branch = opt_true_branch.value_or(if_expr->true_branch);
+ Expr false_branch = opt_false_branch.value_or(if_expr->false_branch);
+ Span span = opt_span.value_or(if_expr->span);
+
+ bool unchanged = cond.same_as(if_expr->cond) && true_branch.same_as(if_expr->true_branch) &&
+ false_branch.same_as(if_expr->false_branch) && span.same_as(if_expr->span);
+
+ if (!unchanged) {
+ IfNode* cow_if_node = if_expr.CopyOnWrite();
+ cow_if_node->cond = cond;
+ cow_if_node->true_branch = true_branch;
+ cow_if_node->false_branch = false_branch;
+ cow_if_node->span = span;
+ }
+ return if_expr;
+}
+
+TVM_REGISTER_NODE_TYPE(IfNode);
+
+TVM_REGISTER_GLOBAL("relax.If")
+ .set_body_typed([](Expr cond, Expr true_branch, Expr false_branch, Span span) {
+ return If(cond, true_branch, false_branch, span);
+ });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<IfNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ auto* node = static_cast<const IfNode*>(ref.get());
+ p->stream << "IfNode(" << node->cond << ", " << node->true_branch << ", "
+ << node->false_branch << ")";
+ });
+
+Tuple::Tuple(tvm::Array<relay::Expr> fields, Span span) {
+ ObjectPtr<TupleNode> n = make_object<TupleNode>();
+ n->fields = std::move(fields);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(TupleNode);
+
+TVM_REGISTER_GLOBAL("relax.Tuple").set_body_typed([](tvm::Array<relay::Expr> fields, Span span) {
+ return Tuple(fields, span);
+});
+
+Tuple WithFields(Tuple tuple, Optional<Array<Expr>> opt_fields, Optional<Span> opt_span) {
+ Array<Expr> fields = opt_fields.value_or(tuple->fields);
+ Span span = opt_span.value_or(tuple->span);
+
+ bool all_fields_unchanged = true;
+ if (fields.size() == tuple->fields.size()) {
+ for (size_t i = 0; i < fields.size(); i++) {
+ all_fields_unchanged &= fields[i].same_as(tuple->fields[i]);
+ }
+ } else {
+ all_fields_unchanged = false;
+ }
+
+ all_fields_unchanged = all_fields_unchanged && span.same_as(tuple->span);
+ if (!all_fields_unchanged) {
+ TupleNode* cow_tuple_node = tuple.CopyOnWrite();
+ cow_tuple_node->fields = fields;
+ cow_tuple_node->span = span;
+ }
+ return tuple;
+}
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<TupleNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ auto* node = static_cast<const TupleNode*>(ref.get());
+ p->stream << "Tuple(" << node->fields << ")";
+ });
+
+TupleGetItem::TupleGetItem(Expr tuple, int index, Span span) {
+ ObjectPtr<TupleGetItemNode> n = make_object<TupleGetItemNode>();
+ n->tuple = std::move(tuple);
+ n->index = index;
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+TupleGetItem WithFields(TupleGetItem tuple_get_item, Optional<Expr> opt_tuple,
+ Optional<Integer> opt_index, Optional<Span> opt_span) {
+ Expr tuple = opt_tuple.value_or(tuple_get_item->tuple);
+ Integer index = opt_index.value_or(tuple_get_item->index);
+ Span span = opt_span.value_or(tuple_get_item->span);
+
+ bool unchanged = tuple.same_as(tuple_get_item->tuple) && (index == tuple_get_item->index) &&
+ span.same_as(tuple_get_item->span);
+ if (!unchanged) {
+ TupleGetItemNode* cow_tuple_get_item_node = tuple_get_item.CopyOnWrite();
+ cow_tuple_get_item_node->tuple = tuple;
+ cow_tuple_get_item_node->index = index.IntValue();
+ cow_tuple_get_item_node->span = span;
+ }
+ return tuple_get_item;
+}
+
+TVM_REGISTER_NODE_TYPE(TupleGetItemNode);
+
+TVM_REGISTER_GLOBAL("relax.TupleGetItem").set_body_typed([](Expr tuple, int index) {
+ return TupleGetItem(tuple, index);
+});
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<TupleGetItemNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ auto* node = static_cast<const TupleGetItemNode*>(ref.get());
+ p->stream << "TupleGetItemNode(" << node->tuple << ", " << node->index << ")";
+ });
+
+TVM_REGISTER_NODE_TYPE(ShapeExprNode);
+
+ShapeExpr::ShapeExpr(Array<PrimExpr> values, Span span) {
+ ObjectPtr<ShapeExprNode> n = make_object<ShapeExprNode>();
+
+ n->values = values.Map([](PrimExpr value) {
+ if (value->IsInstance<IntImmNode>()) {
+ return tvm::cast(DataType::Int(64), value);
+ }
+ ICHECK(value.dtype() == DataType::Int(64))
+ << "the value in ShapeStructInfo can only have dtype of int64";
+ return value;
+ });
+ n->span = span;
+ n->checked_type_ = ShapeType(values.size());
+ n->struct_info_ = ShapeStructInfo(values, span);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.ShapeExpr").set_body_typed([](Array<PrimExpr> values, Span span) {
+ return ShapeExpr(values, span);
+});
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<ShapeExprNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const ShapeExprNode* node = static_cast<const ShapeExprNode*>(ref.get());
+ p->stream << "ShapeExpr(";
+ for (auto it = node->values.begin(); it != node->values.end(); it++) {
+ if (it != node->values.begin()) {
+ p->stream << ", ";
+ }
+ p->stream << *it;
+ }
+ p->stream << ")";
+ });
+
+TVM_REGISTER_NODE_TYPE(VarNode);
+
+Var::Var(Id vid, Optional<StructInfo> struct_info_annotation, Span span) {
+ ObjectPtr<VarNode> n = make_object<VarNode>();
+ n->vid = std::move(vid);
+ if (struct_info_annotation) {
+ n->checked_type_ = GetStaticType(struct_info_annotation.value());
+ }
+ n->struct_info_ = std::move(struct_info_annotation);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.Var")
+ .set_body_typed([](String name_hint, Optional<StructInfo> struct_info_annotation, Span span) {
+ return Var(name_hint, struct_info_annotation, span);
+ });
+
+TVM_REGISTER_GLOBAL("relax.VarFromId")
+ .set_body_typed([](Id vid, Optional<StructInfo> struct_info_annotation, Span span) {
+ return Var(vid, struct_info_annotation, span);
+ });
+
+TVM_REGISTER_NODE_TYPE(DataflowVarNode);
+
+DataflowVar::DataflowVar(Id vid, Optional<StructInfo> struct_info_annotation, Span span) {
+ ObjectPtr<DataflowVarNode> n = make_object<DataflowVarNode>();
+ n->vid = std::move(vid);
+ if (struct_info_annotation) {
+ n->checked_type_ = GetStaticType(struct_info_annotation.value());
+ }
+ n->struct_info_ = std::move(struct_info_annotation);
+ n->span = std::move(span);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.DataflowVar")
+ .set_body_typed([](String name_hint, Optional<StructInfo> struct_info_annotation, Span span) {
+ return DataflowVar(name_hint, struct_info_annotation, span);
+ });
+
+TVM_REGISTER_GLOBAL("relax.DataflowVarFromId")
+ .set_body_typed([](Id vid, Optional<StructInfo> struct_info_annotation, Span span) {
+ return DataflowVar(vid, struct_info_annotation, span);
+ });
+
+Constant::Constant(runtime::NDArray data, Span span) {
+ ObjectPtr<ConstantNode> n = make_object<ConstantNode>();
+ n->data = std::move(data);
+ n->span = std::move(span);
+
+ // set struct info.
+ Array<PrimExpr> values;
+ auto shape_tuple = n->data.Shape();
+ for (size_t dim = 0; dim < shape_tuple.size(); ++dim) {
+ values.push_back(IntImm(DataType::Int(64), shape_tuple[dim]));
+ }
+ TensorStructInfo tinfo(ShapeExpr(values), n->data.DataType(), span);
+
+ n->struct_info_ = tinfo;
+ n->checked_type_ = DynTensorType(tinfo->ndim, tinfo->dtype);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(ConstantNode);
+
+TVM_REGISTER_GLOBAL("relax.Constant").set_body_typed([](runtime::NDArray data, Span span = Span()) {
+ return Constant(data, span);
+});
+
+PrimValue::PrimValue(PrimExpr value, Span span) {
+ ObjectPtr<PrimValueNode> n = make_object<PrimValueNode>();
+ n->checked_type_ = PrimType(value.dtype());
+ n->struct_info_ = PrimStructInfo(value.dtype());
+ n->value = std::move(value);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+PrimValue PrimValue::Int64(int64_t value, Span span) {
+ return PrimValue(IntImm(DataType::Int(64), value), span);
+}
+
+TVM_REGISTER_NODE_TYPE(PrimValueNode);
+
+TVM_REGISTER_GLOBAL("relax.PrimValue").set_body_typed([](PrimExpr value, Span span) {
+ return PrimValue(value, span);
+});
+
+StringImm::StringImm(String value, Span span) {
+ ObjectPtr<StringImmNode> n = make_object<StringImmNode>();
+ n->value = std::move(value);
+ n->span = std::move(span);
+ // use the base structinfo for now
+ // we can choose to introduce more fine-grained struct info later if necessary.
+ n->checked_type_ = ObjectType();
+ n->struct_info_ = ObjectStructInfo();
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(StringImmNode);
+
+TVM_REGISTER_GLOBAL("relax.StringImm").set_body_typed([](String value, Span span) {
+ return StringImm(value, span);
+});
+
+DataTypeImm::DataTypeImm(DataType value, Span span) {
+ ObjectPtr<DataTypeImmNode> n = make_object<DataTypeImmNode>();
+ n->value = std::move(value);
+ n->span = std::move(span);
+ // use the base structinfo for now
+ // we can choose to introduce more fine-grained struct info later if necessary.
+ n->checked_type_ = ObjectType();
+ n->struct_info_ = ObjectStructInfo();
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(DataTypeImmNode);
+
+TVM_REGISTER_GLOBAL("relax.DataTypeImm").set_body_typed([](DataType value, Span span) {
+ return DataTypeImm(value, span);
+});
+
+TVM_REGISTER_NODE_TYPE(MatchCastNode);
+
+MatchCast::MatchCast(Var var, Expr value, StructInfo struct_info, Span span) {
+ ObjectPtr<MatchCastNode> n = make_object<MatchCastNode>();
+ ICHECK(var.defined()) << "MatchCast requires var to be defined";
+ n->var = std::move(var);
+ n->value = std::move(value);
+ n->struct_info = std::move(struct_info);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.MatchCast")
+ .set_body_typed([](Var var, Expr value, StructInfo struct_info, Span span) {
+ return MatchCast(var, value, struct_info, span);
+ });
+
+TVM_REGISTER_NODE_TYPE(VarBindingNode);
+
+VarBinding::VarBinding(Var var, Expr value, Span span) {
+ ObjectPtr<VarBindingNode> n = make_object<VarBindingNode>();
+ n->var = std::move(var);
+ n->value = std::move(value);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.VarBinding").set_body_typed([](Var var, Expr value, Span span) {
+ return VarBinding(var, value, span);
+});
+
+TVM_REGISTER_NODE_TYPE(BindingBlockNode);
+
+BindingBlock::BindingBlock(Array<Binding> bindings, Span span) {
+ ObjectPtr<BindingBlockNode> n = make_object<BindingBlockNode>();
+ n->bindings = std::move(bindings);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.BindingBlock").set_body_typed([](Array<Binding> bindings, Span span) {
+ return BindingBlock(bindings, span);
+});
+
+TVM_REGISTER_NODE_TYPE(DataflowBlockNode);
+
+DataflowBlock::DataflowBlock(Array<Binding> bindings, Span span) {
+ ObjectPtr<DataflowBlockNode> n = make_object<DataflowBlockNode>();
+ n->bindings = std::move(bindings);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.DataflowBlock").set_body_typed([](Array<Binding> bindings, Span span) {
+ return DataflowBlock(bindings, span);
+});
+
+TVM_REGISTER_NODE_TYPE(SeqExprNode);
+
+SeqExpr::SeqExpr(Array<BindingBlock> blocks, Expr body, Span span) {
+ ObjectPtr<SeqExprNode> n = make_object<SeqExprNode>();
+ n->blocks = std::move(blocks);
+ n->body = std::move(body);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.SeqExpr")
+ .set_body_typed([](Array<BindingBlock> blocks, Expr body, Span span) {
+ return SeqExpr(blocks, body, span);
+ });
+
+TVM_REGISTER_NODE_TYPE(FunctionNode);
+
+Function::Function(Array<Var> params, Expr body, Optional<StructInfo> ret_struct_info,
+ DictAttrs attrs, Span span) {
+ // Set the function type.
+ // For function, we take a conservative approach and require the function type
+ // to be known at construction time.
+ Array<StructInfo> param_sinfo;
+
+ for (const Var& param : params) {
+ CHECK(param->struct_info_.defined())
+ << "relax.Function requires params to contain struct_info_";
+ param_sinfo.push_back(GetStructInfo(param));
+ }
+
+ Optional<StructInfo> body_sinfo;
+
+ if (body->struct_info_.defined()) {
+ body_sinfo = GetStructInfo(body);
+ }
+
+ if (ret_struct_info.defined()) {
+ // allow body to override ret if body is more fine-grained.
+ if (body_sinfo.defined()) {
+ if (IsBaseOf(ret_struct_info.value(), body_sinfo.value())) {
+ ret_struct_info = body_sinfo;
+ }
+ }
+ } else {
+ CHECK(body_sinfo.defined())
+ << "Function do not have a return signature and body is not normalized";
+ ret_struct_info = body_sinfo;
+ }
+
+ FuncStructInfo func_sinfo(param_sinfo, ret_struct_info.value());
+
+ // set the fields
+ ObjectPtr<FunctionNode> n = make_object<FunctionNode>();
+ n->params = std::move(params);
+ n->body = std::move(body);
+ n->ret_struct_info = std::move(ret_struct_info.value());
+ n->checked_type_ = GetStaticType(func_sinfo);
+ n->struct_info_ = std::move(func_sinfo);
+ n->attrs = std::move(attrs);
+ n->span = std::move(span);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.Function")
+ .set_body_typed([](Array<Var> params, Expr body, Optional<StructInfo> ret_struct_info,
+ DictAttrs attrs,
+ Span span) { return Function(params, body, ret_struct_info, attrs, span); });
+
+Function Function::CreateEmpty(Array<Var> params, StructInfo ret_struct_info, DictAttrs attrs,
+ Span span) {
+ Array<StructInfo> param_sinfo;
+ for (const Var& param : params) {
+ ICHECK(param->checked_type_.defined())
+ << "relax.Function requires params to contain checked_type_.";
+ param_sinfo.push_back(GetStructInfo(param));
+ }
+ FuncStructInfo finfo(param_sinfo, ret_struct_info);
+
+ // set the fields
+ ObjectPtr<FunctionNode> n = make_object<FunctionNode>();
+ n->params = std::move(params);
+ n->body = Expr();
+ n->checked_type_ = GetStaticType(finfo);
+ n->struct_info_ = std::move(finfo);
+ n->ret_struct_info = std::move(ret_struct_info);
+ n->attrs = std::move(attrs);
+ n->span = std::move(span);
+ return Function(std::move(n));
+}
+
+TVM_REGISTER_GLOBAL("relax.FunctionCreateEmpty")
+ .set_body_typed([](Array<Var> params, StructInfo ret_struct_info, DictAttrs attrs, Span span) {
+ return Function::CreateEmpty(params, ret_struct_info, attrs, span);
+ });
+
+// Special opaque derivation function for ExternFunc
+// Take look at sinfo_args to figure out the return StructInfo.
+TVM_REGISTER_GLOBAL("tvm.relax.struct_info.infer_by_sinfo_args")
+ .set_body_typed([](const Call& call, const BlockBuilder& ctx) -> StructInfo {
+ ICHECK(call->sinfo_args.defined()) << "sinfo_args field of CallNode should always be defined";
+ if (call->sinfo_args.empty()) {
+ return ObjectStructInfo();
+ } else if (call->sinfo_args.size() == 1) {
+ return call->sinfo_args[0];
+ } else {
+ return TupleStructInfo(call->sinfo_args);
+ }
+ });
+
+// Get the derive function.
+FuncStructInfo GetExternFuncStructInfo() {
+ EnvFunc fn = EnvFunc::Get("tvm.relax.struct_info.infer_by_sinfo_args");
+ StructInfoDeriveFunc derive;
+ derive = fn;
+ return FuncStructInfo::OpaqueFunc(derive);
+}
+
+TVM_REGISTER_NODE_TYPE(ExternFuncNode);
+
+ExternFunc::ExternFunc(String global_symbol, Span span) {
+ ObjectPtr<ExternFuncNode> n = make_object<ExternFuncNode>();
+ n->global_symbol = std::move(global_symbol);
+ n->span = span;
+ static auto sinfo = GetExternFuncStructInfo();
+ n->struct_info_ = sinfo;
+ n->checked_type_ = GetStaticType(sinfo);
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.ExternFunc").set_body_typed([](String global_symbol, Span span) {
+ return ExternFunc(global_symbol, span);
+});
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+ .set_dispatch<ExternFuncNode>([](const ObjectRef& ref, ReprPrinter* p) {
+ const auto* node = static_cast<const ExternFuncNode*>(ref.get());
+ p->stream << "ExternFunc(\"" << node->global_symbol << "\")";
+ });
+
+Expr GetShapeOf(const Expr& expr) {
+ // default case, to be normalized.
+ ICHECK(expr->struct_info_.defined()) << "GetShapeOf can only be applied to normalized expr";
+ auto* tinfo = GetStructInfoAs<TensorStructInfoNode>(expr);
+
+ ICHECK(tinfo != nullptr) << "ShapeOf can only be applied to expr with TensorStructInfo";
+ if (tinfo->shape.defined()) return tinfo->shape.value();
+
+ static const Op& op = Op::Get("relax.shape_of");
+ // default case, call shape of, eagerly normalize the expr.
+ relax::Call call_shape_of(op, {expr}, {}, {});
+ UpdateStructInfo(call_shape_of, ShapeStructInfo(tinfo->ndim));
+ return call_shape_of;
+}
+
+TVM_REGISTER_GLOBAL("relax.GetShapeOf").set_body_typed([](const Expr& expr) {
+ return GetShapeOf(expr);
+});
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/expr_functor.cc b/src/relax/ir/expr_functor.cc
new file mode 100644
index 0000000000..048de7950f
--- /dev/null
+++ b/src/relax/ir/expr_functor.cc
@@ -0,0 +1,546 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/expr_functor.cc
+ * \brief A wrapper around ExprFunctor which functionally updates the AST.
+ *
+ * ExprMutator uses memoization and self return in order to amortize
+ * the cost of using functional updates.
+ */
+#include <tvm/ir/type_functor.h>
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/type.h>
+
+// functions to be overriden.
+#define RELAX_VISIT_BINDING_DISPATCH(OP) \
+ vtable.template set_dispatch<OP>( \
+ [](const ObjectRef& n, TSelf* self, const VarBindingNode* binding) { \
+ self->VisitBinding_(binding, static_cast<const OP*>(n.get())); \
+ });
+
+#define RELAX_VAR_BINDING_DISPATCH_IMPL(Type) \
+ Type::VisitBindingVTable Type::InitVisitBindingVTable() { \
+ VisitBindingVTable vtable; \
+ RELAX_VISIT_BINDING_DISPATCH(ConstantNode); \
+ RELAX_VISIT_BINDING_DISPATCH(TupleNode); \
+ RELAX_VISIT_BINDING_DISPATCH(VarNode); \
+ RELAX_VISIT_BINDING_DISPATCH(DataflowVarNode); \
+ RELAX_VISIT_BINDING_DISPATCH(ShapeExprNode); \
+ RELAX_VISIT_BINDING_DISPATCH(ExternFuncNode); \
+ RELAX_VISIT_BINDING_DISPATCH(GlobalVarNode); \
+ RELAX_VISIT_BINDING_DISPATCH(FunctionNode); \
+ RELAX_VISIT_BINDING_DISPATCH(CallNode); \
+ RELAX_VISIT_BINDING_DISPATCH(SeqExprNode); \
+ RELAX_VISIT_BINDING_DISPATCH(IfNode); \
+ RELAX_VISIT_BINDING_DISPATCH(OpNode); \
+ RELAX_VISIT_BINDING_DISPATCH(TupleGetItemNode); \
+ RELAX_VISIT_BINDING_DISPATCH(PrimValueNode); \
+ RELAX_VISIT_BINDING_DISPATCH(StringImmNode); \
+ RELAX_VISIT_BINDING_DISPATCH(DataTypeImmNode); \
+ return vtable; \
+ } \
+ void Type::VisitBinding_(const VarBindingNode* binding) { \
+ static VisitBindingVTable vtable = InitVisitBindingVTable(); \
+ const Expr& value = binding->value; \
+ ICHECK(value.defined()) << "Found null pointer node while traversing AST."; \
+ ICHECK(vtable.can_dispatch(value)) \
+ << "VisitVarBinding do not allow binding value type" << value->GetTypeKey(); \
+ vtable(value, this, binding); \
+ }
+
+// functions to be overriden.
+#define RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(OP) \
+ void ExprVisitor::VisitBinding_(const VarBindingNode* binding, const OP* value) { \
+ this->VisitExpr(binding->value); \
+ this->VisitVarDef(binding->var); \
+ }
+
+// functions to be overriden.
+#define RELAX_EXPR_MUTATOR_VISIT_BINDING_IMPL(OP) \
+ void ExprMutator::VisitBinding_(const VarBindingNode* binding, const OP* value) { \
+ Expr new_value = this->VisitExpr(binding->value); \
+ this->ReEmitBinding(binding, new_value); \
+ }
+
+namespace tvm {
+namespace relax {
+
+// ==================
+// ExprVisitor
+
+void ExprVisitor::VisitExprDepStructInfoField(const StructInfo& struct_info) {
+ // recurse into struct info in case they depend on value
+ // under the current scope.
+ default_struct_info_field_visitor_.VisitStructInfo(struct_info);
+}
+
+ExprVisitor::DefaultStructInfoFieldVisitor::DefaultStructInfoFieldVisitor(ExprVisitor* parent)
+ : parent_(parent) {}
+
+void ExprVisitor::DefaultStructInfoFieldVisitor::VisitStructInfoExprField(const Expr& expr) {
+ parent_->VisitExpr(expr);
+}
+
+void ExprVisitor::DefaultStructInfoFieldVisitor::VisitStructInfoExprField(const PrimExpr& expr) {
+ parent_->VisitPrimExpr(expr);
+}
+
+void ExprVisitor::DefaultStructInfoFieldVisitor::VisitStructInfo_(const FuncStructInfoNode* op) {
+ // Do not recurse into function struct info
+ // as they won't contain ref to values in current scope.
+}
+
+void ExprVisitor::VisitExpr(const Expr& expr) { ExprFunctor::VisitExpr(expr); }
+
+void ExprVisitor::VisitExpr_(const ConstantNode* op) {
+ this->VisitSpan(op->span);
+ // Constant's StructInfo does not depend on Expr.
+}
+
+void ExprVisitor::VisitExpr_(const GlobalVarNode* op) {
+ this->VisitSpan(op->span);
+ // FuncStructInfo is not value-dep
+}
+
+void ExprVisitor::VisitExpr_(const TupleNode* op) {
+ this->VisitSpan(op->span);
+ for (Expr field : op->fields) {
+ this->VisitExpr(field);
+ }
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+// Visit the use-site of a defined Var
+void ExprVisitor::VisitExpr_(const VarNode* op) {
+ this->VisitSpan(op->span);
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+// Visit the use-site of a defined DataflowVar
+void ExprVisitor::VisitExpr_(const DataflowVarNode* op) {
+ this->VisitSpan(op->span);
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const FunctionNode* op) {
+ this->VisitSpan(op->span);
+ for (Var param : op->params) {
+ this->VisitVarDef(param);
+ }
+
+ this->VisitExpr(op->body);
+ // FuncStructInfo does not depend on Expr.
+}
+
+void ExprVisitor::VisitExpr_(const CallNode* op) {
+ this->VisitSpan(op->span);
+ this->VisitExpr(op->op);
+
+ for (StructInfo sinfo_arg : op->sinfo_args) {
+ this->VisitExprDepStructInfoField(sinfo_arg);
+ }
+
+ for (Expr arg : op->args) {
+ this->VisitExpr(arg);
+ }
+
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const IfNode* op) {
+ this->VisitSpan(op->span);
+ this->VisitExpr(op->cond);
+ this->VisitExpr(op->true_branch);
+ this->VisitExpr(op->false_branch);
+
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const OpNode* op) { this->VisitSpan(op->span); }
+
+void ExprVisitor::VisitExpr_(const TupleGetItemNode* op) {
+ this->VisitSpan(op->span);
+ this->VisitExpr(op->tuple);
+
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const ShapeExprNode* op) {
+ for (PrimExpr val : op->values) {
+ this->VisitPrimExpr(val);
+ }
+ this->VisitSpan(op->span);
+
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const ExternFuncNode* op) {
+ this->VisitSpan(op->span);
+ // FuncStructInfo does not depend on Expr.
+}
+
+void ExprVisitor::VisitExpr_(const SeqExprNode* op) {
+ this->VisitSpan(op->span);
+ for (BindingBlock block : op->blocks) {
+ this->VisitBindingBlock(block);
+ }
+ this->VisitExpr(op->body);
+
+ if (auto* sinfo = op->struct_info_.as<StructInfoNode>()) {
+ this->VisitExprDepStructInfoField(GetRef<StructInfo>(sinfo));
+ }
+}
+
+void ExprVisitor::VisitExpr_(const PrimValueNode* op) {
+ this->VisitPrimExpr(op->value);
+ this->VisitSpan(op->span);
+}
+
+void ExprVisitor::VisitExpr_(const StringImmNode* op) { this->VisitSpan(op->span); }
+
+void ExprVisitor::VisitExpr_(const DataTypeImmNode* op) { this->VisitSpan(op->span); }
+
+void ExprVisitor::VisitSpan(const Span& span) {}
+
+void ExprVisitor::VisitPrimExpr(const PrimExpr& expr) {}
+
+// implementations of binding visitor dispatch
+RELAX_VAR_BINDING_DISPATCH_IMPL(ExprVisitor);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(ConstantNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(TupleNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(VarNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(DataflowVarNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(ShapeExprNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(ExternFuncNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(GlobalVarNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(FunctionNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(CallNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(SeqExprNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(IfNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(OpNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(TupleGetItemNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(PrimValueNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(StringImmNode);
+RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(DataTypeImmNode);
+
+void ExprVisitor::VisitBinding_(const MatchCastNode* binding) {
+ this->VisitExpr(binding->value);
+ this->VisitVarDef(binding->var);
+}
+
+void ExprVisitor::VisitBindingBlock_(const BindingBlockNode* block) {
+ for (Binding binding : block->bindings) {
+ this->VisitBinding(binding);
+ }
+}
+
+void ExprVisitor::VisitBindingBlock_(const DataflowBlockNode* block) {
+ for (Binding binding : block->bindings) {
+ this->VisitBinding(binding);
+ }
+}
+
+void ExprVisitor::VisitVarDef_(const DataflowVarNode* var) { this->VisitSpan(var->span); }
+
+void ExprVisitor::VisitVarDef_(const VarNode* var) { this->VisitSpan(var->span); }
+
+void ExprVisitor::VisitBinding(const Binding& binding) {
+ if (const auto* node = binding.as<VarBindingNode>()) {
+ VisitBinding_(node);
+ } else if (const auto* node = binding.as<MatchCastNode>()) {
+ VisitBinding_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << binding->GetTypeKey();
+ }
+}
+
+void ExprVisitor::VisitBindingBlock(const BindingBlock& block) {
+ if (const auto* node = block.as<DataflowBlockNode>()) {
+ VisitBindingBlock_(node);
+ } else if (const auto* node = block.as<BindingBlockNode>()) {
+ VisitBindingBlock_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << block->GetTypeKey();
+ }
+}
+
+void ExprVisitor::VisitVarDef(const Var& var) {
+ if (const auto* node = var.as<DataflowVarNode>()) {
+ VisitVarDef_(node);
+ } else if (const auto* node = var.as<VarNode>()) {
+ VisitVarDef_(node);
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << var->GetTypeKey();
+ }
+}
+
+class ExprApplyVisit : public ExprVisitor {
+ public:
+ explicit ExprApplyVisit(std::function<void(const Expr&)> f) : f_(f) {}
+
+ void VisitExpr(const Expr& e) final {
+ ExprVisitor::VisitExpr(e);
+ f_(e);
+ }
+
+ private:
+ std::function<void(const Expr&)> f_;
+};
+
+void PostOrderVisit(const Expr& e, std::function<void(const Expr&)> fvisit) {
+ ExprApplyVisit(fvisit).VisitExpr(e);
+}
+
+TVM_REGISTER_GLOBAL("relax.analysis.post_order_visit").set_body_typed([](Expr expr, PackedFunc f) {
+ PostOrderVisit(expr, [f](const Expr& n) { f(n); });
+});
+
+// ==================
+// ExprMutatorBase
+
+StructInfo ExprMutatorBase::VisitExprDepStructInfoField(const StructInfo& struct_info) {
+ // recurse into struct info in case they depend on value
+ // under the current scope.
+ return default_struct_info_field_mutator_.VisitStructInfo(struct_info);
+}
+
+ExprMutatorBase::DefaultStructInfoFieldMutator::DefaultStructInfoFieldMutator(
+ ExprMutatorBase* parent)
+ : parent_(parent) {}
+
+Expr ExprMutatorBase::DefaultStructInfoFieldMutator::VisitStructInfoExprField(const Expr& expr) {
+ return parent_->VisitExpr(expr);
+}
+
+PrimExpr ExprMutatorBase::DefaultStructInfoFieldMutator::VisitStructInfoExprField(
+ const PrimExpr& expr) {
+ return parent_->VisitPrimExpr(expr);
+}
+
+StructInfo ExprMutatorBase::DefaultStructInfoFieldMutator::VisitStructInfo_(
+ const FuncStructInfoNode* op) {
+ // Do not recurse into function struct info
+ // as they won't contain ref to values in current scope.
+ return GetRef<StructInfo>(op);
+}
+
+Expr ExprMutatorBase::VisitExpr(const Expr& expr) { return ExprFunctor::VisitExpr(expr); }
+
+Expr ExprMutatorBase::VisitExpr_(const ConstantNode* op) {
+ // Constant' struct info won't be affected by Expr/PrimExpr change.
+ return GetRef<Expr>(op);
+}
+
+Expr ExprMutatorBase::VisitExpr_(const GlobalVarNode* op) {
+ // FuncStructInfo won't be affected by Expr/PrimExpr change.
+ return GetRef<Expr>(op);
+}
+
+Expr ExprMutatorBase::VisitExpr_(const TupleNode* op) {
+ bool unchanged = true;
+ tvm::Array<Expr> fields;
+ for (Expr field : op->fields) {
+ Expr new_field = this->VisitExpr(field);
+ fields.push_back(new_field);
+ unchanged &= new_field.same_as(field);
+ }
+
+ if (unchanged) {
+ // If tuple's struct info change it means that
+ // one of its fields' struct info will change
+ // so un-changed already implies that struct info won't change
+ return GetRef<Expr>(op);
+ } else {
+ // when there is a change return a new tuple node
+ return Tuple(fields, op->span);
+ }
+}
+
+// Visit the use-site of a defined Var
+Expr ExprMutatorBase::VisitExpr_(const VarNode* op) {
+ // struct info of var-use should remain stable
+ // or the var itself will get replaced
+ return GetRef<Expr>(op);
+}
+
+// Visit the use-site of a defined DataflowVar
+Expr ExprMutatorBase::VisitExpr_(const DataflowVarNode* op) {
+ // struct info of var-use should remain stable
+ // or the var itself will get replaced
+ return GetRef<Expr>(op);
+}
+
+Expr ExprMutatorBase::VisitExpr_(const FunctionNode* op) {
+ // struct info of function is not value dependent
+ // so no need to check struct_info field
+ Expr body = this->VisitExpr(op->body);
+
+ if (body.same_as(op->body)) {
+ return GetRef<Expr>(op);
+ } else {
+ return Function(op->params, body, op->ret_struct_info, op->attrs);
+ }
+}
+
+Expr ExprMutatorBase::VisitExpr_(const CallNode* call_node) {
+ Expr new_op = this->VisitExpr(call_node->op);
+ bool unchanged = call_node->op.same_as(new_op);
+
+ Array<StructInfo> sinfo_args;
+ for (StructInfo sinfo_arg : call_node->sinfo_args) {
+ StructInfo new_sinfo_arg = this->VisitExprDepStructInfoField(sinfo_arg);
+ sinfo_args.push_back(new_sinfo_arg);
+ unchanged &= new_sinfo_arg.same_as(sinfo_arg);
+ }
+
+ tvm::Array<Expr> call_args;
+ for (Expr arg : call_node->args) {
+ Expr new_arg = this->VisitExpr(arg);
+ call_args.push_back(new_arg);
+ unchanged &= new_arg.same_as(arg);
+ }
+
+ if (unchanged && VisitAndCheckStructInfoFieldUnchanged(call_node->struct_info_)) {
+ return GetRef<Expr>(call_node);
+ } else {
+ return Call(new_op, call_args, call_node->attrs, sinfo_args, call_node->span);
+ }
+}
+
+Expr ExprMutatorBase::VisitExpr_(const IfNode* op) {
+ Expr guard = this->VisitExpr(op->cond);
+ Expr true_b = this->VisitExpr(op->true_branch);
+ Expr false_b = this->VisitExpr(op->false_branch);
+ if (op->cond.same_as(guard) && op->true_branch.same_as(true_b) &&
+ op->false_branch.same_as(false_b) &&
+ VisitAndCheckStructInfoFieldUnchanged(op->struct_info_)) {
+ return GetRef<Expr>(op);
+ } else {
+ return If(guard, true_b, false_b, op->span);
+ }
+}
+
+Expr ExprMutatorBase::VisitExpr_(const OpNode* op) { return GetRef<Expr>(op); }
+
+Expr ExprMutatorBase::VisitExpr_(const TupleGetItemNode* op) {
+ auto t = this->VisitExpr(op->tuple);
+ if (op->tuple.same_as(t)) {
+ // struct info can be deterministically derived by tuple and index
+ // if t does not change, then struct info won't change.
+ return GetRef<Expr>(op);
+ } else {
+ return TupleGetItem(t, op->index, op->span);
+ }
+}
+
+Expr ExprMutatorBase::VisitExpr_(const PrimValueNode* op) {
+ auto value = this->VisitPrimExpr(op->value);
+ if (op->value.same_as(value)) {
+ // struct info can be deterministically derived by value
+ // if value does not change, then struct info won't change.
+ return GetRef<Expr>(op);
+ }
+ return PrimValue(value, op->span);
+}
+
+Expr ExprMutatorBase::VisitExpr_(const StringImmNode* op) { return GetRef<Expr>(op); }
+
+Expr ExprMutatorBase::VisitExpr_(const DataTypeImmNode* op) { return GetRef<Expr>(op); }
+
+Expr ExprMutatorBase::VisitExpr_(const ShapeExprNode* op) {
+ auto values = op->values.Map([this](const PrimExpr& e) { return this->VisitPrimExpr(e); });
+
+ if (values.same_as(op->values)) {
+ // If values does not change, struct info won't change.
+ return GetRef<Expr>(op);
+ } else {
+ return ShapeExpr(values, op->span);
+ }
+}
+
+Expr ExprMutatorBase::VisitExpr_(const ExternFuncNode* op) {
+ // StructInfo of function remains value independent.
+ return GetRef<Expr>(op);
+}
+
+Expr ExprMutatorBase::VisitExpr_(const SeqExprNode* op) {
+ bool all_blocks_unchanged = true;
+ Array<BindingBlock> blocks;
+ for (auto block : op->blocks) {
+ BindingBlock new_block = this->VisitBindingBlock(block);
+ if (!new_block->bindings.empty()) {
+ blocks.push_back(new_block);
+ }
+ all_blocks_unchanged &= block.same_as(new_block);
+ }
+
+ Expr body = this->VisitExpr(op->body);
+
+ if (all_blocks_unchanged && body.same_as(op->body) &&
+ VisitAndCheckStructInfoFieldUnchanged(op->struct_info_)) {
+ return GetRef<Expr>(op);
+ }
+ return SeqExpr(blocks, body);
+}
+
+BindingBlock ExprMutatorBase::VisitBindingBlock(const BindingBlock& block) {
+ Array<Binding> bindings;
+ if (const auto* node = block.as<BindingBlockNode>()) {
+ for (auto binding : node->bindings) {
+ if (auto var_binding = binding.as<VarBindingNode>()) {
+ Expr new_value = this->VisitExpr(var_binding->value);
+ bindings.push_back(VarBinding(var_binding->var, new_value));
+ } else if (auto match_cast = binding.as<MatchCastNode>()) {
+ Expr new_value = this->VisitExpr(match_cast->value);
+ bindings.push_back(MatchCast(match_cast->var, new_value, match_cast->struct_info));
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << binding->GetTypeKey();
+ }
+ }
+ } else {
+ LOG(FATAL) << "TypeError: Invalid type: " << block->GetTypeKey();
+ }
+
+ if (block.as<DataflowBlockNode>()) {
+ return DataflowBlock(bindings);
+ } else {
+ return BindingBlock(bindings);
+ }
+}
+
+PrimExpr ExprMutatorBase::VisitPrimExpr(const PrimExpr& expr) { return expr; }
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/struct_info.cc b/src/relax/ir/struct_info.cc
index 88046ed81f..9db7cea672 100644
--- a/src/relax/ir/struct_info.cc
+++ b/src/relax/ir/struct_info.cc
@@ -21,7 +21,9 @@
* \file src/relax/ir/struct_info.cc
* \brief Relax struct info.
*/
+#include <tvm/relax/analysis.h>
#include <tvm/relax/struct_info.h>
+#include <tvm/relax/struct_info_functor.h>
#include <tvm/runtime/registry.h>
namespace tvm {
@@ -228,7 +230,17 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
});
// Helper functions
-// TODO(unity-team): add UpdateStructInfo once analysis.cc is upstreamed
+void UpdateStructInfo(Expr expr, StructInfo struct_info) {
+ ICHECK(!expr->struct_info_.defined())
+ << "the struct_info_ of the Expr to be updated must be nullptr for idempotency";
+ expr->struct_info_ = struct_info;
+ // also set checked type
+ expr->checked_type_ = GetStaticType(struct_info);
+}
+
+TVM_REGISTER_GLOBAL("relax.UpdateStructInfo").set_body_typed([](Expr expr, StructInfo struct_info) {
+ UpdateStructInfo(expr, struct_info);
+});
TVM_REGISTER_GLOBAL("ir.ExprStructInfo").set_body_typed([](Expr expr) {
return GetStructInfo(expr);
diff --git a/src/relax/ir/struct_info_functor.cc b/src/relax/ir/struct_info_functor.cc
new file mode 100644
index 0000000000..199491e3c6
--- /dev/null
+++ b/src/relax/ir/struct_info_functor.cc
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file struct_info_functor.cc
+ * \brief Implementations of struct info functors.
+ */
+#include <tvm/relax/struct_info_functor.h>
+
+namespace tvm {
+namespace relax {
+
+void StructInfoVisitor::VisitStructInfo_(const ObjectStructInfoNode* op) {}
+
+void StructInfoVisitor::VisitStructInfo_(const PrimStructInfoNode* op) {}
+
+void StructInfoVisitor::VisitStructInfo_(const ShapeStructInfoNode* op) {
+ if (op->values.defined()) {
+ for (PrimExpr value : op->values.value()) {
+ this->VisitStructInfoExprField(value);
+ }
+ }
+}
+
+void StructInfoVisitor::VisitStructInfo_(const TensorStructInfoNode* op) {
+ if (op->shape.defined()) {
+ this->VisitStructInfoExprField(op->shape.value());
+ }
+}
+
+void StructInfoVisitor::VisitStructInfo_(const TupleStructInfoNode* op) {
+ for (StructInfo field : op->fields) {
+ this->VisitStructInfo(field);
+ }
+}
+
+void StructInfoVisitor::VisitStructInfo_(const FuncStructInfoNode* op) {
+ if (op->params.defined()) {
+ for (StructInfo param : op->params.value()) {
+ this->VisitStructInfo(param);
+ }
+ }
+ this->VisitStructInfo(op->ret);
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const ObjectStructInfoNode* op) {
+ return GetRef<StructInfo>(op);
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const PrimStructInfoNode* op) {
+ return GetRef<StructInfo>(op);
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const ShapeStructInfoNode* op) {
+ Optional<Array<PrimExpr>> values;
+
+ if (op->values.defined()) {
+ // if no changes are made the original array will be returned.
+ values = op->values.value().Map(
+ [this](const PrimExpr& expr) { return this->VisitStructInfoExprField(expr); });
+ }
+
+ if (values.same_as(op->values)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ return ShapeStructInfo(values.value(), op->span);
+ }
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const TensorStructInfoNode* op) {
+ Optional<Expr> shape;
+
+ if (op->shape.defined()) {
+ shape = this->VisitStructInfoExprField(op->shape.value());
+ }
+
+ if (shape.same_as(op->shape)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ return TensorStructInfo(shape.value(), op->dtype, op->span);
+ }
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const TupleStructInfoNode* op) {
+ Array<StructInfo> fields =
+ op->fields.Map([this](const StructInfo& sinfo) { return this->VisitStructInfo(sinfo); });
+
+ if (fields.same_as(op->fields)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ return TupleStructInfo(fields, op->span);
+ }
+}
+
+StructInfo StructInfoMutator::VisitStructInfo_(const FuncStructInfoNode* op) {
+ Optional<Array<StructInfo>> params;
+
+ if (op->params.defined()) {
+ params = op->params.value().Map(
+ [this](const StructInfo& sinfo) { return this->VisitStructInfo(sinfo); });
+ }
+
+ StructInfo ret = this->VisitStructInfo(op->ret);
+
+ if (params.same_as(op->params) && ret.same_as(op->ret)) {
+ return GetRef<StructInfo>(op);
+ } else {
+ ICHECK(ret.defined()) << "FuncStructInfo that contains params must contain ret";
+ return FuncStructInfo(params.value(), ret, op->span);
+ }
+}
+
+} // namespace relax
+} // namespace tvm
diff --git a/src/relax/ir/type.cc b/src/relax/ir/type.cc
new file mode 100644
index 0000000000..49ef1d7163
--- /dev/null
+++ b/src/relax/ir/type.cc
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/ir/type.cc
+ * \brief Relax type system.
+ */
+#include <tvm/relax/type.h>
+#include <tvm/runtime/registry.h>
+
+namespace tvm {
+namespace relax {
+
+TVM_REGISTER_NODE_TYPE(ShapeTypeNode);
+
+ShapeType::ShapeType(int ndim, Span span) {
+ ObjectPtr<ShapeTypeNode> n = make_object<ShapeTypeNode>();
+ n->ndim = ndim;
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_GLOBAL("relax.ShapeType").set_body_typed([](int ndim, Span span) {
+ return ShapeType(ndim, span);
+});
+
+ObjectType::ObjectType(Span span) {
+ ObjectPtr<ObjectTypeNode> n = make_object<ObjectTypeNode>();
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(ObjectTypeNode);
+
+TVM_REGISTER_GLOBAL("relax.ObjectType").set_body_typed([](Span span) { return ObjectType(span); });
+
+DynTensorType::DynTensorType(int ndim, DataType dtype, Span span) {
+ ObjectPtr<DynTensorTypeNode> n = make_object<DynTensorTypeNode>();
+ n->ndim = std::move(ndim);
+ n->dtype = std::move(dtype);
+ n->span = span;
+ data_ = std::move(n);
+}
+
+DynTensorType DynTensorType::CreateUnknownNDim(DataType dtype, Span span) {
+ ObjectPtr<DynTensorTypeNode> n = make_object<DynTensorTypeNode>();
+ n->ndim = -1;
+ n->dtype = std::move(dtype);
+ n->span = std::move(span);
+ return DynTensorType(std::move(n));
+}
+
+TVM_REGISTER_NODE_TYPE(DynTensorTypeNode);
+
+TVM_REGISTER_GLOBAL("relax.DynTensorType").set_body_typed([](int ndim, DataType dtype, Span span) {
+ return DynTensorType(ndim, dtype, span);
+});
+
+PackedFuncType::PackedFuncType(Span span) {
+ ObjectPtr<PackedFuncTypeNode> n = make_object<PackedFuncTypeNode>();
+ n->span = span;
+ data_ = std::move(n);
+}
+
+TVM_REGISTER_NODE_TYPE(PackedFuncTypeNode);
+
+TVM_REGISTER_GLOBAL("relax.PackedFuncType").set_body_typed([](Span span) {
+ return PackedFuncType(span);
+});
+
+} // namespace relax
+} // namespace tvm
diff --git a/tests/python/relax/test_analysis_struct_info_analysis.py b/tests/python/relax/test_analysis_struct_info_analysis.py
new file mode 100644
index 0000000000..faf8fedcf4
--- /dev/null
+++ b/tests/python/relax/test_analysis_struct_info_analysis.py
@@ -0,0 +1,418 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Tests analysis functions of struct info"""
+
+import pytest
+import tvm
+import tvm.testing
+from tvm import relax as rx, TVMError
+from tvm import tir
+
+
+def test_get_static_type_basic():
+ # object
+ s0 = rx.ObjectStructInfo()
+ tvm.ir.assert_structural_equal(rx.analysis.get_static_type(s0), rx.ObjectType())
+
+ # prim
+ s1 = rx.PrimStructInfo("float32")
+ tvm.ir.assert_structural_equal(rx.analysis.get_static_type(s1), tvm.ir.PrimType("float32"))
+
+
+def test_get_static_type_shape():
+ # shape
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+
+ s2 = rx.ShapeStructInfo([1, n + 1, m])
+ s3 = rx.ShapeStructInfo(ndim=2)
+
+ tvm.ir.assert_structural_equal(rx.analysis.get_static_type(s2), rx.ShapeType(ndim=3))
+
+ tvm.ir.assert_structural_equal(rx.analysis.get_static_type(s3), rx.ShapeType(ndim=2))
+
+
+def test_get_static_type_tensor():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ s4 = rx.TensorStructInfo([1, n + 1, m], "int64")
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.get_static_type(s4), rx.DynTensorType(ndim=3, dtype="int64")
+ )
+
+
+def test_get_static_type_tuple():
+ # tuple
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ s0 = rx.ObjectStructInfo()
+ s2 = rx.ShapeStructInfo([1, n + 1, m])
+ s4 = rx.TensorStructInfo([1, n + 1, m], "int64")
+ t0 = rx.TupleStructInfo([s4, s0])
+ t1 = rx.TupleStructInfo([t0, s2])
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.get_static_type(t1),
+ rx.TupleType(
+ [
+ rx.TupleType([rx.DynTensorType(ndim=3, dtype="int64"), rx.ObjectType()]),
+ rx.ShapeType(ndim=3),
+ ]
+ ),
+ )
+
+
+def test_get_static_type_func():
+ # tuple
+ def fn_info(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([c, n, m], "float32")
+ y = rx.TensorStructInfo([c, n, 1], "float32")
+ z = rx.TensorStructInfo([c, n], "float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ def fn_type():
+ x = rx.DynTensorType(ndim=3, dtype="float32")
+ y = rx.DynTensorType(ndim=3, dtype="float32")
+ z = rx.DynTensorType(ndim=2, dtype="float32")
+ return rx.FuncType([x, y], z)
+
+ f0 = fn_info(1)
+
+ tvm.ir.assert_structural_equal(rx.analysis.get_static_type(fn_info(1)), fn_type())
+
+
+def test_erase_to_well_defined_basic():
+ s0 = rx.ObjectStructInfo()
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(s0), s0)
+
+ # prim
+ s1 = rx.PrimStructInfo("float32")
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(s1), s1)
+
+
+def test_erase_to_well_defined_shape():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+
+ s2 = rx.ShapeStructInfo([1, n + 1, m])
+ s3 = rx.ShapeStructInfo(ndim=2)
+ # have undefined
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s2), rx.ShapeStructInfo(ndim=3)
+ )
+ # all defined
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(s2, {n: n, m: m}), s2)
+
+ # replacement
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s2, {n: 2, m: m + 1}), rx.ShapeStructInfo([1, 3, m + 1])
+ )
+
+ # partial defined
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s2, {n: n}), rx.ShapeStructInfo(ndim=3)
+ )
+
+
+def test_erase_to_well_defined_tensor():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ rshape = rx.Var("shape", rx.ShapeStructInfo(ndim=2))
+ s0 = rx.TensorStructInfo(rshape, dtype="int32")
+
+ # undefined
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s0, None, None),
+ rx.TensorStructInfo(ndim=2, dtype="int32"),
+ )
+
+ # defined
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s0, None, {rshape: rshape}), s0
+ )
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s0, None, {rshape: rx.ShapeExpr([1, 2])}),
+ rx.TensorStructInfo([1, 2], dtype="int32"),
+ )
+
+ s1 = rx.TensorStructInfo([m + 1, n], dtype="float32")
+
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(s1, {n: n, m: m}), s1)
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s1, {n: 2, m: 3}),
+ rx.TensorStructInfo([4, 2], dtype="float32"),
+ )
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(s1, {m: m}, {rshape: rshape}),
+ rx.TensorStructInfo(ndim=2, dtype="float32"),
+ )
+
+ s2 = rx.TensorStructInfo([1, 2], dtype="float32")
+
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(s2), s2)
+
+
+def test_erase_to_well_defined_tuple():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ s0 = rx.ObjectStructInfo()
+ s2 = rx.ShapeStructInfo([1, m])
+ s4 = rx.TensorStructInfo([1, n + 1, m], "int64")
+ t0 = rx.TupleStructInfo([s4, s0])
+ t1 = rx.TupleStructInfo([t0, s2])
+
+ tvm.ir.assert_structural_equal(
+ rx.analysis.erase_to_well_defined(t1, {m: m + 1}),
+ rx.TupleStructInfo(
+ [
+ rx.TupleStructInfo(
+ [rx.TensorStructInfo(ndim=3, dtype="int64"), rx.ObjectStructInfo()]
+ ),
+ rx.ShapeStructInfo([1, m + 1]),
+ ]
+ ),
+ )
+
+
+def test_erase_to_well_defined_func():
+ def fn_info(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([c, n, m], "float32")
+ y = rx.TensorStructInfo([c, n, 1], "float32")
+ z = rx.TensorStructInfo([c, n], "float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ f0 = fn_info(1)
+
+ tvm.ir.assert_structural_equal(rx.analysis.erase_to_well_defined(f0), f0)
+
+
+def test_base_check():
+ BR = rx.analysis.BaseCheckResult
+ bcheck = rx.analysis.struct_info_base_check
+
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ obj0 = rx.ObjectStructInfo()
+ prim0 = rx.PrimStructInfo("int32")
+ prim1 = rx.PrimStructInfo("float32")
+
+ shape0 = rx.ShapeStructInfo(ndim=-1)
+ shape1 = rx.ShapeStructInfo(ndim=2)
+ shape2 = rx.ShapeStructInfo(ndim=3)
+ shape3 = rx.ShapeStructInfo([1, 2, 3])
+ shape4 = rx.ShapeStructInfo([1, n, 3])
+
+ tensor0 = rx.TensorStructInfo(ndim=-1, dtype="int32")
+ tensor1 = rx.TensorStructInfo(ndim=-1, dtype="float32")
+ tensor2 = rx.TensorStructInfo(ndim=2, dtype="int32")
+ tensor3 = rx.TensorStructInfo(ndim=2, dtype="float32")
+ tensor4 = rx.TensorStructInfo([n, m], "int32")
+ tensor5 = rx.TensorStructInfo([n, m, 1], "int32")
+ tensor6 = rx.TensorStructInfo([n, m, 2], "int32")
+
+ # obj
+ assert bcheck(obj0, prim0) == BR.PASS
+ assert bcheck(obj0, shape1) == BR.PASS
+ assert bcheck(obj0, tensor2) == BR.PASS
+ assert obj0.is_base_of(tensor2)
+
+ # prim
+ assert prim0.is_base_of(prim0)
+ assert not prim0.is_base_of(prim1)
+ assert bcheck(prim0, obj0) == BR.FAIL_L1
+ assert bcheck(prim0, prim0) == BR.PASS
+ assert bcheck(prim0, prim1) == BR.FAIL_L0
+
+ # shape
+ assert bcheck(shape0, obj0) == BR.FAIL_L1
+ assert bcheck(shape0, prim0) == BR.FAIL_L0
+
+ # unknown dim
+ assert bcheck(shape0, shape1) == BR.PASS
+ assert bcheck(shape1, shape0) == BR.FAIL_L1
+
+ # ndim mismatch
+ assert bcheck(shape1, shape2) == BR.FAIL_L0
+
+ # lhs do not have symbolic value but ndim match
+ assert bcheck(shape2, shape3) == BR.PASS
+
+ # rhs do not symbolic but lhs do
+ assert bcheck(shape3, shape2) == BR.FAIL_L2
+
+ # shape mismatch
+ assert bcheck(shape3, shape4) == BR.FAIL_L2
+ assert shape4.is_base_of(rx.ShapeStructInfo([1, n, 3]))
+
+ # tensor
+ assert bcheck(tensor0, obj0) == BR.FAIL_L1
+ assert bcheck(tensor0, prim0) == BR.FAIL_L0
+ assert bcheck(tensor0, shape0) == BR.FAIL_L0
+
+ # dtype mismatch
+ assert bcheck(tensor0, tensor1) == BR.FAIL_L0
+ assert bcheck(tensor0, tensor3) == BR.FAIL_L0
+ assert bcheck(tensor3, tensor4) == BR.FAIL_L0
+ assert bcheck(tensor1, tensor2) == BR.FAIL_L0
+
+ # ndim mismatch
+ assert bcheck(tensor2, tensor5) == BR.FAIL_L0
+
+ # static shape mismatch
+ assert bcheck(tensor5, tensor6) == BR.FAIL_L0
+
+ # match
+ assert tensor0.is_base_of(rx.TensorStructInfo(ndim=-1, dtype="int32"))
+ assert tensor0.is_base_of(tensor2)
+ assert tensor0.is_base_of(tensor4)
+ assert tensor0.is_base_of(tensor5)
+ assert tensor0.is_base_of(tensor6)
+ assert tensor2.is_base_of(tensor4)
+ assert tensor4.is_base_of(rx.TensorStructInfo([n, m], dtype="int32"))
+
+ # tuple
+ t0 = rx.TupleStructInfo([obj0, tensor0])
+ t1 = rx.TupleStructInfo([prim0, tensor4])
+ t2 = rx.TupleStructInfo([obj0, tensor0, obj0])
+ t3 = rx.TupleStructInfo([tensor0, obj0])
+
+ assert t0.is_base_of(t1)
+
+ assert bcheck(t0, t2) == BR.FAIL_L0
+ assert bcheck(t0, t3) == BR.FAIL_L1
+
+ assert rx.TupleStructInfo([t0, t1]).is_base_of(rx.TupleStructInfo([t1, t1]))
+ assert bcheck(rx.TupleStructInfo([t0, t1]), rx.TupleStructInfo([t1, t0])) == BR.FAIL_L1
+
+ def fn_info_shape(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([c, n, m], "float32")
+ y = rx.TensorStructInfo([c, n, 1], "float32")
+ z = rx.TensorStructInfo([c, n], "float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ def fn_info_erased():
+ x = rx.TensorStructInfo(ndim=3, dtype="float32")
+ y = rx.TensorStructInfo(ndim=3, dtype="float32")
+ z = rx.TensorStructInfo(ndim=2, dtype="float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ assert fn_info_shape(1).is_base_of(fn_info_shape(1))
+ assert fn_info_erased().is_base_of(fn_info_shape(1))
+ assert bcheck(fn_info_shape(1), fn_info_erased()) == BR.FAIL_L2
+
+ fopaque = rx.FuncStructInfo.opaque_func()
+ assert fopaque.is_base_of(fn_info_shape(1))
+
+
+def _check_lca(lhs, rhs, target):
+ tvm.ir.assert_structural_equal(rx.analysis.struct_info_lca(lhs, rhs), target)
+ tvm.ir.assert_structural_equal(rx.analysis.struct_info_lca(rhs, lhs), target)
+
+
+def test_struct_info_lca():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ obj0 = rx.ObjectStructInfo()
+ prim0 = rx.PrimStructInfo("int32")
+ prim1 = rx.PrimStructInfo("float32")
+
+ shape0 = rx.ShapeStructInfo(ndim=-1)
+ shape1 = rx.ShapeStructInfo(ndim=2)
+ shape2 = rx.ShapeStructInfo(ndim=3)
+ shape3 = rx.ShapeStructInfo([1, 2, 3])
+ shape4 = rx.ShapeStructInfo([1, n, 3])
+
+ tensor0 = rx.TensorStructInfo(ndim=-1, dtype="int32")
+ tensor1 = rx.TensorStructInfo(ndim=-1, dtype="float32")
+ tensor2 = rx.TensorStructInfo(ndim=2, dtype="int32")
+ tensor3 = rx.TensorStructInfo(ndim=2, dtype="float32")
+ tensor4 = rx.TensorStructInfo([n, m], "int32")
+ tensor5 = rx.TensorStructInfo([n, m, 1], "int32")
+ tensor6 = rx.TensorStructInfo([n, m, 2], "int32")
+
+ # obj
+ _check_lca(obj0, prim0, obj0)
+ _check_lca(obj0, prim1, obj0)
+
+ # shape
+ _check_lca(shape0, tensor0, obj0)
+ _check_lca(shape0, shape1, shape0)
+ _check_lca(shape1, shape2, shape0)
+ _check_lca(shape1, shape3, shape0)
+
+ _check_lca(shape2, shape3, shape2)
+ _check_lca(shape3, shape4, shape2)
+ _check_lca(shape4, rx.ShapeStructInfo([1, n, 3]), shape4)
+
+ # tensor
+ _check_lca(tensor0, prim0, obj0)
+ _check_lca(tensor0, tensor1, rx.TensorStructInfo(ndim=-1, dtype=None))
+ _check_lca(tensor0, tensor2, tensor0)
+ _check_lca(tensor0, tensor4, tensor0)
+
+ _check_lca(tensor2, tensor4, tensor2)
+ _check_lca(tensor5, tensor6, rx.TensorStructInfo(ndim=3, dtype="int32"))
+ _check_lca(tensor4, tensor5, rx.TensorStructInfo(ndim=-1, dtype="int32"))
+ _check_lca(tensor4, rx.TensorStructInfo([n, m], dtype="int32"), tensor4)
+
+ # tuple
+ t0 = rx.TupleStructInfo([obj0, tensor0])
+ t1 = rx.TupleStructInfo([prim0, tensor4])
+ t2 = rx.TupleStructInfo([obj0, tensor0, obj0])
+ t3 = rx.TupleStructInfo([tensor0, obj0])
+
+ _check_lca(t0, t1, t0)
+ _check_lca(t0, t2, obj0)
+ _check_lca(t0, t3, rx.TupleStructInfo([obj0, obj0]))
+
+ t5 = rx.TupleStructInfo([t0, t1])
+ t6 = rx.TupleStructInfo([t1, t2])
+
+ _check_lca(t5, t6, rx.TupleStructInfo([t0, obj0]))
+
+ t7 = rx.TupleStructInfo([])
+ _check_lca(t7, rx.TupleStructInfo([]), t7)
+
+ def fn_info_shape(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([c, n, m], "float32")
+ y = rx.TensorStructInfo([c, n, 1], "float32")
+ z = rx.TensorStructInfo([c, n], "float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ def fn_info_erased():
+ x = rx.TensorStructInfo(ndim=3, dtype="float32")
+ y = rx.TensorStructInfo(ndim=3, dtype="float32")
+ z = rx.TensorStructInfo(ndim=2, dtype="float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ fopaque0 = lambda: rx.FuncStructInfo.opaque_func()
+ fopaque1 = lambda: rx.FuncStructInfo.opaque_func(ret=prim0)
+ fopaque2 = lambda: rx.FuncStructInfo.opaque_func(
+ ret=rx.TensorStructInfo(ndim=2, dtype="float32")
+ )
+
+ _check_lca(fn_info_shape(1), fn_info_shape(2), fn_info_erased())
+ _check_lca(fn_info_shape(2), fn_info_shape(2), fn_info_shape(2))
+
+ _check_lca(fopaque0(), fopaque1(), fopaque0())
+ _check_lca(fopaque0(), fn_info_shape(1), fopaque0())
+ _check_lca(fopaque2(), fn_info_shape(1), fopaque2())
+
+
+if __name__ == "__main__":
+ tvm.testing.main()
diff --git a/tests/python/relax/test_expr.py b/tests/python/relax/test_expr.py
new file mode 100644
index 0000000000..4eeaed1e0b
--- /dev/null
+++ b/tests/python/relax/test_expr.py
@@ -0,0 +1,258 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import numpy as np
+import pytest
+import tvm
+from tvm import relax as rx
+from tvm import tir
+from tvm.script import relax as R
+
+
+def _check_equal(x, y, map_free_vars=False):
+ tvm.ir.assert_structural_equal(x, y, map_free_vars)
+ tvm.ir.assert_structural_equal(y, x, map_free_vars)
+
+ xhash = tvm.ir.structural_hash(x, map_free_vars)
+ yhash = tvm.ir.structural_hash(y, map_free_vars)
+
+ assert xhash == yhash
+
+
+def _check_json_roundtrip(x):
+ xret = tvm.ir.load_json(tvm.ir.save_json(x))
+ _check_equal(x, xret, map_free_vars=True)
+ return xret
+
+
+def test_var() -> None:
+ v0 = rx.Var("v0")
+ assert v0.name_hint == "v0"
+ assert v0._checked_type_ is None
+ assert v0.struct_info_ is None
+ shape = [54, 96]
+ v1 = rx.Var("v1", R.Tensor(shape, "float32"))
+ assert v1.name_hint == "v1"
+ for s0, s1 in zip(v1.struct_info.shape, shape):
+ assert s0 == s1
+ assert v1.checked_type == rx.DynTensorType(2, "float32")
+ tvm.ir.assert_structural_equal(v1.struct_info, rx.TensorStructInfo(shape, "float32"))
+
+
+def test_dataflow_var() -> None:
+ v0 = rx.DataflowVar("v0")
+ assert v0.name_hint == "v0"
+ assert v0._checked_type_ is None
+ assert v0.struct_info_ is None
+
+ shape = [54, 96]
+ v1 = rx.DataflowVar("v1", R.Tensor(shape, "float16"))
+ assert v1.name_hint == "v1"
+
+ assert v1._checked_type_ == rx.DynTensorType(2, "float16")
+ assert isinstance(v1, rx.DataflowVar)
+ tvm.ir.assert_structural_equal(v1.struct_info, rx.TensorStructInfo(shape, "float16"))
+
+
+def test_tuple() -> None:
+ v0 = rx.Var("v0")
+ v1 = rx.Var("v1")
+ t = rx.Tuple((v0, v1))
+
+ assert t.fields[0] == v0
+ assert t.fields[1] == v1
+ assert t[0] == v0
+ assert t[1] == v1
+ assert t[-1] == v1
+ assert t[-2] == v0
+
+ with pytest.raises(IndexError, match="Tuple index out of range"):
+ t[2]
+
+ with pytest.raises(IndexError, match="Tuple index out of range"):
+ t[-3]
+
+
+def test_match_cast() -> None:
+ # match_cast([16, 8], [m, n])
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ shape = rx.const([16, 8], "int32")
+ var = rx.Var("v0", R.Shape())
+ b0 = rx.MatchCast(var, shape, R.Tensor([m, n], "int32"))
+ assert b0.value == shape
+ assert b0.pattern[0] == m
+ assert b0.pattern[1] == n
+ assert b0.var is not None
+ assert b0.var.checked_type == rx.ShapeType()
+
+ # var1: R.Tensor((m, n), "float32") =
+ # match_cast(var0: R.Tensor("float32", ndim=-1), R.Tensor((m, n), "float32"))
+ value = rx.Var("value", R.Tensor("float32", ndim=-1))
+
+ var = rx.Var("v1", R.Tensor([m, n], "float32"))
+ b1 = rx.MatchCast(var, value, R.Tensor([m, n], "float32"))
+ assert b1.value == value
+ assert b1.pattern[0] == m
+ assert b1.pattern[1] == n
+ assert b1.var is not None
+ assert b1.var.checked_type == rx.DynTensorType(2, "float32")
+
+
+def test_match_cast() -> None:
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ ivalue = rx.Var("input_value")
+ sinfo = rx.TensorStructInfo([n, m], "float32")
+ b0 = rx.MatchCast(rx.Var("v"), ivalue, sinfo)
+ assert b0.value.same_as(ivalue)
+ assert b0.struct_info == sinfo
+ _check_json_roundtrip(b0)
+
+
+def test_var_binding() -> None:
+ v0 = rx.Var("v0")
+ val = rx.const(np.random.rand(24, 56))
+ b0 = rx.VarBinding(v0, val)
+ assert b0.var.name_hint == "v0"
+ assert b0.value == val
+
+
+def test_binding_block() -> None:
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ shape = rx.const([16, 8], "int32")
+ b0 = rx.MatchCast(rx.Var("v0"), shape, R.Tensor([m, n], "int32"))
+
+ v0 = rx.Var("v0")
+ val = rx.const(np.random.rand(24, 56))
+ b1 = rx.VarBinding(v0, val)
+
+ block0 = rx.BindingBlock([b0, b1])
+ assert block0.bindings[0] == b0
+ assert block0.bindings[1] == b1
+
+
+def test_dataflow_block() -> None:
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ shape = rx.const([16, 8], "int32")
+ b0 = rx.MatchCast(rx.Var("v0"), shape, R.Tensor([m, n], "int32"))
+
+ v0 = rx.Var("v0")
+ val = rx.const(np.random.rand(24, 56))
+ b1 = rx.VarBinding(v0, val)
+
+ block0 = rx.DataflowBlock([b0, b1])
+ assert block0.bindings[0] == b0
+ assert block0.bindings[1] == b1
+ assert isinstance(block0, rx.DataflowBlock)
+
+
+def test_seq_expr() -> None:
+ x = rx.Var("foo")
+ bindings = [rx.VarBinding(x, rx.const(1))]
+ blocks = [rx.BindingBlock(bindings)]
+ seqe = rx.SeqExpr(blocks, x)
+ assert seqe.blocks[0] == blocks[0]
+ assert seqe.body == x
+
+
+def test_func():
+ x = rx.Var("foo", R.Tensor(dtype="float32", ndim=2))
+ bindings = [rx.VarBinding(x, rx.const(1))]
+ blocks = [rx.BindingBlock(bindings)]
+
+ seqe = rx.SeqExpr(blocks, x)
+ ret_struct_info = R.Tensor(dtype="float32", ndim=-1)
+ func = rx.Function([x], seqe, ret_struct_info)
+ func = func.with_attr("global_symbol", "func")
+ assert func.params[0] == x
+ assert func.body == seqe
+ assert func.ret_struct_info == ret_struct_info
+ assert func.attrs["global_symbol"] == "func"
+
+
+def test_shape_of():
+ shape = [96, 54]
+ v1 = rx.Var("v1", R.Tensor(shape))
+ s1 = rx.get_shape_of(v1)
+ for x, y in zip(shape, s1):
+ assert x == y
+
+
+def test_shape_expr():
+ m = tir.Var("m", dtype="int64")
+ n = tir.Var("n", dtype="int64")
+ s = rx.ShapeExpr([m, n])
+ assert s.values[0] == m
+ assert s.values[1] == n
+ assert s[0] == m
+ assert s[1] == n
+ assert s[-1] == n
+ assert s[-2] == m
+ assert isinstance(s.struct_info, rx.ShapeStructInfo)
+
+ with pytest.raises(IndexError, match="ShapeExpr index out of range"):
+ s[2]
+
+ with pytest.raises(IndexError, match="ShapeExpr index out of range"):
+ s[-3]
+
+ shape_expr = rx.ShapeExpr([10, 20])
+ assert shape_expr.values[0] == 10
+ assert shape_expr.values[1] == 20
+ assert shape_expr.checked_type == rx.ShapeType(ndim=2)
+ tvm.ir.assert_structural_equal(shape_expr.struct_info, R.Shape((10, 20)))
+
+ x = rx.Var("v0", R.Tensor((10, 20), "float32"))
+ assert x.struct_info.shape[0] == 10
+ assert x.struct_info.shape[1] == 20
+ assert x.struct_info.shape.checked_type == rx.ShapeType(ndim=2)
+ tvm.ir.assert_structural_equal(x.struct_info.shape.struct_info, R.Shape((10, 20)))
+
+ m = tir.Var("m", "int32")
+ with pytest.raises(
+ tvm.TVMError, match="the value in ShapeStructInfo can only have dtype of int64"
+ ):
+ rx.ShapeExpr([m, 3])
+
+
+def test_prim_value():
+ pv = rx.PrimValue(tir.IntImm("int64", 1))
+ assert pv.value.value == 1
+ _check_equal(pv, rx.PrimValue(tir.IntImm("int64", 1)))
+ _check_json_roundtrip(pv)
+
+
+def test_string_imm():
+ s0 = rx.StringImm("hello")
+ s1 = rx.StringImm("hello")
+ assert s0.value == "hello"
+ _check_equal(s0, s1)
+ _check_json_roundtrip(s0)
+
+
+def test_datatype_imm():
+ d0 = rx.DataTypeImm("int32")
+ d1 = rx.DataTypeImm("int32")
+ assert d0.value == "int32"
+ _check_equal(d0, d1)
+ _check_json_roundtrip(d0)
+
+
+if __name__ == "__main__":
+ pytest.main([__file__])
diff --git a/tests/python/relax/test_struct_info.py b/tests/python/relax/test_struct_info.py
new file mode 100644
index 0000000000..80ebc3cb18
--- /dev/null
+++ b/tests/python/relax/test_struct_info.py
@@ -0,0 +1,241 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import tvm
+import tvm.testing
+import pytest
+
+from tvm import relax as rx, TVMError, tir
+
+
+def _check_equal(x, y, map_free_vars=False):
+ tvm.ir.assert_structural_equal(x, y, map_free_vars)
+ tvm.ir.assert_structural_equal(y, x, map_free_vars)
+
+ xhash = tvm.ir.structural_hash(x, map_free_vars)
+ yhash = tvm.ir.structural_hash(y, map_free_vars)
+
+ assert xhash == yhash
+
+
+def _check_json_roundtrip(x):
+ xret = tvm.ir.load_json(tvm.ir.save_json(x))
+ _check_equal(x, xret, map_free_vars=True)
+ return xret
+
+
+def test_object_struct_info():
+ s0 = rx.ObjectStructInfo()
+ s1 = rx.ObjectStructInfo()
+
+ # can turn into str
+ str(s0)
+ _check_equal(s0, s1)
+
+ assert isinstance(s0, rx.ObjectStructInfo)
+ _check_json_roundtrip(s0)
+
+
+def test_shape_type():
+ t0 = rx.ShapeType()
+ t1 = rx.ShapeType()
+ assert t0 == t1
+
+
+def test_dyn_tensor_type():
+ t0 = rx.DynTensorType()
+ assert t0.ndim == -1
+ t1 = rx.DynTensorType(3, "int32")
+ assert t1.ndim == 3
+ assert t1.dtype == "int32"
+
+
+def test_prim_struct_info():
+ s0 = rx.PrimStructInfo("float32")
+ s1 = rx.PrimStructInfo("float32")
+ s2 = rx.PrimStructInfo("int32")
+
+ _check_equal(s0, s1)
+
+ # can turn into str
+ str(s0)
+
+ assert s0 == s1
+ assert s0 != s2
+
+ assert isinstance(s0, rx.PrimStructInfo)
+ _check_json_roundtrip(s0)
+ _check_json_roundtrip(s1)
+
+ assert s1.dtype == "float32"
+ assert s2.dtype == "int32"
+
+ # wrong API constructors
+ with pytest.raises(TVMError):
+ rx.PrimStructInfo(1)
+
+
+def test_shape_struct_info():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+
+ s0 = rx.ShapeStructInfo([1, n + 1, m])
+ s1 = rx.ShapeStructInfo([1, n + 1, m])
+
+ _check_equal(s0, s1)
+
+ assert s0 == s1
+ assert s0.ndim == 3
+ assert s1.ndim == 3
+
+ assert s0.values[2] == m
+
+ assert isinstance(s0, rx.ShapeStructInfo)
+ _check_json_roundtrip(s0)
+ _check_json_roundtrip(s1)
+
+ s2 = rx.ShapeStructInfo(ndim=2)
+
+ assert s2.ndim == 2
+ assert s2.values is None
+ _check_json_roundtrip(s2)
+ assert s0 != s2
+
+ # can turn into str
+ str(s0)
+
+ # wrong argument type
+ with pytest.raises(TVMError):
+ rx.ShapeStructInfo(1)
+
+ # cannot pass both ndim and values
+ with pytest.raises(ValueError):
+ rx.ShapeStructInfo([1, 2], ndim=3)
+
+ # cannot pass both ndim and values even if they are consistent
+ with pytest.raises(ValueError):
+ rx.ShapeStructInfo([1, 2], ndim=2)
+
+
+def test_tensor_struct_info():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+
+ s0 = rx.TensorStructInfo([1, n + 1, m], "float32")
+ s1 = rx.TensorStructInfo(rx.ShapeExpr([1, n + 1, m]), "float32")
+
+ _check_equal(s0, s1)
+
+ assert s0 == s1
+ assert s0.ndim == 3
+ assert s1.ndim == 3
+
+ assert isinstance(s0, rx.TensorStructInfo)
+ _check_json_roundtrip(s0)
+ _check_json_roundtrip(s1)
+
+ s2 = rx.TensorStructInfo(ndim=2, dtype="int32")
+
+ assert s2.ndim == 2
+ assert s2.dtype == "int32"
+ assert s2.shape is None
+ _check_json_roundtrip(s2)
+ assert s0 != s2
+
+ # take in opaque var
+ rshape = rx.Var("shape", rx.ShapeStructInfo(ndim=2))
+
+ s3 = rx.TensorStructInfo(rshape, dtype="int32")
+ assert s3.dtype == "int32"
+ assert s3.shape == rshape
+ assert s3.ndim == 2
+ _check_json_roundtrip(s3)
+
+ # can turn into str
+ str(s0)
+
+ # cannot pass both ndim and values
+ with pytest.raises(ValueError):
+ rx.TensorStructInfo([1, 2], ndim=3)
+
+ # cannot pass both ndim and values even if they are consistent
+ with pytest.raises(ValueError):
+ rx.TensorStructInfo([1, 2], ndim=2)
+
+
+def test_tuple_struct_info():
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+
+ s0 = rx.TensorStructInfo([1, 2, m + n], "float32")
+ s1 = rx.ObjectStructInfo()
+
+ t0 = rx.TupleStructInfo([s0, s1])
+ t1 = rx.TupleStructInfo([s0, rx.ObjectStructInfo()])
+ t2 = rx.TupleStructInfo([s0, s0])
+
+ _check_equal(t0, t1)
+
+ assert t0 == t1
+
+ assert isinstance(t0, rx.TupleStructInfo)
+ t0 = _check_json_roundtrip(t0)
+ t1 = _check_json_roundtrip(t1)
+ t2 = _check_json_roundtrip(t2)
+
+ # can turn into str
+ str(t0)
+
+ # wrong argument type
+ with pytest.raises(TVMError):
+ rx.TupleStructInfo(1)
+
+
+def test_func_struct_info():
+ def fn_info(c):
+ n, m = tir.Var("n", "int64"), tir.Var("m", "int64")
+ x = rx.TensorStructInfo([c, n, m], "float32")
+ y = rx.TensorStructInfo([c, n, 1], "float32")
+ z = rx.TensorStructInfo([c, n, m], "float32")
+ return rx.FuncStructInfo([x, y], z)
+
+ f0 = fn_info(1)
+ f1 = fn_info(1)
+ f2 = fn_info(2)
+ f3 = rx.FuncStructInfo.opaque_func()
+
+ _check_equal(f0, f1)
+
+ assert f0 == f1
+ assert f0 != f2
+
+ assert len(f0.params) == 2
+ assert isinstance(f0.ret, rx.TensorStructInfo)
+ assert f2.derive_func is None
+ assert f3.params is None
+ assert f3.derive_func is None
+ _check_equal(f3.ret, rx.ObjectStructInfo())
+
+ assert isinstance(f0, rx.FuncStructInfo)
+ f0 = _check_json_roundtrip(f0)
+ f1 = _check_json_roundtrip(f1)
+ f2 = _check_json_roundtrip(f2)
+ f3 = _check_json_roundtrip(f3)
+
+ # can turn into str
+ str(f3)
+
+
+if __name__ == "__main__":
+ tvm.testing.main()