use super::helpers::*;
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::ensure_contiguous;
use crate::runtime::wgpu::shaders::{cast_32, cast_i64};
use crate::runtime::wgpu::{WgpuClient, WgpuRuntime};
use crate::tensor::Tensor;
const NATIVE_32: [DType; 3] = [DType::F32, DType::I32, DType::U32];
pub(crate) fn is_native_cast(src_dtype: DType, dst_dtype: DType) -> bool {
(NATIVE_32.contains(&src_dtype) && NATIVE_32.contains(&dst_dtype))
|| cast_i64::is_i64_cast_pair(src_dtype, dst_dtype)
}
pub(crate) fn native_cast_op(
client: &WgpuClient,
a: &Tensor<WgpuRuntime>,
dst_dtype: DType,
) -> Result<Tensor<WgpuRuntime>> {
let src_dtype = a.dtype();
let numel = a.numel();
if src_dtype == dst_dtype {
return Ok(a.clone());
}
let out = alloc_output(client, a.shape(), dst_dtype)?;
if numel == 0 {
return Ok(out);
}
let a_contig = ensure_contiguous(a)?;
let a_buf = get_tensor_buffer(&a_contig)?;
let out_buf = get_tensor_buffer(&out)?;
let launch = if cast_i64::is_i64_cast_pair(src_dtype, dst_dtype) {
cast_i64::launch_cast_i64_op
} else {
cast_32::launch_cast_op
};
let limits = client.wgpu_device().limits();
let src_size = src_dtype.size_in_bytes() as u64;
let dst_size = dst_dtype.size_in_bytes() as u64;
let window = window_elements(
limits.max_storage_buffer_binding_size,
u64::from(limits.min_storage_buffer_offset_alignment),
src_size.max(dst_size),
)
.ok_or_else(|| Error::BackendLimitation {
backend: "wgpu",
operation: "cast",
reason: format!(
"storage binding limit {} bytes holds no aligned window of {src_dtype:?} -> \
{dst_dtype:?} elements",
limits.max_storage_buffer_binding_size
),
})?;
let mut start = 0usize;
while start < numel {
let len = window.min(numel - start);
let binding = |buffer, elem_size: u64| wgpu::BufferBinding {
buffer,
offset: start as u64 * elem_size,
size: std::num::NonZeroU64::new(len as u64 * elem_size),
};
let params = CastParams { numel: len as u32 };
let params_buf = create_params_buffer(client, ¶ms);
launch(
client.pipeline_cache(),
client.wgpu_queue(),
binding(&a_buf, src_size),
binding(&out_buf, dst_size),
¶ms_buf,
len,
src_dtype,
dst_dtype,
)?;
start += len;
}
Ok(out)
}
fn window_elements(binding_limit: u64, alignment: u64, elem_size: u64) -> Option<usize> {
let alignment = alignment.max(1);
let fit = (binding_limit / elem_size).min(u64::from(u32::MAX));
let window = fit / alignment * alignment;
if window == 0 {
return None;
}
usize::try_from(window).ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn window_fills_the_binding_limit_at_the_widest_element() {
assert_eq!(window_elements(1 << 27, 256, 8), Some(1 << 24));
assert_eq!(window_elements(1 << 27, 256, 4), Some(1 << 25));
}
#[test]
fn window_rounds_down_to_the_offset_alignment() {
assert_eq!(window_elements(1000, 32, 4), Some(224));
}
#[test]
fn window_stays_within_u32() {
assert_eq!(
window_elements(u64::MAX, 256, 4),
Some(u32::MAX as usize / 256 * 256)
);
}
#[test]
fn window_is_none_below_one_aligned_run() {
assert_eq!(window_elements(1000, 256, 8), None);
}
}