Skip to content

fix: preserve 16-bit depth in LDM3D rgblike_to_depthmap#14211

Closed
sergioperezcheco wants to merge 1 commit into
huggingface:mainfrom
sergioperezcheco:fix/ldm3d-16bit-depth
Closed

fix: preserve 16-bit depth in LDM3D rgblike_to_depthmap#14211
sergioperezcheco wants to merge 1 commit into
huggingface:mainfrom
sergioperezcheco:fix/ldm3d-16bit-depth

Conversation

@sergioperezcheco

Copy link
Copy Markdown

Problem

VaeImageProcessorLDM3D.rgblike_to_depthmap combines two 8-bit channels into a 16-bit depth value using widened arithmetic (int32), but then casts the result back to the original uint8 input dtype. This truncates any depth value > 255 to the low byte.

Downstream, postprocess(output_type="pil") calls numpy_to_depth, which constructs a mode="I;16" PIL image expecting 2 bytes per pixel. The truncated uint8 buffer is half the expected size, causing:

ValueError: buffer is not large enough

Fix

Both the torch.Tensor and np.ndarray branches now return uint16 instead of casting back to the input dtype. This preserves the full 16-bit depth range [0, 65535] and is consistent with what numpy_to_depth expects.

Removed the now-unused original_dtype variable in both branches.

Test

Added test_rgblike_to_depthmap_preserves_uint16_range verifying:

  • NumPy: uint8 input with high byte=1, low byte=0 → uint16 output with value 256
  • Torch: same check

Fixes #14206

The method combined two 8-bit channels into a 16-bit value using
int32 arithmetic but then cast the result back to the original uint8
dtype, truncating values > 255. This caused numpy_to_depth to fail
with 'buffer is not large enough' when building I;16 PIL images.

Fix: return uint16 from both the torch and numpy branches instead
of casting back to the input dtype.
@github-actions github-actions Bot added tests size/S PR with diff < 50 LOC fixes-issue labels Jul 17, 2026
@sergioperezcheco

Copy link
Copy Markdown
Author

Closing this as a duplicate of #14200 by @chuenchen309, which was opened ~30h earlier and addresses the same bug from #14206. Both PRs converge on uint16 for the numpy path; the only difference is mine also casts the torch path to uint16 whereas #14200 keeps int32 there. Happy to leave that dtype question to the maintainers rather than fragment review across two PRs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes-issue size/S PR with diff < 50 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LDM3D rgblike_to_depthmap truncates the 16-bit depth map back to the 8-bit input dtype

1 participant