torch.fx.passes.runtime_assert.insert_deferred_runtime_asserts
- torch.fx.passes.runtime_assert.insert_deferred_runtime_asserts(gm, shape_env, name, export=False)
During tracing, we may have discovered that some data-dependent values had runtime assert on them; e.g., torch.empty(x.item()) induces a runtime that x.item() >= 0. These asserts can happen unpredictably during fake tensor propagation, so we cannot conveniently insert them into the FX graph when they occur. Instead, we accumulate them in the ShapeEnv, and in this pass insert them into the graph as proper tests.
This pass also deduplicates size-related computation, CSE-ing ops that produce symbolic values and/or are involved in runtime asserts. Additionally, shape calls (size/stride/storage_offset) are turned into compute on input sizes if possible, allowing intermediate tensors to be freed earlier. For example, here dynamo will DCE the cat and repeat calls: