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.

ascend_inference_session.cc 4.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright 2019-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 "session/ascend_inference_session.h"
  17. #include "operator/ops.h"
  18. #include "ir/tensor.h"
  19. #include "ir/tensor_py.h"
  20. #include "ir/anf.h"
  21. #include "ir/param_value_py.h"
  22. #include "device/kernel_runtime.h"
  23. #include "session/anf_runtime_algorithm.h"
  24. #include "common/utils.h"
  25. #include "common/trans.h"
  26. #include "kernel/tbe/tbe_python_funcs.h"
  27. #include "utils/config_manager.h"
  28. #include "utils/base_ref_extends.h"
  29. using mindspore::tensor::TensorPy;
  30. namespace mindspore {
  31. namespace session {
  32. namespace {
  33. std::set<AnfNodePtr> weight_infos;
  34. static TypeId GetDataType(const py::buffer_info &buf) {
  35. if (buf.format.size() == 1) {
  36. switch (buf.format.front()) {
  37. case 'e':
  38. case 'f':
  39. case 'd':
  40. switch (buf.itemsize) {
  41. case 2:
  42. return TypeId::kNumberTypeFloat16;
  43. case 4:
  44. return TypeId::kNumberTypeFloat32;
  45. case 8:
  46. return TypeId::kNumberTypeFloat64;
  47. }
  48. break;
  49. case 'b':
  50. case 'h':
  51. case 'i':
  52. case 'l':
  53. case 'q':
  54. switch (buf.itemsize) {
  55. case 1:
  56. return TypeId::kNumberTypeInt8;
  57. case 2:
  58. return TypeId::kNumberTypeInt16;
  59. case 4:
  60. return TypeId::kNumberTypeInt32;
  61. case 8:
  62. return TypeId::kNumberTypeInt64;
  63. }
  64. break;
  65. case 'B':
  66. case 'H':
  67. case 'I':
  68. case 'L':
  69. case 'Q':
  70. switch (buf.itemsize) {
  71. case 1:
  72. return TypeId::kNumberTypeUInt8;
  73. case 2:
  74. return TypeId::kNumberTypeUInt16;
  75. case 4:
  76. return TypeId::kNumberTypeUInt32;
  77. case 8:
  78. return TypeId::kNumberTypeUInt64;
  79. }
  80. break;
  81. case '?':
  82. return TypeId::kNumberTypeBool;
  83. }
  84. }
  85. MS_LOG(WARNING) << "Unsupported DataType format " << buf.format << " item size " << buf.itemsize;
  86. return TypeId::kTypeUnknown;
  87. }
  88. } // namespace
  89. void AscendInferenceSession::LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  90. const std::vector<tensor::TensorPtr> &inputs_const) const {
  91. MS_EXCEPTION_IF_NULL(kernel_graph);
  92. std::vector<tensor::TensorPtr> inputs(inputs_const);
  93. auto input_nodes = kernel_graph->inputs();
  94. size_t no_weight_input = 0;
  95. for (size_t i = 0; i < input_nodes.size(); ++i) {
  96. tensor::TensorPtr tensor = nullptr;
  97. if (!input_nodes[i]->isa<Parameter>()) {
  98. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter";
  99. continue;
  100. }
  101. auto pk_node = input_nodes[i]->cast<ParameterPtr>();
  102. MS_EXCEPTION_IF_NULL(pk_node);
  103. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  104. MS_EXCEPTION_IF_NULL(device_address);
  105. if (AnfAlgo::IsParameterWeight(pk_node)) {
  106. if (weight_infos.count(pk_node) != 0) {
  107. continue;
  108. }
  109. auto param_value = std::dynamic_pointer_cast<ParamValuePy>(pk_node->default_param());
  110. MS_EXCEPTION_IF_NULL(param_value);
  111. auto py_param = param_value->value();
  112. MS_EXCEPTION_IF_NULL(py_param);
  113. py::array py_array = py_param.cast<py::array>();
  114. py::buffer_info buf = py_array.request();
  115. auto buf_type = GetDataType(buf);
  116. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  117. LongToSize(buf.size * buf.itemsize), buf_type, buf.ptr)) {
  118. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  119. }
  120. weight_infos.insert(pk_node);
  121. } else {
  122. tensor = inputs[no_weight_input++];
  123. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  124. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  125. tensor->data_c())) {
  126. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  127. }
  128. }
  129. }
  130. }
  131. } // namespace session
  132. } // namespace mindspore