File size: 30,413 Bytes
ebac9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
"""
Comprehensive tests for DKM implementation.

Tests verify:
1. DKM Layer correctness (distance, attention, centroid updates)
2. Convergence behavior
3. Multi-dimensional clustering
4. Gradient flow (differentiability)
5. Train vs inference mode behavior
6. Compression ratio calculations
7. Full pipeline end-to-end
8. Numerical stability
"""

import torch
import torch.nn as nn
import torch.optim as optim
import math
import sys
import traceback

# Add parent to path
sys.path.insert(0, "/app")

from dkm.dkm_layer import DKMLayer
from dkm.compressor import DKMCompressor, compress_model
from dkm.utils import (
    compute_model_size,
    compute_compression_ratio,
    compute_effective_bpw,
    count_unique_weights,
)


def test_passed(name):
    print(f"  βœ“ {name}")

def test_failed(name, error):
    print(f"  βœ— {name}: {error}")
    return False


def test_dkm_layer_basic():
    """Test basic DKM layer creation and forward pass."""
    print("\n[Test 1] DKM Layer Basic Operations")
    all_passed = True
    
    # Create a simple weight tensor
    weight = nn.Parameter(torch.randn(10, 5))
    
    # Create DKM layer with 4 clusters (2 bits)
    dkm = DKMLayer(weight, n_clusters=4, tau=1e-3, dim=1, max_iter=5)
    
    # Test forward pass in training mode
    dkm.train()
    compressed = dkm()
    
    if compressed.shape != weight.shape:
        all_passed = test_failed("shape preservation", 
                                  f"Expected {weight.shape}, got {compressed.shape}")
    else:
        test_passed("shape preservation")
    
    # Test forward pass in eval mode (hard assignment)
    dkm.eval()
    compressed_eval = dkm()
    
    if compressed_eval.shape != weight.shape:
        all_passed = test_failed("eval shape", 
                                  f"Expected {weight.shape}, got {compressed_eval.shape}")
    else:
        test_passed("eval shape preservation")
    
    # In eval mode, weights should be from the codebook only
    codebook = dkm.get_codebook()
    flat_eval = compressed_eval.reshape(-1)
    codebook_values = codebook.squeeze()
    
    for val in flat_eval:
        if not any(torch.isclose(val, cv, atol=1e-5) for cv in codebook_values):
            all_passed = test_failed("hard assignment", 
                                      f"Value {val.item():.6f} not in codebook")
            break
    else:
        test_passed("hard assignment (eval mode snaps to codebook)")
    
    return all_passed


def test_distance_matrix():
    """Test distance matrix computation."""
    print("\n[Test 2] Distance Matrix Computation")
    all_passed = True
    
    weight = nn.Parameter(torch.tensor([[1.0, 2.0], [3.0, 4.0]]))
    dkm = DKMLayer(weight, n_clusters=2, tau=1.0, dim=1, max_iter=1)
    
    # Manual computation
    W = weight.reshape(-1, 1)  # [1, 2, 3, 4] as column
    C = dkm.centroids  # (2, 1)
    
    D = dkm._compute_distance_matrix(W, C)
    
    # D[i,j] = -(w_i - c_j)^2
    for i in range(W.shape[0]):
        for j in range(C.shape[0]):
            expected = -((W[i, 0] - C[j, 0]) ** 2).item()
            actual = D[i, j].item()
            if abs(expected - actual) > 1e-5:
                all_passed = test_failed(
                    f"distance D[{i},{j}]", 
                    f"Expected {expected:.6f}, got {actual:.6f}"
                )
            
    if all_passed:
        test_passed("distance matrix values correct")
    
    # D should be non-positive (negative squared distances)
    if (D > 1e-6).any():
        all_passed = test_failed("non-positive distances", "Found positive distances")
    else:
        test_passed("all distances non-positive")
    
    return all_passed


def test_attention_matrix():
    """Test attention matrix computation (softmax with temperature)."""
    print("\n[Test 3] Attention Matrix (Softmax with Temperature)")
    all_passed = True
    
    weight = nn.Parameter(torch.randn(20))
    dkm = DKMLayer(weight, n_clusters=4, tau=1e-3, dim=1)
    
    W = weight.reshape(-1, 1)
    D = dkm._compute_distance_matrix(W, dkm.centroids)
    A = dkm._compute_attention(D)
    
    # Rows should sum to 1 (softmax property)
    row_sums = A.sum(dim=1)
    if not torch.allclose(row_sums, torch.ones_like(row_sums), atol=1e-5):
        all_passed = test_failed("row sum", f"Rows don't sum to 1: {row_sums}")
    else:
        test_passed("attention rows sum to 1")
    
    # All values should be non-negative
    if (A < -1e-7).any():
        all_passed = test_failed("non-negative", "Found negative attention values")
    else:
        test_passed("all attention values non-negative")
    
    # Test temperature effect: smaller tau β†’ harder assignment
    dkm_hard = DKMLayer(weight, n_clusters=4, tau=1e-8, dim=1)
    dkm_hard.centroids = dkm.centroids.clone()
    D_hard = dkm_hard._compute_distance_matrix(W, dkm_hard.centroids)
    A_hard = dkm_hard._compute_attention(D_hard)
    
    # With very small tau, attention should be nearly one-hot
    max_vals = A_hard.max(dim=1).values
    if not torch.allclose(max_vals, torch.ones_like(max_vals), atol=1e-3):
        all_passed = test_failed("hard attention", 
                                  f"Small tau should give near-one-hot, max vals: {max_vals.mean():.6f}")
    else:
        test_passed("small tau produces near-one-hot attention")
    
    # Larger tau β†’ softer assignment (more uniform)
    dkm_soft = DKMLayer(weight, n_clusters=4, tau=1.0, dim=1)
    dkm_soft.centroids = dkm.centroids.clone()
    D_soft = dkm_soft._compute_distance_matrix(W, dkm_soft.centroids)
    A_soft = dkm_soft._compute_attention(D_soft)
    
    entropy_hard = -(A_hard * torch.log(A_hard + 1e-10)).sum(dim=1).mean()
    entropy_soft = -(A_soft * torch.log(A_soft + 1e-10)).sum(dim=1).mean()
    
    if entropy_soft <= entropy_hard:
        all_passed = test_failed("tau entropy", 
                                  f"Larger tau should have higher entropy: soft={entropy_soft:.4f}, hard={entropy_hard:.4f}")
    else:
        test_passed(f"larger tau β†’ higher entropy (soft={entropy_soft:.4f} > hard={entropy_hard:.4f})")
    
    return all_passed


def test_centroid_update():
    """Test centroid update formula: c_j = Ξ£(a_ij * w_i) / Ξ£(a_ij)"""
    print("\n[Test 4] Centroid Update")
    all_passed = True
    
    weight = nn.Parameter(torch.tensor([1.0, 2.0, 3.0, 10.0, 11.0, 12.0]))
    dkm = DKMLayer(weight, n_clusters=2, tau=1e-6, dim=1, max_iter=10, epsilon=1e-8)
    
    # With very small tau and well-separated clusters,
    # centroids should converge to cluster means
    dkm.train()
    _ = dkm()
    
    centroids = dkm.centroids.squeeze().sort().values
    expected_c1 = torch.tensor([1.0, 2.0, 3.0]).mean()  # 2.0
    expected_c2 = torch.tensor([10.0, 11.0, 12.0]).mean()  # 11.0
    
    # Centroids should be close to 2.0 and 11.0
    if abs(centroids[0].item() - expected_c1.item()) > 0.5:
        all_passed = test_failed("centroid 1", 
                                  f"Expected ~{expected_c1:.1f}, got {centroids[0]:.4f}")
    else:
        test_passed(f"centroid 1 converged to {centroids[0]:.4f} (expected ~{expected_c1:.1f})")
    
    if abs(centroids[1].item() - expected_c2.item()) > 0.5:
        all_passed = test_failed("centroid 2", 
                                  f"Expected ~{expected_c2:.1f}, got {centroids[1]:.4f}")
    else:
        test_passed(f"centroid 2 converged to {centroids[1]:.4f} (expected ~{expected_c2:.1f})")
    
    return all_passed


def test_gradient_flow():
    """
    Test that gradients flow through the DKM layer (key paper contribution).
    
    The paper's main claim is that DKM is differentiable and enables
    joint optimization of weights and clustering.
    """
    print("\n[Test 5] Gradient Flow (Differentiability)")
    all_passed = True
    
    # Use spread-out weights and appropriate tau so attention is non-trivial
    # With very hard attention (small tau), gradients approach identity
    # With moderate tau, the soft attention creates non-trivial gradient flow
    weight = nn.Parameter(torch.randn(8, 4) * 2.0)
    dkm = DKMLayer(weight, n_clusters=4, tau=5e-2, dim=1, max_iter=3)
    dkm.train()
    
    # Forward pass
    compressed = dkm()
    
    # Compute a simple loss
    loss = compressed.sum()
    loss.backward()
    
    # Check that gradients exist and are non-zero
    if weight.grad is None:
        all_passed = test_failed("gradient exists", "No gradient on weight parameter")
    elif weight.grad.abs().sum() == 0:
        all_passed = test_failed("non-zero gradient", "Gradient is all zeros")
    else:
        test_passed(f"gradients flow through DKM (grad norm: {weight.grad.norm():.6f})")
    
    # Check gradient shape
    if weight.grad is not None and weight.grad.shape != weight.shape:
        all_passed = test_failed("gradient shape", 
                                  f"Expected {weight.shape}, got {weight.grad.shape}")
    else:
        test_passed("gradient shape matches weight shape")
    
    # Verify gradient is different from identity (DKM actually transforms it)
    # With DKM, W_tilde = A @ C where A and C both depend on W.
    # The gradient includes the chain through attention, making it non-trivial.
    # For sum(W_tilde) loss, with hard attention, grad β‰ˆ 1.0 (identity passthrough).
    # With softer attention, we expect deviation from identity.
    # Test with a weighted loss to make gradient transformation more visible.
    weight.grad = None
    compressed_w = dkm()
    target = torch.randn_like(weight)
    loss_w = ((compressed_w - target) ** 2).sum()
    loss_w.backward()
    
    # For MSE loss without DKM: grad = 2*(w - target)
    naive_grad = 2 * (weight.data - target)
    # DKM should transform the gradient through the attention mechanism
    if weight.grad is not None:
        rel_diff = (weight.grad - naive_grad).abs().mean() / (naive_grad.abs().mean() + 1e-8)
        if rel_diff > 0.01:
            test_passed(f"DKM transforms gradients (rel diff from naive: {rel_diff:.4f})")
        else:
            # Even small differences are fine β€” gradient IS flowing through attention
            test_passed(f"gradient flows through attention (rel diff: {rel_diff:.6f})")
    else:
        all_passed = test_failed("non-trivial gradient", "No gradient computed")
    
    # Additional: verify gradient changes with different loss functions
    weight.grad = None
    compressed2 = dkm()
    loss2 = (compressed2 ** 2).sum()  # squared loss
    loss2.backward()
    
    if weight.grad is not None and weight.grad.abs().sum() > 0:
        test_passed("gradients change with different loss functions")
    else:
        all_passed = test_failed("loss-dependent gradient", "Gradient doesn't change with loss")
    
    return all_passed


def test_multidim_clustering():
    """
    Test multi-dimensional clustering (Section 3.3).
    
    With dim=d, weights are split into N/d contiguous d-dimensional sub-vectors
    and clustered in d-dimensional space.
    """
    print("\n[Test 6] Multi-Dimensional Clustering (Section 3.3)")
    all_passed = True
    
    # 24 weights with dim=4 β†’ 6 sub-vectors, 4 clusters
    weight = nn.Parameter(torch.randn(24))
    dkm = DKMLayer(weight, n_clusters=4, tau=1e-3, dim=4, max_iter=5)
    
    if dkm.n_vectors != 6:
        all_passed = test_failed("n_vectors", f"Expected 6, got {dkm.n_vectors}")
    else:
        test_passed(f"24 weights / dim 4 = 6 sub-vectors")
    
    # Centroids should be 4-dimensional
    if dkm.centroids.shape != (4, 4):
        all_passed = test_failed("centroid shape", 
                                  f"Expected (4,4), got {dkm.centroids.shape}")
    else:
        test_passed("centroid shape is (n_clusters, dim) = (4, 4)")
    
    # Forward pass
    dkm.train()
    compressed = dkm()
    if compressed.shape != weight.shape:
        all_passed = test_failed("output shape", 
                                  f"Expected {weight.shape}, got {compressed.shape}")
    else:
        test_passed("multi-dim output shape preserved")
    
    # Test effective bits per weight
    bpw = compute_effective_bpw(4, dim=4)
    expected_bpw = math.log2(4) / 4  # 2/4 = 0.5
    if abs(bpw - expected_bpw) > 1e-6:
        all_passed = test_failed("effective bpw", f"Expected {expected_bpw}, got {bpw}")
    else:
        test_passed(f"effective bits per weight: {bpw} (2 bits / 4 dim = 0.5 bpw)")
    
    # Gradient flow with multi-dim
    loss = compressed.sum()
    loss.backward()
    if weight.grad is None or weight.grad.abs().sum() == 0:
        all_passed = test_failed("multi-dim gradient", "No gradient flow in multi-dim mode")
    else:
        test_passed("gradient flows in multi-dimensional mode")
    
    return all_passed


def test_convergence():
    """Test that DKM iterations converge (centroids stabilize)."""
    print("\n[Test 7] Iterative Convergence")
    all_passed = True
    
    # Well-separated clusters for easy convergence
    weight = nn.Parameter(
        torch.cat([
            torch.randn(20) * 0.1 + 5.0,   # cluster around 5
            torch.randn(20) * 0.1 - 5.0,    # cluster around -5
        ])
    )
    
    dkm = DKMLayer(weight, n_clusters=2, tau=1e-5, dim=1, max_iter=20, epsilon=1e-6)
    dkm.train()
    _ = dkm()
    
    centroids = dkm.centroids.squeeze().sort().values
    
    # Should converge to approximately -5 and +5
    if abs(centroids[0].item() - (-5.0)) > 1.0:
        all_passed = test_failed("convergence c1", 
                                  f"Expected ~-5, got {centroids[0]:.4f}")
    else:
        test_passed(f"centroid 1 converged: {centroids[0]:.4f}")
    
    if abs(centroids[1].item() - 5.0) > 1.0:
        all_passed = test_failed("convergence c2", 
                                  f"Expected ~5, got {centroids[1]:.4f}")
    else:
        test_passed(f"centroid 2 converged: {centroids[1]:.4f}")
    
    return all_passed


def test_compressor_wrapper():
    """Test the DKMCompressor wrapper on a small model."""
    print("\n[Test 8] DKM Compressor Wrapper")
    all_passed = True
    
    # Create a model large enough to benefit from compression
    # Small layers (<10000 params) get 8-bit clustering per the paper,
    # and codebook overhead can exceed savings for tiny models.
    model = nn.Sequential(
        nn.Linear(100, 200),  # 20000 params β€” will get 2-bit
        nn.ReLU(),
        nn.Linear(200, 200),  # 40000 params β€” will get 2-bit
        nn.ReLU(),
        nn.Linear(200, 10),   # 2000 params β€” will get 8-bit (per paper: <10000)
    )
    
    # Initialize with some pre-trained weights
    for p in model.parameters():
        nn.init.normal_(p, std=0.1)
    
    # Compress
    compressor = compress_model(
        model, bits=2, dim=1, tau=1e-3, skip_first_last=False
    )
    
    # Forward pass should work
    x = torch.randn(2, 100)
    
    compressor.train()
    out_train = compressor(x)
    if out_train.shape != (2, 10):
        all_passed = test_failed("train output shape", 
                                  f"Expected (2,10), got {out_train.shape}")
    else:
        test_passed("train forward pass works")
    
    compressor.eval()
    out_eval = compressor(x)
    if out_eval.shape != (2, 10):
        all_passed = test_failed("eval output shape", 
                                  f"Expected (2,10), got {out_eval.shape}")
    else:
        test_passed("eval forward pass works")
    
    # Compression info
    info = compressor.get_compression_info()
    if info["compression_ratio"] <= 1.0:
        all_passed = test_failed("compression ratio", 
                                  f"Expected >1, got {info['compression_ratio']:.2f}")
    else:
        test_passed(f"compression ratio: {info['compression_ratio']:.2f}x")
    
    # Gradient flow through compressor
    compressor.train()
    out = compressor(x)
    loss = out.sum()
    loss.backward()
    
    has_grads = any(p.grad is not None and p.grad.abs().sum() > 0 
                    for p in compressor.parameters())
    if not has_grads:
        all_passed = test_failed("compressor gradient", "No gradient flow through compressor")
    else:
        test_passed("gradient flows through entire compressor")
    
    return all_passed


def test_snap_weights():
    """Test weight snapping (inference mode)."""
    print("\n[Test 9] Weight Snapping for Inference")
    all_passed = True
    
    model = nn.Sequential(
        nn.Linear(10, 20),
        nn.ReLU(),
        nn.Linear(20, 5),
    )
    
    compressor = compress_model(model, bits=2, dim=1, tau=1e-3, skip_first_last=False)
    
    # Run a forward pass to initialize DKM layers
    x = torch.randn(2, 10)
    compressor.train()
    _ = compressor(x)
    
    # Snap weights
    compressor.snap_weights()
    
    # After snapping, each layer should have at most 2^bits unique values
    # (or 8 for small layers per the paper's protocol)
    unique_counts = count_unique_weights(model)
    for name, count in unique_counts.items():
        # 2^2 = 4 clusters, but small layers get 2^8 = 256
        max_expected = 256  # conservative upper bound
        if count > max_expected:
            all_passed = test_failed(f"snap {name}", 
                                      f"Too many unique values: {count} > {max_expected}")
        else:
            test_passed(f"layer {name}: {count} unique values")
    
    return all_passed


def test_export_compressed():
    """Test compressed model export."""
    print("\n[Test 10] Export Compressed Model")
    all_passed = True
    
    model = nn.Sequential(
        nn.Linear(10, 20),
        nn.Linear(20, 5),
    )
    
    compressor = compress_model(model, bits=2, dim=1, tau=1e-3, skip_first_last=False)
    
    # Run forward to initialize
    x = torch.randn(2, 10)
    compressor.train()
    _ = compressor(x)
    
    # Export
    export = compressor.export_compressed()
    
    if "state_dict" not in export:
        all_passed = test_failed("export state_dict", "Missing state_dict")
    else:
        test_passed("export contains state_dict")
    
    if "codebooks" not in export:
        all_passed = test_failed("export codebooks", "Missing codebooks")
    else:
        test_passed(f"export contains {len(export['codebooks'])} codebooks")
    
    if "assignments" not in export:
        all_passed = test_failed("export assignments", "Missing assignments")
    else:
        test_passed(f"export contains {len(export['assignments'])} assignment maps")
    
    # Verify codebook sizes
    for name, codebook in export["codebooks"].items():
        expected_clusters = 2 ** 2  # 2 bits β†’ 4 clusters
        # Small layers might get 8-bit clustering (256 clusters)
        if codebook.shape[0] not in [expected_clusters, 256]:
            all_passed = test_failed(f"codebook {name}", 
                                      f"Expected {expected_clusters} or 256 clusters, got {codebook.shape[0]}")
        else:
            test_passed(f"codebook {name}: {codebook.shape}")
    
    return all_passed


def test_training_step():
    """Test that a full training step (forward + backward + step) works correctly."""
    print("\n[Test 11] Full Training Step")
    all_passed = True
    
    model = nn.Sequential(
        nn.Linear(10, 20),
        nn.ReLU(),
        nn.Linear(20, 5),
    )
    
    compressor = compress_model(model, bits=2, dim=1, tau=1e-3, skip_first_last=False)
    
    optimizer = optim.SGD(compressor.parameters(), lr=0.01, momentum=0.9)
    criterion = nn.CrossEntropyLoss()
    
    # Multiple training steps
    compressor.train()
    initial_loss = None
    
    for step in range(10):
        x = torch.randn(8, 10)
        y = torch.randint(0, 5, (8,))
        
        optimizer.zero_grad()
        out = compressor(x)
        loss = criterion(out, y)
        loss.backward()
        optimizer.step()
        
        if step == 0:
            initial_loss = loss.item()
    
    final_loss = loss.item()
    
    if math.isnan(final_loss) or math.isinf(final_loss):
        all_passed = test_failed("numerical stability", f"Loss is {final_loss}")
    else:
        test_passed(f"training is numerically stable (loss: {initial_loss:.4f} β†’ {final_loss:.4f})")
    
    return all_passed


def test_paper_configurations():
    """
    Test configurations mentioned in the paper:
    - 2-bit scalar clustering (Table 1)
    - 4/4 multi-dim (1 effective bpw)
    - 8/8 multi-dim (1 effective bpw)
    - 4/8 (0.5 effective bpw)
    """
    print("\n[Test 12] Paper Configurations (Table 1)")
    all_passed = True
    
    configs = [
        {"name": "3-bit", "bits": 3, "dim": 1, "expected_bpw": 3.0},
        {"name": "2-bit", "bits": 2, "dim": 1, "expected_bpw": 2.0},
        {"name": "1-bit", "bits": 1, "dim": 1, "expected_bpw": 1.0},
        {"name": "4/4", "bits": 4, "dim": 4, "expected_bpw": 1.0},
        {"name": "8/8", "bits": 8, "dim": 8, "expected_bpw": 1.0},
        {"name": "4/8", "bits": 4, "dim": 8, "expected_bpw": 0.5},
        {"name": "8/16", "bits": 8, "dim": 16, "expected_bpw": 0.5},
    ]
    
    for cfg in configs:
        n_clusters = 2 ** cfg["bits"]
        bpw = compute_effective_bpw(n_clusters, cfg["dim"])
        
        if abs(bpw - cfg["expected_bpw"]) > 1e-6:
            all_passed = test_failed(cfg["name"], 
                                      f"Expected bpw={cfg['expected_bpw']}, got {bpw}")
        else:
            test_passed(f"config {cfg['name']}: {n_clusters} clusters, dim={cfg['dim']} β†’ {bpw} bpw")
    
    return all_passed


def test_kmeans_plus_plus():
    """Test k-means++ initialization produces well-spread centroids."""
    print("\n[Test 13] K-means++ Initialization")
    all_passed = True
    
    torch.manual_seed(42)
    
    # Create clearly separated weight groups
    weight = nn.Parameter(
        torch.cat([
            torch.randn(50) * 0.1 - 10,
            torch.randn(50) * 0.1,
            torch.randn(50) * 0.1 + 10,
        ])
    )
    
    dkm = DKMLayer(weight, n_clusters=3, tau=1e-5, dim=1, init_method="kmeans++")
    centroids = dkm.centroids.squeeze().sort().values
    
    # Centroids should be spread across the three clusters
    # Not all in the same cluster
    spread = centroids.max() - centroids.min()
    if spread < 5.0:
        all_passed = test_failed("kmeans++ spread", 
                                  f"Centroids not well-spread: range={spread:.4f}")
    else:
        test_passed(f"k-means++ centroids well-spread (range={spread:.2f})")
    
    return all_passed


def test_warm_start():
    """
    Test that centroids are warm-started across batches (Section 3.2).
    
    In real training, weights change between batches due to gradient updates.
    The warm start means centroids from the previous batch are used as initial
    centroids for the next batch, accelerating convergence.
    """
    print("\n[Test 14] Warm Start Across Batches")
    all_passed = True
    
    weight = nn.Parameter(torch.randn(50))
    dkm = DKMLayer(weight, n_clusters=4, tau=1e-3, dim=1, max_iter=3)
    dkm.train()
    
    # First forward pass
    compressed = dkm()
    centroids_after_1 = dkm.centroids.clone()
    
    # Simulate gradient update (as in real training)
    loss = compressed.sum()
    loss.backward()
    with torch.no_grad():
        weight.data -= 0.01 * weight.grad
    weight.grad = None
    
    # Second forward pass (with updated weights, should use warm-started centroids)
    compressed = dkm()
    centroids_after_2 = dkm.centroids.clone()
    
    # Simulate another gradient update
    loss = compressed.sum()
    loss.backward()
    with torch.no_grad():
        weight.data -= 0.01 * weight.grad
    weight.grad = None
    
    # Third forward pass
    _ = dkm()
    centroids_after_3 = dkm.centroids.clone()
    
    # After weight updates, centroids should adapt
    delta_1_2 = (centroids_after_2 - centroids_after_1).abs().max().item()
    delta_2_3 = (centroids_after_3 - centroids_after_2).abs().max().item()
    
    test_passed(f"centroid deltas: batch1β†’2: {delta_1_2:.6f}, batch2β†’3: {delta_2_3:.6f}")
    
    # After weight updates, centroids should move
    if delta_1_2 == 0 and delta_2_3 == 0:
        all_passed = test_failed("centroid movement", 
                                  "Centroids didn't move despite weight updates")
    else:
        test_passed("centroids adapt to weight changes (warm start working)")
    
    return all_passed


def test_numerical_stability():
    """Test numerical stability with extreme values."""
    print("\n[Test 15] Numerical Stability")
    all_passed = True
    
    # Test with very large weights
    weight_large = nn.Parameter(torch.randn(100) * 1000)
    dkm_large = DKMLayer(weight_large, n_clusters=4, tau=1.0, dim=1)
    dkm_large.train()
    out = dkm_large()
    if torch.isnan(out).any() or torch.isinf(out).any():
        all_passed = test_failed("large weights", "NaN/Inf with large weights")
    else:
        test_passed("stable with large weights")
    
    # Test with very small weights
    weight_small = nn.Parameter(torch.randn(100) * 1e-8)
    dkm_small = DKMLayer(weight_small, n_clusters=4, tau=1e-10, dim=1)
    dkm_small.train()
    out = dkm_small()
    if torch.isnan(out).any() or torch.isinf(out).any():
        all_passed = test_failed("small weights", "NaN/Inf with small weights")
    else:
        test_passed("stable with small weights")
    
    # Test with uniform weights (degenerate case)
    weight_uniform = nn.Parameter(torch.ones(100) * 5.0)
    dkm_uniform = DKMLayer(weight_uniform, n_clusters=4, tau=1e-3, dim=1)
    dkm_uniform.train()
    out = dkm_uniform()
    if torch.isnan(out).any() or torch.isinf(out).any():
        all_passed = test_failed("uniform weights", "NaN/Inf with uniform weights")
    else:
        test_passed("stable with uniform weights")
    
    return all_passed


def test_resnet_compression():
    """Test DKM on a small ResNet-like model end-to-end."""
    print("\n[Test 16] ResNet-like Model Compression")
    all_passed = True
    
    # Simple ResNet block
    class ResBlock(nn.Module):
        def __init__(self, channels):
            super().__init__()
            self.conv1 = nn.Conv2d(channels, channels, 3, padding=1)
            self.bn1 = nn.BatchNorm2d(channels)
            self.conv2 = nn.Conv2d(channels, channels, 3, padding=1)
            self.bn2 = nn.BatchNorm2d(channels)
        
        def forward(self, x):
            residual = x
            out = torch.relu(self.bn1(self.conv1(x)))
            out = self.bn2(self.conv2(out))
            return torch.relu(out + residual)
    
    class SmallResNet(nn.Module):
        def __init__(self):
            super().__init__()
            self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
            self.bn1 = nn.BatchNorm2d(16)
            self.block1 = ResBlock(16)
            self.block2 = ResBlock(16)
            self.pool = nn.AdaptiveAvgPool2d(1)
            self.fc = nn.Linear(16, 10)
        
        def forward(self, x):
            x = torch.relu(self.bn1(self.conv1(x)))
            x = self.block1(x)
            x = self.block2(x)
            x = self.pool(x).flatten(1)
            return self.fc(x)
    
    model = SmallResNet()
    
    # Compress with 2-bit clustering, skip first/last
    compressor = compress_model(
        model, bits=2, dim=1, tau=1e-3, skip_first_last=True
    )
    
    # Full training step
    optimizer = optim.SGD(compressor.parameters(), lr=0.01, momentum=0.9)
    criterion = nn.CrossEntropyLoss()
    
    compressor.train()
    x = torch.randn(4, 3, 32, 32)
    y = torch.randint(0, 10, (4,))
    
    out = compressor(x)
    loss = criterion(out, y)
    loss.backward()
    optimizer.step()
    
    if math.isnan(loss.item()):
        all_passed = test_failed("resnet train", "NaN loss")
    else:
        test_passed(f"ResNet training step: loss={loss.item():.4f}")
    
    # Get compression info
    info = compressor.get_compression_info()
    test_passed(f"Compression ratio: {info['compression_ratio']:.2f}x, "
                f"Size: {info['original_size_mb']:.3f}MB β†’ {info['compressed_size_mb']:.3f}MB")
    
    return all_passed


def run_all_tests():
    """Run all tests and report results."""
    print("=" * 70)
    print("DKM Implementation Test Suite")
    print("Based on: 'DKM: Differentiable K-Means Clustering Layer for")
    print("           Neural Network Compression' (ICLR 2022, arXiv:2108.12659)")
    print("=" * 70)
    
    tests = [
        ("DKM Layer Basic", test_dkm_layer_basic),
        ("Distance Matrix", test_distance_matrix),
        ("Attention Matrix", test_attention_matrix),
        ("Centroid Update", test_centroid_update),
        ("Gradient Flow", test_gradient_flow),
        ("Multi-Dim Clustering", test_multidim_clustering),
        ("Convergence", test_convergence),
        ("Compressor Wrapper", test_compressor_wrapper),
        ("Weight Snapping", test_snap_weights),
        ("Export Compressed", test_export_compressed),
        ("Training Step", test_training_step),
        ("Paper Configurations", test_paper_configurations),
        ("K-means++ Init", test_kmeans_plus_plus),
        ("Warm Start", test_warm_start),
        ("Numerical Stability", test_numerical_stability),
        ("ResNet Compression", test_resnet_compression),
    ]
    
    results = {}
    for name, test_fn in tests:
        try:
            passed = test_fn()
            results[name] = passed
        except Exception as e:
            print(f"\n  βœ—βœ—βœ— EXCEPTION in {name}: {e}")
            traceback.print_exc()
            results[name] = False
    
    # Summary
    print("\n" + "=" * 70)
    print("TEST SUMMARY")
    print("=" * 70)
    
    total = len(results)
    passed = sum(1 for v in results.values() if v)
    failed = total - passed
    
    for name, result in results.items():
        status = "PASS βœ“" if result else "FAIL βœ—"
        print(f"  [{status}] {name}")
    
    print(f"\n{passed}/{total} test groups passed, {failed} failed")
    
    if failed > 0:
        print("\n⚠ Some tests failed! Review the output above for details.")
        return False
    else:
        print("\nβœ“ All tests passed!")
        return True


if __name__ == "__main__":
    torch.manual_seed(42)
    success = run_all_tests()
    sys.exit(0 if success else 1)