Skip to content

Fix CSE errors out on string tensor attributes#28422

Closed
rajatmonga wants to merge 5 commits into
mainfrom
rajatmonga/cse-string-crash-issue-28413
Closed

Fix CSE errors out on string tensor attributes#28422
rajatmonga wants to merge 5 commits into
mainfrom
rajatmonga/cse-string-crash-issue-28413

Conversation

@rajatmonga

Copy link
Copy Markdown
Contributor

Description

The CSE optimizer crashed with ORT_ENFORCE when encountering a node with a string tensor attribute (e.g., LabelEncoder with keys_tensor of type STRING). This replaces the enforce with an early return of hash 0, treating such nodes as distinct — consistent with how AreScalarTensorAttributeEqual already handles string tensors.

Motivation and Context

Fixes #28413 — regression in 1.25.1 where models using LabelEncoder (or any op with string tensor attributes) crash during graph optimization.

Changes

  • Replace ORT_ENFORCE(data_type != STRING) with an early return in GetTensorAttributeHash
  • Updated comment to document string tensor handling

@rajatmonga

Copy link
Copy Markdown
Contributor Author

@rajatmonga please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree company="Microsoft"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in the Common Subexpression Elimination (CSE) graph optimizer when encountering scalar STRING tensor attributes (e.g., LabelEncoder’s keys_tensor) by treating those attributes as unsupported for hashing instead of enforcing.

Changes:

  • Replaced an ORT_ENFORCE on STRING tensor attributes in GetTensorAttributeHash with an early return (hash 0) to avoid crashing during optimization.
  • Updated the local comment to document how STRING tensor attributes are handled.
  • Fixed a lifetime bug in a test utility by avoiding a std::string_view bound to a temporary string returned by GetProviderOptionPrefix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/test/util/default_providers.cc Prevents dangling std::string_view by storing the provider option prefix as a std::string before comparing/stripping it.
onnxruntime/core/optimizer/common_subexpression_elimination.cc Avoids CSE crash on scalar STRING tensor attributes by returning a neutral hash instead of enforcing.

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

Comment thread onnxruntime/core/optimizer/common_subexpression_elimination.cc Outdated
@rajatmonga rajatmonga force-pushed the rajatmonga/cse-string-crash-issue-28413 branch from e6ec7d4 to 8a81fb2 Compare May 8, 2026 23:49
@rajatmonga rajatmonga force-pushed the rajatmonga/cse-string-crash-issue-28413 branch from b7f1cc2 to 9535d7f Compare May 11, 2026 19:56
@rajatmonga rajatmonga self-assigned this May 11, 2026
@rajatmonga rajatmonga requested a review from yuslepukhin May 12, 2026 00:41
@yuslepukhin yuslepukhin requested review from Copilot and removed request for yuslepukhin May 12, 2026 02:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread onnxruntime/test/optimizer/cse_test.cc Outdated
Comment thread onnxruntime/test/testdata/transform/cse/generate.py Outdated
@yuslepukhin yuslepukhin changed the title Fix CSE crash on string tensor attributes Fix CSE errors out on string tensor attributes May 12, 2026
@yuslepukhin

Copy link
Copy Markdown
Contributor

Please, do not force-push, it makes it difficult to see what changes occurred in-between the iterations.

Rajat Monga and others added 3 commits May 12, 2026 14:47
The CSE optimizer crashed with ORT_ENFORCE when encountering a node
with a string tensor attribute (e.g., LabelEncoder with keys_tensor
of type STRING). Replace the enforce with an early return of hash 0,
treating such nodes as distinct (consistent with AreScalarTensorAttributeEqual).

Fixes #28413

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use LabelEncoder (ai.onnx.ml) instead of Constant nodes so the STRING
tensor attribute survives Model::Load and actually exercises
GetTensorAttributeHash. Remove the .onnx test data file and generate.py
additions that are no longer needed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread onnxruntime/core/providers/cuda/plugin/cuda_plugin_ep.cc
Comment thread onnxruntime/core/optimizer/common_subexpression_elimination.cc Outdated
@rajatmonga rajatmonga force-pushed the rajatmonga/cse-string-crash-issue-28413 branch from df1e17c to 96696b8 Compare May 12, 2026 22:42
@rajatmonga rajatmonga marked this pull request as draft May 12, 2026 22:43
@rajatmonga rajatmonga marked this pull request as draft May 12, 2026 22:43

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/test/optimizer/cse_test.cc Outdated
Comment thread onnxruntime/test/optimizer/cse_test.cc Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comments suppressed due to low confidence (1)

onnxruntime/test/optimizer/cse_test.cc:307

  • This test hard-codes the ONNX-ML domain string in multiple places (opset import and AddNode). Using the existing kMLDomain constant would avoid typos and keep the domain value consistent across tests.
  Model model("CseStringTensorAttrTest", false, ModelMetaData(), PathString(),
              IOnnxRuntimeOpSchemaRegistryList(),
              {{kOnnxDomain, 21}, {"ai.onnx.ml", 4}}, {}, logger);
  auto& graph = model.MainGraph();

Comment thread onnxruntime/core/optimizer/common_subexpression_elimination.cc Outdated
Comment on lines +298 to +307
TEST(CseTests, StringTensorAttr) {
// Regression test for https://github.com/microsoft/onnxruntime/issues/28413.
// CSE must not crash when it encounters a node with a STRING tensor attribute.
// We use LabelEncoder (ai.onnx.ml) because it retains its STRING tensor attribute
// through Model::Load (unlike Constant nodes which are converted to initializers).
const auto& logger = DefaultLoggingManager().DefaultLogger();
Model model("CseStringTensorAttrTest", false, ModelMetaData(), PathString(),
IOnnxRuntimeOpSchemaRegistryList(),
{{kOnnxDomain, 21}, {"ai.onnx.ml", 4}}, {}, logger);
auto& graph = model.MainGraph();

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines +298 to +306
TEST(CseTests, StringTensorAttr) {
// Regression test for https://github.com/microsoft/onnxruntime/issues/28413.
// CSE must not crash when it encounters a node with a STRING tensor attribute.
// We use LabelEncoder (ai.onnx.ml) because it retains its STRING tensor attribute
// through Model::Load (unlike Constant nodes which are converted to initializers).
const auto& logger = DefaultLoggingManager().DefaultLogger();
Model model("CseStringTensorAttrTest", false, ModelMetaData(), PathString(),
IOnnxRuntimeOpSchemaRegistryList(),
{{kOnnxDomain, 21}, {"ai.onnx.ml", 4}}, {}, logger);
@rajatmonga

Copy link
Copy Markdown
Contributor Author

Reverting PR as there are other issues. @Dmitri is submitting a better one.

@rajatmonga rajatmonga closed this May 13, 2026
@rajatmonga rajatmonga deleted the rajatmonga/cse-string-crash-issue-28413 branch May 13, 2026 19:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comments suppressed due to low confidence (1)

onnxruntime/test/optimizer/cse_test.cc:350

  • This comment is now misleading: the optimizer hashes STRING tensor attributes and AreScalarTensorAttributeEqual compares their string_data(), so string tensor attributes are not generally treated as distinct. In this specific test the LabelEncoder nodes remain distinct because ai.onnx.ml is not treated as a supported deterministic domain by CSE, not because of the string attribute handling.
  // CSE won't merge these nodes (STRING tensor attrs are treated as distinct),
  // but it must not crash when hashing them.

Comment on lines +298 to +306
TEST(CseTests, StringTensorAttr) {
// Regression test for https://github.com/microsoft/onnxruntime/issues/28413.
// CSE must not crash when it encounters a node with a STRING tensor attribute.
// We use LabelEncoder (ai.onnx.ml) because it retains its STRING tensor attribute
// through Model::Load (unlike Constant nodes which are converted to initializers).
const auto& logger = DefaultLoggingManager().DefaultLogger();
Model model("CseStringTensorAttrTest", false, ModelMetaData(), PathString(),
IOnnxRuntimeOpSchemaRegistryList(),
{{kOnnxDomain, 21}, {"ai.onnx.ml", 4}}, {}, logger);
Comment on lines +180 to +181
if (utils::HasString(lhs_t)) {
return AreRangesEqual(lhs_t.string_data(), rhs_t.string_data());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Common subexpression elimination crashes for nodes with string tensor attributes

4 participants