Add Fill-in-the-middle training objective example - PyTorch (#27464)
* add: initial script to train clm fim * fix: if training model from scratch, new tokens will be added and embeddings resized * fix: fixed attention_mask errors when generating FIM data * fix: file formatted using black * add: run_fim_no_trainer.py and fixed some comments in run_fim.py * add: added fim examples to the README.md and ran code fixup * fix: little bug in both fim training scripts * fix: remove comment from notebook and added a note on fim related params * fix: minor typo in README * add: suggested minor changes to README and run_fim.py * add: gradient_accumulation_steps and gradient_checkpointing args * add: improved model embedding resizing * add: pad_to_multiple_of and attn_implementation params * add: requested minor changes * add: deepspeed zero compatibility * add: resize embeddings layer with zero3 support for fim model initialization
This commit is contained in:
@@ -73,6 +73,57 @@ python run_clm_no_trainer.py \
|
|||||||
--output_dir /tmp/test-clm
|
--output_dir /tmp/test-clm
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### GPT-2/GPT and causal language modeling with fill-in-the middle objective
|
||||||
|
|
||||||
|
The following example fine-tunes GPT-2 on WikiText-2 but using the Fill-in-middle training objective. FIM objective was proposed in [Efficient Training of Language Models to Fill in the Middle](https://arxiv.org/abs/2207.14255). They showed that autoregressive language models can learn to infill text after applying a straightforward transformation to the dataset, which simply moves a span of text from the middle of a document to its end.
|
||||||
|
|
||||||
|
We're using the raw WikiText-2 (no tokens were replaced before the tokenization). The loss here is that of causal language modeling.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python run_fim.py \
|
||||||
|
--model_name_or_path gpt2 \
|
||||||
|
--dataset_name wikitext \
|
||||||
|
--dataset_config_name wikitext-2-raw-v1 \
|
||||||
|
--per_device_train_batch_size 8 \
|
||||||
|
--per_device_eval_batch_size 8 \
|
||||||
|
--fim_rate 0.5 \
|
||||||
|
--fim_spm_rate 0.2 \
|
||||||
|
--do_train \
|
||||||
|
--do_eval \
|
||||||
|
--output_dir /tmp/test-clm
|
||||||
|
```
|
||||||
|
|
||||||
|
To run on your own training and validation files, use the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python run_fim.py \
|
||||||
|
--model_name_or_path gpt2 \
|
||||||
|
--train_file path_to_train_file \
|
||||||
|
--validation_file path_to_validation_file \
|
||||||
|
--per_device_train_batch_size 8 \
|
||||||
|
--per_device_eval_batch_size 8 \
|
||||||
|
--fim_rate 0.5 \
|
||||||
|
--fim_spm_rate 0.2 \
|
||||||
|
--do_train \
|
||||||
|
--do_eval \
|
||||||
|
--output_dir /tmp/test-clm
|
||||||
|
```
|
||||||
|
|
||||||
|
This uses the built in HuggingFace `Trainer` for training. If you want to use a custom training loop, you can utilize or adapt the `run_fim_no_trainer.py` script. Take a look at the script for a list of supported arguments. An example is shown below:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python run_fim_no_trainer.py \
|
||||||
|
--model_name_or_path gpt2 \
|
||||||
|
--dataset_name wikitext \
|
||||||
|
--dataset_config_name wikitext-2-raw-v1 \
|
||||||
|
--model_name_or_path gpt2 \
|
||||||
|
--fim_rate 0.5 \
|
||||||
|
--fim_spm_rate 0.2 \
|
||||||
|
--output_dir /tmp/test-clm
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note**: Passing in FIM rate as `0.5` means that FIM transformations will be applied to the dataset with a probability of 50%. Whereas passing in FIM SPM rate as `0.2` means that 20% of FIM transformations will use SPM (or Suffix-Prefix-Middle) and the remaining 80% will use PSM (or Prefix-Suffix-Middle) mode of transformation.
|
||||||
|
|
||||||
### RoBERTa/BERT/DistilBERT and masked language modeling
|
### RoBERTa/BERT/DistilBERT and masked language modeling
|
||||||
|
|
||||||
The following example fine-tunes RoBERTa on WikiText-2. Here too, we're using the raw WikiText-2. The loss is different
|
The following example fine-tunes RoBERTa on WikiText-2. Here too, we're using the raw WikiText-2. The loss is different
|
||||||
@@ -176,11 +227,11 @@ sure all your batches have the same length.
|
|||||||
|
|
||||||
## Streaming
|
## Streaming
|
||||||
|
|
||||||
To use the streaming dataset mode which can be very useful for large datasets, add `--streaming` to the command line. This is currently supported by `run_mlm.py` and `run_clm.py`.
|
To use the streaming dataset mode which can be very useful for large datasets, add `--streaming` to the command line. This is supported by `run_mlm.py`, `run_clm.py` and `run_fim.py`. Make sure to adapt the other scripts to your use case by taking inspiration from them.
|
||||||
|
|
||||||
## Low Cpu Memory Usage
|
## Low Cpu Memory Usage
|
||||||
|
|
||||||
To use low cpu memory mode which can be very useful for LLM, add `--low_cpu_mem_usage` to the command line. This is currently supported by `run_clm.py`,`run_mlm.py`, `run_plm.py`,`run_mlm_no_trainer.py` and `run_clm_no_trainer.py`.
|
To use low cpu memory mode which can be very useful for LLM, add `--low_cpu_mem_usage` to the command line. This is currently supported by `run_clm.py`,`run_mlm.py`, `run_plm.py`, `run_fim.py`, `run_mlm_no_trainer.py`, `run_clm_no_trainer.py` and `run_fim_no_trainer.py`.
|
||||||
|
|
||||||
## Creating a model on the fly
|
## Creating a model on the fly
|
||||||
|
|
||||||
@@ -192,4 +243,4 @@ python run_clm.py --model_type openai-community/gpt2 --tokenizer_name openai-com
|
|||||||
[...]
|
[...]
|
||||||
```
|
```
|
||||||
|
|
||||||
This feature is only available in `run_clm.py`, `run_plm.py` and `run_mlm.py`.
|
This feature is only available in `run_clm.py`, `run_plm.py`, `run_mlm.py` and `run_fim.py`.
|
||||||
|
|||||||
861
examples/pytorch/language-modeling/run_fim.py
Normal file
861
examples/pytorch/language-modeling/run_fim.py
Normal file
@@ -0,0 +1,861 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
"""
|
||||||
|
Fine-tuning the library models for causal language modeling using
|
||||||
|
Fill-in-the middle (FIM) objective on a text file or a dataset.
|
||||||
|
|
||||||
|
Here is the full list of checkpoints on the hub that can be fine-tuned by this script:
|
||||||
|
https://huggingface.co/models?filter=text-generation
|
||||||
|
"""
|
||||||
|
# You should adapt this script on your own causal language modeling task. Pointers for this are left as comments.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from itertools import chain
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import datasets
|
||||||
|
import evaluate
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
from datasets import load_dataset
|
||||||
|
|
||||||
|
import transformers
|
||||||
|
from transformers import (
|
||||||
|
CONFIG_MAPPING,
|
||||||
|
MODEL_FOR_CAUSAL_LM_MAPPING,
|
||||||
|
AutoConfig,
|
||||||
|
AutoModelForCausalLM,
|
||||||
|
AutoTokenizer,
|
||||||
|
HfArgumentParser,
|
||||||
|
Trainer,
|
||||||
|
TrainingArguments,
|
||||||
|
default_data_collator,
|
||||||
|
is_deepspeed_zero3_enabled,
|
||||||
|
is_torch_tpu_available,
|
||||||
|
set_seed,
|
||||||
|
)
|
||||||
|
from transformers.testing_utils import CaptureLogger
|
||||||
|
from transformers.trainer_utils import get_last_checkpoint
|
||||||
|
from transformers.utils import check_min_version, send_example_telemetry
|
||||||
|
from transformers.utils.versions import require_version
|
||||||
|
|
||||||
|
|
||||||
|
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
|
||||||
|
check_min_version("4.36.0.dev0")
|
||||||
|
|
||||||
|
require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt")
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
MODEL_CONFIG_CLASSES = list(MODEL_FOR_CAUSAL_LM_MAPPING.keys())
|
||||||
|
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ModelArguments:
|
||||||
|
"""
|
||||||
|
Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
|
||||||
|
"""
|
||||||
|
|
||||||
|
model_name_or_path: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"The model checkpoint for weights initialization. Don't set if you want to train a model from scratch."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
model_type: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(MODEL_TYPES)},
|
||||||
|
)
|
||||||
|
config_overrides: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Override some existing default config settings when a model is trained from scratch. Example: "
|
||||||
|
"n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
config_name: Optional[str] = field(
|
||||||
|
default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
|
||||||
|
)
|
||||||
|
tokenizer_name: Optional[str] = field(
|
||||||
|
default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
|
||||||
|
)
|
||||||
|
cache_dir: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},
|
||||||
|
)
|
||||||
|
use_fast_tokenizer: bool = field(
|
||||||
|
default=True,
|
||||||
|
metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."},
|
||||||
|
)
|
||||||
|
model_revision: str = field(
|
||||||
|
default="main",
|
||||||
|
metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
|
||||||
|
)
|
||||||
|
token: str = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"The token to use as HTTP bearer authorization for remote files. If not specified, will use the token "
|
||||||
|
"generated when running `huggingface-cli login` (stored in `~/.huggingface`)."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
trust_remote_code: bool = field(
|
||||||
|
default=False,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Whether or not to allow for custom models defined on the Hub in their own modeling files. This option"
|
||||||
|
"should only be set to `True` for repositories you trust and in which you have read the code, as it will "
|
||||||
|
"execute code present on the Hub on your local machine."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
torch_dtype: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Override the default `torch.dtype` and load the model under this dtype. If `auto` is passed, the "
|
||||||
|
"dtype will be automatically derived from the model's weights."
|
||||||
|
),
|
||||||
|
"choices": ["auto", "bfloat16", "float16", "float32"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
low_cpu_mem_usage: bool = field(
|
||||||
|
default=False,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"It is an option to create the model as an empty shell, then only materialize its parameters when the pretrained weights are loaded. "
|
||||||
|
"set True will benefit LLM loading time and RAM consumption."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
pad_to_multiple_of: bool = field(
|
||||||
|
default=False,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Whether to pad the embedding layer to a multiple depending on the device. ",
|
||||||
|
"For NVIDIA GPUs, this will be a multiple of 8, for TPUs a multiple of 128.",
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
attn_implementation: Optional[str] = field(
|
||||||
|
default="sdpa", metadata={"help": ("The attention implementation to use. ")}
|
||||||
|
)
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None):
|
||||||
|
raise ValueError(
|
||||||
|
"--config_overrides can't be used in combination with --config_name or --model_name_or_path"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DataTrainingArguments:
|
||||||
|
"""
|
||||||
|
Arguments pertaining to what data we are going to input our model for training and eval.
|
||||||
|
"""
|
||||||
|
|
||||||
|
dataset_name: Optional[str] = field(
|
||||||
|
default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
|
||||||
|
)
|
||||||
|
dataset_config_name: Optional[str] = field(
|
||||||
|
default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
|
||||||
|
)
|
||||||
|
train_file: Optional[str] = field(default=None, metadata={"help": "The input training data file (a text file)."})
|
||||||
|
validation_file: Optional[str] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."},
|
||||||
|
)
|
||||||
|
max_train_samples: Optional[int] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"For debugging purposes or quicker training, truncate the number of training examples to this "
|
||||||
|
"value if set."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
max_eval_samples: Optional[int] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"For debugging purposes or quicker training, truncate the number of evaluation examples to this "
|
||||||
|
"value if set."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
streaming: bool = field(default=False, metadata={"help": "Enable streaming mode"})
|
||||||
|
block_size: Optional[int] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Optional input sequence length after tokenization. "
|
||||||
|
"The training dataset will be truncated in block of this size for training. "
|
||||||
|
"Default to the model max input length for single sentence inputs (take into account special tokens)."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
fim_rate: Optional[float] = field(
|
||||||
|
default=0.5,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Optional probability with which the FIM transformation is applied to the example. "
|
||||||
|
"Default is 0.5. A rate of 1.0 means every example will undergo FIM transformation, "
|
||||||
|
"while a rate of 0.0 means no example will."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
fim_spm_rate: Optional[float] = field(
|
||||||
|
default=0.5,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Within the examples undergoing FIM transformation, this rate determines the probability "
|
||||||
|
"of applying the Sentence Permutation Mode (SPM). "
|
||||||
|
"Default is 0.5. A rate of 1.0 means all FIM transformations will use SPM, "
|
||||||
|
"while a rate of 0.0 means none will."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
truncate_or_pad: Optional[bool] = field(
|
||||||
|
default=True,
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Indicates whether the transformed example should be truncated or padded to maintain "
|
||||||
|
"the same length as the original example. "
|
||||||
|
"Default is True. If False, the function will not truncate or pad the examples."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
fim_prefix_token: Optional[str] = field(
|
||||||
|
default="<fim_prefix>",
|
||||||
|
metadata={"help": ("Fill-in-Middle Prefix token. Defaults to '<fim_prefix>'.")},
|
||||||
|
)
|
||||||
|
fim_middle_token: Optional[str] = field(
|
||||||
|
default="<fim_middle>",
|
||||||
|
metadata={"help": ("Fill-in-Middle Middle token. Defaults to '<fim_middle>'.")},
|
||||||
|
)
|
||||||
|
fim_suffix_token: Optional[str] = field(
|
||||||
|
default="<fim_suffix>",
|
||||||
|
metadata={"help": ("Fill-in-Middle Suffix token. Defaults to '<fim_suffix>'.")},
|
||||||
|
)
|
||||||
|
pad_token: Optional[str] = field(
|
||||||
|
default="<fim_pad>",
|
||||||
|
metadata={
|
||||||
|
"help": (
|
||||||
|
"Fill-in-Middle Pad token. Used only when 'truncate_or_pad' is set to True. "
|
||||||
|
"Defaults to '<fim_pad>'."
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
overwrite_cache: bool = field(
|
||||||
|
default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
|
||||||
|
)
|
||||||
|
validation_split_percentage: Optional[int] = field(
|
||||||
|
default=5,
|
||||||
|
metadata={
|
||||||
|
"help": "The percentage of the train set used as validation set in case there's no validation split"
|
||||||
|
},
|
||||||
|
)
|
||||||
|
preprocessing_num_workers: Optional[int] = field(
|
||||||
|
default=None,
|
||||||
|
metadata={"help": "The number of processes to use for the preprocessing."},
|
||||||
|
)
|
||||||
|
keep_linebreaks: bool = field(
|
||||||
|
default=True, metadata={"help": "Whether to keep line breaks when using TXT files or not."}
|
||||||
|
)
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.streaming:
|
||||||
|
require_version("datasets>=2.0.0", "The streaming feature requires `datasets>=2.0.0`")
|
||||||
|
|
||||||
|
if self.dataset_name is None and self.train_file is None and self.validation_file is None:
|
||||||
|
raise ValueError("Need either a dataset name or a training/validation file.")
|
||||||
|
else:
|
||||||
|
if self.train_file is not None:
|
||||||
|
extension = self.train_file.split(".")[-1]
|
||||||
|
assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
|
||||||
|
if self.validation_file is not None:
|
||||||
|
extension = self.validation_file.split(".")[-1]
|
||||||
|
assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file."
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# See all possible arguments in src/transformers/training_args.py
|
||||||
|
# or by passing the --help flag to this script.
|
||||||
|
# We now keep distinct sets of args, for a cleaner separation of concerns.
|
||||||
|
|
||||||
|
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
|
||||||
|
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
|
||||||
|
# If we pass only one argument to the script and it's the path to a json file,
|
||||||
|
# let's parse it to get our arguments.
|
||||||
|
model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
|
||||||
|
else:
|
||||||
|
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
|
||||||
|
|
||||||
|
# Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The
|
||||||
|
# information sent is the one passed as arguments along with your Python/PyTorch versions.
|
||||||
|
send_example_telemetry("run_fim", model_args, data_args)
|
||||||
|
|
||||||
|
# Setup logging
|
||||||
|
logging.basicConfig(
|
||||||
|
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
|
||||||
|
datefmt="%m/%d/%Y %H:%M:%S",
|
||||||
|
handlers=[logging.StreamHandler(sys.stdout)],
|
||||||
|
)
|
||||||
|
|
||||||
|
if training_args.should_log:
|
||||||
|
# The default of training_args.log_level is passive, so we set log level at info here to have that default.
|
||||||
|
transformers.utils.logging.set_verbosity_info()
|
||||||
|
|
||||||
|
log_level = training_args.get_process_log_level()
|
||||||
|
logger.setLevel(log_level)
|
||||||
|
datasets.utils.logging.set_verbosity(log_level)
|
||||||
|
transformers.utils.logging.set_verbosity(log_level)
|
||||||
|
transformers.utils.logging.enable_default_handler()
|
||||||
|
transformers.utils.logging.enable_explicit_format()
|
||||||
|
|
||||||
|
# Log on each process the small summary:
|
||||||
|
logger.warning(
|
||||||
|
f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, "
|
||||||
|
+ f"distributed training: {training_args.parallel_mode.value == 'distributed'}, 16-bits training: {training_args.fp16}"
|
||||||
|
)
|
||||||
|
logger.info(f"Training/evaluation parameters {training_args}")
|
||||||
|
|
||||||
|
# Detecting last checkpoint.
|
||||||
|
last_checkpoint = None
|
||||||
|
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
|
||||||
|
last_checkpoint = get_last_checkpoint(training_args.output_dir)
|
||||||
|
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
|
||||||
|
raise ValueError(
|
||||||
|
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
|
||||||
|
"Use --overwrite_output_dir to overcome."
|
||||||
|
)
|
||||||
|
elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
|
||||||
|
logger.info(
|
||||||
|
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
|
||||||
|
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set seed before initializing model.
|
||||||
|
set_seed(training_args.seed)
|
||||||
|
|
||||||
|
# Set a numpy random state for FIM transformations
|
||||||
|
np_rng = np.random.RandomState(seed=training_args.seed)
|
||||||
|
|
||||||
|
# Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below)
|
||||||
|
# or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/
|
||||||
|
# (the dataset will be downloaded automatically from the datasets Hub).
|
||||||
|
#
|
||||||
|
# For CSV/JSON files, this script will use the column called 'text' or the first column if no column called
|
||||||
|
# 'text' is found. You can easily tweak this behavior (see below).
|
||||||
|
#
|
||||||
|
# In distributed training, the load_dataset function guarantee that only one local process can concurrently
|
||||||
|
# download the dataset.
|
||||||
|
if data_args.dataset_name is not None:
|
||||||
|
# Downloading and loading a dataset from the hub.
|
||||||
|
raw_datasets = load_dataset(
|
||||||
|
data_args.dataset_name,
|
||||||
|
data_args.dataset_config_name,
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
streaming=data_args.streaming,
|
||||||
|
)
|
||||||
|
if "validation" not in raw_datasets.keys():
|
||||||
|
raw_datasets["validation"] = load_dataset(
|
||||||
|
data_args.dataset_name,
|
||||||
|
data_args.dataset_config_name,
|
||||||
|
split=f"train[:{data_args.validation_split_percentage}%]",
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
streaming=data_args.streaming,
|
||||||
|
)
|
||||||
|
raw_datasets["train"] = load_dataset(
|
||||||
|
data_args.dataset_name,
|
||||||
|
data_args.dataset_config_name,
|
||||||
|
split=f"train[{data_args.validation_split_percentage}%:]",
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
streaming=data_args.streaming,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
data_files = {}
|
||||||
|
dataset_args = {}
|
||||||
|
if data_args.train_file is not None:
|
||||||
|
data_files["train"] = data_args.train_file
|
||||||
|
if data_args.validation_file is not None:
|
||||||
|
data_files["validation"] = data_args.validation_file
|
||||||
|
extension = (
|
||||||
|
data_args.train_file.split(".")[-1]
|
||||||
|
if data_args.train_file is not None
|
||||||
|
else data_args.validation_file.split(".")[-1]
|
||||||
|
)
|
||||||
|
if extension == "txt":
|
||||||
|
extension = "text"
|
||||||
|
dataset_args["keep_linebreaks"] = data_args.keep_linebreaks
|
||||||
|
raw_datasets = load_dataset(
|
||||||
|
extension,
|
||||||
|
data_files=data_files,
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
**dataset_args,
|
||||||
|
)
|
||||||
|
# If no validation data is there, validation_split_percentage will be used to divide the dataset.
|
||||||
|
if "validation" not in raw_datasets.keys():
|
||||||
|
raw_datasets["validation"] = load_dataset(
|
||||||
|
extension,
|
||||||
|
data_files=data_files,
|
||||||
|
split=f"train[:{data_args.validation_split_percentage}%]",
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
**dataset_args,
|
||||||
|
)
|
||||||
|
raw_datasets["train"] = load_dataset(
|
||||||
|
extension,
|
||||||
|
data_files=data_files,
|
||||||
|
split=f"train[{data_args.validation_split_percentage}%:]",
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
token=model_args.token,
|
||||||
|
**dataset_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
# See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
|
||||||
|
# https://huggingface.co/docs/datasets/loading_datasets.html.
|
||||||
|
|
||||||
|
# Load pretrained model and tokenizer
|
||||||
|
#
|
||||||
|
# Distributed training:
|
||||||
|
# The .from_pretrained methods guarantee that only one local process can concurrently
|
||||||
|
# download model & vocab.
|
||||||
|
|
||||||
|
config_kwargs = {
|
||||||
|
"cache_dir": model_args.cache_dir,
|
||||||
|
"revision": model_args.model_revision,
|
||||||
|
"token": model_args.token,
|
||||||
|
"trust_remote_code": model_args.trust_remote_code,
|
||||||
|
}
|
||||||
|
if model_args.config_name:
|
||||||
|
config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs)
|
||||||
|
elif model_args.model_name_or_path:
|
||||||
|
config = AutoConfig.from_pretrained(model_args.model_name_or_path, **config_kwargs)
|
||||||
|
else:
|
||||||
|
config = CONFIG_MAPPING[model_args.model_type]()
|
||||||
|
logger.warning("You are instantiating a new config instance from scratch.")
|
||||||
|
if model_args.config_overrides is not None:
|
||||||
|
logger.info(f"Overriding config: {model_args.config_overrides}")
|
||||||
|
config.update_from_string(model_args.config_overrides)
|
||||||
|
logger.info(f"New config: {config}")
|
||||||
|
|
||||||
|
tokenizer_kwargs = {
|
||||||
|
"cache_dir": model_args.cache_dir,
|
||||||
|
"use_fast": model_args.use_fast_tokenizer,
|
||||||
|
"revision": model_args.model_revision,
|
||||||
|
"token": model_args.token,
|
||||||
|
"trust_remote_code": model_args.trust_remote_code,
|
||||||
|
}
|
||||||
|
if model_args.tokenizer_name:
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs)
|
||||||
|
elif model_args.model_name_or_path:
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path, **tokenizer_kwargs)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
"You are instantiating a new tokenizer from scratch. This is not supported by this script. "
|
||||||
|
"You can do it from another script, save it, and load it from here, using --tokenizer_name."
|
||||||
|
)
|
||||||
|
|
||||||
|
if model_args.model_name_or_path:
|
||||||
|
torch_dtype = (
|
||||||
|
model_args.torch_dtype
|
||||||
|
if model_args.torch_dtype in ["auto", None]
|
||||||
|
else getattr(torch, model_args.torch_dtype)
|
||||||
|
)
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
model_args.model_name_or_path,
|
||||||
|
from_tf=bool(".ckpt" in model_args.model_name_or_path),
|
||||||
|
config=config,
|
||||||
|
cache_dir=model_args.cache_dir,
|
||||||
|
revision=model_args.model_revision,
|
||||||
|
token=model_args.token,
|
||||||
|
trust_remote_code=model_args.trust_remote_code,
|
||||||
|
torch_dtype=torch_dtype,
|
||||||
|
low_cpu_mem_usage=model_args.low_cpu_mem_usage,
|
||||||
|
attn_implementation=model_args.attn_implementation,
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
model = AutoModelForCausalLM.from_config(
|
||||||
|
config,
|
||||||
|
trust_remote_code=model_args.trust_remote_code,
|
||||||
|
attn_implementation=model_args.attn_implementation,
|
||||||
|
)
|
||||||
|
n_params = sum({p.data_ptr(): p.numel() for p in model.parameters()}.values())
|
||||||
|
logger.info(f"Training new model from scratch - Total size={n_params/2**20:.2f}M params")
|
||||||
|
|
||||||
|
# Add the new FIM tokens to the tokenizer and resize model's vocab embeddings
|
||||||
|
special_tokens = [data_args.fim_prefix_token, data_args.fim_middle_token, data_args.fim_suffix_token]
|
||||||
|
if data_args.truncate_or_pad:
|
||||||
|
special_tokens.append(data_args.pad_token)
|
||||||
|
|
||||||
|
# Get the factor by which the embedding layer should be padded based on the device
|
||||||
|
pad_factor = 1
|
||||||
|
if torch.cuda.is_availble():
|
||||||
|
pad_factor = 8
|
||||||
|
|
||||||
|
elif is_torch_tpu_available():
|
||||||
|
pad_factor = 128
|
||||||
|
|
||||||
|
# Add the new tokens to the tokenizer
|
||||||
|
tokenizer.add_tokens(special_tokens)
|
||||||
|
original_embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
if is_deepspeed_zero3_enabled():
|
||||||
|
import deepspeed
|
||||||
|
|
||||||
|
with deepspeed.zero.GatheredParameters(original_embeddings.weight, modifier_rank=0):
|
||||||
|
# Get the pre-expansion embeddings of the model and resize the embedding layer
|
||||||
|
model.resize_token_embeddings(len(tokenizer), pad_to_multiple_of=pad_factor)
|
||||||
|
embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
# Sample the embeddings for the new tokens from a multivariate normal distribution
|
||||||
|
# We do this so that the new embeddings are close to the original embeddings and not necessarily zero
|
||||||
|
# More on this: https://nlp.stanford.edu/~johnhew/vocab-expansion.html
|
||||||
|
mean = original_embeddings.mean(dim=0)
|
||||||
|
n = original_embeddings.size()[0]
|
||||||
|
sigma = ((original_embeddings - mean).T @ (original_embeddings - mean)) / n
|
||||||
|
dist = torch.distributions.multivariate_normal.MultivariateNormal(
|
||||||
|
mean,
|
||||||
|
covariance_matrix=1e-5 * sigma,
|
||||||
|
)
|
||||||
|
new_token_embeddings = torch.stack(
|
||||||
|
tuple((dist.sample() for _ in range(len(special_tokens)))),
|
||||||
|
dim=0,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
original_embeddings = model.get_input_embeddings()
|
||||||
|
# Get the pre-expansion embeddings of the model and resize the embedding layer
|
||||||
|
model.resize_token_embeddings(len(tokenizer), pad_to_multiple_of=pad_factor)
|
||||||
|
embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
# Sample the embeddings for the new tokens from a multivariate normal distribution
|
||||||
|
# We do this so that the new embeddings are close to the original embeddings and not necessarily zero
|
||||||
|
# More on this: https://nlp.stanford.edu/~johnhew/vocab-expansion.html
|
||||||
|
mean = original_embeddings.mean(dim=0)
|
||||||
|
n = original_embeddings.size()[0]
|
||||||
|
sigma = ((original_embeddings - mean).T @ (original_embeddings - mean)) / n
|
||||||
|
dist = torch.distributions.multivariate_normal.MultivariateNormal(
|
||||||
|
mean,
|
||||||
|
covariance_matrix=1e-5 * sigma,
|
||||||
|
)
|
||||||
|
new_token_embeddings = torch.stack(
|
||||||
|
tuple((dist.sample() for _ in range(len(special_tokens)))),
|
||||||
|
dim=0,
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_deepspeed_zero3_enabled():
|
||||||
|
import deepspeed
|
||||||
|
|
||||||
|
with deepspeed.zero.GatheredParameters(embeddings.weight, modifier_rank=0):
|
||||||
|
# Set the new tokens' embeddings to the newly sampled embeddings
|
||||||
|
embeddings.weight.data[-len(special_tokens) :] = new_token_embeddings
|
||||||
|
else:
|
||||||
|
# Set the new tokens' embeddings to the newly sampled embeddings
|
||||||
|
embeddings.weight.data[-len(special_tokens) :] = new_token_embeddings
|
||||||
|
|
||||||
|
# Update the model's embeddings with the new embeddings
|
||||||
|
model.set_input_embeddings(embeddings)
|
||||||
|
|
||||||
|
logger.info("Added special tokens to the tokenizer and resized model's embedding layer")
|
||||||
|
|
||||||
|
# Preprocessing the datasets.
|
||||||
|
# First we tokenize all the texts.
|
||||||
|
if training_args.do_train:
|
||||||
|
column_names = list(raw_datasets["train"].features)
|
||||||
|
else:
|
||||||
|
column_names = list(raw_datasets["validation"].features)
|
||||||
|
text_column_name = "text" if "text" in column_names else column_names[0]
|
||||||
|
|
||||||
|
# since this will be pickled to avoid _LazyModule error in Hasher force logger loading before tokenize_function
|
||||||
|
tok_logger = transformers.utils.logging.get_logger("transformers.tokenization_utils_base")
|
||||||
|
|
||||||
|
def tokenize_function(examples):
|
||||||
|
with CaptureLogger(tok_logger) as cl:
|
||||||
|
output = tokenizer(examples[text_column_name])
|
||||||
|
# clm-fim input could be much much longer than block_size
|
||||||
|
if "Token indices sequence length is longer than the" in cl.out:
|
||||||
|
tok_logger.warning(
|
||||||
|
"^^^^^^^^^^^^^^^^ Please ignore the warning above - this long input will be chunked into smaller bits"
|
||||||
|
" before being passed to the model."
|
||||||
|
)
|
||||||
|
return output
|
||||||
|
|
||||||
|
with training_args.main_process_first(desc="dataset map tokenization"):
|
||||||
|
if not data_args.streaming:
|
||||||
|
tokenized_datasets = raw_datasets.map(
|
||||||
|
tokenize_function,
|
||||||
|
batched=True,
|
||||||
|
num_proc=data_args.preprocessing_num_workers,
|
||||||
|
remove_columns=column_names,
|
||||||
|
load_from_cache_file=not data_args.overwrite_cache,
|
||||||
|
desc="Running tokenizer on dataset",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
tokenized_datasets = raw_datasets.map(
|
||||||
|
tokenize_function,
|
||||||
|
batched=True,
|
||||||
|
remove_columns=column_names,
|
||||||
|
)
|
||||||
|
|
||||||
|
if data_args.block_size is None:
|
||||||
|
block_size = tokenizer.model_max_length
|
||||||
|
if block_size > config.max_position_embeddings:
|
||||||
|
logger.warning(
|
||||||
|
f"The tokenizer picked seems to have a very large `model_max_length` ({tokenizer.model_max_length}). "
|
||||||
|
f"Using block_size={min(1024, config.max_position_embeddings)} instead. You can change that default value by passing --block_size xxx."
|
||||||
|
)
|
||||||
|
block_size = min(1024, config.max_position_embeddings)
|
||||||
|
else:
|
||||||
|
if data_args.block_size > tokenizer.model_max_length:
|
||||||
|
logger.warning(
|
||||||
|
f"The block_size passed ({data_args.block_size}) is larger than the maximum length for the model "
|
||||||
|
f"({tokenizer.model_max_length}). Using block_size={tokenizer.model_max_length}."
|
||||||
|
)
|
||||||
|
block_size = min(data_args.block_size, tokenizer.model_max_length)
|
||||||
|
|
||||||
|
# Data processing function that will concatenate all texts from our dataset and generate chunks of block_size.
|
||||||
|
def group_texts(examples):
|
||||||
|
# Concatenate all texts.
|
||||||
|
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
|
||||||
|
total_length = len(concatenated_examples[list(examples.keys())[0]])
|
||||||
|
# We drop the small remainder, and if the total_length < block_size we exclude this batch and return an empty dict.
|
||||||
|
# We could add padding if the model supported it instead of this drop, you can customize this part to your needs.
|
||||||
|
total_length = (total_length // block_size) * block_size
|
||||||
|
# Split by chunks of max_len.
|
||||||
|
result = {
|
||||||
|
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
|
||||||
|
for k, t in concatenated_examples.items()
|
||||||
|
}
|
||||||
|
result["labels"] = result["input_ids"].copy()
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Get the FIM-specific token ids
|
||||||
|
prefix_tok_id = tokenizer.convert_tokens_to_ids(data_args.fim_prefix_token)
|
||||||
|
middle_tok_id = tokenizer.convert_tokens_to_ids(data_args.fim_middle_token)
|
||||||
|
suffix_tok_id = tokenizer.convert_tokens_to_ids(data_args.fim_suffix_token)
|
||||||
|
pad_tok_id = None
|
||||||
|
|
||||||
|
# If truncate_or_pad is on, also get pad token id
|
||||||
|
if data_args.truncate_or_pad:
|
||||||
|
pad_tok_id = tokenizer.convert_tokens_to_ids(data_args.pad_token)
|
||||||
|
|
||||||
|
# The two functions below perform the FIM transformation on the data (either PSM or SPM or PSM+SPM)
|
||||||
|
# Don't call fim_transform directly in .map()
|
||||||
|
# Adapted from https://github.com/loubnabnl/santacoder-finetuning/blob/main/fim.py#L22C13-L83
|
||||||
|
def fim_transform(example):
|
||||||
|
"""
|
||||||
|
This function performs FIM transformation on a single example (list of tokens)
|
||||||
|
"""
|
||||||
|
if np_rng.binomial(1, data_args.fim_rate):
|
||||||
|
boundaries = sorted(np_rng.randint(low=0, high=len(example) + 1, size=2))
|
||||||
|
|
||||||
|
prefix = example[: boundaries[0]]
|
||||||
|
middle = example[boundaries[0] : boundaries[1]]
|
||||||
|
suffix = example[boundaries[1] :]
|
||||||
|
|
||||||
|
if data_args.truncate_or_pad:
|
||||||
|
total_length = len(prefix) + len(middle) + len(suffix) + 3
|
||||||
|
diff = total_length - len(example)
|
||||||
|
if diff > 0:
|
||||||
|
suffix = suffix[: max(0, len(suffix) - diff)]
|
||||||
|
elif diff < 0:
|
||||||
|
suffix.extend([pad_tok_id] * (-diff))
|
||||||
|
|
||||||
|
if np_rng.binomial(1, data_args.fim_spm_rate):
|
||||||
|
# Apply Suffix-Prefix-Middle (SPM) transformation
|
||||||
|
transformed_example = [prefix_tok_id, suffix_tok_id] + suffix + [middle_tok_id] + prefix + middle
|
||||||
|
else:
|
||||||
|
# Apply Prefix-Suffix-Middle (PSM) transformation
|
||||||
|
transformed_example = [prefix_tok_id] + prefix + [suffix_tok_id] + suffix + [middle_tok_id] + middle
|
||||||
|
else:
|
||||||
|
transformed_example = example
|
||||||
|
|
||||||
|
return transformed_example
|
||||||
|
|
||||||
|
# Below function is the one you are supposed to call in the .map() function
|
||||||
|
def apply_fim(examples):
|
||||||
|
"""
|
||||||
|
Apply FIM transformation to a batch of examples
|
||||||
|
"""
|
||||||
|
fim_transform_ids = [fim_transform(ids) for ids in examples["input_ids"]]
|
||||||
|
examples["input_ids"] = fim_transform_ids
|
||||||
|
examples["labels"] = fim_transform_ids
|
||||||
|
# If your application requires custom attention mask, please adjust this function's below line.
|
||||||
|
# Since FIM transformation increases the number of tokens in input_ids and labels
|
||||||
|
# but leaves the number of tokens unchanged in attention_masks which would cause problems
|
||||||
|
examples["attention_mask"] = [[1] * len(mask) for mask in examples["input_ids"]]
|
||||||
|
return examples
|
||||||
|
|
||||||
|
# Note that with `batched=True`, this map processes 1,000 texts together, so group_texts throws away a remainder
|
||||||
|
# for each of those groups of 1,000 texts. You can adjust that batch_size here but a higher value might be slower
|
||||||
|
# to preprocess.
|
||||||
|
#
|
||||||
|
# To speed up this part, we use multiprocessing. See the documentation of the map method for more information:
|
||||||
|
# https://huggingface.co/docs/datasets/process#map
|
||||||
|
|
||||||
|
# FIM transformations are only supposed to be applied before group_texts processing otherwise some sentences will
|
||||||
|
# have 3-4 more tokens than others due to probabilistic addition of FIM-specific tokens which will raise errors
|
||||||
|
with training_args.main_process_first(desc="processing texts together"):
|
||||||
|
if not data_args.streaming:
|
||||||
|
fim_datasets = tokenized_datasets.map(
|
||||||
|
apply_fim,
|
||||||
|
batched=True,
|
||||||
|
num_proc=data_args.preprocessing_num_workers,
|
||||||
|
load_from_cache_file=not data_args.overwrite_cache,
|
||||||
|
desc="Performing FIM transformation",
|
||||||
|
)
|
||||||
|
lm_datasets = fim_datasets.map(
|
||||||
|
group_texts,
|
||||||
|
batched=True,
|
||||||
|
num_proc=data_args.preprocessing_num_workers,
|
||||||
|
load_from_cache_file=not data_args.overwrite_cache,
|
||||||
|
desc=f"Grouping texts in chunks of {block_size}",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
fim_datasets = tokenized_datasets.map(
|
||||||
|
apply_fim,
|
||||||
|
batched=True,
|
||||||
|
)
|
||||||
|
lm_datasets = fim_datasets.map(
|
||||||
|
group_texts,
|
||||||
|
batched=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if training_args.do_train:
|
||||||
|
if "train" not in tokenized_datasets:
|
||||||
|
raise ValueError("--do_train requires a train dataset")
|
||||||
|
train_dataset = lm_datasets["train"]
|
||||||
|
if data_args.max_train_samples is not None:
|
||||||
|
max_train_samples = min(len(train_dataset), data_args.max_train_samples)
|
||||||
|
train_dataset = train_dataset.select(range(max_train_samples))
|
||||||
|
|
||||||
|
if training_args.do_eval:
|
||||||
|
if "validation" not in tokenized_datasets:
|
||||||
|
raise ValueError("--do_eval requires a validation dataset")
|
||||||
|
eval_dataset = lm_datasets["validation"]
|
||||||
|
if data_args.max_eval_samples is not None:
|
||||||
|
max_eval_samples = min(len(eval_dataset), data_args.max_eval_samples)
|
||||||
|
eval_dataset = eval_dataset.select(range(max_eval_samples))
|
||||||
|
|
||||||
|
def preprocess_logits_for_metrics(logits, labels):
|
||||||
|
if isinstance(logits, tuple):
|
||||||
|
# Depending on the model and config, logits may contain extra tensors,
|
||||||
|
# like past_key_values, but logits always come first
|
||||||
|
logits = logits[0]
|
||||||
|
return logits.argmax(dim=-1)
|
||||||
|
|
||||||
|
metric = evaluate.load("accuracy")
|
||||||
|
|
||||||
|
def compute_metrics(eval_preds):
|
||||||
|
preds, labels = eval_preds
|
||||||
|
# preds have the same shape as the labels, after the argmax(-1) has been calculated
|
||||||
|
# by preprocess_logits_for_metrics but we need to shift the labels
|
||||||
|
labels = labels[:, 1:].reshape(-1)
|
||||||
|
preds = preds[:, :-1].reshape(-1)
|
||||||
|
return metric.compute(predictions=preds, references=labels)
|
||||||
|
|
||||||
|
# Initialize our Trainer
|
||||||
|
trainer = Trainer(
|
||||||
|
model=model,
|
||||||
|
args=training_args,
|
||||||
|
train_dataset=train_dataset if training_args.do_train else None,
|
||||||
|
eval_dataset=eval_dataset if training_args.do_eval else None,
|
||||||
|
tokenizer=tokenizer,
|
||||||
|
# Data collator will default to DataCollatorWithPadding, so we change it.
|
||||||
|
data_collator=default_data_collator,
|
||||||
|
compute_metrics=compute_metrics if training_args.do_eval and not is_torch_tpu_available() else None,
|
||||||
|
preprocess_logits_for_metrics=(
|
||||||
|
preprocess_logits_for_metrics if training_args.do_eval and not is_torch_tpu_available() else None
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Training
|
||||||
|
if training_args.do_train:
|
||||||
|
checkpoint = None
|
||||||
|
if training_args.resume_from_checkpoint is not None:
|
||||||
|
checkpoint = training_args.resume_from_checkpoint
|
||||||
|
elif last_checkpoint is not None:
|
||||||
|
checkpoint = last_checkpoint
|
||||||
|
train_result = trainer.train(resume_from_checkpoint=checkpoint)
|
||||||
|
trainer.save_model() # Saves the tokenizer too for easy upload
|
||||||
|
|
||||||
|
metrics = train_result.metrics
|
||||||
|
|
||||||
|
max_train_samples = (
|
||||||
|
data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset)
|
||||||
|
)
|
||||||
|
metrics["train_samples"] = min(max_train_samples, len(train_dataset))
|
||||||
|
|
||||||
|
trainer.log_metrics("train", metrics)
|
||||||
|
trainer.save_metrics("train", metrics)
|
||||||
|
trainer.save_state()
|
||||||
|
|
||||||
|
# Evaluation
|
||||||
|
if training_args.do_eval:
|
||||||
|
logger.info("*** Evaluate ***")
|
||||||
|
|
||||||
|
metrics = trainer.evaluate()
|
||||||
|
|
||||||
|
max_eval_samples = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(eval_dataset)
|
||||||
|
metrics["eval_samples"] = min(max_eval_samples, len(eval_dataset))
|
||||||
|
try:
|
||||||
|
perplexity = math.exp(metrics["eval_loss"])
|
||||||
|
except OverflowError:
|
||||||
|
perplexity = float("inf")
|
||||||
|
metrics["perplexity"] = perplexity
|
||||||
|
|
||||||
|
trainer.log_metrics("eval", metrics)
|
||||||
|
trainer.save_metrics("eval", metrics)
|
||||||
|
|
||||||
|
kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-generation"}
|
||||||
|
if data_args.dataset_name is not None:
|
||||||
|
kwargs["dataset_tags"] = data_args.dataset_name
|
||||||
|
if data_args.dataset_config_name is not None:
|
||||||
|
kwargs["dataset_args"] = data_args.dataset_config_name
|
||||||
|
kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
|
||||||
|
else:
|
||||||
|
kwargs["dataset"] = data_args.dataset_name
|
||||||
|
|
||||||
|
if training_args.push_to_hub:
|
||||||
|
trainer.push_to_hub(**kwargs)
|
||||||
|
else:
|
||||||
|
trainer.create_model_card(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def _mp_fn(index):
|
||||||
|
# For xla_spawn (TPUs)
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
913
examples/pytorch/language-modeling/run_fim_no_trainer.py
Normal file
913
examples/pytorch/language-modeling/run_fim_no_trainer.py
Normal file
@@ -0,0 +1,913 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
"""
|
||||||
|
Fine-tuning the library models for causal language modeling using
|
||||||
|
Fill-in-the middle (FIM) objective on a text file or a dataset without using HuggingFace Trainer.
|
||||||
|
|
||||||
|
Here is the full list of checkpoints on the hub that can be fine-tuned by this script:
|
||||||
|
https://huggingface.co/models?filter=text-generation
|
||||||
|
"""
|
||||||
|
# You can also adapt this script on your own fim causal language modeling task. Pointers for this are left as comments.
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
from itertools import chain
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import datasets
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
from accelerate import Accelerator, DistributedType
|
||||||
|
from accelerate.logging import get_logger
|
||||||
|
from accelerate.utils import set_seed
|
||||||
|
from datasets import load_dataset
|
||||||
|
from huggingface_hub import Repository, create_repo
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
from tqdm.auto import tqdm
|
||||||
|
|
||||||
|
import transformers
|
||||||
|
from transformers import (
|
||||||
|
CONFIG_MAPPING,
|
||||||
|
MODEL_MAPPING,
|
||||||
|
AutoConfig,
|
||||||
|
AutoModelForCausalLM,
|
||||||
|
AutoTokenizer,
|
||||||
|
SchedulerType,
|
||||||
|
default_data_collator,
|
||||||
|
get_scheduler,
|
||||||
|
is_deepspeed_zero3_enabled,
|
||||||
|
is_torch_tpu_available,
|
||||||
|
)
|
||||||
|
from transformers.utils import check_min_version, send_example_telemetry
|
||||||
|
from transformers.utils.versions import require_version
|
||||||
|
|
||||||
|
|
||||||
|
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
|
||||||
|
check_min_version("4.36.0.dev0")
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt")
|
||||||
|
|
||||||
|
MODEL_CONFIG_CLASSES = list(MODEL_MAPPING.keys())
|
||||||
|
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Finetune a transformers model on a causal language modeling task using fill-in-the middle objective"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--dataset_name",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The name of the dataset to use (via the datasets library).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--dataset_config_name",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The configuration name of the dataset to use (via the datasets library).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--train_file", type=str, default=None, help="A csv, txt or a json file containing the training data."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--validation_file", type=str, default=None, help="A csv, txt or a json file containing the validation data."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--validation_split_percentage",
|
||||||
|
default=5,
|
||||||
|
help="The percentage of the train set used as validation set in case there's no validation split",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--model_name_or_path",
|
||||||
|
type=str,
|
||||||
|
help="Path to pretrained model or model identifier from huggingface.co/models.",
|
||||||
|
required=False,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--config_name",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Pretrained config name or path if not the same as model_name",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--tokenizer_name",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Pretrained tokenizer name or path if not the same as model_name",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--use_slow_tokenizer",
|
||||||
|
action="store_true",
|
||||||
|
help="If passed, will use a slow tokenizer (not backed by the 🤗 Tokenizers library).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--per_device_train_batch_size",
|
||||||
|
type=int,
|
||||||
|
default=8,
|
||||||
|
help="Batch size (per device) for the training dataloader.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--per_device_eval_batch_size",
|
||||||
|
type=int,
|
||||||
|
default=8,
|
||||||
|
help="Batch size (per device) for the evaluation dataloader.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--learning_rate",
|
||||||
|
type=float,
|
||||||
|
default=5e-5,
|
||||||
|
help="Initial learning rate (after the potential warmup period) to use.",
|
||||||
|
)
|
||||||
|
parser.add_argument("--weight_decay", type=float, default=0.0, help="Weight decay to use.")
|
||||||
|
parser.add_argument("--num_train_epochs", type=int, default=3, help="Total number of training epochs to perform.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--max_train_steps",
|
||||||
|
type=int,
|
||||||
|
default=None,
|
||||||
|
help="Total number of training steps to perform. If provided, overrides num_train_epochs.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--gradient_accumulation_steps",
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
help="Number of updates steps to accumulate before performing a backward/update pass.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--lr_scheduler_type",
|
||||||
|
type=SchedulerType,
|
||||||
|
default="linear",
|
||||||
|
help="The scheduler type to use.",
|
||||||
|
choices=["linear", "cosine", "cosine_with_restarts", "polynomial", "constant", "constant_with_warmup"],
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--num_warmup_steps", type=int, default=0, help="Number of steps for the warmup in the lr scheduler."
|
||||||
|
)
|
||||||
|
parser.add_argument("--output_dir", type=str, default=None, help="Where to store the final model.")
|
||||||
|
parser.add_argument("--seed", type=int, default=42, help="A seed for reproducible training.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--model_type",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Model type to use if training from scratch.",
|
||||||
|
choices=MODEL_TYPES,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--block_size",
|
||||||
|
type=int,
|
||||||
|
default=None,
|
||||||
|
help=(
|
||||||
|
"Optional input sequence length after tokenization. The training dataset will be truncated in block of"
|
||||||
|
" this size for training. Default to the model max input length for single sentence inputs (take into"
|
||||||
|
" account special tokens)."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_rate",
|
||||||
|
type=float,
|
||||||
|
default=0.5,
|
||||||
|
help=(
|
||||||
|
" Optional probability with which the FIM transformation is applied to the example."
|
||||||
|
" Default is 0.5. A rate of 1.0 means every example will undergo FIM transformation,"
|
||||||
|
" while a rate of 0.0 means no example will."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_spm_rate",
|
||||||
|
type=float,
|
||||||
|
default=0.5,
|
||||||
|
help=(
|
||||||
|
"Within the examples undergoing FIM transformation, this rate determines the probability"
|
||||||
|
" of applying the Sentence Permutation Mode (SPM)."
|
||||||
|
" Default is 0.5. A rate of 1.0 means all FIM transformations will use SPM,"
|
||||||
|
" while a rate of 0.0 means none will."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--truncate_or_pad",
|
||||||
|
type=bool,
|
||||||
|
default=True,
|
||||||
|
help=(
|
||||||
|
"Indicates whether the transformed example should be truncated or padded to maintain"
|
||||||
|
" the same length as the original example."
|
||||||
|
" Default is True. If False, the function will not truncate or pad the examples."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_prefix_token",
|
||||||
|
type=str,
|
||||||
|
default="<fim_prefix>",
|
||||||
|
help="Fill-in-Middle Prefix token. Defaults to '<fim_prefix>'.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_middle_token",
|
||||||
|
type=str,
|
||||||
|
default="<fim_middle>",
|
||||||
|
help="Fill-in-Middle Middle token. Defaults to '<fim_middle>'.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_suffix_token",
|
||||||
|
type=str,
|
||||||
|
default="<fim_suffix>",
|
||||||
|
help="Fill-in-Middle Middle token. Defaults to '<fim_suffix>'.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fim_pad_token",
|
||||||
|
type=str,
|
||||||
|
default="<fim_pad>",
|
||||||
|
help=(
|
||||||
|
"Fill-in-Middle Pad token. Used only when 'truncate_or_pad' is set to True." " Defaults to '<fim_pad>'."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--preprocessing_num_workers",
|
||||||
|
type=int,
|
||||||
|
default=None,
|
||||||
|
help="The number of processes to use for the preprocessing.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--overwrite_cache", action="store_true", help="Overwrite the cached training and evaluation sets"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--no_keep_linebreaks", action="store_true", help="Do not keep line breaks when using TXT files."
|
||||||
|
)
|
||||||
|
parser.add_argument("--push_to_hub", action="store_true", help="Whether or not to push the model to the Hub.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`."
|
||||||
|
)
|
||||||
|
parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--trust_remote_code",
|
||||||
|
type=bool,
|
||||||
|
default=False,
|
||||||
|
help=(
|
||||||
|
"Whether or not to allow for custom models defined on the Hub in their own modeling files. This option"
|
||||||
|
"should only be set to `True` for repositories you trust and in which you have read the code, as it will "
|
||||||
|
"execute code present on the Hub on your local machine."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--checkpointing_steps",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--resume_from_checkpoint",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="If the training should continue from a checkpoint folder.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--with_tracking",
|
||||||
|
action="store_true",
|
||||||
|
help="Whether to enable experiment trackers for logging.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--report_to",
|
||||||
|
type=str,
|
||||||
|
default="all",
|
||||||
|
help=(
|
||||||
|
'The integration to report the results and logs to. Supported platforms are `"tensorboard"`,'
|
||||||
|
' `"wandb"`, `"comet_ml"` and `"clearml"`. Use `"all"` (default) to report to all integrations. '
|
||||||
|
"Only applicable when `--with_tracking` is passed."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--low_cpu_mem_usage",
|
||||||
|
action="store_true",
|
||||||
|
help=(
|
||||||
|
"It is an option to create the model as an empty shell, then only materialize its parameters when the pretrained weights are loaded. "
|
||||||
|
"If passed, LLM loading time and RAM consumption will be benefited."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Sanity checks
|
||||||
|
if args.dataset_name is None and args.train_file is None and args.validation_file is None:
|
||||||
|
raise ValueError("Need either a dataset name or a training/validation file.")
|
||||||
|
else:
|
||||||
|
if args.train_file is not None:
|
||||||
|
extension = args.train_file.split(".")[-1]
|
||||||
|
if extension not in ["csv", "json", "txt"]:
|
||||||
|
raise ValueError("`train_file` should be a csv, json or txt file.")
|
||||||
|
if args.validation_file is not None:
|
||||||
|
extension = args.validation_file.split(".")[-1]
|
||||||
|
if extension not in ["csv", "json", "txt"]:
|
||||||
|
raise ValueError("`validation_file` should be a csv, json or txt file.")
|
||||||
|
|
||||||
|
if args.push_to_hub:
|
||||||
|
if args.output_dir is None:
|
||||||
|
raise ValueError("Need an `output_dir` to create a repo when `--push_to_hub` is passed.")
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args()
|
||||||
|
|
||||||
|
# Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The
|
||||||
|
# information sent is the one passed as arguments along with your Python/PyTorch versions.
|
||||||
|
send_example_telemetry("run_fim_no_trainer", args)
|
||||||
|
|
||||||
|
# Initialize the accelerator. We will let the accelerator handle device placement for us in this example.
|
||||||
|
# If we're using tracking, we also need to initialize it here and it will by default pick up all supported trackers
|
||||||
|
# in the environment
|
||||||
|
accelerator_log_kwargs = {}
|
||||||
|
|
||||||
|
if args.with_tracking:
|
||||||
|
accelerator_log_kwargs["log_with"] = args.report_to
|
||||||
|
accelerator_log_kwargs["project_dir"] = args.output_dir
|
||||||
|
|
||||||
|
accelerator = Accelerator(gradient_accumulation_steps=args.gradient_accumulation_steps, **accelerator_log_kwargs)
|
||||||
|
|
||||||
|
# Make one log on every process with the configuration for debugging.
|
||||||
|
logging.basicConfig(
|
||||||
|
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
|
||||||
|
datefmt="%m/%d/%Y %H:%M:%S",
|
||||||
|
level=logging.INFO,
|
||||||
|
)
|
||||||
|
logger.info(accelerator.state, main_process_only=False)
|
||||||
|
if accelerator.is_local_main_process:
|
||||||
|
datasets.utils.logging.set_verbosity_warning()
|
||||||
|
transformers.utils.logging.set_verbosity_info()
|
||||||
|
else:
|
||||||
|
datasets.utils.logging.set_verbosity_error()
|
||||||
|
transformers.utils.logging.set_verbosity_error()
|
||||||
|
|
||||||
|
# If passed along, set the training seed now.
|
||||||
|
if args.seed is not None:
|
||||||
|
set_seed(args.seed)
|
||||||
|
# Set a numpy random state for FIM transformations
|
||||||
|
np_rng = np.random.RandomState(seed=args.seed)
|
||||||
|
else:
|
||||||
|
# Still set a random state for FIM transformations
|
||||||
|
np_rng = np.random.RandomState(seed=42)
|
||||||
|
|
||||||
|
# Handle the repository creation
|
||||||
|
if accelerator.is_main_process:
|
||||||
|
if args.push_to_hub:
|
||||||
|
# Retrieve of infer repo_name
|
||||||
|
repo_name = args.hub_model_id
|
||||||
|
if repo_name is None:
|
||||||
|
repo_name = Path(args.output_dir).absolute().name
|
||||||
|
# Create repo and retrieve repo_id
|
||||||
|
repo_id = create_repo(repo_name, exist_ok=True, token=args.hub_token).repo_id
|
||||||
|
# Clone repo locally
|
||||||
|
repo = Repository(args.output_dir, clone_from=repo_id, token=args.hub_token)
|
||||||
|
|
||||||
|
with open(os.path.join(args.output_dir, ".gitignore"), "w+") as gitignore:
|
||||||
|
if "step_*" not in gitignore:
|
||||||
|
gitignore.write("step_*\n")
|
||||||
|
if "epoch_*" not in gitignore:
|
||||||
|
gitignore.write("epoch_*\n")
|
||||||
|
elif args.output_dir is not None:
|
||||||
|
os.makedirs(args.output_dir, exist_ok=True)
|
||||||
|
accelerator.wait_for_everyone()
|
||||||
|
|
||||||
|
# Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below)
|
||||||
|
# or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/
|
||||||
|
# (the dataset will be downloaded automatically from the datasets Hub).
|
||||||
|
#
|
||||||
|
# For CSV/JSON files, this script will use the column called 'text' or the first column if no column called
|
||||||
|
# 'text' is found. You can easily tweak this behavior (see below).
|
||||||
|
#
|
||||||
|
# In distributed training, the load_dataset function guarantee that only one local process can concurrently
|
||||||
|
# download the dataset.
|
||||||
|
if args.dataset_name is not None:
|
||||||
|
# Downloading and loading a dataset from the hub.
|
||||||
|
raw_datasets = load_dataset(args.dataset_name, args.dataset_config_name)
|
||||||
|
if "validation" not in raw_datasets.keys():
|
||||||
|
raw_datasets["validation"] = load_dataset(
|
||||||
|
args.dataset_name,
|
||||||
|
args.dataset_config_name,
|
||||||
|
split=f"train[:{args.validation_split_percentage}%]",
|
||||||
|
)
|
||||||
|
raw_datasets["train"] = load_dataset(
|
||||||
|
args.dataset_name,
|
||||||
|
args.dataset_config_name,
|
||||||
|
split=f"train[{args.validation_split_percentage}%:]",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
data_files = {}
|
||||||
|
dataset_args = {}
|
||||||
|
if args.train_file is not None:
|
||||||
|
data_files["train"] = args.train_file
|
||||||
|
if args.validation_file is not None:
|
||||||
|
data_files["validation"] = args.validation_file
|
||||||
|
extension = args.train_file.split(".")[-1]
|
||||||
|
if extension == "txt":
|
||||||
|
extension = "text"
|
||||||
|
dataset_args["keep_linebreaks"] = not args.no_keep_linebreaks
|
||||||
|
raw_datasets = load_dataset(extension, data_files=data_files, **dataset_args)
|
||||||
|
# If no validation data is there, validation_split_percentage will be used to divide the dataset.
|
||||||
|
if "validation" not in raw_datasets.keys():
|
||||||
|
raw_datasets["validation"] = load_dataset(
|
||||||
|
extension,
|
||||||
|
data_files=data_files,
|
||||||
|
split=f"train[:{args.validation_split_percentage}%]",
|
||||||
|
**dataset_args,
|
||||||
|
)
|
||||||
|
raw_datasets["train"] = load_dataset(
|
||||||
|
extension,
|
||||||
|
data_files=data_files,
|
||||||
|
split=f"train[{args.validation_split_percentage}%:]",
|
||||||
|
**dataset_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
# See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
|
||||||
|
# https://huggingface.co/docs/datasets/loading_datasets.html.
|
||||||
|
|
||||||
|
# Load pretrained model and tokenizer
|
||||||
|
#
|
||||||
|
# In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently
|
||||||
|
# download model & vocab.
|
||||||
|
if args.config_name:
|
||||||
|
config = AutoConfig.from_pretrained(
|
||||||
|
args.config_name,
|
||||||
|
trust_remote_code=args.trust_remote_code,
|
||||||
|
)
|
||||||
|
elif args.model_name_or_path:
|
||||||
|
config = AutoConfig.from_pretrained(
|
||||||
|
args.model_name_or_path,
|
||||||
|
trust_remote_code=args.trust_remote_code,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
config = CONFIG_MAPPING[args.model_type]()
|
||||||
|
logger.warning("You are instantiating a new config instance from scratch.")
|
||||||
|
|
||||||
|
if args.tokenizer_name:
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(
|
||||||
|
args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code
|
||||||
|
)
|
||||||
|
elif args.model_name_or_path:
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(
|
||||||
|
args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
"You are instantiating a new tokenizer from scratch. This is not supported by this script. "
|
||||||
|
"You can do it from another script, save it, and load it from here, using --tokenizer_name."
|
||||||
|
)
|
||||||
|
|
||||||
|
if args.model_name_or_path:
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
args.model_name_or_path,
|
||||||
|
from_tf=bool(".ckpt" in args.model_name_or_path),
|
||||||
|
config=config,
|
||||||
|
low_cpu_mem_usage=args.low_cpu_mem_usage,
|
||||||
|
trust_remote_code=args.trust_remote_code,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.info("Training new model from scratch")
|
||||||
|
model = AutoModelForCausalLM.from_config(config, trust_remote_code=args.trust_remote_code)
|
||||||
|
|
||||||
|
# Add the new FIM tokens to the tokenizer and resize model's vocab embeddings
|
||||||
|
special_tokens = [args.fim_prefix_token, args.fim_middle_token, args.fim_suffix_token]
|
||||||
|
if args.truncate_or_pad:
|
||||||
|
special_tokens.append(args.fim_pad_token)
|
||||||
|
|
||||||
|
# Get the factor by which the embedding layer should be padded based on the device
|
||||||
|
pad_factor = 1
|
||||||
|
if torch.cuda.is_availble():
|
||||||
|
pad_factor = 8
|
||||||
|
|
||||||
|
elif is_torch_tpu_available():
|
||||||
|
pad_factor = 128
|
||||||
|
|
||||||
|
# Add the new tokens to the tokenizer
|
||||||
|
tokenizer.add_tokens(special_tokens)
|
||||||
|
original_embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
if is_deepspeed_zero3_enabled():
|
||||||
|
import deepspeed
|
||||||
|
|
||||||
|
with deepspeed.zero.GatheredParameters(original_embeddings.weight, modifier_rank=0):
|
||||||
|
# Get the pre-expansion embeddings of the model and resize the embedding layer
|
||||||
|
model.resize_token_embeddings(len(tokenizer), pad_to_multiple_of=pad_factor)
|
||||||
|
embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
# Sample the embeddings for the new tokens from a multivariate normal distribution
|
||||||
|
# We do this so that the new embeddings are close to the original embeddings and not necessarily zero
|
||||||
|
# More on this: https://nlp.stanford.edu/~johnhew/vocab-expansion.html
|
||||||
|
mean = original_embeddings.mean(dim=0)
|
||||||
|
n = original_embeddings.size()[0]
|
||||||
|
sigma = ((original_embeddings - mean).T @ (original_embeddings - mean)) / n
|
||||||
|
dist = torch.distributions.multivariate_normal.MultivariateNormal(
|
||||||
|
mean,
|
||||||
|
covariance_matrix=1e-5 * sigma,
|
||||||
|
)
|
||||||
|
new_token_embeddings = torch.stack(
|
||||||
|
tuple((dist.sample() for _ in range(len(special_tokens)))),
|
||||||
|
dim=0,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
original_embeddings = model.get_input_embeddings()
|
||||||
|
# Get the pre-expansion embeddings of the model and resize the embedding layer
|
||||||
|
model.resize_token_embeddings(len(tokenizer), pad_to_multiple_of=pad_factor)
|
||||||
|
embeddings = model.get_input_embeddings()
|
||||||
|
|
||||||
|
# Sample the embeddings for the new tokens from a multivariate normal distribution
|
||||||
|
# We do this so that the new embeddings are close to the original embeddings and not necessarily zero
|
||||||
|
# More on this: https://nlp.stanford.edu/~johnhew/vocab-expansion.html
|
||||||
|
mean = original_embeddings.mean(dim=0)
|
||||||
|
n = original_embeddings.size()[0]
|
||||||
|
sigma = ((original_embeddings - mean).T @ (original_embeddings - mean)) / n
|
||||||
|
dist = torch.distributions.multivariate_normal.MultivariateNormal(
|
||||||
|
mean,
|
||||||
|
covariance_matrix=1e-5 * sigma,
|
||||||
|
)
|
||||||
|
new_token_embeddings = torch.stack(
|
||||||
|
tuple((dist.sample() for _ in range(len(special_tokens)))),
|
||||||
|
dim=0,
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_deepspeed_zero3_enabled():
|
||||||
|
import deepspeed
|
||||||
|
|
||||||
|
with deepspeed.zero.GatheredParameters(embeddings.weight, modifier_rank=0):
|
||||||
|
# Set the new tokens' embeddings to the newly sampled embeddings
|
||||||
|
embeddings.weight.data[-len(special_tokens) :] = new_token_embeddings
|
||||||
|
else:
|
||||||
|
# Set the new tokens' embeddings to the newly sampled embeddings
|
||||||
|
embeddings.weight.data[-len(special_tokens) :] = new_token_embeddings
|
||||||
|
|
||||||
|
# Update the model's embeddings with the new embeddings
|
||||||
|
model.set_input_embeddings(embeddings)
|
||||||
|
|
||||||
|
logger.info("Added special tokens to the tokenizer and resized model's embedding layer")
|
||||||
|
|
||||||
|
# Preprocessing the datasets.
|
||||||
|
# First we tokenize all the texts.
|
||||||
|
column_names = raw_datasets["train"].column_names
|
||||||
|
text_column_name = "text" if "text" in column_names else column_names[0]
|
||||||
|
|
||||||
|
def tokenize_function(examples):
|
||||||
|
return tokenizer(examples[text_column_name])
|
||||||
|
|
||||||
|
with accelerator.main_process_first():
|
||||||
|
tokenized_datasets = raw_datasets.map(
|
||||||
|
tokenize_function,
|
||||||
|
batched=True,
|
||||||
|
num_proc=args.preprocessing_num_workers,
|
||||||
|
remove_columns=column_names,
|
||||||
|
load_from_cache_file=not args.overwrite_cache,
|
||||||
|
desc="Running tokenizer on dataset",
|
||||||
|
)
|
||||||
|
|
||||||
|
if args.block_size is None:
|
||||||
|
block_size = tokenizer.model_max_length
|
||||||
|
if block_size > config.max_position_embeddings:
|
||||||
|
logger.warning(
|
||||||
|
f"The tokenizer picked seems to have a very large `model_max_length` ({tokenizer.model_max_length}). "
|
||||||
|
f"Using block_size={min(1024, config.max_position_embeddings)} instead. You can change that default value by passing --block_size xxx."
|
||||||
|
)
|
||||||
|
block_size = min(1024, config.max_position_embeddings)
|
||||||
|
else:
|
||||||
|
if args.block_size > tokenizer.model_max_length:
|
||||||
|
logger.warning(
|
||||||
|
f"The block_size passed ({args.block_size}) is larger than the maximum length for the model "
|
||||||
|
f"({tokenizer.model_max_length}). Using block_size={tokenizer.model_max_length}."
|
||||||
|
)
|
||||||
|
block_size = min(args.block_size, tokenizer.model_max_length)
|
||||||
|
|
||||||
|
# Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size.
|
||||||
|
def group_texts(examples):
|
||||||
|
# Concatenate all texts.
|
||||||
|
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
|
||||||
|
total_length = len(concatenated_examples[list(examples.keys())[0]])
|
||||||
|
# We drop the small remainder, and if the total_length < block_size we exclude this batch and return an empty dict.
|
||||||
|
# We could add padding if the model supported it instead of this drop, you can customize this part to your needs.
|
||||||
|
total_length = (total_length // block_size) * block_size
|
||||||
|
# Split by chunks of max_len.
|
||||||
|
result = {
|
||||||
|
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
|
||||||
|
for k, t in concatenated_examples.items()
|
||||||
|
}
|
||||||
|
result["labels"] = result["input_ids"].copy()
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Get the FIM-specific token ids
|
||||||
|
prefix_tok_id = tokenizer.convert_tokens_to_ids(args.fim_prefix_token)
|
||||||
|
middle_tok_id = tokenizer.convert_tokens_to_ids(args.fim_middle_token)
|
||||||
|
suffix_tok_id = tokenizer.convert_tokens_to_ids(args.fim_suffix_token)
|
||||||
|
pad_tok_id = None
|
||||||
|
|
||||||
|
# If truncate_or_pad is on, also get pad token id
|
||||||
|
if args.truncate_or_pad:
|
||||||
|
pad_tok_id = tokenizer.convert_tokens_to_ids(args.fim_pad_token)
|
||||||
|
|
||||||
|
# The two functions below perform the FIM transformation on the data (either PSM or SPM or PSM+SPM)
|
||||||
|
# Don't call fim_transform directly in .map()
|
||||||
|
# Adapted from https://github.com/loubnabnl/santacoder-finetuning/blob/main/fim.py#L22C13-L83
|
||||||
|
def fim_transform(example):
|
||||||
|
"""
|
||||||
|
This function performs FIM transformation on a single example (list of tokens)
|
||||||
|
"""
|
||||||
|
if np_rng.binomial(1, args.fim_rate):
|
||||||
|
boundaries = sorted(np_rng.randint(low=0, high=len(example) + 1, size=2))
|
||||||
|
|
||||||
|
prefix = example[: boundaries[0]]
|
||||||
|
middle = example[boundaries[0] : boundaries[1]]
|
||||||
|
suffix = example[boundaries[1] :]
|
||||||
|
|
||||||
|
if args.truncate_or_pad:
|
||||||
|
total_length = len(prefix) + len(middle) + len(suffix) + 3
|
||||||
|
diff = total_length - len(example)
|
||||||
|
if diff > 0:
|
||||||
|
suffix = suffix[: max(0, len(suffix) - diff)]
|
||||||
|
elif diff < 0:
|
||||||
|
suffix.extend([pad_tok_id] * (-diff))
|
||||||
|
|
||||||
|
if np_rng.binomial(1, args.fim_spm_rate):
|
||||||
|
# Apply Suffix-Prefix-Middle (SPM) transformation
|
||||||
|
transformed_example = [prefix_tok_id, suffix_tok_id] + suffix + [middle_tok_id] + prefix + middle
|
||||||
|
else:
|
||||||
|
# Apply Prefix-Suffix-Middle (PSM) transformation
|
||||||
|
transformed_example = [prefix_tok_id] + prefix + [suffix_tok_id] + suffix + [middle_tok_id] + middle
|
||||||
|
else:
|
||||||
|
transformed_example = example
|
||||||
|
|
||||||
|
return transformed_example
|
||||||
|
|
||||||
|
# Below function is the one you are supposed to call in the .map() function
|
||||||
|
def apply_fim(examples):
|
||||||
|
"""
|
||||||
|
Apply FIM transformation to a batch of examples
|
||||||
|
"""
|
||||||
|
fim_transform_ids = [fim_transform(ids) for ids in examples["input_ids"]]
|
||||||
|
examples["input_ids"] = fim_transform_ids
|
||||||
|
examples["labels"] = fim_transform_ids
|
||||||
|
# If your application requires custom attention mask, please adjust this function's below line.
|
||||||
|
# Since FIM transformation increases the number of tokens in input_ids and labels
|
||||||
|
# but leaves the number of tokens unchanged in attention_masks which would cause problems
|
||||||
|
examples["attention_mask"] = [[1] * len(mask) for mask in examples["input_ids"]]
|
||||||
|
return examples
|
||||||
|
|
||||||
|
# Note that with `batched=True`, this map processes 1,000 texts together, so group_texts throws away a remainder
|
||||||
|
# for each of those groups of 1,000 texts. You can adjust that batch_size here but a higher value might be slower
|
||||||
|
# to preprocess.
|
||||||
|
#
|
||||||
|
# To speed up this part, we use multiprocessing. See the documentation of the map method for more information:
|
||||||
|
# https://huggingface.co/docs/datasets/process#map
|
||||||
|
|
||||||
|
# FIM transformations are only supposed to be applied before group_texts processing otherwise some sentences will
|
||||||
|
# have 3-4 more tokens than others due to probabilistic addition of FIM-specific tokens which will raise errors
|
||||||
|
with accelerator.main_process_first():
|
||||||
|
fim_datasets = tokenized_datasets.map(
|
||||||
|
apply_fim,
|
||||||
|
batched=True,
|
||||||
|
num_proc=args.preprocessing_num_workers,
|
||||||
|
load_from_cache_file=not args.overwrite_cache,
|
||||||
|
desc="Performing FIM transformation",
|
||||||
|
)
|
||||||
|
lm_datasets = fim_datasets.map(
|
||||||
|
group_texts,
|
||||||
|
batched=True,
|
||||||
|
num_proc=args.preprocessing_num_workers,
|
||||||
|
load_from_cache_file=not args.overwrite_cache,
|
||||||
|
desc=f"Grouping texts in chunks of {block_size}",
|
||||||
|
)
|
||||||
|
|
||||||
|
train_dataset = lm_datasets["train"]
|
||||||
|
eval_dataset = lm_datasets["validation"]
|
||||||
|
|
||||||
|
# Log a few random samples from the training set:
|
||||||
|
for index in random.sample(range(len(train_dataset)), 3):
|
||||||
|
logger.info(f"Sample {index} of the training set: {train_dataset[index]}.")
|
||||||
|
|
||||||
|
# DataLoaders creation:
|
||||||
|
train_dataloader = DataLoader(
|
||||||
|
train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=args.per_device_train_batch_size
|
||||||
|
)
|
||||||
|
eval_dataloader = DataLoader(
|
||||||
|
eval_dataset, collate_fn=default_data_collator, batch_size=args.per_device_eval_batch_size
|
||||||
|
)
|
||||||
|
|
||||||
|
# Optimizer
|
||||||
|
# Split weights in two groups, one with weight decay and the other not.
|
||||||
|
no_decay = ["bias", "layer_norm.weight"]
|
||||||
|
optimizer_grouped_parameters = [
|
||||||
|
{
|
||||||
|
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
|
||||||
|
"weight_decay": args.weight_decay,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
|
||||||
|
"weight_decay": 0.0,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
optimizer = torch.optim.AdamW(optimizer_grouped_parameters, lr=args.learning_rate)
|
||||||
|
|
||||||
|
# Scheduler and math around the number of training steps.
|
||||||
|
overrode_max_train_steps = False
|
||||||
|
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
|
||||||
|
if args.max_train_steps is None:
|
||||||
|
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
|
||||||
|
overrode_max_train_steps = True
|
||||||
|
|
||||||
|
lr_scheduler = get_scheduler(
|
||||||
|
name=args.lr_scheduler_type,
|
||||||
|
optimizer=optimizer,
|
||||||
|
num_warmup_steps=args.num_warmup_steps * args.gradient_accumulation_steps,
|
||||||
|
num_training_steps=args.max_train_steps * args.gradient_accumulation_steps,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Prepare everything with our `accelerator`.
|
||||||
|
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = accelerator.prepare(
|
||||||
|
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler
|
||||||
|
)
|
||||||
|
|
||||||
|
# On TPU, the tie weights in our model have been disconnected, so we need to restore the ties.
|
||||||
|
if accelerator.distributed_type == DistributedType.TPU:
|
||||||
|
model.tie_weights()
|
||||||
|
|
||||||
|
# We need to recalculate our total training steps as the size of the training dataloader may have changed.
|
||||||
|
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
|
||||||
|
if overrode_max_train_steps:
|
||||||
|
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
|
||||||
|
# Afterwards we recalculate our number of training epochs
|
||||||
|
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
|
||||||
|
|
||||||
|
# Figure out how many steps we should save the Accelerator states
|
||||||
|
checkpointing_steps = args.checkpointing_steps
|
||||||
|
if checkpointing_steps is not None and checkpointing_steps.isdigit():
|
||||||
|
checkpointing_steps = int(checkpointing_steps)
|
||||||
|
|
||||||
|
# We need to initialize the trackers we use, and also store our configuration.
|
||||||
|
# The trackers initializes automatically on the main process.
|
||||||
|
if args.with_tracking:
|
||||||
|
experiment_config = vars(args)
|
||||||
|
# TensorBoard cannot log Enums, need the raw value
|
||||||
|
experiment_config["lr_scheduler_type"] = experiment_config["lr_scheduler_type"].value
|
||||||
|
accelerator.init_trackers("fim_no_trainer", experiment_config)
|
||||||
|
|
||||||
|
# Train!
|
||||||
|
total_batch_size = args.per_device_train_batch_size * accelerator.num_processes * args.gradient_accumulation_steps
|
||||||
|
|
||||||
|
logger.info("***** Running training *****")
|
||||||
|
logger.info(f" Num examples = {len(train_dataset)}")
|
||||||
|
logger.info(f" Num Epochs = {args.num_train_epochs}")
|
||||||
|
logger.info(f" Instantaneous batch size per device = {args.per_device_train_batch_size}")
|
||||||
|
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}")
|
||||||
|
logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}")
|
||||||
|
logger.info(f" Total optimization steps = {args.max_train_steps}")
|
||||||
|
# Only show the progress bar once on each machine.
|
||||||
|
progress_bar = tqdm(range(args.max_train_steps), disable=not accelerator.is_local_main_process)
|
||||||
|
completed_steps = 0
|
||||||
|
starting_epoch = 0
|
||||||
|
|
||||||
|
# Potentially load in the weights and states from a previous save
|
||||||
|
if args.resume_from_checkpoint:
|
||||||
|
if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "":
|
||||||
|
checkpoint_path = args.resume_from_checkpoint
|
||||||
|
path = os.path.basename(args.resume_from_checkpoint)
|
||||||
|
else:
|
||||||
|
# Get the most recent checkpoint
|
||||||
|
dirs = [f.name for f in os.scandir(os.getcwd()) if f.is_dir()]
|
||||||
|
dirs.sort(key=os.path.getctime)
|
||||||
|
path = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last
|
||||||
|
checkpoint_path = path
|
||||||
|
path = os.path.basename(checkpoint_path)
|
||||||
|
|
||||||
|
accelerator.print(f"Resumed from checkpoint: {checkpoint_path}")
|
||||||
|
accelerator.load_state(checkpoint_path)
|
||||||
|
# Extract `epoch_{i}` or `step_{i}`
|
||||||
|
training_difference = os.path.splitext(path)[0]
|
||||||
|
|
||||||
|
if "epoch" in training_difference:
|
||||||
|
starting_epoch = int(training_difference.replace("epoch_", "")) + 1
|
||||||
|
resume_step = None
|
||||||
|
completed_steps = starting_epoch * num_update_steps_per_epoch
|
||||||
|
else:
|
||||||
|
# need to multiply `gradient_accumulation_steps` to reflect real steps
|
||||||
|
resume_step = int(training_difference.replace("step_", "")) * args.gradient_accumulation_steps
|
||||||
|
starting_epoch = resume_step // len(train_dataloader)
|
||||||
|
completed_steps = resume_step // args.gradient_accumulation_steps
|
||||||
|
resume_step -= starting_epoch * len(train_dataloader)
|
||||||
|
|
||||||
|
# update the progress_bar if load from checkpoint
|
||||||
|
progress_bar.update(completed_steps)
|
||||||
|
|
||||||
|
for epoch in range(starting_epoch, args.num_train_epochs):
|
||||||
|
model.train()
|
||||||
|
if args.with_tracking:
|
||||||
|
total_loss = 0
|
||||||
|
if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
|
||||||
|
# We skip the first `n` batches in the dataloader when resuming from a checkpoint
|
||||||
|
active_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||||
|
else:
|
||||||
|
active_dataloader = train_dataloader
|
||||||
|
for step, batch in enumerate(active_dataloader):
|
||||||
|
with accelerator.accumulate(model):
|
||||||
|
outputs = model(**batch)
|
||||||
|
loss = outputs.loss
|
||||||
|
# We keep track of the loss at each epoch
|
||||||
|
if args.with_tracking:
|
||||||
|
total_loss += loss.detach().float()
|
||||||
|
accelerator.backward(loss)
|
||||||
|
optimizer.step()
|
||||||
|
lr_scheduler.step()
|
||||||
|
optimizer.zero_grad()
|
||||||
|
|
||||||
|
# Checks if the accelerator has performed an optimization step behind the scenes
|
||||||
|
if accelerator.sync_gradients:
|
||||||
|
progress_bar.update(1)
|
||||||
|
completed_steps += 1
|
||||||
|
|
||||||
|
if isinstance(checkpointing_steps, int):
|
||||||
|
if completed_steps % checkpointing_steps == 0:
|
||||||
|
output_dir = f"step_{completed_steps}"
|
||||||
|
if args.output_dir is not None:
|
||||||
|
output_dir = os.path.join(args.output_dir, output_dir)
|
||||||
|
accelerator.save_state(output_dir)
|
||||||
|
if completed_steps >= args.max_train_steps:
|
||||||
|
break
|
||||||
|
|
||||||
|
model.eval()
|
||||||
|
losses = []
|
||||||
|
for step, batch in enumerate(eval_dataloader):
|
||||||
|
with torch.no_grad():
|
||||||
|
outputs = model(**batch)
|
||||||
|
|
||||||
|
loss = outputs.loss
|
||||||
|
losses.append(accelerator.gather_for_metrics(loss.repeat(args.per_device_eval_batch_size)))
|
||||||
|
|
||||||
|
losses = torch.cat(losses)
|
||||||
|
try:
|
||||||
|
eval_loss = torch.mean(losses)
|
||||||
|
perplexity = math.exp(eval_loss)
|
||||||
|
except OverflowError:
|
||||||
|
perplexity = float("inf")
|
||||||
|
|
||||||
|
logger.info(f"epoch {epoch}: perplexity: {perplexity} eval_loss: {eval_loss}")
|
||||||
|
|
||||||
|
if args.with_tracking:
|
||||||
|
accelerator.log(
|
||||||
|
{
|
||||||
|
"perplexity": perplexity,
|
||||||
|
"eval_loss": eval_loss,
|
||||||
|
"train_loss": total_loss.item() / len(train_dataloader),
|
||||||
|
"epoch": epoch,
|
||||||
|
"step": completed_steps,
|
||||||
|
},
|
||||||
|
step=completed_steps,
|
||||||
|
)
|
||||||
|
|
||||||
|
if args.push_to_hub and epoch < args.num_train_epochs - 1:
|
||||||
|
accelerator.wait_for_everyone()
|
||||||
|
unwrapped_model = accelerator.unwrap_model(model)
|
||||||
|
unwrapped_model.save_pretrained(
|
||||||
|
args.output_dir, is_main_process=accelerator.is_main_process, save_function=accelerator.save
|
||||||
|
)
|
||||||
|
if accelerator.is_main_process:
|
||||||
|
tokenizer.save_pretrained(args.output_dir)
|
||||||
|
repo.push_to_hub(
|
||||||
|
commit_message=f"Training in progress epoch {epoch}", blocking=False, auto_lfs_prune=True
|
||||||
|
)
|
||||||
|
|
||||||
|
if args.checkpointing_steps == "epoch":
|
||||||
|
output_dir = f"epoch_{epoch}"
|
||||||
|
if args.output_dir is not None:
|
||||||
|
output_dir = os.path.join(args.output_dir, output_dir)
|
||||||
|
accelerator.save_state(output_dir)
|
||||||
|
|
||||||
|
if args.with_tracking:
|
||||||
|
accelerator.end_training()
|
||||||
|
|
||||||
|
if args.output_dir is not None:
|
||||||
|
accelerator.wait_for_everyone()
|
||||||
|
unwrapped_model = accelerator.unwrap_model(model)
|
||||||
|
unwrapped_model.save_pretrained(
|
||||||
|
args.output_dir, is_main_process=accelerator.is_main_process, save_function=accelerator.save
|
||||||
|
)
|
||||||
|
if accelerator.is_main_process:
|
||||||
|
tokenizer.save_pretrained(args.output_dir)
|
||||||
|
if args.push_to_hub:
|
||||||
|
repo.push_to_hub(commit_message="End of training", auto_lfs_prune=True)
|
||||||
|
|
||||||
|
with open(os.path.join(args.output_dir, "all_results.json"), "w") as f:
|
||||||
|
json.dump({"perplexity": perplexity}, f)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user