1 /*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <algorithm>
12 #include <numeric>
13
14 #include "modules/audio_processing/agc2/rnn_vad/rnn_fc.h"
15 #include "rtc_base/checks.h"
16 #include "rtc_base/numerics/safe_conversions.h"
17 #include "third_party/rnnoise/src/rnn_activations.h"
18 #include "third_party/rnnoise/src/rnn_vad_weights.h"
19
20 namespace webrtc {
21 namespace rnn_vad {
22 namespace {
23
GetScaledParams(rtc::ArrayView<const int8_t> params)24 std::vector<float> GetScaledParams(rtc::ArrayView<const int8_t> params) {
25 std::vector<float> scaled_params(params.size());
26 std::transform(params.begin(), params.end(), scaled_params.begin(),
27 [](int8_t x) -> float {
28 return ::rnnoise::kWeightsScale * static_cast<float>(x);
29 });
30 return scaled_params;
31 }
32
33 // TODO(bugs.chromium.org/10480): Hard-code optimized layout and remove this
34 // function to improve setup time.
35 // Casts and scales `weights` and re-arranges the layout.
PreprocessWeights(rtc::ArrayView<const int8_t> weights,int output_size)36 std::vector<float> PreprocessWeights(rtc::ArrayView<const int8_t> weights,
37 int output_size) {
38 if (output_size == 1) {
39 return GetScaledParams(weights);
40 }
41 // Transpose, scale and cast.
42 const int input_size = rtc::CheckedDivExact(
43 rtc::dchecked_cast<int>(weights.size()), output_size);
44 std::vector<float> w(weights.size());
45 for (int o = 0; o < output_size; ++o) {
46 for (int i = 0; i < input_size; ++i) {
47 w[o * input_size + i] = rnnoise::kWeightsScale *
48 static_cast<float>(weights[i * output_size + o]);
49 }
50 }
51 return w;
52 }
53
GetActivationFunction(ActivationFunction activation_function)54 rtc::FunctionView<float(float)> GetActivationFunction(
55 ActivationFunction activation_function) {
56 switch (activation_function) {
57 case ActivationFunction::kTansigApproximated:
58 return ::rnnoise::TansigApproximated;
59 case ActivationFunction::kSigmoidApproximated:
60 return ::rnnoise::SigmoidApproximated;
61 }
62 }
63
64 } // namespace
65
FullyConnectedLayer(const int input_size,const int output_size,const rtc::ArrayView<const int8_t> bias,const rtc::ArrayView<const int8_t> weights,ActivationFunction activation_function,const AvailableCpuFeatures & cpu_features,absl::string_view layer_name)66 FullyConnectedLayer::FullyConnectedLayer(
67 const int input_size,
68 const int output_size,
69 const rtc::ArrayView<const int8_t> bias,
70 const rtc::ArrayView<const int8_t> weights,
71 ActivationFunction activation_function,
72 const AvailableCpuFeatures& cpu_features,
73 absl::string_view layer_name)
74 : input_size_(input_size),
75 output_size_(output_size),
76 bias_(GetScaledParams(bias)),
77 weights_(PreprocessWeights(weights, output_size)),
78 vector_math_(cpu_features),
79 activation_function_(GetActivationFunction(activation_function)) {
80 RTC_DCHECK_LE(output_size_, kFullyConnectedLayerMaxUnits)
81 << "Insufficient FC layer over-allocation (" << layer_name << ").";
82 RTC_DCHECK_EQ(output_size_, bias_.size())
83 << "Mismatching output size and bias terms array size (" << layer_name
84 << ").";
85 RTC_DCHECK_EQ(input_size_ * output_size_, weights_.size())
86 << "Mismatching input-output size and weight coefficients array size ("
87 << layer_name << ").";
88 }
89
90 FullyConnectedLayer::~FullyConnectedLayer() = default;
91
ComputeOutput(rtc::ArrayView<const float> input)92 void FullyConnectedLayer::ComputeOutput(rtc::ArrayView<const float> input) {
93 RTC_DCHECK_EQ(input.size(), input_size_);
94 rtc::ArrayView<const float> weights(weights_);
95 for (int o = 0; o < output_size_; ++o) {
96 output_[o] = activation_function_(
97 bias_[o] + vector_math_.DotProduct(
98 input, weights.subview(o * input_size_, input_size_)));
99 }
100 }
101
102 } // namespace rnn_vad
103 } // namespace webrtc
104