Spaces:
Sleeping
Sleeping
Commit ·
822f6b9
1
Parent(s): 27f47e2
Materialize meta buffers during model load
Browse files- src/evaluator.py +26 -4
src/evaluator.py
CHANGED
|
@@ -71,6 +71,28 @@ def _load_model_state(model, state_dict, strict=True):
|
|
| 71 |
return model.load_state_dict(state_dict, strict=strict)
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
def _refresh_runtime_buffers(model, device):
|
|
@@ -87,12 +109,12 @@ def _refresh_runtime_buffers(model, device):
|
|
| 87 |
prior = torch.tensor(prior_rows, dtype=torch.float, device=device)
|
| 88 |
prior = prior - prior.mean(dim=-1, keepdim=True)
|
| 89 |
module.source_prior_bias = prior
|
|
|
|
| 90 |
meta_params = [name for name, param in model.named_parameters() if getattr(param, "is_meta", False)]
|
| 91 |
-
|
| 92 |
-
if meta_params or meta_buffers:
|
| 93 |
raise RuntimeError(
|
| 94 |
-
"Checkpoint did not materialize model
|
| 95 |
-
+ ", ".join(
|
| 96 |
)
|
| 97 |
return model
|
| 98 |
|
|
|
|
| 71 |
return model.load_state_dict(state_dict, strict=strict)
|
| 72 |
|
| 73 |
|
| 74 |
+
def _set_module_attr(root, dotted_name, value):
|
| 75 |
+
module = root
|
| 76 |
+
parts = dotted_name.split(".")
|
| 77 |
+
for part in parts[:-1]:
|
| 78 |
+
module = getattr(module, part)
|
| 79 |
+
setattr(module, parts[-1], value)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _materialize_meta_buffers(model, device):
|
| 83 |
+
"""Create real tensors for buffers that are not stored in older checkpoints."""
|
| 84 |
+
for name, buf in list(model.named_buffers()):
|
| 85 |
+
if not getattr(buf, "is_meta", False):
|
| 86 |
+
continue
|
| 87 |
+
shape = tuple(buf.shape)
|
| 88 |
+
dtype = buf.dtype
|
| 89 |
+
if name.endswith("position_ids") and len(shape) == 2:
|
| 90 |
+
value = torch.arange(shape[1], dtype=dtype, device=device).expand(shape[0], -1)
|
| 91 |
+
else:
|
| 92 |
+
value = torch.zeros(shape, dtype=dtype, device=device)
|
| 93 |
+
_set_module_attr(model, name, value)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
|
| 97 |
|
| 98 |
def _refresh_runtime_buffers(model, device):
|
|
|
|
| 109 |
prior = torch.tensor(prior_rows, dtype=torch.float, device=device)
|
| 110 |
prior = prior - prior.mean(dim=-1, keepdim=True)
|
| 111 |
module.source_prior_bias = prior
|
| 112 |
+
_materialize_meta_buffers(model, device)
|
| 113 |
meta_params = [name for name, param in model.named_parameters() if getattr(param, "is_meta", False)]
|
| 114 |
+
if meta_params:
|
|
|
|
| 115 |
raise RuntimeError(
|
| 116 |
+
"Checkpoint did not materialize model parameters. First meta parameters: "
|
| 117 |
+
+ ", ".join(meta_params[:20])
|
| 118 |
)
|
| 119 |
return model
|
| 120 |
|