-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Open
Labels
module: inductormodule: numerical-stabilityProblems related to numerical stability of operationsProblems related to numerical stability of operationsoncall: cpu inductorCPU Inductor issues for Intel team to triageCPU Inductor issues for Intel team to triageoncall: pt2
Description
🐛 Describe the bug
When you divide by scalar you get diffrent results in torch.compile. One solution is to put the scalar into tensor with nn.Parameters to solve the issue.
import torch
from torch import nn
class Foo(nn.Module):
def __init__(
self,
use_parameter: bool
) -> None:
super().__init__()
self.b = 101
if use_parameter:
self.b = nn.Parameter(torch.Tensor([self.b]), requires_grad=False)
def forward(self, x: torch.Tensor) -> torch.Tensor:
# return x + self.b
# return x - self.b
return x / self.b
# return x * self.b
torch.manual_seed(42)
x = torch.rand((5, 5))
expected = Foo(False)(x)
models = [
Foo(False),
Foo(True),
torch.compile(Foo(False), fullgraph=True),
torch.compile(Foo(True), fullgraph=True),
]
for m in models:
print((m(x) - expected).sum())
Results i got are:
tensor(0.)
tensor(0.)
tensor(-2.6776e-09)
tensor(0.)
Versions
gpytorch==1.14
pytorch-lightning==2.5.2
torch==2.7.1
torchinfo==1.8.0
torchmetrics==1.3.1
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @chenyang78 @kadeng @muchulee8 @amjames @chauhang @aakhundov
Metadata
Metadata
Assignees
Labels
module: inductormodule: numerical-stabilityProblems related to numerical stability of operationsProblems related to numerical stability of operationsoncall: cpu inductorCPU Inductor issues for Intel team to triageCPU Inductor issues for Intel team to triageoncall: pt2