use super::super::helpers::*;
use crate::error::{Error, Result};
use crate::runtime::ensure_contiguous;
use crate::runtime::wgpu::shaders::reduce;
use crate::runtime::wgpu::{WgpuClient, WgpuRuntime};
use crate::tensor::Tensor;
pub(crate) fn native_softmax(
client: &WgpuClient,
a: &Tensor<WgpuRuntime>,
dim: isize,
) -> Result<Tensor<WgpuRuntime>> {
let shape = a.shape();
let ndim = shape.len();
let dim = if dim < 0 {
(ndim as isize + dim) as usize
} else {
dim as usize
};
if dim >= ndim {
return Err(Error::InvalidDimension {
dim: dim as isize,
ndim,
});
}
if dim != ndim - 1 {
let mut perm: Vec<usize> = (0..ndim).collect();
perm.remove(dim);
perm.push(dim);
let permuted = a.permute(&perm)?;
let permuted_contig = permuted.contiguous()?;
let result = native_softmax_last_dim(client, &permuted_contig)?;
let mut inv_perm = vec![0; ndim];
for (i, &p) in perm.iter().enumerate() {
inv_perm[p] = i;
}
return result.permute(&inv_perm);
}
native_softmax_last_dim(client, a)
}
fn native_softmax_last_dim(
client: &WgpuClient,
a: &Tensor<WgpuRuntime>,
) -> Result<Tensor<WgpuRuntime>> {
let dtype = a.dtype();
let shape = a.shape();
let ndim = shape.len();
let a_contig = ensure_contiguous(a)?;
let dim = ndim - 1;
let batch_size: usize = shape[..dim].iter().product();
let dim_size = shape[dim];
let out = alloc_output(client, shape, dtype)?;
if out.numel() == 0 {
return Ok(out);
}
let a_buf = get_tensor_buffer(&a_contig)?;
let out_buf = get_tensor_buffer(&out)?;
let params = SoftmaxParams {
batch_size: batch_size as u32,
dim_size: dim_size as u32,
};
let params_buf = create_params_buffer(client, ¶ms);
reduce::launch_softmax_op(
client.pipeline_cache(),
client.wgpu_queue(),
&a_buf,
&out_buf,
¶ms_buf,
batch_size,
dtype,
)?;
Ok(out)
}
pub(crate) fn native_softmax_bwd(
client: &WgpuClient,
grad: &Tensor<WgpuRuntime>,
output: &Tensor<WgpuRuntime>,
dim: isize,
) -> Result<Tensor<WgpuRuntime>> {
let shape = grad.shape();
let ndim = shape.len();
let dim = if dim < 0 {
(ndim as isize + dim) as usize
} else {
dim as usize
};
if dim >= ndim {
return Err(Error::InvalidDimension {
dim: dim as isize,
ndim,
});
}
if dim != ndim - 1 {
let mut perm: Vec<usize> = (0..ndim).collect();
perm.remove(dim);
perm.push(dim);
let grad_p = grad.permute(&perm)?.contiguous()?;
let output_p = output.permute(&perm)?.contiguous()?;
let result = native_softmax_bwd_last_dim(client, &grad_p, &output_p)?;
let mut inv_perm = vec![0; ndim];
for (i, &p) in perm.iter().enumerate() {
inv_perm[p] = i;
}
return result.permute(&inv_perm);
}
native_softmax_bwd_last_dim(client, grad, output)
}
fn native_softmax_bwd_last_dim(
client: &WgpuClient,
grad: &Tensor<WgpuRuntime>,
output: &Tensor<WgpuRuntime>,
) -> Result<Tensor<WgpuRuntime>> {
let shape = grad.shape();
let ndim = shape.len();
let dtype = grad.dtype();
let grad_contig = ensure_contiguous(grad)?;
let output_contig = ensure_contiguous(output)?;
let dim = ndim - 1;
let batch_size: usize = shape[..dim].iter().product();
let dim_size = shape[dim];
let d_input = alloc_output(client, shape, dtype)?;
if d_input.numel() == 0 {
return Ok(d_input);
}
let grad_buf = get_tensor_buffer(&grad_contig)?;
let output_buf = get_tensor_buffer(&output_contig)?;
let d_input_buf = get_tensor_buffer(&d_input)?;
let params = SoftmaxParams {
batch_size: batch_size as u32,
dim_size: dim_size as u32,
};
let params_buf = create_params_buffer(client, ¶ms);
reduce::launch_softmax_bwd_op(
client.pipeline_cache(),
client.wgpu_queue(),
&grad_buf,
&output_buf,
&d_input_buf,
¶ms_buf,
batch_size,
dtype,
)?;
Ok(d_input)
}