Skip to content

Commit 31b2010

Browse files
Fix loss scale (#383)
1 parent 9638b56 commit 31b2010

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

docs/source/LLM/命令行参数.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# 命令行参数
2+
23
## 目录
4+
35
- [sft 参数](#sft-参数)
46
- [dpo 参数](#dpo-参数)
57
- [merge-lora infer 参数](#merge-lora-infer-参数)
68
- [app-ui 参数](#app-ui-参数)
79
- [deploy 参数](#deploy-参数)
810

911
## sft 参数
12+
1013
- `--model_type`: 表示你选择的模型类型, 默认是`None`. 如果没有指定`model_id_or_path`, 则抛出异常. 如果指定了`model_id_or_path`, 则会根据`model_id_or_path`以及`MODEL_MAPPING`推断`model_type`. `model_type``model_id_or_path`这两个参数不能同时指定. 可以选择的`model_type`可以查看`MODEL_MAPPING.keys()`.
1114
- `--model_id_or_path`: 表示模型在ModelScope Hub中的`model_id`, 不区分大小写, 默认为`None`. 如果`--model_id_or_path`未被注册, 则会抛出异常. 你可以使用`model_type`的方式指定模型类型, 也可以通过`model_id_or_path`的方式指定模型类型.
1215
- `--model_revision`: 表示模型在ModelScope Hub中对应`model_id`的版本号, 默认为`None`. `model_revision`指定为`None`, 则使用注册在`MODEL_MAPPING`中的revision. 否则强制使用命令行传入的`model_revision`.
@@ -96,7 +99,7 @@
9699
- `--gpu_memory_fraction`: 默认为None. 该参数旨在指定显卡最大可用显存比例的情况下运行训练,用于极限测试.
97100
- `--train_dataset_mix_ratio`: 默认为0. 该参数定义了如何进行数据集打混训练. 指定该参数时, 训练集会以`train_dataset_mix_ratio`倍数混合`train_dataset_mix_ds`指定的通用知识数据集, 使整体数据集长度达到`train_dataset_sample`.
98101
- `--train_dataset_mix_ds`: 默认为`ms-bench`. 用于防止知识遗忘的通用知识数据集.
99-
- `--use_loss_scale`: 默认为True. 生效时会讲Agent的部分字段(Action/Action Input部分)的loss权重加强以强化CoT, 对普通SFT场景没有任何效果.
102+
- `--use_loss_scale`: 默认为False. 生效时会将Agent的部分字段(Action/Action Input部分)的loss权重加强以强化CoT, 对普通SFT场景没有任何效果.
100103

101104
### AdaLoRA微调参数
102105

@@ -130,8 +133,8 @@ dpo参数继承了sft参数, 除此之外增加了以下参数:
130133
- `--loss_type`: DPOloss类型, 支持'sigmoid', 'hinge', 'ipo', 'kto_pair', 默认值'sigmoid'.
131134
- `--sft_beta`: 是否在DPO中加入sft loss, 默认为0.1, 支持[0, 1)区间,最后的loss为(1-sft_beta)*KL_loss + sft_beta * sft_loss.
132135

133-
134136
## merge-lora infer 参数
137+
135138
- `--model_type`: 默认值为`None`, 具体的参数介绍可以在`sft.sh命令行参数`中查看.
136139
- `--model_id_or_path`: 默认值为`None`, 具体的参数介绍可以在`sft.sh命令行参数`中查看. 推荐使用model_type的方式指定.
137140
- `--model_revision`: 默认值为`None`. 具体的参数介绍可以在`sft.sh命令行参数`中查看. 如果`model_id_or_path`为None或者是本地的模型目录, 则该参数失效.
@@ -176,7 +179,6 @@ dpo参数继承了sft参数, 除此之外增加了以下参数:
176179
- `--gpu_memory_utilization`: 初始化vllm引擎`EngineArgs`的参数, 默认为`0.9`. 该参数只有在使用vllm时才生效.
177180
- `--tensor_parallel_size`: 初始化vllm引擎`EngineArgs`的参数, 默认为`1`. 该参数只有在使用vllm时才生效.
178181

179-
180182
## app-ui 参数
181183

182184
app-ui参数继承了infer参数, 除此之外增加了以下参数:

examples/pytorch/llm/scripts/qwen_7b_chat/lora/sft.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ python llm_sft.py \
1111
--dtype AUTO \
1212
--output_dir output \
1313
--dataset ms-agent \
14+
--use_loss_scale true \
1415
--train_dataset_mix_ratio 2.0 \
1516
--train_dataset_sample -1 \
1617
--num_train_epochs 2 \

swift/llm/agent/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def split_agent_parts_by(text: str, delimiters: List[str]):
5757

5858

5959
def calculate_loss_scale(response: str,
60-
use_loss_scale=True) -> Tuple[List[str], List[float]]:
60+
use_loss_scale=False
61+
) -> Tuple[List[str], List[float]]:
6162
"""Calculate the loss scale by splitting the agent response.
6263
6364
This algorithm comes from paper: https://arxiv.org/pdf/2309.00986.pdf
@@ -88,7 +89,6 @@ def calculate_loss_scale(response: str,
8889
'Observation:'
8990
]
9091
agent_parts = split_agent_parts_by(response, agent_keyword)
91-
assert all([c['key'] for c in agent_parts])
9292
weights = []
9393
agent_content = []
9494
for c in agent_parts:

swift/llm/utils/argument.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SftArguments:
7070
train_dataset_mix_ds: List[str] = field(
7171
default_factory=lambda: ['ms-bench'])
7272
val_dataset_sample: Optional[int] = None # -1: all dataset
73-
use_loss_scale: Optional[bool] = True
73+
use_loss_scale: Optional[bool] = False
7474
system: Optional[str] = None
7575
max_length: int = 2048 # -1: no limit
7676
truncation_strategy: Literal['delete', 'truncation_left'] = 'delete'

swift/llm/utils/template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _init_template(self,
177177
self.max_length = max_length
178178
self.truncation_strategy = truncation_strategy
179179
self.model = kwargs.get('model', None)
180-
self.use_loss_scale = kwargs.get('use_loss_scale', True)
180+
self.use_loss_scale = kwargs.get('use_loss_scale', False)
181181
for key in [
182182
'prefix', 'prompt', 'chat_sep', 'suffix', 'prefix_has_system'
183183
]:
@@ -363,8 +363,9 @@ def _encode(
363363
inputs = {
364364
'input_ids': input_ids,
365365
'labels': labels,
366-
'loss_scale': loss_scale
367366
}
367+
if self.use_loss_scale:
368+
inputs['loss_scale'] = loss_scale
368369
return inputs, tokenizer_kwargs
369370

370371
def get_tokenizer_kwargs(self, context: str) -> Dict[str, Any]:

0 commit comments

Comments
 (0)