Skip to content

Commit f3dd4b4

Browse files
authored
[ET-VK] Keep unused placeholders in the delegate's input list
Fixes pytorch#22478. `VulkanBackend::execute` matches its `args` positionally against `ComputeGraph::inputs()`, but `process_placeholder_node` drops any placeholder with no users before it reaches `input_ids`. The delegate call still passes that argument, so the counts disagree and the model fails at every `execute()` with "Vulkan graph declares N inputs and M outputs, but the delegate call supplied K arguments". A placeholder can lose its only consumers to the passes that run inside `preprocess`, after partitioning has already fixed the call's argument list. A qwen3 0.6B export with attention kept on the host hits this: the final partition is handed 28 per-layer symints that nothing in the subgraph consumes by serialization time, and declares 2 inputs and 1 output against a 31 argument call. Params keep the existing skip, since they are serialized into the blob rather than passed at call time. Test: `test_unused_placeholder_is_still_declared_as_an_input` fails on `main` and passes with the change; the companion test covering used placeholders passes either way. `test_serialization.py`, `test_vulkan_compile_options.py` and `test_vulkan_passes.py` (17 tests) still pass.
1 parent 90c5a67 commit f3dd4b4

2 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎backends/vulkan/serialization/vulkan_graph_builder.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,26 @@ def get_or_create_value_for(self, arg: _Argument):
418418
raise RuntimeError(f"Cannot create value for arg of type {type(arg)}")
419419

420420
def process_placeholder_node(self, node: Node) -> None:
421-
# ignores any tensors that don't get used in any ops
422-
if len(node.users) == 0:
421+
# A non-param placeholder occupies a slot in the delegate call's
422+
# argument list whether or not this graph goes on to use it, and
423+
# VulkanBackend::execute matches `args` to graph inputs positionally.
424+
# Dropping an unused one from input_ids desynchronises the two, and the
425+
# runtime then rejects the call because it was handed more arguments
426+
# than the graph declares inputs and outputs. That happens in practice
427+
# when a placeholder's only consumers are folded away by the passes
428+
# that run after partitioning, so the graph the partitioner tagged and
429+
# the graph serialized here disagree about which inputs are live.
430+
if is_param_node(self.program, node):
431+
# Params are serialized into the blob rather than passed at call
432+
# time, so an unused one costs nothing to skip.
433+
if len(node.users) > 0:
434+
self.create_node_value(node)
423435
return None
424436
ids = self.create_node_value(node)
425-
if not is_param_node(self.program, node):
426-
if isinstance(ids, int):
427-
self.input_ids.append(ids)
428-
else:
429-
self.input_ids += ids
437+
if isinstance(ids, int):
438+
self.input_ids.append(ids)
439+
else:
440+
self.input_ids += ids
430441

431442
def process_getitem_node(self, node: Node) -> None:
432443
# Find ValueList id from the collection node.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import unittest
8+
9+
import torch
10+
from executorch.backends.vulkan.serialization.vulkan_graph_builder import VkGraphBuilder
11+
from executorch.backends.vulkan.vulkan_preprocess import apply_passes
12+
from executorch.exir import to_edge
13+
from executorch.exir.backend.utils import DelegateMappingBuilder
14+
from executorch.exir.passes import SpecPropPass
15+
16+
17+
class TestVkGraphBuilderInputIds(unittest.TestCase):
18+
"""The serialized input list has to match the delegate call's arguments.
19+
20+
VulkanBackend::execute walks `args` positionally against
21+
ComputeGraph::inputs() and rejects the call when the counts disagree, so
22+
every placeholder that the delegate call passes must appear in input_ids,
23+
including ones this graph happens not to use. Unused placeholders are not
24+
hypothetical: passes that run after partitioning can fold away a
25+
placeholder's only consumers, leaving the argument list and the serialized
26+
graph out of step.
27+
"""
28+
29+
def _build(self, module: torch.nn.Module, inputs) -> VkGraphBuilder:
30+
edge = to_edge(torch.export.export(module, inputs, strict=True))
31+
# The builder reads node specs, which the backend's own preprocess
32+
# populates before it gets here.
33+
program = apply_passes(edge.exported_program(), [SpecPropPass()])
34+
builder = VkGraphBuilder(
35+
program, DelegateMappingBuilder(generated_identifiers=True)
36+
)
37+
builder.build_graph()
38+
return builder
39+
40+
def test_unused_placeholder_is_still_declared_as_an_input(self) -> None:
41+
class UsesOnlyTheFirstInput(torch.nn.Module):
42+
def forward(self, used, unused):
43+
return used + used
44+
45+
builder = self._build(
46+
UsesOnlyTheFirstInput(), (torch.randn(2, 3), torch.randn(2, 3))
47+
)
48+
self.assertEqual(len(builder.input_ids), 2)
49+
50+
def test_used_placeholders_are_declared_in_order(self) -> None:
51+
class UsesBothInputs(torch.nn.Module):
52+
def forward(self, first, second):
53+
return first + second
54+
55+
builder = self._build(UsesBothInputs(), (torch.randn(2, 3), torch.randn(2, 3)))
56+
self.assertEqual(len(builder.input_ids), 2)
57+
58+
59+
if __name__ == "__main__":
60+
unittest.main()

0 commit comments

Comments
 (0)