fix(metrics): import compute_reward_score so image_reward stops raising NameError - #1112
Merged
Merged
Conversation
…sing NameError
`metrics.py` binds the per-image helper `compute_reward_score_img` but the only
call site unpacks the directory-level `compute_reward_score`, which is never
imported:
compute_clip_score = _safe_import(".clip_score", "compute_clip_score")
compute_reward_score_img = _safe_import(".image_reward", "compute_reward_score_img")
...
clip_score, n = compute_clip_score(img_test, prompt_true) # ok
image_reward, n = compute_reward_score(img_test, prompt_true) # NameError
So `cache-dit-metrics-cli image_reward ...` always dies with
`NameError: name 'compute_reward_score' is not defined`, and the bound
`compute_reward_score_img` is dead — it is referenced nowhere in the tree.
Bind the same shape the working `clip_score` sibling uses. `compute_reward_score`
returns `Tuple[float, int]` and unpacks at the call site; `compute_reward_score_img`
returns a bare `float` and would have raised `TypeError` on unpack anyway.
flake8's `ignore` in setup.cfg contains F821, which is why the undefined name never
surfaced in CI.
Signed-off-by: Anai-Guo <antai12232931@outlook.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The targeted import now matches the call site’s expected function name and return contract.
Pull request overview
Fixes the image_reward CLI path by importing the directory-level function used by the existing call site.
Changes:
- Replaces the unused per-image helper binding with
compute_reward_score. - Prevents the
NameErrorand preserves optional-dependency handling.
File summaries
| File | Description |
|---|---|
src/cache_dit/metrics/metrics.py |
Corrects the ImageReward function binding. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Problem
cache-dit-metrics-cli image_reward ...can never succeed — it dies with aNameErrorbefore it reaches any model code.src/cache_dit/metrics/metrics.pybinds the per-image helper, but the only call site unpacks the directory-level function, which is never imported:compute_reward_scoreis not defined in this module, and the name that is bound —compute_reward_score_img— is referenced nowhere in the tree (grep -rn compute_reward_score_imgreturns only line 25).setup.cfg's flake8ignorelist containsF821, so the undefined name never showed up in CI.Fix
Bind the same shape the working
clip_scoresibling uses — one line, no behaviour change anywhere else:This is not just a naming slip: the two functions have different contracts, and only the dir-level one fits the call site.
a, n = f(...)?compute_reward_score(dir)(img_dir, prompts, imagereward_model_path=None)Tuple[float, int]compute_reward_score_img(per-image)(img, prompt, imagereward_model_path=None)floatTypeErroron unpackFor reference,
clip_score.pyexposes the exact same pair (compute_clip_score/compute_clip_score_img) andmetrics.pyalready imports the dir-level one there.Verification
Ran the real CLI entrypoint against a throwaway image dir + prompt file (only the unrelated third-party
diffusers/ImageRewardimports are stubbed on this box; allcache_ditcode is the unmodified repo source).Before
After
i.e. the call now resolves and reaches
_safe_import's intended, actionable error for amissing optional dep instead of a bare
NameError. WithImageRewardimportable, thename binds to the real dir-level function whose signature matches
compute_clip_score'sone-for-one:
Lint (pinned to the versions in
.pre-commit-config.yaml):yapf==0.40.2 --diff src/cache_dit/metrics/metrics.py→ no diffflake8==7.1.1 --config=setup.cfg→ unchanged frommain(only pre-existingD1xxdocstring notices)python -m pyflakes src/→ noundefined nameremaining🤖 Generated with Claude Code