nemo_automodel.components.speculative.regenerate_vlm
nemo_automodel.components.speculative.regenerate_vlm
Regenerate image-conversation answers with a vision-language target model.
ViSpec’s stage-2 corpus is built by throwing away the original (short) captions of an image-caption dataset and having the target VLM itself answer each prompt, with a length instruction appended so the answers are long enough to train a draft on. Two properties matter:
- On-policy. The draft is supervised on the target’s own output distribution, so the training text has to come from the target.
- Long assistant turns. Public multimodal SFT corpora answer in a sentence or two; a draft trained on those never sees the long-form decoding it has to accelerate. ViSpec appends “Please answer with at least 1000 words.” to every prompt to force long generations.
The output is ShareGPT-style JSONL plus a meta JSON, which
nemo_automodel.components.datasets.vlm.datasets.make_meta_dataset reads
directly, so the ViSpec recipe consumes it without a bespoke dataset class.
Example::
uv run python -m nemo_automodel.components.speculative.regenerate_vlm
—model Qwen/Qwen2.5-VL-7B-Instruct
—dataset liuhaotian/LLaVA-Pretrain
—image-root /data/LLaVA-Pretrain
—output-dir /data/vispec_stage2
—end 68000
Module Contents
Classes
Functions
Data
API
Settings for one regeneration run.
Fail if the serialized row does not rebuild the exact generation prompt.
The answer is only a valid supervision target for the prompt it was sampled
under, so the prompt stage 2 reconstructs has to be the prompt this script
generated from. That reconstruction happens in
:func:convert_sharegpt_to_conversation, which is what
make_meta_dataset runs over data.jsonl, so the check calls the real
parser rather than a copy of its rules: a change to either side that breaks
the correspondence surfaces here instead of silently training the draft on a
prompt the target never answered.
Parameters:
The row about to be written to data.jsonl.
The user content that was handed to the target.
The media_dir recorded in meta.json.
Raises:
ValueError: If the rebuilt user turn differs fromexpected_content.
Build the command-line parser.
Build the user-turn content parts, in the order the target sees them.
The order (source text, image, length instruction) matches the reference
implementation’s preprocess_function.
Parameters:
The source row’s human turn, image placeholder stripped.
Path to the image for this row.
Sentence appended after the image, or empty.
Returns: list[dict]
The content list of a single multimodal user turn.
Pull the first human turn and image path out of a ShareGPT-style row.
Parameters:
One source row, with a conversations list and an image path.
Returns: tuple[str, str] | None
(prompt_text, image_path), or None when the row has no usable
Run the target once and decode its answer.
Parameters:
The loaded target VLM.
The target’s processor.
Chat messages wrapping :func:_build_user_content.
The active regeneration settings.
Returns: str
The decoded assistant answer, without the prompt prefix.
Load a source corpus from either a dataset ID or a local JSON file.
Parameters:
HuggingFace dataset identifier or local JSON/JSONL file.
Dataset split name passed to datasets.load_dataset.
Returns:
The loaded HuggingFace dataset split.
Load the target VLM and place it on the available local device.
device_map="auto" requires the optional accelerate package. The
regeneration utility only needs one local target replica, so explicit
placement works in both minimal and Accelerate-enabled environments.
Parameters:
HuggingFace model identifier or local checkpoint directory.
Returns: torch.nn.Module
The evaluated target model on CUDA when available, otherwise CPU.
Serialize the generation prompt back into one ShareGPT value string.
make_meta_dataset rebuilds the user turn by splitting this string on the
<image> placeholder positionally, so writing the parts in the order
they were generated is what makes the training prefix reproduce the
generation prefix. :func:_assert_prompt_round_trip verifies that per row.
Parameters:
The source row’s human turn, image placeholder stripped.
Sentence appended after the image, or empty.
Returns: str
The value written to the human turn of data.jsonl.
Write the meta JSON that make_meta_dataset reads.
Parameters:
Directory holding the generated data.jsonl.
Directory the JSONL’s relative image paths resolve against.
Returns: Path
Path to the written meta file.
CLI entry point. Parses argv and returns the process exit code.
Regenerate answers for a slice of the source corpus and write the shard.
Parameters:
The regeneration settings.
Returns: Path
Path to the written data.jsonl.