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

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