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.cc 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright 2021-2022 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <memory>
  17. #include <vector>
  18. #include "common/graph_kernel/expanders/expander_factory.h"
  19. namespace mindspore::graphkernel::expanders {
  20. class ExpandDims : public OpDesc {
  21. public:
  22. ExpandDims() {
  23. std::initializer_list<std::string> attrs{"axis"};
  24. (void)validators_.emplace_back(std::make_unique<CheckAttr>(attrs));
  25. }
  26. ~ExpandDims() = default;
  27. static ShapeVector InferShape(const ShapeVector &shape, const std::vector<int64_t> &axis) {
  28. ShapeVector new_shape = shape;
  29. for (auto x : axis) {
  30. int64_t rank = static_cast<int64_t>(new_shape.size());
  31. if (x > rank || x < -rank - 1) {
  32. MS_LOG(EXCEPTION) << "ExpandDims axis " << x << " is out of range of [" << (-rank - 1) << ", " << rank << "]";
  33. }
  34. if (x >= 0) {
  35. (void)new_shape.insert(new_shape.begin() + x, 1LL);
  36. } else {
  37. (void)new_shape.insert(new_shape.begin() + (x + rank + 1), 1LL);
  38. }
  39. }
  40. return new_shape;
  41. }
  42. protected:
  43. NodePtrList Expand() override {
  44. const auto &inputs = gb.Get()->inputs();
  45. const auto &input_x = inputs[0];
  46. auto shape = MakeValue(ExpandDims::InferShape(input_x->shape, GetAxisList(this->attrs_["axis"])));
  47. auto result = gb.Emit("Reshape", {input_x}, {{"shape", shape}});
  48. return {result};
  49. }
  50. };
  51. OP_EXPANDER_REGISTER("ExpandDims", ExpandDims);
  52. ShapeVector ExpandDimsInferShape(const ShapeVector &shape, const std::vector<int64_t> &axis) {
  53. return ExpandDims::InferShape(shape, axis);
  54. }
  55. } // namespace mindspore::graphkernel::expanders