Coverage Report

Created: 2022-07-27 23:53

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/sigmoid_loss/ccv_nnc_sigmoid_binary_crossentropy_cpu_ref.c
Line
Count
Source
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
static int _ccv_nnc_sigmoid_binary_crossentropy_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)
14
18
{
15
18
  assert(input_size == 2);
16
18
  const ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)inputs[0];
17
18
  assert(ccv_nnc_tensor_nd(a->info.dim) <= 2);
18
18
  const ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[1];
19
18
  assert(output_size == 2);
20
18
  ccv_nnc_tensor_view_t* const c = (ccv_nnc_tensor_view_t*)outputs[0];
21
18
  ccv_nnc_tensor_view_t* const d = (ccv_nnc_tensor_view_t*)outputs[1];
22
18
  int dim[CCV_NNC_MAX_DIM_ALLOC];
23
18
  int ainc[CCV_NNC_MAX_DIM_ALLOC];
24
18
  int binc[CCV_NNC_MAX_DIM_ALLOC];
25
18
  int dinc[CCV_NNC_MAX_DIM_ALLOC];
26
18
  ccv_nnc_tensor_view_get_dim(a, dim);
27
18
  assert(ccv_nnc_tensor_view_check_dim(b, dim));
28
18
  assert(ccv_nnc_tensor_view_check_dim(d, dim));
29
18
  ccv_nnc_tensor_view_get_inc(a, ainc);
30
18
  ccv_nnc_tensor_view_get_inc(b, binc);
31
18
  ccv_nnc_tensor_view_get_inc(d, dinc);
32
18
  assert(ccv_nnc_tensor_nd(a->info.dim) <= 2);
33
18
  const int batch_size = dim[CCV_NNC_MAX_DIM];
34
18
  const int count = dim[CCV_NNC_MAX_DIM + 1];
35
18
  const int astep = ainc[CCV_NNC_MAX_DIM + 1];
36
18
  const int bstep = binc[CCV_NNC_MAX_DIM + 1];
37
18
  const int dstep = dinc[CCV_NNC_MAX_DIM + 1];
38
18
  if (c)
39
6
  {
40
6
    int cinc[CCV_NNC_MAX_DIM_ALLOC];
41
6
    assert(ccv_nnc_tensor_count(c->info) == batch_size);
42
6
    ccv_nnc_tensor_view_get_inc(c, cinc);
43
6
    const int cstep = ccv_nnc_tensor_nd(c->info.dim) == 1 ? 
14
:
cinc[2
CCV_NNC_MAX_DIM2
+ 1];
44
6
    const float pos_weight = cmd.info.binary_crossentropy.pos_weight;
45
6
    if (pos_weight == 1)
46
3
    {
47
116
      
parallel_for61
(i, batch_size) {
48
116
        int j;
49
116
        const float* const ap = a->data.f32 + i * astep;
50
116
        const float* const bp = b->data.f32 + i * bstep;
51
116
        float* const dp = d->data.f32 + i * dstep;
52
116
        float cp = 0;
53
943
        for (j = 0; j < count; 
j++885
)
54
885
        {
55
885
          cp += (1 - bp[j]) * ap[j] + log(1. + exp(-ap[j]));
56
885
          dp[j] = 1. / (1. + exp(-ap[j]));
57
885
        }
58
116
        c->data.f32[i * cstep] = cp;
59
116
      } 
parallel_endfor61
60
3
    } else {
61
3
      const float pos_weight_1 = pos_weight - 1;
62
102
      
parallel_for54
(i, batch_size) {
63
102
        int j;
64
102
        const float* const ap = a->data.f32 + i * astep;
65
102
        const float* const bp = b->data.f32 + i * bstep;
66
102
        float* const dp = d->data.f32 + i * dstep;
67
102
        float cp = 0;
68
951
        for (j = 0; j < count; 
j++900
)
69
900
        {
70
900
          cp += (1 - bp[j]) * ap[j] + (1 + bp[j] * pos_weight_1) * log(1. + exp(-ap[j]));
71
900
          dp[j] = 1. / (1. + exp(-ap[j]));
72
900
        }
73
102
        c->data.f32[i * cstep] = cp;
74
102
      } 
parallel_endfor54
75
3
    }
76
12
  } else {
77
380
    
parallel_for202
(i, batch_size) {
78
380
      int j;
79
380
      const float* const ap = a->data.f32 + i * astep;
80
380
      float* const dp = d->data.f32 + i * dstep;
81
3.83k
      for (j = 0; j < count; 
j++3.64k
)
82
3.64k
        dp[j] = 1. / (1. + exp(-ap[j]));
83
380
    } 
parallel_endfor202
84
12
  }
85
18
  return CCV_NNC_EXEC_SUCCESS;
86
18
}
87
88
static int _ccv_nnc_sigmoid_binary_crossentropy_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)
89
10
{
90
10
  assert(input_size >= 6);
91
10
  assert(output_size >= 1);
92
10
  const ccv_nnc_tensor_view_t* const g = (ccv_nnc_tensor_view_t*)inputs[0];
93
10
  assert(!g || !CCV_IS_TENSOR_VIEW(g));
94
10
  const ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)inputs[5];
95
10
  const ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[3];
96
10
  ccv_nnc_tensor_view_t* const h = (ccv_nnc_tensor_view_t*)outputs[0];
97
10
  int dim[CCV_NNC_MAX_DIM_ALLOC];
98
10
  int ainc[CCV_NNC_MAX_DIM_ALLOC];
99
10
  int binc[CCV_NNC_MAX_DIM_ALLOC];
100
10
  int hinc[CCV_NNC_MAX_DIM_ALLOC];
101
10
  ccv_nnc_tensor_view_get_dim(a, dim);
102
10
  assert(ccv_nnc_tensor_view_check_dim(b, dim));
103
10
  assert(ccv_nnc_tensor_view_check_dim(h, dim));
104
10
  ccv_nnc_tensor_view_get_inc(a, ainc);
105
10
  ccv_nnc_tensor_view_get_inc(b, binc);
106
10
  ccv_nnc_tensor_view_get_inc(h, hinc);
107
10
  assert(ccv_nnc_tensor_nd(a->info.dim) <= 2);
108
10
  const int batch_size = dim[CCV_NNC_MAX_DIM];
109
10
  const int count = dim[CCV_NNC_MAX_DIM + 1];
110
10
  const int astep = ainc[CCV_NNC_MAX_DIM + 1];
111
10
  const int bstep = binc[CCV_NNC_MAX_DIM + 1];
112
10
  const int hstep = hinc[CCV_NNC_MAX_DIM + 1];
113
10
  const float pos_weight = cmd.info.binary_crossentropy.pos_weight;
114
10
  if (pos_weight == 1)
115
5
  {
116
5
    if (g)
117
3
    {
118
3
      int ginc[CCV_NNC_MAX_DIM_ALLOC];
119
3
      ccv_nnc_tensor_view_get_inc(g, ginc);
120
3
      assert(ccv_nnc_tensor_count(g->info) == batch_size);
121
3
      const int gstep = ccv_nnc_tensor_nd(g->info.dim) == 1 ? 
12
:
ginc[1
CCV_NNC_MAX_DIM1
+ 1];
122
68
      
parallel_for37
(i, batch_size) {
123
68
        int j;
124
68
        const float gp = g->data.f32[i * gstep];
125
68
        const float* const ap = a->data.f32 + i * astep;
126
68
        const float* const bp = b->data.f32 + i * bstep;
127
68
        float* const hp = h->data.f32 + i * hstep;
128
572
        for (j = 0; j < count; 
j++538
)
129
538
          hp[j] = gp * (ap[j] - bp[j]);
130
68
      } 
parallel_endfor37
131
3
    } else {
132
54
      
parallel_for29
(i, batch_size) {
133
54
        int j;
134
54
        const float* const ap = a->data.f32 + i * astep;
135
54
        const float* const bp = b->data.f32 + i * bstep;
136
54
        float* const hp = h->data.f32 + i * hstep;
137
312
        for (j = 0; j < count; 
j++285
)
138
285
          hp[j] = ap[j] - bp[j];
139
54
      } 
parallel_endfor29
140
2
    }
141
5
  } else {
142
5
    if (g)
143
3
    {
144
3
      int ginc[CCV_NNC_MAX_DIM_ALLOC];
145
3
      ccv_nnc_tensor_view_get_inc(g, ginc);
146
3
      assert(ccv_nnc_tensor_count(g->info) == batch_size);
147
3
      const int gstep = ccv_nnc_tensor_nd(g->info.dim) == 1 ? 
12
:
ginc[1
CCV_NNC_MAX_DIM1
+ 1];
148
126
      
parallel_for66
(i, batch_size) {
149
126
        int j;
150
126
        const float gp = g->data.f32[i * gstep];
151
126
        const float* const ap = a->data.f32 + i * astep;
152
126
        const float* const bp = b->data.f32 + i * bstep;
153
126
        float* const hp = h->data.f32 + i * hstep;
154
646
        for (j = 0; j < count; 
j++583
)
155
583
          hp[j] = gp * ((ap[j] - 1) * bp[j] * pos_weight + ap[j] * (1 - bp[j]));
156
126
      } 
parallel_endfor66
157
3
    } else {
158
92
      
parallel_for48
(i, batch_size) {
159
92
        int j;
160
92
        const float* const ap = a->data.f32 + i * astep;
161
92
        const float* const bp = b->data.f32 + i * bstep;
162
92
        float* const hp = h->data.f32 + i * hstep;
163
503
        for (j = 0; j < count; 
j++457
)
164
457
          hp[j] = (ap[j] - 1) * bp[j] * pos_weight + ap[j] * (1 - bp[j]);
165
92
      } 
parallel_endfor48
166
2
    }
167
5
  }
168
10
  return CCV_NNC_EXEC_SUCCESS;
169
10
}
170
171
REGISTER_COMMAND_BACKEND(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
172
1
{
173
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW;
174
1
  registry->tensor_datatypes = CCV_32F;
175
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
176
1
  registry->algorithms = 1;
177
1
  registry->exec = _ccv_nnc_sigmoid_binary_crossentropy_forw;
178
1
}
179
180
REGISTER_COMMAND_BACKEND(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
181
1
{
182
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW;
183
1
  registry->tensor_datatypes = CCV_32F;
184
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
185
1
  registry->algorithms = 1;
186
1
  registry->exec = _ccv_nnc_sigmoid_binary_crossentropy_back;
187
1
}