tools/merge_lora.py should save processor artifacts for VLMs
Problem
tools/merge_lora.py saves the merged model and tokenizer, but it does not save the model's processor. Consequently, a merged VLM directory may not be usable as a standalone Hugging Face model.
For example, HuggingFaceTB/SmolVLM-256M-Instruct includes processor artifacts such as preprocessor_config.json. These are absent from the merged output because the tool only saves the tokenizer.
Reproduction
Create a small synthetic LoRA adapter; no training is required:
import torch
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForMultimodalLM
base_model = "HuggingFaceTB/SmolVLM-256M-Instruct"
model = AutoModelForMultimodalLM.from_pretrained(
base_model,
torch_dtype=torch.float16,
)
model = get_peft_model(
model,
LoraConfig(
r=2,
lora_alpha=4,
target_modules=["q_proj"],
bias="none",
),
)
model.save_pretrained("/tmp/smolvlm-lora")
Merge the adapter:
python tools/merge_lora.py \
--base-model HuggingFaceTB/SmolVLM-256M-Instruct \
--adapter-path /tmp/smolvlm-lora \
--output-dir /tmp/smolvlm-merged \
--model-class AutoModelForMultimodalLM
The model weights and tokenizer are saved, but processor artifacts such as /tmp/smolvlm-merged/preprocessor_config.json are absent. Therefore:
from transformers import AutoProcessor
AutoProcessor.from_pretrained("/tmp/smolvlm-merged")
cannot reconstruct the same processor as the base model.
Expected behavior
When the base model has an AutoProcessor, the merged output should contain the processor artifacts required to load and evaluate it as a standalone model directory.
Text-only models should continue to work as they do today.
Suggested approach
- Best-effort load AutoProcessor from
base_model, using the existingtrust_remote_codesetting. - Save the processor into
output_dir. - Preserve the tool's existing NeMoAutoTokenizer behavior.
- Gracefully fall back to tokenizer-only saving for models without a processor.
- Add a unit test covering processor artifact saving.
The LoRA merge itself works correctly; this issue is limited to packaging the processor sidecar files.
I’d like to work on this. I’ll add best-effort AutoProcessor loading/saving with trust_remote_code forwarded, preserve the existing NeMoAutoTokenizer-first behavior, and add CPU unit coverage for processor success and fallback paths.
@coding-famer @taran-dev4u Thank you both for contributing fixes for this issue! We really appreciate your time and effort.
It looks like #3270 and #3271 address the same problem in very similar ways. To avoid duplicating further work, let's continue with #3270, since it was opened first.
The implementation in #3270 looks good overall. We have one remaining request around unit test coverage; we'll provide the details directly in the PR.
@taran-dev4u, thank you for raising your hand early and preparing a thoughtful solution and validation. Please feel free to share useful test ideas or observations from #3271 on #3270.
We'll continue the review on #3270 and treat #3271 as superseded. Thanks again to both of you for contributing!