|
|
@ -13,6 +13,8 @@
|
|
|
|
# limitations under the License.
|
|
|
|
# limitations under the License.
|
|
|
|
# ============================================================================
|
|
|
|
# ============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
from functools import reduce
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
import pytest
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
@ -20,43 +22,67 @@ import mindspore.context as context
|
|
|
|
import mindspore.nn as nn
|
|
|
|
import mindspore.nn as nn
|
|
|
|
from mindspore import Tensor
|
|
|
|
from mindspore import Tensor
|
|
|
|
from mindspore.common import dtype as mstype
|
|
|
|
from mindspore.common import dtype as mstype
|
|
|
|
from mindspore.ops import operations as P
|
|
|
|
import mindspore.ops as ops
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NetArgmax(nn.Cell):
|
|
|
|
class NetArgmax(nn.Cell):
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self, axis=0):
|
|
|
|
super(NetArgmax, self).__init__()
|
|
|
|
super(NetArgmax, self).__init__()
|
|
|
|
axis1 = 0
|
|
|
|
self.argmax = ops.Argmax(axis, output_type=mstype.int32)
|
|
|
|
axis2 = -1
|
|
|
|
|
|
|
|
self.argmax1 = P.Argmax(axis1, output_type=mstype.int32)
|
|
|
|
|
|
|
|
self.argmax2 = P.Argmax(axis2, output_type=mstype.int32)
|
|
|
|
|
|
|
|
self.argmax3 = P.Argmax(output_type=mstype.int32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def construct(self, x):
|
|
|
|
def construct(self, x):
|
|
|
|
return (self.argmax1(x), self.argmax2(x), self.argmax3(x))
|
|
|
|
return self.argmax(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
@pytest.mark.level0
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
def test_argmax():
|
|
|
|
def test_argmax_1d():
|
|
|
|
|
|
|
|
for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
|
|
|
|
|
|
|
|
context.set_context(mode=mode, device_target="GPU")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.array([1., 20., 5.]).astype(np.float32))
|
|
|
|
|
|
|
|
Argmax = NetArgmax(axis=0)
|
|
|
|
|
|
|
|
output = Argmax(x)
|
|
|
|
|
|
|
|
expect = np.array([1]).astype(np.float32)
|
|
|
|
|
|
|
|
assert (output.asnumpy() == expect).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
|
|
|
def test_argmax_2d():
|
|
|
|
|
|
|
|
for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
|
|
|
|
|
|
|
|
context.set_context(mode=mode, device_target="GPU")
|
|
|
|
|
|
|
|
|
|
|
|
x = Tensor(np.array([[1., 20., 5.],
|
|
|
|
x = Tensor(np.array([[1., 20., 5.],
|
|
|
|
[67., 8., 9.],
|
|
|
|
[67., 8., 9.],
|
|
|
|
[130., 24., 15.],
|
|
|
|
[130., 24., 15.],
|
|
|
|
[0.3, -0.4, -15.]]).astype(np.float32))
|
|
|
|
[0.3, -0.4, -15.]]).astype(np.float32))
|
|
|
|
expect1 = np.array([2, 2, 2]).astype(np.int32)
|
|
|
|
Argmax_axis_0 = NetArgmax(axis=0)
|
|
|
|
expect2 = np.array([1, 0, 0, 0]).astype(np.int32)
|
|
|
|
output = Argmax_axis_0(x)
|
|
|
|
|
|
|
|
expect = np.array([2, 2, 2]).astype(np.int32)
|
|
|
|
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
|
|
|
|
assert (output.asnumpy() == expect).all()
|
|
|
|
argmax = NetArgmax()
|
|
|
|
|
|
|
|
output = argmax(x)
|
|
|
|
Argmax_axis_1 = NetArgmax(axis=1)
|
|
|
|
assert (output[0].asnumpy() == expect1).all()
|
|
|
|
output = Argmax_axis_1(x)
|
|
|
|
assert (output[1].asnumpy() == expect2).all()
|
|
|
|
expect = np.array([1, 0, 0, 0]).astype(np.int32)
|
|
|
|
assert (output[2].asnumpy() == expect2).all()
|
|
|
|
assert (output.asnumpy() == expect).all()
|
|
|
|
|
|
|
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
|
|
|
|
|
|
|
argmax1 = NetArgmax()
|
|
|
|
@pytest.mark.level0
|
|
|
|
output1 = argmax1(x)
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
assert (output1[0].asnumpy() == expect1).all()
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
assert (output1[1].asnumpy() == expect2).all()
|
|
|
|
def test_argmax_high_dims():
|
|
|
|
assert (output1[2].asnumpy() == expect2).all()
|
|
|
|
for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
|
|
|
|
|
|
|
|
context.set_context(mode=mode, device_target="GPU")
|
|
|
|
|
|
|
|
for dim in range(3, 10):
|
|
|
|
|
|
|
|
shape = np.random.randint(1, 10, size=dim)
|
|
|
|
|
|
|
|
x = np.random.randn(reduce(lambda x, y: x * y, shape)).astype(np.float32)
|
|
|
|
|
|
|
|
x = x.reshape(shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rnd_axis = random.randint(-dim + 1, dim - 1)
|
|
|
|
|
|
|
|
Argmax = NetArgmax(axis=rnd_axis)
|
|
|
|
|
|
|
|
ms_output = Argmax(Tensor(x))
|
|
|
|
|
|
|
|
np_output = np.argmax(x, axis=rnd_axis)
|
|
|
|
|
|
|
|
assert (ms_output.asnumpy() == np_output).all()
|
|
|
|