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