Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Libraries/MLXVLM/Models/Idefics3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,33 @@ public class Idefics3: Module, VLMModel, KVCacheDimensionProvider {
{
let inputIds = input.text.tokens
let pixelValues = input.image?.pixels
let embeddings = getInputEmbeddings(
var embeddings = getInputEmbeddings(
inputIds: inputIds,
pixelValues: pixelValues
)

// Prefill the merged image+text embeddings in windowSize-sized chunks,
// matching mlx-vlm (and `LLMModel.prepare`'s token chunking): evaluate
// the KV cache between chunks, leaving the last embedding for the logits.
let prefillStepSize = windowSize ?? 512
let totalTokens = embeddings.dim(1)

var processed = 0
while embeddings.dim(1) > 1 {
let nToProcess = min(prefillStepSize, embeddings.dim(1) - 1)
let chunk = embeddings[0..., ..<nToProcess]
_ = languageModel(nil, cache: cache, inputs_embeds: chunk)
eval(cache)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using asyncEval here, see #225

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cools, will take a look and revert.

embeddings = embeddings[0..., nToProcess...]
processed += nToProcess
}

// The prefix is now in the KV cache; the final embedding yields the
// first-token logits.
precondition(
processed == totalTokens - 1,
"Idefics3 chunked prefill: expected one residual embedding, processed "
+ "\(processed) of \(totalTokens)")
let result = languageModel(nil, cache: cache, inputs_embeds: embeddings)
return .logits(result)
}
Expand Down
Loading