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
12 changes: 12 additions & 0 deletions backends/cadence/aot/ref_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,15 @@ def rms_norm(
eps: float,
) -> torch.Tensor:
return W * nn.RMSNorm(list(normalized_shape), eps=eps, dtype=X.dtype)(X)


@impl(m, "where_Scalar")
def where_Scalar(
condition: torch.Tensor,
if_true: float,
if_false: float,
) -> torch.Tensor:
if condition.dtype != torch.bool:
raise ValueError("condition must be a bool tensor")

return torch.where(condition, if_true, if_false)
11 changes: 11 additions & 0 deletions backends/cadence/aot/tests/test_ref_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,14 @@ def test_quantized_relu(
torch.equal(output, expected_output),
f"Output values don't match expected in {name}. Got {output}, expected {expected_output}",
)

def test_where_Scalar(self) -> None:
input_tensor = torch.tensor([1, 2, 3, 4], dtype=torch.int8)
out = torch.ops.cadence.where_Scalar(input_tensor > 2, 1.0, 0.0)
self.assertTrue(
torch.equal(out, torch.tensor([0.0, 0.0, 1.0, 1.0], dtype=torch.float32))
)
with self.assertRaises(ValueError) as context:
torch.ops.cadence.where_Scalar(input_tensor, 1.0, 0.0)

self.assertIn("condition must be a bool tensor", str(context.exception))
Loading