axis_utils.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #pragma once
  15. namespace ultra_infer {
  16. static inline int CanonicalAxis(const int axis, const int rank) {
  17. if (axis < 0) {
  18. return axis + rank;
  19. }
  20. return axis;
  21. }
  22. static inline int SizeToAxis(const int axis, const std::vector<int64_t> &dims) {
  23. int size = 1;
  24. for (int i = 0; i < axis; i++) {
  25. size *= dims[i];
  26. }
  27. return size;
  28. }
  29. static inline int SizeFromAxis(const int axis,
  30. const std::vector<int64_t> &dims) {
  31. int size = 1;
  32. for (int i = axis; i < dims.size(); i++) {
  33. size *= dims[i];
  34. }
  35. return size;
  36. }
  37. static inline int SizeOutAxis(const int axis,
  38. const std::vector<int64_t> &dims) {
  39. int size = 1;
  40. for (int i = axis + 1; i < dims.size(); i++) {
  41. size *= dims[i];
  42. }
  43. return size;
  44. }
  45. } // namespace ultra_infer