MCPcopy Create free account
hub / github.com/OpenRaiser/NanoResearch / compute_logprobs

Method compute_logprobs

nanoresearch/evolution/ram.py:368–396  ·  view source on GitHub ↗

Compute per-token log-probabilities for ``output_text`` given ``input_text`` as context. Used by the SDPO trainer for the original policy: ``log π_θ(y_i | x, y_{<i})``.

(
        self, input_text: str, output_text: str
    )

Source from the content-addressed store, hash-verified

366 # ---- log-prob computation (for SDPO trainer, Step 8) ------------------
367
368 def compute_logprobs(
369 self, input_text: str, output_text: str
370 ) -> "torch.Tensor":
371 """Compute per-token log-probabilities for ``output_text`` given
372 ``input_text`` as context.
373
374 Used by the SDPO trainer for the original policy:
375 ``log π_θ(y_i | x, y_{<i})``.
376 """
377 self._lazy_load()
378 import torch
379
380 full_text = input_text + output_text
381 input_ids = self._tokenizer.encode(
382 full_text, return_tensors="pt", truncation=True, max_length=4096
383 ).to(self._model.device)
384 input_len = len(self._tokenizer.encode(input_text, truncation=True, max_length=4096))
385
386 with torch.no_grad():
387 logits = self._model(input_ids).logits
388
389 # Shift logits and labels for next-token prediction
390 shift_logits = logits[0, input_len - 1 : -1, :] # predictions for output tokens
391 shift_labels = input_ids[0, input_len:] # actual output tokens
392
393 log_probs = torch.nn.functional.log_softmax(shift_logits, dim=-1)
394 token_log_probs = log_probs.gather(1, shift_labels.unsqueeze(1)).squeeze(1)
395
396 return token_log_probs
397
398 def compute_hindsight_logprobs(
399 self, input_text: str, feedback: str, output_text: str

Callers 1

Calls 1

_lazy_loadMethod · 0.95

Tested by

no test coverage detected