Coverage Report

Created: 2021-04-07 21:56

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/blas/ccv_nnc_mul_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
void _ccv_nnc_mul_forw_cpu_ref(const float p, ccv_nnc_tensor_view_t* const a, ccv_nnc_tensor_view_t* const b, ccv_nnc_tensor_view_t* const c)
17
10.1k
{
18
10.1k
  if (b == 0)
19
80
  {
20
80
    if (p == 1)
21
0
    {
22
0
      _ccv_nnc_tensor_transfer_cpu_ref_f32(a, c);
23
0
      return;
24
80
    } else if (p == 0) {
25
0
      ccv_nnc_tensor_zero(c);
26
0
      return;
27
0
    }
28
80
    // Assuming this is float 32.
29
80
    int dim[CCV_NNC_MAX_DIM_ALLOC];
30
80
    int ainc[CCV_NNC_MAX_DIM_ALLOC];
31
80
    int cinc[CCV_NNC_MAX_DIM_ALLOC];
32
80
    assert(ccv_nnc_tensor_nd(a->info.dim) <= CCV_NNC_MAX_DIM + 2);
33
80
    assert(ccv_nnc_tensor_nd(c->info.dim) <= CCV_NNC_MAX_DIM + 2);
34
80
    ccv_nnc_tensor_view_get_dim(a, dim);
35
80
    assert(ccv_nnc_tensor_view_check_dim(c, dim));
36
80
    int x;
37
80
    if (!CCV_IS_TENSOR_VIEW(a) && !CCV_IS_TENSOR_VIEW(c))
38
80
    {
39
76
      // Super optimal case, just do one for-loop for sum.
40
76
      const int tensor_count = ccv_nnc_tensor_count(a->info);
41
726
      for (x = 0; x < tensor_count; 
x++650
)
42
650
        c->data.f32[x] = p * a->data.f32[x];
43
76
      return;
44
76
    }
45
4
    assert(CCV_NNC_MAX_DIM == 2); // Need to change this logic for CCV_NNC_MAX_DIM == other number.
46
4
    ccv_nnc_tensor_view_get_inc(a, ainc);
47
4
    ccv_nnc_tensor_view_get_inc(c, cinc);
48
4
    int i[CCV_NNC_MAX_DIM + 2];
49
4
    float* ap = a->data.f32;
50
4
    float* cp = c->data.f32;
51
4
    const int count = dim[2] * dim[3];
52
4
    if (ainc[3] == dim[3] && cinc[3] == dim[3])
53
0
    {
54
0
      // Special casing if the ainc[3] is the same as dim[3]
55
0
      for (i[0] = 0; i[0] < dim[0]; i[0]++)
56
0
      {
57
0
        for (i[1] = 0; i[1] < dim[1]; i[1]++)
58
0
        {
59
0
          for (x = 0; x < count; x++)
60
0
            cp[x] = p * ap[x];
61
0
          ap += ainc[2] * ainc[3];
62
0
          cp += cinc[2] * cinc[3];
63
0
        }
64
0
        ap += (ainc[1] - dim[1]) * ainc[2] * ainc[3];
65
0
        cp += (cinc[1] - dim[1]) * cinc[2] * cinc[3];
66
0
      }
67
0
      return;
68
0
    }
69
4
    // Non-optimal case, need to do skip copy.
70
8
    
for (i[0] = 0; 4
i[0] < dim[0];
i[0]++4
)
71
4
    {
72
8
      for (i[1] = 0; i[1] < dim[1]; 
i[1]++4
)
73
4
      {
74
8
        for (i[2] = 0; i[2] < dim[2]; 
i[2]++4
)
75
4
        {
76
8
          for (x = 0; x < dim[3]; 
x++4
)
77
4
            cp[x] = p * ap[x];
78
4
          ap += ainc[3];
79
4
          cp += cinc[3];
80
4
        }
81
4
        ap += (ainc[2] - dim[2]) * ainc[3];
82
4
        cp += (cinc[2] - dim[2]) * cinc[3];
83
4
      }
84
4
      ap += (ainc[1] - dim[1]) * ainc[2] * ainc[3];
85
4
      cp += (cinc[1] - dim[1]) * cinc[2] * cinc[3];
86
4
    }
87
4
    return;
88
4
  }
89
10.0k
  int cdim[CCV_NNC_MAX_DIM_ALLOC];
90
10.0k
  assert(ccv_nnc_tensor_nd(a->info.dim) <= CCV_NNC_MAX_DIM + 2);
91
10.0k
  assert(ccv_nnc_tensor_nd(b->info.dim) <= CCV_NNC_MAX_DIM + 2);
92
10.0k
  ccv_nnc_tensor_view_get_dim(a, cdim); // Fill in cdim first.
93
10.0k
  ccv_nnc_tensor_view_get_broadcast_dim(b, cdim);
94
10.0k
  assert(ccv_nnc_tensor_view_check_broadcast_dim(a, cdim));
95
10.0k
  assert(ccv_nnc_tensor_view_check_broadcast_dim(b, cdim));
96
10.0k
  const int a_check_dim = ccv_nnc_tensor_view_check_dim(a, cdim);
97
10.0k
  const int b_check_dim = ccv_nnc_tensor_view_check_dim(b, cdim);
98
10.0k
  if (p == 1 && 
a_check_dim10.0k
&&
b_check_dim10.0k
)
99
10.0k
  {
100
10.0k
    _ccv_nnc_ewprod_forw_cpu_ref((ccv_nnc_tensor_view_t*[]){
101
10.0k
      a, b
102
10.0k
    }, 2, &c, 1);
103
10.0k
    return;
104
10.0k
  } else 
if (14
p == 014
) {
105
0
    ccv_nnc_tensor_zero(c);
106
0
    return;
107
0
  }
108
14
  // Assuming this is float 32.
109
14
  int adim[CCV_NNC_MAX_DIM_ALLOC];
110
14
  int bdim[CCV_NNC_MAX_DIM_ALLOC];
111
14
  ccv_nnc_tensor_view_get_dim(a, adim);
112
14
  ccv_nnc_tensor_view_get_dim(b, bdim);
113
14
  int ainc[CCV_NNC_MAX_DIM_ALLOC];
114
14
  int binc[CCV_NNC_MAX_DIM_ALLOC];
115
14
  int cinc[CCV_NNC_MAX_DIM_ALLOC];
116
14
  assert(ccv_nnc_tensor_nd(c->info.dim) <= CCV_NNC_MAX_DIM + 2);
117
14
  assert(ccv_nnc_tensor_view_check_dim(c, cdim));
118
14
  int x;
119
14
  if (!CCV_IS_TENSOR_VIEW(a) && !CCV_IS_TENSOR_VIEW(b) && !CCV_IS_TENSOR_VIEW(c) && a_check_dim && 
b_check_dim9
)
120
3
  {
121
3
    const int tensor_count = ccv_nnc_tensor_count(a->info);
122
3
    // Super optimal case, just do one for-loop for sum.
123
33
    for (x = 0; x < tensor_count; 
x++30
)
124
30
      c->data.f32[x] = p * a->data.f32[x] * b->data.f32[x];
125
3
    return;
126
3
  }
127
11
  assert(CCV_NNC_MAX_DIM == 2); // Need to change this logic for CCV_NNC_MAX_DIM == other number.
128
11
  ccv_nnc_tensor_view_get_inc(a, ainc);
129
11
  ccv_nnc_tensor_view_get_inc(b, binc);
130
11
  ccv_nnc_tensor_view_get_inc(c, cinc);
131
11
  int i[CCV_NNC_MAX_DIM + 2];
132
11
  float* ap = a->data.f32;
133
11
  float* bp = b->data.f32;
134
11
  float* cp = c->data.f32;
135
11
  const int count = cdim[2] * cdim[3];
136
11
  if (ainc[3] == cdim[3] && 
binc[3] == cdim[3]6
&&
cinc[3] == cdim[3]3
&&
adim[2] == cdim[2]3
&&
bdim[2] == cdim[2]3
)
137
0
  {
138
0
    // Special casing if the ainc[3] is the same as dim[3]
139
0
    for (i[0] = 0; i[0] < cdim[0]; i[0]++)
140
0
    {
141
0
      float* const ap0 = adim[0] == 1 ? ap : ap + i[0] * ainc[1] * ainc[2] * ainc[3];
142
0
      float* const bp0 = bdim[0] == 1 ? bp : bp + i[0] * binc[1] * binc[2] * binc[3];
143
0
      for (i[1] = 0; i[1] < cdim[1]; i[1]++)
144
0
      {
145
0
        float* const ap1 = adim[1] == 1 ? ap0 : ap0 + i[1] * ainc[2] * ainc[3];
146
0
        float* const bp1 = bdim[1] == 1 ? bp0 : bp0 + i[1] * binc[2] * binc[3];
147
0
        for (x = 0; x < count; x++)
148
0
          cp[x] = p * ap1[x] * bp1[x];
149
0
        cp += cinc[2] * cinc[3];
150
0
      }
151
0
      cp += (cinc[1] - cdim[1]) * cinc[2] * cinc[3];
152
0
    }
153
0
    return;
154
0
  }
155
11
  // Non-optimal case, need to do skip copy and handle broadcasting.
156
44
  
for (i[0] = 0; 11
i[0] < cdim[0];
i[0]++33
)
157
33
  {
158
33
    float* const ap0 = adim[0] == 1 ? 
ap7
:
ap + i[0] * ainc[1] * ainc[2] * ainc[3]26
;
159
33
    float* const bp0 = bdim[0] == 1 ? 
bp17
:
bp + i[0] * binc[1] * binc[2] * binc[3]16
;
160
140
    for (i[1] = 0; i[1] < cdim[1]; 
i[1]++107
)
161
107
    {
162
107
      float* const ap1 = adim[1] == 1 ? 
ap07
:
ap0 + i[1] * ainc[2] * ainc[3]100
;
163
107
      float* const bp1 = bdim[1] == 1 ? bp0 : 
bp0 + i[1] * binc[2] * binc[3]0
;
164
510
      for (i[2] = 0; i[2] < cdim[2]; 
i[2]++403
)
165
403
      {
166
403
        float* const ap2 = adim[2] == 1 ? 
ap15
:
ap1 + i[2] * ainc[3]398
;
167
403
        float* const bp2 = bdim[2] == 1 ? bp1 : 
bp1 + i[2] * binc[3]0
;
168
403
        if (adim[3] == 1)
169
25
          
for (x = 0; 8
x < cdim[3];
x++17
)
170
17
            cp[x] = p * ap2[0] * bp2[x];
171
395
        else if (bdim[3] == 1)
172
2.91k
          
for (x = 0; 257
x < cdim[3];
x++2.66k
)
173
2.66k
            cp[x] = p * ap2[x] * bp2[0];
174
138
        else
175
1.50k
          
for (x = 0; 138
x < cdim[3];
x++1.36k
)
176
1.36k
            cp[x] = p * ap2[x] * bp2[x];
177
403
        cp += cinc[3];
178
403
      }
179
107
      cp += (cinc[2] - cdim[2]) * cinc[3];
180
107
    }
181
33
    cp += (cinc[1] - cdim[1]) * cinc[2] * cinc[3];
182
33
  }
183
11
}
184
185
static int _ccv_nnc_mul_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)
186
4.02k
{
187
4.02k
  assert(input_size == 2);
188
4.02k
  _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[0], (ccv_nnc_tensor_view_t*)inputs[1], (ccv_nnc_tensor_view_t*)outputs[0]);
189
4.02k
  return CCV_NNC_EXEC_SUCCESS;
190
4.02k
}
191
192
static int _ccv_nnc_mul_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)
193
4.01k
{
194
4.01k
  int gdim[CCV_NNC_MAX_DIM_ALLOC];
195
4.01k
  int no_broadcasting = 1;
196
4.01k
  if (outputs[0])
197
2.00k
  {
198
2.00k
    assert(input_size >= 3 && inputs[2]);
199
2.00k
    ccv_nnc_tensor_view_get_dim((ccv_nnc_tensor_view_t*)outputs[0], gdim);
200
2.00k
    ccv_nnc_tensor_view_get_broadcast_dim((ccv_nnc_tensor_view_t*)inputs[2], gdim);
201
2.00k
    no_broadcasting = no_broadcasting && (ccv_nnc_tensor_view_check_dim((ccv_nnc_tensor_view_t*)outputs[0], gdim) && 
ccv_nnc_tensor_view_check_dim((ccv_nnc_tensor_view_t*)inputs[2], gdim)2.00k
);
202
2.00k
  }
203
4.01k
  if (no_broadcasting && 
output_size > 14.00k
&&
outputs[1]4.00k
)
204
4.00k
  {
205
4.00k
    assert(inputs[1]);
206
4.00k
    ccv_nnc_tensor_view_get_dim((ccv_nnc_tensor_view_t*)inputs[1], gdim);
207
4.00k
    ccv_nnc_tensor_view_get_broadcast_dim((ccv_nnc_tensor_view_t*)outputs[1], gdim);
208
4.00k
    no_broadcasting = no_broadcasting && (ccv_nnc_tensor_view_check_dim((ccv_nnc_tensor_view_t*)inputs[1], gdim) && ccv_nnc_tensor_view_check_dim((ccv_nnc_tensor_view_t*)outputs[1], gdim));
209
4.00k
  }
210
4.01k
  if (no_broadcasting)
211
4.00k
  {
212
4.00k
    if (outputs[0])
213
2.00k
    {
214
2.00k
      if (inputs[0] == 0)
215
0
        _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[2], 0, (ccv_nnc_tensor_view_t*)outputs[0]);
216
2.00k
      else
217
2.00k
        _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[0], (ccv_nnc_tensor_view_t*)inputs[2], (ccv_nnc_tensor_view_t*)outputs[0]);
218
2.00k
    }
219
4.00k
    if (output_size > 1 && outputs[1])
220
4.00k
    {
221
4.00k
      if (inputs[0] == 0)
222
0
        _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[1], 0, (ccv_nnc_tensor_view_t*)outputs[1]);
223
4.00k
      else
224
4.00k
        _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[0], (ccv_nnc_tensor_view_t*)inputs[1], (ccv_nnc_tensor_view_t*)outputs[1]);
225
4.00k
    }
226
4.00k
    return CCV_NNC_EXEC_SUCCESS;
227
4.00k
  }
228
7
  int adim[CCV_NNC_MAX_DIM_ALLOC];
229
7
  int bdim[CCV_NNC_MAX_DIM_ALLOC];
230
7
  int ainc[CCV_NNC_MAX_DIM_ALLOC];
231
7
  int binc[CCV_NNC_MAX_DIM_ALLOC];
232
7
  int i[CCV_NNC_MAX_DIM + 2];
233
7
  int x;
234
7
  const float p = cmd.info.blas.a[0];
235
7
  // Now the case we need broadcasting.
236
7
  if (inputs[0] == 0)
237
3
  {
238
3
    if (outputs[0])
239
3
    {
240
3
      ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)outputs[0];
241
3
      ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[2];
242
3
      ccv_nnc_tensor_view_get_dim(a, adim);
243
3
      ccv_nnc_tensor_view_get_dim(b, bdim);
244
3
      ccv_nnc_tensor_view_get_inc(a, ainc);
245
3
      ccv_nnc_tensor_view_get_inc(b, binc);
246
3
      ccv_nnc_tensor_zero(a);
247
3
      float* const ap = a->data.f32;
248
3
      float* const bp = b->data.f32;
249
6
      for (i[0] = 0; i[0] < gdim[0]; 
i[0]++3
)
250
3
      {
251
3
        float* const ap0 = adim[0] == 1 ? ap : 
ap + i[0] * ainc[1] * ainc[2] * ainc[3]0
;
252
3
        float* const bp0 = bdim[0] == 1 ? bp : 
bp + i[0] * binc[1] * binc[2] * binc[3]0
;
253
6
        for (i[1] = 0; i[1] < gdim[1]; 
i[1]++3
)
254
3
        {
255
3
          float* const ap1 = adim[1] == 1 ? ap0 : 
ap0 + i[1] * ainc[2] * ainc[3]0
;
256
3
          float* const bp1 = bdim[1] == 1 ? bp0 : 
bp0 + i[1] * binc[2] * binc[3]0
;
257
11
          for (i[2] = 0; i[2] < gdim[2]; 
i[2]++8
)
258
8
          {
259
8
            float* const ap2 = adim[2] == 1 ? 
ap12
:
ap1 + i[2] * ainc[3]6
;
260
8
            float* const bp2 = bdim[2] == 1 ? 
bp16
:
bp1 + i[2] * binc[3]2
;
261
8
            if (adim[3] == 1)
262
12
              
for (x = 0; 4
x < gdim[3];
x++8
)
263
8
                ap2[0] += p * bp2[x];
264
4
            else if (bdim[3] == 1)
265
0
              for (x = 0; x < gdim[3]; x++)
266
0
                ap2[x] += p * bp2[0];
267
4
            else
268
16
              
for (x = 0; 4
x < gdim[3];
x++12
)
269
12
                ap2[x] += p * bp2[x];
270
8
          }
271
3
        }
272
3
      }
273
3
    }
274
3
    if (output_size > 1 && outputs[1])
275
3
    {
276
3
      ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)outputs[1];
277
3
      ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[1];
278
3
      ccv_nnc_tensor_view_get_dim(a, adim);
279
3
      ccv_nnc_tensor_view_get_dim(b, bdim);
280
3
      ccv_nnc_tensor_view_get_inc(a, ainc);
281
3
      ccv_nnc_tensor_view_get_inc(b, binc);
282
3
      ccv_nnc_tensor_zero(a);
283
3
      float* const ap = a->data.f32;
284
3
      float* const bp = b->data.f32;
285
6
      for (i[0] = 0; i[0] < gdim[0]; 
i[0]++3
)
286
3
      {
287
3
        float* const ap0 = adim[0] == 1 ? ap : 
ap + i[0] * ainc[1] * ainc[2] * ainc[3]0
;
288
3
        float* const bp0 = bdim[0] == 1 ? bp : 
bp + i[0] * binc[1] * binc[2] * binc[3]0
;
289
6
        for (i[1] = 0; i[1] < gdim[1]; 
i[1]++3
)
290
3
        {
291
3
          float* const ap1 = adim[1] == 1 ? ap0 : 
ap0 + i[1] * ainc[2] * ainc[3]0
;
292
3
          float* const bp1 = bdim[1] == 1 ? bp0 : 
bp0 + i[1] * binc[2] * binc[3]0
;
293
11
          for (i[2] = 0; i[2] < gdim[2]; 
i[2]++8
)
294
8
          {
295
8
            float* const ap2 = adim[2] == 1 ? 
ap16
:
ap1 + i[2] * ainc[3]2
;
296
8
            float* const bp2 = bdim[2] == 1 ? 
bp12
:
bp1 + i[2] * binc[3]6
;
297
8
            if (adim[3] == 1)
298
0
              for (x = 0; x < gdim[3]; x++)
299
0
                ap2[0] += p * bp2[x];
300
8
            else if (bdim[3] == 1)
301
12
              
for (x = 0; 4
x < gdim[3];
x++8
)
302
8
                ap2[x] += p * bp2[0];
303
4
            else
304
16
              
for (x = 0; 4
x < gdim[3];
x++12
)
305
12
                ap2[x] += p * bp2[x];
306
8
          }
307
3
        }
308
3
      }
309
3
    }
310
3
    return CCV_NNC_EXEC_SUCCESS;
311
3
  }
312
4
  int ginc[CCV_NNC_MAX_DIM_ALLOC];
313
4
  ccv_nnc_tensor_view_t* const g = (ccv_nnc_tensor_view_t*)inputs[0];
314
4
  ccv_nnc_tensor_view_get_dim(g, gdim);
315
4
  ccv_nnc_tensor_view_get_inc(g, ginc);
316
4
  if (outputs[0])
317
4
  {
318
4
    ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)outputs[0];
319
4
    ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[2];
320
4
    ccv_nnc_tensor_view_get_dim(a, adim);
321
4
    ccv_nnc_tensor_view_get_dim(b, bdim);
322
4
    ccv_nnc_tensor_view_get_inc(a, ainc);
323
4
    ccv_nnc_tensor_view_get_inc(b, binc);
324
4
    ccv_nnc_tensor_zero(a);
325
4
    float* const ap = a->data.f32;
326
4
    float* const bp = b->data.f32;
327
4
    float* gp = g->data.f32;
328
16
    for (i[0] = 0; i[0] < gdim[0]; 
i[0]++12
)
329
12
    {
330
12
      float* const ap0 = adim[0] == 1 ? 
ap2
:
ap + i[0] * ainc[1] * ainc[2] * ainc[3]10
;
331
12
      float* const bp0 = bdim[0] == 1 ? 
bp4
:
bp + i[0] * binc[1] * binc[2] * binc[3]8
;
332
50
      for (i[1] = 0; i[1] < gdim[1]; 
i[1]++38
)
333
38
      {
334
38
        float* const ap1 = adim[1] == 1 ? 
ap02
:
ap0 + i[1] * ainc[2] * ainc[3]36
;
335
38
        float* const bp1 = bdim[1] == 1 ? bp0 : 
bp0 + i[1] * binc[2] * binc[3]0
;
336
179
        for (i[2] = 0; i[2] < gdim[2]; 
i[2]++141
)
337
141
        {
338
141
          float* const ap2 = adim[2] == 1 ? 
ap11
:
ap1 + i[2] * ainc[3]140
;
339
141
          float* const bp2 = bdim[2] == 1 ? bp1 : 
bp1 + i[2] * binc[3]0
;
340
141
          if (adim[3] == 1)
341
12
            
for (x = 0; 4
x < gdim[3];
x++8
)
342
8
              ap2[0] += p * gp[x] * bp2[x];
343
137
          else if (bdim[3] == 1)
344
1.50k
            
for (x = 0; 129
x < gdim[3];
x++1.38k
)
345
1.38k
              ap2[x] += p * gp[x] * bp2[0];
346
8
          else
347
88
            
for (x = 0; 8
x < gdim[3];
x++80
)
348
80
              ap2[x] += p * gp[x] * bp2[x];
349
141
          gp += ginc[3];
350
141
        }
351
38
        gp += (ginc[2] - gdim[2]) * ginc[3];
352
38
      }
353
12
      gp += (ginc[1] - gdim[1]) * ginc[2] * ginc[3];
354
12
    }
355
4
  }
356
4
  if (output_size > 1 && outputs[1])
357
4
  {
358
4
    ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)outputs[1];
359
4
    ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)inputs[1];
360
4
    ccv_nnc_tensor_view_get_dim(a, adim);
361
4
    ccv_nnc_tensor_view_get_dim(b, bdim);
362
4
    ccv_nnc_tensor_view_get_inc(a, ainc);
363
4
    ccv_nnc_tensor_view_get_inc(b, binc);
364
4
    ccv_nnc_tensor_zero(a);
365
4
    float* const ap = a->data.f32;
366
4
    float* const bp = b->data.f32;
367
4
    float* gp = g->data.f32;
368
16
    for (i[0] = 0; i[0] < gdim[0]; 
i[0]++12
)
369
12
    {
370
12
      float* const ap0 = adim[0] == 1 ? 
ap4
:
ap + i[0] * ainc[1] * ainc[2] * ainc[3]8
;
371
12
      float* const bp0 = bdim[0] == 1 ? 
bp2
:
bp + i[0] * binc[1] * binc[2] * binc[3]10
;
372
50
      for (i[1] = 0; i[1] < gdim[1]; 
i[1]++38
)
373
38
      {
374
38
        float* const ap1 = adim[1] == 1 ? ap0 : 
ap0 + i[1] * ainc[2] * ainc[3]0
;
375
38
        float* const bp1 = bdim[1] == 1 ? 
bp02
:
bp0 + i[1] * binc[2] * binc[3]36
;
376
179
        for (i[2] = 0; i[2] < gdim[2]; 
i[2]++141
)
377
141
        {
378
141
          float* const ap2 = adim[2] == 1 ? ap1 : 
ap1 + i[2] * ainc[3]0
;
379
141
          float* const bp2 = bdim[2] == 1 ? 
bp11
:
bp1 + i[2] * binc[3]140
;
380
141
          if (adim[3] == 1)
381
1.50k
            
for (x = 0; 129
x < gdim[3];
x++1.38k
)
382
1.38k
              ap2[0] += p * gp[x] * bp2[x];
383
12
          else if (bdim[3] == 1)
384
12
            
for (x = 0; 4
x < gdim[3];
x++8
)
385
8
              ap2[x] += p * gp[x] * bp2[0];
386
8
          else
387
88
            
for (x = 0; 8
x < gdim[3];
x++80
)
388
80
              ap2[x] += p * gp[x] * bp2[x];
389
141
          gp += ginc[3];
390
141
        }
391
38
        gp += (ginc[2] - gdim[2]) * ginc[3];
392
38
      }
393
12
      gp += (ginc[1] - gdim[1]) * ginc[2] * ginc[3];
394
12
    }
395
4
  }
396
4
  return CCV_NNC_EXEC_SUCCESS;
397
4
}
398
399
REGISTER_COMMAND_BACKEND(CCV_NNC_MUL_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
400
1
{
401
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
402
1
  registry->tensor_datatypes = CCV_32F;
403
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
404
1
  registry->algorithms = 1;
405
1
  registry->exec = _ccv_nnc_mul_forw;
406
1
}
407
408
REGISTER_COMMAND_BACKEND(CCV_NNC_MUL_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
409
1
{
410
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
411
1
  registry->tensor_datatypes = CCV_32F;
412
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
413
1
  registry->algorithms = 1;
414
1
  registry->exec = _ccv_nnc_mul_back;
415
1
}
416
417
static int _ccv_nnc_scalar_mul_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)
418
23
{
419
23
  _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[0], 0, (ccv_nnc_tensor_view_t*)outputs[0]);
420
23
  return CCV_NNC_EXEC_SUCCESS;
421
23
}
422
static int _ccv_nnc_scalar_mul_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)
423
5
{
424
5
  if (inputs[0])
425
5
    _ccv_nnc_mul_forw_cpu_ref(cmd.info.blas.a[0], (ccv_nnc_tensor_view_t*)inputs[0], 0, (ccv_nnc_tensor_view_t*)outputs[0]);
426
0
  else
427
0
    _ccv_nnc_tensor_set_cpu_ref((ccv_nnc_tensor_view_t*)outputs[0], cmd.info.blas.a[0]);
428
5
  return CCV_NNC_EXEC_SUCCESS;
429
5
}
430
431
REGISTER_COMMAND_BACKEND(CCV_NNC_SCALAR_MUL_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
432
1
{
433
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
434
1
  registry->tensor_datatypes = CCV_32F;
435
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
436
1
  registry->algorithms = 1;
437
1
  registry->exec = _ccv_nnc_scalar_mul_forw;
438
1
}
439
440
REGISTER_COMMAND_BACKEND(CCV_NNC_SCALAR_MUL_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
441
1
{
442
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
443
1
  registry->tensor_datatypes = CCV_32F;
444
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
445
1
  registry->algorithms = 1;
446
1
  registry->exec = _ccv_nnc_scalar_mul_back;
447
1
}