use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::ScalarOps;
use crate::runtime::wgpu::ops::native::{native_fused_mul_add_scalar, native_scalar_op};
use crate::runtime::wgpu::{WgpuClient, WgpuRuntime};
use crate::runtime::{cap_ipow_exponent, pow_scalar_output_dtype};
use crate::tensor::Tensor;
impl ScalarOps<WgpuRuntime> for WgpuClient {
fn add_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
native_scalar_op(self, "add_scalar", a, scalar)
}
fn sub_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
native_scalar_op(self, "sub_scalar", a, scalar)
}
fn mul_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
native_scalar_op(self, "mul_scalar", a, scalar)
}
fn div_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
native_scalar_op(self, "div_scalar", a, scalar)
}
fn pow_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
let out_dtype = pow_scalar_output_dtype(a.dtype(), scalar);
if out_dtype != a.dtype() {
return Err(Error::UnsupportedDType {
dtype: DType::F64,
op: "pow_scalar",
});
}
let exponent = if a.dtype().is_int() {
cap_ipow_exponent(scalar)
} else {
scalar
};
native_scalar_op(self, "pow_scalar", a, exponent)
}
fn rsub_scalar(&self, a: &Tensor<WgpuRuntime>, scalar: f64) -> Result<Tensor<WgpuRuntime>> {
native_scalar_op(self, "rsub_scalar", a, scalar)
}
fn fused_mul_add_scalar(
&self,
a: &Tensor<WgpuRuntime>,
scale: f64,
bias: f64,
) -> Result<Tensor<WgpuRuntime>> {
native_fused_mul_add_scalar(self, a, scale, bias)
}
}