이 비교의 나머지 부분을 불러오는 중입니다.

fsdp_qdora_trainpy

생성일 비교 결과 만료 없음
9 삭제
570 행
8 추가
569 행
"""
"""
Read our announcement blog post: https://www.answer.ai/posts/2024-03-06-fsdp-qlora.html.
Read our announcement blog post: https://www.answer.ai/posts/2024-03-06-fsdp-qlora.html.


This script trains a model using FSDP with LoRA & QLoRA. It pulls inspiration from
This script trains a model using FSDP with LoRA & QLoRA. It pulls inspiration from
- llama-recipes (https://github.com/facebookresearch/llama-recipes/blob/main/src/llama_recipes/finetuning.py)
- llama-recipes (https://github.com/facebookresearch/llama-recipes/blob/main/src/llama_recipes/finetuning.py)
- PyTorch FSDP docs (https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html)
- PyTorch FSDP docs (https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html)
- bitsandbytes (https://github.com/TimDettmers/bitsandbytes)
- bitsandbytes (https://github.com/TimDettmers/bitsandbytes)


For information on the different arguments, run `python train.py --help`
For information on the different arguments, run `python train.py --help`


You should treat this script as an alpha/preview release. If you're not comfortable with testing and debugging
You should treat this script as an alpha/preview release. If you're not comfortable with testing and debugging
models, we'd suggest holding off for a few months while the community more fully tests the approach.
models, we'd suggest holding off for a few months while the community more fully tests the approach.
"""
"""


# Imports
# Imports


# General
# General
import copy
import copy
import functools
import functools
import gc
import gc
import math
import math
import os
import os
import sys
import sys
import time
import time
import types
import types
from contextlib import nullcontext
from contextlib import nullcontext
from glob import glob
from glob import glob
from pathlib import Path
from pathlib import Path
from typing import Dict, List
from typing import Dict, List


import bitsandbytes as bnb
import bitsandbytes as bnb
import safetensors
import safetensors
import torch
import torch
import torch.distributed as dist
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.multiprocessing as mp
import torch.optim as optim
import torch.optim as optim
from accelerate import init_empty_weights
from accelerate import init_empty_weights
from accelerate.utils import set_seed
from accelerate.utils import set_seed


# Model loading
# Model loading
from bitsandbytes.nn import Linear4bit, Params4bit
from bitsandbytes.nn import Linear4bit, Params4bit
from fastcore.parallel import parallel
from fastcore.parallel import parallel


# Argument parsing
# Argument parsing
from fastcore.script import Param, bool_arg, call_parse
from fastcore.script import Param, bool_arg, call_parse
from packaging.version import parse
from packaging.version import parse
from safetensors.torch import save_file
from safetensors.torch import save_file


# Torch + distributed training
# Torch + distributed training
from torch import Tensor, nn
from torch import Tensor, nn
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
CheckpointImpl,
CheckpointImpl,
apply_activation_checkpointing,
apply_activation_checkpointing,
checkpoint_wrapper,
checkpoint_wrapper,
offload_wrapper,
offload_wrapper,
)
)


# FSDP
# FSDP
from torch.distributed.fsdp import FullStateDictConfig, MixedPrecision, StateDictType
from torch.distributed.fsdp import FullStateDictConfig, MixedPrecision, StateDictType
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
from torch.distributed.fsdp.api import BackwardPrefetch, CPUOffload, ShardingStrategy
from torch.distributed.fsdp.api import BackwardPrefetch, CPUOffload, ShardingStrategy
from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler
from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler
from torch.distributed.fsdp.wrap import (
from torch.distributed.fsdp.wrap import (
_or_policy,
_or_policy,
lambda_auto_wrap_policy,
lambda_auto_wrap_policy,
transformer_auto_wrap_policy,
transformer_auto_wrap_policy,
)
)
from torch.nn.utils.rnn import pad_sequence
from torch.nn.utils.rnn import pad_sequence
from torch.optim.lr_scheduler import LambdaLR
from torch.optim.lr_scheduler import LambdaLR
from torch.profiler import ProfilerActivity, profile, record_function
from torch.profiler import ProfilerActivity, profile, record_function
from torch.utils.data import DataLoader, Dataset, DistributedSampler
from torch.utils.data import DataLoader, Dataset, DistributedSampler
from tqdm.auto import tqdm
from tqdm.auto import tqdm
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from transformers.optimization import get_linear_schedule_with_warmup
from transformers.optimization import get_linear_schedule_with_warmup
from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
from transformers.utils import SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, hub
from transformers.utils import SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, hub


try:
try:
from hqq.core.quantize import BaseQuantizeConfig, HQQBackend, HQQLinear
from hqq.core.quantize import BaseQuantizeConfig, HQQBackend, HQQLinear
except ImportError:
except ImportError:
HQQLinear = None
HQQLinear = None
pass
pass


# To add a new model, import the transformer, attention, & MLP layers
# To add a new model, import the transformer, attention, & MLP layers
# for the wrapping policy and `check_fn` in activation checkpointing
# for the wrapping policy and `check_fn` in activation checkpointing
from transformers.models.llama.modeling_llama import (
from transformers.models.llama.modeling_llama import (
LLAMA_ATTENTION_CLASSES,
LlamaAttention,
LlamaDecoderLayer,
LlamaDecoderLayer,
LlamaMLP,
LlamaMLP,
)
)
from transformers.models.mistral.modeling_mistral import (
from transformers.models.mistral.modeling_mistral import (
MISTRAL_ATTENTION_CLASSES,
MistralAttention,
MistralDecoderLayer,
MistralDecoderLayer,
MistralMLP,
MistralMLP,
)
)


# To get rid of tokenizers warnings for now
# To get rid of tokenizers warnings for now
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["TOKENIZERS_PARALLELISM"] = "false"


# For logging things during training
# For logging things during training