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.

equal_count.py 2.8 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. # Copyright 2019 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """operator dsl function: equal_count"""
  17. import akg.topi
  18. import akg.tvm
  19. from akg.ops.math.cast import cast
  20. from akg.utils.kernel_exec import product_is_mini
  21. from akg.ops.math.sum import sum_value
  22. from akg.utils import validation_check as vc_util
  23. from akg.utils.dsl_create import produce_shapes
  24. from akg.utils.format_transform import get_shape
  25. @vc_util.check_input_type(akg.tvm.tensor.Tensor, akg.tvm.tensor.Tensor)
  26. def equal_count(x, y):
  27. """
  28. compute equal num of x and y.
  29. Args:
  30. x (tvm.tensor.Tensor): Tensor of type int32.
  31. y (tvm.tensor.Tensor): Tensor of type int32.
  32. Returns:
  33. tvm.tensor.Tensor, equal num, type is int32.
  34. """
  35. # check shapes
  36. shape1 = get_shape(x)
  37. shape2 = get_shape(y)
  38. shapes = [shape1, shape2]
  39. for _, shape_ in enumerate(shapes):
  40. vc_util.check_shape(shape_)
  41. if len(shape1) != 1 or len(shape2) != 1:
  42. raise RuntimeError("Two inputs should all be one dim!")
  43. # check types
  44. dtype = x.dtype
  45. vc_util.ops_dtype_check([x.dtype, y.dtype], vc_util.DtypeForDavinci.INT32)
  46. # Due to instruction limitations, the int32 data needs to be converted to
  47. # float16 or float32.
  48. # When the int32 data is casted to float16, there may be overflow problems,
  49. # so as far as possible the int32 data is casted to float32.
  50. orig_dtype = dtype
  51. if product_is_mini():
  52. dtype = "float16"
  53. else:
  54. dtype = "float32"
  55. x = cast(x, dtype)
  56. y = cast(y, dtype)
  57. shape1, shape2, shape = produce_shapes(shape1, shape2)
  58. t = akg.tvm.compute(shape, lambda *indice: akg.tvm.const(1, dtype), "t")
  59. f = akg.tvm.compute(shape, lambda *indice: akg.tvm.const(0, dtype), "f")
  60. x = akg.topi.broadcast_to(x, shape)
  61. y = akg.topi.broadcast_to(y, shape)
  62. z = akg.tvm.compute(shape,
  63. lambda *indice: akg.tvm.expr.Select(x[indice] == y[indice],
  64. t[indice],
  65. f[indice]),
  66. name="z")
  67. res, _ = sum_value(z)
  68. if res.dtype != orig_dtype:
  69. res = cast(res, orig_dtype)
  70. return res