Hi,
For a regression problem with pytorch 2.1.2+cpu, my code works fine as long as I do not use torch.compile
With torch.compile
, I get the following error message
outputs = model(x_train)
^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/eval_frame.py", line 328, in _fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/eval_frame.py", line 490, in catch_errors
return callback(frame, cache_entry, hooks, frame_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 641, in _convert_frame
result = inner_convert(frame, cache_size, hooks, frame_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 133, in _fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 389, in _convert_frame_assert
return _compile(
^^^^^^^^^
File "/usr/lib64/python3.11/site-packages/torch/_dynamo/convert_frame.py", line 429, in _compile
from torch.fx.experimental.validator import (
File "/usr/lib64/python3.11/site-packages/torch/fx/experimental/validator.py", line 58, in <module>
def z3str(e: z3.ExprRef) -> str:
^^^^^^^^^^
AttributeError: module 'z3' has no attribute 'ExprRef'
Here is the code to reproduce:
import torch
import torch.nn as nn
import numpy as np
from time import time
input_size = 1
output_size = 1
hidden_size = 500
num_data = 100000
# seeds
seed = 1234
torch.manual_seed(seed)
np.random.seed(seed)
# hyper-parameters
num_epochs = 50
learning_rate = 0.01
# toy dataset
x_train = np.random.rand(num_data,input_size)
y_train = np.cos(2*np.pi*x_train) + 0.1*np.random.randn(num_data,input_size)
# regression model
model = nn.Sequential(nn.Linear(input_size, hidden_size),
nn.GELU(),
nn.Linear(hidden_size, hidden_size),
nn.GELU(),
nn.Linear(hidden_size, output_size))
if 1:
model = torch.compile(model)
# loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.NAdam(model.parameters(), lr=learning_rate)
# train the model
x_train = torch.from_numpy(x_train.astype(np.float32))
y_train = torch.from_numpy(y_train.astype(np.float32))
for epoch in range(num_epochs):
start_time = time()
# forward pass
outputs = model(x_train)
loss = criterion(outputs, y_train)
# backward and optimize
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
print(f'Epoch {epoch}: Loss: {loss.item():.2e} in {time()-start_time:.1f}s.')
I thought, it has to do with my environment; I had installed pytorch with pip, so alternatively I installed it with conda, but the problem is still the same.
Thanks for any help.