7
7
//! needed in a Sigmoid layer.
8
8
9
9
use co:: { IBackend , SharedTensor } ;
10
- use conn:: { Relu , ReluPointwise } ;
10
+ use conn:: Relu ;
11
+ #[ cfg( all( feature="cuda" , not( feature="native" ) ) ) ]
12
+ use conn:: ReluPointwise ;
11
13
use layer:: * ;
12
14
use util:: ArcLock ;
13
15
@@ -16,6 +18,11 @@ use util::ArcLock;
16
18
/// ReLU Activation Layer
17
19
pub struct ReLU ;
18
20
21
+ //
22
+ // ReLU + ReLUPointwise
23
+ // Only on CUDA
24
+ //
25
+ #[ cfg( all( feature="cuda" , not( feature="native" ) ) ) ]
19
26
impl < B : IBackend + Relu < f32 > + ReluPointwise < f32 > > ILayer < B > for ReLU {
20
27
impl_ilayer_activation ! ( ) ;
21
28
@@ -41,6 +48,7 @@ impl<B: IBackend + Relu<f32> + ReluPointwise<f32>> ILayer<B> for ReLU {
41
48
}
42
49
}
43
50
51
+ #[ cfg( all( feature="cuda" , not( feature="native" ) ) ) ]
44
52
impl < B : IBackend + Relu < f32 > + ReluPointwise < f32 > > ComputeOutput < f32 , B > for ReLU {
45
53
fn compute_output ( & self ,
46
54
backend : & B ,
@@ -54,6 +62,7 @@ impl<B: IBackend + Relu<f32> + ReluPointwise<f32>> ComputeOutput<f32, B> for ReL
54
62
}
55
63
}
56
64
65
+ #[ cfg( all( feature="cuda" , not( feature="native" ) ) ) ]
57
66
impl < B : IBackend + Relu < f32 > + ReluPointwise < f32 > > ComputeInputGradient < f32 , B > for ReLU {
58
67
fn compute_input_gradient ( & self ,
59
68
backend : & B ,
@@ -69,4 +78,64 @@ impl<B: IBackend + Relu<f32> + ReluPointwise<f32>> ComputeInputGradient<f32, B>
69
78
}
70
79
}
71
80
81
+ #[ cfg( all( feature="cuda" , not( feature="native" ) ) ) ]
72
82
impl < B : IBackend + Relu < f32 > + ReluPointwise < f32 > > ComputeParametersGradient < f32 , B > for ReLU { }
83
+
84
+ //
85
+ // ReLU without ReLUPointwise
86
+ // Only on CUDA
87
+ //
88
+ #[ cfg( feature="native" ) ]
89
+ impl < B : IBackend + Relu < f32 > > ILayer < B > for ReLU {
90
+ impl_ilayer_activation ! ( ) ;
91
+
92
+ fn reshape ( & mut self ,
93
+ backend : :: std:: rc:: Rc < B > ,
94
+ input_data : & mut Vec < ArcLock < SharedTensor < f32 > > > ,
95
+ input_gradient : & mut Vec < ArcLock < SharedTensor < f32 > > > ,
96
+ weights_data : & mut Vec < ArcLock < SharedTensor < f32 > > > ,
97
+ weights_gradient : & mut Vec < ArcLock < SharedTensor < f32 > > > ,
98
+ output_data : & mut Vec < ArcLock < SharedTensor < f32 > > > ,
99
+ output_gradient : & mut Vec < ArcLock < SharedTensor < f32 > > > ) {
100
+ if let Some ( inp) = input_data. get ( 0 ) {
101
+ let read_inp = inp. read ( ) . unwrap ( ) ;
102
+ let input_desc = read_inp. desc ( ) ;
103
+ input_gradient[ 0 ] . write ( ) . unwrap ( ) . resize ( input_desc) . unwrap ( ) ;
104
+ output_data[ 0 ] . write ( ) . unwrap ( ) . resize ( input_desc) . unwrap ( ) ;
105
+ output_gradient[ 0 ] . write ( ) . unwrap ( ) . resize ( input_desc) . unwrap ( ) ;
106
+ }
107
+ }
108
+ }
109
+
110
+ #[ cfg( feature="native" ) ]
111
+ impl < B : IBackend + Relu < f32 > > ComputeOutput < f32 , B > for ReLU {
112
+ fn compute_output ( & self ,
113
+ backend : & B ,
114
+ _weights : & [ & SharedTensor < f32 > ] ,
115
+ input_data : & [ & SharedTensor < f32 > ] ,
116
+ output_data : & mut [ & mut SharedTensor < f32 > ] ) {
117
+ match input_data. get ( 0 ) {
118
+ Some ( input) => backend. relu_plain ( input, output_data[ 0 ] ) . unwrap ( ) ,
119
+ None => panic ! ( "No input provided for ReLU layer." ) ,
120
+ }
121
+ }
122
+ }
123
+
124
+ #[ cfg( feature="native" ) ]
125
+ impl < B : IBackend + Relu < f32 > > ComputeInputGradient < f32 , B > for ReLU {
126
+ fn compute_input_gradient ( & self ,
127
+ backend : & B ,
128
+ weights_data : & [ & SharedTensor < f32 > ] ,
129
+ output_data : & [ & SharedTensor < f32 > ] ,
130
+ output_gradients : & [ & SharedTensor < f32 > ] ,
131
+ input_data : & [ & SharedTensor < f32 > ] ,
132
+ input_gradients : & mut [ & mut SharedTensor < f32 > ] ) {
133
+ match output_data. get ( 0 ) {
134
+ Some ( _) => backend. relu_grad_plain ( output_data[ 0 ] , output_gradients[ 0 ] , input_data[ 0 ] , input_gradients[ 0 ] ) . unwrap ( ) ,
135
+ None => panic ! ( "No output_data provided for ReLU layer backward." ) ,
136
+ }
137
+ }
138
+ }
139
+
140
+ #[ cfg( feature="native" ) ]
141
+ impl < B : IBackend + Relu < f32 > > ComputeParametersGradient < f32 , B > for ReLU { }
0 commit comments