Struct Unary

Source
pub struct Unary(pub ComputePipeline, pub UnaryOp);
Expand description

Shader implementing various unary operations selected with UnaryOp.

Tuple Fields§

§0: ComputePipeline§1: UnaryOp

Implementations§

Source§

impl Unary

Source

pub const SRC: &'static str = "// This was mostly ported from ggml-cuda/unary.cu\n// (MIT license).\n\n#import wgblas::shape as Shape;\n\n@group(0) @binding(0)\nvar<uniform> shape_dst: Shape::Shape;\n@group(0) @binding(1)\nvar<uniform> shape_src: Shape::Shape;\n@group(0) @binding(2)\nvar<storage, read_write> dst: array<f32>;\n@group(0) @binding(3)\nvar<storage, read_write> src: array<f32>;\n@group(0) @binding(4)\nvar<uniform> args: vec4<f32>;\n\nconst GELU_COEF_A: f32 = 0.044715f;\nconst SQRT_2_OVER_PI: f32 = 0.79788456080286535587989211986876f;\nconst GELU_QUICK_COEF: f32 = -1.702f;\n\nfn abs_f32(x: f32) -> f32 {\n return abs(x);\n}\n\nfn sgn_f32(x: f32) -> f32 {\n return sign(x);\n}\n\nfn neg_f32(x: f32) -> f32 {\n return -x;\n}\n\nfn step_f32(x: f32) -> f32 {\n if x > 0.0 {\n return 1.0;\n } else {\n return 0.0;\n }\n}\n\nfn elu_f32(x: f32) -> f32 {\n if x > 0.0 {\n return x;\n } else {\n return exp(x) - 1.0;\n }\n}\n\nfn gelu_f32(x: f32) -> f32 {\n return 0.5f * x * (1.0f + tanh(SQRT_2_OVER_PI * x * (1.0f + GELU_COEF_A * x * x)));\n}\n\nfn gelu_quick_f32(x: f32) -> f32 {\n return x * (1.0f / (1.0f + exp(GELU_QUICK_COEF * x)));\n}\n\nfn silu_f32(x: f32) -> f32 {\n return x / (1.0 + exp(-x));\n}\n\nfn tanh_f32(x: f32) -> f32 {\n return tanh(x);\n}\n\nfn relu_f32(x: f32) -> f32 {\n return max(x, 0.0);\n}\n\nfn sigmoid_f32(x: f32) -> f32 {\n return 1.0 / (1.0 + exp(-x));\n}\n\nfn hard_sigmoid_f32(x: f32) -> f32 {\n return min(1.0, max(0.0, (x + 3.0) / 6.0));\n}\n\nfn hard_swish_f32(x: f32) -> f32 {\n return x * min(1.0, max(0.0, (x + 3.0) / 6.0));\n}\n\nfn sqr_f32(x: f32) -> f32 {\n return x * x;\n}\n\nfn sqrt_f32(x: f32) -> f32 {\n return sqrt(x);\n}\n\nfn log_f32(x: f32) -> f32 {\n return log(x);\n}\n\nfn placeholder(x: f32) -> f32 {\n return x;\n}\n\n// Unary ops with extra arguments passed through the args uniform.\nfn leaky_relu_f32(x: f32) -> f32 {\n return max(x, 0.0) + min(x, 0.0) * args.x;\n}\n\nfn clamp_f32(x: f32) -> f32 {\n return min(max(args.x, x), args.y);\n}\n\nfn scale_f32(x: f32) -> f32 {\n return x * args.x;\n}\n\nfn add_scalar_f32(x: f32) -> f32 {\n return x + args.x;\n}\n\n@compute @workgroup_size(64, 1, 1)\nfn main(@builtin(global_invocation_id) invocation_id: vec3<u32>) {\n if (invocation_id.x < shape_src.nrows) {\n let isrc = Shape::iv(shape_src, invocation_id.x);\n let idst = Shape::iv(shape_dst, invocation_id.x);\n dst[idst] = placeholder(src[isrc]);\n }\n}\n"

Source

pub const FILE_PATH: &'static str = "wgml/src/unary.wgsl"

Source

pub fn new(device: &Device, op: UnaryOp) -> Result<Self, ComposerError>

Source

pub fn queue<'a, 'b, T: Pod + Scalar>( &'a self, queue: &mut KernelInvocationQueue<'a>, dest: impl Into<GpuVectorView<'b, T>>, src: impl Into<GpuVectorView<'b, T>>, args: Option<&'b GpuScalar<Vector4<T>>>, )

Source

pub fn run_cpu<S: StorageMut<f32, Dyn>>( &self, vals: &mut Vector<f32, Dyn, S>, args: Vector4<f32>, )

Auto Trait Implementations§

§

impl Freeze for Unary

§

impl !RefUnwindSafe for Unary

§

impl Send for Unary

§

impl Sync for Unary

§

impl Unpin for Unary

§

impl !UnwindSafe for Unary

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,