Repository navigation
Conversation
EntropyModel.decompress converted the CDF tables with tolist() again for every string and built each output row with torch.tensor() on a long Python int list. Convert the tables once before the loop, read the decoded symbols with np.fromiter() + torch.from_numpy(), and build the indexes and dequantize over one flat spatial dim for N-D inputs. Output values, dtype and shape are unchanged. On a workload like TestEntropyBottleneck::test_compression_ND, decompress time goes from 68.6 ms to 47.4 ms. Co-Authored-By: Aiden <aiden@weco.ai>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
EntropyBottleneck.decompressfaster, mainly the loop arounddecode_with_indexesinEntropyModel.decompress.test_compression_ND, median of 3 runs: 68.6 ms -> 47.4 ms, 31% faster.master).What & Why
Before,
EntropyModel.decompresscalledcdf.tolist(),cdf_length.tolist()andoffset.tolist()again for each string. These tables are the same for all strings. Then it built each output row withtorch.tensor(values), andtorch.tensoris slow on a long Python list of ints.Now the code converts the three tables once, before the loop. It reads the decoded list with
np.fromiter(..., dtype=np.int32)and usestorch.from_numpyto wrap it. For outputs with 2 or more spatial dims, it builds the indexes over one flat spatial dim. It also runsdequantizeon a flat(N, C, -1)view and reshapes the result back. The values, dtype and shape stay the same. Only the number of Python and torch calls goes down.Changes
EntropyModel.decompress: convertcdf,cdf_lengthandoffsetto lists once, outside the loop.EntropyModel.decompress: usenp.fromiter+torch.from_numpyinstead oftorch.tensoron the decoded list.EntropyModel.decompress: for inputs with 2 or more spatial dims, dequantize on a flat(N, C, -1)view.EntropyBottleneck.decompress: build the indexes over one flat spatial dim, then reshape to the full size.Results
EntropyBottleneck.decompresstime per call, median of 7 runs, one CPU thread:Why this is safe and easy to maintain
The change is small (about 40 lines in one file) and keeps the same loop structure. It does not change public signatures or the C++ coders. The decoded values, dtype and shape stay the same, and the existing round-trip tests in
tests/test_entropy_models.pyandtests/test_models.pycover this path.Validation
python -m pytest tests/test_models.py tests/test_models_helpers.py tests/test_entropy_models.py tests/test_multi_context_checkerboard.pygives 113 passed, 3 skipped on bothmasterand this branch. The skips are tests that needpytorch_wavelets.TestEntropyBottleneck::test_compression_ND(EntropyBottleneck(128), inputs with 0 to 5 spatial dims) takes 68.6 ms before and 47.4 ms after (3 runs: 0.690, 0.697, 0.690 of the old time).masteron the same inputs.ruff format --checkandruff checkpass on the changed file.AI assistance
This change was found with AI help: Codex (gpt-6.1-sol) suggested candidates and Weco Observe tracked them. Automated checks picked one, then measured it and ran the tests listed above. Before you merge, please review the diff as you would any other contribution.
Weco share link: https://dashboard.weco.ai/share/Yphl6lyOc4sqZEzFk2UP_Pw_QAb4A_xH