Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/transformers/models/nemotron_h/modeling_nemotron_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ def cuda_kernels_forward(
)
hidden_states = hidden_states.view(batch_size, self.num_heads * self.head_dim)
hidden_states = self.norm(hidden_states, gate)
out = self.out_proj(hidden_states)[:, None, ...]
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(hidden_states.to(self.out_proj.weight.dtype))[:, None, ...]
# if no cache is found, calling the kernel
else:
if attention_mask is not None and not torch.all(attention_mask == 1):
Expand Down Expand Up @@ -369,7 +372,10 @@ def cuda_kernels_forward(
scan_output = scan_output.view(batch_size, seq_len, -1)
# Multiply "gate" branch and apply extra normalization layer
scan_output = self.norm(scan_output, gate)
out = self.out_proj(scan_output)
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(scan_output.to(self.out_proj.weight.dtype))
return out

# fmt: off
Expand Down
10 changes: 8 additions & 2 deletions src/transformers/models/zamba2/modeling_zamba2.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ def cuda_kernels_forward(
)
hidden_states = hidden_states.view(batch_size, self.num_heads * self.head_dim)
hidden_states = self.norm(hidden_states, gate)
out = self.out_proj(hidden_states)[:, None, ...]
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(hidden_states.to(self.out_proj.weight.dtype))[:, None, ...]
# if no cache is found, calling the kernel
else:
if attention_mask is not None and not torch.all(attention_mask == 1):
Expand Down Expand Up @@ -657,7 +660,10 @@ def cuda_kernels_forward(
scan_output = scan_output.view(batch_size, seq_len, -1)
# Multiply "gate" branch and apply extra normalization layer
scan_output = self.norm(scan_output, gate)
out = self.out_proj(scan_output)
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(scan_output.to(self.out_proj.weight.dtype))
return out

# fmt: off
Expand Down
10 changes: 8 additions & 2 deletions src/transformers/models/zamba2/modular_zamba2.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ def cuda_kernels_forward(
)
hidden_states = hidden_states.view(batch_size, self.num_heads * self.head_dim)
hidden_states = self.norm(hidden_states, gate)
out = self.out_proj(hidden_states)[:, None, ...]
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(hidden_states.to(self.out_proj.weight.dtype))[:, None, ...]
# if no cache is found, calling the kernel
else:
if attention_mask is not None and not torch.all(attention_mask == 1):
Expand Down Expand Up @@ -445,7 +448,10 @@ def cuda_kernels_forward(
scan_output = scan_output.view(batch_size, seq_len, -1)
# Multiply "gate" branch and apply extra normalization layer
scan_output = self.norm(scan_output, gate)
out = self.out_proj(scan_output)
# The SSM kernels return fp32 regardless of the module dtype; cast back to the
# projection weight dtype so the matmul does not fail when out_proj.weight is a
# narrower dtype than the activations (e.g. fp32 activations with a bf16 out_proj).
out = self.out_proj(scan_output.to(self.out_proj.weight.dtype))
return out

# fmt: off
Expand Down
Loading