Skip to content
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

Fix tensorboad video slow numpy->torch conversion #1910

Merged
merged 6 commits into from
Apr 26, 2024

Conversation

NickLucche
Copy link
Contributor

@NickLucche NickLucche commented Apr 24, 2024

Hey,

this PR is a small docs fix to the speed issue highlighted here #196 (comment), which also shows up as a warning now in more recent torch versions
:UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at ../torch/csrc/utils/tensor_new.cpp:275.).

You can also test the speed difference yourself with:

from time import time
import numpy as np
import torch

rgb = np.ones((3,256,320))
rgbs = [rgb.copy() for _ in range(100)]

s = time()
a = torch.ByteTensor(rgbs)
print(time()-s)

s = time()
a = torch.from_numpy(np.asarray(rgbs))
print(time()-s)

I get two orders of magnitude of difference in times here.

Description

I create a numpy array first and then let torch use the same storage. np.stack could also be suggested if we prefer being more explicit.

Motivation and Context

Fixes #196 (comment).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I've read the CONTRIBUTION guide (required)
  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have opened an associated PR on the SB3-Contrib repository (if necessary)
  • I have opened an associated PR on the RL-Zoo3 repository (if necessary)
  • I have reformatted the code using make format (required)
  • I have checked the codestyle using make check-codestyle and make lint (required)
  • I have ensured make pytest and make type both pass. (required)
  • I have checked that the documentation builds using make doc (required)

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line

@araffin araffin changed the title Fixed tensorboad video slow numpy->torch conversion Fix tensorboad video slow numpy->torch conversion Apr 25, 2024
@NickLucche NickLucche requested a review from araffin April 25, 2024 14:11
Copy link
Member

@araffin araffin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks =)

@araffin araffin merged commit 35eccaf into DLR-RM:master Apr 26, 2024
4 checks passed
friedeggs pushed a commit to friedeggs/stable-baselines3 that referenced this pull request Jul 22, 2024
* fixed tb video docs

* updated changelog

* add comment on expected render() output

* Update changelog.rst

---------

Co-authored-by: Antonin RAFFIN <antonin.raffin@ensta.org>
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.

2 participants