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.

label_switch.cc 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2019 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 "kernel/rts/label_switch.h"
  17. #include <asm-generic/param.h>
  18. #include <memory>
  19. #include <string>
  20. #include "runtime/stream.h"
  21. #include "framework/ge_runtime/task_info.h"
  22. #include "session/anf_runtime_algorithm.h"
  23. #include "common/utils.h"
  24. using ge::model_runner::LabelSwitchTaskInfo;
  25. using LabelSwitchTaskInfoPtr = std::shared_ptr<LabelSwitchTaskInfo>;
  26. namespace mindspore {
  27. namespace kernel {
  28. LabelSwitchKernel::LabelSwitchKernel() {
  29. label_list_ = {};
  30. cond_ = nullptr;
  31. label_size_ = 0;
  32. }
  33. LabelSwitchKernel::~LabelSwitchKernel() {}
  34. bool LabelSwitchKernel::Init(const AnfNodePtr &anf_node) {
  35. MS_EXCEPTION_IF_NULL(anf_node);
  36. MS_LOG(INFO) << "LabelSwitchKernel init";
  37. auto cnode = anf_node->cast<CNodePtr>();
  38. if (!AnfAlgo::HasNodeAttr(kAttrLabelSwitchList, cnode)) {
  39. MS_LOG(EXCEPTION) << "LabelSwitchKernel has no attr label_switch_list";
  40. }
  41. auto primitive = AnfAlgo::GetCNodePrimitive(anf_node);
  42. MS_EXCEPTION_IF_NULL(primitive);
  43. label_list_ = GetValue<std::vector<uint32_t>>(primitive->GetAttr(kAttrLabelSwitchList));
  44. label_size_ = label_list_.size();
  45. MS_LOG(INFO) << "LabelSwitchKernel get attr label size:" << label_size_;
  46. for (auto label : label_list_) {
  47. MS_LOG(INFO) << "label: " << label;
  48. }
  49. return true;
  50. }
  51. bool LabelSwitchKernel::Launch(const std::vector<AddressPtr> & /*inputs*/,
  52. const std::vector<AddressPtr> & /*workspace*/,
  53. const std::vector<AddressPtr> & /*outputs*/, void * /*stream_ptr*/) {
  54. MS_LOG(INFO) << "LabelSwitchKernel launch";
  55. return true;
  56. }
  57. std::vector<TaskInfoPtr> LabelSwitchKernel::GenTask(const std::vector<AddressPtr> &inputs,
  58. const std::vector<AddressPtr> &workspace,
  59. const std::vector<AddressPtr> &outputs, uint32_t stream_id) {
  60. MS_LOG(INFO) << "LabelSwitchKernel GenTask label size:" << label_size_ << ", stream id:" << stream_id;
  61. std::vector<TaskInfoPtr> task_info_list;
  62. cond_ = inputs[0]->addr;
  63. auto task_info_ptr = std::make_shared<LabelSwitchTaskInfo>(stream_id, label_size_, label_list_, cond_);
  64. MS_EXCEPTION_IF_NULL(task_info_ptr);
  65. task_info_list.emplace_back(task_info_ptr);
  66. return task_info_list;
  67. }
  68. std::vector<std::shared_ptr<kernel::KernelBuildInfo>> LabelSwitchDesc::GetKernelInfo() {
  69. std::vector<std::shared_ptr<kernel::KernelBuildInfo>> label_switch_build_info{};
  70. vector<string> input_format{kOpFormat_DEFAULT};
  71. vector<TypeId> input_type{kNumberTypeInt32};
  72. if (input_format.size() != input_type.size()) {
  73. MS_LOG(EXCEPTION) << "Invalid param num, input_format size " << input_format.size() << " input_type size "
  74. << input_type.size();
  75. }
  76. for (size_t i = 0; i < input_format.size(); ++i) {
  77. auto builder = KernelBuildInfo::KernelBuildInfoBuilder();
  78. builder.SetInputsFormat({input_format[i]});
  79. builder.SetInputsDeviceType({input_type[i]});
  80. builder.SetProcessor(AICORE);
  81. builder.SetKernelType(RT_KERNEL);
  82. builder.SetFusionType(OPAQUE);
  83. label_switch_build_info.emplace_back(builder.Build());
  84. }
  85. return label_switch_build_info;
  86. }
  87. } // namespace kernel
  88. } // namespace mindspore