cpu_ops.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright 2019-2020 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 <kernels/cpu/cpu_kernels.h>
  16. #include <runtime/kernel_registry.h>
  17. #include <runtime/cpu/cpu_ops_body.h>
  18. using namespace nncase;
  19. using namespace nncase::runtime;
  20. namespace nncase
  21. {
  22. namespace runtime
  23. {
  24. namespace cpu
  25. {
  26. kernel_call_result cpu_conv2d(cpu_conv2d_options &options, interpreter_t &interpreter, interpreter_step_t step)
  27. {
  28. auto input = interpreter.memory_at<float>(options.input);
  29. auto output = interpreter.memory_at<float>(options.output);
  30. kernels::cpu::conv2d(input.data(), output.data(), options.weights.data(), options.bias.data(), options.in_shape, options.out_channels, options.filter_h,
  31. options.filter_w, options.stride_h, options.stride_w, options.dilation_h, options.dilation_w, options.padding_h, options.padding_w, options.fused_activation);
  32. return kcr_done;
  33. }
  34. kernel_call_result cpu_depthwise_conv2d(cpu_depthwise_conv2d_options &options, interpreter_t &interpreter, interpreter_step_t step)
  35. {
  36. auto input = interpreter.memory_at<float>(options.input);
  37. auto output = interpreter.memory_at<float>(options.output);
  38. kernels::cpu::depthwise_conv2d(input.data(), output.data(), options.weights.data(), options.bias.data(), options.in_shape, options.filter_h,
  39. options.filter_w, options.stride_h, options.stride_w, options.dilation_h, options.dilation_w, options.padding_h, options.padding_w, options.fused_activation);
  40. return kcr_done;
  41. }
  42. runtime::kernel_call_result cpu_reduce_window2d(cpu_reduce_window2d_options &options, interpreter_t &interpreter, runtime::interpreter_step_t step)
  43. {
  44. auto input = interpreter.memory_at<float>(options.input);
  45. auto output = interpreter.memory_at<float>(options.output);
  46. auto reduce = [&](auto binary_op, auto window_op) {
  47. kernels::cpu::reduce_window2d(input.data(), output.data(), options.init_value, options.in_shape, options.filter_h, options.filter_w, options.stride_h,
  48. options.stride_w, options.dilation_h, options.dilation_w, options.padding_h, options.padding_w, options.fused_activation, binary_op, window_op);
  49. };
  50. switch (options.reduce_op)
  51. {
  52. case reduce_mean:
  53. reduce([](auto a, auto b) { return a + b; }, [](auto v, auto k) { return v / k; });
  54. return runtime::kcr_done;
  55. case reduce_min:
  56. reduce([](auto a, auto b) { return std::min(a, b); }, [](auto v, auto k) { return v; });
  57. return runtime::kcr_done;
  58. case reduce_max:
  59. reduce([](auto a, auto b) { return std::max(a, b); }, [](auto v, auto k) { return v; });
  60. return kcr_done;
  61. default:
  62. return kcr_error;
  63. }
  64. }
  65. kernel_call_result cpu_quantized_conv2d(cpu_quantized_conv2d_options &options, interpreter_t &interpreter, interpreter_step_t step)
  66. {
  67. auto input = interpreter.memory_at<uint8_t>(options.input);
  68. auto output = interpreter.memory_at<uint8_t>(options.output);
  69. kernels::cpu::quantized_conv2d(input.data(), output.data(), options.weights.data(), options.bias.data(), options.in_shape, options.out_channels, options.filter_h,
  70. options.filter_w, options.stride_h, options.stride_w, options.dilation_h, options.dilation_w, options.padding_h, options.padding_w,
  71. options.input_offset, options.filter_offset, options.output_mul, options.output_shift, options.output_offset);
  72. return kcr_done;
  73. }
  74. kernel_call_result cpu_quantized_depthwise_conv2d(cpu_quantized_depthwise_conv2d_options &options, interpreter_t &interpreter, interpreter_step_t step)
  75. {
  76. auto input = interpreter.memory_at<uint8_t>(options.input);
  77. auto output = interpreter.memory_at<uint8_t>(options.output);
  78. kernels::cpu::quantized_depthwise_conv2d(input.data(), output.data(), options.weights.data(), options.bias.data(), options.in_shape, options.filter_h,
  79. options.filter_w, options.stride_h, options.stride_w, options.dilation_h, options.dilation_w, options.padding_h, options.padding_w,
  80. options.input_offset, options.filter_offset, options.output_mul, options.output_shift, options.output_offset);
  81. return kcr_done;
  82. }
  83. }
  84. }
  85. }