Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

fix uniform_float (#701)

CCYehd4fb1fe75a08
diff --git a/crates/backend-uzu/src/backends/metal/kernel/rng.h b/crates/backend-uzu/src/backends/metal/kernel/rng.h
index bc887762d..adcdd9b6c 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/rng.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/rng.h
@@ -113,6 +113,8 @@ inline uint philox_next(thread PhiloxState* state) {
return state->output[state->state_idx++];
}
+/* Uniform in [2^-24, 1-2^-24]: -log(-log(u)) needs (0,1) — u == 1 gives +inf and wins
+ * every argmax. 24 bits only; the full 32 rounds the top 128 values up to 2^32. */
inline float uniform_float(thread PhiloxState* state) {
- return float(philox_next(state)) * (1.0f / 4294967296.0f); /* (0,1) */
+ return float(metal::max(philox_next(state) >> 8, 1u)) * (1.0f / 16777216.0f);
}
diff --git a/crates/backend-uzu/src/encodable_block/sampling/gumbel.rs b/crates/backend-uzu/src/encodable_block/sampling/gumbel.rs
index cd7fb2096..ce8d64bdb 100644
--- a/crates/backend-uzu/src/encodable_block/sampling/gumbel.rs
+++ b/crates/backend-uzu/src/encodable_block/sampling/gumbel.rs
@@ -46,7 +46,14 @@ fn uniform_float(
philox4x32_round(&mut ctr, &key);
}
- ctr[word as usize] as f32 * (1.0 / 4294967296.0)
+ unit_interval(ctr[word as usize])
+}
+
+/// Uniform in [2^-24, 1-2^-24]; mirrors Metal `uniform_float` in kernel/rng.h. -ln(-ln u)
+/// needs (0,1): u == 1 gives +inf. 24 bits only; the full 32 rounds the top words to 2^32.
+#[inline]
+fn unit_interval(word: u32) -> f32 {
+ ((word >> 8).max(1)) as f32 * (1.0 / 16777216.0)
}
pub fn gumbel_float(
@@ -72,3 +79,7 @@ pub fn revidx(
(thread_offset + block_idx_offset, block_idx_word)
}
+
+#[cfg(test)]
+#[path = "../../../tests/unit/encodable_block/sampling/gumbel_test.rs"]
+mod tests;
diff --git a/crates/backend-uzu/tests/unit/encodable_block/sampling/gumbel_test.rs b/crates/backend-uzu/tests/unit/encodable_block/sampling/gumbel_test.rs
new file mode 100644
index 000000000..affe865de
--- /dev/null
+++ b/crates/backend-uzu/tests/unit/encodable_block/sampling/gumbel_test.rs
@@ -0,0 +1,12 @@
+use proc_macros::uzu_test;
+
+use super::unit_interval;
+
+/// Monotonic in `word`, so the extremes are the only values that could leave (0, 1),
+/// where the gumbel -ln(-ln u) is +inf (wins every argmax) or -inf (loses every one).
+#[uzu_test]
+fn unit_interval_stays_open() {
+ assert_eq!(unit_interval(u32::MAX), 1.0 - f32::powi(2.0, -24));
+ assert_eq!(unit_interval(0), f32::powi(2.0, -24));
+ assert_eq!(unit_interval(u8::MAX as u32), f32::powi(2.0, -24));
+}