-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfinetune8bh200.py
More file actions
943 lines (812 loc) · 39 KB
/
finetune8bh200.py
File metadata and controls
943 lines (812 loc) · 39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
#!/usr/bin/env python3
"""
Fine-tune Gemma-3-1B model with system/user/assistant message format.
Supports both single GPU and multi-GPU training via torchrun.
"""
import copy
import os
# import re
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.profiler import profile, ProfilerActivity
import json
from datasets import load_dataset
import shutil
from transformers import (
AutoConfig,
AutoTokenizer,
AutoModelForCausalLM,
TrainingArguments,
TrainerCallback,
Trainer,
DataCollatorForLanguageModeling
)
from peft import LoraConfig, get_peft_model, TaskType
import argparse
from pathlib import Path
def get_messages(model_name, messages):
if "google/gemma" in model_name:
full_messages = copy.deepcopy(messages)[1:3]
full_messages[0]["content"] = messages[0]["content"] + "\n\n" + full_messages[0]["content"]
return full_messages
else:
return messages
def get_user_messages(model_name, messages):
return copy.deepcopy(messages)[1:2]
# gsm8k_pattern = re.compile(r"\n#### (.+)$")
def get_assistant_messages(model_name, dataset, messages):
# if dataset.startswith("gsm8k"):
# messages = copy.deepcopy(messages)
# gt_match = re.search(gsm8k_pattern, messages[2]["content"])
# gt_answer = None if not gt_match else gt_match.group(1)
# if gt_answer:
# messages[2]["content"] = messages[2]["content"].replace(gt_answer, "")
if "google/gemma" in model_name:
assistant_messages = copy.deepcopy(messages)[2:3]
assistant_messages[0]["role"] = "user"
return assistant_messages
else:
return messages[2:3]
def load_and_prepare_dataset(data_file, tokenizer, model_name,
max_length=2048, debug=0, predictors=0, regular=False, train_all=False,
plain=False):
"""Load JSONL dataset and format for training with proper label masking"""
# Load dataset
dataset = load_dataset('json', data_files=data_file)['train']
if torch.cuda.current_device() == 0:
print(f"Loaded {len(dataset)} examples from {data_file}")
def tokenize_conversations(examples):
"""Tokenize conversations and mask input tokens properly"""
input_ids_list = []
labels_list = []
attention_mask_list = []
user_input_ids_list = []
user_labels_list = []
user_attention_mask_list = []
assistant_input_ids_list = []
assistant_labels_list = []
assistant_attention_mask_list = []
for messages in examples['messages']:
# Apply chat template if available, otherwise format manually
full_messages = get_messages(model_name, messages)
if plain:
if train_all:
formatted_chat = messages[1]["content"] + "<|eot_id|>"
else:
formatted_chat = messages[1]["content"] + "<|perception|>" + messages[2]["content"] + "<|eot_id|>"
else:
formatted_chat = tokenizer.apply_chat_template(
full_messages,
tokenize=False,
add_generation_prompt=False,
)
# Tokenize the formatted conversation with padding to max_length
tokenized = tokenizer(
formatted_chat,
truncation=True,
max_length=max_length,
padding="max_length", # Pad to max_length for consistent tensor shapes
return_tensors=None
)
input_ids = tokenized["input_ids"]
attention_mask = tokenized["attention_mask"]
# Create labels with proper masking
if train_all:
labels = create_labels_for_all(input_ids, attention_mask)
else:
labels = create_masked_labels(messages, tokenizer, input_ids, attention_mask)
input_ids_list.append(input_ids)
labels_list.append(labels)
attention_mask_list.append(attention_mask)
user_messages = get_user_messages(model_name, messages)
to_add = predictors
while to_add > 0:
user_messages[0]["content"] += f"<|predictor_{to_add}|>"
to_add -= 1
if plain:
formatted_chat_user = user_messages[0]["content"]
else:
formatted_chat_user = tokenizer.apply_chat_template(
user_messages,
tokenize=False,
add_generation_prompt=False,
)
tokenized_user = tokenizer(
formatted_chat_user,
truncation=True,
max_length=max_length,
padding="max_length", # Pad to max_length for consistent tensor shapes
return_tensors=None
)
user_input_ids_list.append(tokenized_user["input_ids"])
user_labels_list.append([-100] * len(tokenized_user["input_ids"]))
user_attention_mask_list.append(tokenized_user["attention_mask"])
assistant_messages = get_assistant_messages(model_name, data_file, messages)
if plain:
formatted_chat_assistant = assistant_messages[0]["content"]
else:
formatted_chat_assistant = tokenizer.apply_chat_template(
assistant_messages,
tokenize=False,
add_generation_prompt=False,
)
tokenized_assistant = tokenizer(
formatted_chat_assistant,
truncation=True,
max_length=max_length,
padding="max_length", # Pad to max_length for consistent tensor shapes
return_tensors=None
)
assistant_input_ids_list.append(tokenized_assistant["input_ids"])
assistant_labels_list.append([-100] * len(tokenized_assistant["input_ids"]))
assistant_attention_mask_list.append(tokenized_assistant["attention_mask"])
if debug == 3 and torch.cuda.current_device() == 0:
print(messages)
print(input_ids_list)
print(tokenizer.decode(input_ids_list[0]))
print(labels_list)
print(tokenizer.decode([item for item in labels_list[0] if item != -100]))
print(attention_mask_list)
print("user Token IDs:", tokenized_user["input_ids"])
print("user Decoded:", tokenizer.decode(tokenized_user["input_ids"]))
print("assistant Token IDs:", tokenized_assistant["input_ids"])
print("assistant Decoded:", tokenizer.decode(tokenized_assistant["input_ids"]))
if debug == 3:
exit(0)
if regular:
return {
"input_ids": input_ids_list,
"labels": labels_list,
"attention_mask": attention_mask_list,
}
else:
return {
"input_ids": input_ids_list,
"labels": labels_list,
"attention_mask": attention_mask_list,
"input_ids_user": user_input_ids_list,
"labels_user": user_labels_list,
"attention_mask_user": user_attention_mask_list,
"input_ids_assistant": assistant_input_ids_list,
"labels_assistant": assistant_labels_list,
"attention_mask_assistant": assistant_attention_mask_list,
}
# def format_messages_manually(messages):
# """Manual formatting when chat template is not available"""
# formatted_parts = []
# for msg in messages:
# role = msg['role']
# content = msg['content']
# if role == 'system':
# formatted_parts.append(f"<|system|>\n{content}")
# elif role == 'user':
# formatted_parts.append(f"<|user|>\n{content}")
# elif role == 'assistant':
# formatted_parts.append(f"<|assistant|>\n{content}")
# return "\n\n".join(formatted_parts) + "<|end|>"
def create_labels_for_all(input_ids, attention_mask):
"""
Create labels for all tokens except padding (mask those with -100).
"""
labels = []
for i, mask in enumerate(attention_mask):
if mask == 0: # Padding token
labels.append(-100)
else:
labels.append(input_ids[i])
return labels
def create_masked_labels(messages, tokenizer, input_ids, attention_mask):
"""Create labels with input tokens masked (-100)"""
labels = [-100] * len(input_ids)
# Mask padding tokens in labels
for i, mask in enumerate(attention_mask):
if mask == 0: # Padding token
labels[i] = -100
# Find assistant responses and unmask only those tokens
for msg in messages:
if msg['role'] == 'assistant':
assistant_content = msg['content']
# Find where this assistant response appears in the tokenized text
assistant_tokens = tokenizer.encode(assistant_content, add_special_tokens=False)
# Find the position of assistant response in input_ids
decoded_assistant = [tokenizer.decode(item) for item in assistant_tokens]
decoded_input = [tokenizer.decode(item) for item in input_ids]
for i in range(len(input_ids) - len(assistant_tokens) + 1):
# Only check non-padding tokens
if debug == 4 and torch.cuda.current_device() == 0:
print(f"=======input_ids: {input_ids[i:i+len(assistant_tokens)]}")
print(f"assistant_tokens: {assistant_tokens}")
# if attention_mask[i] == 1 and input_ids[i:i+len(assistant_tokens)] == assistant_tokens:
if attention_mask[i] == 1 and decoded_input[i:i+len(assistant_tokens)] == decoded_assistant:
# Unmask the assistant response tokens
for j in range(i, min(i + len(assistant_tokens), len(input_ids))):
if attention_mask[j] == 1: # Only unmask non-padding tokens
labels[j] = input_ids[j]
break
if debug == 4:
exit(0)
return labels
# Tokenize dataset
tokenized_dataset = dataset.map(
tokenize_conversations,
batched=True,
remove_columns=dataset.column_names
)
return tokenized_dataset
# def use_llama_3_2_chat_template(tokenizer):
# llama_3_2_chat_template = """{{- bos_token }}
# {%- if custom_tools is defined %}
# {%- set tools = custom_tools %}
# {%- endif %}
# {%- if not tools_in_user_message is defined %}
# {%- set tools_in_user_message = true %}
# {%- endif %}
# {%- if not date_string is defined %}
# {%- if strftime_now is defined %}
# {%- set date_string = strftime_now("%d %b %Y") %}
# {%- else %}
# {%- set date_string = "26 Jul 2024" %}
# {%- endif %}
# {%- endif %}
# {%- if not tools is defined %}
# {%- set tools = none %}
# {%- endif %}
# {#- This block extracts the system message, so we can slot it into the right place. #}
# {%- if messages[0]['role'] == 'system' %}
# {%- set system_message = messages[0]['content']|trim %}
# {%- set messages = messages[1:] %}
# {%- else %}
# {%- set system_message = "" %}
# {%- endif %}
# {#- System message #}
# {{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
# {%- if tools is not none %}
# {{- "Environment: ipython\n" }}
# {%- endif %}
# {{- "Cutting Knowledge Date: December 2023\n" }}
# {{- "Today Date: " + date_string + "\n\n" }}
# {%- if tools is not none and not tools_in_user_message %}
# {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
# {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
# {{- "Do not use variables.\n\n" }}
# {%- for t in tools %}
# {{- t | tojson(indent=4) }}
# {{- "\n\n" }}
# {%- endfor %}
# {%- endif %}
# {{- system_message }}
# {{- "<|eot_id|>" }}
# {#- Custom tools are passed in a user message with some extra guidance #}
# {%- if tools_in_user_message and not tools is none %}
# {#- Extract the first user message so we can plug it in here #}
# {%- if messages | length != 0 %}
# {%- set first_user_message = messages[0]['content']|trim %}
# {%- set messages = messages[1:] %}
# {%- else %}
# {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
# {%- endif %}
# {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
# {{- "Given the following functions, please respond with a JSON for a function call " }}
# {{- "with its proper arguments that best answers the given prompt.\n\n" }}
# {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
# {{- "Do not use variables.\n\n" }}
# {%- for t in tools %}
# {{- t | tojson(indent=4) }}
# {{- "\n\n" }}
# {%- endfor %}
# {{- first_user_message + "<|eot_id|>"}}
# {%- endif %}
# {%- for message in messages %}
# {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
# {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }}
# {%- elif 'tool_calls' in message %}
# {%- if not message.tool_calls|length == 1 %}
# {{- raise_exception("This model only supports single tool-calls at once!") }}
# {%- endif %}
# {%- set tool_call = message.tool_calls[0].function %}
# {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
# {{- '{"name": "' + tool_call.name + '", ' }}
# {{- '"parameters": ' }}
# {{- tool_call.arguments | tojson }}
# {{- "}" }}
# {{- "<|eot_id|>" }}
# {%- elif message.role == "tool" or message.role == "ipython" %}
# {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
# {%- if message.content is mapping or message.content is iterable %}
# {{- message.content | tojson }}
# {%- else %}
# {{- message.content }}
# {%- endif %}
# {{- "<|eot_id|>" }}
# {%- endif %}
# {%- endfor %}
# {%- if add_generation_prompt %}
# {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
# {%- endif %}
# """
# if tokenizer.chat_template != llama_3_2_chat_template:
# tokenizer.chat_template = llama_3_2_chat_template
def setup_model_and_tokenizer(model_name, use_lora=True, lora_rank=16, pretrain=False, debug=0, seed=None):
"""Setup model and tokenizer with optional LoRA"""
# Load tokenizer
if "apple/OpenELM" in model_name:
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
tokenizer.chat_template = "{% for message in messages %}\n{% if message['role'] == 'user' %}\n{{ '<|user|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'system' %}\n{{ '<|system|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'assistant' %}\n{{ '<|assistant|>\n' + message['content'] + eos_token }}\n{% endif %}\n{% if loop.last and add_generation_prompt %}\n{{ '<|assistant|>' }}\n{% endif %}\n{% endfor %}"
else:
tokenizer = AutoTokenizer.from_pretrained(model_name)
# use_llama_3_2_chat_template(tokenizer)
# Add special tokens if not present
if "microsoft/phi" in model_name:
tokenizer.add_special_tokens({"bos_token": "<|startoftext|>"})
if torch.cuda.current_device() == 0:
print("Added <|startoftext|> token")
special_tokens = ["<|predictor_1|>", "<|predictor_2|>", "<|predictor_3|>", "<|predictor_4|>", "<|predictor_5|>",
"<|predictor_6|>", "<|predictor_7|>", "<|predictor_8|>", "<|predictor_9|>", "<|predictor_10|>",
"<|start_header_id|>", "<|end_header_id|>", "<|eot_id|>", "<|perception|>"]
new_tokens = [token for token in special_tokens if token not in tokenizer.vocab]
if new_tokens:
tokenizer.add_special_tokens({"additional_special_tokens": new_tokens})
if torch.cuda.current_device() == 0:
print(f"Added {len(new_tokens)} new special tokens")
# Set pad token
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Load model with better device mapping for multi-GPU
device_map = None
if torch.cuda.is_available():
world_size = int(os.environ.get('WORLD_SIZE', 1))
if world_size == 1:
device_map = "auto"
else:
# For multi-GPU with torchrun, don't use device_map
device_map = None
if pretrain:
if seed is not None:
torch.manual_seed(seed)
config = AutoConfig.from_pretrained(model_name)
model = AutoModelForCausalLM.from_config(
config,
torch_dtype=torch.bfloat16,
)
rank = torch.distributed.get_rank()
device = torch.device(f"cuda:{rank}")
model.to(device)
for p in model.parameters():
torch.distributed.broadcast(p.data, src=0)
for b in model.buffers():
torch.distributed.broadcast(b.data, src=0)
if debug == 6:
for name, param in model.named_parameters():
print(f"Parameter name: {name}, Shape: {param.shape}")
print(param)
exit(0)
else:
if "google/gemma" in model_name:
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map=device_map,
trust_remote_code=True,
# Add these for better multi-GPU stability
low_cpu_mem_usage=True,
use_cache=False, # Disable KV cache for training
attn_implementation="eager",
)
else:
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map=device_map,
trust_remote_code=True,
# Add these for better multi-GPU stability
low_cpu_mem_usage=True,
use_cache=False, # Disable KV cache for training
)
if new_tokens:
model.resize_token_embeddings(len(tokenizer))
# Resize embeddings if we added new tokens
if new_tokens:
model.resize_token_embeddings(len(tokenizer))
# Setup LoRA if requested
if use_lora:
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
inference_mode=False,
r=lora_rank,
lora_alpha=lora_rank * 2,
lora_dropout=0.1,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
)
model = get_peft_model(model, lora_config)
model.enable_input_require_grads()
if torch.cuda.current_device() == 0:
model.print_trainable_parameters()
return model, tokenizer
class RepresentationTrainer(Trainer):
"""
Trainer to regularize representations.
"""
def __init__(self, *args, **kwargs):
# Extract custom loss parameters
self.lbd = kwargs.pop('lbd', 1.0)
self.gamma = kwargs.pop('gamma', 1.0)
self.last_token = kwargs.pop('last_token', -2)
self.debug = kwargs.pop('debug', 0)
super().__init__(*args, **kwargs)
def _last_token_index(self, input_ids, labels, attention_mask):
index = []
def unpad(input_ids, attention_mask):
result = []
can_break = False
for id, mask in zip(input_ids, attention_mask):
if mask != 0:
can_break = True
if mask == 0 and can_break:
break
result.append(id)
return result
for i in range(input_ids.shape[0]):
uii = unpad(input_ids[i], attention_mask[i])
if self.debug == 1 and torch.cuda.current_device() == 0:
print(f"====={len(uii)}=====")
print(input_ids[i][len(uii) - 4], input_ids[i][len(uii) - 3], input_ids[i][len(uii) - 2], input_ids[i][len(uii) - 1], -100 if len(uii) >= len(input_ids[i]) else input_ids[i][len(uii)])
print(labels[i][len(uii) - 4], labels[i][len(uii) - 3], labels[i][len(uii) - 2], labels[i][len(uii) - 1], -100 if len(uii) >= len(labels[i]) else labels[i][len(uii)])
print(attention_mask[i][len(uii) - 4], attention_mask[i][len(uii) - 3], attention_mask[i][len(uii) - 2], attention_mask[i][len(uii) - 1], -100 if len(uii) >= len(attention_mask[i]) else attention_mask[i][len(uii)])
index.append(len(uii) + self.last_token)
index_tensor = torch.tensor(index).to(input_ids.device)
if self.debug == 1 and torch.cuda.current_device() == 0:
print(index_tensor)
return index_tensor
def forward(self, model, inputs):
"""
Custom forward pass that handles all model calls.
"""
# Main forward pass for language modeling
llm_inputs = {
"input_ids": torch.cat([inputs["input_ids"],
inputs["input_ids_user"],
inputs["input_ids_assistant"]], dim=0),
"labels": torch.cat([inputs["labels"],
inputs["labels_user"],
inputs["labels_assistant"]], dim=0),
"attention_mask": torch.cat([inputs["attention_mask"],
inputs["attention_mask_user"],
inputs["attention_mask_assistant"]], dim=0),
}
if self.debug == 2 and torch.cuda.current_device() == 0:
print("=====before:outputs=====")
print("input_ids shapes:")
print(llm_inputs["input_ids"].shape)
print("labels shapes::")
print(llm_inputs["labels"].shape)
print("attention_mask shapes:")
print(llm_inputs["attention_mask"].shape)
with torch.set_grad_enabled(True):
outputs = model(**llm_inputs, output_hidden_states=True)
if self.debug == 2 and torch.cuda.current_device() == 0:
print(f"=====outputs.loss.shape:{outputs.loss.shape}=====")
print(f"=====outputs.hidden_states[-1].shape:{outputs.hidden_states[-1].shape}=====")
batch_size = llm_inputs["input_ids"].shape[0] // 3
user_hidden_states = outputs.hidden_states[-1][batch_size: batch_size * 2]
assistant_hidden_states = outputs.hidden_states[-1][batch_size * 2:]
if self.debug == 2 and torch.cuda.current_device() == 0:
print(f"====={user_hidden_states.shape}=====")
print(f"====={assistant_hidden_states.shape}=====")
# Return all outputs needed for loss computation
return {
'main_outputs': outputs,
'user_hidden_states': user_hidden_states,
'assistant_hidden_states': assistant_hidden_states,
}
def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
"""
Compute loss with additional regularization terms.
"""
# Get indeices
index_user = self._last_token_index(inputs["input_ids_user"], inputs["labels_user"], inputs["attention_mask_user"])
index_assistant = self._last_token_index(inputs["input_ids_assistant"], inputs["labels_assistant"], inputs["attention_mask_assistant"])
first_dim = inputs["input_ids_user"].shape[0]
if self.debug == 1 and torch.cuda.current_device() == 0:
print("=====last tokens=====")
print(inputs["input_ids_user"][range(first_dim), index_user])
print(inputs["input_ids_user"][range(first_dim), index_user - 1])
print(inputs["input_ids_assistant"][range(first_dim), index_assistant])
print(inputs["input_ids_assistant"][range(first_dim), index_assistant - 1])
# Get all forward pass results
forward_results = self.forward(model, inputs)
# Extract main language modeling loss
main_outputs = forward_results['main_outputs']
lm_loss = main_outputs.loss
# Compute representation similarity loss
user_hidden_states = forward_results['user_hidden_states']
assistant_hidden_states = forward_results['assistant_hidden_states']
# Get embeddings (using last token of each sequence)
user_embedding = user_hidden_states[range(first_dim), index_user, :]
assistant_embedding = assistant_hidden_states[range(first_dim), index_assistant, :]
# Compute cosine similarity
cosine_similarity = F.cosine_similarity(user_embedding, assistant_embedding, dim=-1)
if self.debug == 1 and torch.cuda.current_device() == 0:
print(user_embedding.shape, assistant_embedding.shape)
print(cosine_similarity.shape)
# Compute total loss
jepa_loss = 1.0 - torch.mean(cosine_similarity)
total_loss = self.gamma * lm_loss + self.lbd * jepa_loss
if self.debug == 2 and torch.cuda.current_device() == 0:
print(lm_loss, self.lbd, torch.mean(cosine_similarity))
if self.debug == 1 or self.debug == 2:
exit(0)
if self.debug == 5 and torch.cuda.current_device() == 0:
print(f"llm_loss: {lm_loss.float()}, jepa_loss: {jepa_loss.float()}")
return (total_loss, main_outputs) if return_outputs else total_loss
class ProfilerFLOPCallback(TrainerCallback):
def __init__(self, profile_steps=10):
self.profile_steps = profile_steps
self.total_flops = 0
def on_step_begin(self, args, state, control, **kwargs):
if state.global_step < self.profile_steps:
self.profiler = profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
record_shapes=True,
with_flops=True # This enables FLOP counting if available
)
self.profiler.__enter__()
def on_step_end(self, args, state, control, **kwargs):
if state.global_step < self.profile_steps:
self.profiler.__exit__(None, None, None)
# Extract FLOP information
events = self.profiler.key_averages()
step_flops = sum(event.flops for event in events if event.flops > 0)
self.total_flops += step_flops
if torch.cuda.current_device() == 0 and state.global_step == 1:
print(f"Step {state.global_step}: FLOPs: {step_flops:,.0f}")
def main():
parser = argparse.ArgumentParser(description="Fine-tune Gemma-3-1B")
parser.add_argument("--train_file", type=str, help="Path to training JSONL file")
parser.add_argument("--eval_file", type=str, help="Path to evaluation JSONL file")
parser.add_argument("--data_file", type=str, help="Path to single JSONL file (will be split)")
parser.add_argument("--model_name", type=str, default="meta-llama/Llama-3.2-1B-Instruct", help="Model name/path")
parser.add_argument("--output_dir", type=str, default="./llama3-1b-fted", help="Output directory")
parser.add_argument("--max_length", type=int, default=512, help="Maximum sequence length")
parser.add_argument("--batch_size", type=int, default=4, help="Per device batch size")
parser.add_argument("--grad_accum", type=int, default=4, help="Gradient accumulation steps")
parser.add_argument("--learning_rate", type=float, default=2e-5, help="Learning rate")
parser.add_argument("--num_epochs", type=int, default=3, help="Number of training epochs")
parser.add_argument("--eval_steps", type=int, default=10, help="Evaluation steps")
parser.add_argument("--lora", action="store_true", help="Enable LoRA (default: full fine-tuning)")
parser.add_argument("--lora_rank", type=int, default=16, help="LoRA rank. Default: 16.")
parser.add_argument("--eval_split", type=float, default=0.2, help="Evaluation split ratio (if using single data file)")
parser.add_argument("--split_seed", type=int, default=42, help="Random seed for train/eval split")
parser.add_argument("--finetune_seed", type=int, default=42, help="Random seed for fine-tuning")
parser.add_argument("--predictors", type=int, default=0, help="Number of predictor tokens")
parser.add_argument("--lbd", type=float, default=0.1, help="Lambda for similarity loss")
parser.add_argument("--gamma", type=float, default=1.0, help="Gamma for LLM loss")
parser.add_argument("--last_token", type=int, default=-1, help="Index of last token, -1 is '<|eot|>'")
parser.add_argument("--debug", type=int, default=0, help="Debug level. 0 means no debug")
parser.add_argument("--regular", action="store_true", help="Use regular transformer.")
parser.add_argument("--track_flop", action="store_true", help="Whether to track FLOPs.")
parser.add_argument("--pretrain", action="store_true", help="Whether to pretrain from scratch.")
parser.add_argument("--train_all", action="store_true", help="Whether to compute loss from all tokens.")
parser.add_argument("--plain", action="store_true", help="When set, do not apply chat format. If --train_all is not set, use `<|perceptioin|>` to connect query and answer. If --train_all is set, only train query.")
args = parser.parse_args()
# Validate arguments
if not args.train_file and not args.data_file:
parser.error("Must provide either --train_file or --data_file")
if args.train_file and args.data_file:
parser.error("Cannot use both --train_file and --data_file. Choose one.")
if torch.cuda.current_device() == 0:
print("=== Fine-tuning Script ===")
if torch.cuda.current_device() == 0:
if args.train_file:
print(f"Train file: {args.train_file}")
if args.eval_file:
print(f"Eval file: {args.eval_file}")
else:
print("No eval file provided - training without evaluation")
else:
print(f"Data file: {args.data_file} (will split {args.eval_split:.1%} for eval)")
print(f"Model: {args.model_name}")
print(f"Output: {args.output_dir}")
print(f"Using LoRA: {args.lora}")
print(f"LoRA rank: {args.lora_rank}")
# Check if running with torchrun
world_size = int(os.environ.get('WORLD_SIZE', 1))
local_rank = int(os.environ.get('LOCAL_RANK', 0))
if world_size > 1:
if torch.cuda.current_device() == 0:
print(f"Running with torchrun: world_size={world_size}, local_rank={local_rank}")
# Initialize distributed training
torch.distributed.init_process_group(backend='nccl')
torch.cuda.set_device(local_rank)
# Setup model and tokenizer
if torch.cuda.current_device() == 0:
print("\n1. Loading model and tokenizer...")
model, tokenizer = setup_model_and_tokenizer(
args.model_name, use_lora=args.lora, lora_rank=args.lora_rank, pretrain=args.pretrain,
debug=args.debug, seed=args.finetune_seed)
# Load and prepare dataset
if torch.cuda.current_device() == 0:
print("\n2. Loading and preparing dataset...")
if args.train_file:
# Load separate train and eval files
if torch.cuda.current_device() == 0:
print(f"Loading training data from {args.train_file}")
train_dataset = load_and_prepare_dataset(
args.train_file, tokenizer, args.model_name,
args.max_length, predictors=args.predictors, regular=args.regular,
debug=args.debug, train_all=args.train_all, plain=args.plain)
if args.eval_file:
if torch.cuda.current_device() == 0:
print(f"Loading evaluation data from {args.eval_file}")
eval_dataset = load_and_prepare_dataset(
args.eval_file, tokenizer, args.model_name,
args.max_length, regular=args.regular,
debug=args.debug, train_all=args.train_all, plain=args.plain)
else:
eval_dataset = None
if torch.cuda.current_device() == 0:
print("No evaluation file provided")
else:
# Load single file and split
if torch.cuda.current_device() == 0:
print(f"Loading data from {args.data_file} and splitting...")
full_dataset = load_and_prepare_dataset(
args.data_file, tokenizer, args.model_name,
args.max_length, predictors=args.predictors, regular=args.regular,
debug=args.debug, train_all=args.train_all, plain=args.plain)
if args.eval_split > 0:
split_dataset = full_dataset.train_test_split(
test_size=args.eval_split,
seed=args.split_seed,
shuffle=True
)
train_dataset = split_dataset['train']
eval_dataset = split_dataset['test']
else:
train_dataset = full_dataset
eval_dataset = None
# Print dataset info
if torch.cuda.current_device() == 0:
print(f"Train samples: {len(train_dataset)}")
if eval_dataset:
print(f"Eval samples: {len(eval_dataset)}")
else:
print("No evaluation dataset")
# Data collator - don't use padding since we already padded
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer,
mlm=False, # We're doing causal LM, not masked LM
pad_to_multiple_of=None, # We already padded to max_length
)
# Training arguments - optimized for multi-GPU stability
eval_steps = args.eval_steps if not args.pretrain else args.eval_steps * 20
output_dir = os.path.abspath(args.output_dir)
if torch.cuda.current_device() == 0:
shutil.rmtree(output_dir, ignore_errors=True)
os.makedirs(output_dir, exist_ok=True)
training_args = TrainingArguments(
output_dir=output_dir,
overwrite_output_dir=True,
# Training parameters
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.batch_size,
gradient_accumulation_steps=args.grad_accum,
learning_rate=args.learning_rate,
num_train_epochs=args.num_epochs,
# Evaluation
eval_strategy="no", # steps" if eval_dataset else "no",
# eval_steps=eval_steps,
# Saving
save_strategy="no", # steps",
# save_steps=eval_steps,
# save_total_limit=2,
# Logging
logging_dir=f"{args.output_dir}/logs",
logging_steps=args.eval_steps,
# Optimization - key changes for stability
fp16=False,
bf16=True,
gradient_checkpointing=True, # Enable for memory efficiency
dataloader_drop_last=True, # Drop last incomplete batch
# Memory optimization
dataloader_num_workers=0, # Avoid multiprocessing issues
# Multi-GPU settings - completely disable FSDP
ddp_find_unused_parameters=False,
ddp_backend="nccl" if world_size > 1 else None,
# Explicitly disable FSDP and sharding
fsdp="",
fsdp_config={},
# Other
report_to="none",
remove_unused_columns=False,
load_best_model_at_end=False, # True if eval_dataset else False,
# Disable problematic optimizations
tf32=False, # May help with stability
# Set seed for reproducibility
seed=args.finetune_seed,
data_seed=args.finetune_seed,
)
flop_callback = ProfilerFLOPCallback()
# Initialize trainer
if args.regular:
if torch.cuda.current_device() == 0:
print("\n3. Initializing regular trainer...")
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
callbacks=[flop_callback] if args.track_flop else [],
)
else:
if torch.cuda.current_device() == 0:
print("\n3. Initializing representation trainer...")
trainer = RepresentationTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
callbacks=[flop_callback] if args.track_flop else [],
lbd=args.lbd,
gamma=args.gamma,
last_token=args.last_token,
debug=args.debug,
)
if torch.cuda.current_device() == 0 and args.lora:
print("=== PEFT Model Check ===")
model.print_trainable_parameters()
# Check if any parameters require gradients
trainable_params = []
for name, param in model.named_parameters():
if param.requires_grad:
trainable_params.append(name)
print(f"Trainable parameters: {len(trainable_params)}")
if len(trainable_params) == 0:
print("ERROR: No parameters require gradients!")
else:
print("First few trainable params:", trainable_params[:5])
# Start training
if torch.cuda.current_device() == 0:
print("\n4. Starting training...")
try:
trainer.train()
except Exception as e:
if torch.cuda.current_device() == 0:
print(f"Training failed with error: {e}")
print("This might be due to FSDP/sharding issues. Try running with --lora flag for LoRA fine-tuning.")
raise
# Save final model
if torch.cuda.current_device() == 0:
print("\n5. Saving final model...")
def save_model(model):
if torch.cuda.current_device() == 0:
if args.lora:
model = model.merge_and_unload()
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
else:
trainer.save_model()
trainer.save_state()
tokenizer.save_pretrained(output_dir)
retry = 3
while retry > 0:
try:
save_model(model)
break
except Exception as e:
print(f"Success Rate: Saving model encounter error: {e}")
retry -= 1
if retry <= 0:
raise
time.sleep(10)
if torch.cuda.current_device() == 0:
print(f"\n✅ Training completed! Model saved to {args.output_dir}")
if torch.cuda.current_device() == 0:
print("\n🎉 Fine-tuning finished successfully!")
if __name__ == "__main__":
main()