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.

gather_v2.py 4.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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: gather_v2"""
  17. import akg.tvm
  18. from akg.utils import validation_check as vc_util
  19. from akg.utils.format_transform import get_shape
  20. from akg.utils import custom_tiling as ct_util
  21. attrs = {
  22. "RewriteVarTensorIdx": True,
  23. "enable_double_buffer": False,
  24. }
  25. gather_v2_set_dim_map = {
  26. }
  27. def gather_v2_set_dim_func(params, indices, axis):
  28. """set dim info for attr"""
  29. key = []
  30. key.append(tuple(params.shape))
  31. key.append(tuple(indices.shape))
  32. key.append(axis)
  33. key.append(params.dtype)
  34. key.append(indices.dtype)
  35. hash_key = str(tuple(key))
  36. if hash_key in gather_v2_set_dim_map.keys():
  37. return ct_util.set_dims(gather_v2_set_dim_map[hash_key]), hash_key
  38. return "", hash_key
  39. def gather_tiling_strategy(data, axis):
  40. """Custom tiling strategy for gather op"""
  41. strategy = list()
  42. base = 0
  43. for priority_value, pos in enumerate(range(len(data.shape) - 1, axis, -1)):
  44. priority_value = priority_value + base
  45. strategy.append(ct_util.create_constraint_on_tensor(tensor=data,
  46. values=priority_value,
  47. constraints=ct_util.TileConstraint.SET_PRIORITY,
  48. tensor_pos=pos)[0])
  49. return strategy
  50. @vc_util.check_input_type(akg.tvm.tensor.Tensor, akg.tvm.tensor.Tensor, (int, type(None)))
  51. def gather_v2(params, indices, axis=0):
  52. """
  53. Select tensor of related dimensions.
  54. Note:
  55. Each entry in indices must be an index in [0, params.shape[axis]).
  56. Args:
  57. params (tvm.tensor.Tensor): Data to be gathered. Types: int8, int32, float16, float32.
  58. indices (tvm.tensor.Tensor): A 1-D tensor for index. Types: int32.
  59. Each entry in indices must be an index in [0, params.shape[axis]).
  60. axis (int): Axis along which index of params be applied. Default: 0.
  61. Returns:
  62. tvm.tensor.Tensor, which indexes the input tensor along dimension dim (axis) using the entries in index.
  63. """
  64. input_shape = get_shape(params)
  65. indices_shape = get_shape(indices)
  66. vc_util.check_shape(params.shape, tensor_name="params")
  67. vc_util.check_shape(indices.shape, length=1, tensor_name="indices")
  68. vc_util.ops_dtype_check(params.dtype, [vc_util.DtypeForDavinci.ALL_FLOAT, vc_util.DtypeForDavinci.ALL_INT])
  69. vc_util.ops_dtype_check(indices.dtype, vc_util.DtypeForDavinci.INT32)
  70. axis_num = len(input_shape)
  71. vc_util.check_value_on_integer("axis", axis, -axis_num, axis_num)
  72. if axis < 0:
  73. axis = axis_num + axis
  74. def _get_output_shape():
  75. out_shape = []
  76. for i, in_shape in enumerate(input_shape):
  77. if i != axis:
  78. out_shape.append(in_shape)
  79. else:
  80. for indice_shape in indices_shape:
  81. out_shape.append(indice_shape)
  82. return out_shape
  83. def _get_input_index(output_index):
  84. input_index = []
  85. indices_len = len(indices_shape)
  86. axis_input_index = indices[output_index[axis:axis + indices_len]]
  87. for i in range(axis):
  88. input_index.append(output_index[i])
  89. input_index.append(axis_input_index)
  90. for i in range(axis + 1, len(output_index)):
  91. input_index.append(output_index[i])
  92. return input_index
  93. output_shape = _get_output_shape()
  94. output = akg.tvm.compute(
  95. output_shape, lambda *indices_output: params(*_get_input_index(
  96. indices_output)), name="gather_output")
  97. dim_info = gather_v2_set_dim_func(params, indices, axis)[0]
  98. if dim_info != "":
  99. attrs['dim'] = dim_info
  100. attrs["custom_tiling"] = gather_tiling_strategy(params, axis)
  101. attrs["enable_feature_library"] = True
  102. return output, attrs