Computes attention over a sequence of inputs. Args: xs: input sequence of shape (batch_size, sequence_length, num_hidden) start_of_sequence: An input array of shape (batch_size) --- The following must be passed by keyword only. --- importance: Array of shape (batch_si
(
self,
xs: Array,
start_of_sequence: Array,
*,
importance: Optional[Array] = None,
cross_attention_kv: Optional[Tuple[Array, Array]] = None,
window_state: Optional[WindowState] = None,
decoder_state: Optional[DecoderState] = None,
)
| 117 | return (DecoderState(out_decoder_state), out_keys, out_values) |
| 118 | |
| 119 | def __call__( |
| 120 | self, |
| 121 | xs: Array, |
| 122 | start_of_sequence: Array, |
| 123 | *, |
| 124 | importance: Optional[Array] = None, |
| 125 | cross_attention_kv: Optional[Tuple[Array, Array]] = None, |
| 126 | window_state: Optional[WindowState] = None, |
| 127 | decoder_state: Optional[DecoderState] = None, |
| 128 | ): |
| 129 | """Computes attention over a sequence of inputs. |
| 130 | |
| 131 | Args: |
| 132 | xs: input sequence of shape (batch_size, sequence_length, num_hidden) |
| 133 | start_of_sequence: An input array of shape (batch_size) --- The following |
| 134 | must be passed by keyword only. --- |
| 135 | importance: Array of shape (batch_size, sequence_length). An importance |
| 136 | bias for attention. |
| 137 | cross_attention_kv: Keys and values from encoder for cross-attention. |
| 138 | window_state: State object which contains context from the prior window |
| 139 | when using a transformer-XL or sliding window. Initially created with |
| 140 | load_window_state(). |
| 141 | decoder_state: State object for autoregressive decoding, initially created |
| 142 | with from init_decoder_state(). |
| 143 | |
| 144 | Returns: |
| 145 | (ys: outputs of shape (batch_size, sequence_length, num_hidden), |
| 146 | importance_score: importance score for the next layer, |
| 147 | next_window_state: state to pass to the next window, |
| 148 | next_decoder_state: next decoder state for autoregressive decoding, |
| 149 | viz_dict: dictionary of visualizations |
| 150 | ) |
| 151 | """ |
| 152 | |
| 153 | xs = jnp.asarray(xs, dtype=self.dtype) |
| 154 | logging.info("tlayer: recurrent = %r", self.recurrent_attention) |
| 155 | logging.info("tlayer: compute_importance = %r", self.compute_importance) |
| 156 | |
| 157 | is_training = self.mode == "train" |
| 158 | |
| 159 | # Compute keys, values and queries. |
| 160 | # --------------------------------- |
| 161 | logging.info("tlayer: compute keys,values,queries.") |
| 162 | (keys, values, queries, queries2) = self.tbase.kvq(xs) |
| 163 | attention_scale_factors = self.tbase.attention_scale_factors() |
| 164 | (_, sequence_length, num_heads, _) = queries.shape # (b, k, h, d) |
| 165 | |
| 166 | # Get biases and masks that are shared across windows. |
| 167 | # ---------------------------------------------------- |
| 168 | if decoder_state is not None: |
| 169 | logging.info("tlayer: using autoregressive decoder.") |
| 170 | # When decoding, prior keys,values are loaded from the decoder state. |
| 171 | # Other values are precomputed, and loaded from the decoder state. |
| 172 | # The decoder state will be updated with the current token. |
| 173 | assert window_state is None |
| 174 | |
| 175 | prev_kvi = None |
| 176 | recurrent_state = None # Use precomputed recurrent_kvq. |
nothing calls this directly
no test coverage detected