Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

metallib libcompression lzfse (#843)

uuuvn88b652c9823a
diff --git a/Cargo.lock b/Cargo.lock
index 04740e345..5d79cae01 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7659,7 +7659,6 @@ dependencies = [
"walkdir",
"xgrammar-rs",
"xxhash-rust",
- "zstd",
]
[[package]]
@@ -8531,34 +8530,6 @@ version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
-[[package]]
-name = "zstd"
-version = "0.13.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
-dependencies = [
- "zstd-safe",
-]
-
-[[package]]
-name = "zstd-safe"
-version = "7.2.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
-dependencies = [
- "zstd-sys",
-]
-
-[[package]]
-name = "zstd-sys"
-version = "2.0.16+zstd.1.5.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748"
-dependencies = [
- "cc",
- "pkg-config",
-]
-
[[package]]
name = "zune-core"
version = "0.5.1"
diff --git a/Cargo.toml b/Cargo.toml
index 32ee1d86b..509d635c3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -76,7 +76,6 @@ objc2-foundation = "0.3.2"
objc2-core-foundation = "0.3.2"
objc2-io-kit = { version = "0.3.2", default-features = false, features = ["libc", "ps"] }
obfstr = "0.4.4"
-zstd = "0.13.3"
metal = { package = "mtl-rs", version = "0.2.0" }
block2 = "0.6"
bytemuck = "1.23"
diff --git a/Package.swift b/Package.swift
index 4fc3c3052..2aafc2e97 100644
--- a/Package.swift
+++ b/Package.swift
@@ -26,9 +26,9 @@ let package = Package(
path: "crates/legacy/uzu/bindings/swift/Sources/Uzu",
linkerSettings: [
.linkedLibrary("c++"),
- .linkedFramework("SystemConfiguration"),
+ .linkedLibrary("compression"),
+ .linkedFramework("SystemConfiguration", .when(platforms: [.macOS])),
.linkedFramework("Metal"),
- .linkedFramework("MetalPerformanceShadersGraph"),
.linkedFramework("CoreAudio"),
.linkedFramework("AudioToolbox"),
]
diff --git a/crates/legacy/uzu/bindings/swift/Package.swift b/crates/legacy/uzu/bindings/swift/Package.swift
index 78be49c3b..627299968 100644
--- a/crates/legacy/uzu/bindings/swift/Package.swift
+++ b/crates/legacy/uzu/bindings/swift/Package.swift
@@ -24,9 +24,9 @@ let package = Package(
dependencies: ["uzu", "UzuMetalIOSimulatorStubs"],
linkerSettings: [
.linkedLibrary("c++"),
- .linkedFramework("SystemConfiguration"),
+ .linkedLibrary("compression"),
+ .linkedFramework("SystemConfiguration", .when(platforms: [.macOS])),
.linkedFramework("Metal"),
- .linkedFramework("MetalPerformanceShadersGraph"),
.linkedFramework("CoreAudio"),
.linkedFramework("AudioToolbox"),
]
diff --git a/crates/uzu-engine/Cargo.toml b/crates/uzu-engine/Cargo.toml
index a9c02a1a4..1fca4904c 100644
--- a/crates/uzu-engine/Cargo.toml
+++ b/crates/uzu-engine/Cargo.toml
@@ -24,7 +24,6 @@ metal = [
"dep:objc2",
"dep:obfstr",
"dep:xxhash-rust",
- "dep:zstd",
"dep:metal",
]
@@ -52,7 +51,6 @@ clang-ast = { workspace = true, optional = true }
tempfile = { workspace = true, optional = true }
serde_json = { workspace = true, features = ["unbounded_depth"], optional = true }
xxhash-rust = { workspace = true, features = ["xxh3"], optional = true }
-zstd = { workspace = true, optional = true }
[dependencies]
uzu-engine-macros.workspace = true
@@ -76,7 +74,6 @@ parking_lot.workspace = true
objc2 = { workspace = true, optional = true }
obfstr = { workspace = true, optional = true }
xxhash-rust = { workspace = true, features = ["xxh3"], optional = true }
-zstd = { workspace = true, optional = true }
metal = { workspace = true, optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
diff --git a/crates/uzu-engine/build/main.rs b/crates/uzu-engine/build/main.rs
index 24474505e..ae2ae724f 100644
--- a/crates/uzu-engine/build/main.rs
+++ b/crates/uzu-engine/build/main.rs
@@ -40,10 +40,10 @@ async fn main() -> anyhow::Result<ExitCode> {
println!("cargo::rustc-cfg=grammar");
}
- debug_log!("build script started");
+ let out_dir = PathBuf::from(env::var("OUT_DIR").context("missing OUT_DIR")?);
+ debug_log!("build script started in {}", out_dir.display());
if envs::build_clean() {
- let out_dir = PathBuf::from(env::var("OUT_DIR").context("missing OUT_DIR")?);
if out_dir.exists() {
fs::remove_dir_all(&out_dir).with_context(|| format!("cannot clean {}", out_dir.display()))?;
fs::create_dir_all(&out_dir).with_context(|| format!("cannot recreate {}", out_dir.display()))?;
diff --git a/crates/uzu-engine/build/metal/compiler.rs b/crates/uzu-engine/build/metal/compiler.rs
index bd8105175..9f108a57f 100644
--- a/crates/uzu-engine/build/metal/compiler.rs
+++ b/crates/uzu-engine/build/metal/compiler.rs
@@ -25,7 +25,7 @@ use crate::{
identifiers::KernelPath, kernel::Kernel,
},
debug_log,
- metal::gpu_types::gpu_type_gen,
+ metal::{compression, gpu_types::gpu_type_gen},
};
const MIN_VARIANTS_PER_SHARD: usize = 8;
@@ -225,7 +225,7 @@ impl MetalCompiler {
.iter()
.map(|file| {
if self.metallib_compressed {
- file.with_added_extension("zst")
+ file.with_added_extension("lzfse")
} else {
file.clone()
}
@@ -243,7 +243,7 @@ impl MetalCompiler {
let compressed_file = compressed_file.clone();
tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
let metallib_source = fs::read(metallib_file)?;
- fs::write(compressed_file, zstd::encode_all(metallib_source.as_slice(), 22)?)?;
+ fs::write(compressed_file, compression::compress(metallib_source.as_slice()))?;
Ok(())
})
.await??;
diff --git a/crates/uzu-engine/build/metal/compression.rs b/crates/uzu-engine/build/metal/compression.rs
new file mode 100644
index 000000000..e93a47712
--- /dev/null
+++ b/crates/uzu-engine/build/metal/compression.rs
@@ -0,0 +1,39 @@
+use std::ptr::null_mut;
+
+const COMPRESSION_LZFSE: i32 = 0x801;
+const LZFSE_MAX_OVERHEAD: usize = 12;
+
+#[link(name = "compression")]
+unsafe extern "C" {
+ fn compression_encode_buffer(
+ dst_buffer: *mut u8,
+ dst_size: usize,
+ src_buffer: *const u8,
+ src_size: usize,
+ scratch_buffer: *mut u8,
+ algorithm: i32,
+ ) -> usize;
+}
+
+type SizeHeader = u32;
+
+pub fn compress(src: &[u8]) -> Vec<u8> {
+ let max_output_body_size = src.len() + LZFSE_MAX_OVERHEAD;
+ let mut output = Vec::with_capacity(size_of::<SizeHeader>() + max_output_body_size);
+ output.extend((src.len() as SizeHeader).to_le_bytes());
+
+ unsafe {
+ let output_body_size = compression_encode_buffer(
+ output.as_mut_ptr().byte_add(size_of::<SizeHeader>()),
+ max_output_body_size,
+ src.as_ptr(),
+ src.len(),
+ null_mut(),
+ COMPRESSION_LZFSE,
+ );
+ assert!(output_body_size != 0, "compression failed");
+ output.set_len(size_of::<SizeHeader>() + output_body_size);
+ };
+
+ output
+}
diff --git a/crates/uzu-engine/build/metal/mod.rs b/crates/uzu-engine/build/metal/mod.rs
index 49e25d98d..62e76339c 100644
--- a/crates/uzu-engine/build/metal/mod.rs
+++ b/crates/uzu-engine/build/metal/mod.rs
@@ -1,6 +1,7 @@
mod ast;
mod bindgen;
mod compiler;
+mod compression;
mod enum_path_rewrite;
mod gpu_types;
mod toolchain;
diff --git a/crates/uzu-engine/src/backends/metal/context.rs b/crates/uzu-engine/src/backends/metal/context.rs
index f86583644..99765d566 100644
--- a/crates/uzu-engine/src/backends/metal/context.rs
+++ b/crates/uzu-engine/src/backends/metal/context.rs
@@ -26,6 +26,7 @@ use crate::backends::{
common::{Allocation, AllocationPool, AllocationType, Allocator, Backend, Context, DeviceCapabilities},
metal::{
command_buffer::MetalCommandBufferInitial,
+ decompression,
sparse::{MetalSparseBuffer, MetalSparseHeapPool, MetalSparseMappingOpsBatch},
},
};
@@ -70,8 +71,7 @@ impl MetalContext {
let maybe_uncompressed_data_owned;
let data = if compressed {
- maybe_uncompressed_data_owned = zstd::decode_all(data).map_err(MetalError::CannotDecompressLibrary)?;
-
+ maybe_uncompressed_data_owned = decompression::decompress(data);
&maybe_uncompressed_data_owned
} else {
data
diff --git a/crates/uzu-engine/src/backends/metal/decompression.rs b/crates/uzu-engine/src/backends/metal/decompression.rs
new file mode 100644
index 000000000..41ace14b7
--- /dev/null
+++ b/crates/uzu-engine/src/backends/metal/decompression.rs
@@ -0,0 +1,38 @@
+use std::ptr::null_mut;
+
+const COMPRESSION_LZFSE: i32 = 0x801;
+
+#[link(name = "compression")]
+unsafe extern "C" {
+ fn compression_decode_buffer(
+ dst_buffer: *mut u8,
+ dst_size: usize,
+ src_buffer: *const u8,
+ src_size: usize,
+ scratch_buffer: *mut u8,
+ algorithm: i32,
+ ) -> usize;
+}
+
+type SizeHeader = u32;
+
+pub fn decompress(src: &[u8]) -> Vec<u8> {
+ let (src_header, src_body) = src.split_at(size_of::<SizeHeader>());
+ let size = SizeHeader::from_le_bytes(src_header.try_into().unwrap()) as usize;
+
+ let mut output = Vec::with_capacity(size + 1);
+ unsafe {
+ let decompressed_size = compression_decode_buffer(
+ output.as_mut_ptr(),
+ output.capacity(),
+ src_body.as_ptr(),
+ src_body.len(),
+ null_mut(),
+ COMPRESSION_LZFSE,
+ );
+ assert!(decompressed_size > 0 && decompressed_size == size, "decompression failed");
+ output.set_len(decompressed_size);
+ }
+
+ output
+}
diff --git a/crates/uzu-engine/src/backends/metal/error.rs b/crates/uzu-engine/src/backends/metal/error.rs
index 92ee29ccf..0b508ce03 100644
--- a/crates/uzu-engine/src/backends/metal/error.rs
+++ b/crates/uzu-engine/src/backends/metal/error.rs
@@ -15,8 +15,6 @@ pub enum MetalError {
CannotStartGpuCapture(String),
#[error("Cannot create library: {0}")]
CannotCreateLibrary(String),
- #[error("Cannot decompress library: {0}")]
- CannotDecompressLibrary(#[source] std::io::Error),
#[error("Cannot create command queue")]
CannotCreateCommandQueue,
#[error("Cannot create command Metal 4 queue")]
diff --git a/crates/uzu-engine/src/backends/metal/mod.rs b/crates/uzu-engine/src/backends/metal/mod.rs
index 428aec8e5..65d1610e4 100644
--- a/crates/uzu-engine/src/backends/metal/mod.rs
+++ b/crates/uzu-engine/src/backends/metal/mod.rs
@@ -2,6 +2,7 @@ mod backend;
mod buffer;
mod command_buffer;
mod context;
+mod decompression;
mod dense_buffer;
mod error;
mod kernel;