Skip to content

fix(metrics): import compute_reward_score so image_reward stops raising NameError - #1112

Merged
DefTruth merged 1 commit into
vipshop:mainfrom
Anai-Guo:fix/metrics-image-reward-import
Sep 7, 2026
Merged

fix(metrics): import compute_reward_score so image_reward stops raising NameError#1112
DefTruth merged 1 commit into
vipshop:mainfrom
Anai-Guo:fix/metrics-image-reward-import

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Problem

cache-dit-metrics-cli image_reward ... can never succeed — it dies with a NameError before it reaches any model code.

src/cache_dit/metrics/metrics.py binds the per-image helper, but the only call site unpacks the directory-level function, which is never imported:

# metrics.py:24-25
compute_clip_score       = _safe_import(".clip_score",   "compute_clip_score")
compute_reward_score_img = _safe_import(".image_reward", "compute_reward_score_img")

# metrics.py:659-663
if metric == "clip_score":
    clip_score, n = compute_clip_score(img_test, prompt_true)      # ok
    _logging_msg(clip_score, "clip_score", n)
if metric == "image_reward":
    image_reward, n = compute_reward_score(img_test, prompt_true)  # NameError
    _logging_msg(image_reward, "image_reward", n)

compute_reward_score is 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_img returns only line 25).

setup.cfg's flake8 ignore list contains F821, so the undefined name never showed up in CI.

Fix

Bind the same shape the working clip_score sibling uses — one line, no behaviour change anywhere else:

-compute_reward_score_img = _safe_import(".image_reward", "compute_reward_score_img")
+compute_reward_score = _safe_import(".image_reward", "compute_reward_score")

This is not just a naming slip: the two functions have different contracts, and only the dir-level one fits the call site.

name signature returns fits 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) float TypeError on unpack

For reference, clip_score.py exposes the exact same pair (compute_clip_score / compute_clip_score_img) and metrics.py already 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 / ImageReward imports are stubbed on this box; all cache_dit code is the unmodified repo source).

Before

compute_clip_score       -> <function compute_clip_score>
compute_reward_score_img -> <function import_error_metric_func>
compute_reward_score     -> <MISSING>
RESULT: NameError: name 'compute_reward_score' is not defined
        raised at metrics.py:662 | image_reward, n = compute_reward_score(img_test, prompt_true)

After

compute_clip_score       -> <function compute_clip_score>
compute_reward_score_img -> <MISSING>
compute_reward_score     -> <function import_error_metric_func>
RESULT: ImportError: This metric function requires additional dependencies that are
        not installed. Please check the documentation for installation instructions.

i.e. the call now resolves and reaches _safe_import's intended, actionable error for a
missing optional dep instead of a bare NameError. With ImageReward importable, the
name binds to the real dir-level function whose signature matches compute_clip_score's
one-for-one:

metrics.compute_clip_score   -> cache_dit.metrics.clip_score.compute_clip_score
    (img_dir, prompts, clip_model_path=None) -> Union[Tuple[float, int], Tuple[None, None]]
metrics.compute_reward_score -> cache_dit.metrics.image_reward.compute_reward_score
    (img_dir, prompts, imagereward_model_path=None) -> Union[Tuple[float, int], Tuple[None, None]]

Lint (pinned to the versions in .pre-commit-config.yaml):

  • yapf==0.40.2 --diff src/cache_dit/metrics/metrics.py → no diff
  • flake8==7.1.1 --config=setup.cfg → unchanged from main (only pre-existing D1xx docstring notices)
  • python -m pyflakes src/ → no undefined name remaining

🤖 Generated with Claude Code

…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>

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.

🟢 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 NameError and 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.

@DefTruth DefTruth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for this fix!

@DefTruth
DefTruth merged commit 32e2a96 into vipshop:main Sep 7, 2026
4 checks passed
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.

3 participants