From aa225378f7f8d0026d5414ae3ed6ed4dc79acb0e Mon Sep 17 00:00:00 2001 From: Shashankwer Date: Tue, 16 Jun 2020 17:08:25 +0800 Subject: [PATCH 1/4] Adding Opertors Added the Floor operator in autograd.py test_operation.py and sonnx.py Added Abs in sonnx.py --- python/singa/autograd.py | 47 ++++++++++++++++++++++++++++++++++- python/singa/sonnx.py | 7 +++++- test/python/test_operation.py | 22 ++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/python/singa/autograd.py b/python/singa/autograd.py index 0a24984066..73ff0792d4 100644 --- a/python/singa/autograd.py +++ b/python/singa/autograd.py @@ -1202,7 +1202,7 @@ def forward(self, pos, neg): zero.SetFloatValue(0.0) val = singa.AddFloat(singa.__sub__(neg, pos), self.M) gt_zero = singa.__gt__(val, zero) - self.inputs = (gt_zero, ) # (BS,) + self.inputs = (gt_zero,) # (BS,) all_loss = singa.__mul__(gt_zero, val) loss = singa.SumAll(all_loss) loss /= (pos.shape()[0]) @@ -1221,6 +1221,7 @@ def backward(self, dy=1.0): dneg = singa.__mul__(gt_zero, dneg_factor) return dpos, dneg + def qa_lstm_loss(pos, neg, M=0.2): return QALSTMLoss(M)(pos, neg)[0] @@ -4073,6 +4074,50 @@ def ceil(x): return Ceil()(x)[0] +class Floor(Operator): + """ + Floor takes one input data (Tensor) and produces one output data (Tensor), + where the floor is, `y = floor(x)`, is applied to the tensor elementwise + """ + + def __init__(self): + super(Floor, self).__init__() + + def forward(self, x): + """ + forward of floor + Args: + x (CTensor): input tensor + Returns: + the output CTensor + """ + return singa.Floor(x) + + def backward(self, dy): + """ + backward of floor. Derivative of floor is 0 + Args: + dy (CTensor): gradient tensor + Returns: + the gradient tensor over the input tensor. + """ + dy = singa.Tensor(dy.shape(), dy.device()) + dy.SetFloatValue(0.) + return dy + + +def floor(x): + """ + floor takes one input data (Tensor) and produces one output data (Tensor) + the value of floor is `y = floor(x)`, is applied to the tensor elementwise. + Args: + x(Tensor): input tensor. + Returns: + the output tensor + """ + return Floor()(x)[0] + + class Split(Operator): """ Init a Split, Split a tensor into a list of tensors, along the specified diff --git a/python/singa/sonnx.py b/python/singa/sonnx.py index 3d25bc5d1d..d3c473672e 100755 --- a/python/singa/sonnx.py +++ b/python/singa/sonnx.py @@ -1087,6 +1087,8 @@ class SingaBackend(Backend): 'Unsqueeze': 'Unsqueeze', 'NonZero': 'NonZero', 'Ceil': 'Ceil', + 'Floor': 'Floor', + 'Abs': 'Abs', # # special op 'Cast': 'Cast', 'Split': 'Split', @@ -1757,7 +1759,10 @@ def _parse_graph_inputs_outputs(cls, graph, params, device): return inputs, outputs @classmethod - def _onnx_model_to_singa_ops(cls, graph, device, opset_version=_opset_version): + def _onnx_model_to_singa_ops(cls, + graph, + device, + opset_version=_opset_version): """ get all intermediate params, operators, and input info from onnx model Args: diff --git a/test/python/test_operation.py b/test/python/test_operation.py index ccdd2fd9aa..86e134e966 100755 --- a/test/python/test_operation.py +++ b/test/python/test_operation.py @@ -2816,6 +2816,28 @@ def test_ceil_cpu(self): def test_ceil_gpu(self): self.ceil_test(gpu_dev) + def floor_test(self,dev): + X = np.array([-1.9,1.2]).astype(np.float32) + DY = np.ones((2),dtype=np.float32) + y = np.floor(X) + x = tensor.from_numpy(X) + dy = tensor.from_numpy(DY) + x.to_device(dev) + dy.to_device(dev) + + result = autograd.floor(x) + dx = result.creator.backward(dy.data) + DX = np.zeros((2),dtype=np.float32) + np.testing.assert_array_almost_equal(tensor.to_numpy(result),y,decimal=5) + np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),DX,decimal=5) + + def test_floor_cpu(self): + self.floor_test(cpu_dev) + + @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled') + def test_floor_gpu(self): + self.floor_test(gpu_dev) + def split_test(self, dev): X = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32) DY1 = np.ones((2), dtype=np.float32) From dc0b791ca5c763c3c547938b77f633ee7e281c75 Mon Sep 17 00:00:00 2001 From: Shashankwer Date: Tue, 16 Jun 2020 20:26:16 +0800 Subject: [PATCH 2/4] Made correction in autograd.py Corrected the superclass to Operation --- python/singa/autograd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/singa/autograd.py b/python/singa/autograd.py index 73ff0792d4..6cc35afa2e 100644 --- a/python/singa/autograd.py +++ b/python/singa/autograd.py @@ -4074,7 +4074,7 @@ def ceil(x): return Ceil()(x)[0] -class Floor(Operator): +class Floor(Operation): """ Floor takes one input data (Tensor) and produces one output data (Tensor), where the floor is, `y = floor(x)`, is applied to the tensor elementwise From e4bb8bc586c2ef60fca9dc05bed19371a323430d Mon Sep 17 00:00:00 2001 From: Shashankwer Date: Wed, 17 Jun 2020 20:53:36 +0800 Subject: [PATCH 3/4] Changes in the autograd.py after running test_operation.py Made changes in autograd.py after making changes in test_operation --- python/singa/autograd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/singa/autograd.py b/python/singa/autograd.py index 6cc35afa2e..73ff0792d4 100644 --- a/python/singa/autograd.py +++ b/python/singa/autograd.py @@ -4074,7 +4074,7 @@ def ceil(x): return Ceil()(x)[0] -class Floor(Operation): +class Floor(Operator): """ Floor takes one input data (Tensor) and produces one output data (Tensor), where the floor is, `y = floor(x)`, is applied to the tensor elementwise From 56047344b041dd2330362a28030122f793e51d19 Mon Sep 17 00:00:00 2001 From: Joddiy Zhang Date: Fri, 18 Sep 2020 11:19:43 +0800 Subject: [PATCH 4/4] Update test_operation.py --- test/python/test_operation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/python/test_operation.py b/test/python/test_operation.py index a939db66d4..54d251325c 100755 --- a/test/python/test_operation.py +++ b/test/python/test_operation.py @@ -1372,7 +1372,6 @@ def _transpose_helper(self, dev): x = tensor.from_numpy(x) dy = tensor.from_numpy(dy) x.to_device(dev) - dy.to_device(dev) result = autograd.transpose(x, (1, 2, 0))