Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/softmax_loss/ccv_nnc_softmax_crossentropy.c
Line
Count
Source (jump to first uncovered line)
1
#include <ccv.h>
2
#include <nnc/ccv_nnc.h>
3
#include <nnc/ccv_nnc_easy.h>
4
#include <nnc/ccv_nnc_internal.h>
5
6
static int _ccv_nnc_softmax_crossentropy_forw_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
7
6
{
8
6
  // input: activation, label
9
6
  // output: [loss], softmax
10
6
  if ((input_bitmasks[0] & 3u) == 3u && 
output_bitmasks[0] == 3u2
)
11
1
    return 1;
12
5
  if ((input_bitmasks[0] & 3u) == 3u && 
output_bitmasks[0] == 2u1
)
13
1
    return 1;
14
4
  return 0;
15
4
}
16
17
static int _ccv_nnc_softmax_crossentropy_allow_inplace_forw(const int input_idx, const int input_size, const int output_idx, const int output_size)
18
46
{
19
46
  return (input_idx == 0 && 
output_idx == 132
);
20
46
}
21
22
static int _ccv_nnc_softmax_crossentropy_back_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
23
81
{
24
81
  // input: [gradient of loss], [gradient of softmax], [activation], label, [loss], softmax
25
81
  // output: w.r.t activation, [label]
26
81
  if ((input_bitmasks[0] & 41u) == 41u && 
(output_bitmasks[0] & 1u) == 1u45
)
27
45
    return 1;
28
36
  return 0;
29
36
}
30
31
static int _ccv_nnc_softmax_crossentropy_allow_inplace_back(const int input_idx, const int input_size, const int output_idx, const int output_size)
32
54
{
33
54
  if (input_idx == 1 && 
output_idx == 00
)
34
0
    return 1;
35
54
  if (input_idx == 2 && 
output_idx == 00
)
36
0
    return 1;
37
54
  else if (input_idx == 5 && 
output_idx == 014
)
38
7
    return 1;
39
47
  return 0;
40
47
}
41
42
static void _ccv_nnc_softmax_crossentropy_tensor_auto_forw(const ccv_nnc_cmd_param_t cmd, const ccv_nnc_tensor_param_t* const inputs, const int input_size, const ccv_nnc_hint_t hint, ccv_nnc_tensor_param_t* const outputs, const int output_size)
43
32
{
44
32
  assert(input_size == 2);
45
32
  assert(output_size >= 1);
46
32
  outputs[0] = inputs[0];
47
32
  if (output_size > 1)
48
32
  {
49
32
    outputs[1] = inputs[0];
50
32
    // The output should have the same dimentionality of the label data.
51
32
    memcpy(outputs[1].dim, inputs[1].dim, sizeof(outputs[1].dim));
52
32
    const int nd = ccv_nnc_tensor_nd(outputs[1].dim);
53
32
    // Set channel to 1 if it is not..
54
32
    if (nd > 1 && 
ccv_nnc_tensor_get_c(outputs[1]) > 112
)
55
12
      ccv_nnc_tensor_set_c(&outputs[1], nd, 1);
56
32
  }
57
32
}
58
59
static void _ccv_nnc_softmax_crossentropy_tensor_auto_back(const ccv_nnc_cmd_param_t cmd, const ccv_nnc_tensor_param_t* const inputs, const int input_size, const ccv_nnc_hint_t hint, ccv_nnc_tensor_param_t* const outputs, const int output_size)
60
21
{
61
21
  assert(input_size >= 6);
62
21
  assert(output_size >= 1);
63
21
  outputs[0] = inputs[5];
64
21
  if (output_size > 1)
65
21
    outputs[1] = inputs[3];
66
21
}
67
68
REGISTER_COMMAND(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
69
  FIND_BACKEND(ccv_nnc_softmax_crossentropy_cpu_ref.c, gpu/ccv_nnc_softmax_crossentropy_gpu_cudnn.cu)
70
1
{
71
1
  registry->bitmask = _ccv_nnc_softmax_crossentropy_forw_bitmask;
72
1
  registry->tensor_auto = _ccv_nnc_softmax_crossentropy_tensor_auto_forw;
73
1
  registry->allow_inplace = _ccv_nnc_softmax_crossentropy_allow_inplace_forw;
74
1
}
75
76
REGISTER_COMMAND(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
77
  FIND_BACKEND(ccv_nnc_softmax_crossentropy_cpu_ref.c, gpu/ccv_nnc_softmax_crossentropy_gpu_cudnn.cu)
78
1
{
79
1
  registry->flags = CCV_NNC_CMD_ATTR_NULL_IS_ONES;
80
1
  registry->bitmask = _ccv_nnc_softmax_crossentropy_back_bitmask;
81
1
  registry->tensor_auto = _ccv_nnc_softmax_crossentropy_tensor_auto_back;
82
1
  registry->allow_inplace = _ccv_nnc_softmax_crossentropy_allow_inplace_back;
83
1
}
84
85
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD)
86
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD() ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD, 0, ccv_nnc_cmd_auto, 0)
87
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD)
88
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD() ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD, 0, ccv_nnc_cmd_auto, 0)