forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyra_types.h
284 lines (249 loc) · 12.8 KB
/
lyra_types.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* 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.
*/
#ifndef LYRA_CODEC_LYRA_TYPES_H_
#define LYRA_CODEC_LYRA_TYPES_H_
#include <type_traits>
#include "layer_wrapper.h"
#include "sparse_matmul/sparse_matmul.h"
namespace chromemedia {
namespace codec {
// Type inference for the LyraWavegru class, including bit allocations for
// fixed-point types.
template <typename WeightTypeKind,
int kArToGatesWeightExponentBits = 13, // ar_to_gates log
int kArToGatesRhsExponentBits = 0, // conv_ar_input i
int kGruWeightExponentBits = 4, // gru_layer w
int kGruStateExponentBits = 1, // always in [-1, 1]
int kGruRhsExponentBits = 13, // gru_layer i
class Enable = void>
struct WavegruTypes;
template <typename WeightTypeKind, int kArToGatesWeightExponentBits,
int kArToGatesRhsExponentBits, int kGruWeightExponentBits,
int kGruStateExponentBits, int kGruRhsExponentBits>
struct WavegruTypes<
WeightTypeKind, kArToGatesWeightExponentBits, kArToGatesRhsExponentBits,
kGruWeightExponentBits, kGruStateExponentBits, kGruRhsExponentBits,
typename std::enable_if<
std::is_same<WeightTypeKind, csrblocksparse::bfloat16>::value ||
std::is_same<WeightTypeKind, float>::value>::type> {
using DiskWeightType = float;
using ArWeightType = float;
using ArRhsType = float;
using ArOutputType = float;
using GruWeightType = float;
using GruStateType = float;
using GruRhsType = float;
using ScratchType = float;
};
template <typename WeightTypeKind, int kArToGatesWeightExponentBits,
int kArToGatesRhsExponentBits, int kGruWeightExponentBits,
int kGruStateExponentBits, int kGruRhsExponentBits>
struct WavegruTypes<
WeightTypeKind, kArToGatesWeightExponentBits, kArToGatesRhsExponentBits,
kGruWeightExponentBits, kGruStateExponentBits, kGruRhsExponentBits,
typename std::enable_if<std::is_same<
WeightTypeKind, csrblocksparse::fixed16_type>::value>::type> {
using DiskWeightType = csrblocksparse::fixed16_type;
using ArWeightType = csrblocksparse::fixed16<kArToGatesWeightExponentBits>;
using ArRhsType = csrblocksparse::fixed16<kArToGatesRhsExponentBits>;
using ArOutputType =
typename csrblocksparse::TypeOfProduct<ArWeightType, ArRhsType>::type;
using GruWeightType = csrblocksparse::fixed16<kGruWeightExponentBits>;
using GruStateType = csrblocksparse::fixed16<kGruStateExponentBits>;
using GruRhsType = csrblocksparse::fixed32<kGruRhsExponentBits>;
#if defined __ARM_NEON || defined __aarch64__
using ScratchType = int;
#else
using ScratchType = float;
#endif // defined __ARM_NEON || defined __aarch64__
};
template <typename WeightTypeKind,
int kConv1DWeightExponentBits = 2, // conv1d w
int kConv1DRhsExponentBits = 1, // conv1d i
int kCondStack0WeightExponentBits = 1, // conditioning_stack_0 w
int kCondStack0RhsExponentBits = 3, // conditioning_stack_0 i
int kCondStack1WeightExponentBits = 1, // conditioning_stack_1 w
int kCondStack1RhsExponentBits = 3, // conditioning_stack_1 i
int kCondStack2WeightExponentBits = 1, // conditioning_stack_2 w
int kCondStack2RhsExponentBits = 3, // conditioning_stack_2 i
int kTranspose0WeightExponentBits = 1, // transpose_0 w
int kTranspose0RhsExponentBits = 4, // transpose_0 i
int kTranspose1WeightExponentBits = 1, // transpose_1 w
int kTranspose1RhsExponentBits = 3, // transpose_1 i
int kTranspose2WeightExponentBits = 2, // transpose_2 w
int kTranspose2RhsExponentBits = 3, // transpose_2 i
int kConvCondWeightExponentBits = 1, // conv_cond w
int kConvCondRhsExponentBits = 2, // conv_cond i
int kConvToGatesWeightExponentBits = 4, // conv_to_gates w
int kConvToGatesRhsExponentBits = 5, // conv_to_gates i
class Enable = void>
struct ConditioningTypes;
template <typename WeightTypeKind, int kConv1DWeightExponentBits,
int kConv1DRhsExponentBits, int kCondStack0WeightExponentBits,
int kCondStack0RhsExponentBits, int kCondStack1WeightExponentBits,
int kCondStack1RhsExponentBits, int kCondStack2WeightExponentBits,
int kCondStack2RhsExponentBits, int kTranspose0WeightExponentBits,
int kTranspose0RhsExponentBits, int kTranspose1WeightExponentBits,
int kTranspose1RhsExponentBits, int kTranspose2WeightExponentBits,
int kTranspose2RhsExponentBits, int kConvCondWeightExponentBits,
int kConvCondRhsExponentBits, int kConvToGatesWeightExponentBits,
int kConvToGatesRhsExponentBits>
struct ConditioningTypes<
WeightTypeKind, kConv1DWeightExponentBits, kConv1DRhsExponentBits,
kCondStack0WeightExponentBits, kCondStack0RhsExponentBits,
kCondStack1WeightExponentBits, kCondStack1RhsExponentBits,
kCondStack2WeightExponentBits, kCondStack2RhsExponentBits,
kTranspose0WeightExponentBits, kTranspose0RhsExponentBits,
kTranspose1WeightExponentBits, kTranspose1RhsExponentBits,
kTranspose2WeightExponentBits, kTranspose2RhsExponentBits,
kConvCondWeightExponentBits, kConvCondRhsExponentBits,
kConvToGatesWeightExponentBits, kConvToGatesRhsExponentBits,
typename std::enable_if<
std::is_same<WeightTypeKind, csrblocksparse::bfloat16>::value ||
std::is_same<WeightTypeKind, float>::value>::type> {
using DiskWeightType = float;
using Conv1DWeightType = WeightTypeKind;
using Conv1DRhsType = float;
using CondStack0WeightType = WeightTypeKind;
using CondStack0RhsType = float;
using CondStack1WeightType = WeightTypeKind;
using CondStack1RhsType = float;
using CondStack2WeightType = WeightTypeKind;
using CondStack2RhsType = float;
using Transpose0WeightType = WeightTypeKind;
using Transpose0RhsType = float;
using Transpose1WeightType = WeightTypeKind;
using Transpose1RhsType = float;
using Transpose2WeightType = WeightTypeKind;
using Transpose2RhsType = float;
using ConvCondWeightType = WeightTypeKind;
using ConvCondRhsType = float;
using ConvCondOutputType = float;
using ConvToGatesWeightType = WeightTypeKind;
using ConvToGatesRhsType = float;
using ConvToGatesOutType = float;
using OutputType = typename WavegruTypes<WeightTypeKind>::GruRhsType;
};
template <typename WeightTypeKind, int kConv1DWeightExponentBits,
int kConv1DRhsExponentBits, int kCondStack0WeightExponentBits,
int kCondStack0RhsExponentBits, int kCondStack1WeightExponentBits,
int kCondStack1RhsExponentBits, int kCondStack2WeightExponentBits,
int kCondStack2RhsExponentBits, int kTranspose0WeightExponentBits,
int kTranspose0RhsExponentBits, int kTranspose1WeightExponentBits,
int kTranspose1RhsExponentBits, int kTranspose2WeightExponentBits,
int kTranspose2RhsExponentBits, int kConvCondWeightExponentBits,
int kConvCondRhsExponentBits, int kConvToGatesWeightExponentBits,
int kConvToGatesRhsExponentBits>
struct ConditioningTypes<
WeightTypeKind, kConv1DWeightExponentBits, kConv1DRhsExponentBits,
kCondStack0WeightExponentBits, kCondStack0RhsExponentBits,
kCondStack1WeightExponentBits, kCondStack1RhsExponentBits,
kCondStack2WeightExponentBits, kCondStack2RhsExponentBits,
kTranspose0WeightExponentBits, kTranspose0RhsExponentBits,
kTranspose1WeightExponentBits, kTranspose1RhsExponentBits,
kTranspose2WeightExponentBits, kTranspose2RhsExponentBits,
kConvCondWeightExponentBits, kConvCondRhsExponentBits,
kConvToGatesWeightExponentBits, kConvToGatesRhsExponentBits,
typename std::enable_if<std::is_same<
WeightTypeKind, csrblocksparse::fixed16_type>::value>::type> {
using DiskWeightType = csrblocksparse::fixed16_type;
using Conv1DWeightType = csrblocksparse::fixed16<kConv1DWeightExponentBits>;
using Conv1DRhsType = csrblocksparse::fixed16<kConv1DRhsExponentBits>;
using CondStack0WeightType =
csrblocksparse::fixed16<kCondStack0WeightExponentBits>;
using CondStack0RhsType = csrblocksparse::fixed16<kCondStack0RhsExponentBits>;
using CondStack1WeightType =
csrblocksparse::fixed16<kCondStack1WeightExponentBits>;
using CondStack1RhsType = csrblocksparse::fixed16<kCondStack1RhsExponentBits>;
using CondStack2WeightType =
csrblocksparse::fixed16<kCondStack2WeightExponentBits>;
using CondStack2RhsType = csrblocksparse::fixed16<kCondStack2RhsExponentBits>;
using Transpose0WeightType =
csrblocksparse::fixed16<kTranspose0WeightExponentBits>;
using Transpose0RhsType = csrblocksparse::fixed16<kTranspose0RhsExponentBits>;
using Transpose1WeightType =
csrblocksparse::fixed16<kTranspose1WeightExponentBits>;
using Transpose1RhsType = csrblocksparse::fixed16<kTranspose1RhsExponentBits>;
using Transpose2WeightType =
csrblocksparse::fixed16<kTranspose2WeightExponentBits>;
using Transpose2RhsType = csrblocksparse::fixed16<kTranspose2RhsExponentBits>;
using ConvCondWeightType =
csrblocksparse::fixed16<kConvCondWeightExponentBits>;
using ConvCondRhsType = csrblocksparse::fixed16<kConvCondRhsExponentBits>;
using ConvCondOutputType =
typename csrblocksparse::TypeOfProduct<ConvCondWeightType,
ConvCondRhsType>::type;
using ConvToGatesWeightType =
csrblocksparse::fixed16<kConvToGatesWeightExponentBits>;
using ConvToGatesRhsType =
csrblocksparse::fixed16<kConvToGatesRhsExponentBits>;
using ConvToGatesOutType =
typename csrblocksparse::TypeOfProduct<ConvToGatesWeightType,
ConvToGatesRhsType>::type;
using OutputType = typename WavegruTypes<WeightTypeKind>::GruRhsType;
};
template <typename WeightTypeKind,
int kProjWeightExponentBits = 2, // proj w
int kScaleWeightExponentBits = 3, // scales w
int kMeanWeightExponentBits = 3, // means w
int kMixWeightExponentBits = 4, // mix w
int kProjMatMulOutExponentBits = 4, // scales i
class Enable = void>
struct ProjectAndSampleTypes;
template <typename WeightTypeKind, int kProjWeightExponentBits,
int kScaleWeightExponentBits, int kMeanWeightExponentBits,
int kMixWeightExponentBits, int kProjMatMulOutExponentBits>
struct ProjectAndSampleTypes<
WeightTypeKind, kProjWeightExponentBits, kScaleWeightExponentBits,
kMeanWeightExponentBits, kMixWeightExponentBits, kProjMatMulOutExponentBits,
typename std::enable_if<
std::is_same<WeightTypeKind, csrblocksparse::bfloat16>::value ||
std::is_same<WeightTypeKind, float>::value>::type> {
using DiskWeightType = float;
using ScratchType = float;
using ProjWeightType = WeightTypeKind;
// The input to the projection layer is the GRU state.
using ProjRhsType = typename WavegruTypes<WeightTypeKind>::GruStateType;
using ProjMatMulOutType = WeightTypeKind;
using ScaleWeightType = WeightTypeKind;
using MeanWeightType = WeightTypeKind;
using MixWeightType = WeightTypeKind;
};
template <typename WeightTypeKind, int kProjWeightExponentBits,
int kScaleWeightExponentBits, int kMeanWeightExponentBits,
int kMixWeightExponentBits, int kProjMatMulOutExponentBits>
struct ProjectAndSampleTypes<
WeightTypeKind, kProjWeightExponentBits, kScaleWeightExponentBits,
kMeanWeightExponentBits, kMixWeightExponentBits, kProjMatMulOutExponentBits,
typename std::enable_if<std::is_same<
WeightTypeKind, csrblocksparse::fixed16_type>::value>::type> {
using DiskWeightType = csrblocksparse::fixed16_type;
#if defined(__aarch64__)
using ScratchType = int;
#else
using ScratchType = float;
#endif
using ProjWeightType = csrblocksparse::fixed16<kProjWeightExponentBits>;
// The input to the projection layer is the GRU state.
using ProjRhsType = typename WavegruTypes<WeightTypeKind>::GruStateType;
using ProjMatMulOutType = csrblocksparse::fixed16<kProjMatMulOutExponentBits>;
using ScaleWeightType = csrblocksparse::fixed16<kScaleWeightExponentBits>;
using MeanWeightType = csrblocksparse::fixed16<kMeanWeightExponentBits>;
using MixWeightType = csrblocksparse::fixed16<kMixWeightExponentBits>;
};
} // namespace codec
} // namespace chromemedia
#endif // LYRA_CODEC_LYRA_TYPES_H_