|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Unit tests for SAM vision encoder weight rename logic.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from mobius.components._sam_vision import preprocess_sam_encoder_weights |
| 9 | + |
| 10 | + |
| 11 | +class TestPreprocessSamEncoderWeights: |
| 12 | + """Test preprocess_sam_encoder_weights with fake state dicts.""" |
| 13 | + |
| 14 | + def test_patch_embed_projection(self): |
| 15 | + sd = {"patch_embed.projection.weight": "w"} |
| 16 | + result = preprocess_sam_encoder_weights(sd) |
| 17 | + assert "patch_embed.proj.weight" in result |
| 18 | + |
| 19 | + def test_neck_conv1_to_indexed(self): |
| 20 | + sd = {"neck.conv1.weight": "w"} |
| 21 | + result = preprocess_sam_encoder_weights(sd) |
| 22 | + assert "neck.0.weight" in result |
| 23 | + |
| 24 | + def test_neck_layer_norm1_to_indexed(self): |
| 25 | + sd = {"neck.layer_norm1.weight": "w", "neck.layer_norm1.bias": "b"} |
| 26 | + result = preprocess_sam_encoder_weights(sd) |
| 27 | + assert "neck.1.weight" in result |
| 28 | + assert "neck.1.bias" in result |
| 29 | + |
| 30 | + def test_neck_conv2_to_indexed(self): |
| 31 | + sd = {"neck.conv2.weight": "w"} |
| 32 | + result = preprocess_sam_encoder_weights(sd) |
| 33 | + assert "neck.2.weight" in result |
| 34 | + |
| 35 | + def test_neck_layer_norm2_to_indexed(self): |
| 36 | + sd = {"neck.layer_norm2.weight": "w"} |
| 37 | + result = preprocess_sam_encoder_weights(sd) |
| 38 | + assert "neck.3.weight" in result |
| 39 | + |
| 40 | + def test_layers_to_blocks(self): |
| 41 | + sd = {"layers.0.attn.q_proj.weight": "w"} |
| 42 | + result = preprocess_sam_encoder_weights(sd) |
| 43 | + assert "blocks.0.attn.q_proj.weight" in result |
| 44 | + |
| 45 | + def test_layer_norm1_to_norm1(self): |
| 46 | + sd = {"layers.0.layer_norm1.weight": "w"} |
| 47 | + result = preprocess_sam_encoder_weights(sd) |
| 48 | + assert "blocks.0.norm1.weight" in result |
| 49 | + |
| 50 | + def test_layer_norm2_to_norm2(self): |
| 51 | + sd = {"layers.1.layer_norm2.bias": "b"} |
| 52 | + result = preprocess_sam_encoder_weights(sd) |
| 53 | + assert "blocks.1.norm2.bias" in result |
| 54 | + |
| 55 | + def test_mlp_lin1_to_up_proj(self): |
| 56 | + sd = {"layers.0.mlp.lin1.weight": "w"} |
| 57 | + result = preprocess_sam_encoder_weights(sd) |
| 58 | + assert "blocks.0.mlp.up_proj.weight" in result |
| 59 | + |
| 60 | + def test_mlp_lin2_to_down_proj(self): |
| 61 | + sd = {"layers.0.mlp.lin2.bias": "b"} |
| 62 | + result = preprocess_sam_encoder_weights(sd) |
| 63 | + assert "blocks.0.mlp.down_proj.bias" in result |
| 64 | + |
| 65 | + def test_full_state_dict_roundtrip(self): |
| 66 | + """All keys in a typical SAM state dict get renamed.""" |
| 67 | + fake_sd = { |
| 68 | + "patch_embed.projection.weight": "w", |
| 69 | + "patch_embed.projection.bias": "b", |
| 70 | + "layers.0.layer_norm1.weight": "w", |
| 71 | + "layers.0.layer_norm1.bias": "b", |
| 72 | + "layers.0.attn.q_proj.weight": "w", |
| 73 | + "layers.0.attn.k_proj.weight": "w", |
| 74 | + "layers.0.attn.v_proj.weight": "w", |
| 75 | + "layers.0.attn.proj.weight": "w", |
| 76 | + "layers.0.layer_norm2.weight": "w", |
| 77 | + "layers.0.mlp.lin1.weight": "w", |
| 78 | + "layers.0.mlp.lin2.weight": "w", |
| 79 | + "neck.conv1.weight": "w", |
| 80 | + "neck.layer_norm1.weight": "w", |
| 81 | + "neck.layer_norm1.bias": "b", |
| 82 | + "neck.conv2.weight": "w", |
| 83 | + "neck.layer_norm2.weight": "w", |
| 84 | + "neck.layer_norm2.bias": "b", |
| 85 | + "pos_embed": "pe", |
| 86 | + } |
| 87 | + result = preprocess_sam_encoder_weights(fake_sd) |
| 88 | + expected_keys = { |
| 89 | + "patch_embed.proj.weight", |
| 90 | + "patch_embed.proj.bias", |
| 91 | + "blocks.0.norm1.weight", |
| 92 | + "blocks.0.norm1.bias", |
| 93 | + "blocks.0.attn.q_proj.weight", |
| 94 | + "blocks.0.attn.k_proj.weight", |
| 95 | + "blocks.0.attn.v_proj.weight", |
| 96 | + "blocks.0.attn.proj.weight", |
| 97 | + "blocks.0.norm2.weight", |
| 98 | + "blocks.0.mlp.up_proj.weight", |
| 99 | + "blocks.0.mlp.down_proj.weight", |
| 100 | + "neck.0.weight", |
| 101 | + "neck.1.weight", |
| 102 | + "neck.1.bias", |
| 103 | + "neck.2.weight", |
| 104 | + "neck.3.weight", |
| 105 | + "neck.3.bias", |
| 106 | + "pos_embed", |
| 107 | + } |
| 108 | + assert set(result.keys()) == expected_keys |
0 commit comments