2023-03-28 12:25:36 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import torch.nn as nn
|
2023-04-07 03:39:09 +00:00
|
|
|
from transformers import LlamaConfig, LlamaModel
|
2023-03-28 12:25:36 +00:00
|
|
|
|
|
|
|
from ..base import Critic
|
|
|
|
|
|
|
|
|
|
|
|
class LlamaCritic(Critic):
|
|
|
|
"""
|
|
|
|
Llama Critic model.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pretrained (str): Pretrained model name or path.
|
|
|
|
config (LlamaConfig): Model config.
|
|
|
|
checkpoint (bool): Enable gradient checkpointing.
|
|
|
|
lora_rank (int): LoRA rank.
|
|
|
|
lora_train_bias (str): LoRA bias training mode.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
pretrained: Optional[str] = None,
|
|
|
|
config: Optional[LlamaConfig] = None,
|
|
|
|
checkpoint: bool = False,
|
|
|
|
lora_rank: int = 0,
|
|
|
|
lora_train_bias: str = 'none',
|
|
|
|
**kwargs) -> None:
|
|
|
|
|
|
|
|
if pretrained is not None:
|
2023-04-07 03:39:09 +00:00
|
|
|
model = LlamaModel.from_pretrained(pretrained)
|
2023-03-28 12:25:36 +00:00
|
|
|
elif config is not None:
|
2023-04-07 03:39:09 +00:00
|
|
|
model = LlamaModel(config)
|
2023-03-28 12:25:36 +00:00
|
|
|
else:
|
2023-04-07 03:39:09 +00:00
|
|
|
model = LlamaModel(LlamaConfig())
|
2023-03-28 12:25:36 +00:00
|
|
|
|
|
|
|
if checkpoint:
|
|
|
|
model.gradient_checkpointing_enable()
|
|
|
|
|
|
|
|
value_head = nn.Linear(model.config.hidden_size, 1)
|
|
|
|
|
|
|
|
super().__init__(model, value_head, lora_rank, lora_train_bias, **kwargs)
|