use crate::dtype::DType;
use crate::error::{Error, Result};
#[cfg(feature = "fp8")]
use crate::ops::BinaryOps;
use crate::ops::RandomOps;
#[cfg(feature = "fp8")]
use crate::ops::TypeConversionOps;
#[cfg(feature = "fp8")]
use crate::ops::UtilityOps;
use crate::random::next_call_seed;
use crate::runtime::cuda::kernels::{
launch_bernoulli, launch_beta_dist, launch_binomial, launch_chi_squared, launch_exponential,
launch_f_distribution, launch_gamma_dist, launch_laplace, launch_multinomial_with_replacement,
launch_multinomial_without_replacement, launch_poisson, launch_rand, launch_randint,
launch_randn, launch_student_t,
};
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::runtime::validate_rand_into;
use crate::tensor::Tensor;
const NATIVE_FLOAT_DTYPES: [DType; 4] = [DType::F32, DType::F64, DType::F16, DType::BF16];
impl RandomOps<CudaRuntime> for CudaClient {
fn rand(&self, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = self.rand(shape, DType::F32)?;
let clamped = clamp_below_one_for_narrowing(self, &f32_result, dtype)?;
return self.cast(&clamped, dtype);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType { dtype, op: "rand" });
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
self.rand_into(&out)?;
Ok(out)
}
fn rand_into(&self, out: &Tensor<CudaRuntime>) -> Result<()> {
if validate_rand_into(out, "rand_into")? {
return Ok(());
}
rand_into_impl(self, out, None, "rand_into")
}
fn rand_seeded(&self, shape: &[usize], dtype: DType, seed: u64) -> Result<Tensor<CudaRuntime>> {
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = self.rand_seeded(shape, DType::F32, seed)?;
let clamped = clamp_below_one_for_narrowing(self, &f32_result, dtype)?;
return self.cast(&clamped, dtype);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType {
dtype,
op: "rand_seeded",
});
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
self.rand_seeded_into(&out, seed)?;
Ok(out)
}
fn rand_seeded_into(&self, out: &Tensor<CudaRuntime>, seed: u64) -> Result<()> {
if validate_rand_into(out, "rand_seeded_into")? {
return Ok(());
}
rand_into_impl(self, out, Some(seed), "rand_seeded_into")
}
fn randn(&self, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = self.randn(shape, DType::F32)?;
return self.cast(&f32_result, dtype);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType { dtype, op: "randn" });
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
self.randn_into(&out)?;
Ok(out)
}
fn randn_into(&self, out: &Tensor<CudaRuntime>) -> Result<()> {
if validate_rand_into(out, "randn_into")? {
return Ok(());
}
randn_into_impl(self, out, None, "randn_into")
}
fn randn_seeded(
&self,
shape: &[usize],
dtype: DType,
seed: u64,
) -> Result<Tensor<CudaRuntime>> {
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = self.randn_seeded(shape, DType::F32, seed)?;
return self.cast(&f32_result, dtype);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType {
dtype,
op: "randn_seeded",
});
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
self.randn_seeded_into(&out, seed)?;
Ok(out)
}
fn randn_seeded_into(&self, out: &Tensor<CudaRuntime>, seed: u64) -> Result<()> {
if validate_rand_into(out, "randn_seeded_into")? {
return Ok(());
}
randn_into_impl(self, out, Some(seed), "randn_seeded_into")
}
fn randint(
&self,
low: i64,
high: i64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !dtype.is_int() {
return Err(Error::UnsupportedDType {
dtype,
op: "randint",
});
}
if high <= low {
return Err(Error::InvalidArgument {
arg: "high",
reason: format!(
"randint requires high > low, got low={}, high={}",
low, high
),
});
}
if dtype.is_unsigned_int() && low < 0 {
return Err(Error::InvalidArgument {
arg: "low",
reason: format!(
"randint with unsigned dtype {} requires low >= 0, got low={}",
dtype, low
),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
let range = high - low;
unsafe {
launch_randint(
&self.context,
&self.stream,
self.device.index,
dtype,
low,
range,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn multinomial(
&self,
probs: &Tensor<CudaRuntime>,
num_samples: usize,
replacement: bool,
) -> Result<Tensor<CudaRuntime>> {
let dtype = probs.dtype();
if !dtype.is_float() {
return Err(Error::UnsupportedDType {
dtype,
op: "multinomial",
});
}
if num_samples == 0 {
return Err(Error::InvalidArgument {
arg: "num_samples",
reason: "num_samples must be > 0".to_string(),
});
}
let shape = probs.shape();
if shape.is_empty() {
return Err(Error::InvalidArgument {
arg: "probs",
reason: "probs tensor must have at least 1 dimension".to_string(),
});
}
let num_categories = *shape.last().unwrap();
if num_categories == 0 {
return Err(Error::InvalidArgument {
arg: "probs",
reason: "probs tensor must have at least 1 category (last dim > 0)".to_string(),
});
}
if !replacement && num_samples > num_categories {
return Err(Error::InvalidArgument {
arg: "num_samples",
reason: format!(
"cannot sample {} items without replacement from {} categories",
num_samples, num_categories
),
});
}
let num_distributions: usize = shape[..shape.len() - 1].iter().product();
let probs = crate::runtime::ensure_contiguous(probs)?;
let mut out_shape = shape[..shape.len() - 1].to_vec();
out_shape.push(num_samples);
if out_shape.is_empty() {
out_shape.push(num_samples);
}
let out = Tensor::<CudaRuntime>::empty(&out_shape, DType::I64, &self.device)?;
if out.numel() == 0 {
return Ok(out);
}
let seed = next_call_seed();
unsafe {
if replacement {
launch_multinomial_with_replacement(
&self.context,
&self.stream,
self.device.index,
dtype,
probs.ptr(),
out.ptr(),
seed,
num_distributions,
num_categories,
num_samples,
)?;
} else {
launch_multinomial_without_replacement(
&self.context,
&self.stream,
self.device.index,
dtype,
probs.ptr(),
out.ptr(),
seed,
num_distributions,
num_categories,
num_samples,
)?;
}
}
Ok(out)
}
fn bernoulli(&self, p: f64, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "bernoulli",
});
}
if !(0.0..=1.0).contains(&p) {
return Err(Error::InvalidArgument {
arg: "p",
reason: format!("bernoulli requires p in [0, 1], got {}", p),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_bernoulli(
&self.context,
&self.stream,
self.device.index,
dtype,
p,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn beta(
&self,
alpha: f64,
beta: f64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType { dtype, op: "beta" });
}
if alpha <= 0.0 {
return Err(Error::InvalidArgument {
arg: "alpha",
reason: format!("beta requires alpha > 0, got {}", alpha),
});
}
if beta <= 0.0 {
return Err(Error::InvalidArgument {
arg: "beta",
reason: format!("beta requires beta > 0, got {}", beta),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_beta_dist(
&self.context,
&self.stream,
self.device.index,
dtype,
alpha,
beta,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn gamma(
&self,
shape_param: f64,
scale: f64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType { dtype, op: "gamma" });
}
if shape_param <= 0.0 {
return Err(Error::InvalidArgument {
arg: "shape_param",
reason: format!("gamma requires shape_param > 0, got {}", shape_param),
});
}
if scale <= 0.0 {
return Err(Error::InvalidArgument {
arg: "scale",
reason: format!("gamma requires scale > 0, got {}", scale),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_gamma_dist(
&self.context,
&self.stream,
self.device.index,
dtype,
shape_param,
scale,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn exponential(&self, rate: f64, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "exponential",
});
}
if rate <= 0.0 {
return Err(Error::InvalidArgument {
arg: "rate",
reason: format!("exponential requires rate > 0, got {}", rate),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_exponential(
&self.context,
&self.stream,
self.device.index,
dtype,
rate,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn poisson(&self, lambda: f64, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "poisson",
});
}
if lambda <= 0.0 {
return Err(Error::InvalidArgument {
arg: "lambda",
reason: format!("poisson requires lambda > 0, got {}", lambda),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_poisson(
&self.context,
&self.stream,
self.device.index,
dtype,
lambda,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn binomial(
&self,
n: u64,
p: f64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "binomial",
});
}
if n == 0 {
return Err(Error::InvalidArgument {
arg: "n",
reason: "binomial requires n > 0".to_string(),
});
}
if !(0.0..=1.0).contains(&p) {
return Err(Error::InvalidArgument {
arg: "p",
reason: format!("binomial requires p in [0, 1], got {}", p),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_binomial(
&self.context,
&self.stream,
self.device.index,
dtype,
n,
p,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn laplace(
&self,
loc: f64,
scale: f64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "laplace",
});
}
if scale <= 0.0 {
return Err(Error::InvalidArgument {
arg: "scale",
reason: format!("laplace requires scale > 0, got {}", scale),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_laplace(
&self.context,
&self.stream,
self.device.index,
dtype,
loc,
scale,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn chi_squared(&self, df: f64, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "chi_squared",
});
}
if df <= 0.0 {
return Err(Error::InvalidArgument {
arg: "df",
reason: format!("chi_squared requires df > 0, got {}", df),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_chi_squared(
&self.context,
&self.stream,
self.device.index,
dtype,
df,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn student_t(&self, df: f64, shape: &[usize], dtype: DType) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "student_t",
});
}
if df <= 0.0 {
return Err(Error::InvalidArgument {
arg: "df",
reason: format!("student_t requires df > 0, got {}", df),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_student_t(
&self.context,
&self.stream,
self.device.index,
dtype,
df,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn f_distribution(
&self,
df1: f64,
df2: f64,
shape: &[usize],
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
if !matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) {
return Err(Error::UnsupportedDType {
dtype,
op: "f_distribution",
});
}
if df1 <= 0.0 {
return Err(Error::InvalidArgument {
arg: "df1",
reason: format!("f_distribution requires df1 > 0, got {}", df1),
});
}
if df2 <= 0.0 {
return Err(Error::InvalidArgument {
arg: "df2",
reason: format!("f_distribution requires df2 > 0, got {}", df2),
});
}
let numel: usize = shape.iter().product();
if numel == 0 {
return Ok(Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?);
}
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
let seed = next_call_seed();
unsafe {
launch_f_distribution(
&self.context,
&self.stream,
self.device.index,
dtype,
df1,
df2,
seed,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn randperm(&self, n: usize) -> Result<Tensor<CudaRuntime>> {
crate::ops::impl_generic::randperm_impl(self, n)
}
}
fn rand_into_impl(
client: &CudaClient,
out: &Tensor<CudaRuntime>,
seed: Option<u64>,
op_name: &'static str,
) -> Result<()> {
let dtype = out.dtype();
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = match seed {
Some(seed) => client.rand_seeded(out.shape(), DType::F32, seed)?,
None => client.rand(out.shape(), DType::F32)?,
};
let clamped = clamp_below_one_for_narrowing(client, &f32_result, dtype)?;
let casted = client.cast(&clamped, dtype)?;
return client.copy_into(out, &casted);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType { dtype, op: op_name });
}
let numel = out.numel();
let seed = seed.unwrap_or_else(next_call_seed);
unsafe {
launch_rand(
&client.context,
&client.stream,
client.device.index,
dtype,
seed,
out.ptr(),
numel,
)?;
}
Ok(())
}
fn randn_into_impl(
client: &CudaClient,
out: &Tensor<CudaRuntime>,
seed: Option<u64>,
op_name: &'static str,
) -> Result<()> {
let dtype = out.dtype();
#[cfg(feature = "fp8")]
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
let f32_result = match seed {
Some(seed) => client.randn_seeded(out.shape(), DType::F32, seed)?,
None => client.randn(out.shape(), DType::F32)?,
};
let casted = client.cast(&f32_result, dtype)?;
return client.copy_into(out, &casted);
}
if !NATIVE_FLOAT_DTYPES.contains(&dtype) {
return Err(Error::UnsupportedDType { dtype, op: op_name });
}
let numel = out.numel();
let seed = seed.unwrap_or_else(next_call_seed);
unsafe {
launch_randn(
&client.context,
&client.stream,
client.device.index,
dtype,
seed,
out.ptr(),
numel,
)?;
}
Ok(())
}
#[cfg(feature = "fp8")]
fn clamp_below_one_for_narrowing(
client: &CudaClient,
f32_result: &Tensor<CudaRuntime>,
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
let bound = dtype.largest_value_below_one().unwrap_or(1.0);
client.clamp(f32_result, f64::MIN, bound)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime::cuda::CudaDevice;
use crate::runtime::{Runtime, RuntimeClient};
fn setup() -> Option<CudaClient> {
if !crate::runtime::cuda::is_cuda_available() {
return None;
}
Some(CudaRuntime::default_client(&CudaDevice::new(0)))
}
#[test]
fn rand_into_rejects_integer_dtype() {
let Some(client) = setup() else {
return;
};
let out = Tensor::<CudaRuntime>::zeros(&[4], DType::I32, client.device()).unwrap();
assert!(matches!(
client.rand_into(&out),
Err(Error::UnsupportedDType { .. })
));
}
#[test]
fn rand_seeded_into_rejects_non_contiguous_destination() {
let Some(client) = setup() else {
return;
};
let out = Tensor::<CudaRuntime>::zeros(&[2, 3], DType::F32, client.device()).unwrap();
let transposed = out.transpose(0, 1).unwrap();
assert!(!transposed.is_contiguous());
assert!(matches!(
client.rand_seeded_into(&transposed, 7),
Err(Error::Backend(_))
));
}
#[test]
fn rand_into_keeps_destination_address() {
let Some(client) = setup() else {
return;
};
let out = Tensor::<CudaRuntime>::zeros(&[4], DType::F32, client.device()).unwrap();
let before = out.ptr();
client.rand_into(&out).unwrap();
assert_eq!(out.ptr(), before);
}
#[test]
fn unseeded_ops_repeat_under_with_seed() {
use crate::random::with_seed;
let Some(client) = setup() else {
return;
};
let probs = Tensor::<CudaRuntime>::ones(&[2, 32], DType::F32, client.device()).unwrap();
let run = || {
(
client.rand(&[64], DType::F32).unwrap().to_vec::<f32>(),
client.randn(&[64], DType::F32).unwrap().to_vec::<f32>(),
client
.randint(0, 1_000_000, &[64], DType::I64)
.unwrap()
.to_vec::<i64>(),
client
.bernoulli(0.5, &[64], DType::F32)
.unwrap()
.to_vec::<f32>(),
client.multinomial(&probs, 8, true).unwrap().to_vec::<i64>(),
client.randperm(64).unwrap().to_vec::<i64>(),
)
};
let a = with_seed(31, run);
let b = with_seed(31, run);
let c = with_seed(32, run);
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn unseeded_rand_uses_the_call_seed() {
use crate::random::{next_call_seed, with_seed};
let Some(client) = setup() else {
return;
};
let seed = with_seed(33, next_call_seed);
let unseeded = with_seed(33, || client.rand(&[64], DType::F32).unwrap());
let seeded = client.rand_seeded(&[64], DType::F32, seed).unwrap();
assert_eq!(unseeded.to_vec::<f32>(), seeded.to_vec::<f32>());
}
}