Skip to content

[PyTorch FE] Add support for aten::diagonal - #39529

Open
pj3iL wants to merge 8 commits into
openvinotoolkit:masterfrom
pj3iL:diagonal-support
Open

pj3iL wants to merge 8 commits into
openvinotoolkit:masterfrom
pj3iL:diagonal-support

Conversation

@pj3iL

@pj3iL pj3iL commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Adds aten::diagonal converter (TorchScript, torch.export, torch.compile); it decomposes to transpose/reshape + one Gather, handles negative/reversed dims, offsets, empty shapes, unknown rank.

The empty-result shapes needed for testing exposed four plugin bugs, each committed as failing test + fix.

Details:

  • src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/gather.cpp: Adds GatherZeroDimsLayerTestCPU, 4 suites covering zero-sized-dim Gather. Currently SIGFPE (signal: floating-point exception) on static empty inputs, unfilled output on empty gathered axis.
  • src/plugins/intel_cpu/src/nodes/gather.cpp: Fixes zero-sized-dim Gather. Skips JIT setup for static empty inputs (SIGFPE), lets empty-gathered-axis run with zero-filled output per spec, instead of unfilled garbage.
  • src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/reshape.cpp: Adds smoke_ReshapeZeroDims and smoke_ReshapeZeroDimsSpecialZero CPU/GPU instantiations for static zero-sized output.
  • src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/squeeze_unsqueeze.cpp: Adds smoke_ZeroDims and smoke_ZeroDims_emptyAxes instantiations for Squeeze/Unsqueeze to a zero-sized output.
  • src/plugins/intel_gpu/src/plugin/ops/reshape.cpp: Adds empty_static_output check so static zero-sized output goes through the legacy path.
  • src/bindings/python/tests/test_torchdynamo_backend.py: Regression test (5 cases) that the "openvino" backend doesn't break Dynamo guards with suppress_errors=False.
  • *src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/backend.py: Wraps the make_fx re-trace in torch._guards.tracing(None) so it records no sourceless guards.
  • tests/layer_tests/pytorch_tests/test_diagonal.py: Layer tests for torch.diagonal across shapes, offsets, negative dims, dtypes, dynamic shapes, traced graphs, empty input.
  • src/frontends/pytorch/src/op/diagonal.cpp: New converter: move dim1/dim2 last, flatten, gather diagonal positions at n2+1 stride.
  • src/frontends/pytorch/src/op_table.cpp: Registers aten::diagonal to the new converter (TorchScript table; FX table inherits).
  • src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py: Marks torch.ops.aten.diagonal.default as supported for torch.compile.

Tickets:

  • ticket-id

AI Assistance:

  • AI assistance used: yes
  • Used to understand the flow, finding out the issues, writing tests, and finally adding fixes. Manual checks are done for correctness and validation.

Adds GatherZeroDimsLayerTestCPU with four instantiations exercising Gather where the data or the indices tensor has a zero dim, across negative and positive axis, const and parameter indices, and static and dynamic input shapes.

The expected values follow the Gather spec: empty indices produce an empty output, and a gather from an empty axis is an out-of-range index, so the output is zero-filled.
@github-actions github-actions Bot added the category: CPU OpenVINO CPU plugin label Nov 5, 2026
pj3iL added 3 commits November 6, 2026 03:59
Skip kernel setup for static Gather nodes with empty data/output; the
setup divided by those sizes and crashed with SIGFPE. An empty gathered
axis with non-empty output now runs and zero-fills the output (all
indices out of range, zeros per Gather-8 spec) instead of returning an
unfilled buffer.
ReshapeLayerTest and SqueezeUnsqueezeLayerTest cases where the target
shape has a literal 0 dim and the output is statically empty.
The static reshape path builds the primitive from cldnn::tensor, where a
0 dim means "copy from input" and cannot describe an empty output. Send
statically empty outputs to the new shape-infer path instead.
@github-actions github-actions Bot added the category: GPU OpenVINO GPU plugin label Nov 6, 2026
pj3iL added 2 commits November 6, 2026 08:06
With suppress_errors=False, several plain-graph cases (strided slice,
diagonal with offset or negative dims) raise IndexError while Dynamo
builds guards after the openvino backend returns. The harness default
suppress_errors=True hides this behind a silent eager fallback.
fx_openvino re-traces the Dynamo graph with make_fx(tracing_mode="fake"),
which reuses Dynamo's live ShapeEnv and appends sourceless guards Dynamo
cannot build later. Wrap the re-trace in torch._guards.tracing(None) so
make_fx runs without recording guards.
@github-actions github-actions Bot added category: Python API OpenVINO Python bindings category: PyTorch FE OpenVINO PyTorch Frontend labels Nov 6, 2026
pj3iL added 2 commits November 7, 2026 05:14
Diagonal over fixed and negative dim pairs, non-zero offsets, dynamic
shapes and traced graphs, on float32/int64/bool inputs, including an
empty-dimension input.
Move dim1 and dim2 to the end, flatten them, and gather the diagonal's
flat positions. Handles offset, negative dims, and zero-sized dims.
Registered for TorchScript and FX tables; marked supported in the
torch.compile op-support list.
@pj3iL
pj3iL marked this pull request as ready for review November 7, 2026 07:02
@pj3iL
pj3iL requested review from a team as code owners November 7, 2026 07:02
@pj3iL
pj3iL requested review from PiotrKrzem and rkazants November 7, 2026 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: CPU OpenVINO CPU plugin category: GPU OpenVINO GPU plugin category: Python API OpenVINO Python bindings category: PyTorch FE OpenVINO PyTorch Frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant