use super::Tensor;
use crate::backend_authoring::Backend;
use crate::dist::{Placement, PlacementKind};
use crate::err::Result;
use crate::shapes::{DynShape, Shape, ShapeBuf};
use crate::tensor::device::{Device, DeviceId};
use crate::tensor::dtype::{DType, DTypeId};
use crate::tensor::grad::RequiresGrad;
impl<S: Shape, B: Backend, K: DType, G: RequiresGrad, P: Placement> Tensor<S, B, K, G, P> {
#[inline]
pub fn inner(&self) -> &B::Storage<K> {
&self.inner
}
#[inline]
pub fn into_inner(self) -> B::Storage<K> {
self.inner
}
#[inline]
pub fn shape_buf(&self) -> &crate::shapes::ShapeBuf {
self._shape.shape_buf()
}
pub(crate) fn shape_buf_value(&self) -> ShapeBuf {
self._shape.shape_buf().clone()
}
#[inline]
pub fn grad_field(&self) -> &G::Field {
&self._grad
}
#[must_use]
pub fn placement(&self) -> PlacementKind {
P::to_incin(&self._placement)
}
#[must_use]
pub fn rank_index(&self) -> usize {
P::rank(&self._placement)
}
#[must_use]
pub fn local_dims(&self) -> alloc::vec::Vec<usize> {
B::shape(&self.inner).as_ref().to_vec()
}
#[must_use]
pub fn dtype(&self) -> crate::tensor::dtype::DTypeDescriptor {
K::descriptor(&self._dtype)
}
pub fn builtin_dtype_id(&self) -> Option<DTypeId> {
K::descriptor(&self._dtype).builtin_id()
}
#[must_use]
#[deprecated(note = "Use `.dtype()` instead")]
pub fn dtype_descriptor(&self) -> crate::tensor::dtype::DTypeDescriptor {
self.dtype()
}
pub fn device(&self) -> Result<DeviceId> {
B::Device::to_incin(&self._device)
}
#[must_use]
pub fn requires_grad(&self) -> bool {
G::requires_grad(&self._grad)
}
}
impl<S: Shape + DynShape, B: Backend, K: DType, G: RequiresGrad, P: Placement>
Tensor<S, B, K, G, P>
{
#[inline]
pub fn rank(&self) -> usize {
self._shape.shape_buf().rank()
}
#[inline]
pub fn numel(&self) -> usize {
self._shape.shape_buf().numel().unwrap_or(0)
}
#[inline]
pub fn dims(&self) -> crate::shapes::ShapeBuf {
self._shape.shape_buf().clone()
}
}