FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
bitpack_stage.h
Go to the documentation of this file.
1#pragma once
2
43
44#include "stage/stage.h"
45#include "fzm_format.h"
46#include "backend/types.h"
47#include <cstdint>
48#include <cstring>
49#include <stdexcept>
50#include <string>
51#include <type_traits>
52#include <unordered_map>
53#include <vector>
54
55namespace fz {
56
65template<typename T>
66class BitpackStage : public Stage {
68 static_assert(
69 std::is_same_v<T, uint8_t> ||
70 std::is_same_v<T, uint16_t> ||
71 std::is_same_v<T, uint32_t>,
72 "BitpackStage: T must be uint8_t, uint16_t, or uint32_t.");
74
75public:
76 BitpackStage() = default;
77
78 // ── Stage control ──────────────────────────────────────────────────────────
79 void setInverse(bool inv) override { is_inverse_ = inv; }
80 bool isInverse() const override { return is_inverse_; }
81
82 // ── Configuration ──────────────────────────────────────────────────────────
83
95 void setNBits(uint8_t nbits) {
96 if (nbits == 0 || nbits > 8 * sizeof(T) || (nbits & (nbits - 1)) != 0)
97 throw std::invalid_argument(
98 "BitpackStage::setNBits: nbits must be a power of two "
99 "in [1, " + std::to_string(8 * sizeof(T)) + "], got "
100 + std::to_string(nbits));
101 nbits_ = nbits;
102 }
103 uint8_t getNBits() const { return nbits_; }
104
112 void setBase(T base) { base_ = base; }
113 T getBase() const { return base_; }
114
126 void setShift(uint8_t shift) {
127 if (shift >= 8 * sizeof(T))
128 throw std::invalid_argument(
129 "BitpackStage::setShift: shift must be in [0, "
130 + std::to_string(8 * sizeof(T) - 1) + "], got "
131 + std::to_string(shift));
132 shift_ = shift;
133 }
134 uint8_t getShift() const { return shift_; }
135
148 void setAutoDetect(bool enable) { auto_detect_ = enable; }
149 bool isAutoDetect() const { return auto_detect_; }
150
156 void setAutoBase(bool enable) { auto_base_ = enable; }
157 bool isAutoBase() const { return auto_base_; }
158
166 void setAutoShift(bool enable) { auto_shift_ = enable; }
167 bool isAutoShift() const { return auto_shift_; }
168
175 void setAdaptive(bool enable) {
176 auto_base_ = auto_shift_ = auto_detect_ = enable;
177 }
178
179 // ── Execution ──────────────────────────────────────────────────────────────
181 fz::stream_t stream,
182 MemoryPool* pool,
183 const std::vector<void*>& inputs,
184 const std::vector<void*>& outputs,
185 const std::vector<size_t>& sizes
186 ) override;
187
188 // ── Metadata ───────────────────────────────────────────────────────────────
189 std::string getName() const override { return "Bitpack"; }
190 size_t getNumInputs() const override { return 1; }
191 size_t getNumOutputs() const override { return 1; }
192
193 std::vector<size_t> estimateOutputSizes(
194 const std::vector<size_t>& input_sizes
195 ) const override {
196 if (input_sizes.empty()) return {0};
197 if (!is_inverse_) {
198 if (auto_detect_) {
199 // nbits is unknown until execute() scans the data; return worst
200 // case (full-width, no compression) so PREALLOCATE has enough room.
201 return {input_sizes[0]};
202 }
203 // Forward: packed output is ceil(n * nbits / 8) bytes.
204 const size_t n = input_sizes[0] / sizeof(T);
205 return {(n * nbits_ + 7) / 8};
206 } else {
207 // Inverse: worst case — every packed bit expands to a full element.
208 // input_sizes[0] is the packed byte count; max elements = bytes * (8/nbits).
209 const size_t max_elems = (input_sizes[0] * 8 + nbits_ - 1) / nbits_;
210 return {max_elems * sizeof(T)};
211 }
212 }
213
214 std::unordered_map<std::string, size_t>
215 getActualOutputSizesByName() const override {
216 return {{"output", actual_output_size_}};
217 }
218
219 size_t getActualOutputSize(int index) const override {
220 return (index == 0) ? actual_output_size_ : 0;
221 }
222
223 // ── Type system ────────────────────────────────────────────────────────────
224
225 uint16_t getStageTypeId() const override {
226 return static_cast<uint16_t>(StageType::BITPACK);
227 }
228
229 // Packed byte stream has no meaningful element type; opt out of type checking.
230 uint8_t getOutputDataType(size_t /*output_index*/) const override {
231 return static_cast<uint8_t>(DataType::UNKNOWN);
232 }
233 uint8_t getInputDataType(size_t /*input_index*/) const override {
234 return static_cast<uint8_t>(DataType::UNKNOWN);
235 }
236
237 // ── Serialization ──────────────────────────────────────────────────────────
238
240 size_t /*output_index*/, uint8_t* buf, size_t max_size
241 ) const override {
242 if (max_size < 15) return 0;
243 buf[0] = static_cast<uint8_t>(dataTypeOf<T>());
244 buf[1] = nbits_;
245 std::memcpy(buf + 2, &num_elements_, sizeof(uint64_t));
246 buf[10] = shift_;
247 const uint32_t base32 = static_cast<uint32_t>(base_);
248 std::memcpy(buf + 11, &base32, sizeof(uint32_t));
249 return 15;
250 }
251
252 void deserializeHeader(const uint8_t* buf, size_t size) override {
253 // buf[0] (DataType) is used by the factory to pick the right instantiation.
254 // We only need nbits, num_elements, shift, and base here.
255 if (size >= 2) nbits_ = buf[1];
256 if (size >= 10) std::memcpy(&num_elements_, buf + 2, sizeof(uint64_t));
257 // Pre-shift archives stop at 10 bytes; leave shift/base at their defaults.
258 if (size >= 15) {
259 shift_ = buf[10];
260 uint32_t base32 = 0;
261 std::memcpy(&base32, buf + 11, sizeof(uint32_t));
262 base_ = static_cast<T>(base32);
263 }
264 }
265
266 size_t getMaxHeaderSize(size_t /*output_index*/) const override { return 15; }
267
268 // saveState/restoreState: deserializeHeader (called during decompression
269 // setup) overwrites num_elements with the value from the file header.
270 // Save the forward-pass values so they can be restored afterward.
271 void saveState() override {
272 saved_nbits_ = nbits_;
273 saved_num_elements_ = num_elements_;
274 saved_output_size_ = actual_output_size_;
275 saved_shift_ = shift_;
276 saved_base_ = base_;
277 }
278
279 void restoreState() override {
280 nbits_ = saved_nbits_;
281 num_elements_ = saved_num_elements_;
282 actual_output_size_ = saved_output_size_;
283 shift_ = saved_shift_;
284 base_ = saved_base_;
285 }
286
287private:
288 bool is_inverse_ = false;
289 bool auto_detect_ = false;
290 bool auto_base_ = false;
291 bool auto_shift_ = false;
292 uint8_t nbits_ = 8 * sizeof(T); // default: keep all bits (identity)
293 uint8_t shift_ = 0; // low bits dropped before packing
294 T base_ = T(0); // frame-of-reference offset
295 uint64_t num_elements_ = 0; // set by forward execute; used by inverse
296 size_t actual_output_size_ = 0;
297
298 // saveState snapshots
299 uint8_t saved_nbits_ = 8 * sizeof(T);
300 uint8_t saved_shift_ = 0;
301 T saved_base_ = T(0);
302 uint64_t saved_num_elements_ = 0;
303 size_t saved_output_size_ = 0;
304
305 template<typename U>
306 static constexpr DataType dataTypeOf() {
307 if (std::is_same_v<U, uint8_t>) return DataType::UINT8;
308 if (std::is_same_v<U, uint16_t>) return DataType::UINT16;
309 if (std::is_same_v<U, uint32_t>) return DataType::UINT32;
310 return DataType::UINT8; // unreachable
311 }
312};
313
314extern template class BitpackStage<uint8_t>;
315extern template class BitpackStage<uint16_t>;
316extern template class BitpackStage<uint32_t>;
317
318} // namespace fz
Definition bitpack_stage.h:66
void setAdaptive(bool enable)
Definition bitpack_stage.h:175
void setAutoShift(bool enable)
Definition bitpack_stage.h:166
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition bitpack_stage.h:193
void saveState() override
Definition bitpack_stage.h:271
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 bitpack_stage.h:219
uint8_t getInputDataType(size_t) const override
Definition bitpack_stage.h:233
void setShift(uint8_t shift)
Definition bitpack_stage.h:126
std::string getName() const override
Definition bitpack_stage.h:189
void setAutoDetect(bool enable)
Definition bitpack_stage.h:148
uint16_t getStageTypeId() const override
Definition bitpack_stage.h:225
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition bitpack_stage.h:215
size_t getMaxHeaderSize(size_t) const override
Definition bitpack_stage.h:266
void setInverse(bool inv) override
Definition bitpack_stage.h:79
uint8_t getOutputDataType(size_t) const override
Definition bitpack_stage.h:230
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition bitpack_stage.h:252
void setAutoBase(bool enable)
Definition bitpack_stage.h:156
size_t serializeHeader(size_t, uint8_t *buf, size_t max_size) const override
Definition bitpack_stage.h:239
void setNBits(uint8_t nbits)
Definition bitpack_stage.h:95
void setBase(T base)
Definition bitpack_stage.h:112
Definition mempool.h:82
Definition stage.h:31
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
DataType
Element data type identifiers used in buffer and stage descriptors.
Definition fzm_format.h:142
@ UNKNOWN
Byte-transparent stages: skip type checking at finalize().
Definition fzm_format.h:153
Base class interface for all compression stages.
Backend-neutral GPU type aliases.