Coverage Report

Created: 2019-07-03 22:50

/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
#pragma mark - Level-3.5 API
8
9
int ccv_nnc_minimizer_saved_aux_size(const ccv_nnc_cmd_t minimizer)
10
2.03k
{
11
2.03k
  int i, aux_size = -1;
12
2.03k
  uint64_t input_bitmask = 0x1;
13
2.03k
  uint64_t output_bitmask = 0x0;
14
6.10k
  for (i = 0; i < 62 && aux_size < 0; 
i++4.06k
)
15
4.06k
  {
16
4.06k
    input_bitmask |= ((uint64_t)1 << (i + 1));
17
4.06k
    output_bitmask |= ((uint64_t)1 << i);
18
4.06k
    if (ccv_nnc_cmd_bitmask(minimizer, i + 2, i + 1, &input_bitmask, 1, &output_bitmask, 1))
19
2.03k
      aux_size = i;
20
4.06k
  }
21
2.03k
  return aux_size;
22
2.03k
}
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
1.01k
{
26
1.01k
  assert(parameter_size > 0);
27
1.01k
  assert(loss_size > 0);
28
1.01k
  assert((inputs && input_size > 0) || (inputs == 0 && input_size <= 0));
29
1.01k
  // First, compute gradient.
30
1.01k
  if (inputs)
31
1
  {
32
1
    ccv_nnc_tensor_symbol_t* const ingrads = gradients ? gradients : 
ccmalloc0
(sizeof(ccv_nnc_tensor_symbol_t) * (parameter_size + input_size))0
;
33
1
    memcpy(ingrads, parameters, sizeof(ccv_nnc_tensor_symbol_t) * parameter_size);
34
1
    memcpy(ingrads + parameter_size, inputs, sizeof(ccv_nnc_tensor_symbol_t) * input_size);
35
1
    ccv_nnc_symbolic_graph_backward(graph, losses, loss_size, ingrads, parameter_size + input_size, sources, source_size, destinations, destination_size);
36
1
    if (ingrads != gradients)
37
1
      
ccfree0
(ingrads)0
;
38
1
  } else
39
1.01k
    ccv_nnc_symbolic_graph_backward(graph, losses, loss_size, parameters, parameter_size, sources, source_size, destinations, destination_size);
40
1.01k
  int i, j;
41
1.01k
  // At most the minimizer accepts 62 additional parameters.
42
1.01k
  const int aux_size = ccv_nnc_minimizer_saved_aux_size(minimizer);
43
1.01k
  assert(aux_size >= 0);
44
1.01k
  ccv_nnc_tensor_symbol_t update_inputs[aux_size + 2];
45
1.01k
  ccv_nnc_tensor_symbol_t update_outputs[aux_size + 1];
46
2.12k
  for (i = 0; i < parameter_size; 
i++1.10k
)
47
1.10k
  {
48
1.10k
    const ccv_nnc_tensor_symbol_t gradient = ccv_nnc_tensor_symbol_for_backward(graph, parameters[i]);
49
1.10k
    if (gradients)
50
108
      gradients[i] = gradient;
51
1.10k
    const ccv_nnc_graph_exec_symbol_t graph_exec = ccv_nnc_graph_exec_symbol_for_backward(graph, gradient);
52
1.10k
    update_inputs[0] = gradient;
53
1.10k
    update_inputs[1] = parameters[i];
54
1.10k
    const ccv_nnc_tensor_param_t info = ccv_nnc_tensor_symbol_params(graph, update_inputs[1]);
55
1.10k
    update_outputs[0] = updated_parameters[i] = ccv_nnc_tensor_symbol_new(graph, info, 0);
56
2.21k
    for (j = 0; j < aux_size; 
j++1.10k
)
57
1.10k
    {
58
1.10k
      update_inputs[2 + j] = saved_aux[i * aux_size + j].source = ccv_nnc_tensor_symbol_new(graph, info, 0);
59
1.10k
      update_outputs[1 + j] = saved_aux[i * aux_size + j].destination = ccv_nnc_tensor_symbol_new(graph, info, 0);
60
1.10k
    }
61
1.10k
    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);
62
1.10k
    ccv_nnc_graph_exec_symbol_concat(graph, graph_exec, minimize);
63
1.10k
    graph_exec_symbols[i] = minimize;
64
1.10k
  }
65
1.01k
  if (gradients)
66
17
    
for (i = 0; 13
i < input_size;
i++4
)
67
4
      gradients[i + parameter_size] = ccv_nnc_tensor_symbol_for_backward(graph, inputs[i]);
68
1.01k
}