-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_modeling_mistral_yarn_flax.py
258 lines (192 loc) · 9.87 KB
/
test_modeling_mistral_yarn_flax.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
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
import numpy as np
from unittest import TestCase
import jax
from modeling_mistral_yarn_flax import _make_causal_mask as _make_causal_mask_flax
from modeling_mistral_yarn import _make_causal_mask as _make_causal_mask_torch
from modeling_mistral_yarn_flax import _make_sliding_window_causal_mask as _make_sliding_window_causal_mask_flax
from modeling_mistral_yarn import _make_sliding_window_causal_mask as _make_sliding_window_causal_mask_torch
from modeling_mistral_yarn_flax import _yarn_linear_ramp_mask as _yarn_linear_ramp_mask_flax
from modeling_mistral_yarn import _yarn_linear_ramp_mask as _yarn_linear_ramp_mask_torch
from modeling_mistral_yarn_flax import _expand_mask as _expand_mask_flax
from modeling_mistral_yarn import _expand_mask as _expand_mask_torch
from modeling_mistral_yarn_flax import MistralRMSNorm as MistralRMSNorm_flax
from modeling_mistral_yarn import MistralRMSNorm as MistralRMSNorm_torch
from modeling_mistral_yarn_flax import MistralRotaryEmbedding as MistralRotaryEmbedding_flax
from modeling_mistral_yarn import MistralRotaryEmbedding as MistralRotaryEmbedding_torch
from modeling_mistral_yarn_flax import MistralLinearScalingRotaryEmbedding as MistralLinearScalingRotaryEmbedding_flax
from modeling_mistral_yarn import MistralLinearScalingRotaryEmbedding as MistralLinearScalingRotaryEmbedding_torch
from modeling_mistral_yarn_flax import \
MistralDynamicNTKScalingRotaryEmbedding as MistralDynamicNTKScalingRotaryEmbedding_flax
from modeling_mistral_yarn import \
MistralDynamicNTKScalingRotaryEmbedding as MistralDynamicNTKScalingRotaryEmbedding_torch
from modeling_mistral_yarn_flax import MistralYaRNScaledRotaryEmbedding as MistralYaRNScaledRotaryEmbedding_flax
from modeling_mistral_yarn import MistralYaRNScaledRotaryEmbedding as MistralYaRNScaledRotaryEmbedding_torch
import torch
import jax.numpy as jnp
class Test(TestCase):
def test__get_unpad_data(self):
self.fail()
class Test(TestCase):
def test__make_causal_mask(self):
"""
Compare the causal mask function between the pytorch and the flax implementation
"""
# torch version of _make_causal_mask
t = torch.empty(10, 10)
input_ids_shape = t.size()
dtype = torch.float32
device = torch.device("cpu")
past_key_values_length = 10
outputs_pytorch = _make_causal_mask_torch(
input_ids_shape=input_ids_shape,
dtype=dtype,
device=device,
past_key_values_length=past_key_values_length)
# flax version of _make_causal_mask
input_ids_shape = (10, 10)
dtype = jnp.dtype('float32')
past_key_values_length = 10
outputs_flax = _make_causal_mask_flax(
input_ids_shape=input_ids_shape,
dtype=dtype,
past_key_values_length=past_key_values_length)
np_array = np.asarray(outputs_flax)
flax_to_torch_ten = torch.from_numpy(np_array)
tol = torch.max(torch.abs(flax_to_torch_ten - outputs_pytorch))
self.assertTrue(torch.allclose(flax_to_torch_ten, outputs_pytorch, atol=1e-03))
class Test(TestCase):
def test__make_sliding_window_causal_mask(self):
"""
Compare the sliding_window_causal mask function between the pytorch and the flax implementation
"""
# torch version of _make_causal_mask
t = torch.empty(10, 10)
input_ids_shape = t.size()
dtype = torch.float32
device = torch.device("cpu")
past_key_values_length = 10
sliding_window = 5
outputs_pytorch = _make_sliding_window_causal_mask_torch(
input_ids_shape=input_ids_shape,
dtype=dtype,
device=device,
past_key_values_length=past_key_values_length,
sliding_window=sliding_window)
# flax version of _make_causal_mask
input_ids_shape = (10, 10)
dtype = jnp.dtype('float32')
past_key_values_length = 10
sliding_window = 5
outputs_flax = _make_sliding_window_causal_mask_flax(
input_ids_shape=input_ids_shape,
dtype=dtype,
past_key_values_length=past_key_values_length,
sliding_window=sliding_window)
np_array = np.asarray(outputs_flax)
flax_to_torch_ten = torch.from_numpy(np_array)
self.assertTrue(torch.allclose(flax_to_torch_ten, outputs_pytorch, atol=1e-03))
class Test(TestCase):
def test__expand_mask(self):
self.fail()
class Test(TestCase):
def test__yarn_linear_ramp_mask(self):
outputs_pytorch = _yarn_linear_ramp_mask_torch(0, 1, 10)
outputs_flax = _yarn_linear_ramp_mask_flax(0, 1, 10)
np_array = np.asarray(outputs_flax)
flax_to_torch_ten = torch.from_numpy(np_array)
self.assertTrue(torch.allclose(flax_to_torch_ten, outputs_pytorch, atol=1e-03))
class Test(TestCase):
def test__expand_mask(self):
mask = torch.zeros((12, 22))
dtype = torch.float32
tgt_len = 10
outputs_pytorch = _expand_mask_torch(
mask=mask,
dtype=dtype,
tgt_len=tgt_len
)
mask = jnp.zeros((12, 22))
dtype = jnp.float32
tgt_len = 10
outputs_flax = _expand_mask_flax(
mask=mask,
dtype=dtype,
tgt_len=tgt_len
)
np_array = np.asarray(outputs_flax)
flax_to_torch_ten = torch.from_numpy(np_array)
self.assertTrue(torch.allclose(flax_to_torch_ten, outputs_pytorch, atol=1e-03))
class TestMistralRMSNorm(TestCase):
def test_setup(self):
flax_layer = MistralRMSNorm_flax(hidden_size=4096)
params = flax_layer.init(jax.random.key(0), jnp.ones((1, 13, 4096)))
flax_input = jax.random.normal(jax.random.key(0), shape=(1, 13, 4096))
flax_outputs = flax_layer.apply(params, flax_input)
torch_layer = MistralRMSNorm_torch(hidden_size=4096)
np_input = np.asarray(flax_input)
torch_input = torch.from_numpy(np_input)
torch_outputs = torch_layer(torch_input)
np_array = np.asarray(flax_outputs)
flax_to_torch_ten = torch.from_numpy(np_array)
self.assertTrue(torch.allclose(flax_to_torch_ten, torch_outputs, atol=1e-03))
class TestMistralRotaryEmbedding(TestCase):
def test_setup(self):
flax_layer = MistralRotaryEmbedding_flax(dim=50)
params = flax_layer.init(jax.random.key(0), jnp.ones((20, 13, 10, 40)), 10)
flax_input = jax.random.normal(jax.random.key(0), shape=(20, 13, 10, 40))
flax_outputs = flax_layer.apply(params, flax_input, 10)
torch_layer = MistralRotaryEmbedding_torch(dim=50)
np_input = np.asarray(flax_input)
torch_input = torch.from_numpy(np_input)
torch_outputs = torch_layer(torch_input, 10)
np_array = np.asarray(flax_outputs)
flax_to_torch_ten = torch.from_numpy(np_array)
cond1 = torch.allclose(flax_to_torch_ten[0], torch_outputs[0], atol=1e-03)
cond2 = torch.allclose(flax_to_torch_ten[1], torch_outputs[1], atol=1e-03)
self.assertTrue(cond1 and cond2)
class TestMistralLinearScalingRotaryEmbedding(TestCase):
def test_setup(self):
flax_layer = MistralLinearScalingRotaryEmbedding_flax(dim=50, scaling_factor=2)
params = flax_layer.init(jax.random.key(0), jnp.ones((20, 13, 10, 40)), 10)
flax_input = jax.random.normal(jax.random.key(0), shape=(20, 13, 10, 40))
flax_outputs = flax_layer.apply(params, flax_input, 10)
torch_layer = MistralLinearScalingRotaryEmbedding_torch(dim=50, scaling_factor=2)
np_input = np.asarray(flax_input)
torch_input = torch.from_numpy(np_input)
torch_outputs = torch_layer(torch_input, 10)
np_array = np.asarray(flax_outputs)
flax_to_torch_ten = torch.from_numpy(np_array)
cond1 = torch.allclose(flax_to_torch_ten[0], torch_outputs[0], atol=1e-03)
cond2 = torch.allclose(flax_to_torch_ten[1], torch_outputs[1], atol=1e-03)
# !!! There is a possible bug in the original implementation cos,cos is returned instead of cos,sin
self.assertTrue(cond1 and cond2)
class TestMistralDynamicNTScalingRotaryEmbedding(TestCase):
def test_setup(self):
flax_layer = MistralDynamicNTKScalingRotaryEmbedding_flax(dim=50, scaling_factor=2)
params = flax_layer.init(jax.random.key(0), jnp.ones((20, 13, 10, 40)), 10)
flax_input = jax.random.normal(jax.random.key(0), shape=(20, 13, 10, 40))
flax_outputs = flax_layer.apply(params, flax_input, 10)
torch_layer = MistralDynamicNTKScalingRotaryEmbedding_torch(dim=50, scaling_factor=2)
np_input = np.asarray(flax_input)
torch_input = torch.from_numpy(np_input)
torch_outputs = torch_layer(torch_input, 10)
np_array = np.asarray(flax_outputs)
flax_to_torch_ten = torch.from_numpy(np_array)
cond1 = torch.allclose(flax_to_torch_ten[0], torch_outputs[0], atol=1e-03)
cond2 = torch.allclose(flax_to_torch_ten[1], torch_outputs[1], atol=1e-03)
self.assertTrue(cond1 and cond2)
class TestMistralYaRNScaledRotaryEmbedding(TestCase):
def test_setup(self):
flax_layer = MistralYaRNScaledRotaryEmbedding_flax(dim=50)
params = flax_layer.init(jax.random.key(0), jnp.ones((20, 13, 10, 40)), 10)
flax_input = jax.random.normal(jax.random.key(0), shape=(20, 13, 10, 40))
flax_outputs = flax_layer.apply(params, flax_input, 10)
torch_layer = MistralYaRNScaledRotaryEmbedding_torch(dim=50)
np_input = np.asarray(flax_input)
torch_input = torch.from_numpy(np_input)
torch_outputs = torch_layer(torch_input, 10)
np_array = np.asarray(flax_outputs)
flax_to_torch_ten = torch.from_numpy(np_array)
cond1 = torch.allclose(flax_to_torch_ten[0], torch_outputs[0], atol=1e-03)
cond2 = torch.allclose(flax_to_torch_ten[1], torch_outputs[1], atol=1e-03)
self.assertTrue(cond1 and cond2)