Nadav-marketaem commited on
Commit
cb1e9fe
·
verified ·
1 Parent(s): aca8e34

Fix meta-device crash on newer transformers; document required transformers==4.46.3

Browse files

modeling_marketing_classifier.py: build the encoder via AutoModel.from_config() instead of AutoModel.from_pretrained() inside __init__. The nested from_pretrained call conflicted with the meta-device/low_cpu_mem_usage context that the outer PreTrainedModel.from_pretrained() wraps __init__ in on transformers>=5, causing 'You are using from_pretrained with a meta device context manager' errors (reported from a fresh Colab install). from_config is meta-safe and behaves identically here since the real encoder weights are always supplied by this class's own checkpoint, not downloaded separately -- verified byte-identical output vs the original MarketingClassifier class before and after this change.

Separately, transformers>=5 is not usable with this model regardless of the above fix: the frozen encoder's own custom code (Snowflake/snowflake-arctic-embed-m-v2.0's modeling_hf_alibaba_nlp_gte.py, not ours) hits an IndexError in its RoPE position_ids handling under transformers 5.x. README now states the transformers==4.46.3 requirement explicitly in How to Use.

Files changed (2) hide show
  1. README.md +2 -0
  2. modeling_marketing_classifier.py +9 -3
README.md CHANGED
@@ -144,6 +144,8 @@ Only BMse is recommended for production. Others are retained for reproducibility
144
 
145
  ## How to Use
146
 
 
 
147
  ### Pipeline (recommended)
148
 
149
  ```python
 
144
 
145
  ## How to Use
146
 
147
+ All usage paths below require `transformers` in the 4.46.x line (the frozen encoder's own custom code is not compatible with `transformers` 5.x): `pip install "transformers==4.46.3"`.
148
+
149
  ### Pipeline (recommended)
150
 
151
  ```python
modeling_marketing_classifier.py CHANGED
@@ -23,9 +23,15 @@ class MarketingClassifierForRegression(PreTrainedModel):
23
 
24
  encoder_config = AutoConfig.from_pretrained(config.backbone, trust_remote_code=True)
25
  encoder_config.use_memory_efficient_attention = False
26
- self.encoder = AutoModel.from_pretrained(
27
- config.backbone,
28
- config=encoder_config,
 
 
 
 
 
 
29
  add_pooling_layer=False,
30
  trust_remote_code=True,
31
  attn_implementation="sdpa",
 
23
 
24
  encoder_config = AutoConfig.from_pretrained(config.backbone, trust_remote_code=True)
25
  encoder_config.use_memory_efficient_attention = False
26
+ # Build the encoder architecture only (random weights, meta-device safe) --
27
+ # do NOT call AutoModel.from_pretrained() here. This class's own weights
28
+ # (loaded by the outer from_pretrained() call, which already contains the
29
+ # full encoder + head state dict) are what actually populate `self.encoder`.
30
+ # A nested from_pretrained() call here conflicts with the meta-device /
31
+ # low_cpu_mem_usage context that from_pretrained wraps __init__ in on
32
+ # newer transformers versions.
33
+ self.encoder = AutoModel.from_config(
34
+ encoder_config,
35
  add_pooling_layer=False,
36
  trust_remote_code=True,
37
  attn_implementation="sdpa",