Skip to content

Commit bdedc9e

Browse files
committed
Rename function and add tests for SoftmaxCrossEntropyLoss
Renamed `impl_softmax_cross_entropy` to `onnx_softmax_crossentropy_loss` in `softmax_crossentropy_loss.cpp` and updated all function calls accordingly. Updated `ONNX_OP` macro for `SoftmaxCrossEntropyLoss` in `opset_13` to use `OPSET_SINCE(13)`. Added new test cases in `onnx_import.in.cpp` for various scenarios of the `SoftmaxCrossEntropyLoss` operation: - `onnx_model_softmax_crossentropy_loss_mean_weight_ii` - `onnx_model_softmax_crossentropy_loss_sum` - `onnx_model_softmax_crossentropy_loss_none` - `onnx_model_softmax_crossentropy_loss_higher_dim` Included new ONNX model files in `prototxt` format for the new test cases: - `softmax_crossentropy_loss_higher_dim.prototxt` - `softmax_crossentropy_loss_mean_weight_ii.prototxt` - `softmax_crossentropy_loss_none.prototxt` - `softmax_crossentropy_loss_sum.prototxt` These files define the graph structure, input/output tensor shapes, and attributes for the `SoftmaxCrossEntropyLoss` operation with different reduction methods and configurations.
1 parent 187fed8 commit bdedc9e

6 files changed

+280
-4
lines changed

src/frontends/onnx/frontend/src/op/softmax_crossentropy_loss.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ov {
1919
namespace frontend {
2020
namespace onnx {
2121
namespace {
22-
OutputVector impl_softmax_cross_entropy(const Node& node, int64_t axis_default) {
22+
OutputVector onnx_softmax_crossentropy_loss(const Node& node, int64_t axis_default) {
2323
const auto inputs = node.get_ov_inputs();
2424

2525
const auto scores = inputs[0];
@@ -96,15 +96,16 @@ OutputVector impl_softmax_cross_entropy(const Node& node, int64_t axis_default)
9696
namespace ai_onnx {
9797
namespace opset_12 {
9898
OutputVector softmax_cross_entropy_loss(const Node& node) {
99-
return impl_softmax_cross_entropy(node, 1);
99+
return onnx_softmax_crossentropy_loss(node, 1);
100100
}
101101
ONNX_OP("SoftmaxCrossEntropyLoss", OPSET_IN(12), ai_onnx::opset_12::softmax_cross_entropy_loss);
102102
} // namespace opset_12
103103
namespace opset_13 {
104104
OutputVector softmax_cross_entropy_loss(const Node& node) {
105-
return impl_softmax_cross_entropy(node, 1);
105+
// only difference is that opset_13 supports bfloat16 datatype
106+
return onnx_softmax_crossentropy_loss(node, 1);
106107
}
107-
ONNX_OP("SoftmaxCrossEntropyLoss", OPSET_IN(13), ai_onnx::opset_13::softmax_cross_entropy_loss);
108+
ONNX_OP("SoftmaxCrossEntropyLoss", OPSET_SINCE(13), ai_onnx::opset_13::softmax_cross_entropy_loss);
108109
} // namespace opset_13
109110
} // namespace ai_onnx
110111
} // namespace onnx
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ir_version: 7
2+
producer_name: "OpenVINO ONNX Frontend"
3+
graph {
4+
node {
5+
input: "x"
6+
input: "y"
7+
output: "z"
8+
op_type: "SoftmaxCrossEntropyLoss"
9+
attribute {
10+
name: "reduction"
11+
s: "mean"
12+
type: STRING
13+
}
14+
}
15+
name: "test_sce_higher_dim"
16+
input {
17+
name: "x"
18+
type {
19+
tensor_type {
20+
elem_type: 1
21+
shape {
22+
dim { dim_value: 2 }
23+
dim { dim_value: 3 }
24+
dim { dim_value: 2 }
25+
dim { dim_value: 2 }
26+
}
27+
}
28+
}
29+
}
30+
input {
31+
name: "y"
32+
type {
33+
tensor_type {
34+
elem_type: 7
35+
shape {
36+
dim { dim_value: 2 }
37+
dim { dim_value: 2 }
38+
dim { dim_value: 2 }
39+
}
40+
}
41+
}
42+
}
43+
output {
44+
name: "z"
45+
type {
46+
tensor_type {
47+
elem_type: 1
48+
shape {}
49+
}
50+
}
51+
}
52+
}
53+
opset_import {
54+
version: 13
55+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
ir_version: 7
2+
producer_name: "OpenVINO ONNX Frontend"
3+
graph {
4+
node {
5+
input: "x"
6+
input: "y"
7+
input: "w"
8+
output: "z"
9+
op_type: "SoftmaxCrossEntropyLoss"
10+
attribute {
11+
name: "reduction"
12+
s: "mean"
13+
type: STRING
14+
}
15+
attribute {
16+
name: "ignore_index"
17+
i: -1
18+
type: INT
19+
}
20+
}
21+
name: "test_sce_mean_weight_ii"
22+
input {
23+
name: "x"
24+
type {
25+
tensor_type {
26+
elem_type: 1
27+
shape {
28+
dim { dim_value: 2 }
29+
dim { dim_value: 3 }
30+
dim { dim_value: 2 }
31+
}
32+
}
33+
}
34+
}
35+
input {
36+
name: "y"
37+
type {
38+
tensor_type {
39+
elem_type: 7
40+
shape {
41+
dim { dim_value: 2 }
42+
dim { dim_value: 2 }
43+
}
44+
}
45+
}
46+
}
47+
input {
48+
name: "w"
49+
type {
50+
tensor_type {
51+
elem_type: 1
52+
shape {
53+
dim { dim_value: 3 }
54+
}
55+
}
56+
}
57+
}
58+
output {
59+
name: "z"
60+
type {
61+
tensor_type {
62+
elem_type: 1
63+
shape {}
64+
}
65+
}
66+
}
67+
}
68+
opset_import {
69+
version: 13
70+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
ir_version: 7
2+
producer_name: "OpenVINO ONNX Frontend"
3+
graph {
4+
node {
5+
input: "x"
6+
input: "y"
7+
output: "z"
8+
op_type: "SoftmaxCrossEntropyLoss"
9+
attribute {
10+
name: "reduction"
11+
s: "none"
12+
type: STRING
13+
}
14+
}
15+
name: "test_sce_none"
16+
input {
17+
name: "x"
18+
type {
19+
tensor_type {
20+
elem_type: 1
21+
shape {
22+
dim { dim_value: 2 }
23+
dim { dim_value: 3 }
24+
}
25+
}
26+
}
27+
}
28+
input {
29+
name: "y"
30+
type {
31+
tensor_type {
32+
elem_type: 7
33+
shape {
34+
dim { dim_value: 2 }
35+
}
36+
}
37+
}
38+
}
39+
output {
40+
name: "z"
41+
type {
42+
tensor_type {
43+
elem_type: 1
44+
shape {
45+
dim { dim_value: 2 }
46+
}
47+
}
48+
}
49+
}
50+
}
51+
opset_import {
52+
version: 13
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
ir_version: 7
2+
producer_name: "OpenVINO ONNX Frontend"
3+
graph {
4+
node {
5+
input: "x"
6+
input: "y"
7+
output: "z"
8+
op_type: "SoftmaxCrossEntropyLoss"
9+
attribute {
10+
name: "reduction"
11+
s: "sum"
12+
type: STRING
13+
}
14+
}
15+
name: "test_sce_sum"
16+
input {
17+
name: "x"
18+
type {
19+
tensor_type {
20+
elem_type: 1
21+
shape {
22+
dim { dim_value: 2 }
23+
dim { dim_value: 3 }
24+
}
25+
}
26+
}
27+
}
28+
input {
29+
name: "y"
30+
type {
31+
tensor_type {
32+
elem_type: 7
33+
shape {
34+
dim { dim_value: 2 }
35+
}
36+
}
37+
}
38+
}
39+
output {
40+
name: "z"
41+
type {
42+
tensor_type {
43+
elem_type: 1
44+
shape {}
45+
}
46+
}
47+
}
48+
}
49+
opset_import {
50+
version: 13
51+
}

src/frontends/onnx/tests/onnx_import.in.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6945,3 +6945,49 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_float8e4m3fn_constant) {
69456945

69466946
test_case.run();
69476947
}
6948+
6949+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_crossentropy_loss_mean_weight_ii) {
6950+
auto model = convert_model("softmax_crossentropy_loss_mean_weight_ii.onnx");
6951+
6952+
auto test_case = ov::test::TestCase(model, s_device);
6953+
// Input x: shape (2,3,2)
6954+
test_case.add_input<float>({1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f});
6955+
// Labels with ignore_index -1
6956+
test_case.add_input<int64_t>({0, -1, 2, 1});
6957+
// Weights
6958+
test_case.add_input<float>({0.5f, 1.0f, 2.0f});
6959+
// Expected output
6960+
test_case.add_expected_output<float>(Shape{}, {1.28642857f});
6961+
test_case.run();
6962+
}
6963+
6964+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_crossentropy_loss_sum) {
6965+
auto model = convert_model("softmax_crossentropy_loss_sum.onnx");
6966+
6967+
auto test_case = ov::test::TestCase(model, s_device);
6968+
test_case.add_input<float>({0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f});
6969+
test_case.add_input<int64_t>({1, 2});
6970+
test_case.add_expected_output<float>(Shape{}, {1.8137f});
6971+
test_case.run();
6972+
}
6973+
6974+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_crossentropy_loss_none) {
6975+
auto model = convert_model("softmax_crossentropy_loss_none.onnx");
6976+
6977+
auto test_case = ov::test::TestCase(model, s_device);
6978+
test_case.add_input<float>({1.f, 2.f, 3.f, 4.f, 5.f, 6.f});
6979+
test_case.add_input<int64_t>({2, 1});
6980+
test_case.add_expected_output<float>(Shape{2}, {0.407f, 1.4067f});
6981+
test_case.run();
6982+
}
6983+
6984+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_crossentropy_loss_higher_dim) {
6985+
auto model = convert_model("softmax_crossentropy_loss_higher_dim.onnx");
6986+
6987+
auto test_case = ov::test::TestCase(model, s_device);
6988+
// Flattened input for shape (2,3,2,2)
6989+
test_case.add_input<float>(std::vector<float>(24, 1.0f)); // Example input
6990+
test_case.add_input<int64_t>({1, 0, 2, 1, 0, 2, 1, 1}); // Labels shape (2,2,2)
6991+
test_case.add_expected_output<float>(Shape{}, {1.2f}); // Example expected value
6992+
test_case.run();
6993+
}

0 commit comments

Comments
 (0)