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.

square_impl.py 2.9 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2020 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. from __future__ import absolute_import
  16. import te.lang.cce
  17. from te import tvm
  18. from te.platform.fusion_manager import fusion_manager
  19. from topi import generic
  20. from topi.cce import util
  21. from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType
  22. @fusion_manager.register("square")
  23. def square_compute(input_x, output_y):
  24. """
  25. algorithm: square
  26. calculating data's square,y= x*x
  27. Parameters
  28. ----------
  29. input_x: TVM tensor
  30. the placeholder of input data
  31. output_y: dict
  32. shape and dtype of output, should be same shape and type as input
  33. kernel_name: str
  34. cce kernel name, default value is square
  35. Returns
  36. -------
  37. res : tvm.tensor
  38. the result of square
  39. """
  40. res = te.lang.cce.vmul(input_x, input_x)
  41. return res
  42. cus_square_op_info = TBERegOp("CusSquare") \
  43. .fusion_type("OPAQUE") \
  44. .async_flag(False) \
  45. .binfile_name("square.so") \
  46. .compute_cost(10) \
  47. .kernel_name("CusSquareImpl") \
  48. .partial_flag(True) \
  49. .input(0, "x", False, "required", "all") \
  50. .output(0, "y", False, "required", "all") \
  51. .dtype_format(DataType.F32_Default, DataType.F32_Default) \
  52. .dtype_format(DataType.F16_Default, DataType.F16_Default) \
  53. .get_op_info()
  54. @op_info_register(cus_square_op_info)
  55. def CusSquareImpl(input_x, output_y, kernel_name="CusSquareImpl"):
  56. """
  57. algorithm: square
  58. calculating data's square,y= x*x
  59. Parameters
  60. ----------
  61. input_x : dict
  62. shape and dtype of input, only support float32
  63. output_y: dict
  64. shape and dtype of output, should be same shape and type as input
  65. kernel_name : str
  66. kernel name, default value is "square"
  67. Returns
  68. -------
  69. None
  70. """
  71. shape = input_x.get("shape")
  72. dtype = input_x.get("dtype").lower()
  73. shape = util.shape_refine(shape)
  74. data = tvm.placeholder(shape, name="data", dtype=dtype.lower())
  75. with tvm.target.cce():
  76. res = square_compute(data, output_y)
  77. sch = generic.auto_schedule(res)
  78. config = {"print_ir": False,
  79. "name": kernel_name,
  80. "tensor_list": [data, res]}
  81. te.lang.cce.cce_build_code(sch, config)