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_user_define.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 inspect
  16. import numpy as np
  17. import pytest
  18. from mindspore import context, ops, Tensor
  19. from mindspore.common import dtype as mstype
  20. from mindspore.nn import Cell
  21. class UserDefined(ops.PrimitiveWithInfer):
  22. def __init__(self, func, shape, dtype, func_type=None):
  23. ops.PrimitiveWithInfer.__init__(self, "UserDefined")
  24. self.add_prim_attr('akg', True)
  25. if "__wrapped__" in func.__dict__:
  26. func = func.__dict__["__wrapped__"]
  27. func_name = func.__name__
  28. self.add_prim_attr('func_name', func_name)
  29. func_source_str = inspect.getsource(func)
  30. if func_type is None:
  31. if "ir_builder" in func_source_str:
  32. func_type = "ir_builder"
  33. elif "compute" in func_source_str:
  34. func_type = "tvm_compute"
  35. else:
  36. func_type = "hybrid"
  37. self.add_prim_attr('func_source_str', func_source_str)
  38. self.add_prim_attr('func_type', func_type)
  39. self._shape = shape
  40. self._dtype = dtype
  41. def infer_shape(self, *args):
  42. if callable(self._shape):
  43. return self._shape(*args)
  44. return self._shape
  45. def infer_dtype(self, *args):
  46. if callable(self._dtype):
  47. return self._dtype(*args)
  48. return self._dtype
  49. def outer_product(a, b):
  50. c = output_tensor((a.shape[0], b.shape[1]), 'float32')
  51. for i0 in range(a.shape[0]):
  52. for i1 in range(b.shape[1]):
  53. c[i0, i1] = 0.0
  54. for i2 in range(a.shape[1]):
  55. c[i0, i1] = c[i0, i1] + (a[i0, i2] * b[i2, i1])
  56. return c
  57. class TestHybrid(Cell):
  58. def __init__(self):
  59. super(TestHybrid, self).__init__()
  60. def infer_func(x, y):
  61. return x
  62. self.program = UserDefined(
  63. outer_product, shape=infer_func, dtype=infer_func)
  64. def construct(self, x, y):
  65. return self.program(x, y)
  66. def v_add(inputs, attrs):
  67. def vadd_func(dst, data_1, data_2):
  68. ib = tvm.ir_builder.create()
  69. with ib.for_range_n(data_1.shape, "i") as i:
  70. ib.store(dst, i, ib.load(data_1, i) + ib.load(data_2, i))
  71. return ib.get()
  72. data_1, data_2 = inputs[0], inputs[1]
  73. return tvm.extern(data_1.shape, [data_1, data_2],
  74. lambda ins, outs: vadd_func(outs[0], ins[0], ins[1]),
  75. name="v_add", dtype=data_1.dtype)
  76. class TestIRbuilder(Cell):
  77. def __init__(self, shape):
  78. super(TestIRbuilder, self).__init__()
  79. self.program = UserDefined(
  80. v_add, shape=shape, dtype=mstype.float16)
  81. def construct(self, x, y):
  82. return self.program(x, y)
  83. def test_user_defined_hybrid():
  84. input_x = np.random.normal(0, 1, [4, 4]).astype(np.float32)
  85. input_y = np.random.normal(0, 1, [4, 4]).astype(np.float32)
  86. test = TestHybrid()
  87. output = test(Tensor(input_x), Tensor(input_y))
  88. expect = np.matmul(input_x, input_y)
  89. assert np.allclose(expect, output.asnumpy(), 0.001, 0.001)
  90. def test_user_defined_irbuider():
  91. shape = (4, 5)
  92. input_x = np.random.normal(0, 1, shape).astype(np.float16)
  93. input_y = np.random.normal(0, 1, shape).astype(np.float16)
  94. test = TestIRbuilder(shape)
  95. output = test(Tensor(input_x), Tensor(input_y))
  96. assert np.allclose(input_x + input_y, output.asnumpy(), 0.001, 0.001)
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_gpu_training
  99. @pytest.mark.env_onecard
  100. def test_user_defined_gpu():
  101. context.set_context(mode=0, enable_graph_kernel=True)
  102. test_user_defined_hybrid()
  103. test_user_defined_irbuider()