Skip to content

fix(export_utils): avoid rescaling integer uint8 arrays in encode_video - #15505

Open
2422725977 wants to merge 1 commit into
huggingface:mainfrom
2422725977:fix/encode-video-uint8-ndarray
Open

2422725977 wants to merge 1 commit into
huggingface:mainfrom
2422725977:fix/encode-video-uint8-ndarray

Conversation

@2422725977

Copy link
Copy Markdown

What does this PR do?

Fixes a bug in encode_video (src/diffusers/utils/export_utils.py) where the [0, 1] normalization check (0 <= video <= 1) was applied to every np.ndarray regardless of dtype:

  1. Integer uint8 (or other integer) arrays containing only {0, 1} (e.g., binary masks or very dark frames) matched 0 <= video <= 1 and were silently multiplied by 255.
  2. Normal uint8 [0, 255] arrays triggered a spurious "Supplied numpy.ndarray does not have values in [0, 1]" warning whenever any pixel exceeded 1.
  3. Out-of-range float arrays or non-uint8 integer arrays were passed through to av.VideoFrame.from_ndarray(..., format="rgb24") without casting to uint8.

Summary of changes

  • Gate the [0, 1] normalization check and * 255 rescaling behind np.issubdtype(video.dtype, np.floating).
  • For floating arrays outside [0, 1], keep the warning and convert via .round().clip(0, 255).astype("uint8").
  • For non-uint8 integer arrays, convert directly via .clip(0, 255).astype("uint8") without rescaling; keep uint8 arrays untouched.
  • Add TestEncodeVideo (4 unit tests) in tests/others/test_utils.py covering uint8 {0, 1} preservation, float [0, 1] scaling, out-of-range float warning + clamping/casting, and non-uint8 integer casting.

Verification

  • PYTHONPATH=src pytest -v tests/others/test_utils.py -k encode_video — 4 passed
  • ruff check --no-cache src/diffusers/utils/export_utils.py tests/others/test_utils.py — passed
  • ruff format --check --no-cache src/diffusers/utils/export_utils.py tests/others/test_utils.py — passed

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Self-review notes

  • Blocking issues: None.
  • Non-blocking / Leave for actual review:
    1. The encode_video docstring (src/diffusers/utils/export_utils.py:318-321) currently notes that np.ndarray inputs are expected to be float arrays in [0, 1]; left unchanged to keep the diff focused, happy to expand it to document uint8 [0, 255] arrays as well if maintainers prefer.
    2. TestEncodeVideo mocks _import_av because av (PyAV) is an optional dependency not required by tests/others/test_utils.py, verifying the exact uint8 rgb24 frames handed to av.VideoFrame.from_ndarray.
  • Dead code (advisory): None introduced (src/diffusers/utils/export_utils.py:341-356 is executed directly on the np.ndarray branch of encode_video).
  • Verdict: READY.

This branch has not been deployed

No deployments
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.

1 participant