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.

tanh.py 2.9 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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:tanh"""
  17. import akg.topi
  18. import akg.tvm
  19. import akg
  20. from akg.ops.math.rec_positive import rec_positive
  21. from akg.utils import validation_check as vc_util
  22. from akg.utils import kernel_exec as utils
  23. @vc_util.check_input_type(akg.tvm.tensor.Tensor)
  24. def tanh(in_data):
  25. """
  26. Compute tanh function. This version is able to avoid exp(x) overflow when x is large.
  27. ..math:`res = sign(in_data) * (1 - exp(-2*abs(in_data))) / (1 + exp(-2*abs(in_data)))`
  28. Args:
  29. in_data (tvm.tensor.Tensor): input tensor of type float16, float32.
  30. Returns:
  31. tvm.tensor.Tensor, has the same type and shape as in_data.
  32. """
  33. vc_util.check_shape(in_data.shape)
  34. dtype = in_data.dtype
  35. vc_util.ops_dtype_check(dtype, vc_util.DtypeForDavinci.ALL_FLOAT)
  36. ori_dtype = dtype
  37. in_data_compute = in_data
  38. if ori_dtype == "float32" and utils.product_is_mini():
  39. in_data_compute = akg.tvm.compute(in_data.shape, lambda *indice: in_data(* \
  40. indice).astype("float16"), name='type_cast')
  41. dtype = 'float16'
  42. in_data_abs = akg.lang.cce.vabs(in_data_compute)
  43. exponent = akg.lang.cce.vmuls(in_data_abs, akg.tvm.const(-2, dtype))
  44. exp_value = akg.lang.cce.vexp(exponent)
  45. exp_value_add_one = akg.lang.cce.vadds(exp_value, akg.tvm.const(1, dtype))
  46. one_sub_exp_value = akg.topi.subtract(akg.tvm.const(1, dtype), exp_value)
  47. exp_value_add_one_rec = rec_positive(exp_value_add_one)
  48. tanh_value_pos = akg.topi.multiply(one_sub_exp_value, exp_value_add_one_rec)
  49. output_shape = in_data_compute.shape
  50. sign = akg.tvm.compute(output_shape,
  51. lambda *indice:
  52. akg.tvm.expr.Select(in_data_compute(*indice) < akg.tvm.const(0, dtype),
  53. akg.tvm.const(-1, dtype), akg.tvm.const(1, dtype)))
  54. tanh_value = akg.topi.multiply(sign, tanh_value_pos)
  55. if ori_dtype == "float32" and utils.product_is_mini():
  56. tanh_value = akg.tvm.compute(tanh_value.shape,
  57. lambda *indice: tanh_value(*indice).astype("float32"),
  58. name='res')
  59. return tanh_value