[Fix] model init / XPU / transformers-v5 / bench-image fixes (#28292)
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
b42c79c4eb
commit
e63b57da0b
@@ -115,7 +115,10 @@ def create_mm_data_row(
|
||||
# Note (Xinyuan): This is a workaround for an issue where some tokenizers do not support content as a list. (e.g. InternVL)
|
||||
print(f"Error applying chat template: {e}, fallback to <image> tag")
|
||||
# Some tokenizers do not support list content; fall back to a placeholder in the text
|
||||
prompt_str = f"<image>{text_prompt}"
|
||||
if type(processor).__name__ == "MiniCPMOProcessor":
|
||||
prompt_str = f"(<image>./</image>){text_prompt}"
|
||||
else:
|
||||
prompt_str = f"<image>{text_prompt}"
|
||||
|
||||
# Calculate total tokens (text + vision)
|
||||
if type(processor).__name__ == "KimiK25Processor":
|
||||
@@ -125,6 +128,12 @@ def create_mm_data_row(
|
||||
medias=medias,
|
||||
return_tensors="pt",
|
||||
)["input_ids"].numel()
|
||||
elif type(processor).__name__ == "VLChatProcessor":
|
||||
prompt_len = processor(
|
||||
prompt=prompt_str,
|
||||
images=images,
|
||||
force_batchify=False,
|
||||
)["input_ids"].numel()
|
||||
elif type(processor).__name__ == "DeepseekVLV2Processor":
|
||||
result = processor(
|
||||
conversations=prompt_str,
|
||||
|
||||
@@ -1227,7 +1227,7 @@ def tensor_hash(tensor_list) -> int:
|
||||
# CPU path: hash each tensor incrementally without concat
|
||||
hasher = hashlib.sha256()
|
||||
for t in tensors:
|
||||
t = t.detach().contiguous()
|
||||
t = t.detach().cpu().contiguous()
|
||||
hasher.update(memoryview(t.reshape(-1).view(torch.uint8).numpy()))
|
||||
hash_bytes = hasher.digest()[:8]
|
||||
return int.from_bytes(hash_bytes, byteorder="big", signed=False)
|
||||
@@ -1235,7 +1235,7 @@ def tensor_hash(tensor_list) -> int:
|
||||
# Single tensor
|
||||
if tensor.is_cuda:
|
||||
return gpu_tensor_hash(tensor.cuda())
|
||||
tensor = tensor.detach().contiguous()
|
||||
tensor = tensor.detach().cpu().contiguous()
|
||||
hasher = hashlib.sha256()
|
||||
hasher.update(memoryview(tensor.reshape(-1).view(torch.uint8).numpy()))
|
||||
hash_bytes = hasher.digest()[:8]
|
||||
|
||||
@@ -45,7 +45,7 @@ from sglang.srt.layers.linear import (
|
||||
)
|
||||
from sglang.srt.layers.logits_processor import LogitsProcessor
|
||||
from sglang.srt.layers.moe.moe_runner import MoeRunnerConfig
|
||||
from sglang.srt.layers.moe.moe_runner.triton_utils import fused_moe
|
||||
from sglang.srt.layers.moe.moe_runner.triton_utils.fused_moe import fused_moe
|
||||
from sglang.srt.layers.moe.topk import TopK
|
||||
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
||||
from sglang.srt.layers.radix_attention import RadixAttention
|
||||
|
||||
@@ -48,10 +48,11 @@ from sglang.srt.layers.vocab_parallel_embedding import (
|
||||
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
||||
from sglang.srt.model_loader.weight_utils import default_weight_loader
|
||||
from sglang.srt.runtime_context import get_parallel
|
||||
from sglang.srt.utils import add_prefix, is_npu
|
||||
from sglang.srt.utils import add_prefix, is_npu, is_xpu
|
||||
from sglang.srt.utils.hf_transformers_utils import get_rope_config
|
||||
|
||||
_is_npu = is_npu()
|
||||
_is_xpu = is_xpu()
|
||||
|
||||
|
||||
def _get_alibi_slopes(total_num_heads: int) -> torch.Tensor:
|
||||
@@ -190,7 +191,9 @@ class BaiChuanAttention(nn.Module):
|
||||
alibi_slopes = _get_alibi_slopes(self.total_num_heads)
|
||||
alibi_slopes = alibi_slopes[head_start:head_end]
|
||||
self.alibi_slopes = torch.tensor(
|
||||
alibi_slopes, dtype=dtype, device="npu" if _is_npu else "cuda"
|
||||
alibi_slopes,
|
||||
dtype=dtype,
|
||||
device="npu" if _is_npu else "xpu" if _is_xpu else "cuda",
|
||||
)
|
||||
else:
|
||||
self.rotary_emb = get_rope(
|
||||
|
||||
@@ -577,9 +577,8 @@ class Gemma3TextModel(PreTrainedModel):
|
||||
|
||||
global_config = copy.deepcopy(config)
|
||||
global_config.rope_parameters = {
|
||||
**rope_params["full_attention"],
|
||||
"rope_theta": global_theta,
|
||||
"factor": config.rope_parameters["full_attention"]["factor"],
|
||||
"rope_type": "linear",
|
||||
}
|
||||
self.rotary_emb = Gemma3RotaryEmbedding(config=global_config)
|
||||
self.gradient_checkpointing = False
|
||||
|
||||
@@ -389,16 +389,21 @@ class Gemma3nAttention(nn.Module):
|
||||
self.head_dim,
|
||||
rotary_dim=self.head_dim,
|
||||
max_position=config.max_position_embeddings,
|
||||
base=config.rope_local_base_freq,
|
||||
base=config.rope_parameters.get("sliding_attention", {}).get(
|
||||
"rope_theta", 10000.0
|
||||
),
|
||||
rope_scaling={"rope_type": "default"},
|
||||
)
|
||||
else:
|
||||
full_attn_rope = config.rope_parameters.get("full_attention", {})
|
||||
self.rotary_emb = get_rope(
|
||||
self.head_dim,
|
||||
rotary_dim=self.head_dim,
|
||||
max_position=config.max_position_embeddings,
|
||||
base=config.rope_parameters["rope_theta"],
|
||||
rope_scaling=config.rope_parameters,
|
||||
base=full_attn_rope.get("rope_theta", 1000000.0),
|
||||
rope_scaling=(
|
||||
full_attn_rope if full_attn_rope else {"rope_type": "default"}
|
||||
),
|
||||
)
|
||||
|
||||
self.sliding_window = config.sliding_window if self.is_sliding else None
|
||||
|
||||
@@ -445,8 +445,8 @@ class Gemma3nForConditionalGeneration(PreTrainedModel):
|
||||
input_ids, hidden_states, self.language_model.embed_tokens, forward_batch
|
||||
)
|
||||
|
||||
def tie_weights(self):
|
||||
return self.language_model.tie_weights()
|
||||
def tie_weights(self, **kwargs):
|
||||
return self.language_model.tie_weights(**kwargs)
|
||||
|
||||
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
||||
stacked_params_mapping = [
|
||||
|
||||
@@ -82,11 +82,12 @@ class LightOnOCRForConditionalGeneration(nn.Module):
|
||||
|
||||
# Build VisionEncoderArgs from config
|
||||
vision_config = config.vision_config
|
||||
config_dict = vision_config.to_dict()
|
||||
if config_dict.get("rope_parameters"):
|
||||
config_dict["rope_theta"] = config_dict["rope_parameters"].get("rope_theta")
|
||||
dataclass_fields = {field.name for field in fields(VisionEncoderArgs)}
|
||||
vision_args = {
|
||||
key: value
|
||||
for key, value in vision_config.to_dict().items()
|
||||
if key in dataclass_fields
|
||||
key: value for key, value in config_dict.items() if key in dataclass_fields
|
||||
}
|
||||
# LightOnOCR stores these at the top-level config
|
||||
if "image_token_id" not in vision_args:
|
||||
|
||||
@@ -388,7 +388,7 @@ class Phi3SmallForCausalLM(nn.Module):
|
||||
quant_config=quant_config,
|
||||
prefix=add_prefix("lm_head", prefix),
|
||||
)
|
||||
if self.config.tie_word_embeddings:
|
||||
if getattr(self.config, "tie_word_embeddings", True):
|
||||
self.lm_head.weight = self.model.embed_tokens.weight
|
||||
self.logits_processor = LogitsProcessor(config)
|
||||
self.pooler = Pooler(pooling_type=PoolingType.LAST, normalize=True)
|
||||
@@ -466,7 +466,10 @@ class Phi3SmallForCausalLM(nn.Module):
|
||||
continue
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
continue
|
||||
if self.config.tie_word_embeddings and "lm_head.weight" in name:
|
||||
if (
|
||||
getattr(self.config, "tie_word_embeddings", True)
|
||||
and "lm_head.weight" in name
|
||||
):
|
||||
continue
|
||||
|
||||
param = params_dict[name]
|
||||
|
||||
@@ -118,7 +118,9 @@ def _getattr_first(obj, names, default=None):
|
||||
|
||||
|
||||
def _resolve_attention_backend_model_cls(config: PretrainedConfig):
|
||||
model_cls = getattr(transformers, getattr(config, "architectures", [""])[0], None)
|
||||
model_cls = getattr(
|
||||
transformers, (getattr(config, "architectures", None) or [""])[0], None
|
||||
)
|
||||
if model_cls is not None:
|
||||
return model_cls
|
||||
|
||||
|
||||
Reference in New Issue
Block a user