Fix flaky tests by making ImmutableLabelInfo ID assignment deterministic #424
+21
−9
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.
Description
Modified
ImmutableLabelInfoto sort labels lexicographically before assigning IDs. This ensures that the mapping from Label to ID is deterministic and doesn't depend on the iteration order ofHashMap. Updated the regression testsTestSGDLinear,TestFMClassification,TestClassificationEnsemblesthat relied on the previous non-deterministic ID assignment. This made sure the comparisons in the tests are not order-dependent.Motivation
NonDex detected test flakiness in 4 tests:
org.tribuo.classification.mnb.TestMNB.testSingleClassTrainingorg.tribuo.classification.SerializationTest.load431Protobufsorg.tribuo.classification.sgd.fm.TestFMClassification.loadProtobufModelorg.tribuo.classification.sgd.linear.TestSGDLinear.testSingleClassTrainingThis PR is verified by NonDex to fix them all. The root cause for all of them was that the order of label IDs depended on
HashMapiteration order, which has no guarantee of determinism. When the ID assignment changed, the resulting model parameters and predictions varied. By sorting the labels, the ID assignment will be consistent and make model training deterministic.