news 2026/6/6 0:34:12

【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/5/22 17:00:08

论文写作智能化:6款AI工具提升效率与成果

学术写作领域涌现出多款集成人工智能技术的辅助工具,它们基于先进的自然语言处理算法实现论文结构智能生成、文本质量优化及查重检测等核心功能,尤其适用于学位论文撰写与学术报告整理场景。需要强调的是,这些智能化平台应当定位为研究效率的…

作者头像 李华
网站建设 2026/5/30 13:11:51

php python+vue在线课表的设计与实现

目录在线课表系统设计与实现概述后端设计(PHP/Python)前端实现(Vue.js)功能扩展技术栈选择建议部署与优化项目技术支持可定制开发之功能亮点源码获取详细视频演示 :文章底部获取博主联系方式!同行可合作在线…

作者头像 李华
网站建设 2026/5/29 17:00:26

论文写作新范式:6款AI工具助力高效产出

学术写作领域涌现出多款集成人工智能技术的辅助工具,它们基于先进的自然语言处理算法实现论文结构智能生成、文本质量优化及查重检测等核心功能,尤其适用于学位论文撰写与学术报告整理场景。需要强调的是,这些智能化平台应当定位为研究效率的…

作者头像 李华
网站建设 2026/6/3 20:15:51

永磁同步电机ADRC实战:手把手拆解Simulink骚操作

永磁同步电机一阶非线性自抗扰(ADRC)matlab,simulink模型。 参数已调好含有参考文档,送自抗扰相关电子书 不 !电机控制示意图(假装这里有图) 永磁同步电机一阶非线性自抗扰(ADRC)ma…

作者头像 李华
网站建设 2026/5/30 10:23:54

这份榜单够用!10个AI论文网站深度测评,自考毕业论文写作必备

在当前学术研究与论文写作日益依赖AI技术的背景下,自考学生面临着选题困难、资料查找繁琐、格式不规范等多重挑战。为了帮助广大学子高效完成毕业论文,我们基于2026年的最新实测数据与用户反馈,精心打造了这份“10个AI论文网站深度测评”榜单…

作者头像 李华