actor.py 993 Bytes
Newer Older
1
from typing import Optional
Fazzie-Maqianli's avatar
Fazzie-Maqianli committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

import torch
import torch.nn as nn

from ..lora import LoRAModule


class Actor(LoRAModule):
    """
    Actor model base class.

    Args:
        model (nn.Module): Actor Model.
        lora_rank (int): LoRA rank.
        lora_train_bias (str): LoRA bias training mode.
    """

    def __init__(self, model: nn.Module, lora_rank: int = 0, lora_train_bias: str = 'none') -> None:
        super().__init__(lora_rank=lora_rank, lora_train_bias=lora_train_bias)
        self.model = model
        self.convert_to_lora()

    def forward(self,
25
26
27
28
29
                input_ids: torch.LongTensor,
                attention_mask: Optional[torch.Tensor] = None,
                **model_kwargs,  # HACK: `generate` method may pass more kwargs
                ) -> torch.Tensor:
        """Returns model output.
Fazzie-Maqianli's avatar
Fazzie-Maqianli committed
30
        """
31
32
33
34
35
36
        output = self.model(
            input_ids,
            attention_mask=attention_mask,
            **model_kwargs
        )
        return output