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.

utils.cc 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright 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 "common/graph_kernel/expanders/utils.h"
  17. #include <algorithm>
  18. #include <string>
  19. #include <vector>
  20. #include "common/graph_kernel/model/lite_graph.h"
  21. #include "common/graph_kernel/model/node.h"
  22. namespace mindspore::graphkernel::expanders {
  23. inner::LiteGraphPtr OpDesc::Run(const BaseInfoList &inputs, const BaseInfoList &outputs, const inner::DAttrs &attrs,
  24. const std::string &processor) {
  25. this->inputs_info_ = inputs;
  26. this->outputs_info_ = outputs;
  27. this->attrs_ = attrs;
  28. this->processor_ = processor;
  29. if (std::any_of(validators_.begin(), validators_.end(),
  30. [this](const std::unique_ptr<Validator> &v) { return !(v->Check(*this)); })) {
  31. return nullptr;
  32. }
  33. if (!this->CheckInputs()) {
  34. return nullptr;
  35. }
  36. for (auto &inp : inputs) {
  37. (void)gb.Parameter(inp);
  38. }
  39. auto result = this->Expand();
  40. gb.SetOutputs(result);
  41. if (!this->CheckOutputs()) {
  42. return nullptr;
  43. }
  44. return gb.Get();
  45. }
  46. bool OpDesc::CheckOutputs() {
  47. // check the output shape/type/format are same as the original basic node's output.
  48. const NodePtrList &outputs = gb.Get()->GetOutputs();
  49. if (outputs.size() != this->outputs_info_.size()) {
  50. MS_LOG(INFO) << "the output num was not equal to the original output num : " << outputs.size() << " vs "
  51. << outputs_info_.size();
  52. return false;
  53. }
  54. for (size_t i = 0; i < outputs.size(); i++) {
  55. if (outputs[i]->shape != outputs_info_[i].shape) {
  56. std::ostringstream oss;
  57. oss << "Op " << this->op_ << "'s output shape [";
  58. for (auto s : outputs[i]->shape) {
  59. oss << s << ",";
  60. }
  61. oss << "] is wrong. expect: [";
  62. for (auto s : outputs_info_[i].shape) {
  63. oss << s << ",";
  64. }
  65. oss << "]";
  66. MS_LOG(INFO) << oss.str();
  67. return false;
  68. }
  69. if (outputs[i]->type != outputs_info_[i].type) {
  70. MS_LOG(INFO) << "Op " << this->op_ << "'s output type [" << outputs[i]->type << "] is wrong, expect: ["
  71. << outputs_info_[i].type << "]";
  72. return false;
  73. }
  74. if (outputs[i]->format != outputs_info_[i].format) {
  75. MS_LOG(INFO) << "Op " << this->op_ << "'s output format [" << outputs[i]->format << "] is wrong, expect: ["
  76. << outputs_info_[i].format << "]";
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. std::vector<int64_t> GetAxisList(const ValuePtr &value) {
  83. std::vector<int64_t> result;
  84. auto get_int_value = [](const ValuePtr &value) -> int64_t {
  85. return value->isa<Int64Imm>() ? GetValue<int64_t>(value) : static_cast<int64_t>(GetValue<int>(value));
  86. };
  87. if (value->isa<ValueSequence>()) {
  88. const auto &vals = value->cast<ValueSequencePtr>()->value();
  89. (void)std::transform(vals.begin(), vals.end(), std::back_inserter(result), get_int_value);
  90. } else {
  91. result.push_back(get_int_value(value));
  92. }
  93. return result;
  94. }
  95. } // namespace mindspore::graphkernel::expanders