1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use custos::{impl_stack, number::Float, CDatatype, Device, MainMemory, Shape};
#[cfg(feature = "cpu")]
use custos::CPU;
#[cfg(feature = "stack")]
use custos::Stack;
use crate::{each_op, Matrix};
#[cfg(feature = "cuda")]
use crate::cu_str_op;
#[cfg(feature = "cuda")]
use custos::CUDA;
#[cfg(feature = "opencl")]
use crate::opencl::cl_str_op_mat;
#[cfg(feature = "opencl")]
use custos::OpenCL;
impl<'a, T: Float, S: Shape, D: FnsOps<T, S, D>> Matrix<'a, T, D, S> {
#[inline]
pub fn exp(&self) -> Self {
self.device().exp(self)
}
#[inline]
pub fn ln(&self) -> Self {
self.device().ln(self)
}
#[inline]
pub fn neg(&self) -> Self {
self.device().neg(self)
}
#[inline]
pub fn powf(&self, rhs: T) -> Self {
self.device().powf(self, rhs)
}
#[inline]
pub fn powi(&self, rhs: i32) -> Self {
self.device().powi(self, rhs)
}
}
pub trait FnsOps<T, S: Shape = (), D: Device = Self>: Device {
fn exp(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
fn ln(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
fn neg(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
fn powf(&self, x: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S>;
fn powi(&self, x: &Matrix<T, D, S>, rhs: i32) -> Matrix<T, Self, S>;
}
#[impl_stack]
impl<T, D, S> FnsOps<T, S, D> for CPU
where
T: Float,
D: MainMemory,
S: Shape,
{
#[inline]
fn exp(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
each_op(self, x, |x| x.exp())
}
#[inline]
fn ln(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
each_op(self, x, |x| x.ln())
}
#[inline]
fn neg(&self, x: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
each_op(self, x, |x| -x)
}
#[inline]
fn powf(&self, x: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S> {
each_op(self, x, |x| x.powf(rhs))
}
#[inline]
fn powi(&self, x: &Matrix<T, D, S>, rhs: i32) -> Matrix<T, Self, S> {
each_op(self, x, |x| x.powi(rhs))
}
}
#[cfg(feature = "opencl")]
impl<T: CDatatype> FnsOps<T> for OpenCL {
#[inline]
fn exp(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
cl_str_op_mat(self, x, "exp(x)").unwrap()
}
#[inline]
fn ln(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
cl_str_op_mat(self, x, "log(x)").unwrap()
}
#[inline]
fn neg(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
cl_str_op_mat(self, x, "-x").unwrap()
}
#[inline]
fn powf(&self, x: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self> {
cl_str_op_mat(self, x, &format!("pow(x, {rhs})")).unwrap()
}
#[inline]
fn powi(&self, x: &Matrix<T, Self>, rhs: i32) -> Matrix<T, Self> {
cl_str_op_mat(self, x, &format!("pow(x, {rhs})")).unwrap()
}
}
#[cfg(feature = "cuda")]
impl<T: CDatatype> FnsOps<T> for CUDA {
#[inline]
fn exp(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
let out = cu_str_op(self, x, "exp(x)").unwrap();
(out, x.dims()).into()
}
#[inline]
fn ln(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
let out = cu_str_op(self, x, "logf(x)").unwrap();
(out, x.dims()).into()
}
#[inline]
fn neg(&self, x: &Matrix<T, Self>) -> Matrix<T, Self> {
let out = cu_str_op(self, x, "-x").unwrap();
(out, x.dims()).into()
}
#[inline]
fn powf(&self, x: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self> {
let out = cu_str_op(self, x, &format!("powf(x, {rhs})")).unwrap();
(out, x.dims()).into()
}
#[inline]
fn powi(&self, x: &Matrix<T, Self>, rhs: i32) -> Matrix<T, Self> {
let out = cu_str_op(self, x, &format!("powf(x, {rhs})")).unwrap();
(out, x.dims()).into()
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "stack")]
#[test]
fn test_stack_impl() {
use custos::{Buffer, Stack};
use crate::Matrix;
let data = Buffer::from((Stack, &[3., 1., 5.]));
let mat = Matrix { data, dims: (1, 3) };
mat.ln();
}
}