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>
13 #include <math.h>
14
15 #include "aom_dsp/aom_dsp_common.h"
16 #include "av1/encoder/ml.h"
17
18 // Calculate prediction based on the given input features and neural net config.
19 // Assume there are no more than NN_MAX_NODES_PER_LAYER nodes in each hidden
20 // layer.
av1_nn_predict_c(const float * input_nodes,const NN_CONFIG * const nn_config,float * const output)21 void av1_nn_predict_c(const float *input_nodes,
22 const NN_CONFIG *const nn_config, float *const output) {
23 int num_input_nodes = nn_config->num_inputs;
24 int buf_index = 0;
25 float buf[2][NN_MAX_NODES_PER_LAYER];
26
27 // Propagate hidden layers.
28 const int num_layers = nn_config->num_hidden_layers;
29 assert(num_layers <= NN_MAX_HIDDEN_LAYERS);
30 for (int layer = 0; layer < num_layers; ++layer) {
31 const float *layer_weights = nn_config->weights[layer];
32 const float *layer_bias = nn_config->bias[layer];
33 float *output_nodes = buf[buf_index];
34 const int num_output_nodes = nn_config->num_hidden_nodes[layer];
35 assert(num_output_nodes < NN_MAX_NODES_PER_LAYER);
36 for (int node = 0; node < num_output_nodes; ++node) {
37 float val = layer_bias[node];
38 for (int i = 0; i < num_input_nodes; ++i)
39 val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
40 // ReLU as activation function.
41 val = val > 0.0f ? val : 0.0f; // Could use AOMMAX().
42 output_nodes[node] = val;
43 }
44 num_input_nodes = num_output_nodes;
45 input_nodes = output_nodes;
46 buf_index = 1 - buf_index;
47 }
48
49 // Final output layer.
50 const float *layer_weights = nn_config->weights[num_layers];
51 const float *layer_bias = nn_config->bias[num_layers];
52 for (int node = 0; node < nn_config->num_outputs; ++node) {
53 float val = layer_bias[node];
54 for (int i = 0; i < num_input_nodes; ++i)
55 val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
56 output[node] = val;
57 }
58 }
59
av1_nn_softmax(const float * input,float * output,int n)60 void av1_nn_softmax(const float *input, float *output, int n) {
61 // Softmax function is invariant to adding the same constant
62 // to all input values, so we subtract the maximum input to avoid
63 // possible overflow.
64 float max_inp = input[0];
65 for (int i = 1; i < n; i++) max_inp = AOMMAX(max_inp, input[i]);
66 float sum_out = 0.0f;
67 for (int i = 0; i < n; i++) {
68 // Clamp to range [-10.0, 0.0] to prevent FE_UNDERFLOW errors.
69 const float normalized_input = AOMMAX(input[i] - max_inp, -10.0f);
70 output[i] = (float)exp(normalized_input);
71 sum_out += output[i];
72 }
73 for (int i = 0; i < n; i++) output[i] /= sum_out;
74 }
75