From a072b57b50e7da7284a16d1d8c9ce6454064f04d Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 03:19:49 -0300 Subject: [PATCH 01/14] fix module send to cuda --- norch/nn/module.py | 2 -- tests/test_autograd.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/norch/nn/module.py b/norch/nn/module.py index eaabd9a..cebd86f 100644 --- a/norch/nn/module.py +++ b/norch/nn/module.py @@ -53,8 +53,6 @@ class Module(ABC): def to(self, device): for _, _, parameter in self.parameters(): parameter.to(device) - - return self def state_dict(self): state = OrderedDict() diff --git a/tests/test_autograd.py b/tests/test_autograd.py index 82e6b61..6793816 100644 --- a/tests/test_autograd.py +++ b/tests/test_autograd.py @@ -9,7 +9,7 @@ class TestTensorAutograd(unittest.TestCase): self.device = os.environ.get('device') if self.device is None or self.device != 'cuda': self.device = 'cpu' - + print(f"Running tests on: {self.device}") From 065e913b2df047349a6c170920e3f0051b07ee5b Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 09:46:35 -0300 Subject: [PATCH 02/14] fix send to device --- norch/nn/module.py | 2 ++ norch/tensor.py | 2 ++ tests/test_autograd.py | 7 ++----- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/norch/nn/module.py b/norch/nn/module.py index cebd86f..eaabd9a 100644 --- a/norch/nn/module.py +++ b/norch/nn/module.py @@ -53,6 +53,8 @@ class Module(ABC): def to(self, device): for _, _, parameter in self.parameters(): parameter.to(device) + + return self def state_dict(self): state = OrderedDict() diff --git a/norch/tensor.py b/norch/tensor.py index 8063cba..e660df9 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -52,6 +52,8 @@ class Tensor: self.ndim_ctype, self.device_ctype ) + + self.to(device) else: self.tensor = None, diff --git a/tests/test_autograd.py b/tests/test_autograd.py index 6793816..823ba1b 100644 --- a/tests/test_autograd.py +++ b/tests/test_autograd.py @@ -24,11 +24,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True, device=self.device) torch_result = (torch_tensor1 + torch_tensor2).sum() torch_result.backward() From d7a932f8edf95507083c8b39e294b608825b87d5 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 09:48:16 -0300 Subject: [PATCH 03/14] fix send to device --- norch/tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/norch/tensor.py b/norch/tensor.py index e660df9..8f92669 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -53,7 +53,7 @@ class Tensor: self.device_ctype ) - self.to(device) + self = self.to(device) else: self.tensor = None, From b1c28c4ceaa9296bb658513353600ac45f1cdf7c Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 10:00:48 -0300 Subject: [PATCH 04/14] fix send to device --- norch/tensor.py | 2 - tests/test_autograd.py | 275 +++++++++++------------------------------ 2 files changed, 69 insertions(+), 208 deletions(-) diff --git a/norch/tensor.py b/norch/tensor.py index 8f92669..8063cba 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -52,8 +52,6 @@ class Tensor: self.ndim_ctype, self.device_ctype ) - - self = self.to(device) else: self.tensor = None, diff --git a/tests/test_autograd.py b/tests/test_autograd.py index 823ba1b..9065471 100644 --- a/tests/test_autograd.py +++ b/tests/test_autograd.py @@ -9,7 +9,7 @@ class TestTensorAutograd(unittest.TestCase): self.device = os.environ.get('device') if self.device is None or self.device != 'cuda': self.device = 'cpu' - + print(f"Running tests on: {self.device}") @@ -26,7 +26,6 @@ class TestTensorAutograd(unittest.TestCase): torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True, device=self.device) - torch_result = (torch_tensor1 + torch_tensor2).sum() torch_result.backward() torch_tensor1_grad = torch_tensor1.grad @@ -47,11 +46,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True, device=self.device) torch_result = (torch_tensor1 + torch_tensor2).sum(axis=0).sum(axis=0).sum() torch_result.backward() @@ -69,11 +65,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True, device=self.device) torch_result = (torch_tensor1 + torch_tensor2).sum(axis=1).sum() torch_result.backward() @@ -94,9 +87,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True, device=self.device) torch_result = torch_tensor.max() torch_result.backward() @@ -115,9 +106,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True, device=self.device) torch_max_axis, _ = torch_tensor.max(axis=1) torch_result = torch_max_axis.sum() @@ -133,9 +122,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 1], [-4, 0]], [[5., 50], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 1], [-4, 0]], [[5., 50], [7, 8]]], requires_grad=True, device=self.device) torch_max_axis, _ = torch_tensor.max(axis=2) torch_result = torch_max_axis.sum() @@ -199,9 +186,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True, device=self.device) torch_result = torch_tensor.min() torch_result.backward() @@ -220,9 +205,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True, device=self.device) torch_min, _ = torch_tensor.min(axis=1) torch_result = torch_min.sum() @@ -238,9 +221,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 1], [-4, 0]], [[5., 50], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) + torch_tensor = torch.tensor([[[10, 1], [-4, 0]], [[5., 50], [7, 8]]], requires_grad=True, device=self.device) torch_min, _ = torch_tensor.min(axis=2) torch_result = torch_min.sum() @@ -261,12 +242,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True) # Shape (1, 2, 3) - torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True) # Shape (3) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) - + torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True, device=self.device) # Shape (1, 2, 3) + torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True, device=self.device) # Shape (3) torch_result = (torch_tensor1 + torch_tensor2).sum() torch_result.backward() torch_tensor1_grad = torch_tensor1.grad @@ -284,11 +261,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True) # Shape (1, 2, 3) - torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True) # Shape (3) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True, device=self.device) # Shape (1, 2, 3) + torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True, device=self.device) # Shape (3) torch_result = (torch_tensor2 + torch_tensor1).sum() torch_result.backward() @@ -309,12 +283,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad_sub = utils.to_torch(norch_tensor1_sub.grad).to(self.device) norch_tensor2_grad_sub = utils.to_torch(norch_tensor2_sub.grad).to(self.device) - torch_tensor1_sub = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - torch_tensor2_sub = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True) - - torch_tensor1_sub.to(self.device) - torch_tensor2_sub.to(self.device) - + torch_tensor1_sub = torch.tensor([[[1, 2.5], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2_sub = torch.tensor([[[1, 1.], [1, 1.9]], [[1, 1], [1, 1]]], requires_grad=True, device=self.device) torch_result_sub = (torch_tensor1_sub - torch_tensor2_sub).sum() torch_result_sub.backward() torch_tensor1_grad_sub = torch_tensor1_sub.grad @@ -334,12 +304,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True) # Shape (1, 2, 3) - torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True) # Shape (3) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) - + torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True, device=self.device) # Shape (1, 2, 3) + torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True, device=self.device) # Shape (3) torch_result = (torch_tensor1 - torch_tensor2).sum() torch_result.backward() torch_tensor1_grad = torch_tensor1.grad @@ -357,11 +323,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor2_grad = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True) # Shape (1, 2, 3) - torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True) # Shape (3) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1., 2, 3], [4, 5, 6]]], requires_grad=True, device=self.device) # Shape (1, 2, 3) + torch_tensor2 = torch.tensor([1.5, -1, 0], requires_grad=True, device=self.device) # Shape (3) torch_result = (torch_tensor2 - torch_tensor1).sum() torch_result.backward() @@ -383,12 +346,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad_div = utils.to_torch(norch_tensor1_div.grad).to(self.device) norch_tensor2_grad_div = utils.to_torch(norch_tensor2_div.grad).to(self.device) - torch_tensor1_div = torch.tensor([[[2, 5.1], [6, -8]], [[10, 12], [14, 16]]], requires_grad=True) - torch_tensor2_div = torch.tensor([[[1, 1], [2, 2.2]], [[3, 3], [4, 4]]], requires_grad=True) - - torch_tensor1_div.to(self.device) - torch_tensor2_div.to(self.device) - + torch_tensor1_div = torch.tensor([[[2, 5.1], [6, -8]], [[10, 12], [14, 16]]], requires_grad=True, device=self.device) + torch_tensor2_div = torch.tensor([[[1, 1], [2, 2.2]], [[3, 3], [4, 4]]], requires_grad=True, device=self.device) torch_result_div = (torch_tensor1_div / torch_tensor2_div).sum() torch_result_div.backward() torch_tensor1_grad_div = torch_tensor1_div.grad @@ -408,10 +367,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_div_scalar.backward() norch_tensor_grad_div_scalar = utils.to_torch(norch_tensor_div_scalar.grad).to(self.device) - torch_tensor_div_scalar = torch.tensor([[[2, 4.7], [6, 8]], [[10, 12], [14, 16]]], requires_grad=True) - - torch_tensor_div_scalar.to(self.device) - + torch_tensor_div_scalar = torch.tensor([[[2, 4.7], [6, 8]], [[10, 12], [14, 16]]], requires_grad=True, device=self.device) torch_result_div_scalar = (torch_tensor_div_scalar / scalar).sum() torch_result_div_scalar.backward() torch_tensor_grad_div_scalar = torch_tensor_div_scalar.grad @@ -429,10 +385,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_scalar_div.backward() norch_tensor_grad_scalar_div = utils.to_torch(norch_tensor_scalar_div.grad).to(self.device) - torch_tensor_scalar_div = torch.tensor([[[1, 2.23], [3, 4]], [[5, 6], [7, 8]]], requires_grad=True) - - torch_tensor_scalar_div.to(self.device) - + torch_tensor_scalar_div = torch.tensor([[[1, 2.23], [3, 4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_result_scalar_div = (scalar / torch_tensor_scalar_div).sum() torch_result_scalar_div.backward() torch_tensor_grad_scalar_div = torch_tensor_scalar_div.grad @@ -450,10 +403,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_power_st.backward() norch_tensor_grad_power_st = utils.to_torch(norch_tensor_power_st.grad).to(self.device) - torch_tensor_power_st = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True) - - torch_tensor_power_st.to(self.device) - + torch_tensor_power_st = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_result_power_st = (scalar ** torch_tensor_power_st).sum() torch_result_power_st.backward() torch_tensor_grad_power_st = torch_tensor_power_st.grad @@ -470,10 +420,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_power_ts.backward() norch_tensor_grad_power_ts = utils.to_torch(norch_tensor_power_ts.grad).to(self.device) - torch_tensor_power_ts = torch.tensor([[[2, 3], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True) - - torch_tensor_power_ts.to(self.device) - + torch_tensor_power_ts = torch.tensor([[[2, 3], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_result_power_ts = (torch_tensor_power_ts ** scalar).sum() torch_result_power_ts.backward() torch_tensor_grad_power_ts = torch_tensor_power_ts.grad @@ -491,12 +438,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad_matmul = utils.to_torch(norch_tensor1_matmul.grad).to(self.device) norch_tensor2_grad_matmul = utils.to_torch(norch_tensor2_matmul.grad).to(self.device) - torch_tensor1_matmul = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - torch_tensor2_matmul = torch.tensor([[[1.1, 3], [4, 5]], [[6, 7], [8, 9]]], requires_grad=True) - - torch_tensor1_matmul.to(self.device) - torch_tensor2_matmul.to(self.device) - + torch_tensor1_matmul = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2_matmul = torch.tensor([[[1.1, 3], [4, 5]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_result_matmul = (torch_tensor1_matmul @ torch_tensor2_matmul).sum() torch_result_matmul.backward() torch_tensor1_grad_matmul = torch_tensor1_matmul.grad @@ -523,12 +466,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor2_grad_matmul = utils.to_torch(norch_tensor2_matmul.grad).to(self.device) # Repeat the same process with torch tensors - torch_tensor1_matmul = torch.tensor([[[1., 2], [3, -4], [5, 6], [7, 8]] for _ in range(B)], requires_grad=True) - torch_tensor2_matmul = torch.tensor([[[2., 3, 1, 0, 4], [5, -1, 2, 3, 0]] for _ in range(B)], requires_grad=True) - - torch_tensor1_matmul.to(self.device) - torch_tensor2_matmul.to(self.device) - + torch_tensor1_matmul = torch.tensor([[[1., 2], [3, -4], [5, 6], [7, 8]] for _ in range(B)], requires_grad=True, device=self.device) + torch_tensor2_matmul = torch.tensor([[[2., 3, 1, 0, 4], [5, -1, 2, 3, 0]] for _ in range(B)], requires_grad=True, device=self.device) torch_result_matmul = torch.matmul(torch_tensor1_matmul, torch_tensor2_matmul) torch_result_matmul_sum = torch_result_matmul.sum() torch_result_matmul_sum.backward() @@ -559,12 +498,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor2_grad_matmul = utils.to_torch(norch_tensor2_matmul.grad).to(self.device) # Repeat the same process with torch tensors - torch_tensor1_matmul = torch.tensor([[1., 2], [3, -4], [5, 6], [7, 8]], requires_grad=True) - torch_tensor2_matmul = torch.tensor([[[2., 3, 1, 0, 4], [5, -1, 2, 3, 0]] for _ in range(B)], requires_grad=True) - - torch_tensor1_matmul.to(self.device) - torch_tensor2_matmul.to(self.device) - + torch_tensor1_matmul = torch.tensor([[1., 2], [3, -4], [5, 6], [7, 8]], requires_grad=True, device=self.device) + torch_tensor2_matmul = torch.tensor([[[2., 3, 1, 0, 4], [5, -1, 2, 3, 0]] for _ in range(B)], requires_grad=True, device=self.device) torch_result_matmul = torch.matmul(torch_tensor1_matmul, torch_tensor2_matmul) torch_result_matmul_sum = torch_result_matmul.sum() torch_result_matmul_sum.backward() @@ -588,10 +523,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_elemwise_mul_scalar.backward() norch_tensor_grad_elemwise_mul_scalar = utils.to_torch(norch_tensor_elemwise_mul_scalar.grad).to(self.device) - torch_tensor_elemwise_mul_scalar = torch.tensor([[[1.1, 2], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - - torch_tensor_elemwise_mul_scalar.to(self.device) - + torch_tensor_elemwise_mul_scalar = torch.tensor([[[1.1, 2], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_result_elemwise_mul_scalar = (scalar * torch_tensor_elemwise_mul_scalar).sum() torch_result_elemwise_mul_scalar.backward() torch_tensor_grad_elemwise_mul_scalar = torch_tensor_elemwise_mul_scalar.grad @@ -610,12 +542,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor1_grad_elemwise_mul = utils.to_torch(norch_tensor1_elemwise_mul.grad).to(self.device) norch_tensor2_grad_elemwise_mul = utils.to_torch(norch_tensor2_elemwise_mul.grad).to(self.device) - torch_tensor1_elemwise_mul = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True).to(self.device) - torch_tensor2_elemwise_mul = torch.tensor([[[1.1, 3], [4, 5]], [[6, 7], [8, 9]]], requires_grad=True).to(self.device) - - torch_tensor1_elemwise_mul.to(self.device) - torch_tensor2_elemwise_mul.to(self.device) - + torch_tensor1_elemwise_mul = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) + torch_tensor2_elemwise_mul = torch.tensor([[[1.1, 3], [4, 5]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_result_elemwise_mul = (torch_tensor1_elemwise_mul * torch_tensor2_elemwise_mul).sum() torch_result_elemwise_mul.backward() torch_tensor1_grad_elemwise_mul = torch_tensor1_elemwise_mul.grad @@ -633,10 +561,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_sin_tensor.backward() torch_result_sin_tensor_grad = utils.to_torch(norch_sin_tensor.grad).to(self.device) - torch_sin_tensor = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True) - - torch_sin_tensor.to(self.device) - + torch_sin_tensor = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_expected_sin_tensor = (torch.sin(torch_sin_tensor)).sum() torch_expected_sin_tensor.backward() torch_expected_sin_tensor_grad = torch_sin_tensor.grad @@ -652,10 +577,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_cos_tensor.backward() torch_result_cos_tensor_grad = utils.to_torch(norch_cos_tensor.grad).to(self.device) - torch_cos_tensor = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True) - - torch_cos_tensor.to(self.device) - + torch_cos_tensor = torch.tensor([[[2, 3.21], [4, 2.1]], [[6, 7], [8, 9]]], requires_grad=True, device=self.device) torch_expected_cos_tensor = (torch.sin(torch_cos_tensor)).sum() torch_expected_cos_tensor.backward() torch_expected_cos_tensor_grad = torch_cos_tensor.grad @@ -673,10 +595,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result.backward() norch_tensor_grad = utils.to_torch(norch_tensor.grad).to(self.device) - torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True) - - torch_tensor.to(self.device) - + torch_tensor = torch.tensor([[[10, 10], [-4, -4]], [[5., 6], [7, 8]]], requires_grad=True, device=self.device) torch_sigmoid = torch.sigmoid(torch_tensor) torch_result = torch_sigmoid.sum() @@ -698,12 +617,8 @@ class TestTensorAutograd(unittest.TestCase): loss_norch.backward() # Backpropagate the loss grad_norch = predictions_norch.grad - predictions_torch = torch.tensor([1.1, 2, 3, 4], requires_grad=True) - labels_torch = torch.tensor([4, 3, 2.1, 1]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) - + predictions_torch = torch.tensor([1.1, 2, 3, 4], requires_grad=True, device=self.device) + labels_torch = torch.tensor([4, 3, 2.1, 1], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) loss_torch_expected.backward() # Backpropagate the loss grad_torch_expected = predictions_torch.grad @@ -728,12 +643,8 @@ class TestTensorAutograd(unittest.TestCase): loss_norch.backward() # Backpropagate the loss grad_norch = predictions_norch.grad - predictions_torch = torch.tensor([2.0, 1.0, 0.1], requires_grad=True) - labels_torch = torch.tensor(0) - - predictions_torch.to(self.device) - labels_torch.to(self.device) - + predictions_torch = torch.tensor([2.0, 1.0, 0.1], requires_grad=True, device=self.device) + labels_torch = torch.tensor(0, device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) loss_torch_expected.backward() # Backpropagate the loss grad_torch_expected = predictions_torch.grad @@ -750,12 +661,8 @@ class TestTensorAutograd(unittest.TestCase): loss_norch.backward() # Backpropagate the loss grad_norch = predictions_norch.grad - predictions_torch = torch.tensor([[0.5, 1.5, 2.5], [1.0, 2.0, 3.0]], requires_grad=True) - labels_torch = torch.tensor([2, 1]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) - + predictions_torch = torch.tensor([[0.5, 1.5, 2.5], [1.0, 2.0, 3.0]], requires_grad=True, device=self.device) + labels_torch = torch.tensor([2, 1], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) loss_torch_expected.backward() # Backpropagate the loss grad_torch_expected = predictions_torch.grad @@ -772,12 +679,8 @@ class TestTensorAutograd(unittest.TestCase): loss_norch.backward() # Backpropagate the loss grad_norch = predictions_norch.grad - predictions_torch = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], requires_grad=True) - labels_torch = torch.tensor([1, 2]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) - + predictions_torch = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], requires_grad=True, device=self.device) + labels_torch = torch.tensor([1, 2], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) loss_torch_expected.backward() # Backpropagate the loss grad_torch_expected = predictions_torch.grad @@ -794,12 +697,8 @@ class TestTensorAutograd(unittest.TestCase): loss_norch.backward() # Backpropagate the loss grad_norch = predictions_norch.grad - predictions_torch = torch.tensor([[0.5, 0.2, 0.1], [0.1, 0.5, 0.7]], requires_grad=True) - labels_torch = torch.tensor([[1., 0, 0], [0, 1, 0]]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) - + predictions_torch = torch.tensor([[0.5, 0.2, 0.1], [0.1, 0.5, 0.7]], requires_grad=True, device=self.device) + labels_torch = torch.tensor([[1., 0, 0], [0, 1, 0]], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) loss_torch_expected.backward() # Backpropagate the loss grad_torch_expected = predictions_torch.grad @@ -858,10 +757,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_reshape.backward() norch_tensor_grad_reshape = utils.to_torch(norch_tensor_reshape.grad).to(self.device) - torch_tensor_reshape = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - - torch_tensor_reshape.to(self.device) - + torch_tensor_reshape = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_result_reshape = torch_tensor_reshape.reshape(new_shape).sum() torch_result_reshape.backward() torch_tensor_grad_reshape = torch_tensor_reshape.grad @@ -879,10 +775,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_transpose.backward() norch_tensor_grad_transpose = utils.to_torch(norch_tensor_transpose.grad).to(self.device) - torch_tensor_transpose = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - - torch_tensor_transpose.to(self.device) - + torch_tensor_transpose = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_result_transpose = torch_tensor_transpose.transpose(axis1, axis2).sum() torch_result_transpose.backward() torch_tensor_grad_transpose = torch_tensor_transpose.grad @@ -899,10 +792,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_T.backward() norch_tensor_grad_T = utils.to_torch(norch_tensor_T.grad).to(self.device) - torch_tensor_T = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True) - - torch_tensor_T.to(self.device) - + torch_tensor_T = torch.tensor([[[1, 2.1], [3, -4]], [[5, 6], [7, 8]]], requires_grad=True, device=self.device) torch_result_T = torch_tensor_T.mT.sum() torch_result_T.backward() torch_tensor_grad_T = torch_tensor_T.grad @@ -923,11 +813,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor_grad_reshape_matmul1 = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor_grad_reshape_matmul2 = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) torch_result_reshape_matmul = (torch_tensor1.reshape(new_shape) @ torch_tensor2).sum() torch_result_reshape_matmul.backward() @@ -948,10 +835,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_unsqueeze_0.backward() norch_tensor_grad_unsqueeze_0 = utils.to_torch(norch_tensor_unsqueeze.grad).to(self.device) - torch_tensor_unsqueeze = torch.tensor([[1., 2], [3, 4]], requires_grad=True) - - torch_tensor_unsqueeze.to(self.device) - + torch_tensor_unsqueeze = torch.tensor([[1., 2], [3, 4]], requires_grad=True, device=self.device) torch_result_unsqueeze_0 = torch_tensor_unsqueeze.unsqueeze(0).sum() torch_result_unsqueeze_0.backward() torch_tensor_grad_unsqueeze_0 = torch_tensor_unsqueeze.grad @@ -964,10 +848,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_unsqueeze_1.backward() norch_tensor_grad_unsqueeze_1 = utils.to_torch(norch_tensor_unsqueeze.grad).to(self.device) - torch_tensor_unsqueeze = torch.tensor([[1., 2.], [3, 4]], requires_grad=True) - - torch_tensor_unsqueeze.to(self.device) - + torch_tensor_unsqueeze = torch.tensor([[1., 2.], [3, 4]], requires_grad=True, device=self.device) torch_result_unsqueeze_1 = torch_tensor_unsqueeze.unsqueeze(1).sum() torch_result_unsqueeze_1.backward() torch_tensor_grad_unsqueeze_1 = torch_tensor_unsqueeze.grad @@ -980,10 +861,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_unsqueeze_2.backward() norch_tensor_grad_unsqueeze_2 = utils.to_torch(norch_tensor_unsqueeze.grad).to(self.device) - torch_tensor_unsqueeze = torch.tensor([[1., 2], [3, 4]], requires_grad=True) - - torch_tensor_unsqueeze.to(self.device) - + torch_tensor_unsqueeze = torch.tensor([[1., 2], [3, 4]], requires_grad=True, device=self.device) torch_result_unsqueeze_2 = torch_tensor_unsqueeze.unsqueeze(2).sum() torch_result_unsqueeze_2.backward() torch_tensor_grad_unsqueeze_2 = torch_tensor_unsqueeze.grad @@ -1003,11 +881,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor_grad_unsqueeze_matmul1 = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor_grad_unsqueeze_matmul2 = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, requires_grad=True) - torch_tensor2 = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, requires_grad=True, device=self.device) torch_result_unsqueeze_matmul = (torch_tensor1 @ torch_tensor2.unsqueeze(0)).sum() torch_result_unsqueeze_matmul.backward() @@ -1027,10 +902,7 @@ class TestTensorAutograd(unittest.TestCase): norch_result_squeeze_0.backward() norch_tensor_grad_squeeze_0 = utils.to_torch(norch_tensor_squeeze.grad).to(self.device) - torch_tensor_squeeze = torch.tensor([[[1., 2], [3, 4]]], requires_grad=True) - - torch_tensor_squeeze.to(self.device) - + torch_tensor_squeeze = torch.tensor([[[1., 2], [3, 4]]], requires_grad=True, device=self.device) torch_result_squeeze_0 = torch_tensor_squeeze.squeeze(0).sum() torch_result_squeeze_0.backward() torch_tensor_grad_squeeze_0 = torch_tensor_squeeze.grad @@ -1047,14 +919,11 @@ class TestTensorAutograd(unittest.TestCase): # Squeeze at dim=0 then matmul norch_result_squeeze_matmul = (norch_tensor1.squeeze(0) @ norch_tensor2).sum() norch_result_squeeze_matmul.backward() - norch_tensor_grad_squeeze_matmul1 = utils.to_torch(norch_tensor1.grad).to(self.device) - norch_tensor_grad_squeeze_matmul2 = utils.to_torch(norch_tensor2.grad).to(self.device) + norch_tensor_grad_squeeze_matmul1 = utils.to_torch(norch_tensor1.grad, device=self.device) + norch_tensor_grad_squeeze_matmul2 = utils.to_torch(norch_tensor2.grad, device=self.device) - torch_tensor1 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True) - torch_tensor2 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True).to(self.device) + torch_tensor2 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True).to(self.device) torch_result_squeeze_matmul = (torch_tensor1.squeeze(0) @ torch_tensor2).sum() torch_result_squeeze_matmul.backward() @@ -1077,11 +946,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor_grad_T_matmul1 = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor_grad_T_matmult2 = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) torch_result_T_matmul = (torch_tensor1.T @ torch_tensor2).sum() torch_result_T_matmul.backward() @@ -1096,7 +962,7 @@ class TestTensorAutograd(unittest.TestCase): The code has a problem on the following operation tensor1.reshape(..) @ tensor1 print(tensor1.grad) - (also transpose and .T) + (also transpsoe and .T) """ pass @@ -1112,11 +978,8 @@ class TestTensorAutograd(unittest.TestCase): norch_tensor_grad_transpose_matmul1 = utils.to_torch(norch_tensor1.grad).to(self.device) norch_tensor_grad_transpose_matmult2 = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True) - - torch_tensor1.to(self.device) - torch_tensor2.to(self.device) + torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) torch_result_transpose_matmul = (torch_tensor1.T @ torch_tensor2).sum() torch_result_transpose_matmul.backward() From f13d22eb91f6dbd9d069cd42764daea22259e512 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 10:04:44 -0300 Subject: [PATCH 05/14] fix send to device unittest --- tests/test_autograd.py | 2 +- tests/test_nn.py | 65 ++++++++++++------------------------------ 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/tests/test_autograd.py b/tests/test_autograd.py index 9065471..4de2838 100644 --- a/tests/test_autograd.py +++ b/tests/test_autograd.py @@ -980,7 +980,7 @@ class TestTensorAutograd(unittest.TestCase): torch_tensor1 = torch.tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) torch_tensor2 = torch.tensor([[1, 5.1], [0.1, -4], [0, 6], [7, 8]], dtype=torch.float32, requires_grad=True, device=self.device) - + torch_result_transpose_matmul = (torch_tensor1.T @ torch_tensor2).sum() torch_result_transpose_matmul.backward() torch_tensor_grad_transpose_matmul1 = torch_tensor1.grad diff --git a/tests/test_nn.py b/tests/test_nn.py index 4896506..532f5b6 100644 --- a/tests/test_nn.py +++ b/tests/test_nn.py @@ -26,11 +26,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_norch = loss_fn_norch.forward(predictions_norch, labels_norch) loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([[1.1, 2, 3, 4], [1.1, 2, 3, 4]]) - labels_torch = torch.tensor([[1.1, 2, 3, 4], [1.1, 2, 3, 3]]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([[1.1, 2, 3, 4], [1.1, 2, 3, 4]], device=self.device) + labels_torch = torch.tensor([[1.1, 2, 3, 4], [1.1, 2, 3, 3]], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -42,11 +39,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_norch = loss_fn_norch.forward(predictions_norch, labels_norch) loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([1.1, 2, 3, 4]) - labels_torch = torch.tensor([4, 3, 2.1, 1]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([1.1, 2, 3, 4], device=self.device) + labels_torch = torch.tensor([4, 3, 2.1, 1], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -66,11 +60,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([2.0, 1.0, 0.1]) - labels_torch = torch.tensor(0) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([2.0, 1.0, 0.1], device=self.device) + labels_torch = torch.tensor(0, device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -83,11 +74,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([[0.5, 1.5, 2.5], [1.0, 2.0, 3.0]]) - labels_torch = torch.tensor([2, 1]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([[0.5, 1.5, 2.5], [1.0, 2.0, 3.0]], device=self.device) + labels_torch = torch.tensor([2, 1], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -99,11 +87,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_norch = loss_fn_norch.forward(predictions_norch, labels_norch) loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) - labels_torch = torch.tensor([1, 2]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=self.device) + labels_torch = torch.tensor([1, 2], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -131,11 +116,8 @@ class TestNNModuleLoss(unittest.TestCase): loss_norch = loss_fn_norch.forward(predictions_norch, labels_norch) loss_torch_result = utils.to_torch(loss_norch).to(self.device) - predictions_torch = torch.tensor([[0.5, 0.2, 0.1], [0.1, 0.5, 0.7]]) - labels_torch = torch.tensor([[1., 0, 0], [0, 1, 0]]) - - predictions_torch.to(self.device) - labels_torch.to(self.device) + predictions_torch = torch.tensor([[0.5, 0.2, 0.1], [0.1, 0.5, 0.7]], device=self.device) + labels_torch = torch.tensor([[1., 0, 0], [0, 1, 0]], device=self.device) loss_torch_expected = loss_fn_torch(predictions_torch, labels_torch) @@ -161,9 +143,7 @@ class TestNNModuleActivationFn(unittest.TestCase): sigmoid_norch = sigmoid_fn_norch.forward(x) sigmoid_torch_result = utils.to_torch(sigmoid_norch).to(self.device) - x = torch.tensor([[1, 2, 3]]) - - x.to(self.device) + x = torch.tensor([[1, 2, 3]], device=self.device) sigmoid_torch_expected = sigmoid_fn_torch.forward(x) @@ -174,9 +154,7 @@ class TestNNModuleActivationFn(unittest.TestCase): sigmoid_norch = sigmoid_fn_norch.forward(x) sigmoid_torch_result = utils.to_torch(sigmoid_norch).to(self.device) - x = torch.tensor([-1, 2, -3]) - - x.to(self.device) + x = torch.tensor([-1, 2, -3], device=self.device) sigmoid_torch_expected = sigmoid_fn_torch.forward(x) @@ -187,9 +165,7 @@ class TestNNModuleActivationFn(unittest.TestCase): sigmoid_norch = sigmoid_fn_norch.forward(x) sigmoid_torch_result = utils.to_torch(sigmoid_norch).to(self.device) - x = torch.tensor([0, 0, 0]) - - x.to(self.device) + x = torch.tensor([0, 0, 0], device=self.device) sigmoid_torch_expected = sigmoid_fn_torch.forward(x) @@ -205,9 +181,9 @@ class TestNNModuleActivationFn(unittest.TestCase): # Define the input tensors for different test cases test_cases = [ - (norch.Tensor([[[1., 2, 3], [4, 5, 6]]]), torch.tensor([[[1., 2, 3], [4, 5, 6]]])), - (norch.Tensor([[[1., -1, 0], [2, -2, 0]]]), torch.tensor([[[1., -1, 0], [2, -2, 0]]])), - (norch.Tensor([[[0., 0, 0], [0, 0, 0]]]), torch.tensor([[[0., 0, 0], [0, 0, 0]]])) + (norch.Tensor([[[1., 2, 3], [4, 5, 6]]]).to(self.device), torch.tensor([[[1., 2, 3], [4, 5, 6]]], device=self.device)), + (norch.Tensor([[[1., -1, 0], [2, -2, 0]]]).to(self.device), torch.tensor([[[1., -1, 0], [2, -2, 0]]], device=self.device)), + (norch.Tensor([[[0., 0, 0], [0, 0, 0]]]).to(self.device), torch.tensor([[[0., 0, 0], [0, 0, 0]]], device=self.device)) ] for dim in axes: @@ -215,12 +191,7 @@ class TestNNModuleActivationFn(unittest.TestCase): softmax_fn_torch = torch.nn.Softmax(dim=dim) for norch_input, torch_input in test_cases: - # Move tensors to the correct device - norch_input = norch_input.to(self.device) - torch_input = torch_input - torch_input.to(self.device) - # Forward pass using norch softmax_norch = softmax_fn_norch.forward(norch_input) softmax_torch_result = utils.to_torch(softmax_norch).to(self.device) From 8b19ac38c961459800a4481dd98ebf6021e9b48f Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 10:20:08 -0300 Subject: [PATCH 06/14] fix send module to device --- norch/__pycache__/tensor.cpython-38.pyc | Bin 20124 -> 20124 bytes norch/nn/__pycache__/module.cpython-38.pyc | Bin 4054 -> 4082 bytes norch/nn/module.py | 6 +++-- test.py | 28 ++++++++++++++++++--- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index bbd6f48ebcc6ade5e2722e10123f75a02051b96a..445c3e3f4f91daca4b623e4ac36c3ddc8825bcd7 100644 GIT binary patch delta 86 zcmbO;mvPQqM($8vUM>b8SZnRSk=xClF=}&=y$zq<6rfO1CWx2{BBp_e=^$bTh?of^ bZgCV8m*|N55zQn?4uN delta 86 zcmbO;mvPQqM($8vUM>b8C@%He$n9p&cx!Wzy$zqlYgN55zQyE_1MU@uts zK67Jbgch`kN+C6a&fVLAXvD!UH$CpQ!zljap+zyZQV0bP{b!I`{qD6^*o>?A(0C3b z=$lQGe_PBD@UdB;+8eV73;5Z53up0}wLO2Fjn(z2+Yu)y{p}^HJa&L_lJW0hQZs7w zg1%K9uiI@{#t-%nsNuV;Zx>0QDz}17F@fK*qi`9s&I9cXRU7!k*#aMPuCJftT?;tt z`q08vcg8zLg))da4uhhO=vFS~STx8OW$a`0&%+*Kl2Kyt-HL;Zql{CG1xAf=p27ZN t5%+l2e8$iX!%{8HPz>8pgifupEvB*MP3C4;js*Y! delta 637 zcmXw$&ui3B5XUq5@tPl-L|v(ET^rr9?WQisQvBJizY8t2lqwYuimuxfx2;Wmi3`FC z1;v}E0X>QB(SwS3@!~(>!9&19PwJ(D7r|3!QcTEaX7axCX5O1W!MDI$a9vxWXXX9b zr5)#^H=sfTLv>hNietQ?K8FmytEuQEi7eu_QT(*5*&zBjKFl$`&|i_LVw9mDr;HKe zy72=lcvHM2|F;-}Dh~BDX3CV(RW_9^<*IV~;4STmO0U+{HlYh!0LnwNt6W#)ev*Z% z>?f0{X&ma(WmhalopvjdAq8?2>waXly;CJ}DMu%hS<%{~2a#;1 z(cX3kCZdXZus0) z61NM1Qz2QVtwfLIa9(22wO|3O?uIc$YKPrekW7hY&h6nhn1)FwVNmjDW#Y$_3KP`~B)4-32-ok+}c> diff --git a/norch/nn/module.py b/norch/nn/module.py index eaabd9a..0135bb6 100644 --- a/norch/nn/module.py +++ b/norch/nn/module.py @@ -51,8 +51,10 @@ class Module(ABC): parameter.zero_grad() def to(self, device): - for _, _, parameter in self.parameters(): - parameter.to(device) + for module, name, _ in self.parameters(): + parameter = getattr(module, name) + parameter = parameter.to(device) + setattr(module, name, parameter) return self diff --git a/test.py b/test.py index 94f5195..9297870 100644 --- a/test.py +++ b/test.py @@ -1,7 +1,27 @@ import norch -from norch.utils import utils_unittests as utils +import norch.nn as nn +import norch.optim as optim +from norch.utils.data.dataloader import Dataloader +from norch.norchvision import transforms +import numpy as np +import matplotlib.pyplot as plt +import random +random.seed(1) -device = "cpu" +class MyModel(nn.Module): + def __init__(self): + super(MyModel, self).__init__() + self.fc1 = nn.Linear(784, 30) + self.sigmoid1 = nn.Sigmoid() + self.fc2 = nn.Linear(30, 10) + self.sigmoid2 = nn.Sigmoid() -norch_tensor = norch.Tensor([[[[1, 2], [3, -4]], [[5, 6], [7, 8]]], [[[1, 2], [3, -4]], [[5, 6], [7, 8]]]]).to(device) -norch_result = norch_tensor.sum(axis=0) + def forward(self, x): + out = self.fc1(x) + out = self.sigmoid1(out) + out = self.fc2(out) + out = self.sigmoid2(out) + + return out + +model = MyModel().to("cuda") \ No newline at end of file From e8e00609550deb34d62ce0b163193e461d8c5206 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 10:35:55 -0300 Subject: [PATCH 07/14] fix cross entropy loss send to device --- norch/nn/loss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/norch/nn/loss.py b/norch/nn/loss.py index 124da6f..d183e8f 100644 --- a/norch/nn/loss.py +++ b/norch/nn/loss.py @@ -44,7 +44,7 @@ class CrossEntropyLoss(Loss): if input.ndim == 1: if target.numel == 1: num_classes = input.shape[0] - target = norch.one_hot_encode(target, num_classes) + target = norch.one_hot_encode(target, num_classes).to(target.device) logits = norch.softmax(input, dim=0) target = target.reshape(logits.shape) From 472f3189c2c2d75fc87eb2d8c6f0edf7f227d7b2 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 10:41:52 -0300 Subject: [PATCH 08/14] fix cross entropy loss send to device --- norch/nn/loss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/norch/nn/loss.py b/norch/nn/loss.py index d183e8f..6c0abea 100644 --- a/norch/nn/loss.py +++ b/norch/nn/loss.py @@ -67,7 +67,7 @@ class CrossEntropyLoss(Loss): # target -> Ground truth class indices: num_classes = input.shape[1] - target = norch.one_hot_encode(target, num_classes) + target = norch.one_hot_encode(target, num_classes).to(target.device) batch_size = input.shape[0] logits = norch.softmax(input, dim=1) From 27feb8a366575413d009a9f92ae91b46f963ced0 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 12:07:17 -0300 Subject: [PATCH 09/14] fix autograd was not running on device --- norch/tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/norch/tensor.py b/norch/tensor.py index 8063cba..6861f14 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -205,7 +205,7 @@ class Tensor: if gradient is None: if self.shape == [1]: - gradient = Tensor([1]) + gradient = Tensor([1]).to(self.device) else: raise RuntimeError("Gradient argument must be specified for non-scalar tensors.") From 9b49a07d2a001b7ce65cd0b69c2ef3b962f36096 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 12:11:59 -0300 Subject: [PATCH 10/14] fix max min sum access elements autograd backward running on device --- norch/autograd/functions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/norch/autograd/functions.py b/norch/autograd/functions.py index 917bd94..477837d 100644 --- a/norch/autograd/functions.py +++ b/norch/autograd/functions.py @@ -136,7 +136,7 @@ class SumBackward: input_shape = self.input[0].shape.copy() if self.axis == -1: # If axis is None, sum reduces the tensor to a scalar. - grad_output = float(gradient.tensor.contents.data[0]) * self.input[0].ones_like() + grad_output = float(gradient[[0] * len(gradient.shape)]) * self.input[0].ones_like() else: if self.keepdim: @@ -214,9 +214,9 @@ class MaxBackward: max_value = self.input[0].max() mask = self.input[0].equal(max_value) - grad_output = float(gradient.tensor.contents.data[0]) * self.input[0].ones_like() + grad_output = float(gradient[[0] * len(gradient.shape)]) * self.input[0].ones_like() - grad_output = (grad_output * mask) / mask.sum().tensor.contents.data[0] + grad_output = (grad_output * mask) / mask.sum()[0] else: @@ -250,9 +250,9 @@ class MinBackward: min_value = self.input[0].min() mask = self.input[0].equal(min_value) - grad_output = float(gradient.tensor.contents.data[0]) * self.input[0].ones_like() + grad_output = float(gradient[[0] * len(gradient.shape)]) * self.input[0].ones_like() - grad_output = (grad_output * mask) / mask.sum().tensor.contents.data[0] + grad_output = (grad_output * mask) / mask.sum()[0] else: if self.keepdim: From 4676a70d14ad8e657f70bfd570f943c5f0d16c02 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 12:16:07 -0300 Subject: [PATCH 11/14] fix max min sum access elements autograd backward running on device --- tests/test_autograd.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_autograd.py b/tests/test_autograd.py index 4de2838..3be772c 100644 --- a/tests/test_autograd.py +++ b/tests/test_autograd.py @@ -919,11 +919,11 @@ class TestTensorAutograd(unittest.TestCase): # Squeeze at dim=0 then matmul norch_result_squeeze_matmul = (norch_tensor1.squeeze(0) @ norch_tensor2).sum() norch_result_squeeze_matmul.backward() - norch_tensor_grad_squeeze_matmul1 = utils.to_torch(norch_tensor1.grad, device=self.device) - norch_tensor_grad_squeeze_matmul2 = utils.to_torch(norch_tensor2.grad, device=self.device) + norch_tensor_grad_squeeze_matmul1 = utils.to_torch(norch_tensor1.grad,).to(self.device) + norch_tensor_grad_squeeze_matmul2 = utils.to_torch(norch_tensor2.grad).to(self.device) - torch_tensor1 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True).to(self.device) - torch_tensor2 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True).to(self.device) + torch_tensor1 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True, device=self.device) + torch_tensor2 = torch.tensor([[[1., 2], [3, 4]]], dtype=torch.float32, requires_grad=True, device=self.device) torch_result_squeeze_matmul = (torch_tensor1.squeeze(0) @ torch_tensor2).sum() torch_result_squeeze_matmul.backward() From 77cabe41a262716514eaf607f948f71e0f314745 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 12:19:32 -0300 Subject: [PATCH 12/14] fix cross entropy loss cuda --- norch/__pycache__/tensor.cpython-38.pyc | Bin 20124 -> 20142 bytes .../__pycache__/functions.cpython-38.pyc | Bin 10740 -> 10707 bytes norch/nn/__pycache__/loss.cpython-38.pyc | Bin 2846 -> 2874 bytes norch/nn/__pycache__/module.cpython-38.pyc | Bin 4082 -> 4112 bytes norch/nn/functional.py | 6 ++++-- 5 files changed, 4 insertions(+), 2 deletions(-) diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 445c3e3f4f91daca4b623e4ac36c3ddc8825bcd7..882aabd3782323504872aba6c1220c6316c6dff2 100644 GIT binary patch delta 975 zcmYk4OH30{6o%*AP9L;X9&Lj(LIYwdf-!&ug9~URV#=e4l1NYyGk|S*w3m^Ww*e_6 ziW0%AfI_k8N=%Gqma@=|u1#E!l8A{b6L-2WuDoYN!z{iz^PlHEb5|4aG6D8ByWJ{@ zr@150GydBCHhW@Ad@(5`4OtcGoWwwp!WLRB6i}cS!i1Kq1g$tE4MLUIAqE2$)u@m- zOaT)aJ){?6L^Z{wn1K=ciIJpvUAjnOa?Hr&kVUW9$AsN!8Q8~USqfR0u}UIRr-il4 zp)?VYM*Si*Eoo&&(ZHnI@GF$gOmR{tT`K=uOtmvvC2>O6n9K}evv`xFD9DUGWF0_a zhGYg41A-J?i~4!O;m^sWh2nhFjBp`NBdq+VqXSaB#aRzdzU=%A0ltvo&}Loz1|k32 ze*(IBS?<%!E{wj4a3B=^HCHk9q8Z>>1v#4HE;X1_vJ&hsOKD4a^#BpRlV1vhTDoTw z&8c%3eaX+;9hWm=53irzlIn_qFu+Tnr^#1=$3gy`eH4|tO z-Q5#`>#eQ&Mm&4Yi0(mzegG$05q_q6%Qf_0)ZMygUaLHLK(?ghuF>ESeW{dS9sE8Y|-58i?LAe#thS2}|HME8iqX>qGAtn)1h-t(O;uc~SaR)JnxQCcWEFc~s m!idL+6~rncA&H&PHDRVK5jq&@rs^1d!Z(|yA<2Es9e)7Pc=+}J delta 997 zcmZvaT}YEr7{}k|?A`0!M@DYN#GJ}P)P<;tp%>y8g4xtmh?%VHb+*lqdA3sFOtWb! z*@vFAFNGNdH86^ot}5sv>L#MgNT}&hsWF(R=ZG|L6HS&+~8|uEFXW*t%>s zvn0Na>efK$vF*j4$#>$!q>vOft5S!=K$XHKS|%)@LO+BFEs7CT35dgxDP~Yf0xkd( zkpa>VaT1dgQry4@eZxr7xGtR_aXD^ea!AoDsZ40UGPH}yvJ_I7u~H(EUlIN^;ZLXS zbtsc2W?YB35=+NZ$jp=kX^?u#{uGN@m>eSsLRXl~4B<5KCrPW&#sFCdkeDH9V=}6d zm61ArFSFUUi{^{DXd1%76?-@Mc(1()tUP7^2wuLJk*TdZ1`Wb&b{>Y)yxw_tw;!WV zA~F%Z{HIe@`cQiLl-I6Bvq}tBpR5K4OLdm<4EJ8`<$e#q0RNWbfv}e2dIm7aH*-CX zAxv}&JdkN2>WBcJXwKrf<+4_l_nKJx(R>CG;#PNoG9cs(aDP?yQjVGX-Je{TL(xn} z5Qh*I2))CzTF-%}0O#;N%P5-hJ{r}#RSWhRAi`Nu%EpAXkZg52a{k{BuEG`7Cin(Wp10!;Z&!?Rmv0Smt)baJW5Fn0)WS6~fh#;x>xL!1T-)j3 z+ev>L32MJ;e*j$N-|G*;wXNXBhECx6R2vPO3QY3VMi;+-I7 z+R{4NivD&)CqjQ60aO$bLyRHD5x93cfjEztLQEqrB4!cuh)akC#AU>FL=drzSV7!C f+?2#@^p=nxOc6R9>7%i6dYivJIt_PthqwDTDBAaF diff --git a/norch/autograd/__pycache__/functions.cpython-38.pyc b/norch/autograd/__pycache__/functions.cpython-38.pyc index 154463f23fc429a51c445302c2b25fc395cdbc3d..11732bbfe6ef4b5918608f27dd6ed1d08b930e2b 100644 GIT binary patch delta 1009 zcmcgqOHUI~6uy_4X-n&1%oK*U)KaYA7y}_u5{(b2kMal)Ej|iWEFu*IN=~b($#m6) zJ2z+N!n7{gVHYMU>OwKbxN>7ijK9Ieweg%m5ED0UoaB6S?|0_Rx#zpz_vELfeo@!G zf}WN1=a01r3bmj`Mu8pouJqi)U`31S~op72Q80+B z0TVAY-*OG|b^u>DUo-k?S*6&j(xAWA9XKlY3=z4qBcWhdAYNMwo|48e(NA`!V3<4A zMq6T18==G+=7J-*9)2UosHcIC_QtjI#J|O2xDDNr4^l(9um7Z@r~9vZdK^79TeDTa zu+>U^)zLX5O4?F|RP@4Q);R2QGn286YyN6ge=w)bS z2s89C#2NUuEf2VP-N0~$V4{98Q=Gb+S-5j^LtV_|Z^Kzmox=yw9;Lmm3ExE%Mv|zH z6u&g|YLLWe+t>d$*Z4L~V`A3`c^6H9nmoH%h0M&23oA}f^@e{%} B)1UwV delta 1049 zcmcgrOGs2<6u!Ur;W*o0}({zqAXvT&k@5%WTFm^V_x-+oSDkt4h1e+ z*P#~CVrCa@vL*-@?P?hXLBvq|h&Dl+7D4A+Q?qT*h4bD2y#D{(@Bi+*@$cjMq^`Rq zdVcyxQy-t`VXsFaua_+IOv1Mzf2<>`*uqpz%`8ufdTCSGk}2^j?m0^4eDiD>b0?U)dKcYG;hh>Gu%Wq4SZaLF))H7#AZ?MrJT6oNS3_8T}~ zsbF8-Wi8I35sZ5R_@=JM7$&zsw%e`3u+yEa|Dldj{1L7MM~hnxox&I+_vs&{GK_I) zu_w?h)bo^hfgc*9cvC-u+rgJ&oR0P3-NrM<1m#{GXq>=fMlZ%f4}^kWq5~afx#==1 z{-V_FP^#Lht#~C{$$JXYew|;+saBrotVFA6IH)^RYeaRGtr6ASwR<&(@O{%s{2Hzo z)u=^UC%b4A=w|3);QNIzLy+MtLm$IAf|v`+d0oZOPcU7vme`oPnpnNESW?y!nH3n| z)F57qw9DbL0B%M4jVTJfCi_jQTZJj-gX7Wv_Q9uaT%ZAB)%ya2P9Bu?Rg%}1Ex5=o zD-F6B{(+EXYlje+=8bmvkVuVi$&o;z*>IS7-rR4@P{lX0eP(8gH7#2rFvqR)3`v4% z&k`h(bF1m4Wzs!_Sf0RF20R_`L6E|3-G}#Lr+)!uqvkFE diff --git a/norch/nn/__pycache__/loss.cpython-38.pyc b/norch/nn/__pycache__/loss.cpython-38.pyc index 52d18ca0eae972f836bd45e54f7de32b273619ec..26a0e8b048e81d64dbaae3a07fb82921163cbb16 100644 GIT binary patch delta 433 zcmbOywo8mRl$V!_0SM-}`=`y`$Q#1O_-k?!TTK1G8m26c6xM8}q75~SS)3_sk_@%X zC5#KWYM5%67cwp22Fmj+WUOT>Va(#KVN795Vee(CWvO9Wz*obP#R-zBVOhYxkfDZ2 zgkgceLI!SzTGl)tpe|03dD%?G7fP5@I2N#gcp%Y*OwEiVFBMl z25yF0);u4eX`CQ)vYCocOjcwUk0f629iTq86y|KE35-RTfab6PUBv=qO91U;PhkPu z2oVLTD4RTyUBn3J2$-YUpblll@6gHj*(Dig0G%c>nU{mt5#&4qIzV2lVUq@W4`>%Z t(CTL;0yT^^OwCL+tnq@AqdAT+3Qbnv44vG_xtoz^vMrY^kc{TK4*+chVle;! diff --git a/norch/nn/__pycache__/module.cpython-38.pyc b/norch/nn/__pycache__/module.cpython-38.pyc index 1a7a26c439d525b3c137a6fe3d7d92814271107c..f03aec345ff83b00844976cf82c76b47086ed9d6 100644 GIT binary patch delta 578 zcmXAmPfyfP5WqWaU-xahZKEVeFkx3@U5X~ULQZvLi5&FV>1@Zn&Q^?d z+EnlY5yu&kYK8-Ymr0!>Ns8X#EV%Rs>R0It%pZS{0%o;HUbNrDTJE*6+v)Bk+GY(+ zPI8?PiJ}lrvZ2X1U%1qX7*M~<9xSMT3 z+~TudIcfLer`w&aw52ldHLRXa*LH=pq$e%pK}Q}v@O{{h9ThrN0BB3?Y|dy6>B{N~Mk-}iQAZ|GOm-nG_OJYLT(KI(gK z`!zf`tm|_CHM;M8hGD7|$G2TJ$h^igKEp`WzB*7XwKBNuY!&!(5=fo(!VZ8+oqZ>T z1FoK#R4pm&<3Pj33NM-7#BMLibiCHtjIu(q!GaaRTaBJ~A4WLB8yppc|3N>BVPJTx z;3)V|BF}t)=HW-9VG76iI5RIg*z5Kl$C)b?79=~#Xz08>PRPMo!HHa=@A?>(`~c>t z;(vm9`sQzsUU zqL+9ocQx%1xJNhaD}9rd4t=pZP^HDN>fMr7C&grgb~}!ePLg1|y-IJwvGNp)4ZvAh zj^hG-4X;m2Ype@~1*Zl1g)c~TMKC9532qDImw1Qf%gsvBc*gjyuZ?2@ Date: Thu, 23 May 2024 12:28:17 -0300 Subject: [PATCH 13/14] version 0.0.4 cuda running neural network --- README.md | 2 +- examples/train.ipynb | 2 +- norch/nn/functional.py | 2 -- setup.py | 2 +- test.py | 27 --------------------------- 5 files changed, 3 insertions(+), 32 deletions(-) delete mode 100644 test.py diff --git a/README.md b/README.md index 5266f7a..fad9f4a 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ import random random.seed(1) BATCH_SIZE = 32 -device = "cpu" +device = "cuda" #cpu epochs = 10 transform = transforms.Sequential( diff --git a/examples/train.ipynb b/examples/train.ipynb index f1e51a5..c53ea86 100644 --- a/examples/train.ipynb +++ b/examples/train.ipynb @@ -99,7 +99,7 @@ "outputs": [], "source": [ "BATCH_SIZE = 32\n", - "device = \"cpu\"\n", + "device = \"cuda\" #cpu\n", "epochs = 10\n", "\n", "transform = transforms.Sequential(\n", diff --git a/norch/nn/functional.py b/norch/nn/functional.py index d63e3d2..29fb68d 100644 --- a/norch/nn/functional.py +++ b/norch/nn/functional.py @@ -29,6 +29,4 @@ def one_hot_encode(x, num_classes): target_idx = int(x[i]) one_hot[i][target_idx] = 1 - - return norch.Tensor(one_hot) \ No newline at end of file diff --git a/setup.py b/setup.py index 06c8016..36bff91 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ class CustomInstall(install): setuptools.setup( name = "norch", - version = "0.0.3", + version = "0.0.4", author = "Lucas de Lima", author_email = "nogueiralucasdelima@gmail.com", description = "A deep learning framework", diff --git a/test.py b/test.py deleted file mode 100644 index 9297870..0000000 --- a/test.py +++ /dev/null @@ -1,27 +0,0 @@ -import norch -import norch.nn as nn -import norch.optim as optim -from norch.utils.data.dataloader import Dataloader -from norch.norchvision import transforms -import numpy as np -import matplotlib.pyplot as plt -import random -random.seed(1) - -class MyModel(nn.Module): - def __init__(self): - super(MyModel, self).__init__() - self.fc1 = nn.Linear(784, 30) - self.sigmoid1 = nn.Sigmoid() - self.fc2 = nn.Linear(30, 10) - self.sigmoid2 = nn.Sigmoid() - - def forward(self, x): - out = self.fc1(x) - out = self.sigmoid1(out) - out = self.fc2(out) - out = self.sigmoid2(out) - - return out - -model = MyModel().to("cuda") \ No newline at end of file From 55fc1ac020a87ba18eb53456af573f6523fe81de Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Thu, 23 May 2024 12:28:46 -0300 Subject: [PATCH 14/14] version 0.0.4 cuda running neural network --- norch/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/norch/__init__.py b/norch/__init__.py index 25fe68e..7bc1d53 100644 --- a/norch/__init__.py +++ b/norch/__init__.py @@ -4,6 +4,6 @@ from .optim import * from .utils import * from .norchvision import * -__version__ = "0.0.3" +__version__ = "0.0.4" __author__ = 'Lucas de Lima Nogueira' __credits__ = 'Lucas de Lima Nogueira' \ No newline at end of file