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_pyfunc_op.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. """ test loss """
  16. import numpy as np
  17. import pytest
  18. import mindspore as ms
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore import Tensor
  22. from mindspore.ops import operations as P
  23. def func_single_output(x1, x2):
  24. return x1 - x2
  25. def func_multi_output(x1, x2):
  26. return (x1 + x2), (x1 - x2)
  27. output = 0
  28. def func_no_output(x1, x2):
  29. global output
  30. output = x1 + x2
  31. class PyFuncNet(nn.Cell):
  32. def __init__(self, fn, in_types, in_shapes, out_types, out_shapes):
  33. super().__init__()
  34. self.func = P.PyFunc(fn, in_types, in_shapes, out_types, out_shapes)
  35. self.relu = P.ReLU()
  36. def construct(self, x1, x2):
  37. x = self.func((x1, x2))
  38. return self.relu(x[0])
  39. def func_with_dtype(ms_dtype, np_dtype):
  40. shape = (40, 40)
  41. np.random.seed(42)
  42. x1 = np.random.randint(-5, 5, size=shape).astype(np_dtype)
  43. x2 = np.random.randint(-5, 5, size=shape).astype(np_dtype)
  44. expect = func_single_output(x1, x2)
  45. expect = P.ReLU()(Tensor(expect))
  46. net = PyFuncNet(func_single_output, [ms_dtype, ms_dtype], [shape, shape], [ms_dtype], [shape])
  47. x = net(Tensor(x1), Tensor(x2))
  48. assert np.allclose(x.asnumpy(), expect.asnumpy())
  49. @pytest.mark.level0
  50. @pytest.mark.platform_x86_gpu_training
  51. @pytest.mark.env_onecard
  52. def test_pyfunc_single_output():
  53. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  54. func_with_dtype(ms.float16, np.float16)
  55. func_with_dtype(ms.float32, np.float32)
  56. func_with_dtype(ms.float64, np.float64)
  57. func_with_dtype(ms.int32, np.int32)
  58. func_with_dtype(ms.int64, np.int64)
  59. @pytest.mark.level0
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.env_onecard
  62. def test_pyfunc_multi_output():
  63. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  64. shape = (40, 40)
  65. dtype = ms.float32
  66. np.random.seed(42)
  67. x1 = np.random.randint(-5, 5, size=shape).astype(np.float32)
  68. x2 = np.random.randint(-5, 5, size=shape).astype(np.float32)
  69. expect, _ = func_multi_output(x1, x2)
  70. expect = P.ReLU()(Tensor(expect))
  71. net = PyFuncNet(func_multi_output, [dtype, dtype], [shape, shape], [dtype, dtype], [shape, shape])
  72. x = net(Tensor(x1), Tensor(x2))
  73. assert np.allclose(x.asnumpy(), expect.asnumpy())
  74. class PyFuncGraph(nn.Cell):
  75. def __init__(self, fn, in_types, in_shapes, out_types, out_shapes):
  76. super().__init__()
  77. self.func = P.PyFunc(fn, in_types, in_shapes, out_types, out_shapes)
  78. def construct(self, x1, x2):
  79. return self.func((x1, x2))
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_gpu_training
  82. @pytest.mark.env_onecard
  83. def test_pyfunc_no_output():
  84. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  85. shape = (40, 40)
  86. dtype = ms.float32
  87. np.random.seed(42)
  88. x1 = np.random.randint(-5, 5, size=shape).astype(np.float32)
  89. x2 = np.random.randint(-5, 5, size=shape).astype(np.float32)
  90. func_no_output(x1, x2)
  91. global output
  92. expect = output
  93. net = PyFuncGraph(func_no_output, [dtype, dtype], [shape, shape], [], [])
  94. net(Tensor(x1), Tensor(x2))
  95. net_output = output
  96. assert np.allclose(net_output, expect)
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_gpu_training
  99. @pytest.mark.env_onecard
  100. def test_pyfunc_scalar():
  101. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  102. shape = ()
  103. ms_dtype = ms.int32
  104. x1 = int(10)
  105. x2 = int(5)
  106. expect = func_single_output(x1, x2)
  107. net = PyFuncGraph(func_single_output, [ms_dtype, ms_dtype], [shape, shape], [ms_dtype], [shape])
  108. x = net(Tensor(x1), Tensor(x2))
  109. assert np.allclose(x[0].asnumpy(), expect)