def setup(self):
args = self.config
self.head_size = args.head_size_a
self.n_head = args.dim_att // self.head_size
self.min_clamp = args.min_clamp
assert args.dim_att % self.n_head == 0
ratio_0_to_1 = self.layer_id / ((2 if args.n_layer==1 else args.n_layer) - 1)
ratio_1_to_almost0 = 1.0 - (self.layer_id / args.n_layer)
def init_time_maa(name, ratio):
def init(key, shape):
return 1.0 - jnp.power((jnp.arange(args.n_embd) / args.n_embd), ratio).reshape(1, 1, -1)
return self.param(f'blocks.{self.layer_id}.att.{name}', init, (1, 1, args.n_embd))
self.time_maa_x = init_time_maa('time_maa_x', ratio_1_to_almost0)
self.time_maa_w = init_time_maa('time_maa_w', ratio_1_to_almost0)
self.time_maa_k = init_time_maa('time_maa_k', ratio_1_to_almost0)
self.time_maa_v = init_time_maa('time_maa_v', ratio_1_to_almost0 + 0.3 * ratio_0_to_1)
self.time_maa_r = init_time_maa('time_maa_r', 0.5 * ratio_1_to_almost0)
self.time_maa_g = init_time_maa('time_maa_g', 0.5 * ratio_1_to_almost0)
def init_time_decay(key, shape):
return (-6 + 5 * (jnp.arange(args.dim_att) / (args.dim_att - 1)) ** (0.7 + 1.3 * ratio_0_to_1))
self.time_decay = self.param(f'blocks.{self.layer_id}.att.time_decay', init_time_decay, (1, args.dim_att))
def init_time_faaaa(key, shape):
n = jnp.arange(args.dim_att)
return (ratio_0_to_1 * (1 - (n / max(args.dim_att - 1, 1))) + ((n + 1) % 3 - 1) * 0.1).reshape((self.n_head, -1))
self.time_faaaa = self.param(f'blocks.{self.layer_id}.att.time_faaaa', init_time_faaaa, (self.n_head, self.head_size))
self.time_maa_w1 = self.param(f'blocks.{self.layer_id}.att.time_maa_w1', nn.initializers.uniform(1e-4), (args.n_embd, 5*32))
self.time_maa_w2 = self.param(f'blocks.{self.layer_id}.att.time_maa_w2', nn.initializers.uniform(1e-4), (5, 32, args.n_embd))
self.time_decay_w1 = self.param(f'blocks.{self.layer_id}.att.time_decay_w1', nn.initializers.uniform(1e-4), (args.n_embd, 64))
self.time_decay_w2 = self.param(f'blocks.{self.layer_id}.att.time_decay_w2', nn.initializers.uniform(1e-4), (64, args.n_embd))
self.receptance = nn.Dense(args.dim_att, use_bias=False, name=f'blocks.{self.layer_id}.att.receptance')
self.key = nn.Dense(args.dim_att, use_bias=False, name=f'blocks.{self.layer_id}.att.key')
self.value = nn.Dense(args.dim_att, use_bias=False, name=f'blocks.{self.layer_id}.att.value')
self.output = nn.Dense(args.n_embd, use_bias=False, name=f'blocks.{self.layer_id}.att.output')
self.gate = nn.Dense(args.dim_att, use_bias=False, name=f'blocks.{self.layer_id}.att.gate')
self.ln_x = GroupNorm(num_groups=self.n_head, name=f'blocks.{self.layer_id}.att.ln_x')
self.time_maa_k_channel = init_time_maa(f'blocks.{self.layer_id}.ffn.time_maa_k', ratio_1_to_almost0)
self.time_maa_r_channel = init_time_maa(f'blocks.{self.layer_id}.ffn.time_maa_r', ratio_1_to_almost0)
self.key_channel = nn.Dense(args.dim_ffn, use_bias=False, name=f'blocks.{self.layer_id}.ffn.key')
self.receptance_channel = nn.Dense(args.n_embd, use_bias=False, name=f'blocks.{self.layer_id}.ffn.receptance')
self.value_channel = nn.Dense(args.n_embd, use_bias=False, name=f'blocks.{self.layer_id}.ffn.value')
self.ln1 = nn.LayerNorm(epsilon=self.config.layer_norm_epsilon, name=f'blocks.{self.layer_id}.ln1')
self.ln2 = nn.LayerNorm(epsilon=self.config.layer_norm_epsilon, name=f'blocks.{self.layer_id}.ln2')
if self.layer_id == 0:
self.ln0 = nn.LayerNorm(epsilon=self.config.layer_norm_epsilon, name='blocks.0.ln0')
self.dropout = nn.Dropout(rate=self.config.dropout)
def time_shift(self, x):
return jnp.pad(x[:, :-1], ((0, 0), (1, 0), (0, 0)))
@partial(jax.vmap, in_axes=(None, 0, 0, 0, 0, 0, 0))
def chunkwise(self, k, v, r, w, u, state):
C = self.config.chunk_size
L, D = r.shape
pad_size = (C - L % C) % C
pad_fn = lambda x: jnp.pad(x, ((0, pad_size), (0, 0)))
r, k, v, w, u = map(pad_fn, [r, k, v, w, u])
L_padded = L + pad_size
r, k, v, w, u = map(lambda x: x.reshape((L_padded // C, C, -1)), [r, k, v, w, u])
w = jax.lax.clamp(self.min_clamp, w, 1.0)
w = jnp.log(w)
A = jnp.exp(jnp.cumsum(w, axis=1))
A_inter = jnp.exp(jax.lax.cumsum(w, axis=1, reverse=True) - w)
A_intra = jnp.cumsum(w, axis=1)
kv = jnp.einsum('nij,nik->nijk', A_inter * k, v)
def scan_fn(carry, x):
s = carry
A_t, kv_t = x
new_s = A_t * s + kv_t
return new_s, s
new_state, S = jax.lax.scan(scan_fn, state, (A.reshape(L_padded, D), kv.reshape(L_padded, D, D)))
S = S.reshape(L_padded // C, C, D, D)
inter_chunk = jnp.einsum('nij,nijk->nik', r * jnp.exp(A_intra - w), S)
def intra_chunk_fn(r_chunk, k_chunk, v_chunk, A_chunk, u_chunk):
mid = C // 2
r_decay = r_chunk * jnp.exp(jnp.pad(A_chunk[:-1, :], ((1, 0), (0, 0))) - A_chunk[mid:mid+1, :])
k_decay = k_chunk * jnp.exp(A_chunk[mid:mid+1, :] - A_chunk)
u_diag = jnp.diag(jnp.sum(r_chunk * u_chunk * k_chunk, -1))
return (jnp.tril(r_decay @ k_decay.T, -1) + u_diag) @ v_chunk
intra_chunk = jax.vmap(intra_chunk_fn)(r, k, v, A_intra, u)
result = (inter_chunk + intra_chunk).reshape((L_padded, -1))
return result[:L], new_state
def time_mixing(self, x, state):
B, T, C = x.shape
H, S = self.n_head, self.head_size
xx = self.time_shift(x)
sx = xx - x
xx = x + sx * self.time_maa_x
xx = jnn.tanh(xx @ self.time_maa_w1).reshape((B, T, 5, -1)).swapaxes(1, 2)
mw, mk, mv, mr, mg = (xx @ self.time_maa_w2).reshape((5, B, T, -1))
xw = x + sx * (self.time_maa_w + mw)
xk = x + sx * (self.time_maa_k + mk)
xv = x + sx * (self.time_maa_v + mv)
xr = x + sx * (self.time_maa_r + mr)
xg = x + sx * (self.time_maa_g + mg)
r = self.receptance(xr).reshape(B, T, H, S)
k = self.key(xk).reshape(B, T, H, S)
v = self.value(xv).reshape(B, T, H, S)
g = jnn.silu(self.gate(xg))
time_decay = self.time_decay.reshape(1, 1, H, S)
time_decay_offset = jnn.tanh(xw @ self.time_decay_w1) @ self.time_decay_w2
time_decay_offset = time_decay_offset.reshape(B, T, H, S)
w = jnp.exp(-jnp.exp(time_decay + time_decay_offset) + 1e-8)
u = jnp.broadcast_to(self.time_faaaa, (B, T, H, S))
state = state.reshape(B * H, S, S)
k = k.transpose(0, 2, 1, 3).reshape(B * H, T, S)
v = v.transpose(0, 2, 1, 3).reshape(B * H, T, S)
r = r.transpose(0, 2, 1, 3).reshape(B * H, T, S)
w = w.transpose(0, 2, 1, 3).reshape(B * H, T, S)
u = u.transpose(0, 2, 1, 3).reshape(B * H, T, S)
y, new_state = self.chunkwise(k, v, r, w, u, state)
y = y.reshape(B, H, T, S).transpose(0, 2, 1, 3).reshape(B, T, H * S)
new_state = new_state.reshape(B, H, S, S)
y = self.ln_x(y)
y = self.output(y * g)
return y, new_state
def channel_mixing(self, x):
xx = self.time_shift(x)
sx = xx - x
xk = x + sx * self.time_maa_k_channel
xr = x + sx * self.time_maa_r_channel
k = jax.nn.relu(self.key_channel(xk)) ** 2
kv = self.value_channel(k)
return jax.nn.sigmoid(self.receptance_channel(xr)) * kv
def __call__(self, x, state, deterministic=False, rngs=None):
x_attn, new_state = self.time_mixing(self.ln1(x), state)
x = x + x_attn
x = x + self.channel_mixing(self.ln2(x))
if not deterministic:
x = self.dropout(x, deterministic=deterministic, rng=rngs['dropout'] if rngs is not None else None)
return x, new_state