-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence_models.py
144 lines (116 loc) · 4.5 KB
/
sequence_models.py
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
"""This module provides sequence modeling wrappers."""
from typing import Any, Optional
import flax.linen as nn
import jax.numpy as jnp
from .types import Array
class SequenceModel(nn.Module):
"""A sequence model wrapper for discrete token sequences.
This model wraps around a given sequence processing module. It first transforms \
input tokens using an embedding layer, passes them through the inner module, and \
finally decodes them through a dense layer.
Attributes:
module: \
The inner sequence processing module.
num_tokens: \
The number of unique input tokens.
emb_dim: \
The embedding/feature dimension. Defaults to the `dim` attribute \
of the inner module if not specified.
out_dim: \
The dimension of the output of the model. Defaults to `num_tokens` if \
not specified.
pool: \
If `True`, applies mean pooling over the sequence dimension before the \
output layer.
"""
module: nn.Module
num_tokens: int
emb_dim: Optional[int] = None
out_dim: Optional[int] = None
pool: bool = False
@nn.compact
def __call__(self, inputs: Array, *args: Any, **kwargs: Any) -> Array:
"""Applies the sequence model to the input tokens.
Args:
inputs: \
The input array of token IDs with shape `(batch_size, input_length)`.
*args: \
Additional arguments to be passed to the inner module call.
**kwargs: \
Additional keyword arguments to be passed to the inner module call.
Returns:
The output data with shape `(batch_size, input_length, out_dim)`.
"""
# Variable aliasing (for convenience)
module = self.module
num_tokens = self.num_tokens
emb_dim = self.emb_dim
out_dim = self.out_dim
pool = self.pool
# Apply defaults
out_dim = out_dim if (out_dim is not None) else num_tokens
emb_dim = emb_dim if (emb_dim is not None) else module.dim
# Forward pass
return sequence_model(
module=module,
embed=nn.Embed(num_tokens, emb_dim),
out_dim=out_dim,
pool=pool,
)(inputs, *args, **kwargs)
class ContinuousSequenceModel(nn.Module):
"""A sequence model wrapper for continuous-valued sequences.
This model wraps around a given sequence processing module. It first transforms \
input data using a dense layer, passes them through the inner module, and finally \
decodes them with another dense layer.
Attributes:
module: \
The inner sequence processing module.
out_dim: \
The dimension of the output of the model.
emb_dim: \
The embedding/feature dimension. Defaults to the `dim` attribute \
of the inner module if not specified.
pool: \
If `True`, applies mean pooling over the sequence dimension before the \
output layer.
"""
module: nn.Module
out_dim: int
emb_dim: Optional[int] = None
pool: bool = False
@nn.compact
def __call__(self, inputs: Array, *args: Any, **kwargs: Any) -> Array:
"""Applies the sequence model to the input data.
Args:
inputs: \
The input data with shape `(batch_size, input_length, input_dim)`.
*args: \
Additional arguments to be passed to the inner module call.
**kwargs: \
Additional keyword arguments to be passed to the inner module call.
Returns:
The output data with shape `(batch_size, input_length, out_dim)`.
"""
# Variable aliasing (for convenience)
module = self.module
out_dim = self.out_dim
emb_dim = self.emb_dim
pool = self.pool
# Apply defaults
emb_dim = emb_dim if (emb_dim is not None) else module.dim
# Forward pass
return sequence_model(
module=module,
embed=nn.Dense(emb_dim),
out_dim=out_dim,
pool=pool,
)(inputs, *args, **kwargs)
def sequence_model(module: nn.Module, embed: nn.Module, out_dim: int, pool: bool):
def forward(inputs: Array, *args: Any, **kwargs: Any) -> Array:
x = embed(inputs)
x = module(x, *args, **kwargs)
if pool:
x = jnp.mean(x, axis=1)
x = nn.Dense(out_dim)(x)
return x
return forward