Skip to content

feat(executorch): forward ExecuTorch lowering kwargs through save(output_format="executorch")#4433

Open
Conarnar wants to merge 3 commits into
pytorch:mainfrom
Conarnar:feat/executorch-save-lowering-kwargs
Open

feat(executorch): forward ExecuTorch lowering kwargs through save(output_format="executorch")#4433
Conarnar wants to merge 3 commits into
pytorch:mainfrom
Conarnar:feat/executorch-save-lowering-kwargs

Conversation

@Conarnar

Copy link
Copy Markdown

Description

torch_tensorrt.save already forwards partitioners= and backend_config= to the
ExecuTorch lowering. Forward the remaining to_edge_transform_and_lower kwargs the
same way -- pop them in save(), thread them through the three _save_as_executorch
call sites, and pass them to to_edge_transform_and_lower(...):

  • constant_methods: bake constant methods (e.g. LLM-runner metadata) into the .pte
  • transform_passes: run caller-supplied edge transform passes
  • compile_config: override the EdgeCompileConfig. Defaults to
    _check_ir_validity=False (the TRT execute_engine placeholder
    graph fails edge IR validation); a caller-supplied config is
    forwarded verbatim so an explicit _check_ir_validity is honored.
  • generate_etrecord: generate + persist an ETRecord as "_etrecord.bin" next
    to the .pte, following ExecuTorch's example convention
    (e.g. examples/cuda/scripts/export.py)

Tests

test_save_executorch_forwards_lowering_kwargs and
test_save_executorch_defaults_when_lowering_kwargs_omitted assert each kwarg is
forwarded, that an omitted compile_config defaults to _check_ir_validity=False
while a caller-supplied one is forwarded verbatim, and that the ETRecord is written.

Requires #4404

Conarnar added 3 commits July 23, 2026 15:08
lift() built the placeholder's fake meta["val"] with torch.empty_strided from
shape only, so a lifted non-fp32 or non-CPU constant silently got cpu/float32
meta (e.g. a FakeTensorDeviceMismatchError, cuda:0 vs cpu, when a weighted op is
excluded from TensorRT). Fakify the source constant through the graph's own
fake_mode instead -- fake_mode.from_tensor(lift_val, static_shapes=True). The
placeholder meta now preserves the source dtype, device and stride and belongs to
the graph's FakeTensorMode, and no real tensor is allocated on lift_val.device
(so a CUDA constant no longer needs a live GPU, or risks OOM, at export time).
Adds unit tests (bf16 dtype + CUDA device) asserting dtype, device, stride, and
that the meta is a FakeTensor.
… delegation

The CUDA (AOTInductor) backend externalizes its weight constants, so save() must
write the .ptd next to the .pte or the runtime loads a null weights blob and reads
out of bounds. Keep that write, extracted into _write_external_tensor_data() and
guarded on _tensor_data directly (no getattr default) so a future rename fails loud
instead of silently skipping. Document that the runtime must be pointed at the .ptd
(co-location is a convention, not an automatic search).

Drop the cuda_fallback=True flag: save(partitioners=[CudaPartitioner(...)]) already
appends a CUDA catch-all after the TensorRT partitioner, so the flag only duplicated
that, and 'fallback' wrongly implied a downgrade rather than a second GPU delegate.
Document the partitioners= recipe in saving_models.rst and the save() docstring.
Composition-only test added (build, not load/run); a load-run test follows the ATen
device-pointer runtime fix.

Test-Plan: pytest tests/py/dynamo/executorch/{test_api,test_edge_cases}.py -m unit
…put_format="executorch")

torch_tensorrt.save already forwards partitioners= and backend_config= to the
ExecuTorch lowering. Forward the remaining to_edge_transform_and_lower kwargs the
same way -- pop them in save(), thread them through the three _save_as_executorch
call sites, and pass them to to_edge_transform_and_lower(...):

- constant_methods:  bake constant methods (e.g. LLM-runner metadata) into the .pte
- transform_passes:  run caller-supplied edge transform passes
- compile_config:    override the EdgeCompileConfig. Defaults to
                     _check_ir_validity=False (the TRT execute_engine placeholder
                     graph fails edge IR validation); a caller-supplied config is
                     forwarded verbatim so an explicit _check_ir_validity is honored.
- generate_etrecord: generate + persist an ETRecord as "<base>_etrecord.bin" next
                     to the .pte, following ExecuTorch's example convention
                     (e.g. examples/cuda/scripts/export.py)

Tests: test_save_executorch_forwards_lowering_kwargs and
test_save_executorch_defaults_when_lowering_kwargs_omitted assert each kwarg is
forwarded, that an omitted compile_config defaults to _check_ir_validity=False
while a caller-supplied one is forwarded verbatim, and that the ETRecord is written.
@meta-cla meta-cla Bot added the cla signed label Jul 25, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jul 25, 2026
@github-actions
github-actions Bot requested a review from narendasan July 25, 2026 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant