Coverage Report

Created: 2024-08-19 11:27

/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd/loss/ccv_nnc_binary_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_binary_crossentropy_forw_bitmask(const ccv_nnc_cmd_param_t cmd, 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
0
{
8
  // input: activation, label
9
  // output: loss
10
0
  if ((input_bitmasks[0] & 3u) == 3u && output_bitmasks[0] == 1u)
11
0
    return 1;
12
0
  return 0;
13
0
}
14
15
static int _ccv_nnc_binary_crossentropy_back_bitmask(const ccv_nnc_cmd_param_t cmd, 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)
16
14
{
17
  // input: [gradient of loss], activation, label, [loss]
18
  // output: w.r.t activation, [label]
19
14
  if ((input_bitmasks[0] & 6u) == 6u && 
(output_bitmasks[0] & 1u) == 1u6
)
20
6
    return 1;
21
8
  return 0;
22
14
}
23
24
static void _ccv_nnc_binary_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)
25
6
{
26
6
  assert(input_size == 2);
27
6
  assert(output_size >= 1);
28
6
  outputs[0] = inputs[0];
29
  // The output should have the same dimentionality of the label data.
30
6
  memcpy(outputs[0].dim, inputs[1].dim, sizeof(outputs[0].dim));
31
6
  const int nd = ccv_nnc_tensor_nd(outputs[0].dim);
32
  // Set channel to 1 if it is not..
33
6
  if (nd > 1 && ccv_nnc_tensor_get_c(outputs[0]) > 1)
34
6
    ccv_nnc_tensor_set_c(&outputs[0], nd, 1);
35
6
}
36
37
static void _ccv_nnc_binary_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)
38
2
{
39
2
  assert(input_size >= 3);
40
2
  assert(output_size >= 1);
41
2
  outputs[0] = inputs[1];
42
2
  if (output_size > 1)
43
2
    outputs[1] = inputs[2];
44
2
}
45
46
REGISTER_COMMAND(CCV_NNC_BINARY_CROSSENTROPY_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
47
  FIND_BACKEND(ccv_nnc_binary_crossentropy_cpu_ref.c, gpu/ccv_nnc_binary_crossentropy_gpu_ref.cu)
48
1
{
49
1
  registry->bitmask = _ccv_nnc_binary_crossentropy_forw_bitmask;
50
1
  registry->tensor_auto = _ccv_nnc_binary_crossentropy_tensor_auto_forw;
51
1
}
52
53
REGISTER_COMMAND(CCV_NNC_BINARY_CROSSENTROPY_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
54
  FIND_BACKEND(ccv_nnc_binary_crossentropy_cpu_ref.c, gpu/ccv_nnc_binary_crossentropy_gpu_ref.cu)
55
1
{
56
1
  registry->flags = CCV_NNC_CMD_ATTR_NULL_IS_ONES;
57
1
  registry->bitmask = _ccv_nnc_binary_crossentropy_back_bitmask;
58
1
  registry->tensor_auto = _ccv_nnc_binary_crossentropy_tensor_auto_back;
59
1
}
60
61
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_BINARY_CROSSENTROPY_FORWARD)
62
#define CMD_BINARY_CROSSENTROPY_FORWARD_X_0() ccv_nnc_cmd(CCV_NNC_BINARY_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=1}}), 0)
63
#define CMD_BINARY_CROSSENTROPY_FORWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_BINARY_CROSSENTROPY_FORWARD")
64
#define CMD_BINARY_CROSSENTROPY_FORWARD_X_1(_pos_weight) ccv_nnc_cmd(CCV_NNC_BINARY_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=_pos_weight}}), 0)
65
#define CMD_BINARY_CROSSENTROPY_FORWARD_X_SEL(_0, _1, _FX, ...) _FX
66
#define CMD_BINARY_CROSSENTROPY_FORWARD(...) CMD_BINARY_CROSSENTROPY_FORWARD_X_SEL(CMD_BINARY_CROSSENTROPY_FORWARD_X_F, ##__VA_ARGS__, CMD_BINARY_CROSSENTROPY_FORWARD_X_1, CMD_BINARY_CROSSENTROPY_FORWARD_X_0)(__VA_ARGS__)
67
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_BINARY_CROSSENTROPY_BACKWARD)
68
#define CMD_BINARY_CROSSENTROPY_BACKWARD_X_0() ccv_nnc_cmd(CCV_NNC_BINARY_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=1}}), 0)
69
#define CMD_BINARY_CROSSENTROPY_BACKWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_BINARY_CROSSENTROPY_BACKWARD")
70
#define CMD_BINARY_CROSSENTROPY_BACKWARD_X_1(_pos_weight) ccv_nnc_cmd(CCV_NNC_BINARY_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=_pos_weight}}), 0)
71
#define CMD_BINARY_CROSSENTROPY_BACKWARD_X_SEL(_0, _1, _FX, ...) _FX
72
#define CMD_BINARY_CROSSENTROPY_BACKWARD(...) CMD_BINARY_CROSSENTROPY_BACKWARD_X_SEL(CMD_BINARY_CROSSENTROPY_BACKWARD_X_F, ##__VA_ARGS__, CMD_BINARY_CROSSENTROPY_BACKWARD_X_1, CMD_BINARY_CROSSENTROPY_BACKWARD_X_0)(__VA_ARGS__)