int16 uint8 bool supported akg ops

This commit is contained in:
Jonathan Yan 2020-08-11 20:35:31 -04:00
parent 2dc4dae41c
commit e0528615e3
11 changed files with 307 additions and 0 deletions

View File

@ -23,6 +23,9 @@ equal_op_info = AkgGpuRegOp("Equal") \
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default, DataType.BOOL_Default) \
.dtype_format(DataType.BOOL_Default, DataType.BOOL_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -23,6 +23,8 @@ greater_equal_op_info = AkgGpuRegOp("GreaterEqual") \
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -23,6 +23,8 @@ lessequal_op_info = AkgGpuRegOp("LessEqual") \
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -23,6 +23,9 @@ notequal_op_info = AkgGpuRegOp("NotEqual") \
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.BOOL_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default, DataType.BOOL_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default, DataType.BOOL_Default) \
.dtype_format(DataType.BOOL_Default, DataType.BOOL_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -22,6 +22,10 @@ squeeze_op_info = AkgGpuRegOp("Squeeze") \
.attr("axis", "optional", "listInt") \
.dtype_format(DataType.F16_Default, DataType.F16_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default) \
.dtype_format(DataType.BOOL_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -22,6 +22,10 @@ squeeze_grad_op_info = AkgGpuRegOp("SqueezeGrad") \
.attr("x_shape", "required", "listInt") \
.dtype_format(DataType.F16_Default, DataType.F16_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default) \
.dtype_format(DataType.U8_Default, DataType.U8_Default) \
.dtype_format(DataType.BOOL_Default, DataType.BOOL_Default) \
.get_op_info()

View File

@ -22,6 +22,8 @@ tile_op_info = AkgGpuRegOp("Tile") \
.attr("multiples", "required", "listInt") \
.dtype_format(DataType.F16_Default, DataType.F16_Default) \
.dtype_format(DataType.F32_Default, DataType.F32_Default) \
.dtype_format(DataType.I32_Default, DataType.I32_Default) \
.dtype_format(DataType.I16_Default, DataType.I16_Default) \
.get_op_info()

View File

@ -65,6 +65,21 @@ def test_equal():
y2_np = np.array([0, 1, -3]).astype(np.int32)
y2 = Tensor(y2_np)
expect2 = np.equal(x2_np, y2_np)
x3_np = np.array([0, 1, 3]).astype(np.int16)
x3 = Tensor(x3_np)
y3_np = np.array([0, 1, -3]).astype(np.int16)
y3 = Tensor(y3_np)
expect3 = np.equal(x3_np, y3_np)
x4_np = np.array([0, 1, 4]).astype(np.uint8)
x4 = Tensor(x4_np)
y4_np = np.array([0, 1, 3]).astype(np.uint8)
y4 = Tensor(y4_np)
expect4 = np.equal(x4_np, y4_np)
x5_np = np.array([True, False, True]).astype(bool)
x5 = Tensor(x5_np)
y5_np = np.array([True, False, False]).astype(bool)
y5 = Tensor(y5_np)
expect5 = np.equal(x5_np, y5_np)
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
equal = NetEqual()
@ -77,6 +92,17 @@ def test_equal():
output2 = equal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape
output3 = equal(x3, y3)
assert np.all(output3.asnumpy() == expect3)
assert output3.shape == expect3.shape
output4 = equal(x4, y4)
assert np.all(output4.asnumpy() == expect4)
assert output4.shape == expect4.shape
output5 = equal(x5, y5)
assert np.all(output5.asnumpy() == expect5)
assert output5.shape == expect5.shape
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
equal = NetEqual()
@ -89,6 +115,15 @@ def test_equal():
output2 = equal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape
output3 = equal(x3, y3)
assert np.all(output3.asnumpy() == expect3)
assert output3.shape == expect3.shape
output4 = equal(x4, y4)
assert np.all(output4.asnumpy() == expect4)
assert output4.shape == expect4.shape
output5 = equal(x5, y5)
assert np.all(output5.asnumpy() == expect5)
assert output5.shape == expect5.shape
@pytest.mark.level0
@ -98,18 +133,45 @@ def test_notequal():
x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
expect0 = np.array([[True, True], [False, True]])
x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
expect1 = np.array([[True, True], [False, True]])
x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
expect2 = np.array([[True, True], [False, False]])
x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool))
y3 = Tensor(np.array([[True, False]]).astype(bool))
expect3 = np.array([[True, True], [False, False]])
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
notequal = NetNotEqual()
output0 = notequal(x0, y0)
assert np.all(output0.asnumpy() == expect0)
assert output0.shape == expect0.shape
output1 = notequal(x1, y1)
assert np.all(output1.asnumpy() == expect1)
assert output1.shape == expect1.shape
output2 = notequal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape
output3 = notequal(x3, y3)
assert np.all(output3.asnumpy() == expect3)
assert output3.shape == expect3.shape
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
notequal = NetNotEqual()
output0 = notequal(x0, y0)
assert np.all(output0.asnumpy() == expect0)
assert output0.shape == expect0.shape
output1 = notequal(x1, y1)
assert np.all(output1.asnumpy() == expect1)
assert output1.shape == expect1.shape
output2 = notequal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape
output3 = notequal(x3, y3)
assert np.all(output3.asnumpy() == expect3)
assert output3.shape == expect3.shape
@pytest.mark.level0
@ -119,15 +181,33 @@ def test_greaterqual():
x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32))
y0 = Tensor(np.array([[1, 2]]).astype(np.float32))
expect0 = np.array([[True, False], [True, False]])
x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16))
y1 = Tensor(np.array([[1, 2]]).astype(np.int16))
expect1 = np.array([[True, False], [True, False]])
x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8))
y2 = Tensor(np.array([[1, 2]]).astype(np.uint8))
expect2 = np.array([[True, False], [True, True]])
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
gequal = NetGreaterEqual()
output0 = gequal(x0, y0)
assert np.all(output0.asnumpy() == expect0)
assert output0.shape == expect0.shape
output1 = gequal(x1, y1)
assert np.all(output1.asnumpy() == expect1)
assert output1.shape == expect1.shape
output2 = gequal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
gequal = NetGreaterEqual()
output0 = gequal(x0, y0)
assert np.all(output0.asnumpy() == expect0)
assert output0.shape == expect0.shape
output1 = gequal(x1, y1)
assert np.all(output1.asnumpy() == expect1)
assert output1.shape == expect1.shape
output2 = gequal(x2, y2)
assert np.all(output2.asnumpy() == expect2)
assert output2.shape == expect2.shape

View File

@ -38,12 +38,27 @@ def test_lessequal():
x = Tensor(np.array([[1, 2, 3]]).astype(np.float32))
y = Tensor(np.array([[2]]).astype(np.float32))
expect = [[True, True, False]]
x1 = Tensor(np.array([[1, 2, 3]]).astype(np.int16))
y1 = Tensor(np.array([[2]]).astype(np.int16))
expect = [[True, True, False]]
x2 = Tensor(np.array([[1, 2, 3]]).astype(np.uint8))
y2 = Tensor(np.array([[2]]).astype(np.uint8))
expect = [[True, True, False]]
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
lessequal = Net()
output = lessequal(x, y)
assert np.all(output.asnumpy() == expect)
output = lessequal(x1, y1)
assert np.all(output.asnumpy() == expect)
output = lessequal(x2, y2)
assert np.all(output.asnumpy() == expect)
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
lessequal = Net()
output = lessequal(x, y)
assert np.all(output.asnumpy() == expect)
output = lessequal(x1, y1)
assert np.all(output.asnumpy() == expect)
output = lessequal(x2, y2)
assert np.all(output.asnumpy() == expect)

View File

@ -0,0 +1,79 @@
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
import mindspore.context as context
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.ops import operations as P
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.squeeze = P.Squeeze()
def construct(self, tensor):
return self.squeeze(tensor)
def test_net_bool():
x = np.random.randn(1, 16, 1, 1).astype(np.bool)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())
def test_net_uint8():
x = np.random.randn(1, 16, 1, 1).astype(np.uint8)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())
def test_net_int16():
x = np.random.randn(1, 16, 1, 1).astype(np.int16)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())
def test_net_int32():
x = np.random.randn(1, 16, 1, 1).astype(np.int32)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())
def test_net_float16():
x = np.random.randn(1, 16, 1, 1).astype(np.float16)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())
def test_net_float32():
x = np.random.randn(1, 16, 1, 1).astype(np.float32)
net = Net()
output = net(Tensor(x))
print(output.asnumpy())
assert np.all(output.asnumpy() == x.squeeze())

View File

@ -33,6 +33,27 @@ mul1 = (2, 2, 2)
input_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.float32)
mul2 = (1, 1, 1)
input_32_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.int32)
mul_32_0 = (8, 1, 1)
input_32_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int32)
mul_32_1 = (2, 2, 2)
input_32_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.int32)
mul_32_2 = (1, 1, 1)
input_16_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.int16)
mul_16_0 = (8, 1, 1)
input_16_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int16)
mul_16_1 = (2, 2, 2)
input_16_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.int16)
mul_16_2 = (1, 1, 1)
input_8_x0 = np.arange(2).reshape((2, 1, 1)).astype(np.uint8)
mul_8_0 = (8, 1, 1)
input_8_x1 = np.arange(32).reshape((2, 4, 4)).astype(np.int8)
mul_8_1 = (2, 2, 2)
input_8_x2 = np.arange(1).reshape((1, 1, 1)).astype(np.uint8)
mul_8_2 = (1, 1, 1)
class Net(Cell):
def __init__(self):
@ -54,6 +75,46 @@ class Net(Cell):
return output
class Net32(Cell):
def __init__(self):
super(Net32, self).__init__()
self.Tile = Tile()
self.input_32_x0 = Parameter(initializer(Tensor(input_32_x0), input_32_x0.shape), name='x0')
self.mul_32_0 = mul_32_0
self.input_32_x1 = Parameter(initializer(Tensor(input_32_x1), input_32_x1.shape), name='x1')
self.mul_32_1 = mul_32_1
self.input_32_x2 = Parameter(initializer(Tensor(input_32_x2), input_32_x2.shape), name='x2')
self.mul_32_2 = mul_32_2
@ms_function
def construct(self):
output = (self.Tile(self.input_32_x0, self.mul_32_0),
self.Tile(self.input_32_x1, self.mul_32_1),
self.Tile(self.input_32_x2, self.mul_32_2))
return output
class Net16(Cell):
def __init__(self):
super(Net16, self).__init__()
self.Tile = Tile()
self.input_16_x0 = Parameter(initializer(Tensor(input_16_x0), input_16_x0.shape), name='x0')
self.mul_16_0 = mul_16_0
self.input_16_x1 = Parameter(initializer(Tensor(input_16_x1), input_16_x1.shape), name='x1')
self.mul_16_1 = mul_16_1
self.input_16_x2 = Parameter(initializer(Tensor(input_16_x2), input_16_x2.shape), name='x2')
self.mul_16_2 = mul_16_2
@ms_function
def construct(self):
output = (self.Tile(self.input_16_x0, self.mul_16_0),
self.Tile(self.input_16_x1, self.mul_16_1),
self.Tile(self.input_16_x2, self.mul_16_2))
return output
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard
@ -78,3 +139,55 @@ def test_tile():
error2 = np.ones(shape=expect2.shape) * 1.0e-5
assert np.all(diff2 < error2)
assert output[2].shape == expect2.shape
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard
def test_tile_32():
net = Net32()
output = net()
expect0 = np.tile(input_32_x0, mul_32_0)
diff0 = output[0].asnumpy() - expect0
error0 = np.ones(shape=expect0.shape) * 1.0e-5
assert np.all(diff0 < error0)
assert output[0].shape == expect0.shape
expect1 = np.tile(input_32_x1, mul_32_1)
diff1 = output[1].asnumpy() - expect1
error1 = np.ones(shape=expect1.shape) * 1.0e-5
assert np.all(diff1 < error1)
assert output[1].shape == expect1.shape
expect2 = np.tile(input_32_x2, mul_32_2)
diff2 = output[2].asnumpy() - expect2
error2 = np.ones(shape=expect2.shape) * 1.0e-5
assert np.all(diff2 < error2)
assert output[2].shape == expect2.shape
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard
def test_tile_16():
net = Net16()
output = net()
expect0 = np.tile(input_16_x0, mul_16_0)
diff0 = output[0].asnumpy() - expect0
error0 = np.ones(shape=expect0.shape) * 1.0e-5
assert np.all(diff0 < error0)
assert output[0].shape == expect0.shape
expect1 = np.tile(input_16_x1, mul_16_1)
diff1 = output[1].asnumpy() - expect1
error1 = np.ones(shape=expect1.shape) * 1.0e-5
assert np.all(diff1 < error1)
assert output[1].shape == expect1.shape
expect2 = np.tile(input_16_x2, mul_16_2)
diff2 = output[2].asnumpy() - expect2
error2 = np.ones(shape=expect2.shape) * 1.0e-5
assert np.all(diff2 < error2)
assert output[2].shape == expect2.shape