forked from boa-collaboration/boa-constrictor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_range_coder.py
More file actions
550 lines (509 loc) · 25.4 KB
/
gpu_range_coder.py
File metadata and controls
550 lines (509 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from __future__ import annotations
import os
import sys
import sysconfig
import importlib
import importlib.util
import pathlib
import tempfile
from textwrap import dedent
import subprocess
import shutil
def _build_and_import_cuda_extension() -> object:
try:
import pybind11 # noqa: F401
except Exception:
raise RuntimeError("pybind11 is required to build CUDA extension")
build_dir = pathlib.Path(tempfile.gettempdir()) / "gpu_range_build"
build_dir.mkdir(parents=True, exist_ok=True)
ext_name = "_gpu_range_cuda_ext"
src_cu = build_dir / (ext_name + ".cu")
# CUDA source implementing a constriction-compatible word-based Range Coder (u32 words, u64 state)
cuda_code = dedent(r'''
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <cuda_runtime.h>
#include <cstdint>
#include <math.h>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <cstring>
namespace py = pybind11;
static constexpr int PRECISION = 24;
struct EncState {
unsigned long long lower;
unsigned long long range;
int inverted_num;
unsigned int first_inv_lower_word;
int write_idx_words;
};
struct DecState {
unsigned long long lower;
unsigned long long range;
unsigned long long point;
int read_idx_words;
};
__device__ void build_cdf_fast(const float* probs_row, int K, uint32_t* cdf) {
const uint32_t TOTAL = 1u << PRECISION;
if (K <= 0) { cdf[0] = 0; cdf[1] = TOTAL; return; }
const uint32_t free_weight = TOTAL - (uint32_t)K;
double norm = 0.0;
for (int i = 0; i < K; ++i) norm += (double)probs_row[i];
if (!(norm > 0.0) || !isfinite(norm)) {
cdf[0] = 0;
uint32_t acc = 0;
for (int i=0;i<K;i++) { cdf[i] = acc; acc += (free_weight / (uint32_t)K) + 1u; }
cdf[K] = TOTAL; return;
}
double scale = (double)free_weight / norm;
double cumulative_float = 0.0;
uint32_t accumulated_slack = 0;
for (int i=0;i<K;i++) {
uint32_t left = (uint32_t)(cumulative_float * scale) + accumulated_slack;
cdf[i] = left;
cumulative_float += (double)probs_row[i];
accumulated_slack += 1u;
}
cdf[K] = TOTAL;
}
__device__ __forceinline__ void flush_inverted(uint32_t* out_row, EncState* st) {
if (st->inverted_num > 0) {
// We can't know wrap condition without original pre-update lower; use point at seal time.
// This function is only called in finalize; handled there.
}
}
__global__ void encode_kernel(const int32_t* d_symbols, const float* d_probs, int N, int K,
uint32_t* d_out_words, EncState* states, int pitch_words,
const uint8_t* d_mask) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= N) return;
if (d_mask && d_mask[idx] == 0) return;
EncState &st = states[idx];
const int32_t s = d_symbols[idx];
const float* probs_row = d_probs + (size_t)idx * (size_t)K;
if (s < 0 || s >= K) return;
uint32_t cdf[1025];
build_cdf_fast(probs_row, K, cdf);
uint32_t left = cdf[s];
uint32_t prob = cdf[s+1] - cdf[s];
unsigned long long scale = st.range >> PRECISION;
st.range = scale * (unsigned long long)prob;
unsigned long long old_lower = st.lower;
st.lower = st.lower + scale * (unsigned long long)left;
// Handle transition out of inverted
if (st.inverted_num > 0) {
unsigned long long sum = st.lower + st.range;
if (sum > st.lower) {
uint32_t first_word, subsequent;
if (st.lower < old_lower) { first_word = st.first_inv_lower_word + 1u; subsequent = 0u; }
else { first_word = st.first_inv_lower_word; subsequent = 0xFFFFFFFFu; }
int widx = st.write_idx_words;
uint32_t* out = d_out_words + (size_t)idx * (size_t)pitch_words;
out[(size_t)widx++] = first_word;
for (int i=1;i<st.inverted_num;i++) out[(size_t)widx++] = subsequent;
st.write_idx_words = widx;
st.inverted_num = 0;
}
}
// Renormalize if needed (emit possibly multiple words)
while (st.range < (1ull << (64-32))) {
uint32_t lower_word = (uint32_t)(st.lower >> (64-32));
st.lower <<= 32;
st.range <<= 32;
if (st.inverted_num > 0) {
if (st.inverted_num < 0x7FFFFFFF) st.inverted_num += 1;
} else {
unsigned long long sum = st.lower + st.range;
if (sum > st.lower) {
int widx = st.write_idx_words;
uint32_t* out = d_out_words + (size_t)idx * (size_t)pitch_words;
out[(size_t)widx++] = lower_word;
st.write_idx_words = widx;
} else {
st.inverted_num = 1;
st.first_inv_lower_word = lower_word;
}
}
}
}
__global__ void finalize_kernel(int N, uint32_t* d_out_words, EncState* states, int pitch_words, int* sizes_words) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= N) return;
EncState &st = states[idx];
uint32_t* out = d_out_words + (size_t)idx * (size_t)pitch_words;
if (st.range == 0xFFFFFFFFFFFFFFFFull) { sizes_words[idx] = 0; return; }
unsigned long long point = st.lower + ((1ull << (64-32)) - 1ull);
if (st.inverted_num > 0) {
uint32_t first_word, subsequent;
if (point < st.lower) { first_word = st.first_inv_lower_word + 1u; subsequent = 0u; }
else { first_word = st.first_inv_lower_word; subsequent = 0xFFFFFFFFu; }
int widx = st.write_idx_words;
out[(size_t)widx++] = first_word;
for (int i=1;i<st.inverted_num;i++) out[(size_t)widx++] = subsequent;
st.write_idx_words = widx;
st.inverted_num = 0;
}
uint32_t point_word = (uint32_t)(point >> (64-32));
int widx = st.write_idx_words;
out[(size_t)widx++] = point_word;
unsigned long long upper = st.lower + st.range;
uint32_t upper_word = (uint32_t)(upper >> (64-32));
if (upper_word == point_word) out[(size_t)widx++] = 0u;
st.write_idx_words = widx;
sizes_words[idx] = widx;
}
__global__ void init_dec_kernel(int N, const uint32_t* d_in_words, int* sizes_words, DecState* dst, int pitch_words) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= N) return;
const uint32_t* in = d_in_words + (size_t)idx * (size_t)pitch_words;
DecState &st = dst[idx];
st.lower = 0ull; st.range = 0xFFFFFFFFFFFFFFFFull; st.point = 0ull; st.read_idx_words = 0;
for (int i=0;i<2;i++) {
uint32_t w = (st.read_idx_words < sizes_words[idx]) ? in[st.read_idx_words++] : 0u;
st.point = (st.point << 32) | (unsigned long long)w;
}
}
__global__ void decode_step_kernel(int N, int K, const float* d_probs, const uint32_t* d_in_words, int* sizes_words,
DecState* st_arr, int pitch_words, int32_t* out_symbols, const uint8_t* d_mask) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= N) return;
if (d_mask && d_mask[idx] == 0) return;
DecState &st = st_arr[idx];
const float* probs_row = d_probs + (size_t)idx * (size_t)K;
const uint32_t* in = d_in_words + (size_t)idx * (size_t)pitch_words;
if (K <= 0) { out_symbols[idx] = 0; return; }
uint32_t cdf[1025];
build_cdf_fast(probs_row, K, cdf);
unsigned long long scale = st.range >> PRECISION;
unsigned long long q = (st.point - st.lower) / scale;
if (q >= (1ull<<PRECISION)) q = (1ull<<PRECISION)-1ull;
uint32_t target = (uint32_t)q;
int next_symbol = 1;
while (next_symbol <= K && !(cdf[next_symbol] > target)) ++next_symbol;
int s = next_symbol - 1; if (s < 0) s = 0; if (s >= K) s = K-1;
out_symbols[idx] = (int32_t)s;
uint32_t left = cdf[s]; uint32_t prob = cdf[s+1] - cdf[s];
st.lower = st.lower + scale * (unsigned long long)left;
st.range = scale * (unsigned long long)prob;
while (st.range < (1ull << (64-32))) {
st.lower <<= 32;
st.range <<= 32;
uint32_t w = (st.read_idx_words < sizes_words[idx]) ? in[st.read_idx_words++] : 0u;
st.point = (st.point << 32) | (unsigned long long)w;
}
}
class RangeCoderBatch {
public:
int N, K, pitch;
EncState* d_enc_states;
DecState* d_dec_states;
uint32_t* d_words;
int* d_sizes; // sizes in words
RangeCoderBatch(int N_, int K_, int pitch_) : N(N_), K(K_), pitch(pitch_), d_enc_states(nullptr), d_dec_states(nullptr), d_words(nullptr), d_sizes(nullptr) {
cudaMalloc(&d_enc_states, sizeof(EncState) * N);
cudaMalloc(&d_dec_states, sizeof(DecState) * N);
cudaMalloc(&d_words, (size_t)N * (size_t)pitch * sizeof(uint32_t));
cudaMalloc(&d_sizes, sizeof(int) * N);
std::vector<EncState> init(N);
for (int i = 0; i < N; ++i) { init[i].lower=0ull; init[i].range=0xFFFFFFFFFFFFFFFFull; init[i].inverted_num=0; init[i].first_inv_lower_word=0u; init[i].write_idx_words=0; }
cudaMemcpy(d_enc_states, init.data(), sizeof(EncState) * N, cudaMemcpyHostToDevice);
std::vector<int> zeros(N, 0);
cudaMemcpy(d_sizes, zeros.data(), sizeof(int) * N, cudaMemcpyHostToDevice);
cudaMemset(d_words, 0, (size_t)N * (size_t)pitch * sizeof(uint32_t));
}
~RangeCoderBatch() {
cudaFree(d_enc_states);
cudaFree(d_dec_states);
cudaFree(d_words);
cudaFree(d_sizes);
}
void load_compressed_from_host(py::list compressed_list) {
if ((int)compressed_list.size() != N) throw std::runtime_error("compressed_list length must equal N");
std::vector<int> sizes_host(N, 0);
std::vector<uint32_t> buf((size_t)N * (size_t)pitch, 0u);
for (int i=0;i<N;i++) {
py::array arr = py::array::ensure(compressed_list[i]);
if (!arr || arr.ndim()!=1 || arr.itemsize()!=4 || arr.dtype().kind()!='u') throw std::runtime_error("Each compressed item must be uint32[?]");
size_t nwords = (size_t)arr.shape(0);
if (nwords > (size_t)pitch) throw std::runtime_error("Compressed stream exceeds pitch; increase pitch");
const uint32_t* src = static_cast<const uint32_t*>(arr.data());
uint32_t* dst = buf.data() + (size_t)i * (size_t)pitch;
std::memcpy(dst, src, nwords * sizeof(uint32_t));
sizes_host[i] = (int)nwords;
}
cudaMemcpy(d_words, buf.data(), buf.size()*sizeof(uint32_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_sizes, sizes_host.data(), sizeof(int)*N, cudaMemcpyHostToDevice);
}
std::vector<int> get_sizes_host() {
std::vector<int> sizes(N);
cudaMemcpy(sizes.data(), d_sizes, sizeof(int)*N, cudaMemcpyDeviceToHost);
return sizes;
}
void set_sizes_from_host(py::list sizes_list) {
if ((int)sizes_list.size()!=N) throw std::runtime_error("sizes_list length must equal N");
std::vector<int> sizes(N);
for (int i=0;i<N;i++) sizes[i] = sizes_list[i].cast<int>();
cudaMemcpy(d_sizes, sizes.data(), sizeof(int)*N, cudaMemcpyHostToDevice);
}
void encode_step_from_device(uint64_t symbols_ptr, uint64_t probs_ptr, uint64_t mask_ptr) {
const int32_t* d_symbols = reinterpret_cast<const int32_t*>(symbols_ptr);
const float* d_probs = reinterpret_cast<const float*>(probs_ptr);
const uint8_t* d_mask = reinterpret_cast<const uint8_t*>(mask_ptr);
int threads = 128; int blocks = (N + threads - 1) / threads;
encode_kernel<<<blocks, threads>>>(d_symbols, d_probs, N, K, d_words, d_enc_states, pitch, d_mask);
cudaDeviceSynchronize();
}
void finalize() {
int threads = 128; int blocks = (N + threads - 1) / threads;
finalize_kernel<<<blocks, threads>>>(N, d_words, d_enc_states, pitch, d_sizes);
cudaDeviceSynchronize();
}
std::vector<py::array_t<uint32_t>> get_compressed_host() {
std::vector<int> sizes(N);
cudaMemcpy(sizes.data(), d_sizes, sizeof(int) * N, cudaMemcpyDeviceToHost);
std::vector<uint32_t> buf((size_t)N * (size_t)pitch);
cudaMemcpy(buf.data(), d_words, buf.size()*sizeof(uint32_t), cudaMemcpyDeviceToHost);
std::vector<py::array_t<uint32_t>> out; out.reserve(N);
for (int i=0;i<N;i++) {
size_t nwords = (size_t)std::max(0, sizes[i]);
py::array_t<uint32_t> arr(nwords);
auto r = arr.mutable_unchecked<1>();
for (size_t w=0; w<nwords; ++w) r(w) = buf[(size_t)i*(size_t)pitch + w];
out.push_back(arr);
}
return out;
}
void init_decoder_from_current_bytes() {
int threads = 128; int blocks = (N + threads - 1) / threads;
init_dec_kernel<<<blocks, threads>>>(N, d_words, d_sizes, d_dec_states, pitch);
cudaDeviceSynchronize();
}
void decode_step_to_device(uint64_t probs_ptr, uint64_t out_symbols_ptr, uint64_t mask_ptr) {
const float* d_probs = reinterpret_cast<const float*>(probs_ptr);
int32_t* d_out = reinterpret_cast<int32_t*>(out_symbols_ptr);
const uint8_t* d_mask = reinterpret_cast<const uint8_t*>(mask_ptr);
int threads = 128; int blocks = (N + threads - 1) / threads;
decode_step_kernel<<<blocks, threads>>>(N, K, d_probs, d_words, d_sizes, d_dec_states, pitch, d_out, d_mask);
cudaDeviceSynchronize();
}
};
PYBIND11_MODULE(_gpu_range_cuda_ext, m) {
m.doc() = "GPU-backed range coder (constriction-compatible, u32 words, u64 state)";
py::class_<RangeCoderBatch>(m, "RangeCoderBatch")
.def(py::init<int,int,int>())
.def("load_compressed_from_host", &RangeCoderBatch::load_compressed_from_host,
py::arg("compressed_list"))
.def("get_sizes_host", &RangeCoderBatch::get_sizes_host)
.def("set_sizes_from_host", &RangeCoderBatch::set_sizes_from_host,
py::arg("sizes_list"))
.def("encode_step_from_device", &RangeCoderBatch::encode_step_from_device,
py::arg("symbols_ptr"), py::arg("probs_ptr"), py::arg("mask_ptr") = 0)
.def("finalize", &RangeCoderBatch::finalize)
.def("get_compressed_host", &RangeCoderBatch::get_compressed_host)
.def("init_decoder_from_current_bytes", &RangeCoderBatch::init_decoder_from_current_bytes)
.def("decode_step_to_device", &RangeCoderBatch::decode_step_to_device,
py::arg("probs_ptr"), py::arg("out_symbols_ptr"), py::arg("mask_ptr") = 0);
}
''')
src_cu.write_text(cuda_code)
# Compile with nvcc
nvcc = shutil.which('nvcc')
if nvcc is None:
raise RuntimeError('nvcc not found in PATH')
from importlib.machinery import EXTENSION_SUFFIXES
suf = EXTENSION_SUFFIXES[0]
so_path = build_dir / (ext_name + suf)
# Build include dirs robustly (handle spaces in paths)
import pybind11
inc_dirs = []
try:
cfg_paths = sysconfig.get_paths()
for key in ('include', 'platinclude'): # e.g., Python.h location
p = cfg_paths.get(key)
if p:
inc_dirs.append(p)
except Exception:
pass
# pybind11 headers
for k in (pybind11.get_include(False), pybind11.get_include(True)):
if k and k not in inc_dirs:
inc_dirs.append(k)
cmd = [nvcc, str(src_cu), '-shared', '-Xcompiler', '-fPIC', '-o', str(so_path),
'-std=c++14', '-O3']
# CUDA runtime library
cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH') or '/usr/local/cuda'
lib64 = pathlib.Path(cuda_home) / 'lib64'
if lib64.exists():
cmd += ['-L', str(lib64)]
# add rpath for runtime loading
cmd += ['-Xlinker', f'-rpath,{str(lib64)}']
cmd += ['-lcudart']
# Add include directories as separate args to preserve spaces
for inc in inc_dirs:
cmd += ['-I', inc]
# Run nvcc
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode != 0:
raise RuntimeError(f'nvcc build failed: {res.stderr.decode()}')
spec = importlib.util.spec_from_file_location(ext_name, str(so_path))
if spec is None or spec.loader is None:
raise RuntimeError('Failed to load CUDA built extension')
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod) # type: ignore
return mod
# Build extension on import: try CUDA if requested, otherwise CPU fallback
_ext = None
_cpu_ext = None # optional CPU extension placeholder for fallbacks
_ext = _build_and_import_cuda_extension()
# Expose a minimal namespace compatible with the used subset: constriction.stream.queue
class _ModelStub:
def __init__(self, kind: str, **kwargs):
self.kind = kind
self.kwargs = kwargs
class stream:
class model:
class Categorical(_ModelStub):
def __init__(self, perfect: bool = False):
super().__init__("categorical", perfect=perfect)
class queue:
class RangeEncoder:
def __init__(self):
# If extension provides a RangeEncoder class use it, otherwise prepare Python buffer for GPU function
if hasattr(_ext, 'RangeEncoder'):
self._enc = _ext.RangeEncoder()
self._pybuf = None
else:
self._enc = None
self._pybuf = {'symbols': [], 'probs': []}
def clear(self):
if self._enc is not None:
self._enc.clear()
else:
self._pybuf = {'symbols': [], 'probs': []}
def get_compressed(self):
if self._enc is not None:
return self._enc.get_compressed()
else:
import numpy as np
if len(self._pybuf['symbols']) == 0:
return np.zeros(0, dtype=np.uint32)
symbols = np.asarray(self._pybuf['symbols'], dtype=np.int32)
probs = np.asarray(self._pybuf['probs'], dtype=np.float32)
# For correctness, delegate to CPU extension's RangeEncoder to produce
# the exact same compressed format the CPU decoder expects.
if _cpu_ext is not None and hasattr(_cpu_ext, 'RangeEncoder'):
cpu_enc = _cpu_ext.RangeEncoder()
cpu_enc.encode_categorical(symbols, probs)
return cpu_enc.get_compressed()
# Fallback: try CUDA encode function (may be incompatible)
return _ext.encode_rows_gpu(symbols, probs)
def encode(self, symbols, model, probs):
"""
Supports Option 3: encode(symbols, model_family=Categorical, probs).
- symbols: rank-1 int32 numpy array (len=N)
- model: constriction.stream.model.Categorical (ignored other than type)
- probs: rank-2 float32 numpy array with shape (N, K)
"""
import numpy as np
# Normalize inputs
if not hasattr(symbols, "dtype"):
symbols = np.array([int(symbols)], dtype=np.int32)
symbols = np.asarray(symbols, dtype=np.int32)
probs = np.asarray(probs, dtype=np.float32)
if symbols.ndim != 1:
raise ValueError("symbols must be rank-1")
if probs.ndim != 2 or probs.shape[0] != symbols.shape[0]:
raise ValueError("probs must be rank-2 with probs.shape[0] == len(symbols)")
if not isinstance(model, _ModelStub) or model.kind != "categorical":
raise TypeError("Only Categorical model is supported in this GPU stub")
if self._enc is not None:
# delegate to compiled RangeEncoder
self._enc.encode_categorical(symbols, probs)
else:
# buffer for batched GPU encode
for s in symbols.tolist():
self._pybuf['symbols'].append(int(s))
# ensure probs is list of rows
for row in probs.astype(np.float32):
self._pybuf['probs'].append(row.tolist())
class RangeDecoder:
def __init__(self, compressed):
# Prefer compiled decoder if provided; otherwise fall back to CPU extension
if hasattr(_ext, 'RangeDecoder'):
self._dec = _ext.RangeDecoder(compressed)
elif _cpu_ext is not None and hasattr(_cpu_ext, 'RangeDecoder'):
self._dec = _cpu_ext.RangeDecoder(compressed)
else:
raise RuntimeError('No decoder available in extensions')
def decode(self, model, probs_or_amt, *rest):
"""
Supports Option 3: decode(model_family=Categorical, probs) -> symbols array.
"""
import numpy as np
if not isinstance(model, _ModelStub) or model.kind != "categorical":
raise TypeError("Only Categorical model is supported in this GPU stub")
probs = np.asarray(probs_or_amt, dtype=np.float32)
if probs.ndim != 2:
raise ValueError("probs must be rank-2")
return self._dec.decode_categorical(probs)
# For convenience, re-export top-level like constriction
__all__ = ["stream"]
# Optional: GPU batch coder convenience wrapper (requires torch)
class gpu:
class queue:
class RangeCoderBatch:
def __init__(self, N: int, K: int, maxL: int, pitch_bytes: int | None = None):
if pitch_bytes is None:
pitch_bytes = max(256, maxL * 8)
self.N, self.K, self.maxL = N, K, maxL
global _ext
if _ext is None or not hasattr(_ext, 'RangeCoderBatch'):
# Try to build CUDA extension lazily if not present
try:
_ext = _build_and_import_cuda_extension()
except Exception as e:
raise RuntimeError('CUDA extension with RangeCoderBatch not available') from e
# CUDA backend expects pitch in 32-bit words
pitch_words = (int(pitch_bytes) + 3) // 4
self._batch = _ext.RangeCoderBatch(N, K, pitch_words)
def load_compressed_list(self, compressed_list):
# Accept list of np.uint32 arrays (one per stream)
self._batch.load_compressed_from_host(compressed_list)
def encode_step(self, symbols_gpu, probs_gpu, mask=None):
import torch
assert symbols_gpu.is_cuda and probs_gpu.is_cuda
assert symbols_gpu.numel() == self.N and probs_gpu.shape == (self.N, self.K)
if symbols_gpu.dtype != torch.int32:
symbols_gpu = symbols_gpu.to(torch.int32)
if probs_gpu.dtype != torch.float32:
probs_gpu = probs_gpu.to(torch.float32)
mask_ptr = 0
if mask is not None:
assert mask.is_cuda and mask.shape == (self.N,)
if mask.dtype != torch.uint8:
mask = mask.to(torch.uint8)
mask_ptr = int(mask.data_ptr())
self._batch.encode_step_from_device(int(symbols_gpu.data_ptr()), int(probs_gpu.data_ptr()), mask_ptr)
def finalize(self):
self._batch.finalize()
def get_compressed_list(self):
return self._batch.get_compressed_host()
def get_sizes_list(self):
return self._batch.get_sizes_host()
def init_decoder(self):
self._batch.init_decoder_from_current_bytes()
def decode_step(self, probs_gpu, out_symbols_gpu, mask=None):
import torch
assert probs_gpu.is_cuda and out_symbols_gpu.is_cuda
assert probs_gpu.shape == (self.N, self.K) and out_symbols_gpu.numel() == self.N
if probs_gpu.dtype != torch.float32:
probs_gpu = probs_gpu.to(torch.float32)
mask_ptr = 0
if mask is not None:
assert mask.is_cuda and mask.shape == (self.N,)
if mask.dtype != torch.uint8:
mask = mask.to(torch.uint8)
mask_ptr = int(mask.data_ptr())
self._batch.decode_step_to_device(int(probs_gpu.data_ptr()), int(out_symbols_gpu.data_ptr()), mask_ptr)