forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyra_wavegru_test.cc
129 lines (111 loc) · 4.15 KB
/
lyra_wavegru_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lyra_wavegru.h"
#include <memory>
#include <string>
#include <tuple>
#if !defined(USE_FIXED16) && !defined(USE_BFLOAT16)
#include "exported_layers_test.h"
#endif // !defined(USE_FIXED16) && !defined(USE_BFLOAT16)
// Placeholder for get runfiles header.
#include "absl/strings/str_format.h"
#include "gtest/gtest.h"
#include "include/ghc/filesystem.hpp"
#include "lyra_config.h"
#include "sparse_matmul/sparse_matmul.h" // IWYU pragma: keep
namespace chromemedia {
namespace codec {
namespace {
static const int kNumThreads[] = {1, 2, 4};
static const char kPrefixTemplate[] = "lyra_%dkhz";
#ifdef USE_FIXED16
using ComputeType = csrblocksparse::fixed16_type;
#elif USE_BFLOAT16
using ComputeType = csrblocksparse::bfloat16;
#else
using ComputeType = float;
#endif // USE_FIXED16
class LyraWavegruTest
: public testing::TestWithParam<testing::tuple<int, int>> {
protected:
LyraWavegruTest()
: sample_rate_hz_(GetInternalSampleRate(std::get<1>(GetParam()))),
lyra_wavegru_(LyraWavegru<ComputeType>::Create(
std::get<0>(GetParam()),
ghc::filesystem::current_path() / "wavegru",
absl::StrFormat(kPrefixTemplate, sample_rate_hz_ / 1000))) {}
const int sample_rate_hz_;
std::unique_ptr<LyraWavegru<ComputeType>> lyra_wavegru_;
};
TEST_P(LyraWavegruTest, ModelExistsProdFeatures) {
EXPECT_NE(lyra_wavegru_, nullptr);
}
INSTANTIATE_TEST_SUITE_P(
ThreadsAndSampleRates, LyraWavegruTest,
testing::Combine(testing::ValuesIn(kNumThreads),
testing::ValuesIn(kSupportedSampleRates)));
// Test that exported layers with fixed-point and float weights produce
// matching results.
// Run only in the first of the three related test targets: {lyra_wavegru_test,
// lyra_wavegru_test_fixed16, lyra_wavegru_test_bfloat16}.
#if !defined(USE_FIXED16) && !defined(USE_BFLOAT16)
using csrblocksparse::fixed16_type;
static constexpr int kNumGruHiddens = 1024;
static constexpr int kNumSplitBands = 4;
struct ArLayerTypes {
using FloatLayerType = LyraWavegru<float>::ArLayerType;
using FixedLayerType = LyraWavegru<fixed16_type>::ArLayerType;
static LayerParams Params(const std::string& model_path) {
return LayerParams{
.num_input_channels = kNumSplitBands,
.num_filters = 3 * kNumGruHiddens,
.length = 1,
.kernel_size = 1,
.dilation = 1,
.stride = 1,
.relu = false,
.skip_connection = false,
.type = LayerType::kConv1D,
.num_threads = 1,
.per_column_barrier = false,
.from = LayerParams::FromDisk{.path = model_path, .zipped = true},
.prefix = "lyra_16khz_ar_to_gates_"};
}
};
struct GruLayerTypes {
using FloatLayerType = LyraWavegru<float>::GruLayerType;
using FixedLayerType = LyraWavegru<fixed16_type>::GruLayerType;
static LayerParams Params(const std::string& model_path) {
return LayerParams{
.num_input_channels = kNumGruHiddens,
.num_filters = 3 * kNumGruHiddens,
.length = 1,
.kernel_size = 1,
.dilation = 1,
.stride = 1,
.relu = false,
.skip_connection = false,
.type = LayerType::kConv1D,
.num_threads = 1,
.per_column_barrier = false,
.from = LayerParams::FromDisk{.path = model_path, .zipped = true},
.prefix = "lyra_16khz_gru_layer_"};
}
};
using LayerTypesList = testing::Types<ArLayerTypes, GruLayerTypes>;
INSTANTIATE_TYPED_TEST_SUITE_P(Wavegru, ExportedLayersTest, LayerTypesList);
#endif // !defined(USE_FIXED16) && !defined(USE_BFLOAT16)
} // namespace
} // namespace codec
} // namespace chromemedia