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       break;
60     case ActivationFunction::kSigmoidApproximated:
61       return ::rnnoise::SigmoidApproximated;
62       break;
63   }
64 }
65 
66 }  // namespace
67 
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)68 FullyConnectedLayer::FullyConnectedLayer(
69     const int input_size,
70     const int output_size,
71     const rtc::ArrayView<const int8_t> bias,
72     const rtc::ArrayView<const int8_t> weights,
73     ActivationFunction activation_function,
74     const AvailableCpuFeatures& cpu_features,
75     absl::string_view layer_name)
76     : input_size_(input_size),
77       output_size_(output_size),
78       bias_(GetScaledParams(bias)),
79       weights_(PreprocessWeights(weights, output_size)),
80       vector_math_(cpu_features),
81       activation_function_(GetActivationFunction(activation_function)) {
82   RTC_DCHECK_LE(output_size_, kFullyConnectedLayerMaxUnits)
83       << "Insufficient FC layer over-allocation (" << layer_name << ").";
84   RTC_DCHECK_EQ(output_size_, bias_.size())
85       << "Mismatching output size and bias terms array size (" << layer_name
86       << ").";
87   RTC_DCHECK_EQ(input_size_ * output_size_, weights_.size())
88       << "Mismatching input-output size and weight coefficients array size ("
89       << layer_name << ").";
90 }
91 
92 FullyConnectedLayer::~FullyConnectedLayer() = default;
93 
ComputeOutput(rtc::ArrayView<const float> input)94 void FullyConnectedLayer::ComputeOutput(rtc::ArrayView<const float> input) {
95   RTC_DCHECK_EQ(input.size(), input_size_);
96   rtc::ArrayView<const float> weights(weights_);
97   for (int o = 0; o < output_size_; ++o) {
98     output_[o] = activation_function_(
99         bias_[o] + vector_math_.DotProduct(
100                        input, weights.subview(o * input_size_, input_size_)));
101   }
102 }
103 
104 }  // namespace rnn_vad
105 }  // namespace webrtc
106