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
7
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>
17
18#include <stdexcept>
19#include <string>
20#include <type_traits>
21#include <unordered_map>
22#include <vector>
23
24namespace fz {
25
27struct AdaptiveLorenzoConfig {
31 uint8_t enable_order2;
33 uint8_t reserved[3];
34 uint32_t num_elements;
35
36 AdaptiveLorenzoConfig()
39 num_elements(0) {}
40};
41static_assert(sizeof(AdaptiveLorenzoConfig) <= FZM_STAGE_CONFIG_SIZE,
42 "AdaptiveLorenzoConfig must fit in FZM_STAGE_CONFIG_SIZE");
43
97template<typename T>
98class AdaptiveLorenzoStage : public Stage {
99 static_assert(std::is_integral<T>::value && std::is_signed<T>::value,
100 "AdaptiveLorenzoStage requires a signed integer type");
101public:
102 struct Config {
107 uint32_t coder_block_size = 32;
112 uint32_t blocks_per_tile = 8;
113 bool enable_order2 = true;
114 bool enable_centering = true;
115 Config() = default;
116 };
117
118 AdaptiveLorenzoStage() { validate(); }
119 explicit AdaptiveLorenzoStage(const Config& config) : config_(config) { validate(); }
120
121 void setInverse(bool inv) override { is_inverse_ = inv; }
122 bool isInverse() const override { return is_inverse_; }
123
124 uint32_t getTileSize() const {
125 return config_.coder_block_size * config_.blocks_per_tile;
126 }
127
132 if (!decl.valid() || !decl.additive ||
133 (decl.kind != EncodingOracleKind::PlainFixedRateBitpack &&
134 decl.kind != EncodingOracleKind::AdaptiveFixedRateBitpack) ||
135 decl.input_data_type != static_cast<uint8_t>(getElementDataType()) ||
136 decl.unit_elems != config_.coder_block_size) {
137 return false;
138 }
139 bound_oracle_ = decl;
140 has_bound_oracle_ = true;
141 return true;
142 }
143
144 bool hasBoundEncodingOracle() const { return has_bound_oracle_; }
145 EncodingOracleKind getBoundEncodingOracleKind() const {
146 return has_bound_oracle_ ? bound_oracle_.kind
147 : EncodingOracleKind::PlainFixedRateBitpack;
148 }
149
150 FusionSpec getFusionSpec() const override {
151 if (is_inverse_ || !has_bound_oracle_) return {};
152 return FusionSpec{FusionAccess::TileSelector, getTileSize(),
153 config_.coder_block_size};
154 }
155
156 std::vector<FusedAuxOutputDecl> getFusedAuxOutputs() const override {
157 if (!getFusionSpec().fusable()) return {};
158 return {
160 static_cast<uint8_t>(DataType::UINT8), getTileSize(),
161 2u, 0u},
163 static_cast<uint8_t>(getElementDataType()), getTileSize(),
164 0u, 1u},
165 };
166 }
167
169 fz::stream_t stream,
170 MemoryPool* pool,
171 const std::vector<void*>& inputs,
172 const std::vector<void*>& outputs,
173 const std::vector<size_t>& sizes
174 ) override;
175
176 std::string getName() const override { return "AdaptiveLorenzo"; }
177 size_t getNumInputs() const override { return is_inverse_ ? 3 : 1; }
178 size_t getNumOutputs() const override { return is_inverse_ ? 1 : 3; }
179
180 std::vector<std::string> getOutputNames() const override {
181 return {"output", "modes", "means"};
182 }
183
184 ~AdaptiveLorenzoStage() override { releaseScratch(); }
185
189 void postStreamSync(fz::stream_t stream) override;
190
193 const std::vector<size_t>& input_sizes
194 ) const override;
195
196 std::vector<size_t> estimateOutputSizes(
197 const std::vector<size_t>& input_sizes
198 ) const override {
199 if (input_sizes.empty()) return {0, 0, 0};
200 if (is_inverse_) return {input_sizes[0]};
201 const size_t n = input_sizes[0] / sizeof(T);
202 const size_t tiles = numTiles(n);
203 // modes: 2 bits per tile. means: worst case one per tile — the real
204 // length is trimmed in postStreamSync() once the scan total is known.
205 return {input_sizes[0], (tiles + 3) / 4, tiles * sizeof(T)};
206 }
207
208 void saveState() override { saved_output_sizes_ = actual_output_sizes_; }
209 void restoreState() override {
210 if (!saved_output_sizes_.empty()) actual_output_sizes_ = saved_output_sizes_;
211 }
212
213 std::unordered_map<std::string, size_t>
214 getActualOutputSizesByName() const override {
215 auto names = getOutputNames();
216 std::unordered_map<std::string, size_t> r;
217 for (size_t i = 0; i < names.size() && i < actual_output_sizes_.size(); ++i)
218 r[names[i]] = actual_output_sizes_[i];
219 return r;
220 }
221
222 size_t getActualOutputSize(int index) const override {
223 return (index >= 0 && index < static_cast<int>(actual_output_sizes_.size()))
224 ? actual_output_sizes_[index] : 0;
225 }
226
227 void setFusedSideOutput(int output_index, size_t bytes) override {
228 if (actual_output_sizes_.size() < 3) actual_output_sizes_.resize(3, 0);
229 if (output_index == 1 || output_index == 2)
230 actual_output_sizes_[static_cast<size_t>(output_index)] = bytes;
231 }
232
233 uint16_t getStageTypeId() const override {
234 return static_cast<uint16_t>(StageType::ADAPTIVE_LORENZO);
235 }
236
237 uint8_t getOutputDataType(size_t output_index) const override {
238 // The mode map is a byte stream; the residuals and means are T.
239 return static_cast<uint8_t>(output_index == 1 ? DataType::UINT8
240 : getElementDataType());
241 }
242 uint8_t getInputDataType(size_t input_index) const override {
243 return static_cast<uint8_t>(input_index == 1 ? DataType::UINT8
244 : getElementDataType());
245 }
246
247 size_t serializeHeader(size_t /*output_index*/, uint8_t* buf, size_t max_size) const override {
248 if (max_size < sizeof(AdaptiveLorenzoConfig))
249 throw std::runtime_error("AdaptiveLorenzoStage: header buffer too small");
251 cfg.data_type = getElementDataType();
252 cfg.coder_block_size = static_cast<uint8_t>(config_.coder_block_size);
253 cfg.blocks_per_tile = static_cast<uint8_t>(config_.blocks_per_tile);
254 cfg.enable_order2 = config_.enable_order2 ? 1u : 0u;
255 cfg.enable_centering = config_.enable_centering ? 1u : 0u;
256 cfg.num_elements = static_cast<uint32_t>(num_elements_);
257 std::memcpy(buf, &cfg, sizeof(cfg));
258 return sizeof(cfg);
259 }
260
261 void deserializeHeader(const uint8_t* buf, size_t size) override {
262 if (size < sizeof(AdaptiveLorenzoConfig))
263 throw std::runtime_error("AdaptiveLorenzoStage: header too small");
265 std::memcpy(&cfg, buf, sizeof(cfg));
266 config_.coder_block_size = cfg.coder_block_size;
267 config_.blocks_per_tile = cfg.blocks_per_tile;
268 config_.enable_order2 = (cfg.enable_order2 != 0);
269 config_.enable_centering = (cfg.enable_centering != 0);
270 num_elements_ = cfg.num_elements;
271 validate();
272 }
273
274 size_t getMaxHeaderSize(size_t /*output_index*/) const override {
275 return sizeof(AdaptiveLorenzoConfig);
276 }
277
278private:
279 Config config_;
280 EncodingOracleDecl bound_oracle_;
281 bool has_bound_oracle_ = false;
282 bool is_inverse_ = false;
283 size_t num_elements_ = 0;
284 std::vector<size_t> actual_output_sizes_{0, 0, 0};
285 std::vector<size_t> saved_output_sizes_;
286
287 // Persistent compaction scratch, grown when a larger input is seen. Held
288 // across calls so postStreamSync() can read the scan total after the fact.
289 uint8_t* d_modes_dense_ = nullptr;
290 T* d_means_dense_ = nullptr;
291 uint32_t* d_flags_ = nullptr;
292 uint32_t* d_offsets_ = nullptr;
293 size_t scratch_tiles_ = 0;
294 MemoryPool* scratch_pool_ = nullptr;
295 size_t pending_tiles_ = 0;
296
297 size_t ensureScratch(size_t num_tiles, MemoryPool* pool, fz::stream_t stream);
298 void releaseScratch();
299
300 size_t numTiles(size_t n) const {
301 const size_t t = getTileSize();
302 return (n + t - 1) / t;
303 }
304
305 void validate() const {
306 if (config_.coder_block_size != 32)
307 throw std::invalid_argument(
308 "AdaptiveLorenzoStage: coder_block_size must be 32 (the cost model "
309 "and the per-block warp reduction both assume a 32-element block)");
310 if (config_.blocks_per_tile < 1 || config_.blocks_per_tile > 32)
311 throw std::invalid_argument(
312 "AdaptiveLorenzoStage: blocks_per_tile must be in [1, 32] so the "
313 "tile fits one CUDA block, got "
314 + std::to_string(config_.blocks_per_tile));
315 }
316
317 static DataType getElementDataType() { return fused::dataTypeOf<T>(); }
318};
319
320extern template class AdaptiveLorenzoStage<int16_t>;
321extern template class AdaptiveLorenzoStage<int32_t>;
322
324template<typename T>
326 const T* d_input, T* d_residuals, uint8_t* d_modes, T* d_means,
327 uint32_t* d_flags, size_t n, uint32_t tile_size, bool enable_order2,
328 bool enable_centering, EncodingOracleKind oracle_kind, fz::stream_t stream);
329
333template<typename T>
335 const T* d_residuals, const uint8_t* d_modes, const T* d_means,
336 const uint32_t* d_offsets, T* d_output,
337 size_t n, uint32_t tile_size, fz::stream_t stream);
338
341template<typename T>
343 const uint8_t* d_modes_dense, const T* d_means_dense, const uint32_t* d_offsets,
344 uint8_t* d_modes_packed, T* d_means_compact, size_t num_tiles,
345 fz::stream_t stream);
346
350 const uint8_t* d_modes_packed, uint32_t* d_flags, size_t num_tiles,
351 fz::stream_t stream);
352
353} // namespace fz
Definition adaptive_lorenzo_stage.h:98
uint16_t getStageTypeId() const override
Definition adaptive_lorenzo_stage.h:233
uint8_t getInputDataType(size_t input_index) const override
Definition adaptive_lorenzo_stage.h:242
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:247
size_t getMaxHeaderSize(size_t) const override
Definition adaptive_lorenzo_stage.h:274
std::vector< FusedAuxOutputDecl > getFusedAuxOutputs() const override
Definition adaptive_lorenzo_stage.h:156
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition adaptive_lorenzo_stage.h:214
bool bindDownstreamEncodingOracle(const EncodingOracleDecl &decl) override
Definition adaptive_lorenzo_stage.h:131
void setInverse(bool inv) override
Definition adaptive_lorenzo_stage.h:121
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition adaptive_lorenzo_stage.h:261
std::string getName() const override
Definition adaptive_lorenzo_stage.h:176
uint8_t getOutputDataType(size_t output_index) const override
Definition adaptive_lorenzo_stage.h:237
FusionSpec getFusionSpec() const override
Definition adaptive_lorenzo_stage.h:150
std::vector< std::string > getOutputNames() const override
Definition adaptive_lorenzo_stage.h:180
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:222
void postStreamSync(fz::stream_t stream) override
void saveState() override
Definition adaptive_lorenzo_stage.h:208
void setFusedSideOutput(int output_index, size_t bytes) override
Definition adaptive_lorenzo_stage.h:227
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition adaptive_lorenzo_stage.h:196
Definition mempool.h:82
Definition stage.h:31
Compile-time C++ type -> DataType enum mapping, shared by the fused stages that dispatch on multiple ...
FZM binary file format definitions — structs, enums, and helpers.
Fused Lorenzo predictor and quantizer stage.
Definition dag.h:24
void launchAdaptiveLorenzoFlags(const uint8_t *d_modes_packed, uint32_t *d_flags, size_t num_tiles, fz::stream_t stream)
constexpr size_t FZM_STAGE_CONFIG_SIZE
Per-stage serialized config slot (bytes).
Definition fzm_format.h:65
void launchAdaptiveLorenzoInverse(const T *d_residuals, const uint8_t *d_modes, const T *d_means, const uint32_t *d_offsets, T *d_output, size_t n, uint32_t tile_size, fz::stream_t stream)
@ ADAPTIVE_LORENZO
Per-tile adaptive multi-order Lorenzo + centering (FSZ prediction stage).
Definition fzm_format.h:120
DataType
Element data type identifiers used in buffer and stage descriptors.
Definition fzm_format.h:142
@ CompactedElements
runtime count * sizeof(element)
Definition fusion.h:123
@ FixedBitsPerUnit
ceil(num_units * bits_per_unit / 8)
Definition fusion.h:122
void launchAdaptiveLorenzoCompact(const uint8_t *d_modes_dense, const T *d_means_dense, const uint32_t *d_offsets, uint8_t *d_modes_packed, T *d_means_compact, size_t num_tiles, fz::stream_t stream)
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
Base class interface for all compression stages.
Serialized config stored in FZMBufferEntry.stage_config.
Definition adaptive_lorenzo_stage.h:27
uint8_t reserved[3]
Must be zero.
Definition adaptive_lorenzo_stage.h:33
uint8_t enable_order2
1 if LZ2 is a candidate variant.
Definition adaptive_lorenzo_stage.h:31
uint8_t blocks_per_tile
Coder blocks per adaptation tile.
Definition adaptive_lorenzo_stage.h:30
DataType data_type
Signed integer element type (1B).
Definition adaptive_lorenzo_stage.h:28
uint8_t coder_block_size
Downstream coder's block size (fixed at 32).
Definition adaptive_lorenzo_stage.h:29
uint8_t enable_centering
1 if centering is a candidate variant.
Definition adaptive_lorenzo_stage.h:32
uint32_t num_elements
Element count (for tile-count recovery).
Definition adaptive_lorenzo_stage.h:34
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:51
Backend-neutral GPU type aliases.