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.

unstack.cc 2.5 kB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright 2020-2021 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 "ops/unstack.h"
  17. namespace mindspore {
  18. namespace ops {
  19. void Unstack::Init(const int64_t axis) { this->set_axis(axis); }
  20. void Unstack::set_axis(const int64_t axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
  21. int64_t Unstack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
  22. AbstractBasePtr UnstackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  23. const std::vector<AbstractBasePtr> &input_args) {
  24. MS_EXCEPTION_IF_NULL(primitive);
  25. auto prim_name = primitive->name();
  26. MS_EXCEPTION_IF_NULL(input_args[0]);
  27. auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
  28. int64_t dim = SizeToLong(x_shape.size());
  29. int64_t axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
  30. if (axis < 0) {
  31. axis = axis + dim;
  32. }
  33. auto output_num = x_shape[LongToSize(axis)];
  34. (void)CheckAndConvertUtils::CheckInteger("output_num", output_num, kGreaterThan, 0, prim_name);
  35. auto output_valid_check = x_shape[LongToSize(axis)] - output_num;
  36. (void)CheckAndConvertUtils::CheckInteger("The dimension which to unstack divides output_num", output_valid_check,
  37. kEqual, 0, prim_name);
  38. std::vector<int64_t> infer_shape(x_shape.begin(), x_shape.begin() + axis);
  39. (void)infer_shape.insert(infer_shape.end(), x_shape.begin() + axis + 1, x_shape.end());
  40. AbstractBasePtrList output;
  41. auto tensor_type = input_args[0]->BuildType()->cast<TensorTypePtr>();
  42. MS_EXCEPTION_IF_NULL(tensor_type);
  43. auto element = tensor_type->element();
  44. for (int64_t i = 0; i != output_num; i++) {
  45. output.push_back(std::make_shared<abstract::AbstractTensor>(element, infer_shape));
  46. }
  47. return std::make_shared<abstract::AbstractTuple>(output);
  48. }
  49. REGISTER_PRIMITIVE_C(kNameUnstack, Unstack);
  50. } // namespace ops
  51. } // namespace mindspore