Repository navigation
Expand file tree
/
Copy pathparameter_freeze_checker.py
More file actions
228 lines (195 loc) 路 7.89 KB
/
Copy pathparameter_freeze_checker.py
File metadata and controls
228 lines (195 loc) 路 7.89 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
import torch
import argparse
# import contexttimer
import datasets
from datasets import load_dataset
from src.transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
from src.transformers import GPTNeoXForCausalLM
from src.transformers import LlamaConfig, LlamaPreTrainedModel
from tqdm import tqdm
# from sampling.utils import norm_logits, sample
import torch.nn.functional as F
from src.transformers.generation.logits_process import LogitsProcessorList
import time
import numpy as np
from termcolor import colored
from src.transformers import Trainer, TrainingArguments
from torch import nn
from src.transformers import DataCollatorForLanguageModeling
from src.transformers.generation.utils import GenerationConfig
from src.transformers.models.llama.modeling_llama import LlamaForCausalLM, SimpleSmallModel
from src.transformers.models.llama.modeling_llama import LlamaCausalLMWeirdTwo
from src.transformers.models.llama.modeling_llama import LlamaWeirdLarge
import time
from torch.utils.data import random_split
from src.transformers import BitsAndBytesConfig
from packaging import version
from src.transformers.modeling_utils import PreTrainedModel, load_sharded_checkpoint, unwrap_model
from src.transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_MAPPING_NAMES
import datetime
import os
import inspect
# # cache_dir = "/home/bc20/yang/"
# dir_dataset = "/home/yangzho6/c4_parts"
# dir_models = "/home/yangzho6/model_checkpoints2"
# dir_sdata = "/home/yangzho6/c4llm_synthesized/"
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
try:
import wandb
has_wandb = True
except ImportError:
has_wandb = False
# has_wandb = False # disable for debugging
from src.transformers.utils import (
ADAPTER_CONFIG_NAME,
ADAPTER_SAFE_WEIGHTS_NAME,
ADAPTER_WEIGHTS_NAME,
CONFIG_NAME,
SAFE_WEIGHTS_INDEX_NAME,
SAFE_WEIGHTS_NAME,
WEIGHTS_INDEX_NAME,
WEIGHTS_NAME,
PushInProgress,
can_return_loss,
find_labels,
is_accelerate_available,
is_apex_available,
is_bitsandbytes_available,
is_datasets_available,
is_in_notebook,
is_ipex_available,
is_peft_available,
is_safetensors_available,
is_sagemaker_dp_enabled,
is_sagemaker_mp_enabled,
is_torch_compile_available,
is_torch_neuroncore_available,
is_torch_tpu_available,
logging,
strtobool,
)
from src.transformers.trainer_pt_utils import (
DistributedTensorGatherer,
IterableDatasetShard,
LabelSmoother,
LengthGroupedSampler,
SequentialDistributedSampler,
distributed_broadcast_scalars,
distributed_concat,
find_batch_size,
get_dataloader_sampler,
get_model_param_count,
get_module_class_from_name,
get_parameter_names,
nested_concat,
nested_detach,
nested_numpify,
nested_xla_mesh_reduce,
reissue_pt_warnings,
remove_dummy_checkpoint,
)
from src.transformers.trainer_utils import (
PREFIX_CHECKPOINT_DIR,
BestRun,
EvalLoopOutput,
EvalPrediction,
FSDPOption,
HPSearchBackend,
HubStrategy,
IntervalStrategy,
PredictionOutput,
RemoveColumnsCollator,
TrainerMemoryTracker,
TrainOutput,
default_compute_objective,
denumpify_detensorize,
enable_full_determinism,
find_executable_batch_size,
get_last_checkpoint,
has_length,
neftune_post_forward_hook,
number_of_arguments,
seed_worker,
set_seed,
speed_metrics,
)
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
from torch.utils.data import DataLoader, Dataset, RandomSampler, SequentialSampler
from src.transformers.integrations.deepspeed import deepspeed_init, deepspeed_load_checkpoint, is_deepspeed_available
if is_apex_available():
from apex import amp
if is_torch_tpu_available(check_device=False):
import torch_xla.core.xla_model as xm
import torch_xla.debug.metrics as met
if is_accelerate_available():
from accelerate import Accelerator, skip_first_batches
from accelerate import __version__ as accelerate_version
from accelerate.utils import DistributedDataParallelKwargs, GradientAccumulationPlugin
if version.parse(accelerate_version) > version.parse("0.20.3"):
from accelerate.utils import (
load_fsdp_model,
load_fsdp_optimizer,
save_fsdp_model,
save_fsdp_optimizer,
)
DATA_SAMPLERS = [RandomSampler]
if version.parse(accelerate_version) > version.parse("0.23.0"):
from accelerate.data_loader import SeedableRandomSampler
DATA_SAMPLERS += [SeedableRandomSampler]
if is_deepspeed_available():
from accelerate.utils import DeepSpeedSchedulerWrapper
if is_peft_available():
from peft import PeftModel
import subprocess
def get_git_commit_hash():
try:
# Run the git command to get the current commit hash
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
# Decode from bytes to string
return commit_hash.decode('utf-8')
except subprocess.CalledProcessError:
# Handle cases where the git command fails (e.g., not a git repository)
return None
commit_hash = get_git_commit_hash()[: 7] # only 7 digits
print("the commit hash is {}".format(commit_hash))
import datetime
hash_of_time = str(datetime.datetime.now()).split('.')[-1]
print("the hash of time is {}".format(hash_of_time))
import socket
hostname = socket.gethostname()
print("Hostname:", hostname)
if "lovelace" in hostname:
# cache_dir = "/home/bc20/yang/transformersprofiling"
dir_models = "/home/yangzho6/model_checkpoints/"
dir_sdata = "/home/yangzho6/c4llm_synthesized/"
dir_unprocessed_dataset = "/home/yangzho6/c4_parts/downloads/"
elif "ada" in hostname:
# cache_dir = "/home/bc20/yang/transformersprofiling"
dir_models = "/home/beidic/yangzho6/model_checkpoints/"
dir_sdata = "/home/beidic/yangzho6/c4llm_synthesized/"
dir_unprocessed_dataset = "/home/beidic/yangzho6/c4_parts/downloads/"
else:
# cache_dir = "/home/bc20/yang/transformersprofiling"
dir_models = "/home/yangzho6/model_checkpoints/"
dir_sdata = "/home/yangzho6/c4llm_synthesized/"
logger = logging.get_logger(__name__)
# large_model_checkpoint = "/home/yangzho6/model_checkpoints/largemodelopenllama3b_a36f01d_866695"
# large_model_checkpoint = "/home/yangzho6/model_checkpoints/largemodelopenllama3b_a36f01d_866695/checkpoint-500"
large_model_checkpoint = "/home/yangzho6/model_checkpoints/largemodelopenllama3b_7b8b2cd_690836/checkpoint-5000"
small_model_checkpoint = "/home/yangzho6/model_checkpoints/llama-160m_deciphering_openallama3b_setting0_070600"
small_model = SimpleSmallModel.from_pretrained(small_model_checkpoint, sliding_window_length = 7, hostname = hostname, target_model_dim = 3200)
small_model_state_dict = small_model.state_dict()
large_model = None
large_model_state_dict = LlamaWeirdLarge.from_pretrained(large_model_checkpoint, sliding_window_length = 7, addonsmallmodel = small_model, use_mse_loss = False).state_dict()
# small_model_state_dict = SimpleSmallModel.from_pretrained(small_model_checkpoint).state_dict()
print("length of the large model: {} length of the small model state dict: {}".format(len(large_model_state_dict.keys()), len(small_model_state_dict.keys())))
output_together = True
for keys in small_model_state_dict.keys():
large_model_keys = "addonsmallmodel." + keys
tensor_output = torch.equal(large_model_state_dict[large_model_keys], small_model_state_dict[keys])
if not tensor_output:
print(colored("we have divergent parameter with keys, large model keys: {}, {}".format(large_model_keys, keys), "red"))
output_together = False
else:
print(colored("we have identical parameter with keys, large model keys: {}, {}".format(large_model_keys, keys), "green"))
print("output together: {}".format(output_together))