Coverage Report

Created: 2021-04-05 01:08

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/rmsprop/ccv_nnc_rmsprop_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_rmsprop_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
1.00k
{
18
1.00k
  assert(input_size == 4);
19
1.00k
  assert(output_size == 3);
20
1.00k
  ccv_nnc_tensor_view_t* const g = (ccv_nnc_tensor_view_t*)inputs[0];
21
1.00k
  ccv_nnc_tensor_view_t* const a = (ccv_nnc_tensor_view_t*)inputs[1];
22
1.00k
  ccv_nnc_tensor_view_t* const m = (ccv_nnc_tensor_view_t*)inputs[2];
23
1.00k
  ccv_nnc_tensor_view_t* const v = (ccv_nnc_tensor_view_t*)inputs[3];
24
1.00k
  ccv_nnc_tensor_view_t* const b = (ccv_nnc_tensor_view_t*)outputs[0];
25
1.00k
  ccv_nnc_tensor_view_t* const n = (ccv_nnc_tensor_view_t*)outputs[1];
26
1.00k
  ccv_nnc_tensor_view_t* const u = (ccv_nnc_tensor_view_t*)outputs[2];
27
1.00k
  assert(ccv_nnc_tensor_nd(a->info.dim) <= CCV_NNC_MAX_DIM + 2);
28
1.00k
  assert(ccv_nnc_tensor_nd(b->info.dim) <= CCV_NNC_MAX_DIM + 2);
29
1.00k
  // Assuming this is float 32.
30
1.00k
  int adim[CCV_NNC_MAX_DIM_ALLOC];
31
1.00k
  ccv_nnc_tensor_view_get_dim(a, adim);
32
1.00k
  assert(ccv_nnc_tensor_view_check_dim(g, adim));
33
1.00k
  assert(ccv_nnc_tensor_view_check_dim(m, adim));
34
1.00k
  assert(ccv_nnc_tensor_view_check_dim(v, adim));
35
1.00k
  assert(ccv_nnc_tensor_view_check_dim(b, adim));
36
1.00k
  assert(ccv_nnc_tensor_view_check_dim(n, adim));
37
1.00k
  assert(ccv_nnc_tensor_view_check_dim(u, adim));
38
1.00k
  assert(CCV_NNC_MAX_DIM == 2); // Need to change this logic for CCV_NNC_MAX_DIM == other number.
39
1.00k
  int ginc[CCV_NNC_MAX_DIM_ALLOC];
40
1.00k
  int ainc[CCV_NNC_MAX_DIM_ALLOC];
41
1.00k
  int minc[CCV_NNC_MAX_DIM_ALLOC];
42
1.00k
  int vinc[CCV_NNC_MAX_DIM_ALLOC];
43
1.00k
  int binc[CCV_NNC_MAX_DIM_ALLOC];
44
1.00k
  int ninc[CCV_NNC_MAX_DIM_ALLOC];
45
1.00k
  int uinc[CCV_NNC_MAX_DIM_ALLOC];
46
1.00k
  ccv_nnc_tensor_view_get_inc(g, ginc);
47
1.00k
  ccv_nnc_tensor_view_get_inc(a, ainc);
48
1.00k
  ccv_nnc_tensor_view_get_inc(m, minc);
49
1.00k
  ccv_nnc_tensor_view_get_inc(v, vinc);
50
1.00k
  ccv_nnc_tensor_view_get_inc(b, binc);
51
1.00k
  ccv_nnc_tensor_view_get_inc(n, ninc);
52
1.00k
  ccv_nnc_tensor_view_get_inc(u, uinc);
53
1.00k
  const float rate = cmd.info.rmsprop.rate;
54
1.00k
  const float decay = cmd.info.rmsprop.decay;
55
1.00k
  const float alpha = cmd.info.rmsprop.alpha;
56
1.00k
  const float momentum = cmd.info.rmsprop.momentum;
57
1.00k
  const float epsilon = cmd.info.rmsprop.epsilon;
58
1.00k
  int i[CCV_NNC_MAX_DIM + 1];
59
1.00k
  int x;
60
1.00k
  float* gp = g->data.f32;
61
1.00k
  float* ap = a->data.f32;
62
1.00k
  float* mp = m->data.f32;
63
1.00k
  float* vp = v->data.f32;
64
1.00k
  float* bp = b->data.f32;
65
1.00k
  float* np = n->data.f32;
66
1.00k
  float* up = u->data.f32;
67
2.00k
  for (i[0] = 0; i[0] < adim[0]; 
i[0]++1.00k
)
68
1.00k
  {
69
2.00k
    for (i[1] = 0; i[1] < adim[1]; 
i[1]++1.00k
)
70
1.00k
    {
71
3.00k
      for (i[2] = 0; i[2] < adim[2]; 
i[2]++2.00k
)
72
2.00k
      {
73
6.03k
        for (x = 0; x < adim[3]; 
x++4.03k
)
74
4.03k
        {
75
4.03k
          float grad = gp[x];
76
4.03k
          grad += decay * ap[x];
77
4.03k
          const float vel = up[x] = alpha * vp[x] + (1 - alpha) * grad * grad;
78
4.03k
          const float mom = np[x] = momentum * mp[x] + grad / (sqrtf(vel) + epsilon);
79
4.03k
          bp[x] = ap[x] - rate * mom;
80
4.03k
        }
81
2.00k
        gp += ginc[3];
82
2.00k
        ap += ainc[3];
83
2.00k
        mp += minc[3];
84
2.00k
        vp += vinc[3];
85
2.00k
        bp += binc[3];
86
2.00k
        np += ninc[3];
87
2.00k
        up += uinc[3];
88
2.00k
      }
89
1.00k
      gp += (ginc[2] - adim[2]) * ginc[3];
90
1.00k
      ap += (ainc[2] - adim[2]) * ainc[3];
91
1.00k
      mp += (minc[2] - adim[2]) * minc[3];
92
1.00k
      vp += (vinc[2] - adim[2]) * vinc[3];
93
1.00k
      bp += (binc[2] - adim[2]) * binc[3];
94
1.00k
      np += (ninc[2] - adim[2]) * ninc[3];
95
1.00k
      up += (uinc[2] - adim[2]) * uinc[3];
96
1.00k
    }
97
1.00k
    gp += (ginc[1] - adim[1]) * ginc[2] * ginc[3];
98
1.00k
    ap += (ainc[1] - adim[1]) * ainc[2] * ainc[3];
99
1.00k
    mp += (minc[1] - adim[1]) * minc[2] * minc[3];
100
1.00k
    vp += (vinc[1] - adim[1]) * vinc[2] * vinc[3];
101
1.00k
    bp += (binc[1] - adim[1]) * binc[2] * binc[3];
102
1.00k
    np += (ninc[1] - adim[1]) * ninc[2] * ninc[3];
103
1.00k
    up += (uinc[1] - adim[1]) * uinc[2] * uinc[3];
104
1.00k
  }
105
1.00k
  return CCV_NNC_EXEC_SUCCESS;
106
1.00k
}
107
108
static int _ccv_nnc_rmsprop_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)
109
0
{
110
0
  return CCV_NNC_EXEC_INVALID;
111
0
}
112
113
REGISTER_COMMAND_BACKEND(CCV_NNC_RMSPROP_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
114
1
{
115
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
116
1
  registry->tensor_datatypes = CCV_32F;
117
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
118
1
  registry->algorithms = 1;
119
1
  registry->exec = _ccv_nnc_rmsprop_forw;
120
1
}
121
122
REGISTER_COMMAND_BACKEND(CCV_NNC_RMSPROP_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
123
1
{
124
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC | CCV_TENSOR_FORMAT_NCHW | CCV_TENSOR_FORMAT_CHWN;
125
1
  registry->tensor_datatypes = CCV_32F;
126
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
127
1
  registry->algorithms = 1;
128
1
  registry->exec = _ccv_nnc_rmsprop_back;
129
1
}