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.py 2.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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"""
  17. import akg.tvm
  18. import akg
  19. import akg.lang.cce
  20. from akg.ops.math.cast import cast
  21. from akg.ops.math.sub import sub
  22. from akg.utils import validation_check as vc_util
  23. from akg.utils import kernel_exec as utils
  24. @vc_util.check_input_type(akg.tvm.tensor.Tensor, akg.tvm.tensor.Tensor)
  25. def equal(input1, input2):
  26. """
  27. check whether input1 equals to input2.
  28. Args:
  29. input1 (tvm.tensor.Tensor): input argument has type float16, float32 and int32.
  30. input2 (tvm.tensor.Tensor): input argument has type float16, float32 and int32.
  31. Returns:
  32. tvm.tensor.Tensor. If input1 equal to input2 return True, else return False.
  33. """
  34. # check shapes
  35. shape1 = [x.value for x in input1.shape]
  36. shape2 = [x.value for x in input2.shape]
  37. shapes = [shape1, shape2]
  38. for _, shp in enumerate(shapes):
  39. vc_util.check_shape(shp)
  40. vc_util.ops_dtype_check([input1.dtype, input2.dtype],
  41. [vc_util.DtypeForDavinci.ALL_FLOAT, vc_util.DtypeForDavinci.INT32,
  42. vc_util.DtypeForDavinci.INT8, vc_util.DtypeForDavinci.UINT8])
  43. dtype = input1.dtype
  44. orig_dtype = dtype
  45. if utils.product_is_mini() and dtype != "float16":
  46. dtype = "float16"
  47. if (not utils.product_is_mini()) and dtype not in ("float16", "float32"):
  48. # for int32, if cast to float16, there may be overflow
  49. dtype = "float32"
  50. if orig_dtype == "float32" and dtype == "float16":
  51. input_sub = sub(input1, input2)
  52. input_sub = cast(input_sub, dtype)
  53. zero = akg.tvm.const(0.0, dtype)
  54. res = akg.topi.equal(input_sub, zero)
  55. else:
  56. input1 = cast(input1, dtype)
  57. input2 = cast(input2, dtype)
  58. res = akg.topi.equal(input1, input2)
  59. return res