-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcompression_scheme.rs
More file actions
316 lines (275 loc) · 11.7 KB
/
compression_scheme.rs
File metadata and controls
316 lines (275 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::borrow::Cow;
use std::io::{Cursor, Read, Write, copy};
use std::str::FromStr;
use std::time::Instant;
use anyhow::anyhow;
use lz4_flex::frame::{FrameDecoder, FrameEncoder};
use strum::{Display, EnumString};
use crate::byte_grouping::BG4Predictor;
use crate::byte_grouping::bg4::{bg4_regroup, bg4_split};
use crate::error::{CasObjectError, Result};
pub static mut BG4_SPLIT_RUNTIME: f64 = 0.;
pub static mut BG4_REGROUP_RUNTIME: f64 = 0.;
pub static mut BG4_LZ4_COMPRESS_RUNTIME: f64 = 0.;
pub static mut BG4_LZ4_DECOMPRESS_RUNTIME: f64 = 0.;
/// Compression schemes for xorb data.
/// Dis-allow the value of ascii capital letters as valid CompressionScheme, 65-90
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Display, EnumString)]
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
pub enum CompressionScheme {
#[default]
#[strum(serialize = "none")]
None = 0,
#[strum(serialize = "lz4")]
LZ4 = 1,
#[strum(serialize = "bg4-lz4", serialize = "bg4_lz4", serialize = "bg4lz4")]
ByteGrouping4LZ4 = 2, // 4 byte groups
}
pub const NUM_COMPRESSION_SCHEMES: usize = 3;
impl CompressionScheme {
/// Parses an optional compression scheme from a string.
/// Returns None for empty/blank strings (meaning auto-detect).
/// Returns Some(scheme) for valid scheme names.
/// Returns None and logs a warning for invalid scheme names.
pub fn parse_optional(s: &str) -> Option<Self> {
let trimmed = s.trim();
if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("auto") {
return None;
}
match Self::from_str(trimmed) {
Ok(scheme) => Some(scheme),
Err(_) => {
tracing::warn!(
"Invalid compression scheme '{}'; using auto-detection. Valid values: none, lz4, bg4-lz4",
trimmed
);
None
},
}
}
}
impl From<&CompressionScheme> for &'static str {
fn from(value: &CompressionScheme) -> Self {
match value {
CompressionScheme::None => "none",
CompressionScheme::LZ4 => "lz4",
CompressionScheme::ByteGrouping4LZ4 => "bg4-lz4",
}
}
}
impl From<CompressionScheme> for &'static str {
fn from(value: CompressionScheme) -> Self {
From::from(&value)
}
}
impl TryFrom<u8> for CompressionScheme {
type Error = CasObjectError;
fn try_from(value: u8) -> Result<Self> {
match value {
0 => Ok(CompressionScheme::None),
1 => Ok(CompressionScheme::LZ4),
2 => Ok(CompressionScheme::ByteGrouping4LZ4),
_ => Err(CasObjectError::FormatError(anyhow!("cannot convert value {value} to CompressionScheme"))),
}
}
}
impl CompressionScheme {
pub fn compress_from_slice<'a>(&self, data: &'a [u8]) -> Result<Cow<'a, [u8]>> {
Ok(match self {
CompressionScheme::None => data.into(),
CompressionScheme::LZ4 => lz4_compress_from_slice(data).map(Cow::from)?,
CompressionScheme::ByteGrouping4LZ4 => bg4_lz4_compress_from_slice(data).map(Cow::from)?,
})
}
pub fn decompress_from_slice<'a>(&self, data: &'a [u8]) -> Result<Cow<'a, [u8]>> {
Ok(match self {
CompressionScheme::None => data.into(),
CompressionScheme::LZ4 => lz4_decompress_from_slice(data).map(Cow::from)?,
CompressionScheme::ByteGrouping4LZ4 => bg4_lz4_decompress_from_slice(data).map(Cow::from)?,
})
}
pub fn decompress_from_reader<R: Read, W: Write>(&self, reader: &mut R, writer: &mut W) -> Result<u64> {
Ok(match self {
CompressionScheme::None => copy(reader, writer)?,
CompressionScheme::LZ4 => lz4_decompress_from_reader(reader, writer)?,
CompressionScheme::ByteGrouping4LZ4 => bg4_lz4_decompress_from_reader(reader, writer)?,
})
}
/// Chooses the compression scheme based on a KL-divergence heuristic.
pub fn choose_from_data(data: &[u8]) -> Self {
let mut bg4_predictor = BG4Predictor::default();
bg4_predictor.add_data(0, data);
if bg4_predictor.bg4_recommended() {
CompressionScheme::ByteGrouping4LZ4
} else {
CompressionScheme::LZ4
}
}
}
pub fn lz4_compress_from_slice(data: &[u8]) -> Result<Vec<u8>> {
let mut enc = FrameEncoder::new(Vec::new());
enc.write_all(data)?;
Ok(enc.finish()?)
}
pub fn lz4_decompress_from_slice(data: &[u8]) -> Result<Vec<u8>> {
let mut dest = vec![];
lz4_decompress_from_reader(&mut Cursor::new(data), &mut dest)?;
Ok(dest)
}
fn lz4_decompress_from_reader<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> Result<u64> {
let mut dec = FrameDecoder::new(reader);
Ok(copy(&mut dec, writer)?)
}
pub fn bg4_lz4_compress_from_slice(data: &[u8]) -> Result<Vec<u8>> {
let s = Instant::now();
let groups = bg4_split(data);
unsafe {
BG4_SPLIT_RUNTIME += s.elapsed().as_secs_f64();
}
let s = Instant::now();
let mut dest = vec![];
let mut enc = FrameEncoder::new(&mut dest);
enc.write_all(&groups)?;
enc.finish()?;
unsafe {
BG4_LZ4_COMPRESS_RUNTIME += s.elapsed().as_secs_f64();
}
Ok(dest)
}
pub fn bg4_lz4_decompress_from_slice(data: &[u8]) -> Result<Vec<u8>> {
let mut dest = vec![];
bg4_lz4_decompress_from_reader(&mut Cursor::new(data), &mut dest)?;
Ok(dest)
}
fn bg4_lz4_decompress_from_reader<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> Result<u64> {
let s = Instant::now();
let mut g = vec![];
FrameDecoder::new(reader).read_to_end(&mut g)?;
unsafe {
BG4_LZ4_DECOMPRESS_RUNTIME += s.elapsed().as_secs_f64();
}
let s = Instant::now();
let regrouped = bg4_regroup(&g);
unsafe {
BG4_REGROUP_RUNTIME += s.elapsed().as_secs_f64();
}
writer.write_all(®rouped)?;
Ok(regrouped.len() as u64)
}
#[cfg(test)]
mod tests {
use std::mem::size_of;
use std::str::FromStr;
use half::prelude::*;
use rand::Rng;
use super::*;
#[test]
fn test_to_str() {
assert_eq!(Into::<&str>::into(CompressionScheme::None), "none");
assert_eq!(Into::<&str>::into(CompressionScheme::LZ4), "lz4");
assert_eq!(Into::<&str>::into(CompressionScheme::ByteGrouping4LZ4), "bg4-lz4");
}
#[test]
fn test_from_str() {
assert_eq!(CompressionScheme::from_str("none"), Ok(CompressionScheme::None));
assert_eq!(CompressionScheme::from_str("lz4"), Ok(CompressionScheme::LZ4));
assert_eq!(CompressionScheme::from_str("LZ4"), Ok(CompressionScheme::LZ4));
assert_eq!(CompressionScheme::from_str("bg4-lz4"), Ok(CompressionScheme::ByteGrouping4LZ4));
assert_eq!(CompressionScheme::from_str("bg4_lz4"), Ok(CompressionScheme::ByteGrouping4LZ4));
assert_eq!(CompressionScheme::from_str("BG4-LZ4"), Ok(CompressionScheme::ByteGrouping4LZ4));
assert!(CompressionScheme::from_str("invalid").is_err());
}
#[test]
fn test_parse_optional() {
assert_eq!(CompressionScheme::parse_optional(""), None);
assert_eq!(CompressionScheme::parse_optional("auto"), None);
assert_eq!(CompressionScheme::parse_optional("AUTO"), None);
assert_eq!(CompressionScheme::parse_optional(" "), None);
assert_eq!(CompressionScheme::parse_optional("lz4"), Some(CompressionScheme::LZ4));
assert_eq!(CompressionScheme::parse_optional("none"), Some(CompressionScheme::None));
assert_eq!(CompressionScheme::parse_optional("bg4-lz4"), Some(CompressionScheme::ByteGrouping4LZ4));
assert_eq!(CompressionScheme::parse_optional("invalid"), None);
}
#[test]
fn test_from_u8() {
assert_eq!(CompressionScheme::try_from(0u8), Ok(CompressionScheme::None));
assert_eq!(CompressionScheme::try_from(1u8), Ok(CompressionScheme::LZ4));
assert_eq!(CompressionScheme::try_from(2u8), Ok(CompressionScheme::ByteGrouping4LZ4));
assert!(CompressionScheme::try_from(3u8).is_err());
}
#[test]
fn test_bg4_lz4() {
let mut rng = rand::rng();
for i in 0..4 {
let n = 64 * 1024 + i * 23;
let all_zeros = vec![0u8; n];
let all_ones = vec![1u8; n];
let all_0xff = vec![0xFF; n];
let random_u8s: Vec<_> = (0..n).map(|_| rng.random_range(0..255)).collect();
let random_f32s_ng1_1: Vec<_> = (0..n / size_of::<f32>())
.map(|_| rng.random_range(-1.0f32..=1.0))
.flat_map(|f| f.to_le_bytes())
.collect();
let random_f32s_0_2: Vec<_> = (0..n / size_of::<f32>())
.map(|_| rng.random_range(0f32..=2.0))
.flat_map(|f| f.to_le_bytes())
.collect();
let random_f64s_ng1_1: Vec<_> = (0..n / size_of::<f64>())
.map(|_| rng.random_range(-1.0f64..=1.0))
.flat_map(|f| f.to_le_bytes())
.collect();
let random_f64s_0_2: Vec<_> = (0..n / size_of::<f64>())
.map(|_| rng.random_range(0f64..=2.0))
.flat_map(|f| f.to_le_bytes())
.collect();
// f16, a.k.a binary16 format: sign (1 bit), exponent (5 bit), mantissa (10 bit)
let random_f16s_ng1_1: Vec<_> = (0..n / size_of::<f16>())
.map(|_| f16::from_f32(rng.random_range(-1.0f32..=1.0)))
.flat_map(|f| f.to_le_bytes())
.collect();
let random_f16s_0_2: Vec<_> = (0..n / size_of::<f16>())
.map(|_| f16::from_f32(rng.random_range(0f32..=2.0)))
.flat_map(|f| f.to_le_bytes())
.collect();
// bf16 format: sign (1 bit), exponent (8 bit), mantissa (7 bit)
let random_bf16s_ng1_1: Vec<_> = (0..n / size_of::<bf16>())
.map(|_| bf16::from_f32(rng.random_range(-1.0f32..=1.0)))
.flat_map(|f| f.to_le_bytes())
.collect();
let random_bf16s_0_2: Vec<_> = (0..n / size_of::<bf16>())
.map(|_| bf16::from_f32(rng.random_range(0f32..=2.0)))
.flat_map(|f| f.to_le_bytes())
.collect();
let dataset = [
all_zeros, // 231.58, 231.58
all_ones, // 231.58, 231.58
all_0xff, // 231.58, 231.58
random_u8s, // 1.00, 1.00
random_f32s_ng1_1, // 1.08, 1.00
random_f32s_0_2, // 1.15, 1.00
random_f64s_ng1_1, // 1.00, 1.00
random_f64s_0_2, // 1.00, 1.00
random_f16s_ng1_1, // 1.00, 1.00
random_f16s_0_2, // 1.00, 1.00
random_bf16s_ng1_1, // 1.18, 1.00
random_bf16s_0_2, // 1.37, 1.00
];
for data in dataset {
let bg4_lz4_compressed = bg4_lz4_compress_from_slice(&data).unwrap();
let bg4_lz4_uncompressed = bg4_lz4_decompress_from_slice(&bg4_lz4_compressed).unwrap();
assert_eq!(data.len(), bg4_lz4_uncompressed.len());
assert_eq!(data, bg4_lz4_uncompressed);
let lz4_compressed = lz4_compress_from_slice(&data).unwrap();
let lz4_uncompressed = lz4_decompress_from_slice(&lz4_compressed).unwrap();
assert_eq!(data, lz4_uncompressed);
let compression_scheme_predictor = CompressionScheme::choose_from_data(&data);
println!(
"Compression ratio: {:.2}, {:.2} (KL predicted = {compression_scheme_predictor:?}",
data.len() as f32 / bg4_lz4_compressed.len() as f32,
data.len() as f32 / lz4_compressed.len() as f32
);
}
}
}
}