FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
bitshuffle_stage.h
Go to the documentation of this file.
1#pragma once
2
19#include "stage/stage.h"
20#include "fzm_format.h"
21#include "backend/types.h"
22#include <cstdint>
23#include <cstring>
24#include <stdexcept>
25#include <string>
26#include <unordered_map>
27#include <vector>
28
29namespace fz {
30
43class BitshuffleStage : public Stage {
44public:
46 : is_inverse_(false)
47 , block_size_(16384)
48 , element_width_(4)
49 , actual_output_size_(0)
50 {}
51
52 // ── Stage control ──────────────────────────────────────────────────────
53 void setInverse(bool inv) override { is_inverse_ = inv; }
54
55 // Fixed-length cooperative transform, block-local at the chunk granularity.
56 // The fused Bitshuffle32 op covers only the primary 4-byte / 16384-byte shape.
57 FusionSpec getFusionSpec() const override {
58 if (is_inverse_ || element_width_ != 4 || block_size_ != 16384u) return {};
59 return FusionSpec{FusionAccess::BlockLocal, block_size_};
60 }
61
63 FusedOpDecl getFusedOp() const override {
64 if (!getFusionSpec().fusable()) return {};
65 return FusedOpDecl{FusionStrategy::ChunkCooperative, "Bitshuffle32",
66 "fused/chunk_fusion/chunk_fusion.cuh", {}};
67 }
68 bool isInverse() const override { return is_inverse_; }
69
70 void setBlockSize(size_t bytes) { block_size_ = static_cast<uint32_t>(bytes); }
71 void setElementWidth(size_t bytes){ element_width_ = static_cast<uint8_t>(bytes); }
72
73 size_t getBlockSize() const { return block_size_; }
74 size_t getRequiredInputAlignment() const override { return block_size_; }
75 size_t getElementWidth() const { return element_width_; }
76
77 // ── Execution ──────────────────────────────────────────────────────────
78 void execute(
79 fz::stream_t stream,
80 MemoryPool* pool,
81 const std::vector<void*>& inputs,
82 const std::vector<void*>& outputs,
83 const std::vector<size_t>& sizes
84 ) override;
85
86 // ── Metadata ───────────────────────────────────────────────────────────
87 std::string getName() const override { return "Bitshuffle"; }
88 size_t getNumInputs() const override { return 1; }
89 size_t getNumOutputs() const override { return 1; }
90
91 std::vector<size_t> estimateOutputSizes(
92 const std::vector<size_t>& input_sizes
93 ) const override {
94 // Size-preserving transform.
95 return {input_sizes[0]};
96 }
97
98 std::unordered_map<std::string, size_t>
99 getActualOutputSizesByName() const override {
100 return {{"output", actual_output_size_}};
101 }
102 size_t getActualOutputSize(int index) const override {
103 return (index == 0) ? actual_output_size_ : 0;
104 }
105
106 uint16_t getStageTypeId() const override {
107 return static_cast<uint16_t>(StageType::BITSHUFFLE);
108 }
109
110 uint8_t getOutputDataType(size_t) const override {
111 // Raw byte stream — report as UINT8.
112 return static_cast<uint8_t>(DataType::UINT8);
113 }
114
115 // ── Serialization ──────────────────────────────────────────────────────
116 // Header: [0..3] block_size (uint32_t LE), [4] element_width (uint8_t)
118 size_t output_index, uint8_t* buf, size_t max_size
119 ) const override {
120 (void)output_index;
121 if (max_size < 5) return 0;
122 std::memcpy(buf, &block_size_, sizeof(uint32_t));
123 buf[4] = element_width_;
124 return 5;
125 }
126
127 void deserializeHeader(const uint8_t* buf, size_t size) override {
128 if (size >= 4) std::memcpy(&block_size_, buf, sizeof(uint32_t));
129 if (size >= 5) element_width_ = buf[4];
130 }
131
132 size_t getMaxHeaderSize(size_t) const override { return 5; }
133
134 void saveState() override {
135 saved_block_size_ = block_size_;
136 saved_element_width_ = element_width_;
137 saved_actual_output_size_ = actual_output_size_;
138 }
139
140 void restoreState() override {
141 block_size_ = saved_block_size_;
142 element_width_ = saved_element_width_;
143 actual_output_size_ = saved_actual_output_size_;
144 }
145
146private:
147 bool is_inverse_;
148 uint32_t block_size_;
149 uint32_t saved_block_size_ = 0;
150 uint8_t element_width_;
151 uint8_t saved_element_width_ = 0;
152 size_t actual_output_size_ = 0;
153 size_t saved_actual_output_size_ = 0;
154
155 // Validate config and return N_chunk (elements per chunk).
156 // block_size must be a multiple of 1024*element_width so that butterfly
157 // kernels always have full warps in every __shfl_xor_sync call.
158 size_t validateConfig() const {
159 if (element_width_ != 1 && element_width_ != 2 &&
160 element_width_ != 4 && element_width_ != 8)
161 throw std::invalid_argument(
162 "BitshuffleStage: element_width must be 1, 2, 4, or 8");
163 if (block_size_ == 0 || block_size_ % (1024u * element_width_) != 0)
164 throw std::invalid_argument(
165 "BitshuffleStage: block_size must be a positive multiple of "
166 "1024 * element_width (default 16384 satisfies this for all "
167 "supported element widths)");
168 return block_size_ / element_width_;
169 }
170};
171
172} // namespace fz
Definition bitshuffle_stage.h:43
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition bitshuffle_stage.h:91
size_t getMaxHeaderSize(size_t) const override
Definition bitshuffle_stage.h:132
uint8_t getOutputDataType(size_t) const override
Definition bitshuffle_stage.h:110
FusionSpec getFusionSpec() const override
Definition bitshuffle_stage.h:57
size_t getActualOutputSize(int index) const override
Definition bitshuffle_stage.h:102
std::string getName() const override
Definition bitshuffle_stage.h:87
void setInverse(bool inv) override
Definition bitshuffle_stage.h:53
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
void saveState() override
Definition bitshuffle_stage.h:134
FusedOpDecl getFusedOp() const override
Chunk-cooperative fixed-length op: 32-bit bitshuffle. Stateless (no params).
Definition bitshuffle_stage.h:63
uint16_t getStageTypeId() const override
Definition bitshuffle_stage.h:106
size_t getRequiredInputAlignment() const override
Definition bitshuffle_stage.h:74
size_t serializeHeader(size_t output_index, uint8_t *buf, size_t max_size) const override
Definition bitshuffle_stage.h:117
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition bitshuffle_stage.h:127
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition bitshuffle_stage.h:99
Definition mempool.h:82
Definition stage.h:31
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
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.