blob: ad664acf1c80a7e66fce35dd7ea2c9a8a03e2fd2 [file] [log] [blame]
Hui Su8e154702018-03-23 16:10:57 -07001/*
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 Bokov9b5fb2c2018-08-27 14:37:21 -070013#include <math.h>
Hui Su8e154702018-03-23 16:10:57 -070014
Alexander Bokov9b5fb2c2018-08-27 14:37:21 -070015#include "aom_dsp/aom_dsp_common.h"
Hui Su8e154702018-03-23 16:10:57 -070016#include "av1/encoder/ml.h"
17
David Turner486cc982018-11-09 15:48:58 +000018// 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.
21void av1_nn_predict_c(const float *input_nodes,
22 const NN_CONFIG *const nn_config, float *const output) {
Hui Su8e154702018-03-23 16:10:57 -070023 int num_input_nodes = nn_config->num_inputs;
24 int buf_index = 0;
25 float buf[2][NN_MAX_NODES_PER_LAYER];
Hui Su8e154702018-03-23 16:10:57 -070026
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) {
David Turner486cc982018-11-09 15:48:58 +000031 const float *layer_weights = nn_config->weights[layer];
32 const float *layer_bias = nn_config->bias[layer];
Hui Su8e154702018-03-23 16:10:57 -070033 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) {
David Turner486cc982018-11-09 15:48:58 +000037 float val = layer_bias[node];
Hui Su8e154702018-03-23 16:10:57 -070038 for (int i = 0; i < num_input_nodes; ++i)
David Turner486cc982018-11-09 15:48:58 +000039 val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
Hui Su8e154702018-03-23 16:10:57 -070040 // ReLU as activation function.
41 val = val > 0.0f ? val : 0.0f; // Could use AOMMAX().
42 output_nodes[node] = val;
Hui Su8e154702018-03-23 16:10:57 -070043 }
44 num_input_nodes = num_output_nodes;
45 input_nodes = output_nodes;
46 buf_index = 1 - buf_index;
47 }
48
49 // Final output layer.
David Turner486cc982018-11-09 15:48:58 +000050 const float *layer_weights = nn_config->weights[num_layers];
51 const float *layer_bias = nn_config->bias[num_layers];
Hui Su8e154702018-03-23 16:10:57 -070052 for (int node = 0; node < nn_config->num_outputs; ++node) {
David Turner486cc982018-11-09 15:48:58 +000053 float val = layer_bias[node];
Hui Su8e154702018-03-23 16:10:57 -070054 for (int i = 0; i < num_input_nodes; ++i)
David Turner486cc982018-11-09 15:48:58 +000055 val += layer_weights[node * num_input_nodes + i] * input_nodes[i];
56 output[node] = val;
Hui Su8e154702018-03-23 16:10:57 -070057 }
58}
Alexander Bokov9b5fb2c2018-08-27 14:37:21 -070059
60void 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 output[i] = (float)exp(input[i] - max_inp);
69 sum_out += output[i];
70 }
71 for (int i = 0; i < n; i++) output[i] /= sum_out;
72}