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_identity_op.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 2021 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, ops
  20. class Net(nn.Cell):
  21. def __init__(self):
  22. super(Net, self).__init__()
  23. self.identity = ops.Identity()
  24. def construct(self, x):
  25. return self.identity(x)
  26. def generate_testcases(nptype):
  27. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  28. x = np.random.randn(3, 4, 5, 6).astype(nptype)
  29. net = Net()
  30. input_tensor = Tensor(x)
  31. output = net(input_tensor)
  32. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  33. assert id(input_tensor) != id(output)
  34. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  35. x = np.random.randn(3, 4, 5, 6).astype(nptype)
  36. net = Net()
  37. input_tensor = Tensor(x)
  38. output = net(input_tensor)
  39. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  40. assert id(input_tensor) != id(output)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_training
  43. @pytest.mark.env_onecard
  44. def test_identity_float64():
  45. generate_testcases(np.float64)
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_identity_float32():
  50. generate_testcases(np.float32)
  51. @pytest.mark.level1
  52. @pytest.mark.platform_x86_gpu_training
  53. @pytest.mark.env_onecard
  54. def test_identity_float16():
  55. generate_testcases(np.float16)
  56. @pytest.mark.level1
  57. @pytest.mark.platform_x86_gpu_training
  58. @pytest.mark.env_onecard
  59. def test_identity_uint64():
  60. generate_testcases(np.uint64)
  61. @pytest.mark.level1
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_identity_int64():
  65. generate_testcases(np.int64)
  66. @pytest.mark.level1
  67. @pytest.mark.platform_x86_gpu_training
  68. @pytest.mark.env_onecard
  69. def test_identity_uint32():
  70. generate_testcases(np.uint32)
  71. @pytest.mark.level1
  72. @pytest.mark.platform_x86_gpu_training
  73. @pytest.mark.env_onecard
  74. def test_identity_int32():
  75. generate_testcases(np.int32)
  76. @pytest.mark.level1
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_identity_uint16():
  80. generate_testcases(np.uint16)
  81. @pytest.mark.level1
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_identity_int16():
  85. generate_testcases(np.int16)
  86. @pytest.mark.level1
  87. @pytest.mark.platform_x86_gpu_training
  88. @pytest.mark.env_onecard
  89. def test_identity_uint8():
  90. generate_testcases(np.uint8)
  91. @pytest.mark.level1
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_identity_int8():
  95. generate_testcases(np.int8)
  96. @pytest.mark.level1
  97. @pytest.mark.platform_x86_gpu_training
  98. @pytest.mark.env_onecard
  99. def test_identity_bool():
  100. generate_testcases(np.bool)