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.

acl_session.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright 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 <memory>
  17. #include <algorithm>
  18. #include "serving/acl/acl_session.h"
  19. #include "include/infer_log.h"
  20. namespace mindspore::inference {
  21. std::shared_ptr<InferSession> InferSession::CreateSession(const std::string &device, uint32_t device_id) {
  22. try {
  23. auto session = std::make_shared<AclSession>();
  24. auto ret = session->InitEnv(device, device_id);
  25. if (!ret) {
  26. return nullptr;
  27. }
  28. return session;
  29. } catch (std::exception &e) {
  30. MSI_LOG_ERROR << "Inference CreatSession failed";
  31. return nullptr;
  32. }
  33. }
  34. bool AclSession::LoadModelFromFile(const std::string &file_name, uint32_t &model_id) {
  35. return model_process_.LoadModelFromFile(file_name, model_id);
  36. }
  37. bool AclSession::UnloadModel(uint32_t model_id) {
  38. model_process_.UnLoad();
  39. return true;
  40. }
  41. bool AclSession::ExecuteModel(uint32_t model_id, const RequestBase &request,
  42. ReplyBase &reply) { // set d context
  43. aclError rt_ret = aclrtSetCurrentContext(context_);
  44. if (rt_ret != ACL_ERROR_NONE) {
  45. MSI_LOG_ERROR << "set the ascend device context failed";
  46. return false;
  47. }
  48. return model_process_.Execute(request, reply);
  49. }
  50. bool AclSession::InitEnv(const std::string &device_type, uint32_t device_id) {
  51. device_type_ = device_type;
  52. device_id_ = device_id;
  53. auto ret = aclInit(nullptr);
  54. if (ret != ACL_ERROR_NONE) {
  55. MSI_LOG_ERROR << "Execute aclInit Failed";
  56. return false;
  57. }
  58. MSI_LOG_INFO << "acl init success";
  59. ret = aclrtSetDevice(device_id_);
  60. if (ret != ACL_ERROR_NONE) {
  61. MSI_LOG_ERROR << "acl open device " << device_id_ << " failed";
  62. return false;
  63. }
  64. MSI_LOG_INFO << "open device " << device_id_ << " success";
  65. ret = aclrtCreateContext(&context_, device_id_);
  66. if (ret != ACL_ERROR_NONE) {
  67. MSI_LOG_ERROR << "acl create context failed";
  68. return false;
  69. }
  70. MSI_LOG_INFO << "create context success";
  71. ret = aclrtCreateStream(&stream_);
  72. if (ret != ACL_ERROR_NONE) {
  73. MSI_LOG_ERROR << "acl create stream failed";
  74. return false;
  75. }
  76. MSI_LOG_INFO << "create stream success";
  77. aclrtRunMode run_mode;
  78. ret = aclrtGetRunMode(&run_mode);
  79. if (ret != ACL_ERROR_NONE) {
  80. MSI_LOG_ERROR << "acl get run mode failed";
  81. return false;
  82. }
  83. bool is_device = (run_mode == ACL_DEVICE);
  84. model_process_.SetIsDevice(is_device);
  85. MSI_LOG_INFO << "get run mode success is device input/output " << is_device;
  86. MSI_LOG_INFO << "Init acl success, device id " << device_id_;
  87. return true;
  88. }
  89. bool AclSession::FinalizeEnv() {
  90. aclError ret;
  91. if (stream_ != nullptr) {
  92. ret = aclrtDestroyStream(stream_);
  93. if (ret != ACL_ERROR_NONE) {
  94. MSI_LOG_ERROR << "destroy stream failed";
  95. }
  96. stream_ = nullptr;
  97. }
  98. MSI_LOG_INFO << "end to destroy stream";
  99. if (context_ != nullptr) {
  100. ret = aclrtDestroyContext(context_);
  101. if (ret != ACL_ERROR_NONE) {
  102. MSI_LOG_ERROR << "destroy context failed";
  103. }
  104. context_ = nullptr;
  105. }
  106. MSI_LOG_INFO << "end to destroy context";
  107. ret = aclrtResetDevice(device_id_);
  108. if (ret != ACL_ERROR_NONE) {
  109. MSI_LOG_ERROR << "reset devie " << device_id_ << " failed";
  110. }
  111. MSI_LOG_INFO << "end to reset device " << device_id_;
  112. ret = aclFinalize();
  113. if (ret != ACL_ERROR_NONE) {
  114. MSI_LOG_ERROR << "finalize acl failed";
  115. }
  116. MSI_LOG_INFO << "end to finalize acl";
  117. return true;
  118. }
  119. AclSession::AclSession() = default;
  120. } // namespace mindspore::inference