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.

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: sum"""
  17. import akg.topi
  18. import akg.tvm
  19. from akg.utils import format_transform as ft_util
  20. from akg.utils import validation_check as vc_util
  21. from akg.utils.format_transform import get_shape
  22. from akg.ops.math.cast import cast
  23. def get_attrs():
  24. """get attrs."""
  25. attr_map = {"enable_bisect_optimize": True}
  26. return attr_map
  27. @vc_util.check_input_type(akg.tvm.tensor.Tensor, (list, tuple, int, type(None)), (bool, type(None)))
  28. def sum_value(inputs, axis=None, keepdims=False):
  29. """
  30. Computes the sum value of a tensor along the given axes.
  31. Args:
  32. inputs (tvm.tensor.Tensor): Tensor of type float16, float32.
  33. axis (Union[list, tuple, int, None]): Specifies which axis or axes to reduce.
  34. keepdims (bool): If true, the dimension specified by axis will be one.
  35. Returns:
  36. tvm.tensor.Tensor with same type as input tensor.
  37. """
  38. # Check types
  39. dtype = inputs.dtype
  40. vc_util.ops_dtype_check(dtype, vc_util.DtypeForDavinci.ALL_FLOAT)
  41. axis = ft_util.refine_reduce_axis(inputs, axis)
  42. vc_util.check_shape(inputs.shape)
  43. if not axis:
  44. output = akg.topi.identity(inputs)
  45. else:
  46. output = akg.topi.sum(inputs, axis=axis, keepdims=keepdims)
  47. attr_map = get_attrs()
  48. return output, attr_map
  49. @vc_util.check_input_type(akg.tvm.tensor.Tensor, (list, tuple, int, type(None)), (bool, type(None)))
  50. def sum_v2(inputs, axis=None, keepdims=True):
  51. """another implementation of sum with topi api."""
  52. dtype = inputs.dtype
  53. vc_util.ops_dtype_check(dtype, vc_util.DtypeForDavinci.ALL_FLOAT)
  54. axis = ft_util.refine_reduce_axis(inputs, axis)
  55. vc_util.check_shape(inputs.shape)
  56. if not axis:
  57. output = akg.topi.identity(inputs)
  58. else:
  59. if dtype == "float16":
  60. step_sum = cast(inputs, "float32")
  61. else:
  62. step_sum = inputs
  63. step_sum = akg.topi.sum(step_sum, axis=axis, keepdims=keepdims)
  64. if dtype == "float16":
  65. output = cast(step_sum, "float16")
  66. else:
  67. output = step_sum
  68. attr_map = get_attrs()
  69. return output, attr_map
  70. def sum_by_shape(broadcast_data, original_shape):
  71. """sum the broadcast_data by original shape; gradient for Broadcast."""
  72. broadcast_shape = get_shape(broadcast_data)
  73. original_shape = get_shape(original_shape)
  74. if broadcast_shape == original_shape:
  75. return broadcast_data
  76. if original_shape == [1]:
  77. data, _ = sum_value(broadcast_data)
  78. return data
  79. vc_util.broadcast_check(original_shape, broadcast_shape)
  80. axis_len = len(broadcast_shape) - len(original_shape)
  81. if axis_len > 0:
  82. axis = list(range(axis_len))
  83. broadcast_data, _ = sum_value(broadcast_data, axis, False)
  84. broadcast_shape = get_shape(broadcast_data)
  85. axis = []
  86. for i, _ in enumerate(original_shape):
  87. if original_shape[i] != broadcast_shape[i]:
  88. axis.append(i)
  89. res = sum_value(broadcast_data, axis, True)[0] if axis else broadcast_data
  90. return res