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

add pad operator #734

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
142 changes: 142 additions & 0 deletions python/singa/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4698,6 +4698,148 @@ def cossim(a, b):
return CosSim()(a, b)[0]


class Pad(Operator):
"""
Pad operator following ONNX Operator Schemas
https://github.com/onnx/onnx/blob/master/docs/Operators.md#Pad

Example usage::
data =
[
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]
pads = [0, 2, 0, 0]

# constant mode
mode = 'constant'
constant_value = 0.0
output =
[
[
[0.0, 0.0, 1.0, 1.2],
[0.0, 0.0, 2.3, 3.4],
[0.0, 0.0, 4.5, 5.7],
],
]

# reflect mode
mode = 'reflect'
output =
[
[
[1.0, 1.2, 1.0, 1.2],
[2.3, 3.4, 2.3, 3.4],
[4.5, 5.7, 4.5, 5.7],
],
]

# edge mode
mode = 'edge'
output =
[
[
[1.0, 1.0, 1.0, 1.2],
[2.3, 2.3, 2.3, 3.4],
[4.5, 4.5, 4.5, 5.7],
],
]
"""

def __init__(self, mode, pads, constant=0.):
"""
Args:
mode (string): Supported modes: `constant`(default), `reflect`, `edge`.
joddiy marked this conversation as resolved.
Show resolved Hide resolved
pads (list[int]): list of integers indicating the number of padding elements
to add at the beginning each axis.
constant (float): A scalar value to be used if the mode chosen is
`constant`
"""
super(Pad, self).__init__()
self.mode = mode
if self.mode not in ("constant", "reflect", "edge"):
assert False, ('Only support three modes: constant, reflect, edge')
self.constant = constant
self.pads = pads
self.pad_width = ()

def forward(self, x):
if not self.pad_width:
half_width = len(self.pads) // 2
for i in range(half_width):
self.pad_width += ((self.pads[i], self.pads[i + half_width])),

for axis, pads in zip(range(len(x.shape())), self.pad_width):
for pad, is_left in zip(pads, (True, False)):
if pad == 0:
continue
pad_shape = list(x.shape())
if self.mode == "constant":
pad_shape[axis] = pad
padding = singa.Tensor(list(pad_shape), x.device())
padding.SetFloatValue(self.constant)
if is_left:
x = singa.ConcatOn(singa.VecTensor([padding, x]), axis)
else:
x = singa.ConcatOn(singa.VecTensor([x, padding]), axis)
elif self.mode == "reflect":
axis_shape = pad_shape[axis]
if is_left:
padding = singa.SliceOn(x, 0, pad, axis)
x = singa.ConcatOn(singa.VecTensor([padding, x]), axis)
else:
padding = singa.SliceOn(x, axis_shape - pad, axis_shape,
axis)
x = singa.ConcatOn(singa.VecTensor([x, padding]), axis)
elif self.mode == "edge":
axis_shape = pad_shape[axis]
if is_left:
padding = []
for _ in range(pad):
padding.append(singa.SliceOn(x, 0, 1, axis))
padding.append(x)
padding = singa.VecTensor(padding)
x = singa.ConcatOn(padding, axis)
else:
padding = [x]
for _ in range(pad):
padding.append(
singa.SliceOn(x, axis_shape - 1, axis_shape,
axis))
padding = singa.VecTensor(padding)
x = singa.ConcatOn(padding, axis)
return x

def backward(self, dy):
for axis, pads in zip(range(len(dy.shape())), self.pad_width):
for pad, is_left in zip(pads, (True, False)):
if pad == 0:
continue
axis_shape = list(dy.shape())[axis]
if is_left:
dy = singa.SliceOn(dy, pad, axis_shape, axis)
else:
dy = singa.SliceOn(dy, 0, axis_shape - pad, axis)
return dy


def pad(x, mode, pads, constant=0.):
"""
Produces a pad operator
Args:
x (Tensor): input tensor.
mode (string): Supported modes: `constant`(default), `reflect`, `edge`.
pads (list[int]): list of integers indicating the number of padding elements
to add at the beginning each axis.
constant (float): A scalar value to be used if the mode chosen is
`constant`
Returns:
the output Tensor.
"""
return Pad(mode, pads, constant)(x)[0]


''' alias for Operator and Layers
'''
Operation = Operator
Expand Down
19 changes: 19 additions & 0 deletions python/singa/sonnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ class SingaBackend(Backend):
'Reshape': 'Reshape',
'Slice': 'Slice',
'Clip': 'Clip',
'Pad': 'Pad',
'Gemm': 'layer.Gemm', # layer
'BatchNormalization': 'layer.BatchNorm2d', # layer
'Conv': 'layer.Conv2d', # layer
Expand Down Expand Up @@ -1148,8 +1149,26 @@ class SingaBackend(Backend):
'Conv': '_create_conv',
'MaxPool': '_create_max_avg_pool',
'AveragePool': '_create_max_avg_pool',
'Pad': '_create_pad',
}

@classmethod
def _create_pad(cls, onnx_node, operator, opset_version=_opset_version):
"""
get the Pad operator from onnx node
Args:
onnx_node (OnnxNode): a given onnx node
operator (Operator Class): a singa operator class
opset_version (int): the opset version
Returns:
singa operator instance
"""
mode = onnx_node.getattr("mode", "constant")
onnx_node.set_attr_inputs(onnx_node.inputs[1], 'pads')
if len(onnx_node.inputs) == 3:
onnx_node.set_attr_inputs(onnx_node.inputs[2], 'constant')
return operator(mode, None, None)

@classmethod
def _create_cast(cls, onnx_node, operator, opset_version=_opset_version):
"""
Expand Down
83 changes: 77 additions & 6 deletions test/python/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2520,8 +2520,8 @@ def _gemm_helper(self, dev):
result = gemm(a)

params = gemm.get_params()
B = tensor.to_numpy(params['Gemm.W'])
C = tensor.to_numpy(params['Gemm.b'])
B = tensor.to_numpy(params['.W'])
C = tensor.to_numpy(params['.b'])

da, db, dc = result.creator.backward(dy.data)

Expand Down Expand Up @@ -2575,7 +2575,6 @@ def globalaveragepool_channel_first(self, dev):
dy = tensor.from_numpy(DY)
dy.to_device(dev)


result = autograd.globalaveragepool(x)
dx = result.creator.backward(dy.data)

Expand Down Expand Up @@ -3050,8 +3049,6 @@ def test_cudnn_rnn_operation(self, dev=gpu_dev):
dx, dhx, dcx, dw = _rnn.backward(dy.data, dhy.data, dcy.data)

def cossim_helper(self, dev):
from numpy.linalg import norm

A = np.random.randn(*[3, 10]).astype(np.float32)
B = np.random.randn(*[3, 10]).astype(np.float32)

Expand All @@ -3067,7 +3064,7 @@ def cossim_helper(self, dev):
y = autograd.cossim(a, b)
da, db = y.creator.backward(dy.data) # CTensor

self.check_shape(y.shape, (3, ))
self.check_shape(y.shape, (3,))
self.check_shape(da.shape(), (3, 10))
self.check_shape(db.shape(), (3, 10))

Expand All @@ -3078,6 +3075,80 @@ def test_cossim_cpu(self):
def test_cossim_gpu(self):
self.cossim_helper(gpu_dev)

def pad_helper(self, dev):
X = np.array([
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]).astype(np.float32)
Y1 = np.array([
[0.0, 0.0, 1.0, 1.2],
[0.0, 0.0, 2.3, 3.4],
[0.0, 0.0, 4.5, 5.7],
],).astype(np.float32)
Y2 = np.array([
[1.0, 1.2, 1.0, 1.2],
[2.3, 3.4, 2.3, 3.4],
[4.5, 5.7, 4.5, 5.7],
],).astype(np.float32)
Y3 = np.array([
[1.0, 1.0, 1.0, 1.2],
[2.3, 2.3, 2.3, 3.4],
[4.5, 4.5, 4.5, 5.7],
],).astype(np.float32)

x = tensor.from_numpy(X)
x.to_device(dev)
pads = [0, 2, 0, 0]

DY = np.random.randn(3, 4).astype(np.float32)
dy = tensor.from_numpy(DY)
dy.to_device(dev)

y1 = autograd.pad(x, "constant", pads)
y2 = autograd.pad(x, "reflect", pads)
y3 = autograd.pad(x, "edge", pads)
dx1 = y1.creator.backward(dy.data)
dx2 = y2.creator.backward(dy.data)
dx3 = y3.creator.backward(dy.data)
pad_width = []
half_width = len(pads) // 2
for i in range(half_width):
pad_width += [[pads[i], pads[i + half_width]]]

np.testing.assert_array_almost_equal(tensor.to_numpy(y1),
np.pad(
X,
pad_width=pad_width,
mode="constant",
constant_values=0.,
),
decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(y2),
np.pad(
X,
pad_width=pad_width,
mode="reflect",
),
decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(y3),
np.pad(
X,
pad_width=pad_width,
mode="edge",
),
decimal=5)
self.check_shape(dx1.shape(), (3, 2))
self.check_shape(dx2.shape(), (3, 2))
self.check_shape(dx3.shape(), (3, 2))

def test_pad_cpu(self):
self.pad_helper(cpu_dev)

# @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
# def test_pad_gpu(self):
# self.pad_helper(gpu_dev)


if __name__ == '__main__':
unittest.main()