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.

kernel_build_info.cc 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/kernel_build_info.h"
  17. #include <algorithm>
  18. #include "utils/log_adapter.h"
  19. #include "debug/anf_ir_dump.h"
  20. namespace mindspore {
  21. namespace kernel {
  22. std::string KernelBuildInfo::GetInputFormat(size_t input_index) const {
  23. if (input_index >= inputs_format_.size()) {
  24. MS_LOG(EXCEPTION) << "The index [" << input_index << "] is exceed the number of input node";
  25. }
  26. return inputs_format_[input_index];
  27. }
  28. std::string KernelBuildInfo::GetOutputFormat(size_t output_index) const {
  29. if (output_index >= outputs_format_.size()) {
  30. MS_LOG(EXCEPTION) << "The index [" << output_index << "] is exceed the number of input node";
  31. }
  32. return outputs_format_[output_index];
  33. }
  34. TypeId KernelBuildInfo::GetInputDeviceType(size_t input_index) const {
  35. if (input_index >= inputs_device_type_.size()) {
  36. MS_LOG(EXCEPTION) << "The index [" << input_index << "] is exceed the number of input node";
  37. }
  38. return inputs_device_type_[input_index];
  39. }
  40. TypeId KernelBuildInfo::GetOutputDeviceType(size_t output_index) const {
  41. if (output_index >= outputs_device_type_.size()) {
  42. MS_LOG(EXCEPTION) << "The index [" << output_index << "] is exceed the number of input node";
  43. }
  44. return outputs_device_type_[output_index];
  45. }
  46. std::vector<std::string> KernelBuildInfo::GetAllInputFormats() const { return inputs_format_; }
  47. std::vector<std::string> KernelBuildInfo::GetAllOutputFormats() const { return outputs_format_; }
  48. std::vector<TypeId> KernelBuildInfo::GetAllInputDeviceTypes() const { return inputs_device_type_; }
  49. std::vector<TypeId> KernelBuildInfo::GetAllOutputDeviceTypes() const { return outputs_device_type_; }
  50. size_t KernelBuildInfo::GetInputNum() const { return inputs_format_.size(); }
  51. size_t KernelBuildInfo::GetOutputNum() const { return outputs_format_.size(); }
  52. std::vector<Axis> KernelBuildInfo::GetInputReshapeType(size_t input_index) const {
  53. if (input_index >= input_reshape_type_.size()) {
  54. MS_LOG(EXCEPTION) << "The index [" << input_index << "] is exceed the number of input node size "
  55. << input_reshape_type_.size();
  56. }
  57. return input_reshape_type_[input_index];
  58. }
  59. std::vector<Axis> KernelBuildInfo::GetOutputReshapeType(size_t output_index) const {
  60. if (output_index >= output_reshape_type_.size()) {
  61. MS_LOG(EXCEPTION) << "The index [" << output_index << "] is exceed the number of output node size "
  62. << output_reshape_type_.size();
  63. }
  64. return output_reshape_type_[output_index];
  65. }
  66. std::string KernelBuildInfo::ToString() const {
  67. std::ostringstream output_buffer;
  68. output_buffer << "(";
  69. for (size_t index = 0; index < GetInputNum(); ++index) {
  70. if (index != 0) {
  71. output_buffer << ", ";
  72. }
  73. output_buffer << "<" << ToShortString(GetInputDeviceType(index)) << "x" << GetInputFormat(index) << ">";
  74. }
  75. output_buffer << ") -> (";
  76. for (size_t index = 0; index < GetOutputNum(); ++index) {
  77. if (index != 0) {
  78. output_buffer << ", ";
  79. }
  80. output_buffer << "<" << ToShortString(GetOutputDeviceType(index)) << "x" << GetOutputFormat(index) << ">";
  81. }
  82. output_buffer << ")";
  83. return output_buffer.str();
  84. }
  85. bool KernelBuildInfo::operator==(const KernelBuildInfo &other) const {
  86. if (kernel_type_ != other.kernel_type_ || fusion_type_ != other.fusion_type_ || processor_ != other.processor_) {
  87. return false;
  88. }
  89. if (inputs_format_ != other.inputs_format_ || outputs_format_ != other.outputs_format_) {
  90. return false;
  91. }
  92. return !(inputs_device_type_ != other.inputs_device_type_ || outputs_device_type_ != other.outputs_device_type_);
  93. }
  94. bool KernelBuildInfo::IsInputDefaultPadding() const { return output_reshape_type_.empty(); }
  95. bool KernelBuildInfo::IsOutputDefaultPadding() const { return input_reshape_type_.empty(); }
  96. void KernelBuildInfo::KernelBuildInfoBuilder::SetKernelType(const KernelType &kernel_type) {
  97. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  98. kernel_build_info_->kernel_type_ = kernel_type;
  99. }
  100. void KernelBuildInfo::KernelBuildInfoBuilder::SetInputsFormat(const std::vector<std::string> &inputs_format) {
  101. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  102. kernel_build_info_->inputs_format_ = inputs_format;
  103. }
  104. void KernelBuildInfo::KernelBuildInfoBuilder::SetOutputsFormat(const std::vector<std::string> &outputs_format) {
  105. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  106. kernel_build_info_->outputs_format_ = outputs_format;
  107. }
  108. void KernelBuildInfo::KernelBuildInfoBuilder::SetInputsDeviceType(const std::vector<TypeId> &inputs_device_type) {
  109. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  110. kernel_build_info_->inputs_device_type_ = inputs_device_type;
  111. }
  112. void KernelBuildInfo::KernelBuildInfoBuilder::SetOutputsDeviceType(const std::vector<TypeId> &outputs_device_type) {
  113. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  114. kernel_build_info_->outputs_device_type_ = outputs_device_type;
  115. }
  116. void KernelBuildInfo::KernelBuildInfoBuilder::SetFusionType(FusionType fusion_type) {
  117. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  118. kernel_build_info_->fusion_type_ = fusion_type;
  119. }
  120. void KernelBuildInfo::KernelBuildInfoBuilder::SetProcessor(Processor processor) {
  121. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  122. kernel_build_info_->processor_ = processor;
  123. }
  124. std::shared_ptr<KernelBuildInfo> KernelBuildInfo::KernelBuildInfoBuilder::Build() { return kernel_build_info_; }
  125. void KernelBuildInfo::KernelBuildInfoBuilder::SetInputReshapeType(
  126. const std::vector<std::vector<Axis>> &input_reshape_type) {
  127. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  128. kernel_build_info_->input_reshape_type_ = input_reshape_type;
  129. }
  130. void KernelBuildInfo::KernelBuildInfoBuilder::SetOutputReshapeType(
  131. const std::vector<std::vector<Axis>> &output_reshape_type) {
  132. MS_EXCEPTION_IF_NULL(kernel_build_info_);
  133. kernel_build_info_->output_reshape_type_ = output_reshape_type;
  134. }
  135. } // namespace kernel
  136. } // namespace mindspore