operations unit tests

This commit is contained in:
lucasdelimanogueira 2024-05-07 11:52:56 -03:00
parent ef3868066e
commit 2c2569d482
18 changed files with 239 additions and 6866 deletions

1
norch/utils/__init__.py Normal file
View file

@ -0,0 +1 @@
from .utils import *

View file

@ -1,5 +1,5 @@
import random
import numpy as np
import torch
def generate_random_list(shape):
@ -14,4 +14,27 @@ def generate_random_list(shape):
return [random.uniform(-1, 1) for _ in range(shape[0])]
else:
return [generate_random_list(inner_shape) for _ in range(shape[0])]
def to_torch(custom_tensor):
shape = custom_tensor.shape
pytorch_tensor = torch.zeros(shape)
def _iterate_indices(shape):
if len(shape) == 0:
yield ()
else:
for index in range(shape[0]):
for sub_indices in _iterate_indices(shape[1:]):
yield (index,) + sub_indices
# Iterate over all elements using the custom tensor's __getitem__ method
for indices in _iterate_indices(shape):
value = custom_tensor[indices]
pytorch_tensor[tuple(indices)] = value
return pytorch_tensor
def compare_torch(tensor1, tensor2, epsilon=1e-5):
diff = torch.abs(tensor1 - tensor2)
return torch.all(diff < epsilon)