Fix flatten_dict crash/wrong key for bare numpy array values#1247
Open
Kymi808 wants to merge 1 commit into
Open
Fix flatten_dict crash/wrong key for bare numpy array values#1247Kymi808 wants to merge 1 commit into
Kymi808 wants to merge 1 commit into
Conversation
In flatten_dict's recursive helper, the `np.ndarray` branch built its key as `prefix + k + sep + str(i)`, but `i` is the loop variable from the preceding list/tuple branch. For a standalone ndarray value `i` is unbound, raising UnboundLocalError; if a list/tuple key was processed earlier in the same dict, the stale `i` leaks in and produces a bogus indexed key (e.g. "arr/2"). A bare ndarray maps to a single value, so key it as `prefix + k`, matching the scalar branch. Adds regression tests for the unbound and stale-index cases.
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.
Summary
In
flatten_dict(src/lighteval/utils/utils.py), the recursive helper'snp.ndarraybranch builds its key usingi:But
iis the loop variable from the precedinglist/tuplebranch (for i, vv in enumerate(v)). For a standalone ndarray value it is never bound, so the call raisesUnboundLocalError. If alist/tuplekey was processed earlier in the same dict, the staleileaks in and produces a bogus indexed key.flatten_dictfeedsobj_to_markdown, whichevaluation_trackeruses to log config/task details to TensorBoard, so an ndarray-valued config entry crashes logging.Fix
A bare ndarray maps to a single value, so key it as
prefix + k, identical to the scalar branch just below it:The
list/tuple-of-ndarrays path (which legitimately usesi) is unchanged.Test plan
TestFlattenDictcovering the unbound case, the stale-index case, and a regression guard for list-of-ndarrays. The first two fail onmain(UnboundLocalError / bogusarr/2key) and pass with this change.pytest tests/unit/utils/test_utils.py→ 9 passed.ruff check/ruff format --checkclean on both files.