[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)
|
# 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")
|
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
|
# 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)
|
# Calculate total tokens (text + vision)
|
||||||
if type(processor).__name__ == "KimiK25Processor":
|
if type(processor).__name__ == "KimiK25Processor":
|
||||||
@@ -125,6 +128,12 @@ def create_mm_data_row(
|
|||||||
medias=medias,
|
medias=medias,
|
||||||
return_tensors="pt",
|
return_tensors="pt",
|
||||||
)["input_ids"].numel()
|
)["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":
|
elif type(processor).__name__ == "DeepseekVLV2Processor":
|
||||||
result = processor(
|
result = processor(
|
||||||
conversations=prompt_str,
|
conversations=prompt_str,
|
||||||
|
|||||||
@@ -1227,7 +1227,7 @@ def tensor_hash(tensor_list) -> int:
|
|||||||
# CPU path: hash each tensor incrementally without concat
|
# CPU path: hash each tensor incrementally without concat
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
for t in tensors:
|
for t in tensors:
|
||||||
t = t.detach().contiguous()
|
t = t.detach().cpu().contiguous()
|
||||||
hasher.update(memoryview(t.reshape(-1).view(torch.uint8).numpy()))
|
hasher.update(memoryview(t.reshape(-1).view(torch.uint8).numpy()))
|
||||||
hash_bytes = hasher.digest()[:8]
|
hash_bytes = hasher.digest()[:8]
|
||||||
return int.from_bytes(hash_bytes, byteorder="big", signed=False)
|
return int.from_bytes(hash_bytes, byteorder="big", signed=False)
|
||||||
@@ -1235,7 +1235,7 @@ def tensor_hash(tensor_list) -> int:
|
|||||||
# Single tensor
|
# Single tensor
|
||||||
if tensor.is_cuda:
|
if tensor.is_cuda:
|
||||||
return gpu_tensor_hash(tensor.cuda())
|
return gpu_tensor_hash(tensor.cuda())
|
||||||
tensor = tensor.detach().contiguous()
|
tensor = tensor.detach().cpu().contiguous()
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
hasher.update(memoryview(tensor.reshape(-1).view(torch.uint8).numpy()))
|
hasher.update(memoryview(tensor.reshape(-1).view(torch.uint8).numpy()))
|
||||||
hash_bytes = hasher.digest()[:8]
|
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.logits_processor import LogitsProcessor
|
||||||
from sglang.srt.layers.moe.moe_runner import MoeRunnerConfig
|
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.moe.topk import TopK
|
||||||
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
||||||
from sglang.srt.layers.radix_attention import RadixAttention
|
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_executor.forward_batch_info import ForwardBatch
|
||||||
from sglang.srt.model_loader.weight_utils import default_weight_loader
|
from sglang.srt.model_loader.weight_utils import default_weight_loader
|
||||||
from sglang.srt.runtime_context import get_parallel
|
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
|
from sglang.srt.utils.hf_transformers_utils import get_rope_config
|
||||||
|
|
||||||
_is_npu = is_npu()
|
_is_npu = is_npu()
|
||||||
|
_is_xpu = is_xpu()
|
||||||
|
|
||||||
|
|
||||||
def _get_alibi_slopes(total_num_heads: int) -> torch.Tensor:
|
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 = _get_alibi_slopes(self.total_num_heads)
|
||||||
alibi_slopes = alibi_slopes[head_start:head_end]
|
alibi_slopes = alibi_slopes[head_start:head_end]
|
||||||
self.alibi_slopes = torch.tensor(
|
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:
|
else:
|
||||||
self.rotary_emb = get_rope(
|
self.rotary_emb = get_rope(
|
||||||
|
|||||||
@@ -577,9 +577,8 @@ class Gemma3TextModel(PreTrainedModel):
|
|||||||
|
|
||||||
global_config = copy.deepcopy(config)
|
global_config = copy.deepcopy(config)
|
||||||
global_config.rope_parameters = {
|
global_config.rope_parameters = {
|
||||||
|
**rope_params["full_attention"],
|
||||||
"rope_theta": global_theta,
|
"rope_theta": global_theta,
|
||||||
"factor": config.rope_parameters["full_attention"]["factor"],
|
|
||||||
"rope_type": "linear",
|
|
||||||
}
|
}
|
||||||
self.rotary_emb = Gemma3RotaryEmbedding(config=global_config)
|
self.rotary_emb = Gemma3RotaryEmbedding(config=global_config)
|
||||||
self.gradient_checkpointing = False
|
self.gradient_checkpointing = False
|
||||||
|
|||||||
@@ -389,16 +389,21 @@ class Gemma3nAttention(nn.Module):
|
|||||||
self.head_dim,
|
self.head_dim,
|
||||||
rotary_dim=self.head_dim,
|
rotary_dim=self.head_dim,
|
||||||
max_position=config.max_position_embeddings,
|
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"},
|
rope_scaling={"rope_type": "default"},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
full_attn_rope = config.rope_parameters.get("full_attention", {})
|
||||||
self.rotary_emb = get_rope(
|
self.rotary_emb = get_rope(
|
||||||
self.head_dim,
|
self.head_dim,
|
||||||
rotary_dim=self.head_dim,
|
rotary_dim=self.head_dim,
|
||||||
max_position=config.max_position_embeddings,
|
max_position=config.max_position_embeddings,
|
||||||
base=config.rope_parameters["rope_theta"],
|
base=full_attn_rope.get("rope_theta", 1000000.0),
|
||||||
rope_scaling=config.rope_parameters,
|
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
|
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
|
input_ids, hidden_states, self.language_model.embed_tokens, forward_batch
|
||||||
)
|
)
|
||||||
|
|
||||||
def tie_weights(self):
|
def tie_weights(self, **kwargs):
|
||||||
return self.language_model.tie_weights()
|
return self.language_model.tie_weights(**kwargs)
|
||||||
|
|
||||||
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
||||||
stacked_params_mapping = [
|
stacked_params_mapping = [
|
||||||
|
|||||||
@@ -82,11 +82,12 @@ class LightOnOCRForConditionalGeneration(nn.Module):
|
|||||||
|
|
||||||
# Build VisionEncoderArgs from config
|
# Build VisionEncoderArgs from config
|
||||||
vision_config = config.vision_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)}
|
dataclass_fields = {field.name for field in fields(VisionEncoderArgs)}
|
||||||
vision_args = {
|
vision_args = {
|
||||||
key: value
|
key: value for key, value in config_dict.items() if key in dataclass_fields
|
||||||
for key, value in vision_config.to_dict().items()
|
|
||||||
if key in dataclass_fields
|
|
||||||
}
|
}
|
||||||
# LightOnOCR stores these at the top-level config
|
# LightOnOCR stores these at the top-level config
|
||||||
if "image_token_id" not in vision_args:
|
if "image_token_id" not in vision_args:
|
||||||
|
|||||||
@@ -388,7 +388,7 @@ class Phi3SmallForCausalLM(nn.Module):
|
|||||||
quant_config=quant_config,
|
quant_config=quant_config,
|
||||||
prefix=add_prefix("lm_head", prefix),
|
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.lm_head.weight = self.model.embed_tokens.weight
|
||||||
self.logits_processor = LogitsProcessor(config)
|
self.logits_processor = LogitsProcessor(config)
|
||||||
self.pooler = Pooler(pooling_type=PoolingType.LAST, normalize=True)
|
self.pooler = Pooler(pooling_type=PoolingType.LAST, normalize=True)
|
||||||
@@ -466,7 +466,10 @@ class Phi3SmallForCausalLM(nn.Module):
|
|||||||
continue
|
continue
|
||||||
if name.endswith(".bias") and name not in params_dict:
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
continue
|
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
|
continue
|
||||||
|
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
|
|||||||
@@ -118,7 +118,9 @@ def _getattr_first(obj, names, default=None):
|
|||||||
|
|
||||||
|
|
||||||
def _resolve_attention_backend_model_cls(config: PretrainedConfig):
|
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:
|
if model_cls is not None:
|
||||||
return model_cls
|
return model_cls
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user