Skip to content

fix: prevent CPU OOM on large batches by reducing memory fragmentation#50

Open
pxl-pshr wants to merge 1 commit into
PozzettiAndrea:mainfrom
pxl-pshr:fix/memory-fragmentation-large-batches
Open

fix: prevent CPU OOM on large batches by reducing memory fragmentation#50
pxl-pshr wants to merge 1 commit into
PozzettiAndrea:mainfrom
pxl-pshr:fix/memory-fragmentation-large-batches

Conversation

@pxl-pshr

Copy link
Copy Markdown

Summary

When processing large frame batches (1200+ frames at 1456×768), the node crashes with a CPU OOM error (DefaultCPUAllocator: not enough memory: you tried to allocate 16102195200 bytes) even on systems with plenty of RAM (tested with 191GB). The root cause is memory fragmentation from thousands of small tensor allocations accumulated over long-running inference loops (~59 minutes), not insufficient total memory.

Changes

  • Pre-allocate contiguous output tensors (depth_out, conf_out, sky_out, ray_origin_out, ray_dir_out) instead of appending to Python lists — eliminates thousands of small allocations and the subsequent torch.cat() copies
  • Free images_pt and normalized_images early to reduce peak memory (~32GB freed during inference)
  • Lazily allocate ray tensors after first frame to handle variable model output resolutions
  • Use in-place operations (clamp_(), zero_()) to avoid extra tensor copies
  • Delete intermediate tensors (del depth_out, etc.) immediately after processing into final form
  • Reconstruct RGB output from original images input instead of keeping images_pt alive for the entire loop
  • Helper functions (process_tensor_to_image, process_tensor_to_mask) now accept both pre-allocated tensors and lists (backward compatible)

Test plan

  • Tested with 50 frames — no regressions, outputs match expected formats
  • Tested with 1200 frames at 1456×768 on 191GB RAM system — previously crashed, now completes successfully
  • Verify all 11 outputs (depth, confidence, resized RGB, ray origin/direction, camera params, sky mask, gaussian PLY)
  • Test with keep_model_size=True option
  • Test with camera parameters input

When processing large frame batches (1200+ frames at 1456x768), the node
crashes with a CPU OOM error even on systems with 191GB RAM. The root cause
is memory fragmentation from thousands of small tensor allocations over
long-running inference loops, not insufficient total memory.

Changes:
- Pre-allocate contiguous output tensors instead of appending to lists
- Free images_pt and normalized_images early to reduce peak memory
- Lazily allocate ray tensors to handle variable model output resolutions
- Use in-place operations (clamp_, zero_) to avoid extra tensor copies
- Delete intermediate tensors immediately after processing
- Reconstruct RGB output from original input instead of keeping a copy alive
- Helper functions now accept both tensors and lists (backward compatible)

Tested with 1200 frames at 1456x768 on a system with 191GB RAM.
Previously crashed; now completes successfully.
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