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

Add handles for overlaying user text and images to the Passive Viewer #2503

Merged
merged 6 commits into from
Apr 8, 2025

Conversation

Andrew-Luo1
Copy link
Contributor

@Andrew-Luo1 Andrew-Luo1 commented Mar 17, 2025

See issue #2494

Added the text and image overlay handles in the style of commit 37d759.

This supports:
Brax PR 584
Playground PR 83

Copy link
Contributor

@aftersomemath aftersomemath left a comment

Choose a reason for hiding this comment

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

@yuvaltassa asked me to take a look.

This is a great feature to have! My comments are mostly about minor things.

However, there is a race condition introduced with the previous commit for set_figures 37d7591 that is being duplicated in this PR. In my testing this is particular easy to trigger with set_images and it results in a crash/segfault.

That commit and this PR introduce the vectors user_text_, user_images_, and user_figures_ to the Simulate class. These vectors are updated on the Python side and the data in them is used to render the viewers UI. To avoid undefined behavior, there needs to be some synchronization to ensure these vectors are not updated while the viewer is rendering.

In addition to the crashing I mentioned above, I believe this race condition is why there are several comments in the Python side of the code about doing things a certain way to avoid flickering.

To resolve the situation, set_figures, overlay_text and set_images, should be refactored to work like update_hfield, update_mesh, and update_mesh. The latter methods sync the data with the viewer's render thread using a condition variable as can be seen here and here.

Note: It is in theory not enough to simply acquire the Simulate lock with viewer.lock() prior to calling set_figures, overlay_text, or set_images (though in practice I did find it helped with the segfaulting when using set_images). This is because Simulate acquires its lock only to stage the data for rendering. Subsequently, it releases the lock and renders. This was done on purpose helps ensure the viewer runs smoothly, and we should probably keep it that way, thus my suggestion to stage the data like update_hfield, update_mesh, and update_mesh.

@@ -125,6 +128,49 @@ def clear_figures(self):
if sim is not None:
sim.clear_figures()

def overlay_text(self, overlay_texts: List[Tuple[int, int, str, str]]):
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be best to name this method set_text to be consistent with set_figures and set_images.

if sim is not None:
sim.overlay_text(overlay_texts)

def clear_overlay_text(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, this would be clear_text.

if sim is not None:
sim.clear_overlay_text()

def set_images(
Copy link
Contributor

Choose a reason for hiding this comment

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

This method should get a docstring as overlay_text does.

"""Overlay text on the viewer.

Args:
overlay_texts: List of tuples of (font, gridpos, text1, text2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be easy to accept a single tuple or a list of tuples? For example, we do the equivalent of this in rollout.

sim.clear_overlay_text()

def set_images(
self, viewports_images: List[Tuple[mujoco.MjrRect, np.ndarray]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, can this accept a single instance in addition to a list?

unsigned char* image_copy = new unsigned char[size];
std::memcpy(image_copy, buf.ptr, size);

simulate_->user_images_.push_back(std::make_tuple(viewport, image_copy));
Copy link
Contributor

Choose a reason for hiding this comment

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

user_images_ is being modified while it is potentially used by Simulate::Render. See top level review comment about how we can resolve this issue in general.

}

// Set them all at once to prevent overlay text flickering.
simulate_->user_text_ = user_overlay_text;
Copy link
Contributor

Choose a reason for hiding this comment

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

user_text_ is being modified while it is potentially used by Simulate::Render. See top level review comment about how we can resolve this issue in general. The fix should also fix the "flickering" mentioned in the comment above.

size_t size = height * width * 3;

// Make a copy of the image data to prevent flickering
unsigned char* image_copy = new unsigned char[size];
Copy link
Contributor

Choose a reason for hiding this comment

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

See top level review comment about how we should handle locking. It should prevent the flickering and the need to make this copy.

throw std::invalid_argument("image must have 3 channels");
}
if (buf.ndim != 3) {
throw std::invalid_argument("image must have 3 dimensions (H, W, C)");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check the type of the image data here? It is assumed to be a uint8 below.

@@ -2597,6 +2605,16 @@ void Simulate::Render() {
ShowFigure(this, viewport, &figure);
Copy link
Contributor

Choose a reason for hiding this comment

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

These calls to ShowFigure, ShowOverlayText and ShowImage are being made from Simulate's Render function. Render is called from the RenderLoop after unlocking Simulate's lock (this helps with parallelism and makes the viewer run smoother).

However, ShowFigure, ShowOverlayText and ShowImage use vectors, user_figures_, user_text_, and user_images_ which can be modified concurrently by the user code. The result is that undefined behavior can occur.

See top level review comment about how we can resolve this issue in general.

sim = self._sim()
if sim is not None:
# Nearest neighbor resize
resize = lambda a, s: a[(np.arange(s[0]) * a.shape[0]) // s[0]][
Copy link
Contributor

Choose a reason for hiding this comment

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

Does resizing by slicing have good performance? It might be a good idea to check. If not, I would just force the user to pass in images of the correct size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: will check if resize > 1m fps, else check sizes match

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out it's only about 7k FPS. Will remove

@josechenf
Copy link

We'd be super keen to get this in and use this!
@aftersomemath Let us know if this works for you!

Copy link
Contributor

@aftersomemath aftersomemath left a comment

Choose a reason for hiding this comment

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

Looks good to me. My comments are just nits.

Thanks for adding a docstring, typing, etc to set_figures.

I think merging this with the race condition is fine because set_figures is already on main with the race condition. I will submit a PR for fixing the race condition in a few days.

After reviewing this a second time, I came to the conclusion that signatures of the set_X functions should be more pythonic (maybe each argument should be its own list like with rollout). But we don't need to fix that in this PR.

sim.set_figures(viewports_figures)

def clear_figures(self):
sim = self._sim()
if sim is not None:
sim.clear_figures()

def set_text(self, overlay_texts: Union[Tuple[Optional[int], Optional[int], Optional[str], Optional[str]],
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: if the other functions are plural (set_images, set_figures) then this should be set_texts.

for viewport, image in viewports_images:
targ_shape = (viewport.height, viewport.width)
# Check if image is already the correct shape
if image.shape[:2] != targ_shape:
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are no longer resizing the image, passing a viewport with the image is no longer required, the size is implicit and this check is not needed. However, I'm ok with leaving the check here to avoid breaking the API in the future if resizing is brought back somehow.

"""Overlay text on the viewer.

Args:
overlay_texts: Single tuple or list of tuples of (font, gridpos, text1, text2)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since set_figures argument is viewports_figures, this argument should be fonts_gridpos_texts, but that is cumbersome, how about texts? We don't need to include the overlay prefix.

@Andrew-Luo1
Copy link
Contributor Author

Updated!

@copybara-service copybara-service bot merged commit 699ad13 into google-deepmind:main Apr 8, 2025
11 of 14 checks passed
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.

4 participants