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.1 kB

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