-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama_double_eval.py
199 lines (156 loc) · 6.1 KB
/
llama_double_eval.py
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
import time
from transformer_heads import create_headed_qlora, load_lora_with_heads
import gc
import pickle
import utils
import arithmetic_coder
from torch.nn import functional as F
from datasets import load_dataset, Dataset, DatasetDict
from transformers import (
LlamaTokenizer,
LlamaForCausalLM,
Trainer,
BitsAndBytesConfig,
TrainingArguments,
GenerationConfig,
)
import constants
from transformer_heads.util.helpers import DataCollatorWithPadding, get_model_params
from peft import LoraConfig
from transformer_heads.config import HeadConfig
from transformer_heads.util.model import print_trainable_parameters
from transformer_heads.util.evaluate import (
evaluate_head_wise,
)
from transformer_heads import load_headed
import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy.lib.stride_tricks import sliding_window_view
import audioop
import os
import sys
from transformer_heads.util.evaluate import (
evaluate_head_wise,
get_top_n_preds,
get_some_preds,
)
from datasets import Dataset
from torch.utils.data import DataLoader
from tqdm import tqdm
from collections import defaultdict
import random
model_path = "meta-llama/Llama-2-7b-hf"
model_params = get_model_params(model_path)
model_class = model_params["model_class"]
hidden_size = model_params["hidden_size"]
vocab_size = model_params["vocab_size"]
quantization_config = BitsAndBytesConfig(
load_in_4bit=False,
load_in_8bit=True,
llm_int8_threshold=6.0,
llm_int8_has_fp16_weight=False,
bnb_4bit_compute_dtype=torch.float32,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
model = load_headed(
model_class,
model_path,
head_folder_path="test_with_lm_head/checkpoint-9375",
device_map="cuda",
quantization_config = quantization_config,
)
import logging
logging.config.dictConfig(constants.LOGGING_CONFIG)
logger = logging.getLogger()
pdf_llama = pd.DataFrame(0, index = np.arange(10), columns = constants.SNR_SET, dtype = np.float64)
pdf_reduced_head = pd.DataFrame(0, index = np.arange(10), columns = constants.SNR_SET, dtype = np.float64)
cw = 1024
runs = 500
#for scale_index in constants.SCALES:
#for scale_index in [0.9, 0.8, 0.7]:
for file_index in range(constants.NUM_TEST_FILES):
logger.info(f'file: {file_index}')
for snr_index in constants.SNR_SET:
#for snr_index in [999]:
logger.info(f'snr: {snr_index=}')
raw_data = np.fromfile(f'data/new_test/t{file_index:02}.data', dtype = np.int16)
data_iq = raw_data[0::2] + 1j*raw_data[1::2]
signal_real = np.real(data_iq).copy().astype(np.int16)
# Add noise here if any
#signal_real = (scale_index*np.real(data_iq).copy()).astype(np.int16)
if snr_index != 999:
signal_real = signal_real + np.random.normal(0,constants.SNR_SD_TABLE.loc[snr_index, 1],signal_real.shape).astype(np.int16)
signal_real = signal_real.tobytes()
biased = audioop.lin2lin(signal_real, 2, 1)
data = audioop.bias(biased, 1, 2**7)
ndata = np.frombuffer(data, dtype = np.uint8)
data = np.right_shift(ndata, 1)
data_split = sliding_window_view(data, cw+1).copy()
np.random.shuffle(data_split)
tokenizer = LlamaTokenizer.from_pretrained(model_path)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
testing_runs = runs
running_total = 0
lm_head_pdf_collect = list()
lm_head_symbol_collect = list()
reduced_pdf_collect = list()
reduced_symbol_collect = list()
raw_len = 0
start = time.time()
for idx in range(testing_runs):
selection = data_split[idx]
ascii_selection = ''.join(chr(x) for x in selection)
target = ascii_selection[-1]
ascii_select_d = ascii_selection[:-1]
tk = tokenizer.encode(ascii_select_d)
raw_len += len(tokenizer.decode(tk[-1]))
batch = tokenizer(ascii_select_d, return_tensors = 'pt')
with torch.no_grad():
output = model(**batch)
torch.cuda.empty_cache()
gc.collect()
with torch.no_grad():
torch.cuda.empty_cache()
reduced_pdf_collect.append(output['preds_by_head']['reduced_output'][0][-1].clone().detach())
reduced_symbol_collect.append(target)
lm_head_pdf_collect.append(output['preds_by_head']['lm_head'][0][-2].clone().detach())
lm_head_symbol_collect.append(tk[-1])
enc_output = list()
encoder = arithmetic_coder.Encoder(
base=2,
precision=32,
output_fn=enc_output.append,
)
for symbol, logits in zip(reduced_symbol_collect, reduced_pdf_collect):
dist = F.softmax(logits).numpy()
pdf = utils.normalize_pdf_for_arithmetic_coding(dist)
encoder.encode(pdf, ord(symbol))
encoder.terminate()
compressed_bits = ''.join(map(str, enc_output))
compressed_bytes, padding = utils.bits_to_bytes(compressed_bits)
clen = len(compressed_bytes) + testing_runs/8
pdf_reduced_head.loc[file_index, snr_index] = clen/testing_runs
enc_output = list()
encoder = arithmetic_coder.Encoder(
base=2,
precision=32,
output_fn=enc_output.append,
)
for symbol, logits in zip(lm_head_symbol_collect, lm_head_pdf_collect):
dist = F.softmax(logits).numpy()
pdf = utils.normalize_pdf_for_arithmetic_coding(dist)
encoder.encode(pdf, symbol)
encoder.terminate()
end = time.time()
compressed_bits = ''.join(map(str, enc_output))
compressed_bytes, padding = utils.bits_to_bytes(compressed_bits)
print(f'time taken was {end-start}')
clen = len(compressed_bytes) + raw_len/8
pdf_llama.loc[file_index, snr_index] = clen/raw_len
## save
pdf_llama.to_pickle('llama_results/pdf_llama_over_files_final.pkl')
pdf_reduced_head.to_pickle('llama_results/pdf_reduced_head_over_files_final.pkl')