FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
gpulz_stage.h
Go to the documentation of this file.
1#pragma once
2
28#include "stage/stage.h"
29#include "fzm_format.h"
30#include "backend/types.h"
31#include <cstdint>
32#include <cstring>
33#include <stdexcept>
34#include <string>
35#include <unordered_map>
36#include <vector>
37
38namespace fz {
39
65class GPULZStage : public Stage {
66public:
68 : is_inverse_(false)
69 , chunk_size_(2048)
70 , word_size_(4)
71 , actual_output_size_(0)
72 , cached_orig_bytes_(0)
73 {}
74
75 ~GPULZStage() override;
76
77 // ── Stage control ──────────────────────────────────────────────────────
78 void setInverse(bool inv) override { is_inverse_ = inv; }
79 bool isInverse() const override { return is_inverse_; }
80
81 bool isGraphCompatible() const override { return !is_inverse_; }
82
83 void setChunkSize(size_t bytes) { chunk_size_ = static_cast<uint32_t>(bytes); }
84 void setWordSize(size_t bytes) { word_size_ = static_cast<uint8_t>(bytes); }
85
98 void setMatchLevel(int level) { match_level_ = static_cast<uint8_t>(level); }
99 int getMatchLevel() const { return static_cast<int>(match_level_); }
100
127 void setSplitMode(bool on) { split_mode_ = on; }
128 bool getSplitMode() const { return split_mode_; }
129
130 size_t getChunkSize() const { return chunk_size_; }
131
144 size_t getRequiredInputAlignment() const override { return 1; }
145 int getWordSize() const { return static_cast<int>(word_size_); }
146 uint32_t getCachedOrigBytes() const { return cached_orig_bytes_; }
147
148 // ── Execution ─────────────────────────────────────────────────────────
150 fz::stream_t stream,
151 MemoryPool* pool,
152 const std::vector<void*>& inputs,
153 const std::vector<void*>& outputs,
154 const std::vector<size_t>& sizes
155 ) override;
156 void postStreamSync(fz::stream_t stream) override;
157
158 // ── Metadata ──────────────────────────────────────────────────────────
159 std::string getName() const override { return "GPULZ"; }
160 size_t getNumInputs() const override {
161 return (is_inverse_ && split_mode_) ? 4 : 1;
162 }
163 size_t getNumOutputs() const override {
164 return (!is_inverse_ && split_mode_) ? 4 : 1;
165 }
166
167 std::vector<std::string> getOutputNames() const override {
168 if (!is_inverse_ && split_mode_)
169 return {"literals", "lengths", "offsets", "meta"};
170 return {"output"};
171 }
172
173 std::vector<size_t> estimateOutputSizes(
174 const std::vector<size_t>& input_sizes
175 ) const override {
176 if (is_inverse_) {
177 // Capacity must cover the padded extent the decode kernel writes,
178 // even though execute() reports the unpadded size afterwards.
179 if (cached_orig_bytes_ > 0)
180 return {static_cast<size_t>(cached_orig_bytes_)};
181 return {input_sizes.empty() ? 0 : input_sizes[0]};
182 }
183 const size_t n_bytes = input_sizes.empty() ? 0 : input_sizes[0];
184 const size_t n_chunks = (n_bytes + chunk_size_ - 1) / chunk_size_;
185 const size_t hdr = 4 + 4 + 8 * n_chunks;
186 // Every bound below is against the PADDED extent, not the input size.
187 // execute() zero-pads a partial tail chunk up to chunk_size_ and encodes
188 // it as a full chunk, so the tail can contribute chunk_size_ bytes of
189 // output from fewer than chunk_size_ bytes of input. Bounding by
190 // `n_bytes` therefore under-reserves by exactly the tail padding, and
191 // the encode writes past the buffer the DAG allocated (E22 in the
192 // benchmarking repo: overruns of 4-40 B observed, silent when this
193 // stage is mid-pipeline).
194 const size_t padded = n_chunks * chunk_size_;
195
196 if (split_mode_) {
197 const size_t block_elems = chunk_size_ / word_size_;
198 const size_t flag_stride = (block_elems + 7) / 8;
199 // literals: every element a literal (or every chunk raw) -> padded.
200 // lengths/offsets: one byte per match, at most one match per element.
201 // meta: header + every chunk's full-width bitmap.
202 return {align4(padded),
203 align4(n_chunks * block_elems),
204 align4(n_chunks * block_elems),
205 align4(hdr + n_chunks * flag_stride)};
206 }
207 // Forward: worst case = padded data (every chunk falls back to raw
208 // storage) + stream header (two uint32_t per chunk).
209 // postStreamSync() rounds the final size up to a 4-byte boundary and
210 // zero-fills the pad; reserve that pad here too (see RREStage).
211 return {align4(padded + hdr)};
212 }
213
214 std::unordered_map<std::string, size_t>
216 size_t getActualOutputSize(int index) const override;
217
229 const std::vector<size_t>& input_sizes
230 ) const override {
231 if (input_sizes.empty()) return 0;
232 const size_t in_bytes = input_sizes[0];
233 const size_t n_chunks = (in_bytes + chunk_size_ - 1) / chunk_size_;
234 const size_t block_elems = chunk_size_ / word_size_;
235 const size_t flag_bytes_max = (block_elems + 7) / 8;
236 if (is_inverse_) {
237 // Split inverse restripes the four ports back into the packed
238 // single-stream form before running the normal decode path.
239 return split_mode_ ? (in_bytes + n_chunks * flag_bytes_max
240 + 4 * n_chunks * sizeof(uint32_t))
241 : 0;
242 }
243 size_t bytes = n_chunks * (static_cast<size_t>(chunk_size_)
244 + flag_bytes_max + 4 * sizeof(uint32_t));
245 if (split_mode_) bytes += n_chunks * 5 * sizeof(uint32_t) + 16;
246 return bytes;
247 }
248
249 uint16_t getStageTypeId() const override {
250 return static_cast<uint16_t>(StageType::GPULZ);
251 }
252
253 uint8_t getOutputDataType(size_t) const override {
254 return static_cast<uint8_t>(DataType::UINT8);
255 }
256 uint8_t getInputDataType(size_t) const override {
257 return static_cast<uint8_t>(DataType::UNKNOWN);
258 }
259
260 // ── Serialization ─────────────────────────────────────────────────────
262 size_t output_index, uint8_t* buf, size_t max_size
263 ) const override {
264 (void)output_index;
265 if (max_size < 14) return 0;
266 std::memcpy(buf, &chunk_size_, sizeof(uint32_t));
267 buf[4] = word_size_;
268 std::memcpy(buf + 5, &cached_orig_bytes_, sizeof(uint32_t));
269 buf[9] = split_mode_ ? 1u : 0u;
270 std::memcpy(buf + 10, &orig_unpadded_bytes_, sizeof(uint32_t));
271 return 14;
272 }
273
274 void deserializeHeader(const uint8_t* buf, size_t size) override {
275 if (size >= 4) std::memcpy(&chunk_size_, buf, sizeof(uint32_t));
276 if (size >= 5) word_size_ = buf[4];
277 if (size >= 9) std::memcpy(&cached_orig_bytes_, buf + 5, sizeof(uint32_t));
278 if (size >= 10) split_mode_ = (buf[9] != 0);
279 if (size >= 14) std::memcpy(&orig_unpadded_bytes_, buf + 10, sizeof(uint32_t));
280 }
281
282 size_t getMaxHeaderSize(size_t) const override { return 14; }
283
284 void saveState() override {
285 saved_chunk_size_ = chunk_size_;
286 saved_word_size_ = word_size_;
287 saved_cached_orig_bytes_ = cached_orig_bytes_;
288 saved_split_mode_ = split_mode_;
289 saved_orig_unpadded_bytes_ = orig_unpadded_bytes_;
290 }
291
292 void restoreState() override {
293 chunk_size_ = saved_chunk_size_;
294 word_size_ = saved_word_size_;
295 cached_orig_bytes_ = saved_cached_orig_bytes_;
296 split_mode_ = saved_split_mode_;
297 orig_unpadded_bytes_ = saved_orig_unpadded_bytes_;
298 }
299
300private:
301 static constexpr size_t align4(size_t n) { return (n + 3) & ~size_t(3); }
302
304 void finishSplitReadback(fz::stream_t stream) const;
305
306 bool is_inverse_;
307 uint32_t chunk_size_;
308 uint32_t saved_chunk_size_ = 0;
309 uint8_t word_size_;
310 uint8_t saved_word_size_ = 0;
311 uint8_t match_level_ = 1;
312 bool split_mode_ = false;
313 bool saved_split_mode_ = false;
314 size_t actual_output_size_;
315 // Split mode: per-port actual sizes, in getOutputNames() order.
316 size_t actual_split_sizes_[4] = {0, 0, 0, 0};
317 uint32_t cached_orig_bytes_ = 0; // chunk-padded extent the codec works on
318 uint32_t saved_cached_orig_bytes_ = 0;
319 // True input size before tail-chunk padding. The inverse decodes the full
320 // padded extent but must *report* this, or a downstream stage that derives
321 // an element count from its input size (LorenzoQuantStage's inverse does
322 // exactly that) inflates its own output past the allocated buffer.
323 uint32_t orig_unpadded_bytes_ = 0;
324 uint32_t saved_orig_unpadded_bytes_ = 0;
325
326 // ── Persistent forward scratch buffers ─────────────────────────────────
327 uint8_t* d_data_scratch_ = nullptr;
328 uint8_t* d_flag_scratch_ = nullptr;
329 uint32_t* d_flag_size_ = nullptr;
330 uint32_t* d_data_size_ = nullptr;
331 uint32_t* d_clean_dev_ = nullptr;
332 uint32_t* d_dst_off_dev_ = nullptr;
333 // Split mode: per-chunk destination offsets into the literals / token
334 // streams, plus a 4-entry device totals array read back in postStreamSync.
335 uint32_t* d_lit_off_dev_ = nullptr;
336 uint32_t* d_tok_off_dev_ = nullptr;
337 uint32_t* d_meta_off_dev_ = nullptr;
338 uint32_t* d_lit_cnt_dev_ = nullptr;
339 uint32_t* d_tok_cnt_dev_ = nullptr;
340 uint32_t* d_totals_dev_ = nullptr;
341 mutable uint8_t* split_out_ptr_[4] = {nullptr, nullptr, nullptr, nullptr};
342 mutable bool split_readback_pending_ = false;
343 mutable bool tail_readback_pending_ = false;
344 mutable fz::stream_t tail_readback_stream_ = nullptr;
345 mutable uint32_t tail_last_index_ = 0;
346 mutable uint8_t* tail_output_ptr_ = nullptr;
347 size_t scratch_capacity_ = 0;
348 MemoryPool* scratch_pool_owner_ = nullptr;
349 bool scratch_from_pool_ = false;
350};
351
352} // namespace fz
Definition gpulz_stage.h:65
uint8_t getInputDataType(size_t) const override
Definition gpulz_stage.h:256
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition gpulz_stage.h:173
std::vector< std::string > getOutputNames() const override
Definition gpulz_stage.h:167
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
void setInverse(bool inv) override
Definition gpulz_stage.h:78
std::string getName() const override
Definition gpulz_stage.h:159
size_t getActualOutputSize(int index) const override
size_t getMaxHeaderSize(size_t) const override
Definition gpulz_stage.h:282
void saveState() override
Definition gpulz_stage.h:284
void postStreamSync(fz::stream_t stream) override
bool isGraphCompatible() const override
Definition gpulz_stage.h:81
size_t serializeHeader(size_t output_index, uint8_t *buf, size_t max_size) const override
Definition gpulz_stage.h:261
size_t estimateScratchBytes(const std::vector< size_t > &input_sizes) const override
Definition gpulz_stage.h:228
void setMatchLevel(int level)
Definition gpulz_stage.h:98
size_t getRequiredInputAlignment() const override
Definition gpulz_stage.h:144
void setSplitMode(bool on)
Definition gpulz_stage.h:127
uint8_t getOutputDataType(size_t) const override
Definition gpulz_stage.h:253
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition gpulz_stage.h:274
uint16_t getStageTypeId() const override
Definition gpulz_stage.h:249
void execute(fz::stream_t stream, MemoryPool *pool, const std::vector< void * > &inputs, const std::vector< void * > &outputs, const std::vector< size_t > &sizes) override
Definition mempool.h:82
Definition stage.h:30
FZM binary file format definitions — structs, enums, and helpers.
Definition algorithms.h:48
@ GPULZ
TODO: describe this stage.
@ UNKNOWN
Byte-transparent stages: skip type checking at finalize()
Base class interface for all compression stages.
Backend-neutral GPU type aliases.