feat-dynamo(0713): Dynamo based ONNX export with subfunction support for Causal-LM models - #1178
Merged
Merged
Conversation
3 tasks
vbaddi
requested review from
anujgupt-github,
ochougul,
quic-amitraj,
quic-hemagnih and
quic-rishinr
July 14, 2026 08:04
Contributor
Author
|
This review needs to be completed by end of this week to have this in mainline. Re: Backward compatibility, we can revert the torch packages from .toml and have it specific to 2.7.0 and dynamo specific users can enable required packages (torch, onnxscript, compressed-tensors) offline for now. @smedhe |
quic-amitraj
requested changes
Jul 14, 2026
Contributor
|
Please remove all the commented code from all the files. |
quic-amitraj
requested changes
Jul 14, 2026
ochougul
reviewed
Jul 16, 2026
ochougul
reviewed
Jul 16, 2026
Comment on lines
+3908
to
+3909
| if getattr(self.model.config, "model_type", None) == "gpt_oss" and not self.continuous_batching: | ||
| bs = 1 |
ochougul
reviewed
Jul 16, 2026
ochougul
reviewed
Jul 16, 2026
ochougul
reviewed
Jul 16, 2026
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Solved review comments for #1178 --------- Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
## Stacked On This PR is stacked on #1194 by targeting `quic:quic-enable-dynamo-for-causallm`. ## Problem Legacy TorchScript export and dynamo export need different ONNX opsets. The previous code used a single `ONNX_EXPORT_OPSET` value everywhere, so both export paths, custom-op symbolic registration, ONNXScript function protos, and the dynamo custom translation table all effectively shared the same opset-backed custom op definitions. For this branch we need: - legacy export (`dynamo=False`) to stay on ONNX opset 17 - dynamo export (`dynamo=True`) to use ONNX opset 18 - QEff custom ops (`CtxScatter`, `CtxGather`, `CustomRMSNorm`, etc.) to emit function bodies built against the matching default-domain opset ## Approach Instead of duplicating `ctx_scatter_gather.py`, `ctx_scatter_gather_cb.py`, and `rms_norm.py` into separate opset-17/opset-18 files, this PR keeps one source definition for each custom op and compiles two ONNXScript variants from it. The helper in `QEfficient/customop/onnxscript_utils.py` temporarily binds the module-level `ops` symbol to the requested default ONNX opset while the ONNXScript decorator compiles the function. The default returned function is the legacy/opset-17 variant, and the dynamo/opset-18 variant is attached to it for explicit lookup. This avoids code duplication while ensuring both export modes get different ONNXScript function protos. ## Key Changes - Adds explicit constants: - `ONNX_LEGACY_EXPORT_OPSET = 17` - `ONNX_DYNAMO_EXPORT_OPSET = 18` - `get_onnx_export_opset(dynamo)` - Updates legacy export paths to pass `opset_version=17`. - Updates dynamo export to pass `opset_version=18`. - Makes `CustomOpTransform.apply(..., onnx_export_opset=...)` mode-aware so it registers custom symbolics and appends ONNXScript function protos for the selected opset. - Passes the selected opset from export into the ONNX transform pipeline. - Updates `DYNAMO_CUSTOM_OP_TABLE` to point at the opset-18 ONNXScript variants, because dynamo uses that table during export before post-export transforms run. - Adds a regression test proving the same `CustomRMSNorm` source can produce opset-17 and opset-18 function protos. ## Why This Design Duplicating the custom-op files would work, but it creates long-term maintenance risk: every custom-op behavior fix would need to be made in two places. This PR centralizes only the opset selection while keeping the custom-op implementation itself single-source. The mode boundary is explicit at export time and transform time. --------- Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com> Co-authored-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
The PR removed _layerwise_safe_export_passes_enabled() guard from layerwise_safe_onnx_export_patches, causing peephole ONNX passes to be disabled for all TorchScript exports. Phi3's SplitToSequence_182 was no longer folded to a constant Split, making the QAIC compiler reject it with 'Non-constant split tensor not supported' when -sub-functions was active. Gate the patches in _export_via_legacy by _layerwise_active, restoring the original behavior: passes are disabled only during layerwise export. Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
…ments (#1211) fix#1178 :solving multi device compile issue and addressed review comments --------- Signed-off-by: Sharvari Medhe <smedhe@qti.qualcomm.com> Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
quic-amitraj
force-pushed
the
quic-enable-dynamo-for-causallm
branch
from
July 23, 2026 15:03
7e7959d to
1f893d5
Compare
…ged two stage of dynamo testing into one Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
quic-amitraj
force-pushed
the
quic-enable-dynamo-for-causallm
branch
from
July 23, 2026 16:57
28570df to
6f33323
Compare
quic-amitraj
approved these changes
Jul 23, 2026
Signed-off-by: Amit Raj <amitraj@qti.qualcomm.com>
quic-amitraj
force-pushed
the
quic-enable-dynamo-for-causallm
branch
from
July 23, 2026 20:15
0c24097 to
969683c
Compare
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds
dynamo=Trueexport support for CausalLM models, enablingtorch.export-based ONNX export with optional repeated-subgraph subfunction extraction (use_onnx_subfunctions=True). It also includes a suite of robustness fixes to the ONNX transform pipeline discovered during integration.What changed
New infrastructure
QEfficient/utils/custom_op_utils.py:select_interfacehelper that switches between thetorch.autograd.Functionpath (TorchScript/eager) andtorch.ops.qefficient.*path (dynamo) at call time, enabling the same model code to work under both exporters without code duplication.QEfficient/utils/export_utils.py:export_wrapperrefactored into four clearly separated export paths (plain TorchScript, TorchScript+subfunctions, flat dynamo, dynamo+subfunctions).use_dynamois now an explicit parameter throughout rather than being re-read fromkwargs. TorchScript monkey-patches are guarded behindif not use_dynamo:so they are never applied on the dynamo path. Added PyTorch version guard that raises aRuntimeErrorwith install instructions whenuse_dynamo=Trueand torch < 2.13.QEfficient/utils/torch_patches.py: Addedtemporarily_disable_nested_compile_regionscontext manager (Path 3) that unwraps static@torch.compiler.nested_compile_regiondecorators for flat-dynamo export, andtemporarily_enable_nested_compile_regionsreserved for future use.QEfficient/base/modeling_qeff.py:_prune_unused_fake_initializers— removesFakeTensor-valued initializers that dynamo injects but are not referenced in the graph, preventing downstream ONNX load failures._exportextended withdynamic_shapesparameter and full dynamo export path: setsdynamo=True, injectscustom_translation_tablemapping alltorch.ops.qefficient.*ops to their ONNX counterparts, and reordersexample_inputs/dynamic_shapesto match the model'sforwardsignature before passing totorch.onnx.export.QEfficient/base/onnx_transforms.py: Two new transforms for the dynamo+subfunctions path:PreserveNestedCacheRetainedStateTransform— exposes CtxScatter outputs from repeated-subgraph function bodies as explicit ONNX outputs and renames KV cache graph inputs frompast_key.N_RetainedState→past_key.Nso the QAIC compiler can pair inputs and outputs correctly.RenameRepeatedSubgraphTransform— renamesrepeated_subgraphNONNX Local Functions to the actual decoder layer class names, with multi-pattern matching across known PyTorch naming conventions andlogger.warningon mismatch or count mismatch.QEfficient/transformers/models/modeling_auto.py:convert_dynamic_axes_to_dynamic_shapesconverts ONNXdynamic_axesdicts totorch.exportDim-keyeddynamic_shapesdicts, handling nestedpast_key_values, compressed KV (DeepseekV3), and CB pool dimensions.QEfficient/utils/constants.py: ONNX export opset bumped from 17 → 18, required by the dynamo ONNX exporter.pyproject.toml: Updated to torch 2.13.0 stable CPU wheels (minimum required for dynamo export); explicit per-python per-arch wheel URLs for x86_64 and aarch64.onnxscriptupdated to0.6.2,compressed-tensorsupdated to0.17.0.CausalLM model wrappers (
llama,mistral,qwen2,qwen3,gemma,gemma2,gpt2,gpt_bigcode,gptj,falcon,mpt,phi,phi3,codegen,starcoder2,granite,granitemoe,mixtral_moe,qwen3_moe,olmo2,glm4_moe,grok_1,deepseek_v3,gpt_oss,llama_swiftkv):QEffDecoderLayer.forwarddecorated with@torch.compiler.nested_compile_regionso dynamo emits repeated-subgraph ONNX Local Functions instead of inlining each layer individually.torch.tensor(MIN_MASKED_ATTENTION_VALUE)replaced withtorch.full_like(attn_weights, ...)throughout — removes a scalar constant that dynamo cannot trace as a dynamic value and that caused shape inference failures during export.select_interfacewired into all custom op call sites (CtxScatter,CtxGather,CustomRMSNorm, and CB/3D variants) so both the TochTrace and dynamo paths dispatch to the correct backend op without branching in model code.