Spaces:
Sleeping
Sleeping
Commit ·
27f47e2
1
Parent(s): 306b259
Fix Spaces meta tensor model loading
Browse files1123448 unmodified lines535455565657585960616263646 unmodified lines717273747576777879808182838485868788899091929394959697989910010110222 unmodified lines125126127128129130131"""Evaluation: per-aspect metrics, aggregated overall comparison."""Evaluation: per-aspect metrics, aggregated overall comparison.
Two label encodings live in this project (keep them straight!): Per-aspect (ACSA): 0=Not_Mentioned, 1=Positive, 2=Negative48 unmodified lines try: return model.to(device) except NotImplementedError as exc: if "meta tensor" not in str(exc): if "meta" not in str(exc).lower(): raise logger.warning("Model contains meta tensors; using to_empty() before loading checkpoint weights.") return model.to_empty(device=device) except RuntimeError as exc: if "meta" not in str(exc).lower(): raise logger.warning("Model contains meta tensors; using to_empty() before loading checkpoint weights.") return model.to_empty(device=device)6 unmodified lines return model.load_state_dict(state_dict, strict=strict)
def _refresh_runtime_buffers(model, device): """Rebuild non-persistent buffers after a Spaces meta-tensor fallback.""" for module in model.modules(): if hasattr(module, "source_prior_bias") and hasattr(module, "num_aspects"): prior_rows = [] prior_cfg = getattr(cfg, "CROSS_ATTN_SOURCE_PRIOR", {}) for aspect in cfg.ASPECTS[: module.num_aspects]: row = list(prior_cfg.get(aspect, [1.0 / 3.0] * 3)) if len(row) < 3: row = row + [1.0 / 3.0] * (3 - len(row)) prior_rows.append(row[:3]) prior = torch.tensor(prior_rows, dtype=torch.float, device=device) prior = prior - prior.mean(dim=-1, keepdim=True) module.source_prior_bias = prior meta_params = [name for name, param in model.named_parameters() if getattr(param, "is_meta", False)] meta_buffers = [name for name, buf in model.named_buffers() if getattr(buf, "is_meta", False)] if meta_params or meta_buffers: raise RuntimeError( "Checkpoint did not materialize model tensors. First meta tensors: " + ", ".join((meta_params + meta_buffers)[:20]) ) return model
def load_meta_acsa(checkpoint_dir: Path = None, meta_encoder: Optional[MetaEncoder] = None, device=None): if checkpoint_dir is None:22 unmodified lines model = BertMetaFusionACSAModel(bert_name=bert_name, meta_in_dim=meta_in_dim) model = _move_model_to_device(model, device) _load_model_state(model, ckpt["model_state_dict"], strict=False) model = _refresh_runtime_buffers(model, device) model.eval() tokenizer = AutoTokenizer.from_pretrained(checkpoint_dir / "tokenizer") return model, tokenizer, meta_encoder, devicesrc/evaluator.py+34-2