ms-swift训练的感悟2

ms-swift官方中文文档
https://swift.readthedocs.io/zh-cn/latest/BestPractices/Reranker.html
原文

默认会从每条数据中取出MAX_POSITIVE_SAMPLES条正样本和MAX_NEGATIVE_SAMPLES条负样本,每条正样本会和MAX_NEGATIVE_SAMPLES条负样本组成一个group,因此每条数据会扩展成MAX_POSITIVE_SAMPLESx(1 + MAX_NEGATIVE_SAMPLES)条数据。 如果数据中正例/负例数量不足,会取全部正例/负例,如果数据中正例和负例数量超过MAX_POSITIVE_SAMPLES和MAX_NEGATIVE_SAMPLES,会进行随机采样。 IMPORTANT:展开后的数据会放在同一个batch中,因此每个设备上的实际批处理大小(effective batch size)将是 per_device_train_batch_size × MAX_POSITIVE_SAMPLES × (1 + MAX_NEGATIVE_SAMPLES)。请注意调整 per_device_train_batch_size 以避免显存不足。

MAX_POSITIVE_SAMPLESx(1 + MAX_NEGATIVE_SAMPLES) 为什么这里是1+?而不是MAX_POSITIVE_SAMPLESx(1 + MAX_NEGATIVE_SAMPLES) ?

这与他的训练范式有关,他本来就是point2point的,只是损失函数有区别
什么是point2point?直接给prompt

比如

你是一名优秀的数据专家,请从refer_doc中选择与用户query最相关的数据
<query>
迪迦奥特曼是哪一年播出的
</query>
<refer_doc>
a.迪迦奥特曼是1996年在日本首映的
b.迪迦奥特曼的人间体是大古
c.盖亚奥特曼是大地毁灭者
</refer_doc>
你是一名优秀的数据专家,请从refer_doc中选择与用户query最相关的数据
<query>
迪迦奥特曼是哪一年播出的
</query>
<refer_doc>
a.迪迦奥特曼是1996年在日本首映的
</refer_doc>


你是一名优秀的数据专家,请从refer_doc中选择与用户query最相关的数据
<query>
迪迦奥特曼是哪一年播出的
</query>
<refer_doc>
a.迪迦奥特曼的人间体是大古
</refer_doc>


你是一名优秀的数据专家,请从refer_doc中选择与用户query最相关的数据
<query>
迪迦奥特曼是哪一年播出的
</query>
<refer_doc>
a.盖亚奥特曼是大地毁灭者
</refer_doc>

query Official Chinese Documentation of ms-swift
https://swift.readthedocs.io/zh-cn/latest/BestPractices/Reranker.html
Original Text

By default, MAX_POSITIVE_SAMPLES positive samples and MAX_NEGATIVE_SAMPLES negative samples will be taken from each data point. Each positive sample will be paired with MAX_NEGATIVE_SAMPLES negative samples to form a group. Therefore, each data point will be expanded into MAX_POSITIVE_SAMPLES x (1 + MAX_NEGATIVE_SAMPLES) data points. If the number of positive/negative examples in the data is insufficient, all positive/negative examples will be taken. If the number of positive and negative examples exceeds MAX_POSITIVE_SAMPLES and MAX_NEGATIVE_SAMPLES, random sampling will be performed. IMPORTANT: The expanded data will be placed in the same batch, so the actual batch size on each device (effective batch size) will be per_device_train_batch_size × MAX_POSITIVE_SAMPLES × (1 + MAX_NEGATIVE_SAMPLES). Please adjust per_device_train_batch_size to avoid running out of GPU memory.

Why is it 1+ here instead of MAX_POSITIVE_SAMPLESx(1 + MAX_NEGATIVE_SAMPLES) ?

This is related to his training paradigm. He is originally point2point, just with a different loss function.
What is point2point? Just give the prompt.

For example

You are an excellent data expert, please select the data most relevant to the user's query from refer_doc
<query>
When was Ultraman Tiga broadcasted?
</query>
<refer_doc>
a. Ultraman Tiga premiered in Japan in 1996
b. Ultraman Tiga's human form is Takeru
c. Gaia Ultraman is the Earth Destroyer
</refer_doc>

point2point

You are an excellent data expert, please select the most relevant data from refer_doc for the user's query
<query>
When was Ultraman Tiga broadcasted
</query>
<refer_doc>
a. Ultraman Tiga premiered in Japan in 1996
</refer_doc>


You are an excellent data expert, please select the data from refer_doc that is most relevant to the user's query
<query>
When was Ultraman Tiga aired
</query>
<refer_doc>
a. Tiga Ultraman's human form is Gao
</refer_doc>


You are an excellent data expert, please select the most relevant data from refer_doc to the user's query
<query>
When was Ultraman Tiga aired?
</query>
<refer_doc>
a. Ultraman Gaia is the Earth Destroyer
</refer_doc>

MAX_POSITIVE_SAMPLES x (1 + MAX_NEGATIVE_chouchSAMPLES)

为什么是1 + MAX_NEGATIVE_SAMPLES?

例子

MAX_POSITIVE_SAMPLES = 1
MAX_NEGATIVE_chouchSAMPLES = 2
{
query: a
pos:[A]
neg:[B,C]
}

a-A a-B a-C
1x(2+1) =3 //待入官方公式,对上了

因为他最后都是point2point的,
https://huggingface.co/Qwen/Qwen3-Reranker-0.6B
可以看出来输入一个query和多个docs 请求reranker的时候
还是通过推理 query-doc 来解决的

至于listwise ,也只是拿到point2point的loss后再编排而已,怎么编排让point2point的效果更好

### 将ms-swift训练后的模型转换为vllm支持的格式 将ms-swift训练完成的模型导出为vllm可运行的格式,需要确保模型满足vllm框架对模型的支持要求[^2]。以下是具体实现方法和注意事项: #### 1. 模型兼容性检查 首先确认ms-swift训练的模型是否在vllm支持的模型列表中[^2]。如果模型未被直接支持,则可能需要进行一些额外的适配工作,例如调整模型结构或权重格式。 #### 2. 导出模型权重 ms-swift训练完成后,模型权重通常会保存在指定的输出目录(如`output`)中[^2]。确保该目录包含以下文件: - 模型权重文件(通常是`.bin`或`.pt`格式) - 配置文件(如`config.json`) - 分词器文件(如`tokenizer_config.json`) 这些文件是vllm加载模型所必需的。 #### 3. 转换模型格式 vllm支持特定的模型权重格式,因此可能需要对ms-swift生成的权重进行转换。可以使用以下步骤完成转换: ```python from transformers import AutoModelForCausalLM, AutoTokenizer # 加载ms-swift训练后的模型 model_path = "path/to/ms-swift/output" model = AutoModelForCausalLM.from_pretrained(model_path) tokenizer = AutoTokenizer.from_pretrained(model_path) # 保存为vllm兼容的格式 save_path = "path/to/vllm/compatible/model" model.save_pretrained(save_path) tokenizer.save_pretrained(save_path) ``` 此代码片段确保模型以vllm兼容的方式保存[^2]。 #### 4. 验证模型兼容性 完成转换后,可以通过vllm提供的工具验证模型是否能够正确加载。例如,在新的终端中启动vllm服务并测试模型加载情况: ```bash FASTCHAT_USE_MODELSCOPE=true python3 -m fastchat.serve.model_worker --model-path path/to/vllm/compatible/model --revision v1.0.0 ``` 如果模型成功加载,则说明转换过程无误[^3]。 #### 5. 注意事项 - 确保ms-swift训练时使用的模型架构与vllm支持的架构一致。 - 如果模型包含LoRA模块,则需要在导出过程中合并LoRA权重。 - 在转换过程中,建议保留原始模型的配置文件和分词器文件,以避免兼容性问题。 ```python from peft import PeftModel # 合并LoRA权重 base_model_path = "path/to/base/model" lora_model_path = "path/to/lora/model" merged_model_path = "path/to/merged/model" model = AutoModelForCausalLM.from_pretrained(base_model_path) lora_model = PeftModel.from_pretrained(model, lora_model_path) merged_model = lora_model.merge_and_unload() # 保存合并后的模型 merged_model.save_pretrained(merged_model_path) ``` 此代码片段适用于处理包含LoRA模块的模型[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值