Coverage Report

Created: 2021-04-12 03:25

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/ccv_nnc_symbolic_graph_minimize.c
Line
Count
Source (jump to first uncovered line)
1
#include "ccv_nnc.h"
2
#include "ccv_nnc_easy.h"
3
#include "ccv_nnc_internal.h"
4
#include "ccv_internal.h"
5
#include "_ccv_nnc_symbolic_graph.h"
6
7
// MARK - Level-3.5 API
8
9
int ccv_nnc_minimizer_saved_aux_size(const ccv_nnc_cmd_t minimizer)
10
12.3k
{
11
12.3k
  int i, aux_size = -1;
12
12.3k
  uint64_t input_bitmask = 0x1;
13
12.3k
  uint64_t output_bitmask = 0x0;
14
36.8k
  for (i = 0; i < 62 && aux_size < 0; 
i++24.5k
)
15
24.5k
  {
16
24.5k
    input_bitmask |= ((uint64_t)1 << (i + 1));
17
24.5k
    output_bitmask |= ((uint64_t)1 << i);
18
24.5k
    if (ccv_nnc_cmd_bitmask(minimizer, i + 2, i + 1, &input_bitmask, 1, &output_bitmask, 1))
19
12.3k
      aux_size = i;
20
24.5k
  }
21
12.3k
  return aux_size;
22
12.3k
}
23
24
void ccv_nnc_symbolic_graph_minimize(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_cmd_t minimizer, const ccv_nnc_tensor_symbol_t* const losses, const int loss_size, const ccv_nnc_tensor_symbol_t* const parameters, const int parameter_size, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size, ccv_nnc_tensor_symbol_t* const gradients, ccv_nnc_tensor_symbol_t* const updated_parameters, ccv_nnc_tensor_symbol_map_t* const saved_aux, ccv_nnc_graph_exec_symbol_t* const graph_exec_symbols)
25
3.24k
{
26
3.24k
  assert(parameter_size >= 0);
27
3.24k
  assert(loss_size > 0);
28
3.24k
  assert((inputs && input_size > 0) || (inputs == 0 && input_size <= 0));
29
3.24k
  // First, compute gradient.
30
3.24k
  if (inputs)
31
2.22k
  {
32
2.22k
    ccv_nnc_tensor_symbol_t* const ingrads = gradients ? gradients : 
ccmalloc0
(sizeof(ccv_nnc_tensor_symbol_t) * (parameter_size + input_size))0
;
33
2.22k
    if (parameter_size > 0)
34
2.22k
      memcpy(ingrads, parameters, sizeof(ccv_nnc_tensor_symbol_t) * parameter_size);
35
2.22k
    memcpy(ingrads + parameter_size, inputs, sizeof(ccv_nnc_tensor_symbol_t) * input_size);
36
2.22k
    ccv_nnc_symbolic_graph_backward(graph, losses, loss_size, ingrads, parameter_size + input_size, sources, source_size, destinations, destination_size);
37
2.22k
    if (ingrads != gradients)
38
2.22k
      
ccfree0
(ingrads)0
;
39
2.22k
  } else 
if (1.01k
parameter_size > 01.01k
)
40
1.01k
    ccv_nnc_symbolic_graph_backward(graph, losses, loss_size, parameters, parameter_size, sources, source_size, destinations, destination_size);
41
3.24k
  int i, j;
42
3.24k
  // At most the minimizer accepts 62 additional parameters.
43
3.24k
  const int aux_size = ccv_nnc_minimizer_saved_aux_size(minimizer);
44
3.24k
  assert(aux_size >= 0);
45
3.24k
  ccv_nnc_tensor_symbol_t update_inputs[aux_size + 2];
46
3.24k
  ccv_nnc_tensor_symbol_t update_outputs[aux_size + 1];
47
6.93k
  for (i = 0; i < parameter_size; 
i++3.69k
)
48
3.69k
  {
49
3.69k
    const ccv_nnc_tensor_symbol_t gradient = ccv_nnc_tensor_symbol_for_backward(graph, parameters[i]);
50
3.69k
    if (gradients)
51
2.68k
      gradients[i] = gradient;
52
3.69k
    const ccv_nnc_graph_exec_symbol_t graph_exec = ccv_nnc_graph_exec_symbol_for_backward(graph, gradient);
53
3.69k
    update_inputs[0] = gradient;
54
3.69k
    update_inputs[1] = parameters[i];
55
3.69k
    const ccv_nnc_tensor_param_t info = ccv_nnc_tensor_symbol_params(graph, update_inputs[1]);
56
3.69k
    update_outputs[0] = updated_parameters[i] = ccv_nnc_tensor_symbol_new(graph, info, 0);
57
7.33k
    for (j = 0; j < aux_size; 
j++3.64k
)
58
3.64k
    {
59
3.64k
      update_inputs[2 + j] = saved_aux[i * aux_size + j].source = ccv_nnc_tensor_symbol_new(graph, info, 0);
60
3.64k
      update_outputs[1 + j] = saved_aux[i * aux_size + j].destination = ccv_nnc_tensor_symbol_new(graph, info, 0);
61
3.64k
    }
62
3.69k
    const ccv_nnc_graph_exec_symbol_t minimize = ccv_nnc_graph_exec_symbol_new(graph, minimizer, update_inputs, aux_size + 2, update_outputs, aux_size + 1, 0);
63
3.69k
    ccv_nnc_graph_exec_symbol_concat(graph, graph_exec, minimize);
64
3.69k
    graph_exec_symbols[i] = minimize;
65
3.69k
  }
66
3.24k
  if (gradients)
67
4.47k
    
for (i = 0; 2.23k
i < input_size;
i++2.24k
)
68
2.24k
      gradients[i + parameter_size] = ccv_nnc_tensor_symbol_for_backward(graph, inputs[i]);
69
3.24k
}