Skip to content

Instantly share code, notes, and snippets.

@davidberard98
Last active January 25, 2022 22:36
Show Gist options
  • Save davidberard98/52fbd8c707b928af0e38b9393eb0403d to your computer and use it in GitHub Desktop.
Save davidberard98/52fbd8c707b928af0e38b9393eb0403d to your computer and use it in GitHub Desktop.
pytorch uninitialized memory return values
import torch
from torch import tensor
for i in [0, 5]:
a = tensor([[-4, 3, 9, 7, 0, -6, 4, 0, -3, 2],
[-9, -7, -7, -2, 8, 4, -4, -4, -4, 4],
[-6, 8, -4, 5, -7, 5, -2, 9, -7, -1],
[-4, 4, -3, -1, 0, 2, 4, 6, -7, -1],
[-3, 5, -9, 4, -7, -9, -1, 2, -7, -6]], dtype=torch.long)
b = tensor([[-8, 2, -7, -9, -7],
[-8, 6, 3, 8, -4],
[ 9, -5, 5, 4, -6],
[-8, 7, -1, -8, 9],
[-5, -9, -3, 4, 2],
[ 9, -5, 2, 0, -9],
[ 2, 9, -5, 1, 3],
[-6, 2, -5, -7, 2],
[-4, -2, 2, -6, 0],
[ 1, -3, -3, -8, -1]], dtype=torch.long)
a0 = a.size(dim=0)
a1 = a.size(dim=1)
b0 = b.size(dim=0)
b1 = b.size(dim=1)
a = a[:a0-i, :a1-i]
b = b[:b0-i, :b1-i]
print('A:', a)
print('B:', b)
c = torch.linalg.multi_dot([b, a])
d = torch.linalg.multi_dot([b, a])
print('C:', c)
print('D:', d)
assert((c==d).all().item())
import torch
from torch import tensor
for i in [0, 5]:
a = tensor([[-4, 3, 9, 7, 0, -6, 4, 0, -3, 2],
[-9, -7, -7, -2, 8, 4, -4, -4, -4, 4],
[-6, 8, -4, 5, -7, 5, -2, 9, -7, -1],
[-4, 4, -3, -1, 0, 2, 4, 6, -7, -1],
[-3, 5, -9, 4, -7, -9, -1, 2, -7, -6]], dtype=torch.long)
b = tensor([[-8, 2, -7, -9, -7],
[-8, 6, 3, 8, -4],
[ 9, -5, 5, 4, -6],
[-8, 7, -1, -8, 9],
[-5, -9, -3, 4, 2],
[ 9, -5, 2, 0, -9],
[ 2, 9, -5, 1, 3],
[-6, 2, -5, -7, 2],
[-4, -2, 2, -6, 0],
[ 1, -3, -3, -8, -1]], dtype=torch.long)
a0 = a.size(dim=0)
a1 = a.size(dim=1)
b0 = b.size(dim=0)
b1 = b.size(dim=1)
a = a[:a0-i, :a1-i]
b = b[:b0-i, :b1-i]
print('A:', a)
print('B:', b)
c = torch.matmul(b, a)
d = torch.matmul(b, a)
print('C:', c)
print('D:', d)
assert((c==d).all().item())
import torch
from torch import tensor
dtype = torch.long
for i in [0, 4, 5]:
b = tensor([[-4, 3, 9, 7, 0, -6, 4, 0, -3, 2],
[-9, -7, -7, -2, 8, 4, -4, -4, -4, 4],
[-6, 8, -4, 5, -7, 5, -2, 9, -7, -1],
[-4, 4, -3, -1, 0, 2, 4, 6, -7, -1],
[-3, 5, -9, 4, -7, -9, -1, 2, -7, -6]], dtype=dtype)
a = tensor([[-8, 2, -7, -9, -7],
[-8, 6, 3, 8, -4],
[ 9, -5, 5, 4, -6],
[-8, 7, -1, -8, 9],
[-5, -9, -3, 4, 2],
[ 9, -5, 2, 0, -9],
[ 2, 9, -5, 1, 3],
[-6, 2, -5, -7, 2],
[-4, -2, 2, -6, 0],
[ 1, -3, -3, -8, -1]], dtype=dtype)
#add = torch.zeros((10-i, 10-i), dtype=dtype)
a0 = a.size(dim=0)
a1 = a.size(dim=1)
b0 = b.size(dim=0)
b1 = b.size(dim=1)
a = a[:a0-i, :a1-i]
b = b[:b0-i, :b1-i]
print('A:', a)
print('B:', b)
c = torch.Tensor.__rmatmul__(b, a)
d = torch.Tensor.__rmatmul__(b, a)
print('C:', c)
print('D:', d)
assert((c==d).all().item())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment