19 Commits

Author SHA1 Message Date
Shushi Hong eafcba1c44 [Relax][TensorRT] Fix YOLO BYOC offload and partitioning gaps (#19998)
Fixes #19887.

This PR fixes several Relax TensorRT BYOC issues exposed by YOLO-style
models:

- adds TensorRT support for SiLU and resize2d
- preserves operand and TupleGetItem ordering during codegen
- fixes cyclic and unsafe Tuple/TGI region merging
- handles static Shape bindings and nested packed-function outputs
- normalizes PrimType dtype arguments passed to relax.arange

With these changes, yolo11n-seg can be merged into a single TensorRT
region, while yolo11n can be imported and partitioned successfully.
2026-07-16 15:57:40 -04:00
Tianqi Chen e479a5dbe7 [RUNTIME][PYTHON] Add explicit Target device conversion (#20005)
## Summary

Compiler Targets can carry device-type semantics that runtime
device-name parsing does not preserve.

- add `tvm.device_from_target` for canonical Target-to-Device
translation
- use explicit runtime constructors where the device kind is fixed
- update target-derived utilities, tests, and documentation to use the
explicit boundary
2026-07-15 05:34:21 +08:00
Tianqi Chen 3452fd4ffa [TEST] Serialize local GPU execution under pytest-xdist (#19942)
Add tvm.testing.run_with_gpu_lock backed by the existing
tvm_ffi.utils.FileLock. Migrate live local GPU tests to acquire the
machine-local lock around device execution, synchronization, host
transfer, and checks while leaving target construction and compilation
outside the critical section.

Replace the custom xdist scheduler with standard xdist_group placement
for the order-dependent test family. RPC tests retain dynamic port
allocation and per-test process isolation rather than gaining a broad
category lock.
2026-07-04 17:49:45 -04:00
Tianqi Chen 275114b327 [REFACTOR][IR] Unify PrimExpr with Expr typed view (#19910)
## Summary
- Make `PrimExpr` a typed C++ view over `Expr` values whose
`ExprNode::ty` is `PrimType`, instead of using a separate runtime node
class as the proof of primitive-ness.
- Use the shared `ir::Call` node for Relax, TIRX, and primitive-valued
calls, while keeping primitive-only APIs explicit at their semantic
boundaries.
- Keep Python on the general `Expr` surface for primitive-typed values
so `isinstance` behavior does not imply a nominal primitive-expression
subclass.

## Design Rationale
The main advantage of this change is that common expression nodes such
as `Call` can be unified without specializing each one to `PrimType`. A
single `ir::Call` can represent a Relax tensor call, a Relax scalar
call, or a primitive-valued intrinsic call; the result type stored in
`ExprNode::ty` determines whether that particular value can be viewed as
`PrimExpr`.

This keeps the IR node hierarchy focused on expression structure rather
than result-type categories. Nodes that are intrinsically primitive,
such as integer and floating-point literals or TIRX primitive operators,
still have strongly typed C++ APIs and data structures. General nodes
whose result type may vary, such as `Call`, remain general `Expr` nodes
and are narrowed to `PrimExpr` only where primitive-only semantics are
required.

The PR also keeps the compatibility surface practical: C++
primitive-only APIs continue to accept `PrimExpr`, Python exposes a
compatibility predicate for checking the primitive typed category, and
visitors/printers use one natural `Call` path rather than duplicating
Relax and primitive call handling. Missing expression types are
represented explicitly with `Type::Missing()` so constructors can leave
type inference to later analysis without relying on nullable `Type`
values.
2026-07-01 18:55:33 -04:00
Tianqi Chen 120812e9ac [REFACTOR][Relax] Phase out PrimValue and Relax expression wrappers (#19891)
This PR lets Relax expressions directly take `PrimExpr` values without
requiring the explicit `PrimValue` wrapper, continuing the Relax IR
unification work by removing Relax-specific leaf/base expression layers.

Summary:
- Remove `LeafExpr` / `LeafExprNode` and use direct expression-node
checks where needed.
- Converge Relax expression typing onto the shared IR `Expr` base.
- Remove the `PrimValue` node wrapper while keeping `relax.prim_value` /
`R.prim_value` as conversion helpers that return existing `PrimExpr`
values unchanged.
- Register direct `PrimExpr` handling through exact concrete node
dispatch, aligned with the `tirx` expression visitor list and excluding
arith iter-map intermediate nodes.
- Inline the private Python primitive conversion helper into public
`relax.prim_value`.
- Handle direct `PrimExpr` values in frontend scalar paths without
assuming a `.value` field on non-immediate expressions.
2026-06-26 07:18:04 -04:00
Tianqi Chen 1bb5cf6102 [REFACTOR][IR] Unify StructInfo and Type (#19853)
## Summary

- unify Relax's former StructInfo surface into the Type vocabulary and
Expr.ty storage path
- remove leftover DependentTypeNode and legacy OpNode::op_type storage
- keep base Type nullable while concrete Relax/DTensor type refs are
non-nullable
- clean stale StructInfo/TensorStructInfo/sinfo vocabulary in
Python/docs and distributed-op macros
- address Gemini follow-ups for parser annotations, BlockBuilder
docstring, and Adreno TensorType cast audit
2026-06-21 10:12:12 -04:00
Shushi Hong b7807dbc1b [Relax][TensorRT] Add partition_for_tensorrt and a pattern table (#19820)
This pr is the follow-up pr to #19810 Add partition_for_tensorrt, which
offloads TensorRT-supported subgraphs from a module with a single call,
together with the pattern table whose composite names ("tensorrt.<op>")
match the runtime converter registry. This is the entry point other BYOC
backends expose as partition_for_<name>.
2026-06-18 14:47:47 -04:00
Shushi Hong 246e290267 [Relax][TensorRT] Update BYOC operator converters from Relay to Relax (#19810)
This pr is the follow-up pr to #19789. CurrentTensorRT BYOC converters
were ported from Relay and still read attribute names/shapes that no
longer match the Relax ops, so most ops crashed ("Key: <name> is not
found") or produced wrong results when offloaded.

This pr changed
- Converters (tensorrt_ops.cc): port reduce, matmul, expand_dims,
layer_norm, clip, reshape, strided_slice, split and layout_transform to
read Relax's attributes/arguments. Notable shape changes: clip min/max
are PrimValue arguments (not a_min/a_max attrs), reshape's shape is a
Shape argument, matmul has no transpose flags, split is multi-output
with no "mode", and layout_transform is an IndexMap rather than
src/dst_layout strings. Unsupported cases (non-static reshape,
non-permutation layout_transform) now raise a clear error instead of
crashing.
- Codegen (codegen.cc): serialize an op's non-tensor arguments
(PrimValue / ShapeExpr / tuple) as "arg_"-prefixed node attributes,
materialize a reduce op's all-axes default, and translate a
pure-permutation layout_transform IndexMap into a transpose order.
- Runtime: disable the TF32 builder flag so offloaded FP32 subgraphs
match TVM's FP32 reference, and use a process-lifetime TensorRT logger
(a per-runtime logger was left dangling once its runtime was destroyed,
corrupting the heap during TensorRT teardown).

All tests are validated locally.
2026-06-17 08:03:20 -04:00
Shushi Hong d591cd461f [Relax][TensorRT] Update TensorRT runtime to 10 (#19789)
This pr fixes #19609. TensorRT 10 removed a large set of APIs that the
Relax TensorRT BYOC integration relied on, so it failed to compile
against TRT >= 10. Port the runtime and codegen to the TRT10 API and
require TensorRT >= 10:

- Lifetime: obj->destroy() -> delete (destroy() removed in TRT10).
- Builder: drop implicit-batch mode (networks are always explicit-batch
via createNetworkV2(0); setMaxBatchSize removed); setMaxWorkspaceSize ->
setMemoryPoolLimit(kWORKSPACE); buildEngineWithConfig ->
buildSerializedNetwork + deserializeCudaEngine, keeping the IRuntime
alive alongside the engine.
- Execution: the binding-index model (getNbBindings / getBindingIndex /
setBindingDimensions / execute / executeV2) -> the named-tensor model
(getNbIOTensors / setInputShape / setTensorAddress / enqueueV3);
deserializeCudaEngine drops the trailing IPluginFactory* argument.
- Layers: addConvolution / addPooling / addDeconvolution / addPadding ->
the *Nd variants; set{Stride,Dilation} -> *Nd; IFullyConnectedLayer /
addFullyConnected removed -> dense rebuilt with addConstant +
addMatrixMultiply.
- Add a build-time guard that emits a clear error on TensorRT < 10.

Also fix pre-existing issues that prevented this path from running
end-to-end: the runtime had drifted from the current tvm-ffi API
(TVMTensorCopyToBytes / TVMGetLastError, VectorToTrtDims over
ffi::Array, a stale `override` on the destructor), and the conv
converters read a Relay-era "channels" attribute that Relax does not
emit (output channels are now derived from the kernel shape).

All tests are verified correct locally. This pr barely includes api
updates and there is no new parts added
2026-06-16 07:30:13 -04:00
Shushi Hong e4da848e57 [Tests] Modernize test gating (#19777)
This pr modernizes test gating. It replaces the heavy
`tvm.testing.Feature` machinery with a thin `tvm.testing.env` module of
`has_*()` capability probes, used via standard pytest.mark + skipif. And
markers move to `pyproject.toml`
2026-06-15 18:50:57 -04:00
Tianqi Chen 33dcea1686 [REFACTOR][LINT] Modernize ruff config (#18810)
This PR removes the extra lint violations from the codebase so lint
aligns with the latest style
2026-02-23 07:29:21 -05:00
Tianqi Chen aa2e609136 [LINT] Modernize lint to use pre-commit hooks (#18807)
This PR migrates existing lint to use pre-commit hooks
2026-02-22 11:03:21 -05:00
Tianqi Chen 3c36ce2ec6 [FFI][REFACTOR][ABI] Rename NDArray to Tensor (#18275)
This PR Updates the NDArray => Tensor.

Both tensor and ndarray are commonly used terms.

Because the term Tensor is getting more common in the context of ML,
we do the rename to stay more aligned with torch.Tensor and DLTensor.
2025-09-06 14:33:59 -07:00
Siyuan Feng be8e43814a [Refactor] Migrate build API to tvm.compile (#17718)
* tvm.build -> tvm.compile

* relax.build -> tvm.compile

* update
2025-03-09 07:23:52 -04:00
apeskov 11f2253b9c Restore "pytest.mark.gpu" for RELAX tests (#16741)
* [TEST] Mark RELAX GPU tests with pytest.mark.gpu

Missed pytest.mark.gpu prevents tests from launch in CI.

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>

* fix

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>

* Check fp8 compute capability

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>

* fix func signature

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>

* lint

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>

---------

Signed-off-by: Alexander Peskov <alexander.peskov@deelvin.com>
Co-authored-by: Alexander Peskov <alexander.peskov@deelvin.com>
2024-04-23 11:22:55 +03:00
Eric Lunderberg ebbe38f328 [Unity] Include LegalizeOps in the default relax.build lowering flow (#15864)
Prior to this commit, `relax.transform.LegalizeOps` needed to be
called prior to `relax.build`.  This commit adds `LegalizeOps` to the
lowering flow, to simplify the calling steps for an end-user.  If the
`IRModule` contains no legalizable functions, a second legalization
pass has no effect.

Some test cases relied on this behavior as an implicit assertion that
operator fusion patterns applied.  That is, by omitting `LegalizeOps`,
a successful compilation `relax.build` would only occur if all
legalizable operators have already been removed, and so an incorrect
fusion pattern would result in a failure to build the module.  While
these tests would be better expressed by comparing against an expected
fused pattern, updating the tests is outside the scope of this PR.  To
allow these tests to keep their implicit assertions, a
`"relax.transform.apply_legalize_ops"` config can be used to disable
the `LegalizeOps` pass.
2023-10-26 10:15:59 -05:00
masahi 89cff055d3 [Unity][BYOC] Use Relax legalize + CPU build for reference in tests (#14162)
* clean dnnl test

* clean trt test

* clean cutlass test

* fix gelu legalize for fp16

* use memoize in dnnl and trt tests
2023-04-01 15:31:37 -04:00
Tianqi Chen 6b87e35617 [Unity] Refactor Relax Build JIT UX (#14088)
This PR refactors relax build so it get exposed at the opt-level.
We also introduces an explicit jit functionality to handle
live loading of compiled artifacts from cutlass.

We also move relax vm to runtime so it can be clearly isolated
from the rest of the compiler stack.
2023-04-01 15:31:37 -04:00
Sunghyun Park b137d22ed4 [Unity][BYOC][Pass] RunCodegen and TensorRT (#14078)
This PR introduces the fundamental workflow for BYOC and integrate TensorRT as a demonstration.
2023-04-01 15:31:37 -04:00