I am trying to implement F.linear function from scratch using Pytorch, but it returns a slightly different value from Pytorch library function. In my case, the difference is about 1e-5 to 1e-9. 1e-5 is a too-large error when implementing Neural Networks for vision tasks.
How can I simulate F.linear without the errors? The below code is what I have tried so far. It evaluates the error. I would really grateful if you help me solve the problem.
input = torch.randn(64,7,7, 120).to('cuda:0')
weight= torch.randn(360,120).to('cuda:0')
pytorch_output = F.linear(input, weight)
B,H,W,_ = input.shape
BHW = B*H*W
Cout,_ = weight.shape
input = input.flatten(end_dim = -2)
my_output = torch.empty(BHW,Cout).to('cuda:0')
for i in range(BHW):
u = input[i,:]*weight
my_output[i,:] = u.transpose(0,1).sum(0)
my_output = my_output.reshape(B,H,W,-1)
diff = my_output - pytorch_output
diff.max()