Skip to content

perf(entropy_models): decompress 30.7% faster on test_compression_ND - #363

Open
dexhunter wants to merge 2 commits into
InterDigitalInc:masterfrom
dexhunter:perf/entropy-decompress-numpy
Open

dexhunter wants to merge 2 commits into
InterDigitalInc:masterfrom
dexhunter:perf/entropy-decompress-numpy

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 3, 2026 •

Copy link
Copy Markdown

TL;DR

  • This PR makes EntropyModel.decompress faster. EntropyBottleneck.decompress and GaussianConditional use this function.
  • It converts the CDF tables to lists once, before the loop. It reads the decoded symbols with np.fromiter and torch.from_numpy.
  • Measured on a workload like test_compression_ND, median of 3 runs: 30.7% faster (middle run: 68.273 ms -> 46.978 ms).
  • The model, helper, entropy model and checkerboard tests pass on the current code: 113 passed, 3 skipped.

What & Why

Before, EntropyModel.decompress called cdf.tolist(), cdf_length.tolist() and offset.tolist() again for each string. These tables are the same for all strings. Then it built each output row with torch.tensor(values), and torch.tensor is 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 uses torch.from_numpy to wrap it. The assignment into outputs[i] gives the same values, dtype and shape as before. Only the number of Python and torch calls goes down.

Changes

  • EntropyModel.decompress: convert cdf, cdf_length and offset to lists once, outside the loop.
  • EntropyModel.decompress: use np.fromiter + torch.from_numpy instead of torch.tensor on the decoded list.

Results

EntropyBottleneck.decompress time per call, median of 7 runs, one CPU thread:

Situation Before After Change
batch 1, 128 ch, 0D (no spatial dims) 0.188 ms 0.184 ms no change
batch 1, 128 ch, 16x16 2.79 ms 1.91 ms 31% faster
batch 1, 192 ch, 64x64 63.00 ms 41.88 ms 34% faster
batch 4, 128 ch, 32x32 43.02 ms 28.45 ms 34% faster
batch 2, 128 ch, 4x4x4x4x4 21.20 ms 14.04 ms 34% faster

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.py and tests/test_models.py cover this path.

Validation

  • Tests: python -m pytest tests/test_models.py tests/test_models_helpers.py tests/test_entropy_models.py tests/test_multi_context_checkerboard.py gives 113 passed, 3 skipped. The skips are tests that need pytorch_wavelets.
  • Benchmark: a workload like 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.
  • Exact output: the benchmark compares the decoded tensors with master on the same inputs, with zero tolerance.
  • ruff format --check and ruff check pass on the changed file.
  • Short reproduction:
import time, torch
from compressai.entropy_models import EntropyBottleneck

eb = EntropyBottleneck(192)
eb.update(force=True)
x = torch.randn(1, 192, 64, 64) * 3
s = eb.compress(x)
t = time.perf_counter()
for _ in range(20):
    eb.decompress(s, x.shape[2:])
print((time.perf_counter() - t) / 20 * 1e3, "ms")

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

dexhunter and others added 2 commits November 3, 2026 06:17
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>
@dexhunter dexhunter changed the title perf(entropy_models): decompress 31% faster on test_compression_ND perf(entropy_models): decompress 31.1% faster on test_compression_ND Nov 27, 2026
@dexhunter dexhunter changed the title perf(entropy_models): decompress 31.1% faster on test_compression_ND perf(entropy_models): decompress 30.7% faster on test_compression_ND Nov 28, 2026

This branch has not been deployed

No deployments
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.

1 participant