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.

reduce_sum.py 1.8 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """operator dsl function: sum"""
  15. import akg.topi
  16. import akg.tvm
  17. from akg.utils import format_transform as ft_util
  18. from akg.utils import validation_check as vc_util
  19. @vc_util.check_input_type(akg.tvm.tensor.Tensor, (list, tuple, int, type(None)), (bool, type(None)))
  20. def reduce_sum(inputs, axis=None, keepdims=False):
  21. """
  22. Compute the sum of elements across dimensions of a tensor.
  23. Args:
  24. inputs (tvm.tensor.Tensor): Tensor.
  25. axis (Union[list, tuple, int, None]): If the list or tuple is empty, the axis equal to None.
  26. keepdims (bool): If keepdims equal to True, the result shape length is same to input shape length.
  27. Returns:
  28. tvm.tensor.Tensor, has same type as input. If keepdims is True, all reduced dimensions are retained
  29. with length 1, else these reduced axis will be eliminate.
  30. """
  31. axis = ft_util.refine_reduce_axis(inputs, axis)
  32. vc_util.check_shape(inputs.shape)
  33. in_dtype = inputs.dtype
  34. if in_dtype == 'float16':
  35. inputs = akg.topi.cast(inputs, 'float32')
  36. output = akg.topi.sum(inputs, axis=axis, keepdims=keepdims)
  37. if in_dtype == 'float16':
  38. output = akg.topi.cast(output, 'float16')
  39. return output