-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy path_gpu_decode.py
More file actions
2319 lines (1948 loc) · 83.2 KB
/
_gpu_decode.py
File metadata and controls
2319 lines (1948 loc) · 83.2 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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""GPU-accelerated TIFF tile decompression via Numba CUDA.
Provides CUDA kernels for LZW decode, horizontal predictor decode,
and floating-point predictor decode. Each tile is processed by one
thread (LZW is sequential per-stream), but all tiles run in parallel.
"""
from __future__ import annotations
import math
import numpy as np
from numba import cuda
# LZW constants (same as _compression.py)
LZW_CLEAR_CODE = 256
LZW_EOI_CODE = 257
LZW_FIRST_CODE = 258
LZW_MAX_CODE = 4095
LZW_MAX_BITS = 12
# ---------------------------------------------------------------------------
# LZW decode kernel -- one thread per tile
# ---------------------------------------------------------------------------
@cuda.jit
def _lzw_decode_tiles_kernel(
compressed_buf, # uint8: all compressed tile data concatenated
tile_offsets, # int64: start offset of each tile in compressed_buf
tile_sizes, # int64: compressed size of each tile
decompressed_buf, # uint8: output buffer (all tiles concatenated)
tile_out_offsets, # int64: start offset of each tile in decompressed_buf
tile_out_sizes, # int64: expected decompressed size per tile
tile_actual_sizes, # int64: actual bytes written per tile (output)
):
"""Decode one LZW tile per thread block.
One thread block = one tile. Thread 0 in each block does the sequential
LZW decode. The table lives in shared memory (fast, ~20KB per block)
instead of local memory (slow DRAM spill).
"""
tile_idx = cuda.blockIdx.x
if tile_idx >= tile_offsets.shape[0]:
return
# Only thread 0 in each block does the work
if cuda.threadIdx.x != 0:
return
src_start = tile_offsets[tile_idx]
src_len = tile_sizes[tile_idx]
dst_start = tile_out_offsets[tile_idx]
dst_len = tile_out_sizes[tile_idx]
if src_len == 0:
tile_actual_sizes[tile_idx] = 0
return
# LZW table in shared memory (fast on-chip SRAM)
table_prefix = cuda.shared.array(4096, dtype=numba_int32)
table_suffix = cuda.shared.array(4096, dtype=numba_uint8)
stack = cuda.shared.array(4096, dtype=numba_uint8)
# Initialize single-byte entries
for i in range(256):
table_prefix[i] = -1
table_suffix[i] = numba_uint8(i)
for i in range(256, 4096):
table_prefix[i] = -1
table_suffix[i] = numba_uint8(0)
bit_pos = 0
code_size = 9
next_code = LZW_FIRST_CODE
out_pos = 0
old_code = -1
while True:
# Read next code (MSB-first)
byte_offset = bit_pos >> 3
if byte_offset >= src_len:
break
b0 = numba_int32(compressed_buf[src_start + byte_offset]) << 16
if byte_offset + 1 < src_len:
b0 |= numba_int32(compressed_buf[src_start + byte_offset + 1]) << 8
if byte_offset + 2 < src_len:
b0 |= numba_int32(compressed_buf[src_start + byte_offset + 2])
bit_off = bit_pos & 7
code = (b0 >> (24 - bit_off - code_size)) & ((1 << code_size) - 1)
bit_pos += code_size
if code == LZW_EOI_CODE:
break
if code == LZW_CLEAR_CODE:
code_size = 9
next_code = LZW_FIRST_CODE
old_code = -1
continue
if old_code == -1:
if code < 256 and out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = numba_uint8(code)
out_pos += 1
old_code = code
continue
if code < next_code:
# Walk chain, push to stack
c = code
sp = 0
while c >= 0 and c < 4096 and sp < 4096:
stack[sp] = table_suffix[c]
sp += 1
c = table_prefix[c]
# Emit reversed
for i in range(sp - 1, -1, -1):
if out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = stack[i]
out_pos += 1
if next_code <= LZW_MAX_CODE and sp > 0:
table_prefix[next_code] = old_code
table_suffix[next_code] = stack[sp - 1]
next_code += 1
else:
# Special case: code == next_code
c = old_code
sp = 0
while c >= 0 and c < 4096 and sp < 4096:
stack[sp] = table_suffix[c]
sp += 1
c = table_prefix[c]
if sp == 0:
old_code = code
continue
first_char = stack[sp - 1]
for i in range(sp - 1, -1, -1):
if out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = stack[i]
out_pos += 1
if out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = first_char
out_pos += 1
if next_code <= LZW_MAX_CODE:
table_prefix[next_code] = old_code
table_suffix[next_code] = first_char
next_code += 1
# Early change
if next_code > (1 << code_size) - 2 and code_size < LZW_MAX_BITS:
code_size += 1
old_code = code
tile_actual_sizes[tile_idx] = out_pos
# Type aliases for Numba CUDA local arrays
from numba import int32 as numba_int32, uint8 as numba_uint8, int64 as numba_int64
# ---------------------------------------------------------------------------
# Deflate/inflate decode kernel -- one thread block per tile
# ---------------------------------------------------------------------------
# Static tables for deflate
# Length base values and extra bits for codes 257-285
_LEN_BASE = np.array([
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258,
], dtype=np.int32)
_LEN_EXTRA = np.array([
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0,
], dtype=np.int32)
# Distance base values and extra bits for codes 0-29
_DIST_BASE = np.array([
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193,
12289, 16385, 24577,
], dtype=np.int32)
_DIST_EXTRA = np.array([
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
], dtype=np.int32)
# Code length code order (for dynamic Huffman)
_CL_ORDER = np.array([
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
], dtype=np.int32)
@cuda.jit(device=True)
def _inflate_read_bits(src, src_start, src_len, bit_pos, n):
"""Read n bits (LSB-first) from the source stream."""
val = numba_int32(0)
for i in range(n):
byte_idx = (bit_pos[0] >> 3)
bit_idx = bit_pos[0] & 7
if byte_idx < src_len:
val |= numba_int32((src[src_start + byte_idx] >> bit_idx) & 1) << i
bit_pos[0] += 1
return val
@cuda.jit(device=True)
def _inflate_build_table(lengths, n_codes, table, max_bits,
overflow_codes, overflow_lens, n_overflow):
"""Build a Huffman decode table from code lengths.
Codes <= max_bits go into the fast table: table[reversed_code] = (sym << 5) | length.
Codes > max_bits go into overflow arrays for slow-path decode.
"""
bl_count = cuda.local.array(16, dtype=numba_int32)
for i in range(16):
bl_count[i] = 0
for i in range(n_codes):
bl_count[lengths[i]] += 1
bl_count[0] = 0
next_code = cuda.local.array(16, dtype=numba_int32)
code = 0
for bits in range(1, 16):
code = (code + bl_count[bits - 1]) << 1
next_code[bits] = code
for i in range(1 << max_bits):
table[i] = 0
n_overflow[0] = 0
for sym in range(n_codes):
ln = lengths[sym]
if ln == 0:
continue
code = next_code[ln]
next_code[ln] += 1
# Reverse the code bits for LSB-first lookup
rev = numba_int32(0)
c = code
for b in range(ln):
rev = (rev << 1) | (c & 1)
c >>= 1
if ln <= max_bits:
# Fast table: fill all entries that share this prefix
# (entries where the extra high bits vary)
step = 1 << ln
idx = rev
while idx < (1 << max_bits):
table[idx] = numba_int32((sym << 5) | ln)
idx += step
else:
# Overflow: store reversed code + length for slow-path scan
oi = n_overflow[0]
if oi < overflow_codes.shape[0]:
overflow_codes[oi] = rev
overflow_lens[oi] = (sym << 5) | ln
n_overflow[0] = oi + 1
@cuda.jit(device=True)
def _inflate_decode_symbol(src, src_start, src_len, bit_pos, table, max_bits,
overflow_codes, overflow_lens, n_overflow):
"""Decode one Huffman symbol. Fast table for short codes, overflow scan for long."""
# Peek 15 bits (max deflate code length)
peek = numba_int64(0)
for i in range(15):
byte_idx = (bit_pos[0] + i) >> 3
bit_idx = (bit_pos[0] + i) & 7
if byte_idx < src_len:
peek |= numba_int64((src[src_start + byte_idx] >> bit_idx) & 1) << i
# Try fast table first
entry = table[numba_int32(peek) & ((1 << max_bits) - 1)]
length = entry & 0x1F
symbol = entry >> 5
if length > 0:
bit_pos[0] += length
return symbol
# Slow path: scan overflow entries
for i in range(n_overflow[0]):
ov_rev = overflow_codes[i]
ov_entry = overflow_lens[i]
ov_len = ov_entry & 0x1F
ov_sym = ov_entry >> 5
mask = (1 << ov_len) - 1
if (numba_int32(peek) & mask) == ov_rev:
bit_pos[0] += ov_len
return ov_sym
# Should not happen with valid data -- advance 1 bit to avoid freeze
bit_pos[0] += 1
return 0
@cuda.jit
def _inflate_tiles_kernel(
compressed_buf,
tile_offsets,
tile_sizes,
decompressed_buf,
tile_out_offsets,
tile_out_sizes,
tile_actual_sizes,
d_len_base, d_len_extra, d_dist_base, d_dist_extra, d_cl_order,
):
"""Inflate (decompress) one zlib-wrapped deflate tile per thread block.
Thread 0 in each block does the sequential inflate.
Huffman table in shared memory.
"""
tile_idx = cuda.blockIdx.x
if tile_idx >= tile_offsets.shape[0]:
return
if cuda.threadIdx.x != 0:
return
src_start = tile_offsets[tile_idx]
src_len = tile_sizes[tile_idx]
dst_start = tile_out_offsets[tile_idx]
dst_len = tile_out_sizes[tile_idx]
if src_len <= 2:
tile_actual_sizes[tile_idx] = 0
return
# Skip 2-byte zlib header (0x78 0x9C or similar)
bit_pos = cuda.local.array(1, dtype=numba_int64)
bit_pos[0] = numba_int64(16) # skip 2 bytes = 16 bits
out_pos = 0
# Two-level Huffman tables:
# Level 1 (shared memory, fast): 10-bit lookup (1024 entries)
# Level 2 (local memory, slow): overflow for codes > 10 bits
MAX_LIT_BITS = 10
MAX_DIST_BITS = 10
lit_table = cuda.shared.array(1024, dtype=numba_int32)
dist_table = cuda.shared.array(1024, dtype=numba_int32)
# Overflow arrays for long codes (rarely > 50 entries)
lit_ov_codes = cuda.local.array(64, dtype=numba_int32)
lit_ov_lens = cuda.local.array(64, dtype=numba_int32)
n_lit_ov = cuda.local.array(1, dtype=numba_int32)
dist_ov_codes = cuda.local.array(32, dtype=numba_int32)
dist_ov_lens = cuda.local.array(32, dtype=numba_int32)
n_dist_ov = cuda.local.array(1, dtype=numba_int32)
n_lit_ov[0] = 0
n_dist_ov[0] = 0
code_lengths = cuda.local.array(320, dtype=numba_int32)
while True:
# Read block header
bfinal = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 1)
btype = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 2)
if btype == 0:
# Stored block: align to byte boundary, read len
bit_pos[0] = ((bit_pos[0] + 7) >> 3) << 3
ln = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 16)
_inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 16) # nlen (complement)
for i in range(ln):
byte_idx = bit_pos[0] >> 3
if byte_idx < src_len and out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = compressed_buf[src_start + byte_idx]
out_pos += 1
bit_pos[0] += 8
elif btype == 1:
# Fixed Huffman: build fixed tables
for i in range(144):
code_lengths[i] = 8
for i in range(144, 256):
code_lengths[i] = 9
for i in range(256, 280):
code_lengths[i] = 7
for i in range(280, 288):
code_lengths[i] = 8
_inflate_build_table(code_lengths, 288, lit_table, MAX_LIT_BITS,
lit_ov_codes, lit_ov_lens, n_lit_ov)
for i in range(30):
code_lengths[i] = 5
_inflate_build_table(code_lengths, 30, dist_table, MAX_DIST_BITS,
dist_ov_codes, dist_ov_lens, n_dist_ov)
# Decode symbols
while True:
sym = _inflate_decode_symbol(
compressed_buf, src_start, src_len, bit_pos,
lit_table, MAX_LIT_BITS,
lit_ov_codes, lit_ov_lens, n_lit_ov)
if sym < 256:
if out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = numba_uint8(sym)
out_pos += 1
elif sym == 256:
break
else:
# Length-distance pair
li = sym - 257
if li < 29:
length = d_len_base[li]
if d_len_extra[li] > 0:
length += _inflate_read_bits(
compressed_buf, src_start, src_len,
bit_pos, d_len_extra[li])
else:
length = 3
dsym = _inflate_decode_symbol(
compressed_buf, src_start, src_len, bit_pos,
dist_table, MAX_DIST_BITS,
dist_ov_codes, dist_ov_lens, n_dist_ov)
if dsym < 30:
dist = d_dist_base[dsym]
if d_dist_extra[dsym] > 0:
dist += _inflate_read_bits(
compressed_buf, src_start, src_len,
bit_pos, d_dist_extra[dsym])
else:
dist = 1
# Copy from output window
for i in range(length):
if out_pos < dst_len and dist <= out_pos:
decompressed_buf[dst_start + out_pos] = \
decompressed_buf[dst_start + out_pos - dist]
out_pos += 1
elif btype == 2:
# Dynamic Huffman: read code length codes, then build tables
hlit = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 5) + 257
hdist = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 5) + 1
hclen = _inflate_read_bits(compressed_buf, src_start, src_len, bit_pos, 4) + 4
# Read code length code lengths
cl_lengths = cuda.local.array(19, dtype=numba_int32)
for i in range(19):
cl_lengths[i] = 0
for i in range(hclen):
cl_lengths[d_cl_order[i]] = _inflate_read_bits(
compressed_buf, src_start, src_len, bit_pos, 3)
# Build code length Huffman table (small: 7 bits max, no overflow)
cl_table = cuda.local.array(128, dtype=numba_int32)
cl_ov_c = cuda.local.array(4, dtype=numba_int32)
cl_ov_l = cuda.local.array(4, dtype=numba_int32)
n_cl_ov = cuda.local.array(1, dtype=numba_int32)
n_cl_ov[0] = 0
_inflate_build_table(cl_lengths, 19, cl_table, 7,
cl_ov_c, cl_ov_l, n_cl_ov)
# Decode literal/length + distance code lengths
total_codes = hlit + hdist
idx = 0
for i in range(320):
code_lengths[i] = 0
while idx < total_codes:
sym = numba_int32(0)
# Decode from cl_table (7-bit)
peek = numba_int32(0)
for b in range(7):
byte_idx = (bit_pos[0] + b) >> 3
bit_idx = (bit_pos[0] + b) & 7
if byte_idx < src_len:
peek |= numba_int32(
(compressed_buf[src_start + byte_idx] >> bit_idx) & 1) << b
entry = cl_table[peek & 127]
ln = entry & 0x1F
sym = entry >> 5
if ln > 0:
bit_pos[0] += ln
else:
bit_pos[0] += 1
if sym < 16:
code_lengths[idx] = sym
idx += 1
elif sym == 16:
rep = _inflate_read_bits(
compressed_buf, src_start, src_len, bit_pos, 2) + 3
val = code_lengths[idx - 1] if idx > 0 else 0
for _ in range(rep):
if idx < 320:
code_lengths[idx] = val
idx += 1
elif sym == 17:
rep = _inflate_read_bits(
compressed_buf, src_start, src_len, bit_pos, 3) + 3
for _ in range(rep):
if idx < 320:
code_lengths[idx] = 0
idx += 1
elif sym == 18:
rep = _inflate_read_bits(
compressed_buf, src_start, src_len, bit_pos, 7) + 11
for _ in range(rep):
if idx < 320:
code_lengths[idx] = 0
idx += 1
# Build lit/len and dist tables
n_lit_ov[0] = 0
_inflate_build_table(code_lengths, hlit, lit_table, MAX_LIT_BITS,
lit_ov_codes, lit_ov_lens, n_lit_ov)
# Distance codes start at code_lengths[hlit]
dist_lengths = cuda.local.array(32, dtype=numba_int32)
for i in range(32):
dist_lengths[i] = 0
for i in range(hdist):
dist_lengths[i] = code_lengths[hlit + i]
n_dist_ov[0] = 0
_inflate_build_table(dist_lengths, hdist, dist_table, MAX_DIST_BITS,
dist_ov_codes, dist_ov_lens, n_dist_ov)
# Decode symbols (same loop as fixed Huffman)
while True:
sym = _inflate_decode_symbol(
compressed_buf, src_start, src_len, bit_pos,
lit_table, MAX_LIT_BITS,
lit_ov_codes, lit_ov_lens, n_lit_ov)
if sym < 256:
if out_pos < dst_len:
decompressed_buf[dst_start + out_pos] = numba_uint8(sym)
out_pos += 1
elif sym == 256:
break
else:
li = sym - 257
if li < 29:
length = d_len_base[li]
if d_len_extra[li] > 0:
length += _inflate_read_bits(
compressed_buf, src_start, src_len,
bit_pos, d_len_extra[li])
else:
length = 3
dsym = _inflate_decode_symbol(
compressed_buf, src_start, src_len, bit_pos,
dist_table, MAX_DIST_BITS,
dist_ov_codes, dist_ov_lens, n_dist_ov)
if dsym < 30:
dist = d_dist_base[dsym]
if d_dist_extra[dsym] > 0:
dist += _inflate_read_bits(
compressed_buf, src_start, src_len,
bit_pos, d_dist_extra[dsym])
else:
dist = 1
for i in range(length):
if out_pos < dst_len and dist <= out_pos:
decompressed_buf[dst_start + out_pos] = \
decompressed_buf[dst_start + out_pos - dist]
out_pos += 1
else:
break # invalid block type
if bfinal:
break
tile_actual_sizes[tile_idx] = out_pos
# ---------------------------------------------------------------------------
# Predictor decode kernels -- one thread per row
# ---------------------------------------------------------------------------
@cuda.jit
def _predictor_decode_kernel(data, width, height, bytes_per_sample):
"""Undo horizontal differencing (predictor=2), one thread per row."""
row = cuda.grid(1)
if row >= height:
return
row_bytes = width * bytes_per_sample
row_start = row * row_bytes
for col in range(bytes_per_sample, row_bytes):
idx = row_start + col
data[idx] = numba_uint8(
(numba_int32(data[idx]) + numba_int32(data[idx - bytes_per_sample])) & 0xFF)
@cuda.jit
def _fp_predictor_decode_kernel(data, tmp, width, height, bps):
"""Undo floating-point predictor (predictor=3), one thread per row.
data: flat uint8 device array
tmp: scratch buffer, same size as data
"""
row = cuda.grid(1)
if row >= height:
return
row_len = width * bps
start = row * row_len
# Step 1: undo horizontal differencing
for i in range(1, row_len):
idx = start + i
data[idx] = numba_uint8(
(numba_int32(data[idx]) + numba_int32(data[idx - 1])) & 0xFF)
# Step 2: un-transpose byte lanes (MSB-first) back to native order
for sample in range(width):
for b in range(bps):
tmp[start + sample * bps + b] = data[start + (bps - 1 - b) * width + sample]
# Copy back
for i in range(row_len):
data[start + i] = tmp[start + i]
# ---------------------------------------------------------------------------
# Tile assembly kernel -- one thread per output pixel
# ---------------------------------------------------------------------------
@cuda.jit
def _assemble_tiles_kernel(
decompressed_buf, # uint8: all decompressed tiles concatenated
tile_out_offsets, # int64: byte offset of each tile in decompressed_buf
tile_width, # int: tile width in pixels
tile_height, # int: tile height in pixels
bytes_per_pixel, # int: dtype.itemsize * samples_per_pixel
image_width, # int: output image width
image_height, # int: output image height
tiles_across, # int: number of tile columns
output, # uint8: output image buffer (flat, row-major)
):
"""Copy decompressed tile pixels into the output image, one thread per pixel."""
pixel_idx = cuda.grid(1)
total_pixels = image_width * image_height
if pixel_idx >= total_pixels:
return
# Output row and column
out_row = pixel_idx // image_width
out_col = pixel_idx % image_width
# Which tile does this pixel belong to?
tile_row = out_row // tile_height
tile_col = out_col // tile_width
tile_idx = tile_row * tiles_across + tile_col
# Position within the tile
local_row = out_row - tile_row * tile_height
local_col = out_col - tile_col * tile_width
# Source and destination byte offsets
tile_offset = tile_out_offsets[tile_idx]
src_byte = tile_offset + (local_row * tile_width + local_col) * bytes_per_pixel
dst_byte = (out_row * image_width + out_col) * bytes_per_pixel
for b in range(bytes_per_pixel):
output[dst_byte + b] = decompressed_buf[src_byte + b]
# ---------------------------------------------------------------------------
# KvikIO GDS (GPUDirect Storage) -- read file directly to GPU
# ---------------------------------------------------------------------------
def _try_kvikio_read_tiles(file_path, tile_offsets, tile_byte_counts, tile_bytes):
"""Read compressed tile bytes directly from SSD to GPU via GDS.
When kvikio is available and GDS is supported, file data is DMA'd
directly from the NVMe drive to GPU VRAM, bypassing CPU entirely.
Falls back to None if kvikio is not installed or GDS is not available.
Returns list of cupy arrays (one per tile) on GPU, or None.
"""
try:
import kvikio
import cupy
except ImportError:
return None
try:
d_tiles = []
with kvikio.CuFile(file_path, 'r') as f:
for off, bc in zip(tile_offsets, tile_byte_counts):
buf = cupy.empty(bc, dtype=cupy.uint8)
nbytes = f.pread(buf, file_offset=off)
# Verify the read completed correctly
actual = nbytes.get() if hasattr(nbytes, 'get') else int(nbytes)
if actual != bc:
return None # partial read, fall back
d_tiles.append(buf)
cupy.cuda.Device().synchronize()
return d_tiles
except Exception:
# GDS not available, version mismatch, or CUDA error
# Reset CUDA error state if possible
try:
import cupy
cupy.cuda.Device().synchronize()
except Exception:
pass
return None
# ---------------------------------------------------------------------------
# nvCOMP batch decompression (optional, fast path)
# ---------------------------------------------------------------------------
def _find_nvcomp_lib():
"""Find and load libnvcomp.so. Returns ctypes.CDLL or None."""
import ctypes
import os
# Try common locations
search_paths = [
'libnvcomp.so', # system LD_LIBRARY_PATH
]
# Check conda envs
conda_prefix = os.environ.get('CONDA_PREFIX', '')
if conda_prefix:
search_paths.append(os.path.join(conda_prefix, 'lib', 'libnvcomp.so'))
# Also check sibling conda envs that might have rapids
conda_base = os.path.dirname(conda_prefix) if conda_prefix else ''
if conda_base:
for env in ['rapids', 'test-again', 'rtxpy-fire']:
p = os.path.join(conda_base, env, 'lib', 'libnvcomp.so')
if os.path.exists(p):
search_paths.append(p)
for path in search_paths:
try:
return ctypes.CDLL(path)
except OSError:
continue
return None
_nvcomp_lib = None
_nvcomp_checked = False
def _get_nvcomp():
"""Get the nvCOMP library handle (cached). Returns CDLL or None."""
global _nvcomp_lib, _nvcomp_checked
if not _nvcomp_checked:
_nvcomp_checked = True
_nvcomp_lib = _find_nvcomp_lib()
return _nvcomp_lib
def _try_nvcomp_batch_decompress(compressed_tiles, tile_bytes, compression):
"""Try batch decompression via nvCOMP C API. Returns CuPy array or None.
Uses nvcompBatchedDeflateDecompressAsync to decompress all tiles in
one GPU API call. Falls back to None if nvCOMP is not available.
"""
if compression not in (8, 32946, 50000): # Deflate and ZSTD
return None
lib = _get_nvcomp()
if lib is None:
# Try kvikio.nvcomp as alternative
try:
import kvikio.nvcomp as nvcomp
except ImportError:
return None
import cupy
try:
raw_tiles = []
for tile in compressed_tiles:
raw_tiles.append(tile[2:-4] if len(tile) > 6 else tile)
manager = nvcomp.DeflateManager(chunk_size=tile_bytes)
d_compressed = [cupy.asarray(np.frombuffer(t, dtype=np.uint8))
for t in raw_tiles]
d_decompressed = manager.decompress(d_compressed)
return cupy.concatenate([d.ravel() for d in d_decompressed])
except Exception:
return None
# Direct ctypes nvCOMP C API
import ctypes
import cupy
class _NvcompDecompOpts(ctypes.Structure):
"""nvCOMP batched decompression options (passed by value)."""
_fields_ = [
('backend', ctypes.c_int),
('reserved', ctypes.c_char * 60),
]
# Deflate has a different struct with sort_before_hw_decompress field
class _NvcompDeflateDecompOpts(ctypes.Structure):
_fields_ = [
('backend', ctypes.c_int),
('sort_before_hw_decompress', ctypes.c_int),
('reserved', ctypes.c_char * 56),
]
try:
n_tiles = len(compressed_tiles)
# Prepare compressed tiles for nvCOMP
if compression in (8, 32946): # Deflate
# Strip 2-byte zlib header + 4-byte adler32 checksum
raw_tiles = [t[2:-4] if len(t) > 6 else t for t in compressed_tiles]
get_temp_fn = 'nvcompBatchedDeflateDecompressGetTempSizeAsync'
decomp_fn = 'nvcompBatchedDeflateDecompressAsync'
# backend=2 (CUDA) works on all GPUs; backend=1 (HW) needs Ada/Hopper
opts = _NvcompDeflateDecompOpts(backend=2, sort_before_hw_decompress=0,
reserved=b'\x00' * 56)
elif compression == 50000: # ZSTD
raw_tiles = list(compressed_tiles) # no header stripping
get_temp_fn = 'nvcompBatchedZstdDecompressGetTempSizeAsync'
decomp_fn = 'nvcompBatchedZstdDecompressAsync'
opts = _NvcompDecompOpts(backend=0, reserved=b'\x00' * 60)
else:
return None
# Upload compressed tiles to device
d_comp_bufs = [cupy.asarray(np.frombuffer(t, dtype=np.uint8)) for t in raw_tiles]
d_decomp_bufs = [cupy.empty(tile_bytes, dtype=cupy.uint8) for _ in range(n_tiles)]
d_comp_ptrs = cupy.array([b.data.ptr for b in d_comp_bufs], dtype=cupy.uint64)
d_decomp_ptrs = cupy.array([b.data.ptr for b in d_decomp_bufs], dtype=cupy.uint64)
d_comp_sizes = cupy.array([len(t) for t in raw_tiles], dtype=cupy.uint64)
d_buf_sizes = cupy.full(n_tiles, tile_bytes, dtype=cupy.uint64)
d_actual = cupy.empty(n_tiles, dtype=cupy.uint64)
# Set argtypes for proper struct passing
temp_fn = getattr(lib, get_temp_fn)
temp_fn.restype = ctypes.c_int
temp_size = ctypes.c_size_t(0)
status = temp_fn(
ctypes.c_size_t(n_tiles),
ctypes.c_size_t(tile_bytes),
opts,
ctypes.byref(temp_size),
ctypes.c_size_t(n_tiles * tile_bytes),
)
if status != 0:
return None
ts = max(temp_size.value, 1)
d_temp = cupy.empty(ts, dtype=cupy.uint8)
d_statuses = cupy.zeros(n_tiles, dtype=cupy.int32)
dec_fn = getattr(lib, decomp_fn)
dec_fn.restype = ctypes.c_int
status = dec_fn(
ctypes.c_void_p(d_comp_ptrs.data.ptr),
ctypes.c_void_p(d_comp_sizes.data.ptr),
ctypes.c_void_p(d_buf_sizes.data.ptr),
ctypes.c_void_p(d_actual.data.ptr),
ctypes.c_size_t(n_tiles),
ctypes.c_void_p(d_temp.data.ptr),
ctypes.c_size_t(ts),
ctypes.c_void_p(d_decomp_ptrs.data.ptr),
opts,
ctypes.c_void_p(d_statuses.data.ptr),
ctypes.c_void_p(0), # default stream
)
if status != 0:
return None
cupy.cuda.Device().synchronize()
if int(cupy.any(d_statuses != 0)):
return None
return cupy.concatenate(d_decomp_bufs)
except Exception:
return None
# ---------------------------------------------------------------------------
# nvJPEG batch decode/encode (optional, GPU-accelerated JPEG)
# ---------------------------------------------------------------------------
def _find_nvjpeg_lib():
"""Find and load libnvjpeg.so from the CUDA toolkit. Returns CDLL or None."""
import ctypes
import os
search_paths = [
'libnvjpeg.so', # system LD_LIBRARY_PATH
]
# CUDA toolkit path
cuda_home = os.environ.get('CUDA_HOME', os.environ.get('CUDA_PATH', ''))
if cuda_home:
for subdir in ('lib64', 'lib'):
search_paths.append(os.path.join(cuda_home, subdir, 'libnvjpeg.so'))
# Conda env
conda_prefix = os.environ.get('CONDA_PREFIX', '')
if conda_prefix:
search_paths.append(os.path.join(conda_prefix, 'lib', 'libnvjpeg.so'))
# Common CUDA toolkit install locations
for ver_dir in ('/usr/local/cuda/lib64', '/usr/local/cuda/lib'):
search_paths.append(os.path.join(ver_dir, 'libnvjpeg.so'))
for path in search_paths:
try:
return ctypes.CDLL(path)
except OSError:
continue
return None
_nvjpeg_lib = None
_nvjpeg_checked = False
def _get_nvjpeg():
"""Get the nvJPEG library handle (cached). Returns CDLL or None."""
global _nvjpeg_lib, _nvjpeg_checked
if not _nvjpeg_checked:
_nvjpeg_checked = True
_nvjpeg_lib = _find_nvjpeg_lib()
return _nvjpeg_lib
# nvJPEG status codes
_NVJPEG_STATUS_SUCCESS = 0
# nvJPEG output formats
_NVJPEG_OUTPUT_RGB = 2 # planar RGB
_NVJPEG_OUTPUT_RGBI = 3 # interleaved RGB (R0G0B0 R1G1B1 ...)
_NVJPEG_OUTPUT_UNCHANGED = 5 # native colorspace
# nvJPEG backend
_NVJPEG_BACKEND_DEFAULT = 0
_NVJPEG_BACKEND_GPU_HYBRID = 2
def _try_nvjpeg_batch_decode(compressed_tiles, tile_width, tile_height,
samples):
"""Try batch JPEG decode via nvJPEG. Returns CuPy buffer or None.
Decodes all JPEG tiles on GPU in one batched call. Falls back to None
if nvJPEG is unavailable or any decode fails.
"""
lib = _get_nvjpeg()
if lib is None:
return None
import ctypes
import cupy
try:
n_tiles = len(compressed_tiles)
tile_pixels = tile_width * tile_height
tile_bytes = tile_pixels * samples # JPEG is always uint8
# nvJPEG handle type (opaque pointer)
nvjpeg_handle = ctypes.c_void_p()
# nvjpegCreateSimple(&handle)
create_fn = getattr(lib, 'nvjpegCreateSimple', None)
if create_fn is None:
return None
create_fn.restype = ctypes.c_int
status = create_fn(ctypes.byref(nvjpeg_handle))
if status != _NVJPEG_STATUS_SUCCESS:
return None
try:
# Create JPEG state: nvjpegJpegStateCreate(handle, &state)
jpeg_state = ctypes.c_void_p()
state_create = getattr(lib, 'nvjpegJpegStateCreate')
state_create.restype = ctypes.c_int
status = state_create(nvjpeg_handle, ctypes.byref(jpeg_state))
if status != _NVJPEG_STATUS_SUCCESS:
return None
try:
# Decode tiles one at a time using the simple API.
# nvJPEG batch API requires more setup; the simple decode
# is still GPU-accelerated and avoids complex state management.
output_format = _NVJPEG_OUTPUT_RGBI if samples == 3 else _NVJPEG_OUTPUT_UNCHANGED