Skip to content

Fix device allocation for top-down models#174

Open
C-Achard wants to merge 6 commits into
mainfrom
cy/tensor-device-alloc-fix
Open

Fix device allocation for top-down models#174
C-Achard wants to merge 6 commits into
mainfrom
cy/tensor-device-alloc-fix

Conversation

@C-Achard

@C-Achard C-Achard commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Fixes parts of the pose estimation runner to improve tensor handling and ensure consistency in the use of PyTorch tensor operations by:

  • Replacing raw tensor constructors with device- and dtype-aware methods
  • Correcting the structure of scaling and offset parameters fallbacks
  • Slightly improving the handling of batched pose post-processing

Tensor handling improvements:

  • Replaced raw tensor constructors like torch.zeros and torch.ones with pose.new_zeros and pose.new_ones to ensure tensors are created on the correct device and with the correct dtype. Also, used pose.new_tensor for creating tensors from lists, improving consistency.
  • In _postprocess_top_down, replaced torch.zeros with batch_pose.new_zeros for empty batch handling, and ensured all scale and offset operations use tensors created with the same device and dtype as the pose input.

Bug fixes and API consistency:

  • Fixed the structure of offsets_and_scales to always be a pair of tuples, ensuring the expected format
  • Changed the post-processing aggregation from torch.cat to torch.stack to preserve batch dimension and avoid shape errors

C-Achard added 3 commits July 17, 2026 12:02
Update PyTorch top-down pose postprocessing to keep tensors on the same device/dtype by using `new_zeros` and `new_tensor` instead of creating CPU tensors. Also replace `torch.cat(poses)` with `torch.stack(..., dim=0)` so batched pose outputs preserve the expected batch dimension.
Refactor bbox creation in `SkipFrames.__call__` to build tensors from `pose` (`new_zeros/new_tensor/new_ones`) so boxes and scores stay on the same device and dtype. The NaN handling for min/max coordinates is now explicit via `nan=` arguments, and bbox limiting uses `torch.clamp` with typed min/max tensors.
Update the PyTorch top-down path to use a correctly structured fallback `offsets_and_scales` value when no detections are found. This keeps the data shape consistent with post-processing expectations and avoids downstream tuple unpacking/type mismatches.
@C-Achard
C-Achard requested a review from Copilot July 17, 2026 10:11
@C-Achard C-Achard self-assigned this Jul 17, 2026
@C-Achard C-Achard added bug Something isn't working pytorch Related to the pytorch backend labels Jul 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 targets the PyTorch pose-estimation runner’s top-down pipeline, aiming to make tensor creation device/dtype-consistent and to fix shape/parameter handling issues during top-down post-processing.

Changes:

  • Switched bbox tensor construction to pose.new_*-style APIs for device/dtype consistency.
  • Adjusted top-down offset/scale fallback structure and improved post-processing aggregation (stack vs cat).
  • Improved empty-batch handling in _postprocess_top_down via batch_pose.new_zeros(...).

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

Comment thread dlclive/pose_estimation_pytorch/runner.py Outdated
Comment on lines +98 to +101
self._detections = {
"boxes": bboxes,
"scores": pose.new_ones(num_det),
}
C-Achard added 3 commits July 17, 2026 13:50
Improve the PyTorch runner’s frame handling by validating HWC input shape and allowed channel counts, correctly unpacking dimensions, and dropping alpha for 4-channel frames. Also fix the top-down fallback `offsets_and_scales` format to use paired float scales instead of a scalar, preventing downstream shape/format issues.
In `PyTorchRunner`, transformed pose tensors are now merged with `torch.cat(..., dim=0)` instead of `torch.stack`. This aligns aggregation with tensors that already carry a leading batch dimension and avoids introducing an extra axis.
Introduce a reusable `DebugTimingStats` utility in `dlclive.utils` and instrument `PyTorchRunner.get_pose` with stage-level timing (input conversion, detector, pose transform/inference/decode, dynamic crop/update, postprocess, and CPU transfer). The runner now synchronizes CUDA around timed inference sections for accurate measurements and periodically logs aggregated performance stats. This also fixes top-down fallback metadata shape (`offsets_and_scales`) and restores the missing return in `_postprocess_top_down`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pytorch Related to the pytorch backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants