Skip to content

Commit 4930e7c

Browse files
authored
Fix quantized_w8a32_gru output shape to preserve batch dimensions
Differential Revision: D92570517 Pull Request resolved: #17291
1 parent 2d2e422 commit 4930e7c

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎backends/cadence/aot/ops_registrations.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2858,7 +2858,7 @@ def quantized_w8a32_gru_meta(
28582858
bias_hidden: torch.Tensor,
28592859
b_h_scale: float,
28602860
) -> torch.Tensor:
2861-
return hidden.new_empty((2, hidden.shape[-1]), dtype=torch.float32)
2861+
return hidden.new_empty((2, *hidden.shape), dtype=torch.float32)
28622862

28632863

28642864
@register_fake("cadence::slice_scatter_")

‎backends/cadence/aot/ref_implementations.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,11 +1019,13 @@ def quantized_w8a32_gru(
10191019
assert inputs.dtype == torch.float32
10201020
assert hidden.dtype == torch.float32
10211021

1022-
if len(hidden.shape) > 2:
1023-
raise ValueError("Hidden state must be 2D or 1D")
1024-
1025-
if len(hidden.shape) == 2 and hidden.shape[0] != 1:
1026-
raise ValueError("Leading dimension of hidden state must be 1")
1022+
# Hidden state can be 1D, 2D (1, hidden_dim), or 3D (1, 1, hidden_dim).
1023+
# All leading dimensions must be 1.
1024+
for d in range(len(hidden.shape) - 1):
1025+
if hidden.shape[d] != 1:
1026+
raise ValueError(
1027+
f"Leading dimension {d} of hidden state must be 1, got {hidden.shape[d]}"
1028+
)
10271029

10281030
original_hidden_shape = hidden.shape
10291031
hidden = hidden.view(-1)
@@ -1059,7 +1061,7 @@ def quantized_w8a32_gru(
10591061

10601062
assert new_hidden.shape == original_hidden_shape
10611063

1062-
new_hidden = new_hidden.view(-1)
1064+
new_hidden = new_hidden.view(original_hidden_shape)
10631065
return torch.stack([new_hidden, new_hidden], dim=0)
10641066

10651067

‎backends/cadence/aot/tests/test_ref_implementations.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ def test_quantized_w8a32_gru(
29522952
b_h_scale,
29532953
)
29542954
self.assertIn(
2955-
"Leading dimension of hidden state must be 1", str(context.exception)
2955+
"Leading dimension 0 of hidden state must be 1", str(context.exception)
29562956
)
29572957
return
29582958

@@ -2977,8 +2977,8 @@ def test_quantized_w8a32_gru(
29772977
)
29782978
self.assertEqual(
29792979
output.shape,
2980-
(2, hidden.shape[-1]),
2981-
f"Output shape should match {(2, hidden.shape[-1])} in {name}",
2980+
(2, *hidden.shape),
2981+
f"Output shape should match {(2, *hidden.shape)} in {name}",
29822982
)
29832983
assert isinstance(output, torch.Tensor)
29842984

0 commit comments

Comments
 (0)