Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Operators #738

Merged
merged 5 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion python/singa/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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]

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion python/singa/sonnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,8 @@ class SingaBackend(Backend):
'Unsqueeze': 'Unsqueeze',
'NonZero': 'NonZero',
'Ceil': 'Ceil',
'Floor': 'Floor',
'Abs': 'Abs',
# # special op
'Cast': 'Cast',
'Split': 'Split',
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions test/python/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down