-
Notifications
You must be signed in to change notification settings - Fork 162
Disable KD mode from saving problematic state #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
compute_environment: LOCAL_MACHINE | ||
debug: false | ||
distributed_type: FSDP | ||
downcast_bf16: 'no' | ||
enable_cpu_affinity: false | ||
fsdp_config: | ||
fsdp_activation_checkpointing: true | ||
fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP | ||
fsdp_cpu_ram_efficient_loading: true | ||
fsdp_offload_params: false | ||
fsdp_reshard_after_forward: true | ||
fsdp_state_dict_type: SHARDED_STATE_DICT | ||
fsdp_transformer_layer_cls_to_wrap: LlamaDecoderLayer | ||
fsdp_version: 2 | ||
machine_rank: 0 | ||
main_training_function: main | ||
mixed_precision: bf16 | ||
num_machines: 1 | ||
num_processes: gpu | ||
rdzv_backend: static | ||
same_network: true | ||
tpu_env: [] | ||
tpu_use_cluster: false | ||
tpu_use_sudo: false | ||
use_cpu: false |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,7 +21,6 @@ | |||||||||||||
import torch | ||||||||||||||
import torch.distributed | ||||||||||||||
import transformers | ||||||||||||||
from accelerate import PartialState | ||||||||||||||
from accelerate.logging import get_logger | ||||||||||||||
from transformers import AutoTokenizer | ||||||||||||||
from trl import SFTTrainer | ||||||||||||||
|
@@ -48,38 +47,28 @@ class TrainingArguments(transformers.TrainingArguments): | |||||||||||||
do_train: bool = True | ||||||||||||||
do_eval: bool = True | ||||||||||||||
save_strategy: str = "no" | ||||||||||||||
max_seq_length: int = 1024 | ||||||||||||||
max_length: int = 1024 | ||||||||||||||
optim: str = "adamw_torch" | ||||||||||||||
learning_rate: float = 1e-5 | ||||||||||||||
lr_scheduler_type: str = "cosine" | ||||||||||||||
dataloader_drop_last: bool = True | ||||||||||||||
dataset_num_proc: int = 8 | ||||||||||||||
dataset_batch_size: int = 500 | ||||||||||||||
bf16: bool = True | ||||||||||||||
tf32: bool = True | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def llama_text_format_func(sample): | ||||||||||||||
texts = [] | ||||||||||||||
for p, q, r in zip(sample["system_prompt"], sample["question"], sample["response"]): | ||||||||||||||
if not p: | ||||||||||||||
texts.append(f"<s>[INST] {q}[/INST]\n{r}</s>") | ||||||||||||||
else: | ||||||||||||||
texts.append(f"<s>[INST] <<SYS>>{p}<</SYS>>\n{q}[/INST]\n{r}</s>") | ||||||||||||||
return texts | ||||||||||||||
p, q, r = sample["system_prompt"], sample["question"], sample["response"] | ||||||||||||||
if not p: | ||||||||||||||
return f"<s>[INST] {q}[/INST]\n{r}</s>" | ||||||||||||||
else: | ||||||||||||||
return f"<s>[INST] <<SYS>>{p}<</SYS>>\n{q}[/INST]\n{r}</s>" | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
class KDSFTTrainer(SFTTrainer, KDTrainer): | ||||||||||||||
pass | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def _teacher_factory(model_name_or_path): | ||||||||||||||
return transformers.AutoModelForCausalLM.from_pretrained( | ||||||||||||||
model_name_or_path, | ||||||||||||||
device_map=PartialState().process_index, | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def train(): | ||||||||||||||
parser = transformers.HfArgumentParser((ModelArguments, TrainingArguments)) | ||||||||||||||
model_args, training_args = parser.parse_args_into_dataclasses() | ||||||||||||||
|
@@ -117,34 +106,31 @@ def train(): | |||||||||||||
|
||||||||||||||
if model_args.single_model: | ||||||||||||||
logger.info("Loading single model only...") | ||||||||||||||
model = _teacher_factory(model_path) | ||||||||||||||
model = transformers.AutoModelForCausalLM.from_pretrained( | ||||||||||||||
model_path, dtype=torch.bfloat16 if training_args.bf16 else None | ||||||||||||||
) | ||||||||||||||
logger.info("Model loaded.") | ||||||||||||||
else: | ||||||||||||||
logger.info("Loading student model...") | ||||||||||||||
model = transformers.AutoModelForCausalLM.from_pretrained( | ||||||||||||||
model_args.student_name_or_path, | ||||||||||||||
device_map=PartialState().process_index, | ||||||||||||||
model_args.student_name_or_path, dtype=torch.bfloat16 if training_args.bf16 else None | ||||||||||||||
) | ||||||||||||||
logger.info("Student loaded.") | ||||||||||||||
# Load checkpoint | ||||||||||||||
logger.info("Loading teacher model and converting to Distillation model...") | ||||||||||||||
teacher_model = transformers.AutoModelForCausalLM.from_pretrained( | ||||||||||||||
model_args.teacher_name_or_path, dtype=torch.bfloat16 if training_args.bf16 else None | ||||||||||||||
) | ||||||||||||||
Comment on lines
+121
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same kwarg bug for teacher load: use torch_dtype Keep teacher/student loads consistent. - teacher_model = transformers.AutoModelForCausalLM.from_pretrained(
- model_args.teacher_name_or_path, dtype=torch.bfloat16 if training_args.bf16 else None
- )
+ teacher_model = transformers.AutoModelForCausalLM.from_pretrained(
+ model_args.teacher_name_or_path, torch_dtype=torch.bfloat16 if training_args.bf16 else None
+ ) 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||
kd_config = { | ||||||||||||||
"teacher_model": ( | ||||||||||||||
_teacher_factory, | ||||||||||||||
(model_args.teacher_name_or_path,), | ||||||||||||||
{}, | ||||||||||||||
), | ||||||||||||||
"teacher_model": teacher_model, | ||||||||||||||
"criterion": LMLogitsLoss(), | ||||||||||||||
"expose_minimal_state_dict": False, # FSDP forces us to disable this | ||||||||||||||
AAnoosheh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
} | ||||||||||||||
model = mtd.convert(model, mode=[("kd_loss", kd_config)]) | ||||||||||||||
logger.info("Models converted.") | ||||||||||||||
|
||||||||||||||
# Fix problematic settings that logger.info excessive warnings | ||||||||||||||
model.generation_config.temperature = None | ||||||||||||||
model.generation_config.top_p = None | ||||||||||||||
if training_args.gradient_checkpointing: | ||||||||||||||
training_args.gradient_checkpointing_kwargs = {"use_reentrant": False} | ||||||||||||||
|
||||||||||||||
# Trainer | ||||||||||||||
trainer_cls = SFTTrainer if model_args.single_model else KDSFTTrainer | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pyarrow | ||
trl==0.13.0 | ||
trl>=0.23.0 |
Uh oh!
There was an error while loading. Please reload this page.