Uniform float32 weights, clean dtype handling: modeling_slip.py
Browse files- modeling_slip.py +11 -16
modeling_slip.py
CHANGED
|
@@ -170,9 +170,9 @@ class MultiSizePatchEmbed(nn.Module):
|
|
| 170 |
res_outputs = torch.empty(len(x_list), N, self.hidden_size, device=device, dtype=dtype)
|
| 171 |
for psize in unique_sizes.tolist():
|
| 172 |
idxs = (sizes == psize).nonzero(as_tuple=True)[0]
|
| 173 |
-
xs = torch.stack([x_list[i] for i in idxs]).to(device=device,
|
| 174 |
-
mask = torch.stack([attention_mask[i] for i in idxs]).to(device=device,
|
| 175 |
-
ti = torch.stack([time_idx[i] for i in idxs]).to(device=device,
|
| 176 |
xs = torch.cat([xs, mask, ti], dim=-1)
|
| 177 |
w, b, r_w, r_b = self.resize_weight(psize * 3)
|
| 178 |
res_outputs[idxs] = F.linear(xs, r_w, r_b)
|
|
@@ -193,10 +193,9 @@ class PatchEmbedding(nn.Module):
|
|
| 193 |
self.residual_layer = nn.Linear(patch_size * 3, hidden_size)
|
| 194 |
|
| 195 |
def forward(self, x, mask, time_idx):
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
time_idx = rearrange(time_idx, 'bs nvar (nump ps) -> (bs nvar) nump ps', ps=self.patch_size).to(dtype=dtype)
|
| 200 |
x = torch.cat([x, mask, time_idx], dim=-1)
|
| 201 |
return self.dropout(self.output_layer(self.act(self.hidden_layer(x)))) + self.residual_layer(x)
|
| 202 |
|
|
@@ -244,10 +243,6 @@ class CrossAttention(nn.Module):
|
|
| 244 |
self.o_proj = nn.Linear(dim, dim, bias=False)
|
| 245 |
|
| 246 |
def forward(self, query, context, attention_mask=None, **kwargs):
|
| 247 |
-
# Cast inputs to match weight dtype (handles mixed-precision boundaries)
|
| 248 |
-
weight_dtype = self.q_proj.weight.dtype
|
| 249 |
-
query = query.to(dtype=weight_dtype)
|
| 250 |
-
context = context.to(dtype=weight_dtype)
|
| 251 |
bsz, q_len, _ = query.size()
|
| 252 |
assert context.size(0) == bsz, (
|
| 253 |
f"Context batch size ({context.size(0)}) must match query batch size ({bsz}). "
|
|
@@ -424,8 +419,6 @@ class Gemma3MultimodalLayer(nn.Module):
|
|
| 424 |
assert self.vis_x is not None, "vis_x must be set before forward pass."
|
| 425 |
outputs = self.original_layer(hidden_states, **kwargs)
|
| 426 |
hidden_states = self.cross_attn_block(outputs[0], context=self.vis_x)
|
| 427 |
-
# Match dtype of original layer output (cross-attn may use different dtype)
|
| 428 |
-
hidden_states = hidden_states.to(dtype=outputs[0].dtype)
|
| 429 |
return (hidden_states,) + outputs[1:]
|
| 430 |
|
| 431 |
|
|
@@ -440,6 +433,9 @@ class Gemma3MultimodalModel(nn.Module):
|
|
| 440 |
model_id, trust_remote_code=True)
|
| 441 |
else:
|
| 442 |
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
| 443 |
self.model = AutoModelForCausalLM.from_config(
|
| 444 |
config, trust_remote_code=True)
|
| 445 |
|
|
@@ -455,8 +451,7 @@ class Gemma3MultimodalModel(nn.Module):
|
|
| 455 |
self.model.model.layers[i], Residual(cross_attn))
|
| 456 |
|
| 457 |
def condition_image(self, image_embeds):
|
| 458 |
-
|
| 459 |
-
self.image_embeds = image_embeds.to(device=param.device, dtype=param.dtype)
|
| 460 |
for layer in self.model.model.layers:
|
| 461 |
if isinstance(layer, Gemma3MultimodalLayer):
|
| 462 |
layer.condition_vis_x(self.image_embeds)
|
|
@@ -487,7 +482,7 @@ class EmbedToLatents(nn.Module):
|
|
| 487 |
self.to_latents = nn.Linear(dim, dim_latents, bias=False)
|
| 488 |
|
| 489 |
def forward(self, x):
|
| 490 |
-
return F.normalize(self.to_latents(x
|
| 491 |
|
| 492 |
|
| 493 |
class SLIPPreTrainedModel(PreTrainedModel):
|
|
|
|
| 170 |
res_outputs = torch.empty(len(x_list), N, self.hidden_size, device=device, dtype=dtype)
|
| 171 |
for psize in unique_sizes.tolist():
|
| 172 |
idxs = (sizes == psize).nonzero(as_tuple=True)[0]
|
| 173 |
+
xs = torch.stack([x_list[i] for i in idxs]).to(device=device, non_blocking=True)
|
| 174 |
+
mask = torch.stack([attention_mask[i] for i in idxs]).to(device=device, non_blocking=True)
|
| 175 |
+
ti = torch.stack([time_idx[i] for i in idxs]).to(device=device, non_blocking=True)
|
| 176 |
xs = torch.cat([xs, mask, ti], dim=-1)
|
| 177 |
w, b, r_w, r_b = self.resize_weight(psize * 3)
|
| 178 |
res_outputs[idxs] = F.linear(xs, r_w, r_b)
|
|
|
|
| 193 |
self.residual_layer = nn.Linear(patch_size * 3, hidden_size)
|
| 194 |
|
| 195 |
def forward(self, x, mask, time_idx):
|
| 196 |
+
x = rearrange(x, 'bs nvar (nump ps) -> (bs nvar) nump ps', ps=self.patch_size)
|
| 197 |
+
mask = rearrange(mask, 'bs nvar (nump ps) -> (bs nvar) nump ps', ps=self.patch_size)
|
| 198 |
+
time_idx = rearrange(time_idx, 'bs nvar (nump ps) -> (bs nvar) nump ps', ps=self.patch_size)
|
|
|
|
| 199 |
x = torch.cat([x, mask, time_idx], dim=-1)
|
| 200 |
return self.dropout(self.output_layer(self.act(self.hidden_layer(x)))) + self.residual_layer(x)
|
| 201 |
|
|
|
|
| 243 |
self.o_proj = nn.Linear(dim, dim, bias=False)
|
| 244 |
|
| 245 |
def forward(self, query, context, attention_mask=None, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
bsz, q_len, _ = query.size()
|
| 247 |
assert context.size(0) == bsz, (
|
| 248 |
f"Context batch size ({context.size(0)}) must match query batch size ({bsz}). "
|
|
|
|
| 419 |
assert self.vis_x is not None, "vis_x must be set before forward pass."
|
| 420 |
outputs = self.original_layer(hidden_states, **kwargs)
|
| 421 |
hidden_states = self.cross_attn_block(outputs[0], context=self.vis_x)
|
|
|
|
|
|
|
| 422 |
return (hidden_states,) + outputs[1:]
|
| 423 |
|
| 424 |
|
|
|
|
| 433 |
model_id, trust_remote_code=True)
|
| 434 |
else:
|
| 435 |
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
| 436 |
+
# Override Gemma's default bfloat16 so all params init in float32;
|
| 437 |
+
# users control runtime dtype via AutoModel.from_pretrained(torch_dtype=...)
|
| 438 |
+
config.torch_dtype = torch.float32
|
| 439 |
self.model = AutoModelForCausalLM.from_config(
|
| 440 |
config, trust_remote_code=True)
|
| 441 |
|
|
|
|
| 451 |
self.model.model.layers[i], Residual(cross_attn))
|
| 452 |
|
| 453 |
def condition_image(self, image_embeds):
|
| 454 |
+
self.image_embeds = image_embeds
|
|
|
|
| 455 |
for layer in self.model.model.layers:
|
| 456 |
if isinstance(layer, Gemma3MultimodalLayer):
|
| 457 |
layer.condition_vis_x(self.image_embeds)
|
|
|
|
| 482 |
self.to_latents = nn.Linear(dim, dim_latents, bias=False)
|
| 483 |
|
| 484 |
def forward(self, x):
|
| 485 |
+
return F.normalize(self.to_latents(x), dim=-1)
|
| 486 |
|
| 487 |
|
| 488 |
class SLIPPreTrainedModel(PreTrainedModel):
|