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.

stack.cc 3.4 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright 2020 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/stack.h"
  17. namespace mindspore {
  18. namespace ops {
  19. namespace {
  20. abstract::AbstractBasePtr StackInfer(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
  21. MS_EXCEPTION_IF_NULL(primitive);
  22. auto stack_prim = primitive->cast<PrimStackPtr>();
  23. MS_EXCEPTION_IF_NULL(stack_prim);
  24. auto prim_name = stack_prim->name();
  25. if (input_args.size() != 1) {
  26. MS_LOG(ERROR) << "Invalid output size:" << input_args.size();
  27. }
  28. if (input_args.size() < 1) {
  29. MS_LOG(ERROR) << "Invalid input size " << input_args.size();
  30. }
  31. auto input_shape =
  32. CheckAndConvertUtils::ConvertShapePtrToShape("input_shape", input_args[0]->BuildShape(), prim_name);
  33. for (int64_t i = 1; i < (int64_t)input_args.size(); ++i) {
  34. auto input_shape_tmp =
  35. CheckAndConvertUtils::ConvertShapePtrToShape("input_shape", input_args[i]->BuildShape(), prim_name);
  36. if (input_shape_tmp.size() != input_shape.size()) {
  37. MS_LOG(ERROR) << "All input shape size should be the same!";
  38. }
  39. for (int64_t j = 0; j < (int64_t)input_shape.size(); ++j) {
  40. if (input_shape_tmp.at(j) != input_shape.at(j)) {
  41. MS_LOG(ERROR) << "All input shape should be the same!";
  42. }
  43. }
  44. }
  45. std::vector<int64_t> infer_shape = input_shape;
  46. infer_shape.insert(infer_shape.begin() + stack_prim->get_axis(), input_args.size());
  47. auto infer_type0 = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
  48. for (int64_t i = 1; i < (int64_t)input_args.size(); i++) {
  49. if (input_args[i]->BuildType()->cast<TensorTypePtr>()->element() == infer_type0) {
  50. MS_LOG(ERROR) << "All input should have the same data type!input[" << i
  51. << "] data type = " << input_args[i]->BuildType()->cast<TensorTypePtr>()->element();
  52. }
  53. }
  54. auto infer_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
  55. auto output0 = std::make_shared<abstract::AbstractTensor>(infer_type, infer_shape);
  56. AbstractBasePtrList output1 = {output0};
  57. return std::make_shared<abstract::AbstractTuple>(output1);
  58. }
  59. } // namespace
  60. void Stack::set_axis(const int64_t axis) { AddAttr(kAxis, MakeValue(axis)); }
  61. int64_t Stack::get_axis() const {
  62. auto value_ptr = this->GetAttr(kAxis);
  63. return GetValue<int64_t>(value_ptr);
  64. }
  65. void Stack::Init(const int64_t axis) { this->set_axis(axis); }
  66. AbstractBasePtr StackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  67. const std::vector<AbstractBasePtr> &input_args) {
  68. return std::make_shared<abstract::AbstractTensor>(StackInfer(primitive, input_args));
  69. }
  70. REGISTER_PRIMITIVE_EVAL_IMPL(Stack, prim::kPrimStack, StackInfer);
  71. REGISTER_PRIMITIVE_C(kNameStack, Stack);
  72. } // namespace ops
  73. } // namespace mindspore