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_zeroslike_op.py 2.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  25. class NetZerosLike(nn.Cell):
  26. def __init__(self):
  27. super(NetZerosLike, self).__init__()
  28. self.zeros_like = P.ZerosLike()
  29. def construct(self, x):
  30. return self.zeros_like(x)
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_gpu_training
  33. @pytest.mark.env_onecard
  34. def test_ZerosLike():
  35. x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  36. x1_np = np.random.uniform(-2, 2, 1).astype(np.float32)
  37. x0 = Tensor(x0_np)
  38. x1 = Tensor(x1_np)
  39. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  40. zeros_like = NetZerosLike()
  41. output0 = zeros_like(x0)
  42. expect0 = np.zeros_like(x0_np)
  43. diff0 = output0.asnumpy() - expect0
  44. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  45. assert np.all(diff0 < error0)
  46. assert (output0.shape() == expect0.shape)
  47. output1 = zeros_like(x1)
  48. expect1 = np.zeros_like(x1_np)
  49. diff1 = output1.asnumpy() - expect1
  50. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  51. assert np.all(diff1 < error1)
  52. assert (output1.shape() == expect1.shape)
  53. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  54. zeros_like = NetZerosLike()
  55. output0 = zeros_like(x0)
  56. expect0 = np.zeros_like(x0_np)
  57. diff0 = output0.asnumpy() - expect0
  58. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  59. assert np.all(diff0 < error0)
  60. assert (output0.shape() == expect0.shape)
  61. output1 = zeros_like(x1)
  62. expect1 = np.zeros_like(x1_np)
  63. diff1 = output1.asnumpy() - expect1
  64. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  65. assert np.all(diff1 < error1)
  66. assert (output1.shape() == expect1.shape)