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.

plugin_op.cc 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright 2021 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 "minddata/dataset/kernels/plugin_op.h"
  17. #include "minddata/dataset/core/tensor.h"
  18. #include "minddata/dataset/plugin/plugin_loader.h"
  19. namespace mindspore {
  20. namespace dataset {
  21. errno_t memcpy_s_loop(uchar *dest, size_t destMax, const uchar *src, size_t count) {
  22. int64_t step = 0;
  23. while (count >= SECUREC_MEM_MAX_LEN) {
  24. int ret_code = memcpy_s(dest + step * SECUREC_MEM_MAX_LEN, SECUREC_MEM_MAX_LEN, src + step * SECUREC_MEM_MAX_LEN,
  25. SECUREC_MEM_MAX_LEN);
  26. if (ret_code != 0) return ret_code;
  27. count -= SECUREC_MEM_MAX_LEN;
  28. step++;
  29. }
  30. return memcpy_s(dest + step * SECUREC_MEM_MAX_LEN, count, src + step * SECUREC_MEM_MAX_LEN, count);
  31. }
  32. Status PluginOp::PluginToTensorRow(const std::vector<plugin::Tensor> &in_row, TensorRow *out_row) {
  33. CHECK_FAIL_RETURN_UNEXPECTED(out_row != nullptr && out_row->empty(), "null/empty out_row received!");
  34. out_row->reserve(in_row.size());
  35. for (const auto &tensor : in_row) {
  36. std::shared_ptr<Tensor> output;
  37. DataType tp = DataType(tensor.type_);
  38. CHECK_FAIL_RETURN_UNEXPECTED(tp.IsNumeric() && tp != DataType::DE_UNKNOWN, "Unsupported type: " + tensor.type_);
  39. RETURN_IF_NOT_OK(Tensor::CreateFromMemory(TensorShape(tensor.shape_), tp, tensor.buffer_.data(), &output));
  40. out_row->emplace_back(output);
  41. }
  42. return Status::OK();
  43. }
  44. Status PluginOp::TensorRowToPlugin(const TensorRow &in_row, std::vector<plugin::Tensor> *out_row) {
  45. CHECK_FAIL_RETURN_UNEXPECTED(out_row != nullptr && out_row->empty(), "null/empty out_row received!");
  46. out_row->resize(in_row.size());
  47. for (size_t ind = 0; ind < in_row.size(); ind++) {
  48. plugin::Tensor &tensor = (*out_row)[ind];
  49. if (in_row[ind]->type().IsNumeric()) {
  50. dsize_t buffer_size = in_row[ind]->SizeInBytes();
  51. tensor.buffer_.resize(buffer_size);
  52. int ret_code = memcpy_s_loop(tensor.buffer_.data(), buffer_size, in_row[ind]->GetBuffer(), buffer_size);
  53. CHECK_FAIL_RETURN_UNEXPECTED(ret_code == 0, "Failed to copy data into tensor.");
  54. } else { // string tensor, for now, only tensor with 1 string is supported!
  55. CHECK_FAIL_RETURN_UNEXPECTED(in_row[ind]->shape().NumOfElements() == 1,
  56. "String tensor with more than 1 element is not yet supported.");
  57. // get the first and only string in this tensor
  58. std::string str1(*(in_row[ind]->begin<std::string_view>()));
  59. tensor.buffer_.resize(str1.size());
  60. std::memcpy(tensor.buffer_.data(), str1.data(), str1.size());
  61. }
  62. tensor.shape_ = in_row[ind]->shape().AsVector();
  63. tensor.type_ = in_row[ind]->type().ToString();
  64. }
  65. return Status::OK();
  66. }
  67. Status PluginOp::Compute(const TensorRow &input, TensorRow *output) {
  68. // Compute should quit if init fails. Error code has already been logged, no need to repeat
  69. RETURN_IF_NOT_OK(init_code_);
  70. std::vector<plugin::Tensor> in_row, out_row;
  71. RETURN_IF_NOT_OK(TensorRowToPlugin(input, &in_row));
  72. plugin::Status rc = plugin_op_->Compute(&in_row, &out_row);
  73. CHECK_FAIL_RETURN_UNEXPECTED(rc.IsOk(), rc.ToString());
  74. RETURN_IF_NOT_OK(PluginToTensorRow(out_row, output));
  75. return Status::OK();
  76. }
  77. PluginOp::PluginOp(const std::string &lib_path, const std::string &func_name, const std::string &user_args)
  78. : lib_path_(lib_path), func_name_(func_name), user_args_(user_args) {
  79. init_code_ = Init();
  80. }
  81. Status PluginOp::Init() {
  82. plugin::PluginManagerBase *plugin;
  83. RETURN_IF_NOT_OK(PluginLoader::GetInstance()->LoadPlugin(lib_path_, &plugin));
  84. // casting a void pointer to specific type
  85. plugin_op_ = dynamic_cast<plugin::TensorOp *>(plugin->GetModule(func_name_));
  86. RETURN_UNEXPECTED_IF_NULL(plugin_op_);
  87. plugin::Status rc = plugin_op_->ParseSerializedArgs(user_args_);
  88. CHECK_FAIL_RETURN_UNEXPECTED(rc.IsOk(), rc.ToString());
  89. return Status::OK();
  90. }
  91. } // namespace dataset
  92. } // namespace mindspore