Transformer Block Base */
| 180 | |
| 181 | /* Transformer Block Base */ |
| 182 | struct Block { |
| 183 | Block( |
| 184 | int layer_i, |
| 185 | const std::shared_ptr<Config> config, |
| 186 | const Tensor* rms_att_weight, |
| 187 | const Tensor* rms_ffn_weight, |
| 188 | const Tensor* w1, |
| 189 | const Tensor* s1, |
| 190 | const Tensor* w2, |
| 191 | const Tensor* s2, |
| 192 | const Tensor* w3, |
| 193 | const Tensor* s3, |
| 194 | const Tensor* shared_w1, |
| 195 | const Tensor* shared_s1, |
| 196 | const Tensor* shared_w2, |
| 197 | const Tensor* shared_s2, |
| 198 | const Tensor* shared_w3, |
| 199 | const Tensor* shared_s3, |
| 200 | const Tensor* moegate, |
| 201 | const Tensor* moegate_bias |
| 202 | ); |
| 203 | virtual ~Block(); |
| 204 | |
| 205 | float* rms_att_weight() const { return _rms_att_weight ? static_cast<float*>(_rms_att_weight->data) : nullptr; } |
| 206 | float* rms_ffn_weight() const { return _rms_ffn_weight ? static_cast<float*>(_rms_ffn_weight->data) : nullptr; } |
| 207 | std::optional<QTensor> w1() const { return _w1; } |
| 208 | std::optional<QTensor> w2() const { return _w2; } |
| 209 | std::optional<QTensor> w3() const { return _w3; } |
| 210 | std::optional<QTensor> moegate() const { return _moegate; } |
| 211 | std::optional<QTensor> moegate_bias() const { return _moegate_bias; } |
| 212 | std::optional<QTensor> shared_w1() const { return _shared_w1; } |
| 213 | std::optional<QTensor> shared_w2() const { return _shared_w2; } |
| 214 | std::optional<QTensor> shared_w3() const { return _shared_w3; } |
| 215 | |
| 216 | // Compute forward pass for this block and update the inference state accordingly. |
| 217 | // PRECONDITIONS: |
| 218 | // - `s.x()` contains the input to the block. Output will also go here. |
| 219 | // - Block KV cache is hydrated. |
| 220 | void block( |
| 221 | InferenceState& s, // inference state |
| 222 | int pos, // index of the current token in the sequence |
| 223 | int kv_sink, // number of sink tokens currently in the KV cache |
| 224 | int kv_pos, // index of the current token in the kv cache, must be in [0..kv_len) since kv cache is a ring buffer |
| 225 | int kv_len // number of tokens in the kv cache that we will attend over |
| 226 | ) const; |
| 227 | |
| 228 | virtual double active_bytes(size_t pos) const; |
| 229 | |
| 230 | protected: |
| 231 | virtual void attention_impl( |
| 232 | InferenceState& s, // inference state |
| 233 | int pos, // index of the current token in the sequence |
| 234 | int kv_sink, // number of sink tokens currently in the KV cache |
| 235 | int kv_pos, // index of the current token in the kv cache, must be in [0..kv_len) since kv cache is a ring buffer |
| 236 | int kv_len // number of tokens in the kv cache that we will attend over |
| 237 | ) const = 0; |
| 238 | |
| 239 | template <typename T> |
nothing calls this directly
no outgoing calls
no test coverage detected