use super::super::{CudaClient, CudaRuntime};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::TypeConversionOps;
use crate::runtime::RuntimeClient;
use crate::tensor::Tensor;
pub(super) fn indices_to_i32(
client: &CudaClient,
indices: &Tensor<CudaRuntime>,
op: &'static str,
) -> Result<Tensor<CudaRuntime>> {
match indices.dtype() {
DType::I32 => indices.contiguous(),
DType::I64 if indices.numel() == 0 => {
Tensor::<CudaRuntime>::zeros(&[0], DType::I32, client.device())
}
DType::I64 => client.cast(&indices.contiguous()?, DType::I32),
dtype => Err(Error::UnsupportedDType { dtype, op }),
}
}
pub(super) fn indices_to_i64(
client: &CudaClient,
indices: &Tensor<CudaRuntime>,
) -> Result<Tensor<CudaRuntime>> {
match indices.dtype() {
DType::I64 => Ok(indices.clone()),
_ if indices.numel() == 0 => {
Tensor::<CudaRuntime>::zeros(&[0], DType::I64, client.device())
}
_ => client.cast(indices, DType::I64),
}
}
pub(super) fn check_merge_dtype(dtype: DType, op: &'static str) -> Result<()> {
match dtype {
DType::I8 | DType::I16 | DType::I32 | DType::I64 => Ok(()),
_ => check_float_dtype(dtype, op),
}
}
pub(super) fn check_float_dtype(dtype: DType, op: &'static str) -> Result<()> {
match dtype {
DType::F32 | DType::F64 | DType::F16 | DType::BF16 => Ok(()),
_ => Err(Error::UnsupportedDType { dtype, op }),
}
}