FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
adaptive_lorenzo_stage.h
Go to the documentation of this file.
1#pragma once
2
8#include "stage/stage.h"
9#include "fzm_format.h"
10#include "backend/types.h"
11#include <algorithm>
12#include <array>
13#include <cstdint>
14#include <cstring>
15#include <stdexcept>
16#include <string>
17#include <type_traits>
18#include <unordered_map>
19#include <vector>
20
21namespace fz {
22
38static_assert(sizeof(AdaptiveLorenzoConfig) <= FZM_STAGE_CONFIG_SIZE,
39 "AdaptiveLorenzoConfig must fit in FZM_STAGE_CONFIG_SIZE");
40
94template<typename T>
96 static_assert(std::is_integral<T>::value && std::is_signed<T>::value,
97 "AdaptiveLorenzoStage requires a signed integer type");
98public:
99 struct Config {
104 uint32_t coder_block_size = 32;
109 uint32_t blocks_per_tile = 8;
110 bool enable_order2 = true;
111 bool enable_centering = true;
112 Config() = default;
113 };
114
115 AdaptiveLorenzoStage() { validate(); }
116 explicit AdaptiveLorenzoStage(const Config& config) : config_(config) { validate(); }
117
118 void setInverse(bool inv) override { is_inverse_ = inv; }
119 bool isInverse() const override { return is_inverse_; }
120
121 uint32_t getTileSize() const {
122 return config_.coder_block_size * config_.blocks_per_tile;
123 }
124
129 if (!decl.valid() || !decl.additive ||
130 (decl.kind != EncodingOracleKind::PlainFixedRateBitpack &&
131 decl.kind != EncodingOracleKind::AdaptiveFixedRateBitpack) ||
132 decl.input_data_type != static_cast<uint8_t>(getElementDataType()) ||
133 decl.unit_elems != config_.coder_block_size) {
134 return false;
135 }
136 bound_oracle_ = decl;
137 has_bound_oracle_ = true;
138 return true;
139 }
140
141 bool hasBoundEncodingOracle() const { return has_bound_oracle_; }
142 EncodingOracleKind getBoundEncodingOracleKind() const {
143 return has_bound_oracle_ ? bound_oracle_.kind
144 : EncodingOracleKind::PlainFixedRateBitpack;
145 }
146
147 FusionSpec getFusionSpec() const override {
148 if (is_inverse_ || !has_bound_oracle_) return {};
149 return FusionSpec{FusionAccess::TileAdaptive, getTileSize(),
150 config_.coder_block_size};
151 }
152
153 std::vector<FusedAuxOutputDecl> getFusedAuxOutputs() const override {
154 if (!getFusionSpec().fusable()) return {};
155 return {
157 static_cast<uint8_t>(DataType::UINT8), getTileSize(),
158 2u, 0u},
160 static_cast<uint8_t>(getElementDataType()), getTileSize(),
161 0u, 1u},
162 };
163 }
164
166 fz::stream_t stream,
167 MemoryPool* pool,
168 const std::vector<void*>& inputs,
169 const std::vector<void*>& outputs,
170 const std::vector<size_t>& sizes
171 ) override;
172
173 std::string getName() const override { return "AdaptiveLorenzo"; }
174 size_t getNumInputs() const override { return is_inverse_ ? 3 : 1; }
175 size_t getNumOutputs() const override { return is_inverse_ ? 1 : 3; }
176
177 std::vector<std::string> getOutputNames() const override {
178 return {"output", "modes", "means"};
179 }
180
181 ~AdaptiveLorenzoStage() override { releaseScratch(); }
182
186 void postStreamSync(fz::stream_t stream) override;
187
190 bool isGraphCompatible() const override { return false; }
191
194 const std::vector<size_t>& input_sizes
195 ) const override;
196
197 std::vector<size_t> estimateOutputSizes(
198 const std::vector<size_t>& input_sizes
199 ) const override {
200 if (input_sizes.empty()) return {0, 0, 0};
201 if (is_inverse_) return {input_sizes[0]};
202 const size_t n = input_sizes[0] / sizeof(T);
203 const size_t tiles = numTiles(n);
204 // modes: 2 bits per tile. means: worst case one per tile — the real
205 // length is trimmed in postStreamSync() once the scan total is known.
206 return {input_sizes[0], (tiles + 3) / 4, tiles * sizeof(T)};
207 }
208
209 void saveState() override { saved_output_sizes_ = actual_output_sizes_; }
210 void restoreState() override {
211 if (!saved_output_sizes_.empty()) actual_output_sizes_ = saved_output_sizes_;
212 }
213
214 std::unordered_map<std::string, size_t>
215 getActualOutputSizesByName() const override {
216 auto names = getOutputNames();
217 std::unordered_map<std::string, size_t> r;
218 for (size_t i = 0; i < names.size() && i < actual_output_sizes_.size(); ++i)
219 r[names[i]] = actual_output_sizes_[i];
220 return r;
221 }
222
223 size_t getActualOutputSize(int index) const override {
224 return (index >= 0 && index < static_cast<int>(actual_output_sizes_.size()))
225 ? actual_output_sizes_[index] : 0;
226 }
227
228 void setFusedSideOutput(int output_index, size_t bytes) override {
229 if (actual_output_sizes_.size() < 3) actual_output_sizes_.resize(3, 0);
230 if (output_index == 1 || output_index == 2)
231 actual_output_sizes_[static_cast<size_t>(output_index)] = bytes;
232 }
233
234 uint16_t getStageTypeId() const override {
235 return static_cast<uint16_t>(StageType::ADAPTIVE_LORENZO);
236 }
237
238 uint8_t getOutputDataType(size_t output_index) const override {
239 // The mode map is a byte stream; the residuals and means are T.
240 return static_cast<uint8_t>(output_index == 1 ? DataType::UINT8
241 : getElementDataType());
242 }
243 uint8_t getInputDataType(size_t input_index) const override {
244 return static_cast<uint8_t>(input_index == 1 ? DataType::UINT8
245 : getElementDataType());
246 }
247
248 size_t serializeHeader(size_t /*output_index*/, uint8_t* buf, size_t max_size) const override {
249 if (max_size < sizeof(AdaptiveLorenzoConfig))
250 throw std::runtime_error("AdaptiveLorenzoStage: header buffer too small");
252 cfg.data_type = getElementDataType();
253 cfg.coder_block_size = static_cast<uint8_t>(config_.coder_block_size);
254 cfg.blocks_per_tile = static_cast<uint8_t>(config_.blocks_per_tile);
255 cfg.enable_order2 = config_.enable_order2 ? 1u : 0u;
256 cfg.enable_centering = config_.enable_centering ? 1u : 0u;
257 cfg.num_elements = static_cast<uint32_t>(num_elements_);
258 std::memcpy(buf, &cfg, sizeof(cfg));
259 return sizeof(cfg);
260 }
261
262 void deserializeHeader(const uint8_t* buf, size_t size) override {
263 if (size < sizeof(AdaptiveLorenzoConfig))
264 throw std::runtime_error("AdaptiveLorenzoStage: header too small");
266 std::memcpy(&cfg, buf, sizeof(cfg));
267 config_.coder_block_size = cfg.coder_block_size;
268 config_.blocks_per_tile = cfg.blocks_per_tile;
269 config_.enable_order2 = (cfg.enable_order2 != 0);
270 config_.enable_centering = (cfg.enable_centering != 0);
271 num_elements_ = cfg.num_elements;
272 validate();
273 }
274
275 size_t getMaxHeaderSize(size_t /*output_index*/) const override {
276 return sizeof(AdaptiveLorenzoConfig);
277 }
278
279private:
280 Config config_;
281 EncodingOracleDecl bound_oracle_;
282 bool has_bound_oracle_ = false;
283 bool is_inverse_ = false;
284 size_t num_elements_ = 0;
285 std::vector<size_t> actual_output_sizes_{0, 0, 0};
286 std::vector<size_t> saved_output_sizes_;
287
288 // Persistent compaction scratch, grown when a larger input is seen. Held
289 // across calls so postStreamSync() can read the scan total after the fact.
290 uint8_t* d_modes_dense_ = nullptr;
291 T* d_means_dense_ = nullptr;
292 uint32_t* d_flags_ = nullptr;
293 uint32_t* d_offsets_ = nullptr;
294 size_t scratch_tiles_ = 0;
295 MemoryPool* scratch_pool_ = nullptr;
296 size_t pending_tiles_ = 0;
297
298 size_t ensureScratch(size_t num_tiles, MemoryPool* pool, fz::stream_t stream);
299 void releaseScratch();
300
301 size_t numTiles(size_t n) const {
302 const size_t t = getTileSize();
303 return (n + t - 1) / t;
304 }
305
306 void validate() const {
307 if (config_.coder_block_size != 32)
308 throw std::invalid_argument(
309 "AdaptiveLorenzoStage: coder_block_size must be 32 (the cost model "
310 "and the per-block warp reduction both assume a 32-element block)");
311 if (config_.blocks_per_tile < 1 || config_.blocks_per_tile > 32)
312 throw std::invalid_argument(
313 "AdaptiveLorenzoStage: blocks_per_tile must be in [1, 32] so the "
314 "tile fits one CUDA block, got "
315 + std::to_string(config_.blocks_per_tile));
316 }
317
318 static DataType getElementDataType() {
319 if (std::is_same<T, int8_t>::value) return DataType::INT8;
320 if (std::is_same<T, int16_t>::value) return DataType::INT16;
321 if (std::is_same<T, int32_t>::value) return DataType::INT32;
322 if (std::is_same<T, int64_t>::value) return DataType::INT64;
323 return DataType::INT32;
324 }
325};
326
327extern template class AdaptiveLorenzoStage<int16_t>;
328extern template class AdaptiveLorenzoStage<int32_t>;
329
331template<typename T>
333 const T* d_input, T* d_residuals, uint8_t* d_modes, T* d_means,
334 uint32_t* d_flags, size_t n, uint32_t tile_size, bool enable_order2,
335 bool enable_centering, EncodingOracleKind oracle_kind, fz::stream_t stream);
336
338template<typename T>
340 const T* d_residuals, const uint8_t* d_modes, const T* d_means, T* d_output,
341 size_t n, uint32_t tile_size, fz::stream_t stream);
342
343} // namespace fz
Definition adaptive_lorenzo_stage.h:95
uint16_t getStageTypeId() const override
Definition adaptive_lorenzo_stage.h:234
uint8_t getInputDataType(size_t input_index) const override
Definition adaptive_lorenzo_stage.h:243
size_t estimateScratchBytes(const std::vector< size_t > &input_sizes) const override
Defined in the .cu — sizing the CUB scan temp needs a CUDA translation unit.
size_t serializeHeader(size_t, uint8_t *buf, size_t max_size) const override
Definition adaptive_lorenzo_stage.h:248
size_t getMaxHeaderSize(size_t) const override
Definition adaptive_lorenzo_stage.h:275
bool isGraphCompatible() const override
Definition adaptive_lorenzo_stage.h:190
std::vector< FusedAuxOutputDecl > getFusedAuxOutputs() const override
Definition adaptive_lorenzo_stage.h:153
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition adaptive_lorenzo_stage.h:215
bool bindDownstreamEncodingOracle(const EncodingOracleDecl &decl) override
Definition adaptive_lorenzo_stage.h:128
void setInverse(bool inv) override
Definition adaptive_lorenzo_stage.h:118
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition adaptive_lorenzo_stage.h:262
std::string getName() const override
Definition adaptive_lorenzo_stage.h:173
uint8_t getOutputDataType(size_t output_index) const override
Definition adaptive_lorenzo_stage.h:238
FusionSpec getFusionSpec() const override
Definition adaptive_lorenzo_stage.h:147
std::vector< std::string > getOutputNames() const override
Definition adaptive_lorenzo_stage.h:177
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
size_t getActualOutputSize(int index) const override
Definition adaptive_lorenzo_stage.h:223
void postStreamSync(fz::stream_t stream) override
void saveState() override
Definition adaptive_lorenzo_stage.h:209
void setFusedSideOutput(int output_index, size_t bytes) override
Definition adaptive_lorenzo_stage.h:228
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition adaptive_lorenzo_stage.h:197
Definition mempool.h:82
Definition stage.h:31
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
constexpr size_t FZM_STAGE_CONFIG_SIZE
Per-stage serialized config slot (bytes)
Definition fzm_format.h:65
@ ADAPTIVE_LORENZO
Per-tile adaptive multi-order Lorenzo + centering (FSZ prediction stage)
DataType
Element data type identifiers used in buffer and stage descriptors.
Definition fzm_format.h:139
@ CompactedElements
runtime count * sizeof(element)
@ FixedBitsPerUnit
ceil(num_units * bits_per_unit / 8)
void launchAdaptiveLorenzoForward(const T *d_input, T *d_residuals, uint8_t *d_modes, T *d_means, uint32_t *d_flags, size_t n, uint32_t tile_size, bool enable_order2, bool enable_centering, EncodingOracleKind oracle_kind, fz::stream_t stream)
Forward: select the best variant per tile and emit its residuals.
EncodingOracleKind
Registered exact encoded-size policies used by an upstream adaptive stage for an algorithmic mode dec...
Definition fusion.h:90
void launchAdaptiveLorenzoInverse(const T *d_residuals, const uint8_t *d_modes, const T *d_means, T *d_output, size_t n, uint32_t tile_size, fz::stream_t stream)
Inverse: replay each tile's recorded variant.
Base class interface for all compression stages.
Serialized config stored in FZMBufferEntry.stage_config.
Definition adaptive_lorenzo_stage.h:24
uint8_t reserved[3]
Must be zero.
Definition adaptive_lorenzo_stage.h:30
uint8_t enable_order2
1 if LZ2 is a candidate variant.
Definition adaptive_lorenzo_stage.h:28
uint8_t blocks_per_tile
Coder blocks per adaptation tile.
Definition adaptive_lorenzo_stage.h:27
DataType data_type
Signed integer element type (1B).
Definition adaptive_lorenzo_stage.h:25
uint8_t coder_block_size
Downstream coder's block size (fixed at 32).
Definition adaptive_lorenzo_stage.h:26
uint8_t enable_centering
1 if centering is a candidate variant.
Definition adaptive_lorenzo_stage.h:29
uint32_t num_elements
Element count (for tile-count recovery).
Definition adaptive_lorenzo_stage.h:31
Host-side declaration of a local, exact encoded-size oracle.
Definition fusion.h:104
uint8_t input_data_type
DataType value; 0xFF = unknown.
Definition fusion.h:109
Definition fusion.h:131
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.