Skip to content

Commit 93776ca

Browse files
authored
[Vulkan] Keep unrepresentable and symbolic scalars out of the delegate (pytorch#23247)
The binary-scalar and comparison kernels receive scalars as int32 or float, yet the partitioner accepted NaN, integers outside int32 and symbolic scalars, which lowered with wrong values or failed in the graph builder. is_scalar_value_supported rejects them for pow.Tensor_Scalar and the comparison scalar ops. The partitioner also rejects SymFloat and SymBool values and keeps SymInt producers on CPU when a consumer is unsupported; node support is memoized so that check stays linear. Part 8/15 of the Vulkan transformer and operator-conformance stack. Depends on pytorch#23246; review against the selected base branch. Integration PR: pytorch#23254. Validation: 5 passed, 2 warnings in 150.36s (0:02:30). Lintrunner and git diff --check pass. Native tests use MoltenVK with portable CPU kernels; hardware and SwiftShader CI are pending. Recreates pytorch#23208 through ghstack. Prior review discussion remains on that PR. Authored with OpenAI Codex; split planned with Claude Code. cc @SS-JIA @manuelcandales @digantdesai @cbilgin
1 parent 5f72739 commit 93776ca

3 files changed

Lines changed: 163 additions & 4 deletions

File tree

‎backends/vulkan/op_registry.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# pyre-unsafe
88

9+
import math
910
import operator
1011
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
1112

@@ -318,13 +319,26 @@ def register_bool_binary_ops():
318319
# =============================================================================
319320

320321

322+
def is_scalar_value_supported(value: Any, dtype: torch.dtype) -> bool:
323+
if type(value) not in (bool, int, float):
324+
return False
325+
if isinstance(value, float) and math.isnan(value):
326+
return False
327+
if dtype in utils.INT_T:
328+
return -(2**31) <= value <= 2**31 - 1
329+
return True
330+
331+
321332
@update_features(exir_ops.edge.aten.pow.Tensor_Scalar)
322-
def register_pow_tensor_scalar():
333+
def register_binary_scalar_ops():
323334
return OpFeatures(
324335
inputs_storage=utils.ANY_STORAGE,
325336
inputs_dtypes=utils.FP_T,
326337
supports_resize=True,
327338
supports_highdim=True,
339+
are_node_inputs_supported_fn=lambda node: is_scalar_value_supported(
340+
node.args[1], node.meta["val"].dtype
341+
),
328342
)
329343

330344

@@ -336,6 +350,9 @@ def register_eq_scalar():
336350
outputs_dtypes=utils.BOOL_T,
337351
supports_resize=True,
338352
supports_highdim=True,
353+
are_node_inputs_supported_fn=lambda node: is_scalar_value_supported(
354+
node.args[1], node.args[0].meta["val"].dtype
355+
),
339356
)
340357

341358

@@ -1890,6 +1907,9 @@ def register_compare_scalar_ops():
18901907
outputs_dtypes=utils.BOOL_T,
18911908
supports_resize=True,
18921909
supports_highdim=True,
1910+
are_node_inputs_supported_fn=lambda node: is_scalar_value_supported(
1911+
node.args[1], node.args[0].meta["val"].dtype
1912+
),
18931913
)
18941914

18951915

‎backends/vulkan/partitioner/vulkan_partitioner.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def __init__(
8787

8888
self.nn_module_blocklist = nn_module_blocklist
8989
self.nn_module_allowlist = nn_module_allowlist
90+
self._node_support: Dict[torch.fx.Node, bool] = {}
9091

9192
def op_node_is_compatible( # noqa: C901: Function is too complex
9293
self, node: torch.fx.Node, features: Optional[OpFeatures] = None
@@ -198,10 +199,27 @@ def log_skip(self, node: torch.fx.Node, reason: str) -> None:
198199
def is_node_supported(
199200
self, submodules: Mapping[str, torch.nn.Module], node: torch.fx.Node
200201
) -> bool:
201-
r = self._is_node_supported(node)
202-
return r
202+
return self._is_node_supported(node)
203+
204+
def _is_node_supported(self, node: torch.fx.Node) -> bool:
205+
if node not in self._node_support:
206+
self._node_support[node] = self._check_node_support(node)
207+
return self._node_support[node]
208+
209+
def _check_node_support(self, node: torch.fx.Node) -> bool: # noqa: C901
210+
if any(
211+
isinstance(arg.meta.get("val"), (torch.SymFloat, torch.SymBool))
212+
for arg in [node, *node.all_input_nodes]
213+
):
214+
self.log_skip(node, "symbolic float or bool values are not supported")
215+
return False
216+
217+
if utils.is_symint_node(node) and any(
218+
not self._is_node_supported(user) for user in node.users
219+
):
220+
self.log_skip(node, "symbolic scalar has an unsupported consumer")
221+
return False
203222

204-
def _is_node_supported(self, node: torch.fx.Node) -> bool: # noqa: C901
205223
# Check if tensor node dtype is supported by vulkan
206224
if utils.is_tensor_node(node) and not utils.io_dtypes_are_supported(node):
207225
self.log_skip(node, "dtype not supported")

‎backends/vulkan/test/test_vulkan_dynamic.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import torch
1717

18+
import torch.nn.functional as F
19+
1820
from executorch.backends.vulkan.partitioner.vulkan_partitioner import (
1921
parse_compile_options,
2022
VulkanPartitioner,
@@ -208,6 +210,125 @@ def test_constant_bool_mask(self):
208210
)
209211
self._run(edge, model, inputs, atol=0, rtol=0)
210212

213+
def test_nan_scalars_fall_back(self):
214+
class NanScalar(torch.nn.Module):
215+
def __init__(self, kind):
216+
super().__init__()
217+
self.kind = kind
218+
219+
def forward(self, x):
220+
if self.kind == "where":
221+
return torch.where(x > 0, x, torch.nan)
222+
if self.kind == "masked_fill":
223+
return x.masked_fill(x > 0, torch.nan)
224+
if self.kind == "full":
225+
return torch.full_like(x, torch.nan)
226+
if self.kind == "scalar_tensor":
227+
return torch.scalar_tensor(torch.nan)
228+
if self.kind == "pow":
229+
return x**torch.nan
230+
return torch.ops.aten.mul.Scalar(x, torch.nan)
231+
232+
inputs = [(torch.tensor([-1.0, 0.0, 1.0, 2.0]),)]
233+
for kind in ("where", "masked_fill", "full", "scalar_tensor", "pow", "mul"):
234+
with self.subTest(kind=kind):
235+
model = NanScalar(kind)
236+
edge = self._lower(model, inputs[0], fully_delegated=False)
237+
self._run(edge, model, inputs, atol=0, rtol=0, equal_nan=True)
238+
239+
def test_dynamic_scalar_values_fall_back(self):
240+
class DynamicScalars(torch.nn.Module):
241+
def forward(self, x):
242+
n = x.shape[0]
243+
value = n * 2
244+
return (
245+
x**value,
246+
torch.ops.aten.mul.Scalar(x, value),
247+
torch.full((n,), value),
248+
torch.scalar_tensor(value, dtype=torch.int64),
249+
torch.ops.aten.mul.Scalar(x, n * 0.5),
250+
x + torch.full((n,), n * 0.5),
251+
torch.full((n,), 0.5),
252+
F.gelu(x),
253+
torch.clamp(x, max=n * 0.5),
254+
F.leaky_relu(x, negative_slope=n * 0.1),
255+
)
256+
257+
model = DynamicScalars()
258+
inputs = [(torch.linspace(-0.9, 4.1, n),) for n in (4, 2, 7, 3, 4)]
259+
edge = self._lower(
260+
model, inputs[0], ({0: Dim("n", min=2, max=8)},), fully_delegated=False
261+
)
262+
self.assertTrue(_vulkan_graphs(edge))
263+
self._run(edge, model, inputs)
264+
265+
def test_dynamic_compare_scalars_fall_back(self):
266+
class Compare(torch.nn.Module):
267+
def __init__(self, op):
268+
super().__init__()
269+
self.op = op
270+
271+
def forward(self, x):
272+
return self.op(x, x.shape[1])
273+
274+
inputs = [
275+
(torch.arange(2 * s, dtype=torch.float32).reshape(2, s),)
276+
for s in (16, 3, 31, 2, 16)
277+
]
278+
for op in (torch.eq, torch.ne, torch.lt, torch.le, torch.gt, torch.ge):
279+
with self.subTest(op=op):
280+
model = Compare(op)
281+
edge = self._lower(
282+
model,
283+
inputs[0],
284+
({1: Dim("s", min=2, max=32)},),
285+
fully_delegated=False,
286+
)
287+
self.assertEqual(_vulkan_graphs(edge), [])
288+
self._run(edge, model, inputs, atol=0, rtol=0)
289+
290+
def test_compare_scalar_values_fall_back(self):
291+
class Compare(torch.nn.Module):
292+
def __init__(self, op, value):
293+
super().__init__()
294+
self.op = op
295+
self.value = value
296+
297+
def forward(self, x):
298+
return self.op(x, self.value)
299+
300+
for op in (torch.eq, torch.ne, torch.lt, torch.le, torch.gt, torch.ge):
301+
for x, value in (
302+
(torch.tensor([-1.0, 0.0, 1.0, 2.0]), torch.nan),
303+
(torch.tensor([-3, 0, 1, 7], dtype=torch.int32), 2**40),
304+
(torch.tensor([-(2**40), 0, 2**40, 2**40 + 1]), 2**40),
305+
):
306+
with self.subTest(op=op, dtype=x.dtype, value=value):
307+
model = Compare(op, value)
308+
inputs = [(x,)]
309+
edge = self._lower(model, inputs[0], fully_delegated=False)
310+
self.assertEqual(_vulkan_graphs(edge), [])
311+
self._run(edge, model, inputs, atol=0, rtol=0)
312+
313+
def test_compare_scalar_values(self):
314+
class Compare(torch.nn.Module):
315+
def __init__(self, op, value):
316+
super().__init__()
317+
self.op = op
318+
self.value = value
319+
320+
def forward(self, x):
321+
return self.op(x, self.value)
322+
323+
for op in (torch.eq, torch.ne, torch.lt, torch.le, torch.gt, torch.ge):
324+
for dtype in (torch.int32, torch.float32):
325+
for value in (2.0, 2.5, -1.5):
326+
with self.subTest(op=op, dtype=dtype, value=value):
327+
x = torch.arange(-7, 14, dtype=dtype).reshape(3, 7)
328+
model = Compare(op, value)
329+
edge = self._lower(model, (x,))
330+
self._run(edge, model, [(x,)], atol=0, rtol=0)
331+
211332
def test_4d_reductions(self):
212333
class Reduce(torch.nn.Module):
213334
def __init__(self, op, dim):

0 commit comments

Comments
 (0)