Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

大規模言語モデルのファインチューニング

規模言語モデル入門Ⅱ〜生成型LLMの実装と評価(2024)のソースコードを参考して作成します。

1LoRAチューニング

1.1データセットの準備

{'datetime': '2012/7/31 23:48',
 'label': 1,
 'sentence': 'ぼけっとしてたらこんな時間。チャリあるから食べにでたいのに…',
 'user_id': 1}
{'datetime': Value(dtype='string', id=None),
 'label': ClassLabel(names=['positive', 'negative'], id=None),
 'sentence': Value(dtype='string', id=None),
 'user_id': Value(dtype='int64', id=None)}

1.2トークナイザ

BertJapaneseTokenizer
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
  warnings.warn(
{'attention_mask': [1, 1, 1, 1, 1, 1, 1],
 'input_ids': [2, 12538, 465, 14985, 13037, 385, 3],
 'token_type_ids': [0, 0, 0, 0, 0, 0, 0]}
['[CLS]', 'これ', 'は', 'テスト', 'です', '。', '[SEP]']

1.3データセット統計の可視化

100%|██████████| 20149/20149 [00:01<00:00, 14694.70it/s]
<Figure size 640x480 with 1 Axes>
100%|██████████| 1608/1608 [00:00<00:00, 11939.01it/s]
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

1.4データセットの前処理

Parameter 'function'=<function preprocess_text_classification at 0x7c1259c3f6a0> of the transform datasets.arrow_dataset.Dataset._map_single couldn't be hashed properly, a random hash was used instead. Make sure your transforms and parameters are serializable with pickle or dill for the dataset fingerprinting and caching to work. If you reuse this transform, the caching mechanism will consider it to be different from the previous calls and recompute everything. This warning is only showed once. Subsequent hashing failures won't be showed.
Loading...
Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.
Loading...

1.5モデルの準備

`low_cpu_mem_usage` was None, now set to True since model is quantized.
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at cl-tohoku/bert-base-japanese-v3 and are newly initialized: ['classifier.bias', 'classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
BertForSequenceClassification
PeftModelForSequenceClassification
trainable params: 296,450 || all params: 111,505,156 || trainable%: 0.2659

1.6ミニバッチ構築

{'attention_mask': torch.Size([4, 36]),
 'input_ids': torch.Size([4, 36]),
 'labels': torch.Size([4]),
 'token_type_ids': torch.Size([4, 36])}

1.7訓練の実行

/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/transformers/training_args.py:1545: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead
  warnings.warn(
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/accelerate/accelerator.py:494: FutureWarning: `torch.cuda.amp.GradScaler(args...)` is deprecated. Please use `torch.amp.GradScaler('cuda', args...)` instead.
  self.scaler = torch.cuda.amp.GradScaler(**kwargs)
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/torch/nn/parallel/_functions.py:71: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn(
Loading...
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/torch/nn/parallel/_functions.py:71: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn(
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/torch/nn/parallel/_functions.py:71: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn(
/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/torch/nn/parallel/_functions.py:71: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn(
TrainOutput(global_step=945, training_loss=0.17979497354497354, metrics={'train_runtime': 169.1342, 'train_samples_per_second': 357.391, 'train_steps_per_second': 5.587, 'total_flos': 2988703002246600.0, 'train_loss': 0.17979497354497354, 'epoch': 3.0})

1.8訓練後のモデルの評価

/home/lvzeyu/anaconda3/envs/llm-ft/lib/python3.11/site-packages/torch/nn/parallel/_functions.py:71: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.
  warnings.warn(
Loading...
{'epoch': 3.0,
 'eval_accuracy': 0.9309701492537313,
 'eval_loss': 0.17510245740413666,
 'eval_runtime': 1.9986,
 'eval_samples_per_second': 804.578,
 'eval_steps_per_second': 13.009}