fix: pad token perf in perplexity scoring, SGLang SSRF validation

- Use -100 ignore_index for pad tokens in perplexity computation (avoids
  redundant softmax on padding positions)
- Block URL-based model paths in SGLang create_sglang_runtime (SSRF protection)
This commit is contained in:
Alpamys 2026-03-26 13:51:57 +05:00
parent 0b7759898c
commit bc743e2814
2 changed files with 14 additions and 1 deletions

View File

@ -63,7 +63,10 @@ def compute_perplexity_scores(
shift_labels = input_ids[:, 1:].contiguous()
shift_mask = attention_mask[:, 1:].contiguous()
loss_fct = torch.nn.CrossEntropyLoss(reduction="none")
# Set pad positions to -100 so CrossEntropyLoss skips them
shift_labels = shift_labels.masked_fill(shift_mask == 0, -100)
loss_fct = torch.nn.CrossEntropyLoss(reduction="none", ignore_index=-100)
per_token_loss = loss_fct(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),

View File

@ -47,8 +47,18 @@ def create_sglang_runtime(
Returns:
(runtime, runtime_model_name) tuple.
"""
import re
import sglang as sgl
# SSRF protection: block URL-based model paths
for path_val in (model_path, base_model):
if path_val and re.match(r'^https?://', path_val):
raise ValueError(
"model_path/base_model must be a local path or HuggingFace model ID, "
"not a URL"
)
# For LoRA adapters, load the base model
if is_adapter and base_model:
runtime = sgl.Runtime(