Standard adapter for the trainable policy model used by RL losses.
| 75 | |
| 76 | |
| 77 | class PolicyModelWrapper(torch.nn.Module): |
| 78 | """Standard adapter for the trainable policy model used by RL losses.""" |
| 79 | |
| 80 | def __init__(self, model: torch.nn.Module): |
| 81 | super().__init__() |
| 82 | self.model = model |
| 83 | |
| 84 | def forward(self, input_ids: torch.Tensor, **model_kwargs: Any) -> Any: |
| 85 | return self.model(input_ids, **model_kwargs) |
| 86 | |
| 87 | def forward_logits(self, input_ids: torch.Tensor, **model_kwargs: Any) -> torch.Tensor: |
| 88 | return extract_logits(self.forward(input_ids, **model_kwargs)) |
| 89 | |
| 90 | def selected_logprobs( |
| 91 | self, |
| 92 | input_ids: torch.Tensor, |
| 93 | token_ids: torch.Tensor, |
| 94 | *, |
| 95 | mask: Optional[torch.Tensor] = None, |
| 96 | logits_start: Optional[int] = None, |
| 97 | logits_end: Optional[int] = None, |
| 98 | temperature: float = 1.0, |
| 99 | output_dtype: torch.dtype = torch.float32, |
| 100 | **model_kwargs: Any, |
| 101 | ) -> torch.Tensor: |
| 102 | logits = self.forward_logits(input_ids, **model_kwargs) |
| 103 | logits = _slice_logits(logits, logits_start=logits_start, logits_end=logits_end) |
| 104 | return selected_logprobs_reference( |
| 105 | logits, |
| 106 | token_ids, |
| 107 | mask=mask, |
| 108 | temperature=temperature, |
| 109 | output_dtype=output_dtype, |
| 110 | ) |
| 111 | |
| 112 | |
| 113 | class ReferenceModelWrapper(PolicyModelWrapper): |
no outgoing calls