-
Notifications
You must be signed in to change notification settings - Fork 6
predict_action function performs pyTorch conversion using GPU #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/improve-predict
Are you sure you want to change the base?
predict_action function performs pyTorch conversion using GPU #37
Conversation
There was a problem hiding this 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 optimizes predict_action
by moving the tensor device transfer to before data transformations so that normalization and reshaping occur on the GPU, improving inference performance under CPU load.
- Moves
observation[name].to(device)
to the start of the conversion loop and removes the redundant transfer at the end - Ensures all type casting, normalization, permute, and unsqueeze operations happen on the CUDA device
Comments suppressed due to low confidence (1)
lerobot/common/robot_devices/control_utils.py:112
- Add a unit test to verify that after
predict_action
, all observation tensors are on the specified device and have the correct shape and dtype.
observation[name] = observation[name].to(device)
observation[name] = observation[name].to(device) | ||
if "image" in name: | ||
observation[name] = observation[name].type(torch.float32) / 255 | ||
observation[name] = observation[name].permute(2, 0, 1).contiguous() | ||
observation[name] = observation[name].unsqueeze(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can combine device transfer, dtype conversion, and normalization into a single chained call to reduce intermediate allocations, e.g.: observation[name] = observation[name].to(device=device, dtype=torch.float32).div(255).permute(2,0,1).unsqueeze(0)
.
observation[name] = observation[name].to(device) | |
if "image" in name: | |
observation[name] = observation[name].type(torch.float32) / 255 | |
observation[name] = observation[name].permute(2, 0, 1).contiguous() | |
observation[name] = observation[name].unsqueeze(0) | |
if "image" in name: | |
observation[name] = observation[name].to(device=device, dtype=torch.float32).div(255).permute(2, 0, 1).unsqueeze(0) | |
else: | |
observation[name] = observation[name].to(device).unsqueeze(0) |
Copilot uses AI. Check for mistakes.
@@ -109,11 +109,11 @@ def predict_action(observation, policy, device, use_amp): | |||
): | |||
# Convert to pytorch format: channel first and float32 in [0,1] with batch dimension | |||
for name in observation: | |||
observation[name] = observation[name].to(device) | |||
if "image" in name: | |||
observation[name] = observation[name].type(torch.float32) / 255 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using the .float()
alias instead of .type(torch.float32)
for readability and consistency with common PyTorch code style.
observation[name] = observation[name].type(torch.float32) / 255 | |
observation[name] = observation[name].float() / 255 |
Copilot uses AI. Check for mistakes.
What this does
(⚡️ Performance)
Optimizes predict_action function's observations' pyTorch conversions by using CUDA GPU
How to checkout & try? (for the reviewer)
Run the same ACT policy evaluation in these 3 different scenarios:
Examples: