-
Notifications
You must be signed in to change notification settings - Fork 4
/
approach3_isoloss.py
187 lines (156 loc) · 6.58 KB
/
approach3_isoloss.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
'''
Use all the data points collected during the experiments.
We design the loss formula as:
lossu(Nnv,V,H) = -E + A1/[Nnv]^alpha1 + A2/[Vd]^alpha2 + B/[F/(6(Nv+Vd))]^beta
lossu is negative typically, so I set the constant -E instead of E
Then, we use the fitted (E, A1, alpha1, A2, alpha2, B, beta) to predict the optimal vocabulary size
by computing the solution of dlossu/dV=0.
Constraint: alpha1=beta, low < alpha1 < 1, low < beta < 1
'''
import numpy as np
import pandas as pd
from scipy.optimize import minimize
from scipy.special import huber
import pdb
import math
from scipy.optimize import fsolve
from pathlib import Path
from tqdm import tqdm
import matplotlib.pyplot as plt
from scipy.interpolate import griddata, interp1d
from sklearn.metrics import r2_score
import matplotlib.cm as cm
import warnings
import contextlib
from utils import (D_to_H, H_to_D, interpolate, func_flops, Nnv_to_d,
merge_nearest_flops, relative_mse)
np.random.seed(42) # For reproducibility
def dl_dv(V, Nnv, d, F):
term1 = 0 # Derivative of -E
term2 = 0 # Derivative of A1/[Nnv]^alpha1
term3 = -alpha2 * A2 * d / (V * d) ** (alpha2 + 1)
u = F / (6 * (Nnv + V * d))
du_dV = F * d / (6 * (Nnv + V * d) ** 2)
term4 = beta * B * du_dV / (u ** (beta + 1))
return term1 + term2 + term3 + term4
df = pd.read_csv('exp_data.csv')
V_data = df['vocab_size']
d_data = df['embed_dim']
H_data = df['num_characters']
Nnv_data = df['Non_vocab_parameters']
flops_data = df['FLOPs']
lossu_data = df['Lossu']
# We follow DeepMind Chinchilla's approach to omit the points with flops < 10e18
flops_idxs = np.argsort(flops_data)
flops_data = flops_data[flops_idxs]
num_points = np.sum(flops_data < 2.18 * 10 ** 17)
V_data = V_data[flops_idxs][num_points:]
H_data = H_data[flops_idxs][num_points:]
Nnv_data = Nnv_data[flops_idxs][num_points:]
d_data = d_data[flops_idxs][num_points:]
lossu_data = lossu_data[flops_idxs][num_points:]
## normalization
V_data = V_data / 1_000
H_data = H_data / 1_000_000_000
Nnv_data = Nnv_data / 1_000_000
d_data = d_data / 1000
print('Num of points for fitting: ', len(lossu_data))
def LSE(params, Nnv, V, d, H):
# fit a, b, e, alpha, beta, where A = exp(a), B = exp(b), E = exp(e)
a1, a2, b, e, alpha2, beta = params
alpha1 = beta
term1_1 = a1 - alpha1 * np.log(Nnv)
term1_2 = a2 - alpha2 * np.log(V*d)
term2 = b - beta * np.log(H_to_D(H,V))
term3 = e
return (np.exp(term1_1) + np.exp(term1_2) + np.exp(term2) - np.exp(term3))
def objective_function(params, delta=0.001):
predict_lossu = LSE(params, Nnv_data, V_data, d_data, H_data)
residuals = (predict_lossu - lossu_data)
return np.sum(huber(delta, residuals))
'''
Given the (Nnv, d, H), we predict the optimal V based on the fitted loss function
'''
print('start fitting...')
low_bound = 0.1
best_mse = float('inf')
best_r2 = 0
best_mse_init_guess, best_r2_init_guess = None, None
best_mse_guess, best_r2_guess = None, None
best_data_predicted = None
cnt = 0
for init_a1 in np.linspace(0, 5, 3):
for init_a2 in np.linspace(0, 5, 3):
for init_b in np.linspace(0, 5, 3):
for init_e in np.linspace(0, 2, 2):
for init_alpha2 in np.linspace(0, 1, 3):
for init_beta in np.linspace(0, 1, 3):
cnt += 1
if cnt % 100 == 0:
print('The number of init guess: ', cnt, 'best_mse', best_mse)
initial_guess = [init_a1, init_a2, init_b, init_e, init_alpha2, init_beta]
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
result = minimize(objective_function, initial_guess,
method='L-BFGS-B',
)
data_actual = lossu_data
data_predicted = np.array([LSE(result.x, Nnv,V,d,H) for Nnv,V,d,H in zip(Nnv_data, V_data, d_data, H_data)])
mse = relative_mse(data_actual, data_predicted)
r2 = r2_score(data_actual, data_predicted)
constraint = low_bound < result.x[4] < 1 and low_bound < result.x[5] < 1
if mse < best_mse and constraint:
best_mse = mse
best_mse_init_guess = initial_guess
best_mse_guess = result.x
best_data_predicted = data_predicted
if r2 > best_r2 and constraint:
best_r2 = r2
best_r2_init_guess = initial_guess
best_r2_guess = result.x
print(f"RMSE (good RMSE near to 0): {best_mse}\n\
best_r2 (good r2 near to 1): {best_r2}\n\
best_mse_init_guess is {best_mse_init_guess}\n\
best_mse_guess is {best_mse_guess}\n")
a1,a2,b,e,alpha2,beta = best_mse_guess
alpha1 = beta
A1,A2,B,E = math.exp(a1),math.exp(a2),math.exp(b),math.exp(e)
print(f'A1={A1}, A2={A2}, B={B}, E={E},\nalpha1={alpha1}, alpha2={alpha2}, beta={beta}')
from approach1_isoflops import Nnvopt_to_flops
Nnvs =[2.87*10**9, 3*10**9, 7*10**9, 13*10**9, 30*10**9, 70*10**9, 130*10**9, 300*10**9]
Nnvs = np.array(Nnvs, dtype=np.float64)
Nvs = []
for Nnv in Nnvs:
# normalization
d = Nnv_to_d(Nnv)
F = Nnvopt_to_flops(Nnv)
Nnv = Nnv / 1_000_000
d = d / 1_000
F = F / (1_000_000_000*1_000_000)
v = fsolve(dl_dv, 1, args=(Nnv,d,F,))[0]
# de-normalization
d = d * 1000
Nnv = Nnv * 1_000_000
F = F * (1_000_000_000*1_000_000)
v = int(v*1000)
Nv = v * d
Nvs.append(Nv)
print(f'Approach3: Nnv={Nnv:.1e}, FLOPs={F:.1e}, Vopt-isoloss:{v}, Nv={Nv/10**9}B')
## Study the cases of under-training / over-training.
print('\nStudy the cases of under-training / over-training. Example Nnv=302M')
for time in [0.2,0.3,0.5,1,2,3,4,5,6]:
# normalization
Nnv = 302*10**6
d = Nnv_to_d(Nnv)
F = time * Nnvopt_to_flops(Nnv)
Nnv = Nnv / 1_000_000
d = d / 1_000
F = F / (1_000_000_000*1_000_000)
v = fsolve(dl_dv, 1, args=(Nnv,d,F,))[0]
print(f'Approach3: Nnv={Nnv*1_000_000:.1e}, FLOPs={F*(1_000_000_000*1_000_000):.1e}, Vopt-isoloss:{int(v*1000)}')
print('\nThe scaling factor of Nv with respect to Nnv:')
for i in range(1, len(Nnvs)):
dNnv = Nnvs[i] / Nnvs[0]
dNv = Nvs[i] / Nvs[0]
diff = math.log(dNv) / math.log(dNnv)
print(f"Approach3: Nnv={Nnvs[i]:.1e}, Nv={Nvs[i]:.1e}, log(dNv)/log(dNnv)={diff:.4f}")