| 29 | |
| 30 | @functools.partial(jax.checkpoint, prevent_cse=False) |
| 31 | def summarize_chunk(query, key, value): |
| 32 | attn_weights = jnp.einsum("...qhd,...khd->...qhk", query, key, precision=precision) |
| 33 | |
| 34 | max_score = jnp.max(attn_weights, axis=-1, keepdims=True) |
| 35 | max_score = jax.lax.stop_gradient(max_score) |
| 36 | exp_weights = jnp.exp(attn_weights - max_score) |
| 37 | |
| 38 | exp_values = jnp.einsum("...vhf,...qhv->...qhf", value, exp_weights, precision=precision) |
| 39 | max_score = jnp.einsum("...qhk->...qh", max_score) |
| 40 | |
| 41 | return (exp_values, exp_weights.sum(axis=-1), max_score) |
| 42 | |
| 43 | def chunk_scanner(chunk_idx): |
| 44 | # julienne key array |