Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_pretrain.py

### 2. 精调

PaddleNLP 支持多个主流大模型的 SFT、LoRA、Prefix Tuning 等精调策略,提供统一、高效精调方案:
PaddleNLP 支持多个主流大模型的 SFT、PEFT 等精调策略,提供统一、高效精调方案:

- **统一训练入口**。飞桨大模型套件精调方案可适配业界主流大模型,用户只需修改配置文件,即能在单卡或多卡(支持4D 并行分布式策略)进行多种大模型精调。
- **高效数据和分布式策略**。Zero Padding 零填充优化策略结合 FlashMask 策略有效提升模型训练效率。独创 PEFT 结合低比特和分布式并行策略,大幅降低大模型精调硬件门槛,支持单卡(A100 80G)百亿模型微调、单机(A100 80G * 8)千亿模型微调。
Expand Down Expand Up @@ -150,7 +150,7 @@ python run_finetune.py ./config/llama/lora_argument.json
python run_finetune.py ./config/llama/pt_argument.json
```

更多大模型精调分布式使用文档、训练细节和效果请参见[大模型精调教程](./docs/finetune.md)。
除了 LoRA、Prefix Tuning 外,还支持 LoKr、VeRA、MoRA、ReFT、rsLoRA、LoRA+、PiSSA、MoSLoRA 等多种精调算法,更多大模型精调使用文档、训练细节和效果请参见[大模型精调教程](./docs/finetune.md)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoRA-GA加上


### 3. 对齐

Expand Down
115 changes: 107 additions & 8 deletions llm/docs/finetune.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- 易用并行策略:支持纯数据并行(Data Parallelism)、分组参数切片的数据并行(Sharding Parallelism)、张量模型并行(Tensor Parallelism)、流水线模型并行(Pipeline Parallelism)、序列并行(Sequence parallelism)。
- 多种精度训练:16/32bit 全量精调、4/8/16bit LoRA 精调、混合量化 LoRA 精调。
- 性能极致优化:FlashAttention-2、FlashMask、Greedy Zero Padding。
- 先进精调策略:LoRA+、PiSSA、rsLoRA、NEFTune、VeRA。
- 先进精调策略:LoRA+、PiSSA、rsLoRA、NEFTune、VeRA、MoRA、ReFT、MoSLoRA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以加上LoRA-GA,代码已经合入

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块我看还没有开源模型适配示例,等 @greycooker 适配后由他加入吧

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,这块我来加一下


更多算法原理细节详见[飞桨大模型常见算法文档](algorithm_overview.md)

Expand Down Expand Up @@ -71,7 +71,8 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py
2. 设置`use_flash_attention`为 True 使用 FlashAttention。在 FlashAttention 打开的基础上设置`flash_mask`为 True 使用 FlashMask。
3. SFT API 支持4D 并行策略,可以通过控制`tensor_parallel_degree`、`pipeline_parallel_degree`、 `sharding`、`sharding_parallel_degree`调整

### 3.4 LoRA/QLoRA
### 3.4 PEFT
#### 3.4.1 LoRA/QLoRA

```
# 单卡LoRA
Expand All @@ -93,13 +94,12 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.
3. 可以通过设置`weight_quantize_algo`将主干模型量化低比特,例如'weight_only_int4','weight_only_int8','nf4'或'fp4'。具体参考精调参数介绍
4. 设置`use_flash_attention`为 True 使用 FlashAttention。在 FlashAttention 打开的基础上设置`flash_mask`为 True 使用 FlashMask。
5. LoRA API 支持4D 并行策略,可以通过控制`tensor_parallel_degree`、`pipeline_parallel_degree`、 `sharding`、`sharding_parallel_degree`调整并行训练策略,可拓展至**单机 LoRA 微调千亿模型**。

### 3.5 LoRA 参数合并
6. 可配置`rslora`、`lora_plus_scale`、`pissa`、`lora_use_mixer`、`use_mora`等参数,使用 rsLoRA、LoRa+、PiSSA、MosLoRA(暂不支持张量模型并行)、MoRA(暂不支持张量模型并行) 等算法。

为了后续的**压缩**和**静态图推理**方便,我们提供 LoRA 参数合并脚本,可以将 LoRA 参数合并到主干模型并保存相应的权重。
```
python merge_lora_params.py \
--model_name_or_path ./checkpoints/sft_ckpts \
--model_name_or_path ./base_model \
--lora_path ./checkpoints/lora_ckpts \
--output_path ./checkpoints/lora_merge \
--device "gpu" \
Expand All @@ -115,6 +115,102 @@ python merge_lora_params.py \
- `safe_serialization`: 是否保存为 safetensor 格式,默认为 True。
</div>

#### 3.4.2 Prefix Tuning
```
# 单卡Prefix Tuning
python run_finetune.py ./config/llama/pt_argument.json

# 多卡Prefix Tuning
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py ./config/llama/pt_argument.json
```

#### 3.4.3 VeRA
```
# 单卡VeRA
python run_finetune.py ./config/llama/vera_argument.json

# 多卡VeRA(暂不支持张量模型并行)
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py ./config/llama/vera_argument.json
```

为了后续的**压缩**和**静态图推理**方便,我们提供 VeRA 参数合并脚本,可以将 VeRA 参数合并到主干模型并保存相应的权重。
```
python merge_vera_params.py \
--model_name_or_path ./base_model \
--vera_path ./checkpoints/vera_ckpts \
--merge_vera_model_path ./checkpoints/vera_merge \
--device "gpu" \
--safe_serialization True
```

<summary>&emsp; 脚本参数介绍</summary><div>

- `vera_path`: VeRA 参数和配置路径,对 VeRA 参数进行初始化,默认为 None。
- `model_name_or_path`: 必须,主干模型参数路径,默认为 None。
- `merge_vera_model_path`: 必须,合并参数后保存路径,默认为 None。
- `device`: 运行环境,默认为 gpu。
</div>

#### 3.4.4 LoKr
```
# 单卡LoKr
python run_finetune.py ./config/llama/lokr_argument.json

# 多卡LoKr(暂不支持张量模型并行)
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py ./config/llama/lokr_argument.json
```
为了后续的**压缩**和**静态图推理**方便,我们提供 LoKr 参数合并脚本,可以将 LoKr 参数合并到主干模型并保存相应的权重。
```
python merge_lokr_params.py \
--model_name_or_path ./base_model \
--lokr_path ./checkpoints/lokr_ckpts \
--merge_lokr_model_path ./checkpoints/lokr_merge \
--device "gpu" \
--safe_serialization True
```

<summary>&emsp; 脚本参数介绍</summary><div>

- `lokr_path`: LoKr 参数和配置路径,对 LoKr 参数进行初始化,默认为 None。
- `model_name_or_path`: 必须,主干模型参数路径,默认为 None。
- `merge_lokr_model_path`: 必须,合并参数后保存路径,默认为 None。
- `device`: 运行环境,默认为 gpu。
</div>

#### 3.4.4 ReFT
```
# 单卡ReFT
python run_finetune.py ./config/llama/reft_argument.json

# 多卡ReFT(暂不支持张量模型并行)
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py ./config/llama/reft_argument.json
```
ReFT 目前仅支持动态图预测,预测脚本如下
```
python ./predict/reft_predictor.py \
--model_name_or_path ./base_model \
--reft_path ./checkpoints/lokr_ckpts \
--output_file output.json \
--batch_size 1 \
--data_file "./data/dev.json"
--max_length 4096
```

<summary>&emsp; 脚本参数介绍</summary><div>

- `reft_path`: ReFT 参数和配置路径,对 ReFT 参数进行初始化。
- `model_name_or_path`: 主干模型参数路径。
- `batch_size`: 批大小。该参数越大,占用显存越高;该参数越小,占用显存越低。
- `data_file`: 待推理 json 文件,默认为 None。样例数据:
```json
{"tgt":"", "src": "写一个300字的小说大纲,内容是李白穿越到现代,最后成为公司文职人员的故事"}
{"tgt":"", "src": "我要采访一位科幻作家,创建一个包含5个问题的列表"}
```
- `output_file`: 保存推理结果文件。
- `src_length`: 模型输入上下文最大 token 长度。
- `max_length`:模型输入(上下文+生成内容)的最大 token 长度。
</div>

## 4.精调参数介绍
<summary>&emsp; 模型参数(ModelArgument) </summary><div>

Expand All @@ -128,15 +224,18 @@ python merge_lora_params.py \
- `lora_plus_scale`: 是否使用 LoRA+,设置 B 与 A 的学习率比例。
- `neftune`: 是否使用[NEFT](https://arxiv.org/abs/2310.05914),进行微调。默认为 False。
- `neftune_noise_alpha`: NEFT alpha 参数,默认为5.0。
- `vera`: 是否开启 VeRA 微调策略,默认为 False。
- `vera`: 是否开启 [VeRA](https://arxiv.org/abs/2310.11454) 微调策略,默认为 False。
- `vera_rank`: VeRA 算法中 rank(秩)的值,默认为8。
- `lokr`: 是否开启 LoKr 微调策略,默认为 False。
- `lokr`: 是否开启 [LoKr](https://arxiv.org/abs/2309.14859) 微调策略,默认为 False。
- `lokr_rank`: LoKr 算法中 rank(秩)的值,默认为8。
- `use_long_sequence_strategies`: 是否使用长序列扩展策略,默认为 False。
- `reft`: 是否开启 [ReFT](https://arxiv.org/abs/2404.03592) 微调策略,默认为 False。
- `use_mora`: 是否开启 [MoRA](https://arxiv.org/abs/2405.12130) 微调策略,默认为 False。
- `lora_use_mixer`: 是否开启 [MosLoRA](https://arxiv.org/abs/2406.11909) 策略,默认为 False。
- `pissa`: 是否开启 [PiSSA](https://arxiv.org/abs/2404.02948) 策略,默认为 False。
- `strategy_type`: 长序列扩展策略的类型,默认为 None。
- `strategy_name`: 长序列扩展策略的具体名称,默认为 None。
- `rope_scaling_factor`: 应用 RoPE 扩展策略时的缩放因子。
- `lora_use_mixer`: 是否开启 MosLoRA 策略。
</div>

<summary>&emsp; 数据参数(DataArgument)</summary><div>
Expand Down
4 changes: 2 additions & 2 deletions llm/predict/reft_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def reft_predict(predictor_args):
tokenizer.pad_token_id = tokenizer.eos_token_id
dev_ds = load_dataset(
"json",
data_files=os.path.join(predictor_args.dataset_name_or_path, "dev.json"),
data_files=predictor_args.data_file,
)[0]
trans_func = partial(
convert_example_for_reft,
Expand Down Expand Up @@ -80,7 +80,7 @@ def get_pred_parser():
parser.add_argument("--reft_path", type=str, help="The reft model path")
parser.add_argument("--output_file", type=str, help="The output file path")
parser.add_argument("--batch_size", type=int, help="The batch size in prediction")
parser.add_argument("--dataset_name_or_path", type=str, help="The dataset name or path")
parser.add_argument("--data_file", type=str, help="The dataset name or path")
parser.add_argument("--max_length", type=int, default=1024, help="The maximum length of input sequences")
parser.add_argument("--src_length", type=int, default=512, help="The source sequence length")
parser.add_argument("--num_beams", type=int, default=4, help="The maximum length of input sequences")
Expand Down
2 changes: 1 addition & 1 deletion tests/llm/test_reft.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_reft(self):
perdict_params = {
"model_name_or_path": reft_config["model_name_or_path"],
"reft_path": self.output_dir,
"dataset_name_or_path": self.data_dir,
"data_file": os.path.join(self.data_dir, "dev.json"),
"batch_size": 8,
}
self.run_reft_predictor(perdict_params)
Expand Down