Skip to content

Conversation

@swahtz
Copy link
Contributor

@swahtz swahtz commented Feb 12, 2026

Summary

  • Make pinned_memory conditional on the tensor device being CUDA in two locations where single-element JaggedTensor construction unconditionally allocated pinned (page-locked) memory via cudaHostAlloc, which forced CUDA runtime initialization even for CPU-only tensors.
  • This caused crashes in forked DataLoader worker processes (where re-initializing CUDA after fork() is forbidden) and added unnecessary overhead for CPU-only workloads.
  • Add a test verifying that CPU single-element JaggedTensor offsets are not pinned.

Fixes #467

Changes

src/fvdb/JaggedTensor.cpp

.pinned_memory(true).pinned_memory(mData.device().is_cuda()) in the JaggedTensor(const std::vector<torch::Tensor>&) single-element branch.

src/fvdb/detail/ops/JOffsetsFromJIdx.cu

.pinned_memory(true).pinned_memory(jdata.device().is_cuda()) in joffsetsFromJIdx(), which is the shared implementation called by CPU, CUDA, and PrivateUse1 dispatch paths.

tests/unit/test_jagged_tensor.py

New test_cpu_single_element_no_cuda_init verifying both constructor paths produce non-pinned offsets for CPU tensors.

Test plan

  • python -m pytest tests/unit/test_jagged_tensor.py::TestJaggedTensor::test_cpu_single_element_no_cuda_init -v passes
  • python -m pytest tests/unit/test_jagged_tensor.py::TestJaggedTensor::test_batch_size_one_cpu_float32 -v passes (existing single-element test)
  • python -m pytest tests/unit/test_jagged_tensor.py -v full test suite passes
  • Reproduction script from issue JaggedTensor single-element constructor unconditionally initializes CUDA via pinned_memory #467 (CPU JaggedTensor in forked DataLoader) no longer crashes

Made with Cursor

…e type

- Modified the JaggedTensor constructor to allocate pinned memory only when the device is CUDA.
- Updated the joffsetsFromJIdx function to reflect the same change for empty tensor cases.
- Added unit tests to ensure that single-element CPU JaggedTensors do not use pinned memory, addressing issue openvdb#467.

This change prevents unnecessary CUDA runtime initialization in CPU contexts, improving stability in forked DataLoader worker processes.

Signed-off-by: Jonathan Swartz <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where constructing a single-element JaggedTensor on CPU would still allocate pinned (page-locked) host memory, which can trigger CUDA runtime initialization and crash in forked DataLoader workers.

Changes:

  • Make pinned-memory allocation conditional on the target tensor device being CUDA in two single-element construction paths.
  • Add a unit test asserting CPU single-element JaggedTensor offsets are not pinned (covering both constructor entry points).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/fvdb/JaggedTensor.cpp Avoids unconditional pinned-memory allocation for the single-element list-of-tensors constructor on CPU.
src/fvdb/detail/ops/JOffsetsFromJIdx.cu Avoids unconditional pinned-memory allocation in the shared offsets implementation used by CPU/CUDA/PrivateUse1 dispatch.
tests/unit/test_jagged_tensor.py Adds regression coverage ensuring CPU single-element offsets are not pinned for both constructor paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@swahtz swahtz enabled auto-merge (squash) February 12, 2026 04:58
@swahtz swahtz self-assigned this Feb 12, 2026
@swahtz swahtz added bug Something isn't working core library Core fVDB library. i.e. anything in the _Cpp module (C++) or fvdb python module labels Feb 12, 2026
@swahtz swahtz added this to fVDB Feb 12, 2026
@swahtz swahtz added this to the v0.2 milestone Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core library Core fVDB library. i.e. anything in the _Cpp module (C++) or fvdb python module

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

JaggedTensor single-element constructor unconditionally initializes CUDA via pinned_memory

1 participant