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>
Keep the two changes that give the speed-up in EntropyModel.decompress: convert the CDF tables once before the loop, and read the decoded symbols with np.fromiter() + torch.from_numpy(). Remove the flat dequantize path and the flat index build in EntropyBottleneck.decompress. They added four branches each and gave no measurable gain. Convert the tables without the `len(strings) > 0` check: the conversion has no side effect when there are no strings. Output values, dtype and shape are unchanged. 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
EntropyModel.decompressfaster.EntropyBottleneck.decompressandGaussianConditionaluse this function.np.fromiterandtorch.from_numpy.test_compression_ND, median of 3 runs: 30.7% faster (middle run: 68.273 ms -> 46.978 ms).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. The assignment intooutputs[i]gives the same values, dtype and shape as before. 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.Results
EntropyBottleneck.decompresstime per call, median of 7 runs, one CPU thread:Why this is safe and easy to maintain
The change is small (9 lines added and 8 removed in one function) and keeps the same loop structure. It does not change public signatures or the C++ coders. 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. The skips are tests that needpytorch_wavelets.TestEntropyBottleneck::test_compression_ND(EntropyBottleneck(128), inputs with 0 to 5 spatial dims). The 3 runs took 0.675, 0.693 and 0.693 of the old time.masteron the same inputs, with zero tolerance.ruff format --checkandruff checkpass on the changed file.AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/Yphl6lyOc4sqZEzFk2UP_Pw_QAb4A_xH