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.

cast.py 3.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: cast"""
  17. import akg.tvm
  18. import akg.topi
  19. from akg.utils import kernel_exec as utils
  20. from akg.utils import validation_check as vc_util
  21. from akg.utils.format_transform import get_shape
  22. @vc_util.check_input_type(akg.tvm.tensor.Tensor, str)
  23. def cast(data, dst_type):
  24. """
  25. cast data to target type.
  26. Args:
  27. data (tvm.tensor.Tensor): Tensor to be casted, type can be float32, float16, int32, int8, uint8 and bool.
  28. dst_type (str): target cast type.
  29. Returns:
  30. tvm.tensor.Tensor, type is dst_type.
  31. """
  32. ori_type = data.dtype
  33. shape = get_shape(data)
  34. # dtype check
  35. dst_check_list = ["int8", "float32", "float16", "uint8", "int32"]
  36. if dst_type not in dst_check_list:
  37. raise RuntimeError("cast only support cast to %s while dtype is %s" %
  38. (",".join(dst_check_list), dst_type))
  39. if utils.product_is_mini():
  40. # product mini has not conv insn between float32 and int32.
  41. if ori_type == "float32" and dst_type == "int32":
  42. tmp = akg.topi.cast(data, "float16")
  43. return akg.topi.cast(tmp, dst_type)
  44. if ori_type == "int32" and dst_type == "float32":
  45. tmp = akg.topi.cast(data, "float16")
  46. return akg.topi.cast(tmp, dst_type)
  47. dtype_pair = (ori_type, dst_type)
  48. support_dtype = (('float32', 'float16'), ('float16', 'float32'), ('float16', 'int8'), ('float16', 'uint8'),
  49. ('int32', 'float16'), ('int32', 'float32'), ('float16', 'int32'), ('float32', 'int32'),
  50. ('uint8', 'float16'), ('int8', 'float16'))
  51. tmp_trans_dtype = (('int8', 'float32'), ('float32', 'int8'), ('bool', 'float32'), ('uint8', 'float32'),
  52. ('uint8', 'int32'), ('bool', 'int32'), ('float32', 'uint8'))
  53. if dtype_pair not in support_dtype and dtype_pair not in tmp_trans_dtype and ori_type != dst_type:
  54. raise RuntimeError("Don't support cast from ", ori_type, " to ", dst_type)
  55. need_tmp_transfer = dtype_pair in tmp_trans_dtype
  56. if need_tmp_transfer:
  57. if data.dtype == 'float32' and dst_type == 'int8' and not utils.product_is_mini():
  58. tmp = akg.tvm.compute(shape, lambda *indice: akg.tvm.trunc(data(*indice)).astype('int32'))
  59. tmp = akg.topi.cast(tmp, 'float16')
  60. out = akg.tvm.compute(shape, lambda *indice: akg.tvm.trunc(tmp(*indice)).astype(dst_type))
  61. else:
  62. tmp = akg.topi.cast(data, 'float16')
  63. out = akg.topi.cast(tmp, dst_type)
  64. else:
  65. if data.dtype in ('float16', 'float32') and dst_type in ('int8, int32') and not utils.product_is_mini():
  66. out = akg.tvm.compute(shape, lambda *indice: akg.tvm.trunc(data(*indice)).astype(dst_type))
  67. else:
  68. out = akg.topi.cast(data, dst_type)
  69. return out