Skip to content

rome.train.llm

LLM trainer tasks: GRPO (reinforcement) and SFT (supervised).

The trainer shipped with the framework (the ProteinMPNN trainer lives with the IMPRESS-R example, examples/impress_r/mpnn.py). It is the demonstration that "adding new models and training algorithms requires just one task": everything below this line is TRL-specific, and nothing above it — the data manager, the training manager, the stream manager — knows that an LLM is involved.

The task takes the dataset the data manager built, runs TRL's GRPO over it, and writes a checkpoint (a LoRA adapter when one is configured, otherwise the full model). The training manager publishes that path and the inference streams hot-swap onto it.

ModelConfig dataclass

ModelConfig(base_model_name: Optional[str] = None, model_name: Optional[str] = None, lora_name: Optional[str] = None, lora_config: Optional['LoraConfig'] = None, generation_config: Optional['GenerationConfig'] = None, dtype: Optional[str] = 'auto', required_gpus: int = 1, device_map: Any = 'auto', max_seq_length: int = 2048)

Model configurations used for loading the model and tokenizer, as well as generation.

Parameters:

  • base_model_name (Optional[str], default: None ) –

    Foundation model name, used to load tokenizer and model weights (when model_name is not provided). Should be a model name recognized by HuggingFace's AutoModelForCausalLM and AutoTokenizer classes.

  • model_name (Optional[str], default: None ) –

    Full model name, used to load model weights. If no model_name is provided, base_model_name is used.

  • lora_name (Optional[str], default: None ) –

    LoRA adapter name, used to load LoRA adapter weights. If no lora_name is provided, the full model is trained and saved.

  • lora_config (Optional['LoraConfig'], default: None ) –

    Adapter shape, used when lora_name names a directory that does not exist yet. ROME's defaults are used when omitted.

  • generation_config (Optional['GenerationConfig'], default: None ) –

    Generation parameters, used by inference streams sharing this config.

  • dtype (Optional[str], default: 'auto' ) –

    Loading and placement knobs forwarded to transformers.

  • device_map (Optional[str], default: 'auto' ) –

    Loading and placement knobs forwarded to transformers.

  • required_gpus (Optional[str], default: 'auto' ) –

    Loading and placement knobs forwarded to transformers.

  • max_seq_length (Optional[str], default: 'auto' ) –

    Loading and placement knobs forwarded to transformers.

resolved_model_name

resolved_model_name() -> str

The weights to load, preferring a fine-tuned checkpoint over the base.

Source code in rome/train/llm.py
def resolved_model_name(self) -> str:
    """The weights to load, preferring a fine-tuned checkpoint over the base."""
    name = self.model_name or self.base_model_name
    if name is None:
        raise ValueError(
            "ModelConfig needs either base_model_name or model_name"
        )
    return name

tokenizer_name

tokenizer_name() -> str

Tokenizers come from the foundation model whenever one is named.

Source code in rome/train/llm.py
def tokenizer_name(self) -> str:
    """Tokenizers come from the foundation model whenever one is named."""
    name = self.base_model_name or self.model_name
    if name is None:
        raise ValueError(
            "ModelConfig needs either base_model_name or model_name"
        )
    return name

GRPOConfig dataclass

GRPOConfig(model_config: Optional[ModelConfig] = None, learning_rate: float = 5e-06, batch_size: int = 4, num_epochs: int = 3, gradient_accumulation_steps: int = 16, num_generations: int = 4, reward_funcs: List[Callable] = list(), prompt_column: str = 'prompt', rollout_func: Optional[Callable] = None, trl_config: Any = None, extra_args: Dict[str, Any] = dict())

Configuration class for managing GRPO training settings in the ROME framework.

Holds only the knobs ROME itself cares about. Anything else TRL accepts goes in trl_config (a trl.GRPOConfig) or in extra_args, which is merged into the TRL config that gets built.

Parameters:

  • model_config (ModelConfig, default: None ) –

    What to train. Required.

  • learning_rate (float, default: 5e-06 ) –

    The usual. Defaults follow the values ROME used for its LLM runs.

  • batch_size (float, default: 5e-06 ) –

    The usual. Defaults follow the values ROME used for its LLM runs.

  • num_epochs (float, default: 5e-06 ) –

    The usual. Defaults follow the values ROME used for its LLM runs.

  • gradient_accumulation_steps (float, default: 5e-06 ) –

    The usual. Defaults follow the values ROME used for its LLM runs.

  • num_generations (int, default: 4 ) –

    Completions GRPO samples per prompt.

  • reward_funcs (List[Callable], default: list() ) –

    Reward functions TRL calls inline during training, with the signature fn(prompts, completions, **kwargs) -> list[float]. Rewards that are expensive or need their own resources belong in a ROME reward stream instead — see rome.stream.

  • prompt_column (str, default: 'prompt' ) –

    Field of a corpus record holding the prompt TRL should train on.

  • trl_config (Optional[GRPOConfig], default: None ) –

    Fully-specified TRL config. Overrides the individual knobs above.

  • extra_args (dict, default: dict() ) –

    Additional keyword arguments merged into the constructed TRL config.

build_trl_config

build_trl_config(output_dir: str) -> Any

Materialize a trl.GRPOConfig.

Imported here rather than at module scope so the manager process — which only ever constructs configs — never has to pay for TRL.

Source code in rome/train/llm.py
def build_trl_config(self, output_dir: str) -> Any:
    """Materialize a ``trl.GRPOConfig``.

    Imported here rather than at module scope so the manager process — which
    only ever constructs configs — never has to pay for TRL.
    """
    if self.trl_config is not None:
        return self.trl_config
    from trl import GRPOConfig as TRLGRPOConfig

    args = dict(
        output_dir=output_dir,
        learning_rate=self.learning_rate,
        adam_beta1=0.9,
        adam_beta2=0.99,
        weight_decay=0.01,
        warmup_ratio=0.1,
        lr_scheduler_type="cosine",
        logging_steps=1,
        num_train_epochs=self.num_epochs,
        per_device_train_batch_size=self.batch_size,
        gradient_accumulation_steps=self.gradient_accumulation_steps,
        num_generations=self.num_generations,
        # Generation batches must not exceed the training batch, or TRL
        # ends up padding partial groups.
        generation_batch_size=self.batch_size,
    )
    args.update(self.extra_args)
    return TRLGRPOConfig(**args)

GRPOTrainer

GRPOTrainer(config: GRPOConfig, *, gpus: Optional[int] = None, nodes: int = 1, trainer_callbacks: Optional[List[Any]] = None, name: Optional[str] = None)

Bases: TrainTask

GRPO trainer task in the ROME framework.

Parameters:

  • config (GRPOConfig) –

    Training settings, including the ModelConfig to train.

  • gpus (int, default: None ) –

    Resources one training round needs. Forwarded to asyncflow by the training manager. Defaults to model_config.required_gpus.

  • nodes (int, default: None ) –

    Resources one training round needs. Forwarded to asyncflow by the training manager. Defaults to model_config.required_gpus.

  • trainer_callbacks (Optional[List[Any]], default: None ) –

    transformers callbacks passed through to TRL — e.g. a callback that checkpoints mid-round so streams can reload before the round ends.

Source code in rome/train/llm.py
def __init__(
    self,
    config: GRPOConfig,
    *,
    gpus: Optional[int] = None,
    nodes: int = 1,
    trainer_callbacks: Optional[List[Any]] = None,
    name: Optional[str] = None,
):
    if config.model_config is None:
        raise ValueError("GRPOConfig.model_config must be set")
    super().__init__(
        gpus=gpus if gpus is not None else config.model_config.required_gpus,
        nodes=nodes,
        name=name or "grpo",
    )
    self.config = config
    self.trainer_callbacks = trainer_callbacks

wants_hf_dataset class-attribute instance-attribute

wants_hf_dataset = True

TRL wants a datasets.Dataset, so the data manager builds one.

train

train(dataset: Any, output_dir: str, **kwargs: Any) -> str

Run one GRPO round and return the checkpoint path.

Runs inside the asyncflow task, so this is the first point where torch, transformers, TRL and peft are actually imported — on the node that has the GPUs.

Source code in rome/train/llm.py
def train(self, dataset: Any, output_dir: str, **kwargs: Any) -> str:
    """Run one GRPO round and return the checkpoint path.

    Runs inside the asyncflow task, so this is the first point where torch,
    transformers, TRL and peft are actually imported — on the node that has
    the GPUs.
    """
    from trl import GRPOTrainer as TRLGRPOTrainer

    model_config = self.config.model_config
    model, tokenizer = load_model(model_config)

    trainer = TRLGRPOTrainer(
        model=model,
        processing_class=tokenizer,
        reward_funcs=list(self.config.reward_funcs),
        args=self.config.build_trl_config(output_dir),
        train_dataset=dataset,
        callbacks=self.trainer_callbacks,
        **({"rollout_func": self.config.rollout_func}
           if self.config.rollout_func is not None else {}),
    )
    trainer.train()
    return save_model(model, model_config, output_dir)

as_command

as_command(dataset: Any, output_dir: str, **kwargs: Any) -> Optional[Tuple[str, str]]

Optionally run this round as an executable task instead of a function.

Return (shell_command, checkpoint_path) to have the training manager submit shell_command as an executable task — a subprocess on the allocation's resources, the way IMPRESS submits its wrapper scripts — rather than pickling train into a worker. checkpoint_path is where the command will write the checkpoint, since an executable task's return value is not the path.

Return None (the default) to run train in-process as a function task. A GPU fine-tune should prefer the command form: the subprocess exits when the round ends, releasing its VRAM.

Completion contract: the command MUST write a file named train_complete (rome.trainer.TRAIN_COMPLETE_MARKER) into its output_dir as its final action, after the checkpoint is safely on disk. The training manager polls for that marker to detect the round finished, because on Dragon a task can run to completion and never resolve its future (a running service blocks result delivery — see docs/dragon.md). The marker, not the checkpoint, is the signal: with publish-in-place the checkpoint path already exists from the prior round.

Source code in rome/train/base.py
def as_command(self, dataset: Any, output_dir: str,
               **kwargs: Any) -> Optional[Tuple[str, str]]:
    """Optionally run this round as an executable task instead of a function.

    Return ``(shell_command, checkpoint_path)`` to have the training manager
    submit ``shell_command`` as an *executable* task — a subprocess on the
    allocation's resources, the way IMPRESS submits its wrapper scripts —
    rather than pickling :meth:`train` into a worker. ``checkpoint_path`` is
    where the command will write the checkpoint, since an executable task's
    return value is not the path.

    Return ``None`` (the default) to run :meth:`train` in-process as a
    function task. A GPU fine-tune should prefer the command form: the
    subprocess exits when the round ends, releasing its VRAM.

    Completion contract: the command MUST write a file named
    ``train_complete`` (``rome.trainer.TRAIN_COMPLETE_MARKER``) into its
    ``output_dir`` as its *final* action, after the checkpoint is safely on
    disk. The training manager polls for that marker to detect the round
    finished, because on Dragon a task can run to completion and never
    resolve its future (a running service blocks result delivery — see
    ``docs/dragon.md``). The marker, not the checkpoint, is the signal: with
    publish-in-place the checkpoint path already exists from the prior round.
    """
    return None

prepare_output_dir staticmethod

prepare_output_dir(base_dir: str, version: int, name: str) -> str

Standard checkpoint layout: <base_dir>/<name>/v<version>.

Source code in rome/train/base.py
@staticmethod
def prepare_output_dir(base_dir: str, version: int, name: str) -> str:
    """Standard checkpoint layout: ``<base_dir>/<name>/v<version>``."""
    path = os.path.join(base_dir, name, f"v{version}")
    os.makedirs(path, exist_ok=True)
    return path

SFTConfig dataclass

SFTConfig(model_config: Optional[ModelConfig] = None, learning_rate: float = 2e-05, batch_size: int = 4, num_epochs: int = 3, gradient_accumulation_steps: int = 8, prompt_column: str = 'prompt', completion_column: str = 'completion', text_column: str = 'text', trl_config: Any = None, extra_args: Dict[str, Any] = dict())

Configuration for supervised fine-tuning in the ROME framework.

SFT trains the model to imitate chosen (prompt, completion) pairs. In the self-improvement loop it is the "train on the good ones" half: an inference stream generates candidates, a reward stream scores them, the data manager keeps the best/correct ones, and this fine-tunes on those — rejection-sampling / STaR-style, with no preference or RL objective. The reward that selects the data lives in the data manager (a filter/sampler) or a reward stream, not here; SFT only sees the winners.

Parameters:

  • model_config (ModelConfig, default: None ) –

    What to train. Required.

  • learning_rate (float, default: 2e-05 ) –

    The usual SFT knobs.

  • batch_size (float, default: 2e-05 ) –

    The usual SFT knobs.

  • num_epochs (float, default: 2e-05 ) –

    The usual SFT knobs.

  • gradient_accumulation_steps (float, default: 2e-05 ) –

    The usual SFT knobs.

  • prompt_column (str, default: 'prompt' ) –

    Corpus fields holding the instruction and the response to imitate.

  • completion_column (str, default: 'prompt' ) –

    Corpus fields holding the instruction and the response to imitate.

  • text_column (str, default: 'text' ) –

    Field holding a fully-formatted training string. When a record lacks it, the trainer builds it from prompt+completion via the tokenizer's chat template; when present, it is used as-is.

  • trl_config (Optional[SFTConfig], default: None ) –

    Fully-specified TRL config. Overrides the individual knobs above.

  • extra_args (dict, default: dict() ) –

    Additional keyword arguments merged into the constructed TRL config — e.g. max_seq_length / max_length, whose name varies by TRL version, so it is left out of the defaults on purpose.

build_trl_config

build_trl_config(output_dir: str) -> Any

Materialize a trl.SFTConfig (imported here so the manager never pays for TRL).

Source code in rome/train/llm.py
def build_trl_config(self, output_dir: str) -> Any:
    """Materialize a ``trl.SFTConfig`` (imported here so the manager never
    pays for TRL)."""
    if self.trl_config is not None:
        return self.trl_config
    from trl import SFTConfig as TRLSFTConfig

    args = dict(
        output_dir=output_dir,
        learning_rate=self.learning_rate,
        warmup_ratio=0.1,
        lr_scheduler_type="cosine",
        logging_steps=1,
        num_train_epochs=self.num_epochs,
        per_device_train_batch_size=self.batch_size,
        gradient_accumulation_steps=self.gradient_accumulation_steps,
    )
    args.update(self.extra_args)
    return TRLSFTConfig(**args)

SFTTrainer

SFTTrainer(config: SFTConfig, *, gpus: Optional[int] = None, nodes: int = 1, trainer_callbacks: Optional[List[Any]] = None, name: Optional[str] = None)

Bases: TrainTask

SFT trainer task in the ROME framework — the counterpart to GRPO.

Parameters mirror GRPOTrainer: a SFTConfig (with the ModelConfig to train), resources forwarded to asyncflow, and optional transformers callbacks.

Source code in rome/train/llm.py
def __init__(
    self,
    config: SFTConfig,
    *,
    gpus: Optional[int] = None,
    nodes: int = 1,
    trainer_callbacks: Optional[List[Any]] = None,
    name: Optional[str] = None,
):
    if config.model_config is None:
        raise ValueError("SFTConfig.model_config must be set")
    super().__init__(
        gpus=gpus if gpus is not None else config.model_config.required_gpus,
        nodes=nodes,
        name=name or "sft",
    )
    self.config = config
    self.trainer_callbacks = trainer_callbacks

train

train(dataset: Any, output_dir: str, **kwargs: Any) -> str

Run one SFT round on the chosen pairs and return the checkpoint path.

Like GRPO, torch/transformers/TRL/peft are imported here — on the node that has the GPUs, never in the manager process.

Source code in rome/train/llm.py
def train(self, dataset: Any, output_dir: str, **kwargs: Any) -> str:
    """Run one SFT round on the chosen pairs and return the checkpoint path.

    Like GRPO, torch/transformers/TRL/peft are imported here — on the node
    that has the GPUs, never in the manager process.
    """
    from trl import SFTTrainer as TRLSFTTrainer

    model_config = self.config.model_config
    model, tokenizer = load_model(model_config)
    dataset = self._ensure_text(dataset, tokenizer)

    trainer = TRLSFTTrainer(
        model=model,
        processing_class=tokenizer,
        args=self.config.build_trl_config(output_dir),
        train_dataset=dataset,
        callbacks=self.trainer_callbacks,
    )
    trainer.train()
    return save_model(model, model_config, output_dir)

as_command

as_command(dataset: Any, output_dir: str, **kwargs: Any) -> Optional[Tuple[str, str]]

Optionally run this round as an executable task instead of a function.

Return (shell_command, checkpoint_path) to have the training manager submit shell_command as an executable task — a subprocess on the allocation's resources, the way IMPRESS submits its wrapper scripts — rather than pickling train into a worker. checkpoint_path is where the command will write the checkpoint, since an executable task's return value is not the path.

Return None (the default) to run train in-process as a function task. A GPU fine-tune should prefer the command form: the subprocess exits when the round ends, releasing its VRAM.

Completion contract: the command MUST write a file named train_complete (rome.trainer.TRAIN_COMPLETE_MARKER) into its output_dir as its final action, after the checkpoint is safely on disk. The training manager polls for that marker to detect the round finished, because on Dragon a task can run to completion and never resolve its future (a running service blocks result delivery — see docs/dragon.md). The marker, not the checkpoint, is the signal: with publish-in-place the checkpoint path already exists from the prior round.

Source code in rome/train/base.py
def as_command(self, dataset: Any, output_dir: str,
               **kwargs: Any) -> Optional[Tuple[str, str]]:
    """Optionally run this round as an executable task instead of a function.

    Return ``(shell_command, checkpoint_path)`` to have the training manager
    submit ``shell_command`` as an *executable* task — a subprocess on the
    allocation's resources, the way IMPRESS submits its wrapper scripts —
    rather than pickling :meth:`train` into a worker. ``checkpoint_path`` is
    where the command will write the checkpoint, since an executable task's
    return value is not the path.

    Return ``None`` (the default) to run :meth:`train` in-process as a
    function task. A GPU fine-tune should prefer the command form: the
    subprocess exits when the round ends, releasing its VRAM.

    Completion contract: the command MUST write a file named
    ``train_complete`` (``rome.trainer.TRAIN_COMPLETE_MARKER``) into its
    ``output_dir`` as its *final* action, after the checkpoint is safely on
    disk. The training manager polls for that marker to detect the round
    finished, because on Dragon a task can run to completion and never
    resolve its future (a running service blocks result delivery — see
    ``docs/dragon.md``). The marker, not the checkpoint, is the signal: with
    publish-in-place the checkpoint path already exists from the prior round.
    """
    return None

prepare_output_dir staticmethod

prepare_output_dir(base_dir: str, version: int, name: str) -> str

Standard checkpoint layout: <base_dir>/<name>/v<version>.

Source code in rome/train/base.py
@staticmethod
def prepare_output_dir(base_dir: str, version: int, name: str) -> str:
    """Standard checkpoint layout: ``<base_dir>/<name>/v<version>``."""
    path = os.path.join(base_dir, name, f"v{version}")
    os.makedirs(path, exist_ok=True)
    return path

load_model

load_model(model_config: ModelConfig)

Load model and tokenizer according to the provided model configuration.

Also usable from an inference stream's load_func, which is the point: the stream and the trainer agree on what a checkpoint means because they load it the same way.

Source code in rome/train/llm.py
def load_model(model_config: ModelConfig):
    """Load model and tokenizer according to the provided model configuration.

    Also usable from an inference stream's ``load_func``, which is the point:
    the stream and the trainer agree on what a checkpoint means because they
    load it the same way.
    """
    from transformers import AutoModelForCausalLM, AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(
        model_config.tokenizer_name(), padding_side="left"
    )
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    model = AutoModelForCausalLM.from_pretrained(
        model_config.resolved_model_name(),
        torch_dtype=model_config.dtype,
        device_map=model_config.device_map,
    )

    if model_config.lora_name is not None:
        from peft import LoraConfig, PeftModel, get_peft_model

        if os.path.exists(model_config.lora_name):
            model = PeftModel.from_pretrained(
                model, model_config.lora_name, is_trainable=True
            )
        else:
            lora_config = model_config.lora_config or LoraConfig(
                r=128,
                lora_alpha=128,
                lora_dropout=0.0,
                inference_mode=False,
                task_type="CAUSAL_LM",
            )
            model = get_peft_model(model, lora_config)
    return model, tokenizer

save_model

save_model(model, model_config: ModelConfig, output_dir: str) -> str

Persist weights for cross-task reload; returns the directory written to.

When a LoRA adapter is configured only the adapter is saved — that is the whole reason ROME can hot-swap weights mid-campaign without stalling inference on a multi-gigabyte read.

Source code in rome/train/llm.py
def save_model(model, model_config: ModelConfig, output_dir: str) -> str:
    """Persist weights for cross-task reload; returns the directory written to.

    When a LoRA adapter is configured only the adapter is saved — that is the
    whole reason ROME can hot-swap weights mid-campaign without stalling
    inference on a multi-gigabyte read.
    """
    os.makedirs(output_dir, exist_ok=True)
    model.save_pretrained(output_dir)
    if model_config.lora_name is not None:
        # Keep the configured adapter location current too, so a stream that
        # was pointed at lora_name rather than at the published checkpoint
        # still sees the new weights.
        model.save_pretrained(model_config.lora_name)
    return output_dir