You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_expand_dims.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.expand_dims = P.ExpandDims()
  26. def construct(self, tensor):
  27. return self.expand_dims(tensor, -1)
  28. class NetDynamic(nn.Cell):
  29. def __init__(self):
  30. super(NetDynamic, self).__init__()
  31. self.conv = inner.GpuConvertToDynamicShape()
  32. self.expand_dims = P.ExpandDims()
  33. def construct(self, x):
  34. x_conv = self.conv(x)
  35. return self.expand_dims(x_conv, -1)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_net_bool():
  40. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  41. x = np.random.randn(1, 16, 1, 1).astype(np.bool)
  42. net = NetDynamic()
  43. output = net(Tensor(x))
  44. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  45. @pytest.mark.level0
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.env_onecard
  48. def test_net_int8():
  49. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  50. x = np.random.randn(1, 16, 1, 1).astype(np.int8)
  51. net = NetDynamic()
  52. output = net(Tensor(x))
  53. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_gpu_training
  56. @pytest.mark.env_onecard
  57. def test_net_uint8():
  58. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  59. x = np.random.randn(1, 16, 1, 1).astype(np.uint8)
  60. net = Net()
  61. output = net(Tensor(x))
  62. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_training
  65. @pytest.mark.env_onecard
  66. def test_net_int16():
  67. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  68. x = np.random.randn(1, 16, 1, 1).astype(np.int16)
  69. net = Net()
  70. output = net(Tensor(x))
  71. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_gpu_training
  74. @pytest.mark.env_onecard
  75. def test_net_int32():
  76. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  77. x = np.random.randn(1, 16, 1, 1).astype(np.int32)
  78. net = Net()
  79. output = net(Tensor(x))
  80. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_net_int64():
  85. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  86. x = np.random.randn(1, 16, 1, 1).astype(np.int64)
  87. net = Net()
  88. output = net(Tensor(x))
  89. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  90. @pytest.mark.level0
  91. @pytest.mark.platform_x86_gpu_training
  92. @pytest.mark.env_onecard
  93. def test_net_float16():
  94. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  95. x = np.random.randn(1, 16, 1, 1).astype(np.float16)
  96. net = Net()
  97. output = net(Tensor(x))
  98. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  99. @pytest.mark.level0
  100. @pytest.mark.platform_x86_gpu_training
  101. @pytest.mark.env_onecard
  102. def test_net_float32():
  103. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  104. x = np.random.randn(1, 16, 1, 1).astype(np.float32)
  105. net = Net()
  106. output = net(Tensor(x))
  107. assert np.all(output.asnumpy() == np.expand_dims(x, -1))
  108. @pytest.mark.level0
  109. @pytest.mark.platform_x86_gpu_training
  110. @pytest.mark.env_onecard
  111. def test_net_float64():
  112. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  113. x = np.random.randn(1, 16, 1, 1).astype(np.float64)
  114. net = Net()
  115. output = net(Tensor(x))
  116. assert np.all(output.asnumpy() == np.expand_dims(x, -1))