Fix: Enforce deterministic SDFG graph traversal and code generation#2320
Draft
kotsaloscv wants to merge 14 commits intospcl:mainfrom
Draft
Fix: Enforce deterministic SDFG graph traversal and code generation#2320kotsaloscv wants to merge 14 commits intospcl:mainfrom
kotsaloscv wants to merge 14 commits intospcl:mainfrom
Conversation
…le node/edge key generation for deterministic sorting
tbennun
requested changes
Mar 12, 2026
Collaborator
tbennun
left a comment
There was a problem hiding this comment.
This could have some performance implications. Could we make this configurable?
tbennun
reviewed
Mar 13, 2026
Collaborator
philip-paul-mueller
left a comment
There was a problem hiding this comment.
I have some comments ans suggestions.
Collaborator
philip-paul-mueller
left a comment
There was a problem hiding this comment.
There are still some things that needs to be checked, especially the global object.
Collaborator
philip-paul-mueller
left a comment
There was a problem hiding this comment.
There are a lot of small things.
|
|
||
| sdfg_alphabetical_sorting: | ||
| type: bool | ||
| default: false |
Collaborator
There was a problem hiding this comment.
I would change the CI configuration such that when auto optimize is activated (probably also when just simplification is enabled) that sorting is enabled.
You can do that by modifying .github/workflows/general-ci.yml.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When generating code via DaCe (e.g., as a backend for GT4Py/ICON4Py), clearing the Python cache between runs can result in structurally shifting generated code. While mathematically identical, these shifting instruction orders disrupt C++ compiler heuristics (such as register allocation and loop unrolling), leading to unpredictable performance regressions.
This non-determinism stems from graph traversal operations falling back on memory addresses or volatile generated UUIDs to break ties during dictionary and set iterations, which cascades into DaCe's internal adjacency lists and NetworkX topological sorts.
Solution
This PR introduces a deterministic sorting pass (
sort_sdfg_alphabetically) that locks the internal memory layout of the SDFG before code generation by sorting elements based on their intrinsic semantic properties rather than their location in memory.Specifically, this PR:
get_deterministic_node_keyandget_deterministic_edge_key. To safely break ties between identical nodes, these functions evaluate graph topology (in/out degrees), interface semantics (connectors), loop parameters, and a stable MD5 hash of internal Tasklet code.VOLATILE_STR_REGEX) to strip memory addresses, full UUIDs, and partial 8-character suffix hashes from node labels, connectors, and memlet data payloads._arrays,symbols), master node/edge dictionaries, and nestedin_edges,out_edges) in-place._nxmultigraphs based on the newly stabilized registries.Testing
Added
tests/sdfg/deterministic_sort_test.py.This test manually scrambles the internal dictionaries of an
SDFGandSDFGState, applies the sorting pass, and asserts that the resulting adjacency lists and NetworkX nodes are forced back into a strict, predictable topological order based on the new semantic keys.