Skip to content

Commit 22df868

Browse files
authored
Support 1D linear in the CPU Reference provider
Summary: `aten::linear` with a 1D input or a 1D weight failed at execution with `0x12` (InvalidArgument). The Reference provider routes every `linear` to `opt_linear_out`, which requires an input of rank >= 2 and a 2D weight. Portable has no `linear` kernel, and XNNPACK declines non-constant or non-2D weights. The Reference provider now binds 1D operands as single-row views: a `[K]` input or weight becomes `[1, K]`, and the output gains the matching unit dimension. Scalar bias is also viewed as `[1]` to satisfy the optimized kernel's bias contract. Views share the value's buffer, so nothing is copied. The partitioner keeps preserving these `linear` nodes instead of decomposing them, so a provider can later claim the 1D shapes with a dedicated kernel. Authored with assistance from Claude Code and Codex. Differential Revision: D128153701 Pull Request resolved: #24254
1 parent 7c72b5d commit 22df868

3 files changed

Lines changed: 97 additions & 11 deletions

File tree

‎backends/cpu/runtime/providers/reference/ReferenceProvider.cpp‎

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,46 @@ std::string_view check_node(
884884
return {};
885885
}
886886

887+
using Sizes = std::vector<aten::SizesType>;
888+
889+
struct LinearViews {
890+
std::array<Sizes, 3> inputs;
891+
Sizes output;
892+
};
893+
894+
// aten::linear.out requires an input of rank >= 2, a 2D weight and a 1D bias.
895+
std::optional<LinearViews> linear_views(
896+
const Kernel& node,
897+
const ptn::Graph& graph) {
898+
if (node.target != "torch.ops.aten.linear.default" ||
899+
node.inputs.size() < 2 ||
900+
node.inputs[0].arg.kind() != ptn::ArgKind::Tensor ||
901+
node.inputs[1].arg.kind() != ptn::ArgKind::Tensor) {
902+
return std::nullopt;
903+
}
904+
const auto& input =
905+
graph.value(node.inputs[0].arg.as_tensor().id).tensor_meta().sizes;
906+
const auto& weight =
907+
graph.value(node.inputs[1].arg.as_tensor().id).tensor_meta().sizes;
908+
if (input.empty() || (input.size() >= 2 && weight.size() == 2)) {
909+
return std::nullopt;
910+
}
911+
const auto row = [](const auto& sizes) {
912+
return sizes.size() == 1 ? Sizes{1, static_cast<aten::SizesType>(sizes[0])}
913+
: Sizes(sizes.begin(), sizes.end());
914+
};
915+
LinearViews views{{row(input), row(weight), {}}, {}};
916+
if (node.inputs.size() > 2 &&
917+
node.inputs[2].arg.kind() == ptn::ArgKind::Tensor) {
918+
const auto& bias =
919+
graph.value(node.inputs[2].arg.as_tensor().id).tensor_meta().sizes;
920+
views.inputs[2] = bias.empty() ? Sizes{1} : Sizes(bias.begin(), bias.end());
921+
}
922+
views.output.assign(views.inputs[0].begin(), views.inputs[0].end() - 1);
923+
views.output.push_back(views.inputs[1][0]);
924+
return views;
925+
}
926+
887927
struct Instruction {
888928
const ptn::Node* node = nullptr;
889929
OpFunction kernel = nullptr;
@@ -908,9 +948,16 @@ class ReferenceExecutable final : public Executable {
908948
};
909949

910950
Result<EValue*> tensor_value(ptn::ValueId id, bool writable) {
951+
const auto& shape = context_.graph.value(id).tensor_meta().sizes;
952+
return tensor_value(id, writable, Sizes(shape.begin(), shape.end()));
953+
}
954+
955+
// Views of one value share its buffer but keep separate tensor metadata.
956+
Result<EValue*>
957+
tensor_value(ptn::ValueId id, bool writable, const Sizes& sizes) {
911958
const auto existing = std::find_if(
912-
tensors_.begin(), tensors_.end(), [id](const auto& storage) {
913-
return storage->id == id;
959+
tensors_.begin(), tensors_.end(), [id, &sizes](const auto& storage) {
960+
return storage->id == id && storage->sizes == sizes;
914961
});
915962
if (existing != tensors_.end()) {
916963
(*existing)->writable |= writable;
@@ -921,23 +968,22 @@ class ReferenceExecutable final : public Executable {
921968
return bytes.error();
922969
}
923970
const auto& meta = context_.graph.value(id).tensor_meta();
924-
const auto& shape = meta.sizes;
925971
auto storage = std::make_unique<TensorStorage>();
926972
storage->id = id;
927973
storage->writable = writable;
928-
storage->sizes.assign(shape.begin(), shape.end());
929-
storage->order.resize(shape.size());
930-
storage->strides.resize(shape.size());
974+
storage->sizes = sizes;
975+
storage->order.resize(sizes.size());
976+
storage->strides.resize(sizes.size());
931977
int64_t stride = 1;
932-
for (size_t reverse = shape.size(); reverse > 0; --reverse) {
978+
for (size_t reverse = sizes.size(); reverse > 0; --reverse) {
933979
const auto dim = reverse - 1;
934980
storage->order[dim] = dim;
935981
storage->strides[dim] = static_cast<aten::StridesType>(stride);
936-
stride *= shape[dim];
982+
stride *= sizes[dim];
937983
}
938984
storage->tensor = std::make_unique<aten::TensorImpl>(
939985
to_aten(meta.dtype),
940-
shape.size(),
986+
sizes.size(),
941987
storage->sizes.data(),
942988
nullptr,
943989
storage->order.data(),
@@ -1036,6 +1082,7 @@ class ReferenceExecutable final : public Executable {
10361082
"Empty negative view copy requires matching input/output metadata");
10371083
continue;
10381084
}
1085+
const auto views = linear_views(node, context_.graph);
10391086
auto instruction = std::make_unique<Instruction>();
10401087
instruction->node = &node;
10411088
instruction->kernel = route->kernel;
@@ -1059,7 +1106,9 @@ class ReferenceExecutable final : public Executable {
10591106
const auto& input = node.inputs[index];
10601107
if (input.arg.kind() == ptn::ArgKind::Tensor) {
10611108
const auto value_id = input.arg.as_tensor().id;
1062-
auto value = tensor_value(value_id, false);
1109+
auto value = views && index < views->inputs.size()
1110+
? tensor_value(value_id, false, views->inputs[index])
1111+
: tensor_value(value_id, false);
10631112
if (!value.ok()) {
10641113
return value.error();
10651114
}
@@ -1114,7 +1163,8 @@ class ReferenceExecutable final : public Executable {
11141163
InvalidProgram,
11151164
"Reference provider requires tensor outputs: %s",
11161165
node.target.c_str());
1117-
auto value = tensor_value(output.value_id, true);
1166+
auto value = views ? tensor_value(output.value_id, true, views->output)
1167+
: tensor_value(output.value_id, true);
11181168
if (!value.ok()) {
11191169
return value.error();
11201170
}

‎backends/cpu/test/test_aot.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ def test_non_fp32_linear_decomposes(self):
197197
self.assertEqual(program.executorch_program.execution_plan[0].delegates, [])
198198
self.assertGreater(len(program.buffer), 0)
199199

200+
def test_1d_linear_is_preserved_and_delegated(self):
201+
class Linear1D(torch.nn.Module):
202+
def forward(self, x, w):
203+
return torch.ops.aten.linear.default(x, w, None)
204+
205+
edge = to_edge_transform_and_lower(
206+
torch.export.export(Linear1D(), (torch.randn(3), torch.randn(3))),
207+
partitioner=[CPUPartitioner()],
208+
)
209+
program = edge.to_executorch()
210+
delegate_targets = [
211+
node.target
212+
for data in program.executorch_program.backend_delegate_data
213+
for node in deserialize_graph(bytes(data.data)).nodes
214+
]
215+
self.assertEqual(delegate_targets.count("torch.ops.aten.linear.default"), 1)
216+
200217
def test_constants_have_recorded_readable_tail(self):
201218
model = torch.nn.Linear(2, 3).eval()
202219
result = CpuBackend.preprocess(

‎backends/cpu/test/test_runtime.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ def forward(self, x):
132132
actual = runtime.forward((x,))[0]
133133
torch.testing.assert_close(actual, model(x))
134134

135+
def test_1d_linear_reference_fallback(self):
136+
class Linear(torch.nn.Module):
137+
def forward(self, *args):
138+
return torch.nn.functional.linear(*args)
139+
140+
model = Linear()
141+
for inputs in (
142+
(torch.randn(4), torch.randn(3, 4), torch.randn(3)),
143+
(torch.randn(4), torch.randn(4)),
144+
(torch.randn(2, 4), torch.randn(4)),
145+
(torch.randn(4), torch.randn(4), torch.randn(())),
146+
(torch.randn(2, 3, 4), torch.randn(4), torch.randn(())),
147+
):
148+
with self.subTest(shapes=[x.shape for x in inputs]):
149+
with self._lower(model, inputs) as runtime:
150+
torch.testing.assert_close(
151+
runtime.forward(inputs)[0], model(*inputs)
152+
)
153+
135154
def test_maxpool2d_reference_fallback(self):
136155
model = torch.nn.MaxPool2d(2)
137156
for x in (

0 commit comments

Comments
 (0)