AbstractPhil commited on
Commit
52a917f
·
verified ·
1 Parent(s): 89ad98a

Update modeling_captionbert.py

Browse files
Files changed (1) hide show
  1. modeling_captionbert.py +41 -9
modeling_captionbert.py CHANGED
@@ -1,8 +1,8 @@
1
  # ============================================================================
2
- # modeling_captionbert.py -- AbstractPhil/captionbert-8192-v2-b
3
  #
4
  # from transformers import AutoModel, AutoTokenizer
5
- # model = AutoModel.from_pretrained("AbstractPhil/captionbert-8192-v2-b",
6
  # trust_remote_code=True)
7
  # tok = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
8
  # out = model(**tok(["a cat on a windowsill"], return_tensors="pt"))
@@ -237,11 +237,22 @@ class CaptionBertV2Binding:
237
  # mechanism, not a bug -- see the model card.
238
  # ---------------------------------------------------------------------------
239
 
240
- AMOE_REPO = "AbstractPhil/captionbert-8192-v2"
 
 
 
 
 
241
  AMOE_LIB = "amoe/arms"
242
- AMOE_DEFAULT_DISPATCH = "amoe/collective/captionbert-v2-collective.dispatch.pt"
243
- # campaign folders to search when the canonical library has not been published
244
- AMOE_FALLBACKS = lambda n: [f"amoe/collective/{n}.anchor.pt", f"amoe/moe/{n}.anchor.pt"]
 
 
 
 
 
 
245
  _AMOE_HINT = ("amoe-lora is required for arms: "
246
  "pip install git+https://github.com/AbstractEyes/amoe-lora")
247
 
@@ -274,8 +285,7 @@ class _ArmsOff:
274
  d.enabled = list(p)
275
 
276
 
277
- def _attach_amoe(self, arms=None, dispatch=AMOE_DEFAULT_DISPATCH,
278
- repo=AMOE_REPO, tau=None, verify=True):
279
  """
280
  Attach AMOE arms under a trained dispatch.
281
 
@@ -285,11 +295,33 @@ def _attach_amoe(self, arms=None, dispatch=AMOE_DEFAULT_DISPATCH,
285
  dispatch path in `repo`, a local file, or None for an untrained dispatch.
286
  verify assert that all-arms-disabled reproduces the bare trunk.
287
  """
288
- from huggingface_hub import hf_hub_download
289
  A = _amoe()
290
  if getattr(self, "_amoe_original_layers", None) is not None:
291
  raise RuntimeError("arms already attached; call detach_amoe() first")
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  dck = None
294
  if dispatch is not None:
295
  p = dispatch if os.path.exists(dispatch) else hf_hub_download(repo, dispatch)
 
1
  # ============================================================================
2
+ # modeling_captionbert.py -- AbstractPhil/captionbert-8192-v2
3
  #
4
  # from transformers import AutoModel, AutoTokenizer
5
+ # model = AutoModel.from_pretrained("AbstractPhil/captionbert-8192-v2",
6
  # trust_remote_code=True)
7
  # tok = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
8
  # out = model(**tok(["a cat on a windowsill"], return_tensors="pt"))
 
237
  # mechanism, not a bug -- see the model card.
238
  # ---------------------------------------------------------------------------
239
 
240
+ # AMOE_REPO defaults to THE REPO THIS MODEL WAS LOADED FROM, not a hardcoded
241
+ # name. Anchors are TRUNK-BOUND: measured 2026-08-02, the captionbert-8192-v2
242
+ # arms lose 31% of their gain on the -B trunk (.7287 -> .6863), and re-aligning
243
+ # the routing keys alone recovers only 29% of that. Retrained natively they
244
+ # reach .7294. So a hardcoded arms repo silently hands a trunk the wrong arms.
245
+ AMOE_REPO = None # None -> config._name_or_path
246
  AMOE_LIB = "amoe/arms"
247
+ # dispatch checkpoints are searched in order; the first that exists wins
248
+ AMOE_DISPATCH_CANDIDATES = (
249
+ "amoe/b-collective/captionbert-b-arms-native.dispatch.pt",
250
+ "amoe/collective/captionbert-v2-collective.dispatch.pt",
251
+ )
252
+ AMOE_DEFAULT_DISPATCH = None # None -> first candidate present in the repo
253
+ AMOE_FALLBACKS = lambda n: [f"amoe/b-collective/{n}.anchor.pt",
254
+ f"amoe/collective/{n}.anchor.pt",
255
+ f"amoe/moe/{n}.anchor.pt"]
256
  _AMOE_HINT = ("amoe-lora is required for arms: "
257
  "pip install git+https://github.com/AbstractEyes/amoe-lora")
258
 
 
285
  d.enabled = list(p)
286
 
287
 
288
+ def _attach_amoe(self, arms=None, dispatch=None, repo=None, tau=None, verify=True):
 
289
  """
290
  Attach AMOE arms under a trained dispatch.
291
 
 
295
  dispatch path in `repo`, a local file, or None for an untrained dispatch.
296
  verify assert that all-arms-disabled reproduces the bare trunk.
297
  """
298
+ from huggingface_hub import hf_hub_download, HfApi
299
  A = _amoe()
300
  if getattr(self, "_amoe_original_layers", None) is not None:
301
  raise RuntimeError("arms already attached; call detach_amoe() first")
302
 
303
+ # resolve the repo: this model's own, unless told otherwise
304
+ if repo is None:
305
+ repo = AMOE_REPO or getattr(self.config, "_name_or_path", None)
306
+ if not repo:
307
+ raise ValueError("cannot determine the arms repo; pass repo=...")
308
+ # resolve the dispatch: first candidate actually present in THAT repo
309
+ if dispatch is None:
310
+ dispatch = AMOE_DEFAULT_DISPATCH
311
+ if dispatch is None:
312
+ try:
313
+ present = set(HfApi().list_repo_files(repo))
314
+ except Exception:
315
+ present = set()
316
+ dispatch = next((c for c in AMOE_DISPATCH_CANDIDATES if c in present), None)
317
+ if dispatch is None:
318
+ raise FileNotFoundError(
319
+ f"no dispatch found in {repo}. Tried {list(AMOE_DISPATCH_CANDIDATES)}. "
320
+ f"Pass dispatch=<path>, or dispatch=False for an untrained one.")
321
+ print(f"[amoe] dispatch: {repo}/{dispatch}")
322
+ if dispatch is False:
323
+ dispatch = None
324
+
325
  dck = None
326
  if dispatch is not None:
327
  p = dispatch if os.path.exists(dispatch) else hf_hub_download(repo, dispatch)