perf(runtime): skip TensorRT engine fakification during export by reporting tracing_mode "real" - #4489
Open
Conarnar wants to merge 1 commit into
Open
perf(runtime): skip TensorRT engine fakification during export by reporting tracing_mode "real"#4489Conarnar wants to merge 1 commit into
Conarnar wants to merge 1 commit into
Conversation
torch.export fakifies every torchbind script object it traces through. For a TensorRT engine that is enormously expensive twice over: maybe_to_fake_obj calls __obj_flatten__, which runs TRTEngine::serialize() (a full cuda_engine->serialize() plus base64) and then base64-decodes it again in FakeTRTEngine.__obj_unflatten__, and FakeScriptObject.__init__ deep-copies the engine through its pickle, which serializes and then fully deserializes another copy onto the device. The fake stores the resulting blob and never reads it -- every field it uses is metadata. ExecuTorch re-runs decompositions during to_edge_transform_and_lower, so this happens roughly three times per engine, and the cost grows with engine count and size. torch offers an opt-out: tracing_with_real() returns True for objects whose tracing_mode() reports "real", and maybe_to_fake_obj then hands the real object to the meta kernel untouched. fake_tensorrt_execute_engine already handles that case -- it branches on real_obj and otherwise reads get_serialized_metadata(), taking output shapes from the stored symbolic expressions -- so nothing executes or mutates the engine during tracing. The Python TRTEngine has reported "real" for this reason since it was added; the TorchBind class simply never did. Measured on a 2-layer Gemma4-MoE hybrid TensorRT + CUDA ExecuTorch export (3 engines): 706s -> 180s wall, with the fakification phase going from 518s to 0.0s. The exported program is unchanged -- identical delegate count, operators, values, and byte-identical delegate payloads.
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.
Description
torch.exportfakifies every TorchBind script object it traces through, and for a TensorRT engine that is expensive twice over:maybe_to_fake_objcalls__obj_flatten__, which runsTRTEngine::serialize()— a fullcuda_engine->serialize()plus base64 — andFakeTRTEngine.__obj_unflatten__then base64-decodes it again.FakeScriptObject.__init__deep-copies the real object, which for a TorchBind class goes throughdef_pickle: serialize, then a fulldeserializeCudaEngineof a second copy onto the device.The fake never reads any of it.
FakeTRTEngineassigns the blob toself.serialized_engineand every field it actually uses is metadata. ExecuTorch re-runs decompositions duringto_edge_transform_and_lower, so this happens roughly three times per engine, and the cost grows with engine count and size.torchalready provides the opt-out:tracing_with_real()returns True for an object whosetracing_mode()reports"real", andmaybe_to_fake_objthen hands the real object to the meta kernel untouched. This adds that method to the TorchBind engine.Why this is safe
fake_tensorrt_execute_enginealready handles being given a real engine: it branches onreal_objand otherwise callsget_serialized_metadata()directly, taking output shapes from the symbolic expressions stored in the engine's metadata. Nothing executes and nothing mutates the engine during tracing.The Python
TRTEnginehas reported"real"for exactly this reason since it was added — its docstring citestracing_with_real. The TorchBind class simply never did the same.Measurements
The mechanism is
torch.exportfakification, so any export carrying TensorRT engines benefits; the numbers below come from a two-layer Gemma4-MoE exported through the hybrid TensorRT + CUDA ExecuTorch path (3 engines), fresh Inductor cache, otherwise identical runs:The exported program is unchanged: same delegate count, operators and values, and byte-identical delegate payloads.
Testing
Verified by rebuilding and re-running the export above with no patches: fakification drops to zero and the artifact is byte-identical to the baseline. Existing
tests/py/dynamo/executorchandtests/py/dynamo/lowering/test_buffer_lifting.pypass (75).No automated test is included. Nothing in
tests/currently exercises fakification of an engine, and the meaningful property — that export stops serializing the engine per fakify call — is a timing assertion. The cheapest real check would be assertingtracing_mode() == "real"on a built engine, which requires a GPU. Happy to add that if you want it gated.