Skip to content

Conversation

@MohammadSadeghSalehi
Copy link
Contributor

This PR fixes a potential overflow bug in rgblike_to_depthmap when computing
depth values from RGB-like depth images.

Changes:

  • Cast image tensors/arrays to int32 before performing the 16-bit combination (*256 +).
  • Prevents overflow when input dtype is uint8 or small int.
  • Supports both PyTorch and NumPy paths consistently.
  • Added comments for optional uint16 casting.

Rationale:
The original implementation used 2**8 multiplication directly on small integer tensors,
which could overflow for high byte values (≥128). The fix ensures safe arithmetic and
consistent return types.

cc @patrickvonplaten @multimodalart @psuraj

# You may want to cast the final result to uint16, but casting to a
# larger int type (like int32) is sufficient to fix the overflow.
# depth_map = depth_map.to(torch.uint16) # Uncomment if uint16 is strictly required
return depth_map
Copy link
Collaborator

@DN6 DN6 Oct 27, 2025

Choose a reason for hiding this comment

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

Change looks good. Could we just store the original dtype and cast back once the computation is done.

original_dtype = image.dtype
# perform compute
return depth_map.to(original_dtype)

@yiyixuxu
Copy link
Collaborator

yiyixuxu commented Nov 6, 2025

@bot /style

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

Style bot fixed some files and pushed the changes.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@yiyixuxu yiyixuxu merged commit e4393fa into huggingface:main Nov 6, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants