You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by ch...@apache.org on 2020/07/03 13:01:45 UTC
[singa] branch dev updated: add round and rounde operators
This is an automated email from the ASF dual-hosted git repository.
chrishkchris pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/singa.git
The following commit(s) were added to refs/heads/dev by this push:
new e96704e add round and rounde operators
new 67ca2a4 Merge pull request #748 from joddiy/add-operators
e96704e is described below
commit e96704e976d3bf81bc31a70003121af5333f5e9f
Author: joddiy <jo...@qq.com>
AuthorDate: Fri Jul 3 00:35:16 2020 +0800
add round and rounde operators
---
python/singa/autograd.py | 55 +++++++++++++++++++++++++++++++++++++++++++
test/python/test_operation.py | 45 +++++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+)
diff --git a/python/singa/autograd.py b/python/singa/autograd.py
index 7c1f38d..de4a6ec 100644
--- a/python/singa/autograd.py
+++ b/python/singa/autograd.py
@@ -5089,6 +5089,61 @@ def upsample(x, mode, scales):
return UpSample(mode, scales)(x)[0]
+class Round(Operator):
+ """
+ Element-wise round the input
+ """
+
+ def __init__(self):
+ super(Round, self).__init__()
+
+ def forward(self, x):
+ return singa.Round(x)
+
+ def backward(self, dy):
+ dy = singa.Tensor(dy.shape(), dy.device())
+ dy.SetFloatValue(0.)
+ return dy
+
+
+def round(x):
+ """
+ Element-wise round the input
+ Args:
+ x (Tensor): input tensor.
+ Returns:
+ the output Tensor.
+ """
+ return Round()(x)[0]
+
+
+class Rounde(Operator):
+ """
+ Element-wise round the input, In case of halfs, round to the nearest even integer
+ """
+
+ def __init__(self):
+ super(Rounde, self).__init__()
+
+ def forward(self, x):
+ return singa.RoundE(x)
+
+ def backward(self, dy):
+ dy = singa.Tensor(dy.shape(), dy.device())
+ dy.SetFloatValue(0.)
+ return dy
+
+
+def rounde(x):
+ """
+ Element-wise round the input, In case of halfs, round to the nearest even integer
+ Args:
+ x (Tensor): input tensor.
+ Returns:
+ the output Tensor.
+ """
+ return Rounde()(x)[0]
+
''' alias for Operator and Layers
'''
Operation = Operator
diff --git a/test/python/test_operation.py b/test/python/test_operation.py
index f7f0b0b..9dfdf6c 100755
--- a/test/python/test_operation.py
+++ b/test/python/test_operation.py
@@ -3233,6 +3233,51 @@ class TestPythonOperation(unittest.TestCase):
self.assertRaises(AssertionError, autograd.qa_lstm_loss, _2d,
_1d)
+ def rounde_helper(self, dev):
+ X = np.array([0.1, 0.5, 0.9, 1.2, 1.5,
+ 1.8, 2.3, 2.5, 2.7, -1.1,
+ -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float32)
+ x = tensor.from_numpy(X)
+ x.to_device(dev)
+
+ y_t = np.array([0., 0., 1., 1., 2.,
+ 2., 2., 2., 3., -1.,
+ -2., -2., -2., -2., -3.]).astype(np.float32)
+ dy = tensor.from_numpy(y_t)
+ dy.to_device(dev)
+
+ y = autograd.rounde(x)
+ np.testing.assert_array_almost_equal(tensor.to_numpy(y), y_t)
+
+ def test_rounde_cpu(self):
+ self.rounde_helper(cpu_dev)
+
+ @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+ def test_rounde_gpu(self):
+ self.rounde_helper(gpu_dev)
+
+ def round_helper(self, dev):
+ X = np.array([0.1, 0.5, 0.9, 1.2, 1.5,
+ 1.8, 2.3, 2.5, 2.7, -1.1,
+ -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float32)
+ x = tensor.from_numpy(X)
+ x.to_device(dev)
+
+ y_t = np.array([0., 1., 1., 1., 2.,
+ 2., 2., 3., 3., -1.,
+ -2., -2., -2., -3., -3.]).astype(np.float32)
+ dy = tensor.from_numpy(y_t)
+ dy.to_device(dev)
+
+ y = autograd.round(x)
+ np.testing.assert_array_almost_equal(tensor.to_numpy(y), y_t)
+
+ def test_round_cpu(self):
+ self.round_helper(cpu_dev)
+
+ @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+ def test_round_gpu(self):
+ self.round_helper(gpu_dev)
if __name__ == '__main__':
unittest.main()