`torch.compile` fails to trace `datetime.now()` with Dynamo guard check failure
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.5k
- PR merge metrics
- PR metrics pending
Description
### 🐛 Describe the bug
When compiling a model that uses `datetime.now()` function, `torch.compile` fails with a Dynamo guard check error. The warning message explicitly identifies this as a Python builtin that Dynamo cannot trace, and suggests filing an issue to add support.
```python
import torch
from datetime import datetime
class TestModel(torch.nn.Module):
def forward(self, x):
current_time = datetime.now()
return x + current_time.second
x = torch.randn(5)
model = TestModel()
print("Eager output:", model(x))
print("Compiled output:", torch.compile(model)(x))
```
### Error logs
```
Eager output: tensor([51.0676, 52.0309, 52.6077, 50.6691, 53.6591])
D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\variables\functions.py:1598: UserWarning: Dynamo does not know how to trace the builtin `.datetime.now.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).
If it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.
If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.
torch._dynamo.utils.warn_once(explanation + "\n" + "\n".join(hints))
Traceback (most recent call last):
File "E:\DL_Compiler_Test\torch_code\test.py", line 12, in
print("Compiled output:", torch.compile(model)(x))
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\eval_frame.py", line 418, in __call__
return super().__call__(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\nn\modules\module.py", line 1777, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\nn\modules\module.py", line 1788, in _call_impl
return forward_call(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\eval_frame.py", line 886, in compile_wrapper
return fn(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\nn\modules\module.py", line 1777, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\nn\modules\module.py", line 1788, in _call_impl
return forward_call(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 2010, in __call__
result = self._torchdynamo_orig_backend(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 1760, in __call__
result = self._inner_convert(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 691, in __call__
result = _compile(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 1569, in _compile
guarded_code, tracer_output = compile_inner(code, one_graph, hooks)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_utils_internal.py", line 97, in wrapper_function
return function(*args, **kwargs)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 1251, in compile_inner
return _compile_inner(code, one_graph, hooks)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 1385, in _compile_inner
check_fn = dynamo_output.build_guards(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\convert_frame.py", line 860, in build_guards
return CheckFunctionManager(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\_dynamo\guards.py", line 3593, in __init__
raise AssertionError(f"Guard check failed: {reasons}")
AssertionError: Guard check failed: 0/0: ___check_obj_id(G['datetime'].now, 2702242757856) # current_time = datetime.now() # E:\DL_Compiler_Test\torch_code\test.py:6 in forward
```
### Versions
Collecting environment information...
PyTorch version: 2.10.0.dev20251005+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: Microsoft Windows 11
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 4.0.2
Libc version: N/A
Python version: 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.26100-SP0
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @amjames @Lucaskabela
Contributor guide
Assessment
This issue has not been assessed yet.