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