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

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