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_flatten_op.py 4.5 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. class NetFlatten(nn.Cell):
  22. def __init__(self):
  23. super(NetFlatten, self).__init__()
  24. self.flatten = P.Flatten()
  25. def construct(self, x):
  26. return self.flatten(x)
  27. class NetAllFlatten(nn.Cell):
  28. def __init__(self):
  29. super(NetAllFlatten, self).__init__()
  30. self.flatten = P.Flatten()
  31. def construct(self, x):
  32. loop_count = 4
  33. while loop_count > 0:
  34. x = self.flatten(x)
  35. loop_count = loop_count - 1
  36. return x
  37. class NetFirstFlatten(nn.Cell):
  38. def __init__(self):
  39. super(NetFirstFlatten, self).__init__()
  40. self.flatten = P.Flatten()
  41. self.relu = P.ReLU()
  42. def construct(self, x):
  43. loop_count = 4
  44. while loop_count > 0:
  45. x = self.flatten(x)
  46. loop_count = loop_count - 1
  47. x = self.relu(x)
  48. return x
  49. class NetLastFlatten(nn.Cell):
  50. def __init__(self):
  51. super(NetLastFlatten, self).__init__()
  52. self.flatten = P.Flatten()
  53. self.relu = P.ReLU()
  54. def construct(self, x):
  55. loop_count = 4
  56. x = self.relu(x)
  57. while loop_count > 0:
  58. x = self.flatten(x)
  59. loop_count = loop_count - 1
  60. return x
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_flatten():
  65. x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32))
  66. expect = np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32)
  67. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  68. flatten = NetFlatten()
  69. output = flatten(x)
  70. assert (output.asnumpy() == expect).all()
  71. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  72. flatten = NetFlatten()
  73. output = flatten(x)
  74. assert (output.asnumpy() == expect).all()
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_gpu_training
  77. @pytest.mark.env_onecard
  78. def test_all_flatten():
  79. x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32))
  80. expect = np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32)
  81. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  82. flatten = NetAllFlatten()
  83. output = flatten(x)
  84. assert (output.asnumpy() == expect).all()
  85. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  86. flatten = NetAllFlatten()
  87. output = flatten(x)
  88. assert (output.asnumpy() == expect).all()
  89. @pytest.mark.level0
  90. @pytest.mark.platform_x86_gpu_training
  91. @pytest.mark.env_onecard
  92. def test_first_flatten():
  93. x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32))
  94. expect = np.array([[0, 0.3, 3.6], [0.4, 0.5, 0]]).astype(np.float32)
  95. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  96. flatten = NetFirstFlatten()
  97. output = flatten(x)
  98. assert (output.asnumpy() == expect).all()
  99. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  100. flatten = NetFirstFlatten()
  101. output = flatten(x)
  102. assert (output.asnumpy() == expect).all()
  103. @pytest.mark.level0
  104. @pytest.mark.platform_x86_gpu_training
  105. @pytest.mark.env_onecard
  106. def test_last_flatten():
  107. x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype(np.float32))
  108. expect = np.array([[0, 0.3, 3.6], [0.4, 0.5, 0]]).astype(np.float32)
  109. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  110. flatten = NetLastFlatten()
  111. output = flatten(x)
  112. assert (output.asnumpy() == expect).all()
  113. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  114. flatten = NetLastFlatten()
  115. output = flatten(x)
  116. assert (output.asnumpy() == expect).all()