Repository: quic/aimet (language: python)

Issue:
Implement element-wise log, abs, neg



Discussion / hints:

Fix patch:
diff --git a/TrainingExtensions/torch/src/python/aimet_torch/elementwise_ops.py b/TrainingExtensions/torch/src/python/aimet_torch/elementwise_ops.py
index cbc2ba123c..02e545a80c 100644
--- a/TrainingExtensions/torch/src/python/aimet_torch/elementwise_ops.py
+++ b/TrainingExtensions/torch/src/python/aimet_torch/elementwise_ops.py
@@ -3,7 +3,7 @@
 # =============================================================================
 #  @@-COPYRIGHT-START-@@
 #
-#  Copyright (c) 2021, Qualcomm Innovation Center, Inc. All rights reserved.
+#  Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
 #
 #  Redistribution and use in source and binary forms, with or without
 #  modification, are permitted provided that the following conditions are met:
@@ -95,6 +95,9 @@ def create_wrapper_module(class_name: str, functional: Callable) -> Callable:
 Mean = create_wrapper_module('Mean', torch.mean)
 Sum = create_wrapper_module('Sum', torch.sum)
 Prod = create_wrapper_module('Prod', torch.prod)
+Log = create_wrapper_module('Log', torch.log)
+Abs = create_wrapper_module('Abs', torch.abs)
+Neg = create_wrapper_module('Neg', torch.neg)
 
 
 # modules for functional operations defined under torch.nn.functional package


Test patch:
diff --git a/TrainingExtensions/torch/test/python/test_elementwise_ops.py b/TrainingExtensions/torch/test/python/test_elementwise_ops.py
index f3bb1cb8c7..8cac00dc2d 100644
--- a/TrainingExtensions/torch/test/python/test_elementwise_ops.py
+++ b/TrainingExtensions/torch/test/python/test_elementwise_ops.py
@@ -3,7 +3,7 @@
 # =============================================================================
 #  @@-COPYRIGHT-START-@@
 #
-#  Copyright (c) 2021, Qualcomm Innovation Center, Inc. All rights reserved.
+#  Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
 #
 #  Redistribution and use in source and binary forms, with or without
 #  modification, are permitted provided that the following conditions are met:
@@ -44,7 +44,7 @@
 import aimet_common.libpymo as libpymo
 from aimet_common.defs import QuantScheme
 from aimet_torch.elementwise_ops import Add, Subtract, Multiply, Divide, Concat, MatMul, Erf, Sqrt, \
-    Greater, Less, GreaterEqual, LessEqual, NotEqual, Equal, Where, Mean, Sum, Prod
+    Greater, Less, GreaterEqual, LessEqual, NotEqual, Equal, Where, Mean, Sum, Prod, Log, Abs, Neg
 from aimet_torch.quantsim import QuantizationSimModel
 
 
@@ -437,3 +437,30 @@ def test_prod_op(self):
         out = model(input1)
         out1 = torch.prod(input1)
         self.assertTrue(np.allclose(out, out1))
+
+    def test_log_op(self):
+        torch.manual_seed(42)
+        model = Model3(Log())
+        inputs = torch.rand(4, 3, 28, 28)
+
+        custom_module_out = model(inputs)
+        original_module_out = torch.log(inputs)
+        self.assertTrue(np.allclose(custom_module_out, original_module_out))
+
+    def test_abs_op(self):
+        torch.manual_seed(42)
+        model = Model3(Abs())
+        inputs = torch.rand(4, 3, 28, 28)
+
+        custom_module_out = model(inputs)
+        original_module_out = torch.abs(inputs)
+        self.assertTrue(np.allclose(custom_module_out, original_module_out))
+
+    def test_neg_op(self):
+        torch.manual_seed(42)
+        model = Model3(Neg())
+        inputs = torch.rand(4, 3, 28, 28)
+
+        custom_module_out = model(inputs)
+        original_module_out = torch.neg(inputs)
+        self.assertTrue(np.allclose(custom_module_out, original_module_out))