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 _data_to_texture method to LabelColormap and remove caching of (u)int8 and (uint16) #6602

Merged
merged 7 commits into from Jan 22, 2024

Conversation

Czaki
Copy link
Collaborator

@Czaki Czaki commented Jan 19, 2024

Description

In this PR, I extract the most obvious parts of #6583 to allow cherrypick them to 0.4.19.

The changes are:

  1. As we use memory view for (u)int8 and (u)int16 data, this PR removes caching in _raw_to_dispayed as it is obsolete.
  2. add a _data_to_texture method to LabelColormap to provide uniform interface for mapping between the data dtype and the (possibly smaller, unsigned) texture dtype and avoid branching.
  3. fix Colormap.map for int to fit documentation.

@Czaki Czaki added the maintenance PR with maintance changes, label Jan 19, 2024
@Czaki Czaki added this to the 0.4.19 milestone Jan 19, 2024
@Czaki Czaki requested a review from jni January 19, 2024 09:07
@github-actions github-actions bot added tests Something related to our tests qt Relates to qt labels Jan 19, 2024
@Czaki Czaki added the run-benchmarks Add this label to trigger a full benchmark run in a PR label Jan 19, 2024
Copy link

codecov bot commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f290506) 92.27% compared to head (b2ab139) 92.23%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6602      +/-   ##
==========================================
- Coverage   92.27%   92.23%   -0.05%     
==========================================
  Files         603      603              
  Lines       53902    53933      +31     
==========================================
+ Hits        49737    49743       +6     
- Misses       4165     4190      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@jni jni left a comment

Choose a reason for hiding this comment

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

@Czaki I've made minor suggestions but this is basically ready to go imho. I'll merge tonight sync with you so we can move forward with the release.

napari/layers/labels/labels.py Outdated Show resolved Hide resolved
napari/layers/labels/labels.py Outdated Show resolved Hide resolved
napari/utils/colormaps/colormap.py Outdated Show resolved Hide resolved
Comment on lines 287 to 298
@overload
def _map_to_gpu(self, values: np.ndarray) -> np.ndarray:
...

@overload
def _map_to_gpu(self, values: np.integer) -> np.integer:
...

def _map_to_gpu(
self, values: Union[np.ndarray, np.integer]
) -> Union[np.ndarray, np.integer]:
"""Map input values to values for send to GPU."""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@overload
def _map_to_gpu(self, values: np.ndarray) -> np.ndarray:
...
@overload
def _map_to_gpu(self, values: np.integer) -> np.integer:
...
def _map_to_gpu(
self, values: Union[np.ndarray, np.integer]
) -> Union[np.ndarray, np.integer]:
"""Map input values to values for send to GPU."""
@overload
def _data_to_texture_dtype(self, values: np.ndarray) -> np.ndarray:
...
@overload
def _data_to_texture_dtype(self, values: np.integer) -> np.integer:
...
def _data_to_texture_dtype(
self, values: Union[np.ndarray, np.integer]
) -> Union[np.ndarray, np.integer]:
"""Map input values to a (possibly reduced) dtype to set a GPU texture."""

... Typing question: since the overload is defined in the base class, is it also needed here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mypy was complaining about lack of overload...

napari/utils/colormaps/colormap.py Outdated Show resolved Hide resolved
Comment on lines 377 to 379
@overload
def _map_to_gpu(self, values: np.ndarray) -> np.ndarray:
...
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@overload
def _map_to_gpu(self, values: np.ndarray) -> np.ndarray:
...
@overload
def _data_to_texture_dtype(self, values: np.ndarray) -> np.ndarray:
...

napari/utils/colormaps/colormap.py Outdated Show resolved Hide resolved
@@ -887,7 +887,7 @@ def test_background_color(qtbot, qt_viewer: QtViewer, dtype):
data = np.zeros((10, 10), dtype=dtype)
data[5:] = 10
layer = qt_viewer.viewer.add_labels(data, opacity=1)
color = layer.colormap.map(10)[0] * 255
color = layer.colormap.map(10) * 255
Copy link
Member

Choose a reason for hiding this comment

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

As you know, I like this change, but when I tried to do it you told me that it might be a breaking change for some users... I don't mind going in this direction either way — it is an easy fix for downstream users and matches the documentation.

@Czaki
Copy link
Collaborator Author

Czaki commented Jan 22, 2024

@jni Ready to merge?

@jni jni changed the title Add _map_to_gpu to Labels colormap and remove caching of (u)int8 and (uint16) Add _data_to_texture method to LabelColormap and remove caching of (u)int8 and (uint16) Jan 22, 2024
@jni jni merged commit 0f85200 into napari:main Jan 22, 2024
34 checks passed
@jni jni deleted the do_not_cache_low_bytes branch January 22, 2024 12:22
@jni jni added the performance Relates to performance label Jan 22, 2024
@jni
Copy link
Member

jni commented Jan 22, 2024

Done! 🚀

Czaki added a commit that referenced this pull request Jan 22, 2024
# References and relevant issues

closes #6579
supersedes #6583

# Description

#5732 introduced a cache of mapped data so that only changed indices
were mapped to texture dtypes/values and sent on to the GPU. In this PR,
an alternate strategy is introduced: rather than caching
previously-transformed data and then doing a diff with the cache, we
paint the data *and* the texture-mapped data directly.

The partial update of the on-GPU texture also introduced in #5732 is
maintained, as it can dramatically reduce the amount of data needing to
be transferred from CPU to GPU memory.

This PR is built on top of #6602.

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Czaki added a commit that referenced this pull request Jan 23, 2024
…)int8 and (uint16) (#6602)

# Description

In this PR, I extract the most obvious parts of #6583 to allow
cherrypick them to 0.4.19.

The changes are:

1) As we use memory view for (u)int8 and (u)int16 data, this PR removes
caching in `_raw_to_dispayed` as it is obsolete.
2) add a `_data_to_texture` method to `LabelColormap` to provide uniform
interface for mapping between the data dtype and the (possibly smaller,
unsigned) texture dtype and avoid branching.
3) fix `Colormap.map` for `int` to fit documentation.

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Czaki added a commit that referenced this pull request Jan 23, 2024
closes #6579
supersedes #6583

were mapped to texture dtypes/values and sent on to the GPU. In this PR,
an alternate strategy is introduced: rather than caching
previously-transformed data and then doing a diff with the cache, we
paint the data *and* the texture-mapped data directly.

The partial update of the on-GPU texture also introduced in #5732 is
maintained, as it can dramatically reduce the amount of data needing to
be transferred from CPU to GPU memory.

This PR is built on top of #6602.

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PR with maintance changes, performance Relates to performance qt Relates to qt run-benchmarks Add this label to trigger a full benchmark run in a PR tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants