use super::super::kernels;
use super::super::{CpuClient, CpuRuntime};
use crate::dispatch_dtype;
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::ensure_contiguous;
use crate::tensor::Tensor;
struct BincountInput {
values: Vec<i64>,
out_dtype: DType,
}
fn prepare(
input: &Tensor<CpuRuntime>,
weights: Option<&Tensor<CpuRuntime>>,
) -> Result<BincountInput> {
if input.ndim() != 1 {
return Err(Error::ShapeMismatch {
expected: vec![input.numel()],
got: input.shape().to_vec(),
});
}
let input_dtype = input.dtype();
if !matches!(input_dtype, DType::I32 | DType::I64) {
return Err(Error::DTypeMismatch {
lhs: DType::I64,
rhs: input_dtype,
});
}
let out_dtype = if let Some(w) = weights {
if w.shape() != input.shape() {
return Err(Error::ShapeMismatch {
expected: input.shape().to_vec(),
got: w.shape().to_vec(),
});
}
w.dtype()
} else {
DType::I64 };
let input_contig = ensure_contiguous(input)?;
let numel = input.numel();
let values: Vec<i64> = if input_dtype == DType::I64 {
unsafe { std::slice::from_raw_parts(input_contig.ptr() as *const i64, numel).to_vec() }
} else {
let i32_slice =
unsafe { std::slice::from_raw_parts(input_contig.ptr() as *const i32, numel) };
i32_slice.iter().map(|&x| x as i64).collect()
};
Ok(BincountInput { values, out_dtype })
}
fn accumulate(
client: &CpuClient,
prepared: &BincountInput,
weights: Option<&Tensor<CpuRuntime>>,
output_len: usize,
reject_negative: bool,
) -> Result<Tensor<CpuRuntime>> {
let numel = prepared.values.len();
let out_dtype = prepared.out_dtype;
let out = Tensor::<CpuRuntime>::empty(&[output_len], out_dtype, &client.device)?;
if output_len == 0 {
return Ok(out);
}
let out_ptr = out.ptr();
if let Some(w) = weights {
let w_contig = ensure_contiguous(w)?;
let w_ptr = w_contig.ptr();
dispatch_dtype!(out_dtype, T => {
let success = unsafe {
kernels::bincount_kernel::<T>(
prepared.values.as_ptr(),
w_ptr as *const T,
out_ptr as *mut T,
numel,
output_len,
reject_negative,
)
};
if !success {
return Err(Error::InvalidArgument {
arg: "input",
reason: "bincount requires non-negative values".to_string(),
});
}
}, "bincount");
} else {
let success = unsafe {
kernels::bincount_kernel::<i64>(
prepared.values.as_ptr(),
std::ptr::null(),
out_ptr as *mut i64,
numel,
output_len,
reject_negative,
)
};
if !success {
return Err(Error::InvalidArgument {
arg: "input",
reason: "bincount requires non-negative values".to_string(),
});
}
}
Ok(out)
}
pub fn bincount_impl(
client: &CpuClient,
input: &Tensor<CpuRuntime>,
weights: Option<&Tensor<CpuRuntime>>,
minlength: usize,
) -> Result<Tensor<CpuRuntime>> {
let prepared = prepare(input, weights)?;
let output_len = if prepared.values.is_empty() {
minlength
} else {
let max_val =
unsafe { kernels::max_i64_kernel(prepared.values.as_ptr(), prepared.values.len()) };
if max_val < 0 {
return Err(Error::InvalidArgument {
arg: "input",
reason: "bincount requires non-negative values".to_string(),
});
}
(max_val as usize + 1).max(minlength)
};
accumulate(client, &prepared, weights, output_len, true)
}
pub fn bincount_with_len_impl(
client: &CpuClient,
input: &Tensor<CpuRuntime>,
weights: Option<&Tensor<CpuRuntime>>,
len: usize,
) -> Result<Tensor<CpuRuntime>> {
let prepared = prepare(input, weights)?;
accumulate(client, &prepared, weights, len, false)
}