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 3.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. void AscendInferenceSession::LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  33. const std::vector<tensor::TensorPtr> &inputs_const) const {
  34. MS_EXCEPTION_IF_NULL(kernel_graph);
  35. std::vector<tensor::TensorPtr> inputs(inputs_const);
  36. auto input_nodes = kernel_graph->inputs();
  37. auto ms_context = MsContext::GetInstance();
  38. MS_EXCEPTION_IF_NULL(ms_context);
  39. size_t no_weight_input = 0;
  40. for (size_t i = 0; i < input_nodes.size(); ++i) {
  41. tensor::TensorPtr tensor = nullptr;
  42. if (!input_nodes[i]->isa<Parameter>()) {
  43. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter";
  44. continue;
  45. }
  46. auto pk_node = input_nodes[i]->cast<ParameterPtr>();
  47. MS_EXCEPTION_IF_NULL(pk_node);
  48. if (AnfAlgo::IsParameterWeight(pk_node)) {
  49. auto param_value = std::dynamic_pointer_cast<ParamValuePy>(pk_node->default_param());
  50. MS_EXCEPTION_IF_NULL(param_value);
  51. auto py_param = param_value->value();
  52. MS_EXCEPTION_IF_NULL(py_param);
  53. py::array py_array = py_param.cast<py::array>();
  54. tensor = TensorPy::MakeTensor(py_array);
  55. } else {
  56. tensor = inputs[no_weight_input++];
  57. }
  58. MS_EXCEPTION_IF_NULL(tensor);
  59. if (AnfAlgo::OutputAddrExist(pk_node, 0)) {
  60. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  61. bool need_sync = false;
  62. if (ms_context->enable_pynative_infer()) {
  63. if (tensor->device_address().get() == nullptr || tensor->device_address() != device_address) {
  64. need_sync = true;
  65. }
  66. } else {
  67. if (tensor->is_dirty()) {
  68. need_sync = true;
  69. } else if (tensor->device_address() != device_address) {
  70. (void)tensor->data_sync();
  71. need_sync = true;
  72. }
  73. }
  74. if (need_sync) {
  75. if (ms_context->execution_mode() == kPynativeMode || AnfAlgo::IsParameterWeight(pk_node)) {
  76. tensor->set_device_address(device_address);
  77. }
  78. MS_EXCEPTION_IF_NULL(device_address);
  79. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  80. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  81. tensor->data_c())) {
  82. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  83. }
  84. }
  85. }
  86. tensor->set_dirty(false);
  87. }
  88. }
  89. } // namespace session
  90. } // namespace mindspore