sequence-layers 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sequence_layers/__init__.py +16 -0
- sequence_layers/internal/types.py +521 -0
- sequence_layers/jax/__init__.py +29 -0
- sequence_layers/jax/attention.py +6124 -0
- sequence_layers/jax/combinators.py +1907 -0
- sequence_layers/jax/conditioning.py +511 -0
- sequence_layers/jax/convolution.py +2103 -0
- sequence_layers/jax/dense.py +447 -0
- sequence_layers/jax/dsp.py +1686 -0
- sequence_layers/jax/examples/gemma3n_audio_encoder.py +358 -0
- sequence_layers/jax/export.py +313 -0
- sequence_layers/jax/meta.py +54 -0
- sequence_layers/jax/normalization.py +889 -0
- sequence_layers/jax/pooling.py +925 -0
- sequence_layers/jax/position.py +370 -0
- sequence_layers/jax/recurrent.py +674 -0
- sequence_layers/jax/sharding.py +48 -0
- sequence_layers/jax/signal.py +511 -0
- sequence_layers/jax/simple.py +2679 -0
- sequence_layers/jax/test_utils.py +1343 -0
- sequence_layers/jax/time_varying.py +673 -0
- sequence_layers/jax/types.py +1537 -0
- sequence_layers/jax/typing.py +59 -0
- sequence_layers/jax/utils.py +2149 -0
- sequence_layers/tensorflow/__init__.py +56 -0
- sequence_layers/tensorflow/attention.py +3426 -0
- sequence_layers/tensorflow/combinators.py +938 -0
- sequence_layers/tensorflow/conditioning.py +644 -0
- sequence_layers/tensorflow/convolution.py +1855 -0
- sequence_layers/tensorflow/dense.py +285 -0
- sequence_layers/tensorflow/dsp.py +740 -0
- sequence_layers/tensorflow/examples/bert.py +144 -0
- sequence_layers/tensorflow/examples/conformer.py +138 -0
- sequence_layers/tensorflow/examples/deep_voice.py +72 -0
- sequence_layers/tensorflow/examples/listen_attend_spell.py +102 -0
- sequence_layers/tensorflow/examples/mobilenet.py +92 -0
- sequence_layers/tensorflow/examples/t5.py +225 -0
- sequence_layers/tensorflow/examples/tacotron.py +124 -0
- sequence_layers/tensorflow/examples/transformer.py +159 -0
- sequence_layers/tensorflow/examples/very_attentive_tacotron.py +1771 -0
- sequence_layers/tensorflow/examples/wavernn.py +225 -0
- sequence_layers/tensorflow/normalization.py +1127 -0
- sequence_layers/tensorflow/pooling.py +462 -0
- sequence_layers/tensorflow/position.py +509 -0
- sequence_layers/tensorflow/recurrent.py +645 -0
- sequence_layers/tensorflow/simple.py +1207 -0
- sequence_layers/tensorflow/squeeze.py +178 -0
- sequence_layers/tensorflow/test_util.py +834 -0
- sequence_layers/tensorflow/time_varying.py +439 -0
- sequence_layers/tensorflow/types.py +961 -0
- sequence_layers/tensorflow/utils.py +858 -0
- sequence_layers-0.1.dist-info/METADATA +69 -0
- sequence_layers-0.1.dist-info/RECORD +55 -0
- sequence_layers-0.1.dist-info/WHEEL +4 -0
- sequence_layers-0.1.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Copyright 2025 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Package directory file for Sequence Layers."""
|
|
15
|
+
|
|
16
|
+
__version__ = '0.1'
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
# Copyright 2023 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Type ABC definitions for streaming sequence layers."""
|
|
15
|
+
|
|
16
|
+
import abc
|
|
17
|
+
import enum
|
|
18
|
+
import fractions
|
|
19
|
+
import re
|
|
20
|
+
from typing import Any, Callable, Generator, Iterable, Optional, Union
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# Type hint aliases for `Any`, but are used here to represent semantic meaning.
|
|
24
|
+
TensorLike = Any
|
|
25
|
+
DType = Any
|
|
26
|
+
Shape = Any
|
|
27
|
+
OpLike = Any
|
|
28
|
+
SequenceLike = Union['Sequence', Any]
|
|
29
|
+
State = SequenceLike
|
|
30
|
+
Constants = Optional[dict[str, SequenceLike]]
|
|
31
|
+
# Any nest of Tensors or Sequences.
|
|
32
|
+
Emits = SequenceLike
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
_CAMEL_TO_SNAKE_R = re.compile(r'((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _camel_to_snake(value):
|
|
39
|
+
return _CAMEL_TO_SNAKE_R.sub(r'_\1', value).lower()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class PaddingMode(enum.Enum):
|
|
43
|
+
CAUSAL = 'causal'
|
|
44
|
+
VALID = 'valid'
|
|
45
|
+
SAME = 'same'
|
|
46
|
+
REVERSE_CAUSAL = 'reverse_causal'
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def validate_padding(padding: str) -> str:
|
|
50
|
+
if padding not in [mode.value for mode in PaddingMode]:
|
|
51
|
+
raise ValueError(
|
|
52
|
+
'Expected padding of "causal", "valid", "same" or "reverse_causal".'
|
|
53
|
+
f' Got {padding}'
|
|
54
|
+
)
|
|
55
|
+
return padding
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Sequence(metaclass=abc.ABCMeta):
|
|
59
|
+
"""A structure representing a sequence and its valid timesteps (a mask).
|
|
60
|
+
|
|
61
|
+
Sequences are immutable.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
@abc.abstractmethod
|
|
66
|
+
def concatenate_sequences(
|
|
67
|
+
cls, sequences: Iterable[SequenceLike]
|
|
68
|
+
) -> SequenceLike:
|
|
69
|
+
raise NotImplementedError()
|
|
70
|
+
|
|
71
|
+
@abc.abstractmethod
|
|
72
|
+
def apply(
|
|
73
|
+
self,
|
|
74
|
+
map_fn: Callable[..., tuple[TensorLike, TensorLike]],
|
|
75
|
+
*args,
|
|
76
|
+
**kwargs,
|
|
77
|
+
) -> SequenceLike:
|
|
78
|
+
raise NotImplementedError()
|
|
79
|
+
|
|
80
|
+
@abc.abstractmethod
|
|
81
|
+
def apply_values(
|
|
82
|
+
self, map_fn: Callable[..., TensorLike], *args, **kwargs
|
|
83
|
+
) -> SequenceLike:
|
|
84
|
+
raise NotImplementedError()
|
|
85
|
+
|
|
86
|
+
@abc.abstractmethod
|
|
87
|
+
def lengths(self) -> TensorLike:
|
|
88
|
+
raise NotImplementedError()
|
|
89
|
+
|
|
90
|
+
@abc.abstractmethod
|
|
91
|
+
def mask_invalid(self) -> SequenceLike:
|
|
92
|
+
"""Returns a new Sequence with invalid timesteps replaced with zeros."""
|
|
93
|
+
raise NotImplementedError()
|
|
94
|
+
|
|
95
|
+
@abc.abstractmethod
|
|
96
|
+
def expanded_mask(self) -> TensorLike:
|
|
97
|
+
"""Returns mask reshaped to the same rank as values."""
|
|
98
|
+
raise NotImplementedError()
|
|
99
|
+
|
|
100
|
+
@abc.abstractmethod
|
|
101
|
+
def concatenate(self, other: SequenceLike) -> SequenceLike:
|
|
102
|
+
raise NotImplementedError()
|
|
103
|
+
|
|
104
|
+
@abc.abstractmethod
|
|
105
|
+
def __getitem__(
|
|
106
|
+
self, the_slice: Union[slice, tuple[slice], tuple[slice, slice]]
|
|
107
|
+
) -> SequenceLike:
|
|
108
|
+
raise NotImplementedError()
|
|
109
|
+
|
|
110
|
+
@abc.abstractmethod
|
|
111
|
+
def pad_time(
|
|
112
|
+
self,
|
|
113
|
+
pad_left: Union[int, TensorLike],
|
|
114
|
+
pad_right: Union[int, TensorLike],
|
|
115
|
+
valid: bool,
|
|
116
|
+
pad_value=0,
|
|
117
|
+
) -> SequenceLike:
|
|
118
|
+
"""Pads this sequence with timesteps on the left and right."""
|
|
119
|
+
raise NotImplementedError()
|
|
120
|
+
|
|
121
|
+
@abc.abstractmethod
|
|
122
|
+
def reverse_time(self) -> SequenceLike:
|
|
123
|
+
raise NotImplementedError()
|
|
124
|
+
|
|
125
|
+
@abc.abstractmethod
|
|
126
|
+
def print(self, message, summarize):
|
|
127
|
+
"""Prints this Sequence with an optional message."""
|
|
128
|
+
raise NotImplementedError()
|
|
129
|
+
|
|
130
|
+
@property
|
|
131
|
+
@abc.abstractmethod
|
|
132
|
+
def channel_shape(self) -> Shape:
|
|
133
|
+
"""Returns the sequence's channel shape (i.e. excluding batch and time)."""
|
|
134
|
+
raise NotImplementedError()
|
|
135
|
+
|
|
136
|
+
@property
|
|
137
|
+
@abc.abstractmethod
|
|
138
|
+
def dtype(self) -> DType:
|
|
139
|
+
"""Returns the values dtype."""
|
|
140
|
+
raise NotImplementedError()
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class SequenceArray(metaclass=abc.ABCMeta):
|
|
144
|
+
"""A SequenceArray is a Array container type for sl.Sequences.
|
|
145
|
+
|
|
146
|
+
Typically used to dynamically concatenate Sequences of fixed length into a
|
|
147
|
+
single Sequence in a loop. For example (in Tensorflow):
|
|
148
|
+
|
|
149
|
+
sa = sl.SequenceArray.new(tf.float32, size=num_steps)
|
|
150
|
+
for i in range(num_steps):
|
|
151
|
+
input_block = inputs[:, i * layer.block_size:(i + 1) * layer.block_size]
|
|
152
|
+
output, state = layer.step(input_block, state, training=False)
|
|
153
|
+
sa = sa.write(i, output)
|
|
154
|
+
result: Sequence = sa.concat()
|
|
155
|
+
|
|
156
|
+
The size of a SequenceArray is the number of Sequence blocks that can be
|
|
157
|
+
written to the SequenceArray, not the total length of the resulting Sequence
|
|
158
|
+
returned from concat.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
@classmethod
|
|
162
|
+
@abc.abstractmethod
|
|
163
|
+
def new(
|
|
164
|
+
cls, dtype: DType, size: TensorLike | None, dynamic_size: bool | None
|
|
165
|
+
) -> 'SequenceArray':
|
|
166
|
+
"""Constructs a new SequenceArray.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
dtype: The dtype of the Sequence's values.
|
|
170
|
+
size: The number of Sequence blocks that will be written to this
|
|
171
|
+
SequenceArray.
|
|
172
|
+
dynamic_size: Whether the array should dynamically grow if write indices
|
|
173
|
+
exceed size.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
A SequenceArray containing the appropriate Array typess for the
|
|
177
|
+
Sequence values and mask.
|
|
178
|
+
"""
|
|
179
|
+
raise NotImplementedError()
|
|
180
|
+
|
|
181
|
+
@abc.abstractmethod
|
|
182
|
+
def write(self, index: TensorLike, sequence: SequenceLike) -> 'SequenceArray':
|
|
183
|
+
"""Writes the provided Sequence to the specified index.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
index: An integer tensor indicating the index to write to.
|
|
187
|
+
sequence: A Sequence to write.
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
A new SequenceArray object that ensures the write occurs. Use this object
|
|
191
|
+
for all subsequent operations.
|
|
192
|
+
"""
|
|
193
|
+
raise NotImplementedError()
|
|
194
|
+
|
|
195
|
+
@abc.abstractmethod
|
|
196
|
+
def concat(self) -> SequenceLike:
|
|
197
|
+
"""Concatenates the Sequences written to this SequenceArray together."""
|
|
198
|
+
raise NotImplementedError()
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class SequenceLayer(metaclass=abc.ABCMeta):
|
|
202
|
+
"""A sequence processing layer that can be executed layerwise or stepwise.
|
|
203
|
+
|
|
204
|
+
Step-wise execution:
|
|
205
|
+
|
|
206
|
+
A SequenceLayer supports step-wise execution if its `supports_step` property
|
|
207
|
+
is true. Most built-in SequenceLayers support step-wise processing by default,
|
|
208
|
+
but may support processing features that are not causal and therefore cannot
|
|
209
|
+
be executed step-by-step (e.g. non-causal convolutions, bidirectional RNNs,
|
|
210
|
+
etc.).
|
|
211
|
+
|
|
212
|
+
When executing step-wise, use the `step` or `step_with_emits` method to
|
|
213
|
+
process a block of inputs (a `Sequence` shaped `[b, block_size * n, ...]`) and
|
|
214
|
+
a `state` input whose structure matches `get_initial_state`.
|
|
215
|
+
|
|
216
|
+
This produces:
|
|
217
|
+
- An output `Sequence` shaped `[b, block_size * n * output_ratio, ...]`
|
|
218
|
+
whose `...` shape matches `get_output_shape`.
|
|
219
|
+
- A `state` output whose structure matches `get_initial_state`.
|
|
220
|
+
- (Optionally) an `emits` output whose structure/specs match `get_emit_specs`.
|
|
221
|
+
|
|
222
|
+
The output `Sequence` is the primary output of the step, while the `emits`
|
|
223
|
+
represent "auxiliary" outputs that are produced by the layer (for example,
|
|
224
|
+
debug output).
|
|
225
|
+
|
|
226
|
+
Layer-wise execution:
|
|
227
|
+
|
|
228
|
+
When executing layer-wise, use the `layer` or `layer_with_emits` method to
|
|
229
|
+
process inputs (a `Sequence` shaped `[b, t, ...]`).
|
|
230
|
+
|
|
231
|
+
This produces:
|
|
232
|
+
- An output `Sequence` shaped `[b, t * output_ratio, ...]`
|
|
233
|
+
whose `...` shape matches `get_output_shape`.
|
|
234
|
+
- (Optionally) an `emits` output whose structure/specs match `get_emit_specs`.
|
|
235
|
+
|
|
236
|
+
The output `Sequence` is the primary output of the layer, while the `emits`
|
|
237
|
+
represent "auxiliary" outputs that are produced by the layer (for example,
|
|
238
|
+
debug output).
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
@classmethod
|
|
242
|
+
def _default_name(cls):
|
|
243
|
+
"""The default module name for this class."""
|
|
244
|
+
return _camel_to_snake(cls.__name__)
|
|
245
|
+
|
|
246
|
+
@property
|
|
247
|
+
def block_size(self) -> int:
|
|
248
|
+
"""The block size multiple required by the layer.
|
|
249
|
+
|
|
250
|
+
Sequences (`[b, t, ...]`) passed to `step` will come in multiples of
|
|
251
|
+
`block_size` timesteps. In other words, `t % block_size == 0`.
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
The layer's block size.
|
|
255
|
+
"""
|
|
256
|
+
return 1
|
|
257
|
+
|
|
258
|
+
@property
|
|
259
|
+
def output_ratio(self) -> fractions.Fraction:
|
|
260
|
+
"""The number of output frames for one input frame."""
|
|
261
|
+
return fractions.Fraction(1)
|
|
262
|
+
|
|
263
|
+
def supports_step(self) -> bool:
|
|
264
|
+
"""Returns whether this layer supports the SequenceLayer.step method."""
|
|
265
|
+
return True
|
|
266
|
+
|
|
267
|
+
@abc.abstractmethod
|
|
268
|
+
def step(
|
|
269
|
+
self,
|
|
270
|
+
x: SequenceLike,
|
|
271
|
+
state: State,
|
|
272
|
+
training: bool,
|
|
273
|
+
constants: Constants | None,
|
|
274
|
+
) -> tuple[SequenceLike, State]:
|
|
275
|
+
"""Process this layer step-wise.
|
|
276
|
+
|
|
277
|
+
If `supports_step` is False, it is an error to call this function and
|
|
278
|
+
incorrect behavior will result.
|
|
279
|
+
|
|
280
|
+
Args:
|
|
281
|
+
x: Input sequence with values shaped [b, t_i, ...], where t_i is a
|
|
282
|
+
multiple of block_size.
|
|
283
|
+
state: A structure of state tensors matching get_initial_state. The
|
|
284
|
+
previous state for this layer.
|
|
285
|
+
training: Python bool. Whether we are in training mode.
|
|
286
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
287
|
+
Values or sequences that are "constant" with respect to the
|
|
288
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
289
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
290
|
+
|
|
291
|
+
Returns:
|
|
292
|
+
y: The outputs corresponding to this step with values shaped [b, t_o, ...]
|
|
293
|
+
where `t_o == t_i * output_ratio`.
|
|
294
|
+
state: A structure of state tensors matching get_initial_state. The
|
|
295
|
+
new state for this layer.
|
|
296
|
+
"""
|
|
297
|
+
raise NotImplementedError()
|
|
298
|
+
|
|
299
|
+
@abc.abstractmethod
|
|
300
|
+
def step_with_emits(
|
|
301
|
+
self,
|
|
302
|
+
x: SequenceLike,
|
|
303
|
+
state: State,
|
|
304
|
+
training: bool,
|
|
305
|
+
constants: Constants | None,
|
|
306
|
+
) -> tuple[SequenceLike, State, Emits]:
|
|
307
|
+
"""Process this layer step-wise, producing emitted tensors.
|
|
308
|
+
|
|
309
|
+
This is like `step`, except it has an additional return value which is the
|
|
310
|
+
"emitted" tensors for the step. The emitted tensors are a structure of
|
|
311
|
+
tensors whose spec matches the return value of `get_emit_specs` and whose
|
|
312
|
+
values are `TensorLikes` or `Sequence`s.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
x: Input sequence with values shaped [b, t_i, ...], where t_i is a
|
|
316
|
+
multiple of block_size.
|
|
317
|
+
state: A structure of state tensors matching get_initial_state. The
|
|
318
|
+
previous state for this layer.
|
|
319
|
+
training: Python bool. Whether we are in training mode.
|
|
320
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
321
|
+
Values or sequences that are "constant" with respect to the
|
|
322
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
323
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
324
|
+
|
|
325
|
+
Returns:
|
|
326
|
+
y: The outputs corresponding to this step with values shaped [b, t_o, ...]
|
|
327
|
+
where `t_o == t_i * output_ratio`.
|
|
328
|
+
state: A structure of state tensors matching get_initial_state. The
|
|
329
|
+
new state for this layer.
|
|
330
|
+
emits: A nest of emitted tensors or Sequences. The nest structure and
|
|
331
|
+
tensor specs match `get_emit_specs`.
|
|
332
|
+
"""
|
|
333
|
+
outputs, state = self.step(x, state, training, constants)
|
|
334
|
+
return outputs, state, ()
|
|
335
|
+
|
|
336
|
+
@abc.abstractmethod
|
|
337
|
+
def layer(
|
|
338
|
+
self,
|
|
339
|
+
x: SequenceLike,
|
|
340
|
+
training: bool,
|
|
341
|
+
initial_state: State | None,
|
|
342
|
+
constants: Constants | None,
|
|
343
|
+
) -> SequenceLike:
|
|
344
|
+
"""Process this layer layer-wise.
|
|
345
|
+
|
|
346
|
+
Args:
|
|
347
|
+
x: Input sequence with values shaped [b, t_i, ...].
|
|
348
|
+
training: Python bool. Whether we are in training mode.
|
|
349
|
+
initial_state: A structure of state tensors matching get_initial_state.
|
|
350
|
+
The initial state to use for this layer.
|
|
351
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
352
|
+
Values or sequences that are "constant" with respect to the
|
|
353
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
354
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
y: The outputs corresponding to this layer with values shaped
|
|
358
|
+
[b, t_o, ...] where `t_o == t_i * output_ratio`. t_o may have been
|
|
359
|
+
truncated to only represent valid frames.
|
|
360
|
+
"""
|
|
361
|
+
raise NotImplementedError()
|
|
362
|
+
|
|
363
|
+
@abc.abstractmethod
|
|
364
|
+
def layer_with_emits(
|
|
365
|
+
self,
|
|
366
|
+
x: SequenceLike,
|
|
367
|
+
training: bool,
|
|
368
|
+
initial_state: State | None,
|
|
369
|
+
constants: Constants | None,
|
|
370
|
+
) -> tuple[Sequence, Emits]:
|
|
371
|
+
"""Process this layer layer-wise, producing emitted tensors.
|
|
372
|
+
|
|
373
|
+
This is like `layer`, except it has an additional return value which is the
|
|
374
|
+
"emitted" tensors for the laeyr. The emitted tensors are a structure of
|
|
375
|
+
tensors whose spec matches the return value of `get_emit_specs` and whose
|
|
376
|
+
values are `TensorLikes` or `Sequence`s.
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
x: Input sequence with values shaped [b, t_i, ...].
|
|
380
|
+
training: Python bool. Whether we are in training mode.
|
|
381
|
+
initial_state: A structure of state tensors matching get_initial_state.
|
|
382
|
+
The initial state to use for this layer.
|
|
383
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
384
|
+
Values or sequences that are "constant" with respect to the
|
|
385
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
386
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
387
|
+
|
|
388
|
+
Returns:
|
|
389
|
+
y: The outputs corresponding to this layer with values shaped
|
|
390
|
+
[b, t_o, ...] where `t_o == t_i * output_ratio`. t_o may have been
|
|
391
|
+
truncated to only represent valid frames.
|
|
392
|
+
emits: A nest of emitted tensors or Sequences. The nest structure and
|
|
393
|
+
tensor specs match `get_emit_specs`.
|
|
394
|
+
"""
|
|
395
|
+
outputs = self.layer(x, training, initial_state, constants)
|
|
396
|
+
return outputs, ()
|
|
397
|
+
|
|
398
|
+
@abc.abstractmethod
|
|
399
|
+
def get_initial_state(
|
|
400
|
+
self, x: SequenceLike, constants: Constants | None
|
|
401
|
+
) -> State:
|
|
402
|
+
"""Returns the initial state for this SequenceLayer.
|
|
403
|
+
|
|
404
|
+
Args:
|
|
405
|
+
x: Sequence. A representative input sequence. Do not rely on its values,
|
|
406
|
+
just shape.
|
|
407
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
408
|
+
Values or sequences that are "constant" with respect to the
|
|
409
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
410
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
411
|
+
|
|
412
|
+
Returns:
|
|
413
|
+
An integer, TensorShape or structure of integer/TensorShapes.
|
|
414
|
+
"""
|
|
415
|
+
raise NotImplementedError()
|
|
416
|
+
|
|
417
|
+
@abc.abstractmethod
|
|
418
|
+
def get_output_shape_for_sequence(
|
|
419
|
+
self, x: SequenceLike, constants: Constants | None
|
|
420
|
+
) -> Shape:
|
|
421
|
+
"""Returns the output shape this layer produces for the provided Sequence.
|
|
422
|
+
|
|
423
|
+
A convenience wrapper around get_output_shape.
|
|
424
|
+
|
|
425
|
+
Args:
|
|
426
|
+
x: Sequence. An input sequence.
|
|
427
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
428
|
+
Values or sequences that are "constant" with respect to the
|
|
429
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
430
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
431
|
+
|
|
432
|
+
Returns:
|
|
433
|
+
A TensorShape representing the channels dimensions (i.e. not including the
|
|
434
|
+
batch or time dimension).
|
|
435
|
+
"""
|
|
436
|
+
return self.get_output_shape(x.channel_shape, constants=constants)
|
|
437
|
+
|
|
438
|
+
@abc.abstractmethod
|
|
439
|
+
def get_output_shape(
|
|
440
|
+
self, input_shape: Shape, constants: Constants | None
|
|
441
|
+
) -> Shape:
|
|
442
|
+
"""Returns the output shape this layer produces for an input shape.
|
|
443
|
+
|
|
444
|
+
Args:
|
|
445
|
+
input_shape: A TensorShape representing the channels dimension of the
|
|
446
|
+
input sequence (i.e. not including the batch or time dimension).
|
|
447
|
+
constants: A dictionary of constant name to TensorLike or sl.Sequence.
|
|
448
|
+
Values or sequences that are "constant" with respect to the
|
|
449
|
+
SequenceLayer, but may affect its processing. For example, for an
|
|
450
|
+
Attention layer this may contain the key/value sequence to attend to.
|
|
451
|
+
|
|
452
|
+
Returns:
|
|
453
|
+
A TensorShape representing the channels dimensions (i.e. not including the
|
|
454
|
+
batch or time dimension).
|
|
455
|
+
"""
|
|
456
|
+
raise NotImplementedError()
|
|
457
|
+
|
|
458
|
+
@abc.abstractmethod
|
|
459
|
+
def get_output_dtype(self, input_dtype: DType) -> DType:
|
|
460
|
+
"""Returns the layer's output dtype."""
|
|
461
|
+
# Default to no change of dtype in the layer. The dtype altering layers
|
|
462
|
+
# should override this.
|
|
463
|
+
return input_dtype
|
|
464
|
+
|
|
465
|
+
@abc.abstractmethod
|
|
466
|
+
def _yield_emits(
|
|
467
|
+
self, emits: Emits
|
|
468
|
+
) -> Generator[tuple['SequenceLayer', Emits], None, None]:
|
|
469
|
+
"""Yields (layer, emits) tuples to allow associating emits with layers."""
|
|
470
|
+
yield self, emits
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
class Emitting(SequenceLayer, metaclass=abc.ABCMeta):
|
|
474
|
+
"""A SequenceLayer that emits auxiliary tensors.
|
|
475
|
+
|
|
476
|
+
This is a convenience subclass that implements step and layer in terms of
|
|
477
|
+
step_with_emits and layer_with_emits, so that implementors need only implement
|
|
478
|
+
two of the four methods. For emits that are substantially expensive to compute
|
|
479
|
+
subclasses can choose to implement all four and save computation in those that
|
|
480
|
+
do not produce emits.
|
|
481
|
+
"""
|
|
482
|
+
|
|
483
|
+
@abc.abstractmethod
|
|
484
|
+
def step(
|
|
485
|
+
self,
|
|
486
|
+
x: SequenceLike,
|
|
487
|
+
state: State,
|
|
488
|
+
training: bool,
|
|
489
|
+
constants: Constants | None,
|
|
490
|
+
) -> tuple[SequenceLike, State]:
|
|
491
|
+
raise NotImplementedError()
|
|
492
|
+
|
|
493
|
+
@abc.abstractmethod
|
|
494
|
+
def step_with_emits(
|
|
495
|
+
self,
|
|
496
|
+
x: SequenceLike,
|
|
497
|
+
state: State,
|
|
498
|
+
training: bool,
|
|
499
|
+
constants: Constants | None,
|
|
500
|
+
) -> tuple[SequenceLike, State, Emits]:
|
|
501
|
+
raise NotImplementedError()
|
|
502
|
+
|
|
503
|
+
@abc.abstractmethod
|
|
504
|
+
def layer(
|
|
505
|
+
self,
|
|
506
|
+
x: SequenceLike,
|
|
507
|
+
training: bool,
|
|
508
|
+
initial_state: State | None,
|
|
509
|
+
constants: Constants | None,
|
|
510
|
+
) -> SequenceLike:
|
|
511
|
+
raise NotImplementedError()
|
|
512
|
+
|
|
513
|
+
@abc.abstractmethod
|
|
514
|
+
def layer_with_emits(
|
|
515
|
+
self,
|
|
516
|
+
x: SequenceLike,
|
|
517
|
+
training: bool,
|
|
518
|
+
initial_state: State | None,
|
|
519
|
+
constants: Constants | None,
|
|
520
|
+
) -> tuple[SequenceLike, Emits]:
|
|
521
|
+
raise NotImplementedError()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Copyright 2024 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Sequence layers in JAX."""
|
|
15
|
+
|
|
16
|
+
# pylint: disable=wildcard-import
|
|
17
|
+
from sequence_layers.jax.attention import *
|
|
18
|
+
from sequence_layers.jax.combinators import *
|
|
19
|
+
from sequence_layers.jax.conditioning import *
|
|
20
|
+
from sequence_layers.jax.convolution import *
|
|
21
|
+
from sequence_layers.jax.dense import *
|
|
22
|
+
from sequence_layers.jax.dsp import *
|
|
23
|
+
from sequence_layers.jax.normalization import *
|
|
24
|
+
from sequence_layers.jax.pooling import *
|
|
25
|
+
from sequence_layers.jax.position import *
|
|
26
|
+
from sequence_layers.jax.recurrent import *
|
|
27
|
+
from sequence_layers.jax.simple import *
|
|
28
|
+
from sequence_layers.jax.time_varying import *
|
|
29
|
+
from sequence_layers.jax.types import *
|