Python implementation of Arithmetic Decoding. Strictly aligned with CUDA decode_with_accessor_kernel. bytestreams shape: [nlayers, nchannels, buffer_size]
(cdf, bytestreams, lengths, output)
| 2339 | |
| 2340 | |
| 2341 | def decode_fast_new(cdf, bytestreams, lengths, output): |
| 2342 | """ |
| 2343 | Python implementation of Arithmetic Decoding. |
| 2344 | Strictly aligned with CUDA decode_with_accessor_kernel. |
| 2345 | bytestreams shape: [nlayers, nchannels, buffer_size] |
| 2346 | """ |
| 2347 | cdf_np = cdf.cpu().numpy().view(np.uint16).astype(np.uint32) |
| 2348 | bs_np = bytestreams.cpu().numpy().astype(np.uint8) |
| 2349 | len_np = lengths.cpu().numpy().astype(np.int32) |
| 2350 | |
| 2351 | n_layers, n_tokens, n_channels = output.shape |
| 2352 | out_np = np.zeros(output.shape, dtype=np.uint8) |
| 2353 | |
| 2354 | def decode_one(args): |
| 2355 | layer_idx, c = args |
| 2356 | curr_len = int(len_np[layer_idx, c]) |
| 2357 | # For decode_fast_new, each channel has its own contiguous buffer, |
| 2358 | # so start_off=0 and end_off=curr_len within channel_bs |
| 2359 | channel_bs = bs_np[layer_idx, c] # shape [buffer_size] |
| 2360 | _decode_single_channel( |
| 2361 | cdf_np[layer_idx, c], |
| 2362 | channel_bs, |
| 2363 | 0, |
| 2364 | curr_len, |
| 2365 | n_tokens, |
| 2366 | out_np[layer_idx, :, c], |
| 2367 | ) |
| 2368 | |
| 2369 | tasks = [(layer_idx, c) for layer_idx in range(n_layers) for c in range(n_channels)] |
| 2370 | |
| 2371 | with ThreadPoolExecutor() as executor: |
| 2372 | list(executor.map(decode_one, tasks)) |
| 2373 | |
| 2374 | if output is not None: |
| 2375 | output.copy_(torch.from_numpy(out_np)) |
| 2376 | |
| 2377 | |
| 2378 | def decode_fast_prefsum(cdf, bytestreams, lengths_prefsum, output): |