/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd/loss/ccv_nnc_categorical_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_categorical_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 | 0 | if ((input_bitmasks[0] & 3u) == 3u && output_bitmasks[0] == 1u) |
9 | 0 | return 1; |
10 | 0 | return 0; |
11 | 0 | } |
12 | | |
13 | | static int _ccv_nnc_categorical_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) |
14 | 32 | { |
15 | 32 | if ((input_bitmasks[0] & 7u) == 7u && (output_bitmasks[0] & 1u) == 1u13 ) |
16 | 13 | return 1; |
17 | 19 | return 0; |
18 | 32 | } |
19 | | |
20 | | static void _ccv_nnc_categorical_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) |
21 | 18 | { |
22 | 18 | assert(input_size == 2); |
23 | 18 | assert(output_size == 1); |
24 | 18 | outputs[0] = inputs[0]; |
25 | | // The output should have the same dimentionality of the label data. |
26 | 18 | memcpy(outputs[0].dim, inputs[1].dim, sizeof(outputs[0].dim)); |
27 | 18 | const int nd = ccv_nnc_tensor_nd(outputs[0].dim); |
28 | | // Set channel to 1 if it is not.. |
29 | 18 | if (nd > 1 && ccv_nnc_tensor_get_c(outputs[0]) > 113 ) |
30 | 9 | ccv_nnc_tensor_set_c(&outputs[0], nd, 1); |
31 | 18 | } |
32 | | |
33 | | static void _ccv_nnc_categorical_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) |
34 | 5 | { |
35 | 5 | assert(input_size >= 2); |
36 | 5 | assert(output_size >= 1); |
37 | 5 | outputs[0] = inputs[1]; |
38 | 5 | if (output_size > 1) |
39 | 5 | outputs[1] = inputs[2]; |
40 | 5 | } |
41 | | |
42 | | REGISTER_COMMAND(CCV_NNC_CATEGORICAL_CROSSENTROPY_FORWARD)(ccv_nnc_cmd_registry_t* const registry) |
43 | | FIND_BACKEND(ccv_nnc_categorical_crossentropy_cpu_ref.c, gpu/ccv_nnc_categorical_crossentropy_gpu_ref.cu) |
44 | 1 | { |
45 | 1 | registry->bitmask = _ccv_nnc_categorical_crossentropy_forw_bitmask; |
46 | 1 | registry->tensor_auto = _ccv_nnc_categorical_crossentropy_tensor_auto_forw; |
47 | 1 | } |
48 | | |
49 | | REGISTER_COMMAND(CCV_NNC_CATEGORICAL_CROSSENTROPY_BACKWARD)(ccv_nnc_cmd_registry_t* const registry) |
50 | | FIND_BACKEND(ccv_nnc_categorical_crossentropy_cpu_ref.c, gpu/ccv_nnc_categorical_crossentropy_gpu_ref.cu) |
51 | 1 | { |
52 | 1 | registry->flags = CCV_NNC_CMD_ATTR_NULL_IS_ONES; |
53 | 1 | registry->bitmask = _ccv_nnc_categorical_crossentropy_back_bitmask; |
54 | 1 | registry->tensor_auto = _ccv_nnc_categorical_crossentropy_tensor_auto_back; |
55 | 1 | } |
56 | | |
57 | | //@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_CATEGORICAL_CROSSENTROPY_FORWARD) |
58 | | #define CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_0() ccv_nnc_cmd(CCV_NNC_CATEGORICAL_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=0,.trim1=1}}), 0) |
59 | | #define CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_CATEGORICAL_CROSSENTROPY_FORWARD") |
60 | | #define CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_2(_trim0, _trim1) ccv_nnc_cmd(CCV_NNC_CATEGORICAL_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=_trim0,.trim1=_trim1}}), 0) |
61 | | #define CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_SEL(_0, _1, _2, _FX, ...) _FX |
62 | | #define CMD_CATEGORICAL_CROSSENTROPY_FORWARD(...) CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_SEL(CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_F, ##__VA_ARGS__, CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_2, CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_F, CMD_CATEGORICAL_CROSSENTROPY_FORWARD_X_0)(__VA_ARGS__) |
63 | | //@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_CATEGORICAL_CROSSENTROPY_BACKWARD) |
64 | | #define CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_0() ccv_nnc_cmd(CCV_NNC_CATEGORICAL_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=0,.trim1=1}}), 0) |
65 | | #define CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_CATEGORICAL_CROSSENTROPY_BACKWARD") |
66 | | #define CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_2(_trim0, _trim1) ccv_nnc_cmd(CCV_NNC_CATEGORICAL_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=_trim0,.trim1=_trim1}}), 0) |
67 | | #define CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_SEL(_0, _1, _2, _FX, ...) _FX |
68 | | #define CMD_CATEGORICAL_CROSSENTROPY_BACKWARD(...) CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_SEL(CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_F, ##__VA_ARGS__, CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_2, CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_F, CMD_CATEGORICAL_CROSSENTROPY_BACKWARD_X_0)(__VA_ARGS__) |