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