use std::vec::Vec;
use crate::dispatch::{GpuBackend, GpuBuffer, backend::MetaId};
#[inline]
pub(crate) fn build_dims(shape: &[MetaId], meta: &[u32]) -> Vec<u32> {
let mut dims = Vec::with_capacity(shape.len());
for dim in shape {
dims.push(meta[*dim]);
}
dims
}
#[inline]
pub(crate) fn calc_grid(shape: &[u32], block: [u32; 3]) -> [u32; 3] {
let out_rank = shape.len();
if block[0] == block[1] {
[
shape[out_rank - 1].div_ceil(block[0]),
shape[out_rank - 2].div_ceil(block[1]),
shape[..out_rank - 2]
.iter()
.product::<u32>()
.div_ceil(block[2]),
]
} else if block[0] == 1 {
[
1,
shape[out_rank - 1],
shape[..out_rank - 2]
.iter()
.product::<u32>()
.div_ceil(block[2]),
]
} else {
[
shape[out_rank - 2],
1,
shape[..out_rank - 2]
.iter()
.product::<u32>()
.div_ceil(block[2]),
]
}
}
pub trait ToBuffer<B: GpuBackend> {
fn to_buffer(self) -> B::Buffer;
fn as_buffer(&self) -> &B::Buffer;
}
#[derive(Debug)]
pub struct Tensor<B: GpuBackend = crate::dispatch::backend::GpuContext> {
pub(crate) shape: Vec<u32>,
pub(crate) data: GpuBuffer<B>,
}
impl<B: GpuBackend> ToBuffer<B> for Tensor<B> {
#[inline]
fn to_buffer(self) -> <B as GpuBackend>::Buffer {
self.data.inner
}
#[inline]
fn as_buffer(&self) -> &<B as GpuBackend>::Buffer {
&self.data.inner
}
}
impl<B: GpuBackend> PartialEq for Tensor<B> {
fn eq(&self, other: &Self) -> bool {
self.shape == other.shape
}
}
impl<B: GpuBackend> Eq for Tensor<B> {}
impl<B: GpuBackend> Tensor<B> {
#[inline]
#[must_use]
pub fn calc_grid(&self, block: [u32; 3]) -> [u32; 3] {
calc_grid(&self.shape, block)
}
#[inline]
#[must_use]
pub const fn rank(&self) -> usize {
self.shape.len()
}
#[inline]
pub fn dims(&self) -> &[u32] {
&self.shape
}
#[inline]
#[must_use]
pub fn len(&self) -> u32 {
self.data.size()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.size_bytes() == 0
}
}
pub use half::{bf16, f16};