Run the full encoder forward pass, returning hidden states. FLUX.2-klein extracts hidden states from layers [9, 18, 27] (0-indexed) and concatenates them: 3 × 2560 = 7680 = joint_attention_dim. Encode with attention mask. attn_mask: (1, seq) with 1=real, 0=padding.
(&self, token_ids: &Tensor, attn_mask: Option<&Tensor>)
| 376 | /// and concatenates them: 3 × 2560 = 7680 = joint_attention_dim. |
| 377 | /// Encode with attention mask. attn_mask: (1, seq) with 1=real, 0=padding. |
| 378 | pub fn encode(&self, token_ids: &Tensor, attn_mask: Option<&Tensor>) -> Result<Tensor> { |
| 379 | const OUTPUT_LAYERS: [usize; 3] = [8, 17, 26]; |
| 380 | |
| 381 | let mut x = self.backend.embedding(token_ids, &self.embeddings_weight)?; |
| 382 | let mut layer_outputs: Vec<Tensor> = Vec::new(); |
| 383 | |
| 384 | for (i, block) in self.blocks.iter().enumerate() { |
| 385 | x = block.forward_with_mask(&x, attn_mask)?; |
| 386 | if OUTPUT_LAYERS.contains(&i) { |
| 387 | layer_outputs.push(x.clone()); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Concatenate along feature dimension: (b, seq, 2560*3) = (b, seq, 7680) |
| 392 | Tensor::cat(&layer_outputs, candle_core::D::Minus1) |
| 393 | } |
| 394 | } |
no test coverage detected