Skip to content

Commit 1ddfb2f

Browse files
feat: add state dict loading function for UniCoilEncoder model
1 parent fb4949f commit 1ddfb2f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

tests/test_models/test_coil.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor | None =
109109
return out
110110

111111

112+
def _load_unicoil_state_dict(model: UniCoilEncoder, hf_model: str) -> None:
113+
state_dict_path = hf_hub_download(hf_model, filename="pytorch_model.bin")
114+
state_dict = torch.load(state_dict_path, map_location="cpu")
115+
state_dict = {
116+
key.removeprefix("coil_encoder."): value
117+
for key, value in state_dict.items()
118+
if key.startswith(("coil_encoder.bert.", "coil_encoder.tok_proj."))
119+
}
120+
model.load_state_dict(state_dict, strict=False)
121+
122+
112123
@pytest.mark.model
113124
@pytest.mark.parametrize(
114125
"hf_model",
@@ -175,6 +186,7 @@ def test_same_as_unicoil(hf_model: str):
175186
]
176187

177188
orig_model = UniCoilEncoder.from_pretrained(hf_model)
189+
_load_unicoil_state_dict(orig_model, hf_model)
178190
orig_tokenizer = AutoTokenizer.from_pretrained(hf_model)
179191
orig_query_encoded = orig_tokenizer(
180192
query, padding=True, truncation=True, max_length=512, return_tensors="pt", return_token_type_ids=False

0 commit comments

Comments
 (0)