Skip to content

Commit 7aca03b

Browse files
authored
Route _neg_view_copy to ET neg kernel (pytorch#24194) (pytorch#24194)
Summary: `aten::_neg_view_copy` (negation with view-copy autograd metadata, identical numerics to neg for inference) had no provider route, crashing at backend init (0x10). Add it to the ET spec table mapped to `aten::neg.out`. Reviewed By: digantdesai Differential Revision: D126929421
1 parent 5272830 commit 7aca03b

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ constexpr auto kKernels = std::to_array<KernelSpec>({
106106
nullptr,
107107
false,
108108
"56"},
109+
{"torch.ops.aten._neg_view_copy.default", "aten::neg.out", 1, 1, nullptr},
109110
{"torch.ops.aten._pdist_forward.default",
110111
"aten::_pdist_forward.out",
111112
2,
@@ -1021,6 +1022,20 @@ class ReferenceExecutable final : public Executable {
10211022
reason.data());
10221023
return Error::NotSupported;
10231024
}
1025+
if (node.target == "torch.ops.aten._neg_view_copy.default" &&
1026+
tensor_bytes(context_.graph.value(node.inputs[0].arg.as_tensor().id))
1027+
.get() == 0) {
1028+
const auto& input =
1029+
context_.graph.value(node.inputs[0].arg.as_tensor().id)
1030+
.tensor_meta();
1031+
const auto& output =
1032+
context_.graph.value(node.outputs[0].value_id).tensor_meta();
1033+
ET_CHECK_OR_RETURN_ERROR(
1034+
input.dtype == output.dtype && input.sizes == output.sizes,
1035+
InvalidProgram,
1036+
"Empty negative view copy requires matching input/output metadata");
1037+
continue;
1038+
}
10241039
auto instruction = std::make_unique<Instruction>();
10251040
instruction->node = &node;
10261041
instruction->kernel = route->kernel;

‎backends/cpu/test/test_runtime.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ def forward(self, x, mask):
9595
for output, expected in zip(actual, model(x, current), strict=True):
9696
torch.testing.assert_close(output, expected, rtol=0, atol=0)
9797

98+
def test_neg_view_copy_routes_to_neg(self):
99+
class NegView(torch.nn.Module):
100+
def forward(self, x):
101+
return torch.ops.aten._neg_view_copy.default(x)
102+
103+
model = NegView()
104+
x = torch.randn(2, 3)
105+
with self._lower(model, (x,)) as runtime:
106+
actual = runtime.forward((x,))[0]
107+
torch.testing.assert_close(actual, model(x))
108+
109+
def test_neg_view_copy_empty_bool(self):
110+
class NegView(torch.nn.Module):
111+
def forward(self, x):
112+
return torch.ops.aten._neg_view_copy.default(x)
113+
114+
model = NegView()
115+
x = torch.empty(0, dtype=torch.bool)
116+
expected = model(x)
117+
with self._lower(model, (x,)) as runtime:
118+
actual = runtime.forward((x,))[0]
119+
torch.testing.assert_close(actual, expected)
120+
98121
def test_optional_integer_index_lists(self):
99122
class Index(torch.nn.Module):
100123
def __init__(self, accumulate):

0 commit comments

Comments
 (0)