1use super::*;
2
3impl ActivationOps<Self> for Dispatch {
4 fn silu(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
5 let selection = Selection::tensors(core::iter::once(&tensor));
6 macro_rules! call {
7 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
8 let result = <$B>::silu(unpack!($mode, $variant, Float, tensor, $context));
9 pack!($mode, $variant, Float, result)
10 }};
11 }
12 dispatch!(selection, call)
13 }
14
15 fn leaky_relu(tensor: FloatTensor<Self>, negative_slope: Scalar) -> FloatTensor<Self> {
16 let selection = Selection::tensors(core::iter::once(&tensor));
17 macro_rules! call {
18 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
19 let result = <$B>::leaky_relu(unpack!($mode, $variant, Float, tensor, $context), negative_slope);
20 pack!($mode, $variant, Float, result)
21 }};
22 }
23 dispatch!(selection, call)
24 }
25
26 fn relu(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
27 let selection = Selection::tensors(core::iter::once(&tensor));
28 macro_rules! call {
29 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
30 let result = <$B>::relu(unpack!($mode, $variant, Float, tensor, $context));
31 pack!($mode, $variant, Float, result)
32 }};
33 }
34 dispatch!(selection, call)
35 }
36
37 fn relu_backward(output: FloatTensor<Self>, grad: FloatTensor<Self>) -> FloatTensor<Self> {
38 let selection = Selection::tensors(core::iter::once(&output).chain(core::iter::once(&grad)));
39 macro_rules! call {
40 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
41 let result = <$B>::relu_backward(unpack!($mode, $variant, Float, output, $context), unpack!($mode, $variant, Float, grad, $context));
42 pack!($mode, $variant, Float, result)
43 }};
44 }
45 dispatch!(selection, call)
46 }
47
48 fn gelu(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
49 let selection = Selection::tensors(core::iter::once(&tensor));
50 macro_rules! call {
51 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
52 let result = <$B>::gelu(unpack!($mode, $variant, Float, tensor, $context));
53 pack!($mode, $variant, Float, result)
54 }};
55 }
56 dispatch!(selection, call)
57 }
58
59 fn prelu(tensor: FloatTensor<Self>, alpha: FloatTensor<Self>) -> FloatTensor<Self> {
60 let selection = Selection::tensors(core::iter::once(&tensor).chain(core::iter::once(&alpha)));
61 macro_rules! call {
62 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
63 let result = <$B>::prelu(unpack!($mode, $variant, Float, tensor, $context), unpack!($mode, $variant, Float, alpha, $context));
64 pack!($mode, $variant, Float, result)
65 }};
66 }
67 dispatch!(selection, call)
68 }
69
70 fn gelu_backward(x: FloatTensor<Self>, grad: FloatTensor<Self>) -> FloatTensor<Self> {
71 let selection = Selection::tensors(core::iter::once(&x).chain(core::iter::once(&grad)));
72 macro_rules! call {
73 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
74 let result = <$B>::gelu_backward(unpack!($mode, $variant, Float, x, $context), unpack!($mode, $variant, Float, grad, $context));
75 pack!($mode, $variant, Float, result)
76 }};
77 }
78 dispatch!(selection, call)
79 }
80
81 fn sigmoid(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
82 let selection = Selection::tensors(core::iter::once(&tensor));
83 macro_rules! call {
84 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
85 let result = <$B>::sigmoid(unpack!($mode, $variant, Float, tensor, $context));
86 pack!($mode, $variant, Float, result)
87 }};
88 }
89 dispatch!(selection, call)
90 }
91
92 fn sigmoid_backward(output: FloatTensor<Self>, grad: FloatTensor<Self>) -> FloatTensor<Self> {
93 let selection = Selection::tensors(core::iter::once(&output).chain(core::iter::once(&grad)));
94 macro_rules! call {
95 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
96 let result = <$B>::sigmoid_backward(unpack!($mode, $variant, Float, output, $context), unpack!($mode, $variant, Float, grad, $context));
97 pack!($mode, $variant, Float, result)
98 }};
99 }
100 dispatch!(selection, call)
101 }
102
103 fn hard_sigmoid(tensor: FloatTensor<Self>, alpha: Scalar, beta: Scalar) -> FloatTensor<Self> {
104 let selection = Selection::tensors(core::iter::once(&tensor));
105 macro_rules! call {
106 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
107 let result = <$B>::hard_sigmoid(unpack!($mode, $variant, Float, tensor, $context), alpha, beta);
108 pack!($mode, $variant, Float, result)
109 }};
110 }
111 dispatch!(selection, call)
112 }
113
114 fn log_sigmoid(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
115 let selection = Selection::tensors(core::iter::once(&tensor));
116 macro_rules! call {
117 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
118 let result = <$B>::log_sigmoid(unpack!($mode, $variant, Float, tensor, $context));
119 pack!($mode, $variant, Float, result)
120 }};
121 }
122 dispatch!(selection, call)
123 }
124
125 fn softmax(tensor: FloatTensor<Self>, dim: usize) -> FloatTensor<Self> {
126 let selection = Selection::tensors(core::iter::once(&tensor));
127 macro_rules! call {
128 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
129 let result = <$B>::softmax(unpack!($mode, $variant, Float, tensor, $context), dim);
130 pack!($mode, $variant, Float, result)
131 }};
132 }
133 dispatch!(selection, call)
134 }
135
136 fn log_softmax(tensor: FloatTensor<Self>, dim: usize) -> FloatTensor<Self> {
137 let selection = Selection::tensors(core::iter::once(&tensor));
138 macro_rules! call {
139 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
140 let result = <$B>::log_softmax(unpack!($mode, $variant, Float, tensor, $context), dim);
141 pack!($mode, $variant, Float, result)
142 }};
143 }
144 dispatch!(selection, call)
145 }
146
147 fn softmin(tensor: FloatTensor<Self>, dim: usize) -> FloatTensor<Self> {
148 let selection = Selection::tensors(core::iter::once(&tensor));
149 macro_rules! call {
150 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
151 let result = <$B>::softmin(unpack!($mode, $variant, Float, tensor, $context), dim);
152 pack!($mode, $variant, Float, result)
153 }};
154 }
155 dispatch!(selection, call)
156 }
157
158 fn log_sigmoid_backward(x: FloatTensor<Self>, grad: FloatTensor<Self>) -> FloatTensor<Self> {
159 let selection = Selection::tensors(core::iter::once(&x).chain(core::iter::once(&grad)));
160 macro_rules! call {
161 ($B:ty, $variant:ident, $mode:ident, $device:expr, $context:expr) => {{
162 let result = <$B>::log_sigmoid_backward(unpack!($mode, $variant, Float, x, $context), unpack!($mode, $variant, Float, grad, $context));
163 pack!($mode, $variant, Float, result)
164 }};
165 }
166 dispatch!(selection, call)
167 }
168}