Skip to content
Open
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
2 changes: 1 addition & 1 deletion backends/cadence/aot/ops_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2858,7 +2858,7 @@ def quantized_w8a32_gru_meta(
bias_hidden: torch.Tensor,
b_h_scale: float,
) -> torch.Tensor:
return hidden.new_empty((2, hidden.shape[-1]), dtype=torch.float32)
return hidden.new_empty((2, *hidden.shape), dtype=torch.float32)


@register_fake("cadence::slice_scatter_")
Expand Down
14 changes: 8 additions & 6 deletions backends/cadence/aot/ref_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,13 @@ def quantized_w8a32_gru(
assert inputs.dtype == torch.float32
assert hidden.dtype == torch.float32

if len(hidden.shape) > 2:
raise ValueError("Hidden state must be 2D or 1D")

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

original_hidden_shape = hidden.shape
hidden = hidden.view(-1)
Expand Down Expand Up @@ -1059,7 +1061,7 @@ def quantized_w8a32_gru(

assert new_hidden.shape == original_hidden_shape

new_hidden = new_hidden.view(-1)
new_hidden = new_hidden.view(original_hidden_shape)
return torch.stack([new_hidden, new_hidden], dim=0)


Expand Down
6 changes: 3 additions & 3 deletions backends/cadence/aot/tests/test_ref_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,7 @@ def test_quantized_w8a32_gru(
b_h_scale,
)
self.assertIn(
"Leading dimension of hidden state must be 1", str(context.exception)
"Leading dimension 0 of hidden state must be 1", str(context.exception)
)
return

Expand All @@ -2977,8 +2977,8 @@ def test_quantized_w8a32_gru(
)
self.assertEqual(
output.shape,
(2, hidden.shape[-1]),
f"Output shape should match {(2, hidden.shape[-1])} in {name}",
(2, *hidden.shape),
f"Output shape should match {(2, *hidden.shape)} in {name}",
)
assert isinstance(output, torch.Tensor)

Expand Down
Loading