训练脚本
PRE_SEQ_LEN=64
LR=2e-2
CUDA_VISIBLE_DEVICES=0 python3 main.py \
--do_train \
--train_file trainVX.json \
--validation_file devVX.json \
--prompt_column content \
--response_column summary \
--overwrite_cache \
--model_name_or_path /home/lyc/workspace/ChatGLM-6B/chatglm-6b \
--output_dir output/dsbtpg-chatglm-6b-pt-$PRE_SEQ_LEN-$LR \
--overwrite_output_dir \
--max_source_length 64 \
--max_target_length 64 \
--per_device_train_batch_size 4 \
--per_device_eval_batch_size 1 \
--gradient_accumulation_steps 16 \
--predict_with_generate \
--max_steps 500 \
--logging_steps 20 \
--save_steps 100 \
--learning_rate $LR \
--pre_seq_len $PRE_SEQ_LEN \
--quantization_bit 8
训练脚本如上例,各参数含义可以参考 [1] ,我们尝试了一些参数变化对于模型的影响,但由于目标任务的难度并不大,所以在实际使用过程中差异并不明显。
训练过程
$ python gen_dataVX.py
Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
Explicitly passing a `revision` is encouraged when loading a configuration with custom code to ensure no malicious code has been contributed in a newer revision.
Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████| 8/8 [00:10<00:00, 1.34s/it]
The dtype of attention mask (torch.int64) is not bool
(/home/lyc/workspace/Conda-env/dsbtpg) root@autodl-container-07ff4facf2-80be90b7:/home/lyc/workspace/ChatGLM-6B/ptuning# bash trainVX.sh
05/21/2024 12:09:10 - WARNING - __main__ - Process rank: -1, device: cuda:0, n_gpu: 1distributed training: False, 16-bits training: False
05/21/2024 12:09:10 - INFO - __main__ - Training/evaluation parameters Seq2SeqTrainingArguments(
_n_gpu=1,
adafactor=False,
adam_beta1=0.9,
adam_beta2=0.999,
adam_epsilon=1e-08,
auto_find_batch_size=False,
bf16=False,
bf16_full_eval=False,
data_seed=None,
dataloader_drop_last=False,
dataloader_num_workers=0,
dataloader_pin_memory=True,
ddp_bucket_cap_mb=None,
ddp_find_unused_parameters=None,
ddp_timeout=1800,
debug=[],
deepspeed=None,
disable_tqdm=False,
do_eval=False,
do_predict=False,
do_train=True,
eval_accumulation_steps=None,
eval_delay=0,
eval_steps=None,
evaluation_strategy=no,
fp16=False,
fp16_backend=auto,
fp16_full_eval=False,
fp16_opt_level=O1,
fsdp=[],
fsdp_config={'fsdp_min_num_params': 0, 'xla': False, 'xla_fsdp_grad_ckpt': False},
fsdp_min_num_params=0,
fsdp_transformer_layer_cls_to_wrap=None,
full_determinism=False,
generation_max_length=None,
generation_num_beams=None,
gradient_accumulation_steps=16,
gradient_checkpointing=False,
greater_is_better=None,
group_by_length=False,
half_precision_backend=auto,
hub_model_id=None,
hub_private_repo=False,
hub_strategy=every_save,
hub_token=<HUB_TOKEN>,
ignore_data_skip=False,
include_inputs_for_metrics=False,
jit_mode_eval=False,
label_names=None,
label_smoothing_factor=0.0,
learning_rate=0.02,
length_column_name=length,
load_best_model_at_end=False,
local_rank=-1,
log_level=passive,
log_level_replica=warning,
log_on_each_node=True,
logging_dir=output/dsbtpg-chatglm-6b-pt-64-2e-2/runs/Jun24_02-09-10_autodl-container-07ff4facf2-80be90b7,
logging_first_step=False,
logging_nan_inf_filter=True,
logging_steps=20,
logging_strategy=steps,
lr_scheduler_type=linear,
max_grad_norm=1.0,
max_steps=1500,
metric_for_best_model=None,
mp_parameters=,
no_cuda=False,
num_train_epochs=3.0,
optim=adamw_hf,
optim_args=None,
output_dir=output/dsbtpg-chatglm-6b-pt-64-2e-2,
overwrite_output_dir=True,
past_index=-1,
per_device_eval_batch_size=1,
per_device_train_batch_size=4,
predict_with_generate=True,
prediction_loss_only=False,
push_to_hub=False,
push_to_hub_model_id=None,
push_to_hub_organization=None,
push_to_hub_token=<PUSH_TO_HUB_TOKEN>,
ray_scope=last,
remove_unused_columns=True,
report_to=[],
resume_from_checkpoint=None,
run_name=output/dsbtpg-chatglm-6b-pt-64-2e-2,
save_on_each_node=False,
save_steps=500,
save_strategy=steps,
save_total_limit=None,
seed=42,
sharded_ddp=[],
skip_memory_metrics=True,
sortish_sampler=False,
tf32=None,
torch_compile=False,
torch_compile_backend=None,
torch_compile_mode=None,
torchdynamo=None,
tpu_metrics_debug=False,
tpu_num_cores=None,
use_ipex=False,
use_legacy_prediction_loop=False,
use_mps_device=False,
warmup_ratio=0.0,
warmup_steps=0,
weight_decay=0.0,
xpu_backend=None,
)
/home/lyc/workspace/Conda-env/dsbtpg/lib/python3.10/site-packages/datasets/load.py:2547: FutureWarning: 'use_auth_token' was deprecated in favor of 'token' in version 2.14.0 and will be removed in 3.0.0.
You can remove this warning by passing 'token=<use_auth_token>' instead.
warnings.warn(
Generating train split: 540 examples [00:00, 79703.14 examples/s]
Generating validation split: 140 examples [00:00, 74565.40 examples/s]
[INFO|configuration_utils.py:666] 2024-05-21 12:09:10,905 >> loading configuration file /home/lyc/workspace/ChatGLM-6B/chatglm-6b/config.json
[WARNING|configuration_auto.py:905] 2024-05-21 12:09:10,905 >> Explicitly passing a `revision` is encouraged when loading a configuration with custom code to ensure no malicious code has been contributed in a newer revision.
[INFO|configuration_utils.py:666] 2024-05-21 12:09:10,930 >> loading configuration file /home/lyc/workspace/ChatGLM-6B/chatglm-6b/config.json
[INFO|configuration_utils.py:720] 2024-05-21 12:09:10,930 >> Model config ChatGLMConfig {
"_name_or_path": "/home/lyc/workspace/ChatGLM-6B/chatglm-6b",
"architectures": [
"ChatGLMModel"
],
"auto_map": {
"AutoConfig": "configuration_chatglm.ChatGLMConfig",
"AutoModel": "modeling_chatglm.ChatGLMForConditionalGeneration",
"AutoModelForSeq2SeqLM": "modeling_chatglm.ChatGLMForConditionalGeneration"
},
"bos_token_id": 130004,
"eos_token_id": 130005,
"gmask_token_id": 130001,
"hidden_size": 4096,
"inner_hidden_size": 16384,
"layernorm_epsilon": 1e-05,
"mask_token_id": 130000,
"max_sequence_length": 2048,
"model_type": "chatglm",
"num_attention_heads": 32,
"num_layers": 28,
"pad_token_id": 3,
"position_encoding_2d": true,
"pre_seq_len": null,
"prefix_projection": false,
"quantization_bit": 0,
"torch_dtype": "float16",
"transformers_version": "4.27.1",
"use_cache": true,
"vocab_size": 130528
}
[WARNING|tokenization_auto.py:652] 2024-05-21 12:09:10,931 >> Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
[INFO|tokenization_utils_base.py:1800] 2024-05-21 12:09:10,951 >> loading file ice_text.model
[INFO|tokenization_utils_base.py:1800] 2024-05-21 12:09:10,951 >> loading file added_tokens.json
[INFO|tokenization_utils_base.py:1800] 2024-05-21 12:09:10,951 >> loading file special_tokens_map.json
[INFO|tokenization_utils_base.py:1800] 2024-05-21 12:09:10,951 >> loading file tokenizer_config.json
[WARNING|auto_factory.py:456] 2024-05-21 12:09:11,153 >> Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
[INFO|modeling_utils.py:2400] 2024-05-21 12:09:11,204 >> loading weights file /home/lyc/workspace/ChatGLM-6B/chatglm-6b/pytorch_model.bin.index.json
[INFO|configuration_utils.py:575] 2024-05-21 12:09:11,205 >> Generate config GenerationConfig {
"_from_model_config": true,
"bos_token_id": 130004,
"eos_token_id": 130005,
"pad_token_id": 3,
"transformers_version": "4.27.1"
}
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████| 8/8 [00:11<00:00, 1.38s/it]
[INFO|modeling_utils.py:3032] 2024-05-21 12:09:22,387 >> All model checkpoint weights were used when initializing ChatGLMForConditionalGeneration.
[WARNING|modeling_utils.py:3034] 2024-05-21 12:09:22,387 >> Some weights of ChatGLMForConditionalGeneration were not initialized from the model checkpoint at /home/lyc/workspace/ChatGLM-6B/chatglm-6b and are newly initialized: ['transformer.prefix_encoder.embedding.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
[INFO|modeling_utils.py:2690] 2024-05-21 12:09:22,458 >> Generation config file not found, using a generation config created from the model config.
Quantized to 8 bit
以上为模型的加载内容,模型训练过程中会展示loss和部分训练样例,如:
{'loss': 1.1579, 'learning_rate': 0.0192, 'epoch': 2.37}
{'loss': 0.3397, 'learning_rate': 0.0184, 'epoch': 4.74}
{'loss': 0.3101, 'learning_rate': 0.0176, 'epoch': 7.11}
{'loss': 0.3134, 'learning_rate': 0.0168, 'epoch': 9.48}
{'loss': 0.3046, 'learning_rate': 0.016, 'epoch': 11.85}
20%|█████▊ | 100/500 [15:49<1:03:04, 9.46s/it]
可以观察到loss有明显下降,并且lr随训练次数降低以确保模型收敛,模型训练完后会自动保存并展示训练指标:
{'loss': 0.2978, 'learning_rate': 0.0032, 'epoch': 49.78}
{'loss': 0.2993, 'learning_rate': 0.0024, 'epoch': 52.15}
{'loss': 0.2991, 'learning_rate': 0.0016, 'epoch': 54.52}
{'loss': 0.2983, 'learning_rate': 0.0008, 'epoch': 56.89}
{'loss': 0.2957, 'learning_rate': 0.0, 'epoch': 59.26}
100%|███████████████████████████████████████████████████████████████████████████████| 500/500 [1:18:43<00:00, 9.45s/it]Saving PrefixEncoder
[INFO|configuration_utils.py:457] 2024-05-21 13:34:01,058 >> Configuration saved in output/dsbtpg-chatglm-6b-pt-64-2e-2/checkpoint-500/config.json
[INFO|configuration_utils.py:362] 2024-05-21 13:34:01,060 >> Configuration saved in output/dsbtpg-chatglm-6b-pt-64-2e-2/checkpoint-500/generation_config.json
[INFO|modeling_utils.py:1762] 2024-05-21 13:34:01,148 >> Model weights saved in output/dsbtpg-chatglm-6b-pt-64-2e-2/checkpoint-500/pytorch_model.bin
[INFO|tokenization_utils_base.py:2163] 2024-05-21 13:34:01,149 >> tokenizer config file saved in output/dsbtpg-chatglm-6b-pt-64-2e-2/checkpoint-500/tokenizer_config.json
[INFO|tokenization_utils_base.py:2170] 2024-05-21 13:34:01,149 >> Special tokens file saved in output/dsbtpg-chatglm-6b-pt-64-2e-2/checkpoint-500/special_tokens_map.json
{'train_runtime': 4723.6339, 'train_samples_per_second': 6.774, 'train_steps_per_second': 0.106, 'train_loss': 0.33856764221191404, 'epoch': 59.26}
100%|███████████████████████████████████████████████████████████████████████████████| 500/500 [1:18:43<00:00, 9.45s/it]
***** train metrics *****
epoch = 59.26
train_loss = 0.3386
train_runtime = 1:18:43.63
train_samples = 540
train_samples_per_second = 6.774
train_steps_per_second = 0.106
注:这里由于训练问题重新训练了,上述数据仅作为格式展示。
参考资料
[1] #ChatGLM-6B模型基于 P-Tuning v2 微调脚本参数解释 - Linux自动化运维 (linuxdevops.cn)
标签:Tuning,05,--,py,2024,v2,实训,chatglm,21 From: https://www.cnblogs.com/yichengliu0219/p/18264221