pytorch/test/dynamo
Ryan Guo 3141e038f0 [dynamo] Fix VariableBuilder._wrap on frozenset and enforce invariants on ConstantVariable (#141504)
Prior to this patch, we are using `ConstantVariable.create` to create VT
for frozenset objects, and intended yet failed to predicate that on all
itmes being literals (see https://github.com/pytorch/pytorch/pull/140984#discussion_r1847393736).

The code was from https://github.com/pytorch/torchdynamo/commit/7c03434 and
the original goal was to help DBR quantization, but as the new test in
this patch shows, it could lead to silent incorrectness.

Upon a closer look, this exposes some subtleties in how Dynamo handles
`ConstantVariable` and `LOAD_CONST`, so this patch both fixes the
aforementioned issue and documents, enforces, and makes explicit the
invariants around `ConstantVariable` and `LOAD_CONST` -- only immutable
objects are supported.

Specifically, this patch:
1. refine the checks for wrapping a `frozenset` object, document why we
   can't just wrap its items directly due to lack of `Sourcec` for set
   items, and use a safe workaround (`SourcelessBuilder`) to ensure
   soundness while keeping the DBR quantization support.
2. Adds more types to `common_constant_types`, thereby making
   `ConstantVariable.is_base_literal` more lenient, and strictly checks
   this property in the constructor of `ConstantVariable`.
3. Change relevant uses of `create_instruction("LOAD_CONST", ...)` to
   `create_load_const` which checks `is_safe_constant`, and makes
   developer overrides explicit by using `create_load_const_unchecked`
   when needed.
4. In a few places, use more specific `VariableTracker`, e.g.,
   `TypingVariable` rather than `ConstantVariable`, and
   `FrozensetVariable` rather than `SetVariable`.

(2) and (3) are mainly to future-proof Dynamo against bugs like (1).

Pull Request resolved: https://github.com/pytorch/pytorch/pull/141504
Approved by: https://github.com/jansel
2024-11-27 21:58:35 +00:00
..
mock_modules
__init__.py
mock_store_global_crossfile_inline.py
test_activation_checkpointing.py [ca] expose option to collect sizes as dynamic (#141153) 2024-11-22 19:26:27 +00:00
test_after_aot.py
test_aot_autograd.py [Donated Buffer] support metadata mutation ops (#141308) 2024-11-26 17:06:46 +00:00
test_aot_autograd_cache.py Enable autograd cache on inductor tests (#140890) 2024-11-27 20:41:43 +00:00
test_autograd_function.py [Dynamo][autograd.Function] Use fake tensor prop to infer fwd output (#136184) 2024-11-26 01:10:08 +00:00
test_backends.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_backward_higher_order_ops.py [ca] expose option to collect sizes as dynamic (#141153) 2024-11-22 19:26:27 +00:00
test_base_output.py
test_bytecode_utils.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_compile.py Flip default on weights_only (#137602) 2024-11-04 18:30:29 +00:00
test_compiler_bisector.py Class rename (#139490) 2024-11-02 00:10:17 +00:00
test_comptime.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [5/N] (#140663) 2024-11-18 04:11:56 +00:00
test_config.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [5/N] (#140663) 2024-11-18 04:11:56 +00:00
test_ctx_manager.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_cudagraphs.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_cudagraphs_expandable_segments.py
test_debug_utils.py
test_decorators.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_deviceguard.py
test_dynamic_shapes.py
test_exc.py [dynamo] log all graph breaks to graph_breaks logging artifact (#137244) 2024-10-07 22:34:27 +00:00
test_exceptions.py
test_export.py Refactor ShapeGuardPrinter for future C++ addiiton (#140968) 2024-11-27 20:09:58 +00:00
test_export_mutations.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_frame_init.py [dynamo][refactor][config-cleanp] Use guard_manager consistently instead of check_fn (#138896) 2024-10-26 15:14:46 +00:00
test_functions.py [dynamo] fix generic namedtuple support when the class is created via class MyTuple(NamedTuple, Generic[T]): ... (#141360) 2024-11-27 00:21:58 +00:00
test_fx_passes_pre_grad.py
test_global.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_guard_manager.py [dynamo][guards] Introduce a diff_guard_manager (#140250) 2024-11-20 17:59:30 +00:00
test_higher_order_ops.py Revert "Always unspecialize float in OSS (#138922)" 2024-11-26 00:03:03 +00:00
test_hooks.py [ca] expose option to collect sizes as dynamic (#141153) 2024-11-22 19:26:27 +00:00
test_inline_inbuilt_nn_modules.py
test_input_attr_tracking.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_interop.py Remove onnx imports in dynamo (#136334) 2024-09-24 14:54:23 +00:00
test_logging.py Move Sympy printers to torch/utils/_sympy/printers.py (#140597) 2024-11-26 18:11:00 +00:00
test_metrics_context.py [logging] Move population of common MetricsContext fields to record_compilation_metrics (#141291) 2024-11-25 13:18:40 +00:00
test_minifier.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_misc.py [dynamo] Fix VariableBuilder._wrap on frozenset and enforce invariants on ConstantVariable (#141504) 2024-11-27 21:58:35 +00:00
test_model_output.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_modes.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_modules.py Revert "[dynamo][guards] Consider tensors as immutable for dict tag matches (#139560)" 2024-11-19 17:37:44 +00:00
test_nops.py
test_optimizers.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_pgo.py Make PGO work correctly with NJT inputs (#140046) 2024-11-08 04:27:39 +00:00
test_pre_dispatch.py Replace clone.detach with detach.clone (#140264) 2024-11-13 07:01:02 +00:00
test_prim_hop_base.py Add base class for single-subgraph inductor HOPs (#139898) 2024-11-11 16:12:35 +00:00
test_profiler.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_python_autograd.py [BE]: Add better optional typing (#138426) 2024-10-27 14:19:00 +00:00
test_recompile_ux.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_recompiles.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [5/N] (#140663) 2024-11-18 04:11:56 +00:00
test_reconstruct.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_reorder_logs.py
test_repros.py Revert "Always unspecialize float in OSS (#138922)" 2024-11-26 00:03:03 +00:00
test_resume.py
test_sdpa.py
test_skip_guard_eval_unsafe.py [dynamo] skip_guard_eval_unsafe stance for power users (#140251) 2024-11-21 06:28:58 +00:00
test_skip_non_tensor.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_sources.py remove commented out breakpoints (#135363) 2024-09-08 02:15:45 +00:00
test_structured_trace.py Enable autograd cache on inductor tests (#140890) 2024-11-27 20:41:43 +00:00
test_subclasses.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [6/N] (#140688) 2024-11-18 04:09:09 +00:00
test_subgraphs.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [5/N] (#140663) 2024-11-18 04:11:56 +00:00
test_torchrec.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_trace_rules.py
test_unspec.py Restart dynamo analysis when we fail to tensorify away all symfloat inputs (#140346) 2024-11-20 21:20:41 +00:00
test_utils.py Revert "Always unspecialize float in OSS (#138922)" 2024-11-26 00:03:03 +00:00
test_verify_correctness.py [Dynamo] Replace torch._dynamo.optimize() with torch.compile() [2/N] (#140238) 2024-11-13 05:13:39 +00:00
test_view.py
utils.py [dynamo][guards] Support cloning of Guard Manager (#140223) 2024-11-20 05:28:45 +00:00