/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd/isnan/ccv_nnc_reduce_isnan_cpu_ref.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "ccv.h" |
2 | | #include "ccv_internal.h" |
3 | | #include "nnc/ccv_nnc.h" |
4 | | #include "nnc/ccv_nnc_easy.h" |
5 | | #include "nnc/ccv_nnc_internal.h" |
6 | | #ifdef USE_OPENMP |
7 | | #include <omp.h> |
8 | | #endif |
9 | | #ifdef USE_DISPATCH |
10 | | #include <dispatch/dispatch.h> |
11 | | #endif |
12 | | |
13 | | // Shared methods. |
14 | | #include "../_ccv_nnc_cpu_ref.h" |
15 | | |
16 | | static int _ccv_nnc_reduce_isnan_forw(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context) |
17 | 3 | { |
18 | 3 | assert(input_size == 1); |
19 | 3 | ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)inputs[0]; |
20 | 3 | ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)outputs[0]; |
21 | 3 | assert(ccv_nnc_tensor_nd(a->info.dim) <= CCV_NNC_MAX_DIM + 2); |
22 | 3 | assert(ccv_nnc_tensor_nd(b->info.dim) <= CCV_NNC_MAX_DIM + 2); |
23 | | // Assuming this is float 32. |
24 | 3 | int adim[CCV_NNC_MAX_DIM_ALLOC]; |
25 | 3 | int bdim[CCV_NNC_MAX_DIM_ALLOC]; |
26 | 3 | ccv_nnc_tensor_view_get_dim(a, adim); |
27 | 3 | ccv_nnc_tensor_view_get_dim(b, bdim); |
28 | 3 | assert(ccv_nnc_tensor_view_check_broadcast_dim(b, adim)); |
29 | 3 | int astride[CCV_NNC_MAX_DIM_ALLOC]; |
30 | 3 | int bstride[CCV_NNC_MAX_DIM_ALLOC]; |
31 | 3 | assert(CCV_NNC_MAX_DIM == 2); // Need to change this logic for CCV_NNC_MAX_DIM == other number. |
32 | 3 | ccv_nnc_tensor_view_get_stride(a, astride); |
33 | 3 | ccv_nnc_tensor_view_get_stride(b, bstride); |
34 | 3 | int i[CCV_NNC_MAX_DIM + 2]; |
35 | 3 | int x; |
36 | 3 | _ccv_nnc_tensor_set_cpu_ref_i32(b, 0); |
37 | 3 | float* const ap = a->data.f32; |
38 | 3 | int* const bp = b->data.i32; |
39 | | // Non-optimal case, need to do skip if needed. |
40 | 6 | for (i[0] = 0; i[0] < adim[0]; i[0]++3 ) |
41 | 3 | { |
42 | 3 | float* const ap0 = ap + i[0] * astride[0]; |
43 | 3 | int* const bp0 = bdim[0] == 1 ? bp : bp + i[0] * bstride[0]0 ; |
44 | 6 | for (i[1] = 0; i[1] < adim[1]; i[1]++3 ) |
45 | 3 | { |
46 | 3 | float* ap1 = ap0 + i[1] * astride[1]; |
47 | 3 | int* const bp1 = bdim[1] == 1 ? bp0 : bp0 + i[1] * bstride[1]0 ; |
48 | 9 | for (i[2] = 0; i[2] < adim[2]; i[2]++6 ) |
49 | 6 | { |
50 | 6 | int* const bp2 = bdim[2] == 1 ? bp14 : bp1 + i[2] * bstride[2]2 ; |
51 | 6 | if (bdim[3] == 1) |
52 | 4 | { |
53 | 16 | for (x = 0; x < adim[3]; x++12 ) |
54 | 12 | if (isnan(ap1[x])) |
55 | 2 | bp2[0] = 1; |
56 | 4 | } else { |
57 | 8 | for (x = 0; x < adim[3]; x++6 ) |
58 | 6 | if (isnan(ap1[x])) |
59 | 1 | bp2[x] = 1; |
60 | 2 | } |
61 | 6 | ap1 += astride[2]; |
62 | 6 | } |
63 | 3 | } |
64 | 3 | } |
65 | 3 | return CCV_NNC_EXEC_SUCCESS; |
66 | 3 | } |
67 | | |
68 | | static int _ccv_nnc_reduce_isnan_back(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context) |
69 | 0 | { |
70 | 0 | return CCV_NNC_EXEC_INVALID; |
71 | 0 | } |
72 | | |
73 | | REGISTER_COMMAND_BACKEND(CCV_NNC_REDUCE_ISNAN_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry) |
74 | 1 | { |
75 | 1 | registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN; |
76 | 1 | registry->tensor_datatypes = CCV_32F | CCV_32S; |
77 | 1 | registry->tensor_memory = CCV_TENSOR_CPU_MEMORY; |
78 | 1 | registry->algorithms = 1; |
79 | 1 | registry->exec = _ccv_nnc_reduce_isnan_forw; |
80 | 1 | } |
81 | | |
82 | | REGISTER_COMMAND_BACKEND(CCV_NNC_REDUCE_ISNAN_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry) |
83 | 1 | { |
84 | 1 | registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN; |
85 | 1 | registry->tensor_datatypes = CCV_32F | CCV_32S; |
86 | 1 | registry->tensor_memory = CCV_TENSOR_CPU_MEMORY; |
87 | 1 | registry->algorithms = 1; |
88 | 1 | registry->exec = _ccv_nnc_reduce_isnan_back; |
89 | 1 | } |