@@ -69,7 +69,9 @@ def activation_compression(model, compile_config, activation_indexes, bits,
69
69
"""
70
70
assert len (activation_indexes ) > 0
71
71
assert 0.0 < sample_size <= 1.0
72
- km_models = [KMeans (2 ** bits )] * len (activation_indexes )
72
+ # n_init=10 maintains the same behavior as legacy versions of sklearn. This
73
+ # was changed to "auto" in sklearn 1.4.
74
+ km_models = [KMeans (2 ** bits , n_init = 10 )] * len (activation_indexes )
73
75
cb_tables = [[]] * len (activation_indexes )
74
76
models = []
75
77
x = x_in = model .layers [0 ].output
@@ -139,7 +141,7 @@ def weight_compression(weights, bits, axis=0, quantizer=None):
139
141
for i , w in tqdm (enumerate (np .split (weights , weights .shape [axis ], axis ))):
140
142
original_shape = w .shape
141
143
w = w .ravel ()
142
- km = KMeans (n )
144
+ km = KMeans (n , n_init = 10 )
143
145
km .fit (w .reshape (- 1 , 1 ))
144
146
if quantizer :
145
147
km .cluster_centers_ = quantizer (km .cluster_centers_ ).numpy ()
@@ -178,7 +180,7 @@ def two_tier_embedding_compression(embeddings, bits, quantizer=None):
178
180
cluster_index_table = np .zeros (index_table .shape [0 ], dtype = np .uint8 )
179
181
codebook_table = np .zeros ((n , n ))
180
182
181
- km1 = KMeans (n )
183
+ km1 = KMeans (n , n_init = 10 )
182
184
km1 .fit (embeddings )
183
185
tier1 = km1 .predict (embeddings )
184
186
@@ -188,7 +190,7 @@ def two_tier_embedding_compression(embeddings, bits, quantizer=None):
188
190
mask = block_label == tier1
189
191
indices = np .arange (embeddings .shape [0 ])[mask ]
190
192
block = embeddings [mask ]
191
- km2 = KMeans (n )
193
+ km2 = KMeans (n , n_init = 10 )
192
194
km2 .fit (block .flatten ().reshape (- 1 , 1 ))
193
195
if quantizer :
194
196
km2 .cluster_centers_ = quantizer (km2 .cluster_centers_ ).numpy ()
0 commit comments