nncase.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "v0/nncase_v0.h"
  16. #include "v1/nncase_v1.h"
  17. #include <cstring>
  18. #include <nncase.h>
  19. #include <stdio.h>
  20. #include <utils.h>
  21. extern "C"
  22. {
  23. struct model_header
  24. {
  25. uint32_t identifier;
  26. uint32_t version;
  27. };
  28. int nncase_load_kmodel(kpu_model_context_t *ctx, const uint8_t *buffer)
  29. {
  30. auto header = reinterpret_cast<const model_header *>(buffer);
  31. if (header->version == 4)
  32. return nncase_v0_load_kmodel(ctx, buffer);
  33. else
  34. return nncase_v1_load_kmodel(ctx, buffer);
  35. }
  36. int nncase_get_output(kpu_model_context_t *ctx, uint32_t index, uint8_t **data, size_t *size)
  37. {
  38. if (ctx->nncase_version == 0)
  39. return nncase_v0_get_output(ctx, index, data, size);
  40. else
  41. return nncase_v1_get_output(ctx, index, data, size);
  42. }
  43. void nncase_model_free(kpu_model_context_t *ctx)
  44. {
  45. if (ctx->nncase_version == 0)
  46. return nncase_v0_model_free(ctx);
  47. else
  48. return nncase_v1_model_free(ctx);
  49. }
  50. int nncase_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)
  51. {
  52. if (ctx->nncase_version == 0)
  53. return nncase_v0_run_kmodel(ctx, src, dma_ch, done_callback, userdata);
  54. else
  55. return nncase_v1_run_kmodel(ctx, src, dma_ch, done_callback, userdata);
  56. }
  57. }