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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! cuDNN (CUDA Deep Neural Network library) integration
//!
//! This module provides comprehensive cuDNN support for the ToRSh deep learning framework,
//! including tensor operations, neural network layers, and RNN functionality. The implementation
//! is organized into focused submodules for maintainability and clarity.
//!
//! # Architecture
//!
//! The cuDNN integration is structured as follows:
//!
//! - **types**: Core enums and type definitions
//! - **handle**: cuDNN handle management with automatic resource cleanup
//! - **descriptors**: Tensor, filter, convolution, activation, and pooling descriptors
//! - **operations**: High-level operations interface (CudnnOps)
//! - **rnn**: Recurrent Neural Network support (LSTM, GRU, vanilla RNN)
//!
//! # Features
//!
//! - **Convolution Operations**: 2D convolution with various algorithms and configurations
//! - **Batch Normalization**: Spatial and per-activation batch normalization
//! - **Activation Functions**: ReLU, Sigmoid, Tanh, and other activation functions
//! - **Pooling Operations**: Max pooling, average pooling with various configurations
//! - **Layer Normalization**: Transformer-style layer normalization
//! - **RNN Support**: LSTM, GRU, and vanilla RNN with bidirectional support
//! - **Algorithm Selection**: Automatic algorithm finding for optimal performance
//! - **Memory Management**: Automatic workspace size calculation and management
//!
//! # Usage Examples
//!
//! ## Basic Operations
//!
//! ```rust,ignore
//! use torsh_backend::cuda::cudnn::{CudnnOps, ActivationMode, PoolingMode};
//!
//! // Initialize cuDNN operations
//! let ops = CudnnOps::new()?;
//!
//! // Perform 2D convolution
//! ops.conv2d_forward(
//! input_ptr,
//! weight_ptr,
//! Some(bias_ptr),
//! output_ptr,
//! (1, 3, 224, 224), // input shape (N, C, H, W)
//! (64, 3, 7, 7), // weight shape (K, C, H, W)
//! (1, 64, 218, 218), // output shape (N, K, H_out, W_out)
//! (0, 0), // padding
//! (1, 1), // stride
//! (1, 1), // dilation
//! )?;
//!
//! // Apply activation function
//! ops.activation_forward(
//! ActivationMode::Relu,
//! input_ptr,
//! output_ptr,
//! (1, 64, 218, 218),
//! )?;
//!
//! // Perform max pooling
//! ops.pooling2d_forward(
//! PoolingMode::Max,
//! input_ptr,
//! output_ptr,
//! (1, 64, 218, 218), // input shape
//! (1, 64, 109, 109), // output shape
//! (2, 2), // window size
//! (0, 0), // padding
//! (2, 2), // stride
//! )?;
//! ```
//!
//! ## RNN Operations
//!
//! ```rust,ignore
//! use torsh_backend::cuda::cudnn::{
//! CudnnHandle, CudnnOps,
//! rnn::{RNNDescriptor, RNNDataDescriptor, DropoutDescriptor},
//! rnn::{RNNMode, RNNInputMode, RNNDirectionMode, RNNAlgorithm, MathType, RNNForwardMode, RNNDataLayout},
//! };
//! use torsh_core::DType;
//!
//! // Create cuDNN handle
//! let handle = CudnnHandle::new()?;
//!
//! // Create dropout descriptor
//! let dropout_desc = DropoutDescriptor::new(&handle, 0.1, 12345)?;
//!
//! // Create and configure RNN descriptor
//! let mut rnn_desc = RNNDescriptor::new()?;
//! rnn_desc.set_lstm(
//! 128, // hidden_size
//! 2, // num_layers
//! &dropout_desc,
//! RNNInputMode::LinearInput,
//! RNNDirectionMode::Bidirectional,
//! RNNMode::LSTM,
//! RNNAlgorithm::Standard,
//! MathType::Default,
//! )?;
//!
//! // Create RNN data descriptors
//! let mut x_desc = RNNDataDescriptor::new()?;
//! x_desc.set(
//! DType::F32,
//! RNNDataLayout::SeqMajorUnpacked,
//! 20, // max_seq_length
//! 32, // batch_size
//! 256, // vector_size
//! &sequence_lengths,
//! None, // no padding fill
//! )?;
//!
//! // Perform LSTM forward pass
//! let ops = CudnnOps::new()?;
//! ops.lstm_forward(
//! &rnn_desc,
//! RNNForwardMode::Training,
//! Some(seq_lengths_ptr),
//! &x_desc,
//! x_ptr,
//! &y_desc,
//! y_ptr,
//! &h_desc,
//! Some(hx_ptr),
//! Some(hy_ptr),
//! &c_desc,
//! Some(cx_ptr),
//! Some(cy_ptr),
//! weight_space_size,
//! weight_space_ptr,
//! work_space_size,
//! work_space_ptr,
//! reserve_space_size,
//! Some(reserve_space_ptr),
//! )?;
//! ```
//!
//! ## Descriptor Management
//!
//! ```rust,ignore
//! use torsh_backend::cuda::cudnn::{
//! TensorDescriptor, FilterDescriptor, ConvolutionDescriptor,
//! ActivationDescriptor, PoolingDescriptor,
//! ConvolutionMode, ActivationMode, NanPropagation, PoolingMode,
//! };
//! use torsh_core::DType;
//!
//! // Create tensor descriptor
//! let mut input_desc = TensorDescriptor::new()?;
//! input_desc.set_4d(DType::F32, 1, 3, 224, 224)?;
//!
//! // Create filter descriptor
//! let mut filter_desc = FilterDescriptor::new()?;
//! filter_desc.set_4d(DType::F32, 64, 3, 7, 7)?;
//!
//! // Create convolution descriptor
//! let mut conv_desc = ConvolutionDescriptor::new()?;
//! conv_desc.set_2d(
//! 3, 3, // padding
//! 2, 2, // stride
//! 1, 1, // dilation
//! ConvolutionMode::CrossCorrelation,
//! )?;
//!
//! // Create activation descriptor
//! let mut act_desc = ActivationDescriptor::new()?;
//! act_desc.set(ActivationMode::Relu, NanPropagation::NotPropagate, 0.0)?;
//! ```
//!
//! # Feature Requirements
//!
//! This module requires the "cudnn" feature to be enabled. When the feature is disabled,
//! all operations will return appropriate errors indicating that cuDNN is not available.
//!
//! # Error Handling
//!
//! All operations return `CudaResult<T>` which provides comprehensive error information
//! including cuDNN status codes and descriptive error messages.
//!
//! # Thread Safety
//!
//! All types in this module implement `Send` and `Sync` where appropriate, allowing
//! safe use in multi-threaded environments. cuDNN handles are protected by mutexes
//! to ensure thread-safe access.
// Re-export core functionality for easy access
pub use ;
pub use CudnnHandle;
pub use ;
pub use ;
// Re-export RNN functionality
pub use ;
// Convenience type aliases for common operations
/// Alias for the main cuDNN operations interface
pub type Operations = CudnnOps;
/// Alias for cuDNN handle
pub type Handle = CudnnHandle;