mirror of
https://github.com/FlashML-org/FreeToken.git
synced 2026-11-25 12:42:56 +08:00
* feat(mm): serve image input on the Qwen VL families - frontend: OpenAI image_url, Anthropic image blocks and Responses input_image (http(s), base64, gated file://) become MMItems in the tokenizer workers; image spans carry content pseudo ids so the radix cache keys them by content; mrope positions are precomputed per request - registry: EncoderSpec per family (Qwen3-VL, Qwen3.6, Qwen3.8-Flash-Next register a vision encoder serving image) and ModelSpec.mm_processor; EngineConfig.active_encoders decides what a process builds, served_modalities what it accepts; --text-model-only, --mm-disable, --mm-encoder-weights, --mm-embed-cache-device, --mm-max-pixels - engine and scheduler: per-request encoder-cache row ownership, chunk planning on the batch (jobs, gather rows, scatter rows), items encoded right before the LM forward and gathered into batch.mm_embeds - models: the Qwen VL vision tower on the text tower's TP layers with DeepStack, block weights streamed from pinned host banks by default, merger outputs parked on the host between blocks; SupportsMultimodal (encode, place_encoder_weights) and a shared embed_input_ids - server: image inputs rejected with a client-facing reason when no vision encoder is served; GET /v1/stats reports model.input_modalities * feat(mm): replace the pixel cap with a token budget and processor kwargs - --image-min-tokens / --image-max-tokens replace --mm-max-pixels; MMProcessor.get_mm_processor_kwargs turns the MultimodalConfig into the processor call's arguments, the default passes mm.processor_kwargs through and Qwen VL converts the budget to pixel areas in size.shortest_edge / longest_edge - --mm-processor-kwargs hands a JSON object to the checkpoint's image processor call, applied after the token budget - MMProcessor is an ABC that owns the model path, the config and the lazily loaded image processor; get_mm_processor returns None when --mm-disable leaves no encoder to serve - the frontend tokenizer, the tokenizer worker and the offline LLM build one processor each and hand it to TokenizeManager, so the tokenize message carries no per-request pixel cap
94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from contextlib import contextmanager
|
|
from typing import TYPE_CHECKING, Protocol, runtime_checkable
|
|
|
|
import torch
|
|
|
|
from freetoken.layers import (
|
|
BaseOP,
|
|
LinearColParallelMerged,
|
|
LinearRowParallel,
|
|
gelu_and_mul,
|
|
gelu_tanh_and_mul,
|
|
silu_and_mul,
|
|
)
|
|
from freetoken.utils import nvtx_annotate
|
|
|
|
if TYPE_CHECKING:
|
|
import torch
|
|
|
|
from freetoken.core import Batch
|
|
from freetoken.layers.quantization import QuantConfig
|
|
|
|
from .config import ModelConfig
|
|
|
|
|
|
class BaseLLMModel(ABC, BaseOP):
|
|
@abstractmethod
|
|
def forward(self) -> torch.Tensor: ...
|
|
|
|
@contextmanager
|
|
def forward_host_ctx(self, batch: Batch, use_graph: bool):
|
|
"""Around one forward dispatch: enter before it is enqueued, exit right after. A backend that feeds the forward from host memory overrides this."""
|
|
yield
|
|
|
|
|
|
@runtime_checkable
|
|
class SupportsMultimodal(Protocol):
|
|
"""What the engine asks of a model that serves multimodal items; the text forward takes its token embeddings from embed_input_ids."""
|
|
|
|
def encode(self, item: MMItem) -> torch.Tensor:
|
|
"""``[item.num_tokens, D]`` soft tokens for one item of any modality the family serves, on the model device."""
|
|
...
|
|
|
|
def place_encoder_weights(self, mode: str) -> None:
|
|
"""Every encoder tower's weights: ``gpu`` resident or ``host`` streamed from pinned banks."""
|
|
...
|
|
|
|
|
|
def embed_input_ids(embed_tokens, input_ids: torch.Tensor, batch: Batch) -> torch.Tensor:
|
|
"""Token embeddings of the batch; on a chunk with multimodal rows, batch.mm_embeds' leading columns replace the rows batch.mm_rows."""
|
|
if batch.mm_embeds is None:
|
|
return embed_tokens.forward(input_ids)
|
|
# multimodal rows carry content pad ids above the vocab: clamp for the lookup, the copy overwrites those rows
|
|
x = embed_tokens.forward(input_ids.clamp(max=embed_tokens.num_embeddings - 1))
|
|
x.index_copy_(0, batch.mm_rows, batch.mm_embeds[:, : x.shape[1]].to(x.dtype))
|
|
return x
|
|
|
|
|
|
class GatedMLP(BaseOP):
|
|
def __init__(self, config: ModelConfig, *, quant_config: QuantConfig | None = None, prefix: str = ""):
|
|
self.gate_up_proj = LinearColParallelMerged(
|
|
config.hidden_size,
|
|
[config.intermediate_size, config.intermediate_size],
|
|
has_bias=False,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.gate_up_proj",
|
|
)
|
|
|
|
fn_map = {"silu": silu_and_mul, "gelu": gelu_and_mul, "gelu_tanh": gelu_tanh_and_mul}
|
|
act_fn = fn_map.get(config.hidden_act, None)
|
|
if act_fn is None:
|
|
raise ValueError(f"Unsupported activation function: {config.hidden_act}")
|
|
self.act_fn = act_fn
|
|
self.down_proj = LinearRowParallel(
|
|
config.intermediate_size,
|
|
config.hidden_size,
|
|
has_bias=False,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.down_proj",
|
|
)
|
|
|
|
@nvtx_annotate("MLP")
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
gate_up = self.gate_up_proj.forward(x)
|
|
del x
|
|
y = self.act_fn(gate_up)
|
|
del gate_up
|
|
return self.down_proj.forward(y)
|
|
|
|
|
|
__all__ = ["BaseLLMModel", "GatedMLP"]
|