FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
clog_stage.h
Go to the documentation of this file.
1#pragma once
2
29
30#include "stage/stage.h"
31#include "fzm_format.h"
32#include "backend/types.h"
33#include <cstdint>
34#include <cstring>
35#include <memory>
36#include <stdexcept>
37#include <string>
38#include <unordered_map>
39#include <vector>
40
41namespace fz {
42
57class CLOGStage : public Stage {
58public:
59 CLOGStage()
60 : is_inverse_(false)
61 , chunk_size_(16384)
62 , word_size_(1)
63 , actual_output_size_(0)
64 , cached_orig_bytes_(0)
65 , d_scratch_(nullptr)
66 , d_sizes_dev_(nullptr)
67 , d_clean_dev_(nullptr)
68 , d_dst_off_dev_(nullptr)
69 , scratch_capacity_(0)
70 {}
71
72 ~CLOGStage() override;
73
74 // ── Stage control ──────────────────────────────────────────────────────
75 void setInverse(bool inv) override { is_inverse_ = inv; }
76 bool isInverse() const override { return is_inverse_; }
77
78 void setChunkSize(size_t bytes) { chunk_size_ = static_cast<uint32_t>(bytes); }
79 void setWordSize(size_t bytes) { word_size_ = static_cast<uint8_t>(bytes); }
80
81 size_t getChunkSize() const { return chunk_size_; }
82 size_t getRequiredInputAlignment() const override { return chunk_size_; }
83 int getWordSize() const { return static_cast<int>(word_size_); }
84 uint32_t getCachedOrigBytes() const { return cached_orig_bytes_; }
85
86 // ── Fusion (chunk-cooperative coder sink) ────────────────────────────────
87 // Any byte-word chunk_size the fusion harness supports fuses — matches the
88 // fused CLOGCoder<ChunkBytes> device op (d_CLOG<uint8_t, ChunkBytes>,
89 // identical to word_size==1 execute()); see chunk_geometry.h's
90 // kSupportedChunkBytes.
91 FusionSpec getFusionSpec() const override {
92 if (is_inverse_ || word_size_ != 1 ||
93 (chunk_size_ != 4096u && chunk_size_ != 8192u && chunk_size_ != 16384u)) return {};
94 return FusionSpec{FusionAccess::SegmentCodec, chunk_size_};
95 }
96 FusedOpDecl getFusedOp() const override {
97 if (!getFusionSpec().fusable()) return {};
98 return FusedOpDecl{FusionStrategy::ChunkCooperative, "CLOGCoder",
99 "fused/chunk_fusion/chunk_fusion.cuh", {}};
100 }
101 void setFusedResult(size_t archive_bytes, size_t orig_bytes) {
102 actual_output_size_ = archive_bytes;
103 cached_orig_bytes_ = static_cast<uint32_t>(orig_bytes);
104 tail_readback_pending_ = false;
105 }
106 void setFusedArchiveResult(size_t archive_bytes, size_t orig_bytes) override {
107 setFusedResult(archive_bytes, orig_bytes);
108 }
109
110 // ── Execution ──────────────────────────────────────────────────────────
112 cudaStream_t stream,
113 MemoryPool* pool,
114 const std::vector<void*>& inputs,
115 const std::vector<void*>& outputs,
116 const std::vector<size_t>& sizes
117 ) override;
118 void postStreamSync(cudaStream_t stream) override;
119
120 // ── Metadata ───────────────────────────────────────────────────────────
121 std::string getName() const override { return "CLOG"; }
122 size_t getNumInputs() const override { return 1; }
123 size_t getNumOutputs() const override { return 1; }
124
125 std::vector<size_t> estimateOutputSizes(
126 const std::vector<size_t>& input_sizes
127 ) const override {
128 if (is_inverse_) {
129 if (cached_orig_bytes_ > 0)
130 return {static_cast<size_t>(cached_orig_bytes_)};
131 return {input_sizes.empty() ? 0 : input_sizes[0]};
132 }
133 // Forward: worst case = original data + stream header.
134 const size_t n_bytes = input_sizes.empty() ? 0 : input_sizes[0];
135 const size_t n_chunks = (n_bytes + chunk_size_ - 1) / chunk_size_;
136 const size_t hdr = 4 + 4 + 4 * n_chunks;
137 // postStreamSync()/getActualOutputSizesByName() always round the final
138 // size up to a 4-byte boundary and zero-fill the pad, even when the
139 // real total isn't already aligned (e.g. a partial final chunk stored
140 // raw at a byte count that isn't a multiple of 4) -- reserve that pad
141 // here too, or the caller's allocation is up to 3 bytes short and the
142 // memset in postStreamSync writes out of bounds.
143 const size_t worst = n_bytes + hdr;
144 return {(worst + 3) & ~size_t(3)};
145 }
146
147 std::unordered_map<std::string, size_t>
149 size_t getActualOutputSize(int index) const override;
150
160 const std::vector<size_t>& input_sizes
161 ) const override {
162 if (is_inverse_ || input_sizes.empty()) return 0;
163 const size_t in_bytes = input_sizes[0];
164 const size_t n_chunks = (in_bytes + chunk_size_ - 1) / chunk_size_;
165 return n_chunks * (static_cast<size_t>(chunk_size_) + 3 * sizeof(uint32_t));
166 }
167
168 uint16_t getStageTypeId() const override {
169 return static_cast<uint16_t>(StageType::CLOG);
170 }
171
172 uint8_t getOutputDataType(size_t) const override {
173 return static_cast<uint8_t>(DataType::UINT8);
174 }
175
176 // ── Serialization ──────────────────────────────────────────────────────
178 size_t output_index, uint8_t* buf, size_t max_size
179 ) const override {
180 (void)output_index;
181 if (max_size < 9) return 0;
182 std::memcpy(buf, &chunk_size_, sizeof(uint32_t));
183 buf[4] = word_size_;
184 std::memcpy(buf + 5, &cached_orig_bytes_, sizeof(uint32_t));
185 return 9;
186 }
187
188 void deserializeHeader(const uint8_t* buf, size_t size) override {
189 if (size >= 4) std::memcpy(&chunk_size_, buf, sizeof(uint32_t));
190 if (size >= 5) word_size_ = buf[4];
191 if (size >= 9) std::memcpy(&cached_orig_bytes_, buf + 5, sizeof(uint32_t));
192 }
193
194 size_t getMaxHeaderSize(size_t) const override { return 9; }
195
196 void saveState() override {
197 saved_chunk_size_ = chunk_size_;
198 saved_word_size_ = word_size_;
199 saved_cached_orig_bytes_ = cached_orig_bytes_;
200 }
201
202 void restoreState() override {
203 chunk_size_ = saved_chunk_size_;
204 word_size_ = saved_word_size_;
205 cached_orig_bytes_ = saved_cached_orig_bytes_;
206 }
207
208private:
209 bool is_inverse_;
210 uint32_t chunk_size_;
211 uint32_t saved_chunk_size_ = 0;
212 uint8_t word_size_;
213 uint8_t saved_word_size_ = 0;
214 size_t actual_output_size_;
215 uint32_t cached_orig_bytes_ = 0;
216 uint32_t saved_cached_orig_bytes_ = 0;
217
218 // ── Persistent forward scratch buffers ───────────────────────────────────
219 uint8_t* d_scratch_;
220 uint32_t* d_sizes_dev_;
221 uint32_t* d_clean_dev_;
222 uint32_t* d_dst_off_dev_;
223 mutable bool tail_readback_pending_ = false;
224 mutable cudaStream_t tail_readback_stream_ = nullptr;
225 mutable uint32_t tail_last_index_ = 0;
226 mutable uint8_t* tail_output_ptr_ = nullptr;
227 size_t scratch_capacity_;
228 MemoryPool* scratch_pool_owner_ = nullptr;
229 bool scratch_from_pool_ = false;
231 std::weak_ptr<const void> scratch_alive_;
232};
233
234} // namespace fz
size_t serializeHeader(size_t output_index, uint8_t *buf, size_t max_size) const override
Definition clog_stage.h:177
void postStreamSync(cudaStream_t stream) override
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition clog_stage.h:188
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition clog_stage.h:125
uint16_t getStageTypeId() const override
Definition clog_stage.h:168
size_t getMaxHeaderSize(size_t) const override
Definition clog_stage.h:194
void setInverse(bool inv) override
Definition clog_stage.h:75
size_t getActualOutputSize(int index) const override
FusionSpec getFusionSpec() const override
Definition clog_stage.h:91
size_t getRequiredInputAlignment() const override
Definition clog_stage.h:82
uint8_t getOutputDataType(size_t) const override
Definition clog_stage.h:172
FusedOpDecl getFusedOp() const override
Definition clog_stage.h:96
size_t estimateScratchBytes(const std::vector< size_t > &input_sizes) const override
Definition clog_stage.h:159
std::string getName() const override
Definition clog_stage.h:121
void execute(cudaStream_t stream, MemoryPool *pool, const std::vector< void * > &inputs, const std::vector< void * > &outputs, const std::vector< size_t > &sizes) override
void setFusedArchiveResult(size_t archive_bytes, size_t orig_bytes) override
Definition clog_stage.h:106
void saveState() override
Definition clog_stage.h:196
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition mempool.h:82
Definition stage.h:31
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
@ CLOG
Per-subchunk leading-zero compression and bit packing (LC framework component).
Definition fzm_format.h:115
Base class interface for all compression stages.
A stage's contribution to a generated fused kernel — the device-op it maps to, where its source lives...
Definition fusion.h:161
A stage's fusion contract. Stages that can participate in a fused kernel override Stage::getFusionSpe...
Definition fusion.h:51
Backend-neutral GPU type aliases.