rules.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
  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. from .primitives import * # noqa: F403
  15. from .register import register_decomp
  16. # TODO(kevincheng2): python implementation of prim feature,
  17. # now it has been sunk to c++, waiting for further deletion.
  18. @register_decomp('pd_op.unsqueeze')
  19. def unsqueeze(x, axis):
  20. """define composite rule of op unsqueeze"""
  21. """using reshape to implement unsqueeze op"""
  22. axis = axis.get_defining_op().attrs()["value"]
  23. x_shape = list(x.shape)
  24. axis_list = list(axis)
  25. for i in axis_list:
  26. if i < 0:
  27. i += len(x_shape) + 1
  28. x_shape = (
  29. x_shape[:i]
  30. + [
  31. 1,
  32. ]
  33. + x_shape[i:]
  34. )
  35. out = reshape(x, x_shape)
  36. return [out, None]