Skip to content

frontend/pytorch: add aten::cross_entropy_loss translator and tests (fixes #29691, relates #28584)#32554

Closed
Rahuldrabit wants to merge 3 commits intoopenvinotoolkit:masterfrom
Rahuldrabit:feature/pytorch-cross-entropy
Closed

frontend/pytorch: add aten::cross_entropy_loss translator and tests (fixes #29691, relates #28584)#32554
Rahuldrabit wants to merge 3 commits intoopenvinotoolkit:masterfrom
Rahuldrabit:feature/pytorch-cross-entropy

Conversation

@Rahuldrabit
Copy link
Copy Markdown

1. Core Implementation

File: src/frontends/pytorch/src/op/cross_entropy_loss.cpp

Implements the translate_cross_entropy_loss function that converts PyTorch's cross_entropy_loss operation to OpenVINO operations. The implementation follows PyTorch's specification:

cross_entropy_loss = log_softmax + nll_loss

Key features:

  • Applies LogSoftmax along dimension 1 (class dimension)
  • Uses one-hot masking plus ReduceSum to select target class probabilities (NLL loss)
  • Supports optional class weights via Gather + Multiply
  • Supports three reduction modes:
    • none (0): No reduction, returns per-sample loss
    • mean (1): Mean of all losses (default)
    • sum (2): Sum of all losses
  • Handles both 2D inputs (N, C) and higher-dimensional inputs (N, C, d1, d2, ...)

2. Operation Registration

File: src/frontends/pytorch/src/op_table.cpp

  • Added forward declaration: OP_CONVERTER(translate_cross_entropy_loss);
  • Registered operation: {"aten::cross_entropy_loss", op::translate_cross_entropy_loss},

3. Tests

File: tests/layer_tests/pytorch_tests/test_cross_entropy_loss.py

Comprehensive test suite covering:

  • Basic functionality with different reduction modes (mean, sum, none)
  • Class weights
  • Different input shapes (2D and spatial dimensions)
  • Edge cases (binary classification, single sample, many classes)

@Rahuldrabit Rahuldrabit requested a review from a team as a code owner October 26, 2025 14:45
@github-actions github-actions bot added the category: PyTorch FE OpenVINO PyTorch Frontend label Oct 26, 2025
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Oct 26, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This PR will be closed in a week because of 2 weeks of no activity.

@github-actions github-actions bot added the Stale label Jan 25, 2026
Copilot AI review requested due to automatic review settings January 26, 2026 18:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements the PyTorch cross_entropy_loss operation translator for OpenVINO, addressing issue #29691. The implementation follows PyTorch's specification where cross_entropy_loss = log_softmax + nll_loss, providing a complete translation from PyTorch operations to OpenVINO's operation set.

Changes:

  • Implements cross_entropy_loss translator with support for three reduction modes (none, mean, sum), optional class weights, and spatial dimensions
  • Registers the operation in the op_table with proper alphabetical ordering
  • Adds comprehensive tests covering basic functionality, reduction modes, class weights, and spatial inputs

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/frontends/pytorch/src/op/cross_entropy_loss.cpp Core translator implementation with log_softmax + one-hot masking approach for NLL loss, supporting weights and all reduction modes
src/frontends/pytorch/src/op_table.cpp Adds forward declaration and registration of the new translator in alphabetical order
tests/layer_tests/pytorch_tests/test_cross_entropy_loss.py Test suite covering reduction modes (mean/sum/none), class weights, and spatial dimensions (2D and 4D inputs)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,153 @@
// Copyright (C) 2018-2025 Intel Corporation
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year should be 2018-2026 to be consistent with all other files in this directory.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

@@ -0,0 +1,58 @@
# Copyright (C) 2018-2025 Intel Corporation
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year should be 2018-2026 to be consistent with all other test files in this directory.

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot removed the Stale label Jan 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR will be closed in a week because of 2 weeks of no activity.

@github-actions github-actions bot added the Stale label Feb 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 2 week with no activity.

@github-actions github-actions bot closed this Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: PyTorch FE OpenVINO PyTorch Frontend ExternalPR External contributor Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants