paulomouraj commited on
Commit
04c7b58
·
verified ·
1 Parent(s): 0831449

Tie LM head to embeddings (149.7M params, was 188.4M); load on transformers 4.x; SparseEncoder honors revision and model_kwargs

Browse files

- model.safetensors: drop decoder.weight, an exact copy of the input embeddings
(38.7M duplicated params). config.json: tie_word_embeddings true.
- modeling_splade.py: tie_weights reports decoder.weight as missing so
transformers 5.x applies the tie for this remote-code model instead of leaving
it on meta.
- tokenizer_config.json: tokenizer_class PreTrainedTokenizerFast (was
TokenizersBackend, which transformers 4.x cannot load).
- custom_st.py: forward revision/token/cache_dir/local_files_only and
model_kwargs to from_pretrained (a pinned revision previously loaded main);
float32 sparse output.

Outputs are unchanged: query and document vectors match the previous revision
exactly (AutoModel and SparseEncoder, transformers 4.57 and 5.23).

Files changed (5) hide show
  1. config.json +1 -1
  2. custom_st.py +14 -6
  3. model.safetensors +2 -2
  4. modeling_splade.py +10 -0
  5. tokenizer_config.json +1 -1
config.json CHANGED
@@ -82,7 +82,7 @@
82
  "sep_token_id": 50282,
83
  "sparse_pred_ignore_index": -100,
84
  "sparse_prediction": false,
85
- "tie_word_embeddings": false,
86
  "transformers_version": "5.3.0",
87
  "vocab_fold": "case_space",
88
  "vocab_size": 50370
 
82
  "sep_token_id": 50282,
83
  "sparse_pred_ignore_index": -100,
84
  "sparse_prediction": false,
85
+ "tie_word_embeddings": true,
86
  "transformers_version": "5.3.0",
87
  "vocab_fold": "case_space",
88
  "vocab_size": 50370
custom_st.py CHANGED
@@ -2,10 +2,14 @@ from sentence_transformers.base.modules import InputModule
2
  from transformers import AutoModel, AutoTokenizer
3
  class SpladeSTModule(InputModule):
4
  save_in_root = True
5
- def __init__(self, model_name_or_path: str, **kwargs):
6
  super().__init__()
7
- self.model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True)
8
- self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) # for SparseEncoder.decode
 
 
 
 
9
  def preprocess(self, inputs, prompt=None, **kwargs):
10
  prefix = prompt or ""
11
  cfg = self.model.config
@@ -13,14 +17,18 @@ class SpladeSTModule(InputModule):
13
  ids, attn, pool, _ = self.model._tokenize(list(inputs), prefix, max_length)
14
  return {"input_ids": ids, "attention_mask": attn, "pooling_mask": pool}
15
  def forward(self, features, **kwargs):
 
16
  features["sentence_embedding"] = self.model(
17
  features["input_ids"], features["attention_mask"], features["pooling_mask"]
18
- )
19
  return features
20
  def get_embedding_dimension(self):
21
  return self.model.config.vocab_size
22
  @classmethod
23
- def load(cls, model_name_or_path, **kwargs):
24
- return cls(model_name_or_path)
 
 
 
25
  def save(self, output_path, **kwargs):
26
  pass # repo is assembled by export.py, never by ST save
 
2
  from transformers import AutoModel, AutoTokenizer
3
  class SpladeSTModule(InputModule):
4
  save_in_root = True
5
+ def __init__(self, model_name_or_path: str, model_kwargs: dict | None = None, **hub_kwargs):
6
  super().__init__()
7
+ # model_kwargs (e.g. {"dtype": torch.bfloat16}) go to from_pretrained.
8
+ self.model = AutoModel.from_pretrained(
9
+ model_name_or_path, trust_remote_code=True, **hub_kwargs, **(model_kwargs or {})
10
+ )
11
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, **hub_kwargs) # for SparseEncoder.decode
12
+ self.model._tokenizer = self.tokenizer # same revision as the weights
13
  def preprocess(self, inputs, prompt=None, **kwargs):
14
  prefix = prompt or ""
15
  cfg = self.model.config
 
17
  ids, attn, pool, _ = self.model._tokenize(list(inputs), prefix, max_length)
18
  return {"input_ids": ids, "attention_mask": attn, "pooling_mask": pool}
19
  def forward(self, features, **kwargs):
20
+ # [B, V] float32 (lossless upcast of bf16 weights), so scoring never runs in bf16.
21
  features["sentence_embedding"] = self.model(
22
  features["input_ids"], features["attention_mask"], features["pooling_mask"]
23
+ ).float()
24
  return features
25
  def get_embedding_dimension(self):
26
  return self.model.config.vocab_size
27
  @classmethod
28
+ def load(cls, model_name_or_path, subfolder="", token=None, cache_folder=None, revision=None,
29
+ local_files_only=False, model_kwargs=None, **kwargs):
30
+ # Forward the hub args: a pinned revision must load that revision's weights.
31
+ return cls(model_name_or_path, model_kwargs=model_kwargs, token=token, cache_dir=cache_folder,
32
+ revision=revision, local_files_only=local_files_only)
33
  def save(self, output_path, **kwargs):
34
  pass # repo is assembled by export.py, never by ST save
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:8b9116e4234b03d5c5eae21d9036d4f1568205152cc019a113abb73c4bdcccbc
3
- size 753780968
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:066de6bea174ba842674d12dad5f108d9ff99857cbfb3bc6116743bf3d5da0d9
3
+ size 599044232
modeling_splade.py CHANGED
@@ -57,6 +57,16 @@ class SpladeModel(ModernBertForMaskedLM):
57
  )
58
  self._tokenizer = None
59
 
 
 
 
 
 
 
 
 
 
 
60
  # -- forward path ---------------------------------------------------------
61
 
62
  def _token_weights(
 
57
  )
58
  self._tokenizer = None
59
 
60
+ def tie_weights(self, *args, **kwargs):
61
+ # decoder.weight is tied to the input embeddings and not stored. transformers
62
+ # 5.x drops it from `missing_keys` for remote-code models (its meta placeholder
63
+ # counts as initialized), then skips the tie and leaves it on meta. Report it
64
+ # missing so the standard tie applies. 4.x calls this without missing_keys.
65
+ missing_keys = kwargs.get("missing_keys", args[0] if args else None)
66
+ if missing_keys is not None and self.get_output_embeddings().weight.is_meta:
67
+ missing_keys.add("decoder.weight")
68
+ return super().tie_weights(*args, **kwargs)
69
+
70
  # -- forward path ---------------------------------------------------------
71
 
72
  def _token_weights(
tokenizer_config.json CHANGED
@@ -16,7 +16,7 @@
16
  "padding_side": "right",
17
  "sep_token": "[SEP]",
18
  "stride": 0,
19
- "tokenizer_class": "TokenizersBackend",
20
  "truncation_side": "right",
21
  "truncation_strategy": "longest_first",
22
  "unk_token": "[UNK]"
 
16
  "padding_side": "right",
17
  "sep_token": "[SEP]",
18
  "stride": 0,
19
+ "tokenizer_class": "PreTrainedTokenizerFast",
20
  "truncation_side": "right",
21
  "truncation_strategy": "longest_first",
22
  "unk_token": "[UNK]"