Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
Alexander Bokov | 9b5fb2c | 2018-08-27 14:37:21 -0700 | [diff] [blame] | 13 | #include <math.h> |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 14 | |
Alexander Bokov | 9b5fb2c | 2018-08-27 14:37:21 -0700 | [diff] [blame] | 15 | #include "aom_dsp/aom_dsp_common.h" |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 16 | #include "av1/encoder/ml.h" |
| 17 | |
Debargha Mukherjee | d44f5d1 | 2019-06-27 14:56:05 -0700 | [diff] [blame] | 18 | void av1_nn_output_prec_reduce(float *const output, int num_output) { |
| 19 | const int prec_bits = 11; |
| 20 | const int prec = 1 << prec_bits; |
| 21 | const float inv_prec = (float)(1.0 / prec); |
| 22 | for (int i = 0; i < num_output; i++) { |
| 23 | output[i] = ((int)(output[i] * prec + 0.5)) * inv_prec; |
| 24 | } |
| 25 | } |
| 26 | |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 27 | // Calculate prediction based on the given input features and neural net config. |
| 28 | // Assume there are no more than NN_MAX_NODES_PER_LAYER nodes in each hidden |
| 29 | // layer. |
| 30 | void av1_nn_predict_c(const float *input_nodes, |
Debargha Mukherjee | d44f5d1 | 2019-06-27 14:56:05 -0700 | [diff] [blame] | 31 | const NN_CONFIG *const nn_config, int reduce_prec, |
| 32 | float *const output) { |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 33 | int num_input_nodes = nn_config->num_inputs; |
| 34 | int buf_index = 0; |
| 35 | float buf[2][NN_MAX_NODES_PER_LAYER]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 36 | |
| 37 | // Propagate hidden layers. |
| 38 | const int num_layers = nn_config->num_hidden_layers; |
| 39 | assert(num_layers <= NN_MAX_HIDDEN_LAYERS); |
| 40 | for (int layer = 0; layer < num_layers; ++layer) { |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 41 | const float *layer_weights = nn_config->weights[layer]; |
| 42 | const float *layer_bias = nn_config->bias[layer]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 43 | float *output_nodes = buf[buf_index]; |
| 44 | const int num_output_nodes = nn_config->num_hidden_nodes[layer]; |
| 45 | assert(num_output_nodes < NN_MAX_NODES_PER_LAYER); |
| 46 | for (int node = 0; node < num_output_nodes; ++node) { |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 47 | float val = layer_bias[node]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 48 | for (int i = 0; i < num_input_nodes; ++i) |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 49 | val += layer_weights[node * num_input_nodes + i] * input_nodes[i]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 50 | // ReLU as activation function. |
| 51 | val = val > 0.0f ? val : 0.0f; // Could use AOMMAX(). |
| 52 | output_nodes[node] = val; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 53 | } |
| 54 | num_input_nodes = num_output_nodes; |
| 55 | input_nodes = output_nodes; |
| 56 | buf_index = 1 - buf_index; |
| 57 | } |
| 58 | |
| 59 | // Final output layer. |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 60 | const float *layer_weights = nn_config->weights[num_layers]; |
| 61 | const float *layer_bias = nn_config->bias[num_layers]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 62 | for (int node = 0; node < nn_config->num_outputs; ++node) { |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 63 | float val = layer_bias[node]; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 64 | for (int i = 0; i < num_input_nodes; ++i) |
David Turner | 486cc98 | 2018-11-09 15:48:58 +0000 | [diff] [blame] | 65 | val += layer_weights[node * num_input_nodes + i] * input_nodes[i]; |
| 66 | output[node] = val; |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 67 | } |
Debargha Mukherjee | d44f5d1 | 2019-06-27 14:56:05 -0700 | [diff] [blame] | 68 | if (reduce_prec) av1_nn_output_prec_reduce(output, nn_config->num_outputs); |
Hui Su | 8e15470 | 2018-03-23 16:10:57 -0700 | [diff] [blame] | 69 | } |
Alexander Bokov | 9b5fb2c | 2018-08-27 14:37:21 -0700 | [diff] [blame] | 70 | |
mlchen | 759cdac | 2019-06-13 15:52:02 -0700 | [diff] [blame] | 71 | #if CONFIG_NN_V2 |
| 72 | // Applies the ReLu activation to one fc layer |
| 73 | // output[i] = Max(input[i],0.0f) |
| 74 | static float *nn_relu(const float *input, FC_LAYER *layer) { |
| 75 | for (int i = 0; i < layer->num_outputs; ++i) { |
| 76 | layer->output[i] = AOMMAX(input[i], 0.0f); |
| 77 | } |
| 78 | |
| 79 | return layer->output; |
| 80 | } |
| 81 | |
| 82 | // Applies the Sigmoid activation to one fc layer |
| 83 | // output[i] = 1/(1+exp(input[i])) |
| 84 | static float *nn_sigmoid(const float *input, FC_LAYER *layer) { |
| 85 | for (int i = 0; i < layer->num_outputs; ++i) { |
| 86 | const float tmp = AOMMIN(AOMMAX(input[i], -10.0f), 10.0f); |
| 87 | layer->output[i] = 1.0f / (1.0f + expf(-tmp)); |
| 88 | } |
| 89 | |
| 90 | return layer->output; |
| 91 | } |
| 92 | |
| 93 | // Forward prediction in one fc layer, used in function av1_nn_predict_V2 |
| 94 | static float *nn_fc_forward(const float *input, FC_LAYER *layer) { |
| 95 | const float *weights = layer->weights; |
| 96 | const float *bias = layer->bias; |
| 97 | assert(layer->num_outputs < NN_MAX_NODES_PER_LAYER); |
| 98 | // fc |
| 99 | for (int node = 0; node < layer->num_outputs; ++node) { |
| 100 | float val = bias[node]; |
| 101 | for (int i = 0; i < layer->num_inputs; ++i) val += weights[i] * input[i]; |
| 102 | layer->output[node] = val; |
| 103 | weights += layer->num_inputs; |
| 104 | } |
| 105 | |
| 106 | // activation |
| 107 | switch (layer->activation) { |
| 108 | case NONE: return layer->output; |
| 109 | case RELU: return nn_relu(layer->output, layer); |
| 110 | case SIGMOID: return nn_sigmoid(layer->output, layer); |
| 111 | case SOFTSIGN: |
| 112 | assert(0 && "Softsign has not been supported in NN."); // TO DO |
| 113 | return NULL; |
| 114 | default: |
| 115 | assert(0 && "Unknown activation"); // Unknown activation |
| 116 | return NULL; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void av1_nn_predict_v2(const float *feature, NN_CONFIG_V2 *nn_config, |
Debargha Mukherjee | d44f5d1 | 2019-06-27 14:56:05 -0700 | [diff] [blame] | 121 | int reduce_prec, float *output) { |
mlchen | 759cdac | 2019-06-13 15:52:02 -0700 | [diff] [blame] | 122 | const float *input_nodes = feature; |
| 123 | |
| 124 | // Propagate the layers. |
| 125 | const int num_layers = nn_config->num_hidden_layers; |
| 126 | assert(num_layers <= NN_MAX_HIDDEN_LAYERS); |
| 127 | for (int i = 0; i < num_layers; ++i) { |
| 128 | input_nodes = nn_fc_forward(input_nodes, nn_config->layer + i); |
| 129 | assert(nn_config->layer[i + 1].num_inputs == |
| 130 | nn_config->layer[i].num_outputs); |
| 131 | } |
| 132 | |
| 133 | // Final layer |
| 134 | input_nodes = nn_fc_forward(input_nodes, nn_config->layer + num_layers); |
| 135 | assert(nn_config->layer[num_layers].num_outputs == nn_config->num_logits); |
| 136 | // Copy the final layer output |
| 137 | memcpy(output, input_nodes, sizeof(*input_nodes) * nn_config->num_logits); |
Debargha Mukherjee | d44f5d1 | 2019-06-27 14:56:05 -0700 | [diff] [blame] | 138 | if (reduce_prec) av1_nn_output_prec_reduce(output, nn_config->num_logits); |
mlchen | 759cdac | 2019-06-13 15:52:02 -0700 | [diff] [blame] | 139 | } |
| 140 | #endif // CONFIG_NN_V2 |
| 141 | |
Alexander Bokov | 9b5fb2c | 2018-08-27 14:37:21 -0700 | [diff] [blame] | 142 | void av1_nn_softmax(const float *input, float *output, int n) { |
| 143 | // Softmax function is invariant to adding the same constant |
| 144 | // to all input values, so we subtract the maximum input to avoid |
| 145 | // possible overflow. |
| 146 | float max_inp = input[0]; |
| 147 | for (int i = 1; i < n; i++) max_inp = AOMMAX(max_inp, input[i]); |
| 148 | float sum_out = 0.0f; |
| 149 | for (int i = 0; i < n; i++) { |
Hui Su | b20b2dc | 2019-01-07 14:22:49 -0800 | [diff] [blame] | 150 | // Clamp to range [-10.0, 0.0] to prevent FE_UNDERFLOW errors. |
| 151 | const float normalized_input = AOMMAX(input[i] - max_inp, -10.0f); |
| 152 | output[i] = (float)exp(normalized_input); |
Alexander Bokov | 9b5fb2c | 2018-08-27 14:37:21 -0700 | [diff] [blame] | 153 | sum_out += output[i]; |
| 154 | } |
| 155 | for (int i = 0; i < n; i++) output[i] /= sum_out; |
| 156 | } |