Skip to content

Commit dc6e233

Browse files
committed
[Relax] Implement relax.transform.TopologicalSort
This commit implements a utility `relax.transform.TopologicalSort`, which can re-order the bindings that occur in a `relax.DataflowBlock`. This is not intended for use in a general-purpose optimization pipeline, but instead as a utility that may be used as needed in specific cases. For example, normalization of unit tests that should not depend on the order of variable binding.
1 parent 254e90a commit dc6e233

File tree

4 files changed

+871
-0
lines changed

4 files changed

+871
-0
lines changed

python/tvm/relax/transform/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
StaticPlanBlockMemory,
7373
ToMixedPrecision,
7474
ToNonDataflow,
75+
TopologicalSort,
7576
UpdateParamStructInfo,
7677
UpdateVDevice,
7778
VMBuiltinLower,

python/tvm/relax/transform/transform.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@ def ToNonDataflow() -> tvm.ir.transform.Pass:
233233
return _ffi_api.ToNonDataflow() # type: ignore
234234

235235

236+
def TopologicalSort(order="depth-first", direction="from-inputs") -> tvm.ir.transform.Pass:
237+
"""Sort relax.Dataflow blocks
238+
239+
Parameters
240+
----------
241+
order: str
242+
243+
The order in which bindings should be emitted. Allowed values
244+
are "depth-first" and "breadth-first".
245+
246+
direciton: str
247+
248+
The direction in which the sort should be performed. Allowed
249+
values are "from-inputs" and "from-outputs".
250+
251+
Returns
252+
-------
253+
ret: tvm.ir.transform.Pass
254+
255+
"""
256+
return _ffi_api.TopologicalSort(order, direction) # type: ignore
257+
258+
236259
def RemovePurityChecking() -> tvm.ir.transform.Pass:
237260
"""Activate relax.force_pure on all pure functions in the module
238261
and unwrap all pure override ops into the normal versions.

0 commit comments

Comments
 (0)