tensor_fusion_helper.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) 2022 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. import paddle
  15. import paddle.nn as nn
  16. import paddle.nn.functional as F
  17. def avg_max_reduce_channel_helper(x, use_concat=True):
  18. # Reduce hw by avg and max, only support single input
  19. assert not isinstance(x, (list, tuple))
  20. mean_value = paddle.mean(x, axis=1, keepdim=True)
  21. max_value = paddle.max(x, axis=1, keepdim=True)
  22. if use_concat:
  23. res = paddle.concat([mean_value, max_value], axis=1)
  24. else:
  25. res = [mean_value, max_value]
  26. return res
  27. def avg_max_reduce_channel(x):
  28. # Reduce hw by avg and max
  29. # Return cat([avg_ch_0, max_ch_0, avg_ch_1, max_ch_1, ...])
  30. if not isinstance(x, (list, tuple)):
  31. return avg_max_reduce_channel_helper(x)
  32. elif len(x) == 1:
  33. return avg_max_reduce_channel_helper(x[0])
  34. else:
  35. res = []
  36. for xi in x:
  37. res.extend(avg_max_reduce_channel_helper(xi, False))
  38. return paddle.concat(res, axis=1)