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.

reshape.py 1.9 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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: reshape"""
  15. import akg
  16. import akg.topi
  17. from akg.utils import validation_check as vc_util
  18. from akg.utils.format_transform import get_shape
  19. from functools import reduce
  20. @vc_util.check_input_type(akg.tvm.tensor.Tensor, (list, tuple))
  21. def reshape(data, out_shape):
  22. """
  23. Rearranges input tensor data to new shape out_shape.
  24. Args:
  25. data (tvm.tensor.Tensor): The tensor to be reshaped.
  26. out_shape (list, tuple): The new shape applied on the input tensor data,
  27. should be compatible with the original shape of data.
  28. Returns:
  29. The reshaped akg.tvm.tensor of same type as input tensor data.
  30. """
  31. data_shape = data.shape
  32. vc_util.check_shape(data_shape)
  33. in_shape = get_shape(data)
  34. out_shape = list(out_shape)
  35. if -1 in out_shape:
  36. access_size = 1
  37. for i, o_shape in enumerate(out_shape):
  38. if -1 != o_shape:
  39. access_size *= o_shape
  40. else:
  41. hit_idx = i
  42. ori_size = reduce(lambda x, y: x * y, in_shape)
  43. if ori_size % access_size != 0:
  44. raise ValueError(("Invalid out_shape ({})".format(out_shape)))
  45. out_shape[hit_idx] = int(ori_size / access_size)
  46. res = akg.topi.reshape(data, out_shape)
  47. return res