Skip to content

[BYOC] Fix tir_vars handling for dynamic shape BYOC backends - #20753

Open
Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix-byoc-tir-vars
Open

Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix-byoc-tir-vars

Conversation

@Asthenia0412

Copy link
Copy Markdown

Background

TVM's BYOC (Bring Your Own Codegen) flow with dynamic shapes fails when FuseOps injects ShapeType parameters (tir_vars) into Composite functions. The JSON codegen path doesn't properly handle these non-Tensor arguments, causing crashes in external codegen backends like TensorRT.

Problem

  • JSONSerializer crashes when serializing ShapeType parameters (no Tensor type annotation)
  • Call sites don't filter out ShapeType parameters before calling external functions
  • External codegen receives misaligned argument lists

Solution

  • JSONSerializer::serialize() skips ShapeType params but registers them in memo_ to prevent subsequent Var ref crashes
  • AddNode() handles TensorType without shape annotations (FuseOps-generated Composite returns)
  • run_codegen.cc pre-scans BYOC functions, builds ShapeType param masks, and filters call-site args

Testing

  • Example NPU backend test suite: 7/7 pass
  • Custom dynamic shape tests with symbolic arithmetic (n//2): RunCodegen succeeds, call-site correctly filters to (lv, bias)
  • Simple symbolic variable case: passes

Fixes #20037

When compiling Relax models with dynamic shapes through BYOC backends
(e.g. TensorRT), FuseOps injects a tir_vars: Shape([...]) parameter
into Composite functions that have symbolic arithmetic in their shape
expressions. The original code had two bugs:

1. JSONSerializer in codegen_json.h asserted when encountering
   ShapeType params (tir_vars), since it expected all function params
   to be tensor inputs.

2. AddNode() asserted when the function return type had no explicit
   shape (FuseOps generates Composite function returns with only
   ndim/dtype and no shape annotation).

3. RunCodegen forwarded the ShapeType tir_vars params as regular
   tensor arguments to the external codegen, which cannot handle them.

Fixes:
- codegen_json.h serialize(): skip ShapeType params but register them
  in memo_ (empty entry) so body expressions still resolve
- codegen_json.h AddNode(): handle TensorType without shape using -1
  as placeholder for unknown dynamic dims
- run_codegen.cc: pre-scan BYOC functions, build param filter masks
  excluding ShapeType params, and filter call-site args accordingly

All existing BYOC backends (TensorRT, CUTLASS, DNNL, NNAPI, etc.)
benefit from this fix. Existing example_npu tests still pass.

Fixes apache#20037

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Relax][BYOC] JSONSerializer fails to handle tir_vars in BYOC for models with symbolic arithmetic

1 participant