## Summary
`ApplyPassToFunction` is a general-purpose wrapper that runs a pass on
only the functions in an IRModule whose name matches a regex. Its sole
in-tree production callers are `DecomposeOpsForInference` /
`DecomposeOpsForTraining` in `src/relax/transform/decompose_ops.cc`, and
both callers always supply a literal function name (never a regex
pattern). Inlining the logic as a file-local helper simplifies the
module-level context and removes an abstraction that exists only to
support one use case.
- Inline the helper as `ApplyDecomposeToFunction` (exact-name match, not
regex) in `src/relax/transform/decompose_ops.cc`
- Delete `src/ir/apply_pass_to_function.cc`, its `transform.h`
declaration, and the Python wrapper in `python/tvm/ir/transform.py`
- Remove two DCE tests
(`test_compatibility_with_apply_pass_to_function`,
`test_well_formed_output_with_restricted_scope`) that tested the
utility's plumbing rather than DCE behavior
This PR enables ruff pyupgrade (UP) rules with py310 target, auto-fixing
~5600 annotation modernizations (PEP 585 generics, PEP 604 unions,
deprecated typing imports).
Also removes from __future__ import annotations from ir/module.py and
rmsnorm.py, bumps requires-python to >=3.10, and removes absolute_import
aliases from topi/contrib files.
This commit addresses various compilation warnings across the codebase:
- Fixed warnings in IR transform infrastructure (transform.h,
transform.cc)
- Updated Python bindings to resolve type-related warnings
(transform.py)
- Addressed warnings in Relax alter_op_impl transformation
- Fixed compilation warnings in TIR schedule compute_inline primitive
These changes improve code quality and ensure clean compilation across
different compilers and platforms.
* [FFI][REFACTOR] Establish tvm_ffi as a standalone python module
This PR establishes tvm_ffi as a standalone python module.
The ffi is structured as a minimal pip module that can be
directly install by path or url.
examples/get_started provided a minimal example.
This is a major change as we are decoupling tvm_ffi as a
separate package, users need to install tvm_ffi separately.
Thanks to its minimal dependency, tvm_ffi can be easily installed
even just from the source by pip install ./ffi
This change would enable future improvement for library plugins
to have lightweight dependencies by just working on top of
the tvm_ffi, while the main compiler toolchain and runtime
can be layered on top.
* [FFI] Improve traceback setups
This PR improves traceback related setups
This PR phases out tvm._ffi redirections in favor of new FFI
new functions are now called via tvm.ffi.
We also enabled limited API support for python 3.12+
so the compiled binary can be forward compatible to future
python versions.
This PR modernizes the FFI foundation of the project and introduce
a new minimal and lightweight module [tvm ffi](https://github.com/apache/tvm/tree/refactor-s3/ffi)
based on our lessons in the past few years. It implements a modern
version of the [Unified Packed and Object RFC](https://github.com/apache/tvm-rfcs/blob/main/rfcs/0097-unify-packed-and-object.md)
that unifies the packed function call and object systems.
Summary of the change:
- A dedicated clean Any/AnyView that can store strong and weak
references of items
- Function(previously PackedFunc) system built on top of the Any/AnyView
- A minimal C API that backs the overall calls. We are stabilizing the
API with a goal to bring clean, stable FFI conventions for both compiled
and registered code
- A rewrite of core python binding and generated code based on the module
- Update existing code and test cases to the new module
- Latest dlpack support
The new module brings many benefits thanks to the cleaner design,
to name a few:
- Any can support both POD types(int) and object types.
- Containers (e.g. Array) can now also contain Any value, e.g. now
`Array<int>` is supported, no need for boxed types
- Error handling now upgrades to object-based, allowing cleaner
traceback across languages
- Map now preserves insertion orders
- Path toward isolated stabilize minimum core ABI/API foundation module
- Type traits based design that cleanly defines how values interact
with Any system
- Automatic conversion of different types based on traits if needed
Because FFI upgrade is at heart of the project, the change touches every
component of the system. Importantly, this is an upgrade of the ABI so the
change is not backward compatible. The code compiled under the old
FFI won't work under the new one. We did provide example ABI translation
(e.g. LegacyTVMArgValueToFFIAny) functions for compatibility.
The PR tries to leave files in their old places while creating redirections.
The goal is to have the first milestone landed and infrastructure in place,
so we can do further refactors to complete features and cleanup legacy code
as trackable PRs. As of now, python binding and compiled code are under the
new convention while RPC and some other bindings still relies on legacy ABI
translation. We will work on upgrades in the coming PRs, including areas such
as reflection, phasing out legacy redirections etc.
* [Support] Add PackedFunc "tvm.support.regex_match"
This function should be used instead of `std::regex` within C++ call
sites, to avoid ABI incompatibilities with pytorch.
Currently, the pytorch wheels available through pip install use the
pre-C++11 ABI by setting `-DUSE_CXX11_ABI=0` [0]. If TVM were to user
the pre-C++11 ABI, this would cause breakages with dynamically-linked
LLVM environments.
Use of the `<regex>` header in TVM should be avoided, as its
implementation is not supported by gcc's dual ABI. This ABI
incompatibility results in runtime errors either when `std::regex` is
called from TVM, or when `std::regex` is called from pytorch,
depending on which library was loaded first. This restriction can be
removed when a version of pytorch compiled using `-DUSE_CXX11_ABI=1`
is available from PyPI.
[0] https://github.com/pytorch/pytorch/issues/51039
* [Redo][Unity] Split DecomposeOpsForTraining into two steps
This is a reapplication of https://github.com/apache/tvm/pull/15954,
after resolving the breakages that required reverting in
https://github.com/apache/tvm/pull/16442. The regex matching is now
implemented without the `#include <regex>` from the C++ stdlib, to
avoid ABI incompatibility with pytorch.
Prior to this commit, the `DecomposeOpsForTraining` transform directly
replaced `relax.nn.batch_norm` into more primitive relax operations.
This required the decomposed form of `relax.nn.batch_norm` to be
duplicated with `DecomposeOpsForInference`. This commit refactors the
pass to occur in two steps, first to apply training-specific
mutations, and then to decompose.
Having a clear `DecomposeOps` pass also has a clear single location
for operator decomposition, which may be migrated into the operator
definition in the future, similar to `FLegalize`.
Previously, type-checking of a callable arguments, such as to
`tvm.ir.transform.module_pass`, was done using
`isinstance(arg, (types.FunctionType, types.LambdaType))`. This check
can give false negatives for valid python types, such as a bound
method or an instance of a class that implements `__call__`.
This commit replaces the checks with the builtin function `callable()`,
which handles any Python object that can be called using function-like
syntax.
* Fix AttributeError when TEST_DATA_ROOT_PATH is set
Initiate a Path object from TEST_DATA_ROOT_PATH to fix the error:
AttributeError: 'str' object has no attribute 'mkdir'
* [DOCS] Add docs for Pass Instrument
- Add a tutorial about how to use pass instrument.
- Add related sections in Pass Infrastructure documents.
* Fix ir.rst, the length of separator.
* Fix unused local name
* Fix linting errors
* Fix linting errors
* Fix linting errors
* Address code-review feedbacks
* Fix linting
* Fix the order of tutorial.
* Add exception handling. Address feedbacks.
* Fix CI error -- clearing instruments in global pass_ctx
* Clarify section hierachy.
* Emphasize to use decorator instead of subclassing
* Add a sentence to explain Pass Instrument. Fix typo.
* Shrink python docs a little.
* Fix tag name.
* Address feedbacks.
* Rename PassContext::ListConfigNames() to PassContext::ListConfigs() and its
Python counterpart tvm.ir.transform.PassContext.list_config_names -> list_configs()
* Adjust PassContext::ListConfigs() to include also metadata (currently only including the data type)
* Adjust unit tests
* Expose C++ PassContext::ListAllConfigs via its Python counterpart
tvm.ir.transform.PassContext.list_configs()
* Add unit tests for the C++ and Python layers
* basic pass profiler prototype
* allow enable/disable of pass profiling
* lint
* add example pass profiler usage as test
* render pass profiles to String instead of stdout
* [REFACTOR][TIR] Migrate BuildConfig to PassContext.
This PR migrates the TIR configurations from BuildConfig to the
PassContext used by the unified IR.
Moving forward, PassContext will be the unified way to configure passes in the TVM stack.
Changes
- Refactored TVM_PASS_REGISTER_CONFIG_OPTION to take in the reference type.
- Removed BuildConfig.
- Migrated the passes to use PassContext.
* Update include/tvm/ir/attrs.h
Co-authored-by: Zhi <5145158+zhiics@users.noreply.github.com>
Co-authored-by: Zhi <5145158+zhiics@users.noreply.github.com>
This PR introduces a new config field to the PassContext
to allow it store arbitary config values.
To make sure that the config is validated, we allow each pass
to register the config key they would expect and the corresponding types.
We also introduce a CreateObject from Map<str, Object> to allow config creation
from a json-nest(like in vscode) in python.
We added an example of UnrollLoopConfig.
Followup PR should migrate the passes to use the new config field.
* [TIR][REFACTOR] Remove te::Tensor dependencies from TIR passes.
te::Tensor is an useful object for tensor expression, but brings
un-necessary reverse dependency in TIR nodes such as Provide and Realize.
This PR is a first step to remove this dependency. We will use Buffer in all the places
where the te::Tensor was used. The rough correspondence are:
- Provide -> BufferStore
- Realize -> BufferRealize
- HalideCall -> BufferLoad.
After this change, we can not use IRModule of PrimFuncs cleanly to represent TIR
at any point of the optimizations. Buffer will serve as the abstraction for the TIR data
models to represent the intermediate storages and their constraints.
We still keep Realize/HalideCall and Provide as TIR nodes for now to make the change minimum.
Right after ScheduleOps, we call SchedulePostProcToPrimFunc to canonicalize the temporary IR
generated by TE(which contains these nodes) to the TIR.
The TIR optimizations are now mostly migrated to to the pass manager.
Followup PRs are needed to migrate the remaining few passes.
* Fix dev tutorial
Previously MakePackedAPI was in the target independent stage,
but never the less requires the device_type information that will be
binded at a later target dependent stage.
The previous implementation was due to the limitation of LoweredFunc
which can not carry buffer_map info(so they have to be lowered right away).
This is no longer the case after the unified IR refactor.
This PR migrates MakePackedAPI to a target dependent stage
and removes the un-necessary BindDevice pass.