MCPcopy Create free account
hub / github.com/evilsocket/cake / encode

Method encode

cake-core/src/models/flux/text_encoder.rs:378–393  ·  view source on GitHub ↗

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>)

Source from the content-addressed store, hash-verified

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}

Callers 3

generate_imageMethod · 0.45
generate_imageMethod · 0.45
forwardMethod · 0.45

Calls 4

embeddingMethod · 0.80
forward_with_maskMethod · 0.80
pushMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected