news 2026/4/18 9:54:42

【Qwen】train()函数说明

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【Qwen】train()函数说明

train()函数文档

train(attn_implementation='flash_attention_2')

Runs the main training loop for Qwen VL (Qwen2-VL, Qwen2.5-VL, Qwen3-VL, or Qwen3-VL-MoE) instruction tuning.
Parses command-line arguments for model, data, and training config; loads the appropriate model class and processor; optionally applies LoRA or configures which modules to tune (vision encoder, MLP merger, LLM); builds the supervised data module and Hugging FaceTrainer, runs training (with optional resume), then saves the final model and processor tooutput_dir.

Parameters

NameTypeDefaultDescription
attn_implementationstr"flash_attention_2"Attention implementation passed to the model (e.g."flash_attention_2"for Flash Attention 2).

Command-line arguments (parsed viaHfArgumentParser)

  • ModelArguments

    • model_name_or_path(str) – HuggingFace model id or path (e.g.Qwen/Qwen2.5-VL-3B-Instruct,Qwen/Qwen3-VL-8B-Instruct). Used to select model class (Qwen2-VL, Qwen2.5-VL, Qwen3-VL, or Qwen3-VL-MoE).
    • tune_mm_llm(bool) – Whether to train the language model (andlm_head).
    • tune_mm_mlp(bool) – Whether to train the vision merger (MLP).
    • tune_mm_vision(bool) – Whether to train the vision encoder.

  • DataArguments

    • dataset_use(str) – Comma-separated dataset names (with optional%Nsampling, e.g.dataset1%50).
    • data_flatten(bool) – Whether to flatten/concat batch sequences.
    • data_packing(bool) – Whether to use packed data (requires preprocessing withpack_data.py).
    • max_pixels(int) – Max image pixels (default28*28*576).
    • min_pixels(int) – Min image pixels (default28*28*16).
    • video_max_frames,video_min_frames,video_max_pixels,video_min_pixels,video_fps– Video sampling and resolution settings.
  • TrainingArguments(extendstransformers.TrainingArguments)

    • cache_dir(str, optional) – Cache directory for model/processor.
    • model_max_length(int) – Maximum sequence length for tokenizer.
    • lora_enable(bool) – IfTrue, apply LoRA and ignoretune_mm_*for the base model.
    • lora_r,lora_alpha,lora_dropout– LoRA rank, alpha, and dropout.
    • mm_projector_lr,vision_tower_lr– Optional learning rates for projector and vision tower.
    • Plus standard Trainer args:output_dir,bf16,per_device_train_batch_size,gradient_accumulation_steps,learning_rate,num_train_epochs,save_steps,gradient_checkpointing,deepspeed, etc.

Returns

None. Model and processor are saved undertraining_args.output_dir.

Notes

  • Ifoutput_diralready containscheckpoint-*directories, training is resumed withresume_from_checkpoint=True.
  • Whendata_flattenordata_packingis enabled, the Qwen2 VL attention class is replaced for compatibility.
  • Qwen3-VL MoE models useQwen3VLMoeForConditionalGeneration; other Qwen3-VL models useQwen3VLForConditionalGeneration; Qwen2.5-VL and Qwen2-VL use the corresponding classes inferred frommodel_name_or_path.

Example

# Typical usage: arguments are passed via command line (e.g. from scripts/sft_qwen3_4b.sh)torchrun --nproc_per_node=4qwenvl/train/train_qwen.py\--model_name_or_path Qwen/Qwen3-VL-8B-Instruct\--dataset_use my_dataset\--data_flatten True\--tune_mm_vision False --tune_mm_mlp True --tune_mm_llm True\--output_dir ./output\--bf16 --per_device_train_batch_size4--gradient_accumulation_steps4\--learning_rate 1e-5 --num_train_epochs0.5
# Programmatic call (still requires sys.argv or explicit parse for HfArgumentParser)fromqwenvl.train.train_qwenimporttrain train(attn_implementation="flash_attention_2")
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 4:53:27

10大AIGC工具实测:免费版vs付费版功能差异

�� 10大降AIGC平台核心对比速览 排名 工具名称 降AIGC效率 适用场景 免费/付费 1 askpaper ⭐⭐⭐⭐⭐ 学术论文精准降AI 付费 2 秒篇 ⭐⭐⭐⭐⭐ 快速降AIGC降重 付费 3 Aibiye ⭐⭐⭐⭐ 多学科论文降AI 付费 4 Aicheck ⭐⭐⭐⭐…

作者头像 李华
网站建设 2026/4/16 2:21:43

单北斗变形监测一体机在基础设施安全与地质灾害监测中的应用价值分析

单北斗变形监测一体机在基础设施安全与地质灾害监测中发挥着关键作用。这种一体机结合了高精度定位和抗干扰技术,使其能够在各种环境条件下稳定运行。通过实时监测微小形变,相关部门能够迅速掌握基础设施的健康状态,从而及时应对潜在风险。同…

作者头像 李华
网站建设 2026/3/27 14:54:59

亲测好用!千笔AI,冠绝行业的一键生成论文工具

你是否曾为论文选题而烦恼?是否在深夜面对空白文档无从下笔?是否反复修改却仍对内容不满意?MBA学子的论文写作之路,往往伴随着选题难、框架乱、查重高、格式错等重重困境。而如今,一款专为学术写作打造的AI工具——千笔…

作者头像 李华
网站建设 2026/4/15 5:37:32

宏智树AI封神!新手写课程论文,不用熬大夜也能稳拿高分

作为深耕论文写作科普的教育博主,每天都能收到无数宝子的求助:“博主,课程论文怎么写啊?”“熬了3天,还是只写了标题”“怕查重超标,又怕内容空洞,老师给低分”…… 其实大家都陷入了一个误区&…

作者头像 李华
网站建设 2026/4/15 17:17:59

实测才敢推!自考论文神器 —— 千笔·专业论文写作工具

你是否曾为论文选题发愁,绞尽脑汁却无从下手?是否在深夜面对空白文档,文思枯竭、无从下笔?又是否反复修改仍不满意,查重率居高不下?自考论文写作的每一步都充满挑战,而这些难题,或许…

作者头像 李华
网站建设 2026/4/18 8:51:00

C语言对话-31.与大虾对话 领悟设计模式

myan(孟岩) 翻译 [译者按] 本文根据发表在CUJ Expert Forum上的两篇文章编译而成。C/C Users Journal是目前最出色的C/C语言专业杂志,特别是在C Report闭刊之后,CUJ的地位更加突出。CUJ Expert Forum是CUJ主办的网上技术专栏,汇集2000年10月以…

作者头像 李华