nncase_v1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright 2019 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <cstring>
  16. #include <nncase/runtime/interpreter.h>
  17. #include <nncase/runtime/runtime_op_utility.h>
  18. #include <nncase_v1.h>
  19. #include <stdio.h>
  20. #include <sysctl.h>
  21. #include <utils.h>
  22. using namespace nncase;
  23. using namespace nncase::runtime;
  24. #define NNCASE_DEBUG 0
  25. namespace
  26. {
  27. class nncase_context
  28. {
  29. public:
  30. result<void> load_kmodel(const uint8_t *buffer)
  31. {
  32. return interp_.load_model({ reinterpret_cast<const gsl::byte *>(buffer), SIZE_MAX });
  33. }
  34. result<void> get_output(uint32_t index, uint8_t **data, size_t *size)
  35. {
  36. try_var(tensor, interp_.output_tensor(index));
  37. try_var(host_tensor, tensor.as_host());
  38. try_var(map, hrt::map(host_tensor, hrt::map_read));
  39. auto buffer = map.buffer();
  40. *data = reinterpret_cast<uint8_t *>(buffer.data());
  41. *size = buffer.size_bytes();
  42. return ok();
  43. }
  44. result<void> run_kmodel(const uint8_t *src, dmac_channel_number_t dma_ch)
  45. {
  46. #if NNCASE_DEBUG
  47. auto micro = sysctl_get_time_us();
  48. #endif
  49. try_(interp_.options().set("dma_ch", (uint32_t)dma_ch));
  50. auto &shape = interp_.input_shape(0);
  51. auto type = interp_.input_desc(0).datatype;
  52. try_var(input_tensor, hrt::create(type, shape, { (gsl::byte *)src, get_bytes(type, shape) }, false, hrt::pool_shared));
  53. try_(hrt::sync(input_tensor, hrt::sync_write_back));
  54. try_(interp_.input_tensor(0, input_tensor));
  55. try_(interp_.run());
  56. #if NNCASE_DEBUG
  57. auto duration = sysctl_get_time_us() - micro;
  58. printf("run kmodel takes %f ms.\n", duration / 1e3f);
  59. #endif
  60. return ok();
  61. }
  62. private:
  63. interpreter interp_;
  64. };
  65. }
  66. int nncase_v1_load_kmodel(kpu_model_context_t *ctx, const uint8_t *buffer)
  67. {
  68. auto nnctx = new (std::nothrow) nncase_context();
  69. if (ctx)
  70. {
  71. ctx->is_nncase = 1;
  72. ctx->nncase_ctx = nnctx;
  73. ctx->nncase_version = 1;
  74. auto ret = nnctx->load_kmodel(buffer);
  75. if (ret.is_ok())
  76. return 0;
  77. return -1;
  78. }
  79. else
  80. {
  81. return -1;
  82. }
  83. }
  84. int nncase_v1_get_output(kpu_model_context_t *ctx, uint32_t index, uint8_t **data, size_t *size)
  85. {
  86. auto nnctx = reinterpret_cast<nncase_context *>(ctx->nncase_ctx);
  87. auto ret = nnctx->get_output(index, data, size);
  88. if (ret.is_ok())
  89. return 0;
  90. return -1;
  91. }
  92. void nncase_v1_model_free(kpu_model_context_t *ctx)
  93. {
  94. auto nnctx = reinterpret_cast<nncase_context *>(ctx->nncase_ctx);
  95. delete nnctx;
  96. ctx->nncase_ctx = nullptr;
  97. }
  98. int nncase_v1_run_kmodel(kpu_model_context_t *ctx, const uint8_t *src, dmac_channel_number_t dma_ch, kpu_done_callback_t done_callback, void *userdata)
  99. {
  100. auto nnctx = reinterpret_cast<nncase_context *>(ctx->nncase_ctx);
  101. auto ret = nnctx->run_kmodel(src, dma_ch);
  102. if (ret.is_ok())
  103. {
  104. done_callback(userdata);
  105. return 0;
  106. }
  107. else
  108. {
  109. printf("error: %s\n", ret.unwrap_err().message().c_str());
  110. }
  111. return -1;
  112. }