mirror of
https://github.com/pytorch/pytorch.git
synced 2026-10-25 18:53:39 +08:00
C++ fake tensors are plain torch.Tensor objects, so code that reaches into Python FakeTensor attributes (fake_device, item_memo, constant, real_tensor, dispatch_keys) or checks isinstance(x, FakeTensor) silently skips them. This adds maybe_get_*/maybe_set_* helpers in fake_tensor.py that handle both kinds (plus the _clear_fake_real_tensor binding they need), and switches fake_tensor.py itself over to them and to is_fake_tensor. The following commits in the stack switch the rest of torch over, one file or folder at a time. This is intended to be a refactor for today's (Python FakeTensor) behavior: every new C++ branch is gated on torch._C._is_fake_tensor, which is false for every tensor unless a C++ fake exists. Equivalences worth checking when reviewing: - merge_devices reads the fake device directly instead of t.device. All _find_common_device callers run outside in_kernel_invocation, where FakeTensor.device already returns fake_device. - allow_non_fake_inputs_temporarily is added here as a replacement for mock.patch.object(mode, "allow_non_fake_inputs", True), which cannot restore C++-backed properties. allow_non_fake_inputs is a plain instance attribute set in __init__ on the Python mode. - FakeTensorMode.clear_non_cpu_constants forwards to the converter. Test Plan: ``` python -m py_compile <touched files> lintrunner <touched files> ``` Authored with an AI assistant. Pull Request resolved: https://github.com/pytorch/pytorch/pull/193750 Approved by: https://github.com/ezyang ghstack dependencies: #200174