| from abc import ABC, abstractmethod |
| from dataclasses import dataclass, field |
| import logging |
| from typing import Optional |
|
|
| from transformers import GenerationConfig |
|
|
| from interactions.tensor_utils import TensorHelper, TensorConfig |
|
|
|
|
| @dataclass |
| class InteractionConfig: |
| max_turns: int = 1 |
| max_start_length: int = 1024 |
| max_prompt_length: int = 4096 |
| max_response_length: int = 512 |
| max_obs_length: int = 512 |
| |
| temperature: float = 1.0 |
| batch_size: int = 8 |
| output_dir: Optional[str] = None |
| weaver_do_sample: bool = False |
| trigger_do_sample: bool = False |
|
|
| @dataclass |
| class InteractionDataProto: |
| batch: dict = field(default_factory=dict) |
| no_tensor_batch: dict = field(default_factory=dict) |
|
|
| class InteractionManager(ABC): |
| |
| def __init__( |
| self, |
| tokenizer, |
| actor_rollout_wg, |
| config: InteractionConfig, |
| is_validation: bool = False, |
| ): |
| self.tokenizer = tokenizer |
| self.tokenizer.padding_side = "left" |
| self.actor_rollout_wg = actor_rollout_wg |
| self.config = config |
| self.is_validation = is_validation |
| |
| assert tokenizer.pad_token_id is not None |
| self.tensor_fn = TensorHelper(TensorConfig( |
| pad_token_id=tokenizer.pad_token_id, |
| max_prompt_length=config.max_prompt_length, |
| max_obs_length=config.max_obs_length, |
| max_start_length=config.max_start_length |
| )) |
|
|
| |
| self.generation_config = GenerationConfig( |
| max_new_tokens=self.config.max_response_length, |
| temperature=self.config.temperature, |
| pad_token_id=self.tokenizer.pad_token_id, |
| eos_token_id=self.tokenizer.eos_token_id |
| ) |
| self.generation_config.weaver_do_sample = self.config.weaver_do_sample |
| self.generation_config.trigger_do_sample = self.config.trigger_do_sample |
|
|
| logging.info(f"Weaver do sample: {self.generation_config.weaver_do_sample}, Trigger do sample: {self.generation_config.trigger_do_sample}") |
| |
| @abstractmethod |
| def run_agent_loop(self, gen_batch: InteractionDataProto) -> InteractionDataProto: |
| ... |