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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! ONNX LSTM node import implementation.
//!
//! ## Supported ONNX Features
//!
//! - Forward, reverse, and bidirectional directions
//! - Batch-first and sequence-first layouts (`layout` attribute)
//! - Initial hidden and cell states
//! - Custom activations: Sigmoid, Tanh, Relu, HardSigmoid, LeakyRelu, Softplus
//! - Cell state clipping (`clip` attribute)
//! - Input-forget gate coupling (`input_forget` attribute)
//!
//! ## Unsupported ONNX Features
//!
//! - **Peephole connections**: ONNX input `P` with shape `[num_directions, 3*hidden_size]` allows
//! gates to "peek" at the cell state. This is rarely used in modern models.
//!
//! - **Variable sequence lengths**: ONNX input `sequence_lens` with shape `[batch_size]` specifies
//! the actual length of each sequence in a batch. Currently, all sequences in a batch must have
//! the same length.
use super::prelude::*;
use burn::nn::activation::ActivationConfig;
use burn_store::TensorSnapshot;
use onnx_ir::lstm::{LstmActivationFunction, LstmDirection};
/// Convert ONNX activation function to Burn ActivationConfig.
///
/// # Panics
///
/// Panics if the ONNX activation function is not supported by burn-nn.
/// Supported activations: Sigmoid, Tanh, Relu, HardSigmoid, LeakyRelu, Softplus.
fn to_burn_activation(onnx_activation: LstmActivationFunction) -> ActivationConfig {
match onnx_activation {
LstmActivationFunction::Sigmoid => ActivationConfig::Sigmoid,
LstmActivationFunction::Tanh => ActivationConfig::Tanh,
LstmActivationFunction::Relu => ActivationConfig::Relu,
LstmActivationFunction::HardSigmoid => {
ActivationConfig::HardSigmoid(burn::nn::HardSigmoidConfig::new())
}
LstmActivationFunction::LeakyRelu => {
ActivationConfig::LeakyRelu(burn::nn::LeakyReluConfig::new())
}
LstmActivationFunction::Softplus => {
ActivationConfig::Softplus(burn::nn::SoftplusConfig::new())
}
unsupported => panic!(
"LSTM activation '{:?}' is not supported by burn-nn. \
Supported activations: Sigmoid, Tanh, Relu, HardSigmoid, LeakyRelu, Softplus. \
Consider using a supported activation or implementing support in burn-nn.",
unsupported
),
}
}
/// Collect tensor snapshots for LSTM burnpack serialization.
///
/// This function handles the complex weight transformation from ONNX's packed format
/// to Burn's individual GateController structure using NdArray backend for tensor ops.
///
/// ONNX LSTM weight layout:
/// - W: `[num_directions, 4*hidden_size, input_size]` - gates ordered as [i, o, f, c]
/// - R: `[num_directions, 4*hidden_size, hidden_size]` - gates ordered as [i, o, f, c]
/// - B: `[num_directions, 8*hidden_size]` - Wb[i,o,f,c] then Rb[i,o,f,c]
///
/// Burn LSTM structure (per direction):
/// - input_gate.input_transform: weight `[input_size, hidden_size]`, bias `[hidden_size]`
/// - input_gate.hidden_transform: weight `[hidden_size, hidden_size]`, bias `[hidden_size]`
/// - forget_gate, output_gate, cell_gate: same structure
#[allow(clippy::single_range_in_vec_init)]
fn collect_lstm_snapshots(
field_name: &str,
inputs: &[Argument],
config: &onnx_ir::lstm::LstmConfig,
) -> Vec<TensorSnapshot> {
use crate::burn::node_traits::{SerializationBackend, extract_node_data};
use burn::tensor::Tensor;
let hidden_size = config.hidden_size;
let input_size = config.input_size;
// Extract weight tensors from inputs
let data_w = extract_node_data(inputs, 1);
let data_r = extract_node_data(inputs, 2);
let data_b = extract_node_data(inputs, 3);
let Some(data_w) = data_w else {
return vec![];
};
let Some(data_r) = data_r else {
return vec![];
};
let dtype = data_w.dtype;
let device = Default::default();
// ONNX gate order: i(input), o(output), f(forget), c(cell)
// Burn gate order: input_gate, forget_gate, output_gate, cell_gate
let onnx_to_burn_gate_order = [0usize, 2, 1, 3]; // input, forget, output, cell
let gate_names = ["input_gate", "forget_gate", "output_gate", "cell_gate"];
// Determine direction prefixes based on LSTM type
let direction_prefixes: Vec<&str> = match config.direction {
LstmDirection::Forward | LstmDirection::Reverse => vec![""],
LstmDirection::Bidirectional => vec!["forward.", "reverse."],
};
let mut snapshots = Vec::new();
// Create tensors from data
let w_tensor: Tensor<SerializationBackend, 3> = Tensor::from_data(data_w.clone(), &device);
let r_tensor: Tensor<SerializationBackend, 3> = Tensor::from_data(data_r.clone(), &device);
let b_tensor: Option<Tensor<SerializationBackend, 2>> =
data_b.clone().map(|b| Tensor::from_data(b, &device));
for (dir_idx, dir_prefix) in direction_prefixes.iter().enumerate() {
// Select direction slice from W and R
// W shape: [num_directions, 4*hidden_size, input_size]
let w_dir = w_tensor
.clone()
.slice([dir_idx..dir_idx + 1, 0..4 * hidden_size, 0..input_size])
.squeeze::<2>(); // [4*hidden_size, input_size]
// R shape: [num_directions, 4*hidden_size, hidden_size]
let r_dir = r_tensor
.clone()
.slice([dir_idx..dir_idx + 1, 0..4 * hidden_size, 0..hidden_size])
.squeeze::<2>(); // [4*hidden_size, hidden_size]
// B shape: [num_directions, 8*hidden_size]
let b_dir = b_tensor.as_ref().map(|b| {
b.clone()
.slice([dir_idx..dir_idx + 1, 0..8 * hidden_size])
.squeeze::<1>() // [8*hidden_size]
});
for (gate_idx, gate_name) in gate_names.iter().enumerate() {
let onnx_gate_idx = onnx_to_burn_gate_order[gate_idx];
let start = onnx_gate_idx * hidden_size;
let end = start + hidden_size;
// Input transform weight: slice from W and transpose
// ONNX: [hidden_size, input_size] -> Burn: [input_size, hidden_size]
let w_gate = w_dir.clone().slice([start..end, 0..input_size]).transpose(); // [input_size, hidden_size]
let w_gate_data = w_gate.into_data();
let path = format!(
"{}.{}{}.input_transform.weight",
field_name, dir_prefix, gate_name
);
snapshots.push(create_snapshot_from_data(
w_gate_data,
&path,
"Linear",
dtype,
));
// Input transform bias: Wb + Rb for this gate
if let Some(ref b) = b_dir {
let wb_start = onnx_gate_idx * hidden_size;
let wb_end = wb_start + hidden_size;
let rb_start = 4 * hidden_size + onnx_gate_idx * hidden_size;
let rb_end = rb_start + hidden_size;
let wb: Tensor<SerializationBackend, 1> = b.clone().slice([wb_start..wb_end]);
let rb: Tensor<SerializationBackend, 1> = b.clone().slice([rb_start..rb_end]);
let bias = wb.add(rb);
let bias_data = bias.into_data();
let path = format!(
"{}.{}{}.input_transform.bias",
field_name, dir_prefix, gate_name
);
snapshots.push(create_snapshot_from_data(bias_data, &path, "Linear", dtype));
}
// Hidden transform weight: slice from R and transpose
// ONNX: [hidden_size, hidden_size] -> Burn: [hidden_size, hidden_size]
let r_gate = r_dir
.clone()
.slice([start..end, 0..hidden_size])
.transpose(); // [hidden_size, hidden_size]
let r_gate_data = r_gate.into_data();
let path = format!(
"{}.{}{}.hidden_transform.weight",
field_name, dir_prefix, gate_name
);
snapshots.push(create_snapshot_from_data(
r_gate_data,
&path,
"Linear",
dtype,
));
// Hidden transform bias: zeros (combined bias is in input_transform)
if b_dir.is_some() {
let zeros: Tensor<SerializationBackend, 1> = Tensor::zeros([hidden_size], &device);
let zeros_data = zeros.into_data();
let path = format!(
"{}.{}{}.hidden_transform.bias",
field_name, dir_prefix, gate_name
);
snapshots.push(create_snapshot_from_data(
zeros_data, &path, "Linear", dtype,
));
}
}
}
snapshots
}
/// Create a TensorSnapshot from TensorData.
///
/// Converts the data to the target dtype to preserve the original precision.
/// This is important because intermediate tensor operations use f64 for precision,
/// but we need to store the data in the original dtype (e.g., F16, F32).
fn create_snapshot_from_data(
data: burn::tensor::TensorData,
path: &str,
container_type: &str,
dtype: burn::tensor::DType,
) -> TensorSnapshot {
use burn::module::ParamId;
use burn_store::TensorSnapshotError;
use std::rc::Rc;
// Convert data back to the original dtype
// This is necessary because we use f64 for intermediate operations to preserve precision
let data = data.convert_dtype(dtype);
let shape = data.shape.clone();
let path_stack: Vec<String> = path.split('.').map(String::from).collect();
let container_stack = vec![format!("Struct:{}", container_type)];
let data_fn = Rc::new(
move || -> Result<burn::tensor::TensorData, TensorSnapshotError> { Ok(data.clone()) },
);
TensorSnapshot::from_closure(
data_fn,
dtype,
shape,
path_stack,
container_stack,
ParamId::new(),
)
}
/// Convert ActivationConfig to tokens for code generation
fn activation_to_tokens(activation: &ActivationConfig) -> TokenStream {
match activation {
ActivationConfig::Sigmoid => quote! { ActivationConfig::Sigmoid },
ActivationConfig::Tanh => quote! { ActivationConfig::Tanh },
ActivationConfig::Relu => quote! { ActivationConfig::Relu },
ActivationConfig::HardSigmoid(_) => {
quote! { ActivationConfig::HardSigmoid(burn::nn::HardSigmoidConfig::new()) }
}
ActivationConfig::LeakyRelu(_) => {
quote! { ActivationConfig::LeakyRelu(burn::nn::LeakyReluConfig::new()) }
}
ActivationConfig::Softplus(_) => {
quote! { ActivationConfig::Softplus(burn::nn::SoftplusConfig::new()) }
}
_ => panic!("Unsupported activation config for LSTM"),
}
}
impl NodeCodegen for onnx_ir::lstm::LstmNode {
fn inputs(&self) -> &[Argument] {
&self.inputs
}
fn outputs(&self) -> &[Argument] {
&self.outputs
}
fn field(&self) -> Option<Field> {
let name = Ident::new(&self.name, Span::call_site());
let d_input = self.config.input_size.to_tokens();
let d_hidden = self.config.hidden_size.to_tokens();
let bias = self.config.has_bias;
let batch_first = self.config.batch_first;
let input_forget = self.config.input_forget;
// Convert activations to tokens
let gate_act = to_burn_activation(self.config.gate_activation);
let cell_act = to_burn_activation(self.config.cell_activation);
let hidden_act = to_burn_activation(self.config.hidden_activation);
let gate_activation = activation_to_tokens(&gate_act);
let cell_activation = activation_to_tokens(&cell_act);
let hidden_activation = activation_to_tokens(&hidden_act);
// Generate clip config if present
let clip_config = if let Some(clip) = self.config.clip {
let clip_val = clip as f64;
quote! { .with_clip(Some(#clip_val)) }
} else {
quote! {}
};
// Only add non-default activations to config
let activations_config = {
let mut tokens = quote! {};
if !matches!(gate_act, ActivationConfig::Sigmoid) {
tokens = quote! { #tokens .with_gate_activation(#gate_activation) };
}
if !matches!(cell_act, ActivationConfig::Tanh) {
tokens = quote! { #tokens .with_cell_activation(#cell_activation) };
}
if !matches!(hidden_act, ActivationConfig::Tanh) {
tokens = quote! { #tokens .with_hidden_activation(#hidden_activation) };
}
tokens
};
match self.config.direction {
LstmDirection::Forward => Some(Field::new(
self.name.clone(),
quote! { Lstm<B> },
quote! {
let #name = LstmConfig::new(#d_input, #d_hidden, #bias)
.with_batch_first(#batch_first)
.with_input_forget(#input_forget)
#clip_config
#activations_config
.init(device);
},
)),
LstmDirection::Reverse => Some(Field::new(
self.name.clone(),
quote! { Lstm<B> },
quote! {
let #name = LstmConfig::new(#d_input, #d_hidden, #bias)
.with_batch_first(#batch_first)
.with_reverse(true)
.with_input_forget(#input_forget)
#clip_config
#activations_config
.init(device);
},
)),
LstmDirection::Bidirectional => Some(Field::new(
self.name.clone(),
quote! { BiLstm<B> },
quote! {
let #name = BiLstmConfig::new(#d_input, #d_hidden, #bias)
.with_batch_first(#batch_first)
.with_input_forget(#input_forget)
#clip_config
#activations_config
.init(device);
},
)),
}
}
fn collect_snapshots(&self, field_name: &str) -> Vec<TensorSnapshot> {
collect_lstm_snapshots(field_name, &self.inputs, &self.config)
}
fn forward(&self, scope: &mut ScopeAtPosition<'_>) -> TokenStream {
let input = scope.arg(self.inputs.first().unwrap());
let field = Ident::new(&self.name, Span::call_site());
// Get output variable names
let output_y = self.outputs.first().map(arg_to_ident);
let output_y_h = self.outputs.get(1).map(arg_to_ident);
let output_y_c = self.outputs.get(2).map(arg_to_ident);
// Handle initial states if provided
let has_initial_h = self.config.has_initial_h;
let has_initial_c = self.config.has_initial_c;
// Get initial state inputs if present
// Input indices: 0=X, 1=W, 2=R, 3=B, 4=sequence_lens, 5=initial_h, 6=initial_c
// ONNX initial states: [num_directions, batch_size, hidden_size]
// Burn expects: [batch_size, hidden_size] for unidirectional
let initial_state_expr = if has_initial_h && has_initial_c {
let h_input = scope.arg(&self.inputs[5]);
let c_input = scope.arg(&self.inputs[6]);
match self.config.direction {
LstmDirection::Forward | LstmDirection::Reverse => {
// Squeeze out the direction dimension (index 0) for unidirectional LSTM
// ONNX: [1, batch_size, hidden_size] -> Burn: [batch_size, hidden_size]
quote! { Some(LstmState::new(#c_input.squeeze_dim(0), #h_input.squeeze_dim(0))) }
}
LstmDirection::Bidirectional => {
// For bidirectional, keep all dimensions but reshape appropriately
quote! { Some(LstmState::new(#c_input, #h_input)) }
}
}
} else {
quote! { None }
};
// The LSTM module now handles batch_first and reverse internally via config,
// so no input/output transformation is needed here
let forward_call = quote! {
let (output_seq, final_state) = self.#field.forward(#input, #initial_state_expr);
};
// Transform outputs to ONNX format
// Burn output shape depends on batch_first config:
// batch_first=true: [batch_size, seq_length, hidden_size] or [batch_size, seq_length, 2*hidden_size] for bidirectional
// batch_first=false: [seq_length, batch_size, hidden_size] or [seq_length, batch_size, 2*hidden_size] for bidirectional
// ONNX Y output: [seq_length, num_directions, batch_size, hidden_size]
// Y_h: [num_directions, batch_size, hidden_size]
// Y_c: [num_directions, batch_size, hidden_size]
// For unidirectional LSTM:
// - Burn final_state.hidden/cell: [batch_size, hidden_size] (2D)
// - Need to unsqueeze to add num_directions dimension
// - Burn output: [seq, batch, hidden] -> ONNX Y: [seq, 1, batch, hidden]
// For bidirectional LSTM:
// - Burn final_state.hidden/cell: [2, batch_size, hidden_size] (already 3D)
// - No unsqueeze needed
// - Burn output: [seq, batch, 2*hidden] -> ONNX Y: [seq, 2, batch, hidden]
// This requires reshape + transpose
let is_bidirectional = matches!(self.config.direction, LstmDirection::Bidirectional);
let hidden_size = self.config.hidden_size;
let (hidden_expr, cell_expr) = if is_bidirectional {
(quote! { final_state.hidden }, quote! { final_state.cell })
} else {
(
quote! { final_state.hidden.unsqueeze_dims::<3>(&[0]) },
quote! { final_state.cell.unsqueeze_dims::<3>(&[0]) },
)
};
// Y output transformation
// For unidirectional: unsqueeze at dim 1 to add num_directions=1
// For bidirectional: reshape to split the concatenated hidden states, then reorder dims
// ONNX layout=0 (batch_first=false): Y is [seq, num_dirs, batch, hidden]
// ONNX layout=1 (batch_first=true): Y is [batch, seq, num_dirs, hidden]
let y_output_expr = if is_bidirectional {
let batch_first = self.config.batch_first;
if batch_first {
// Burn output: [batch, seq, 2*hidden]
// Reshape to: [batch, seq, 2, hidden] - already matches ONNX layout=1
quote! {
{
let [batch_size, seq_len, _] = output_seq.dims();
output_seq.reshape([batch_size, seq_len, 2, #hidden_size])
}
}
} else {
// Burn output: [seq, batch, 2*hidden]
// Reshape to: [seq, batch, 2, hidden]
// Then swap dims 1 and 2 to get: [seq, 2, batch, hidden] for ONNX layout=0
quote! {
{
let [seq_len, batch_size, _] = output_seq.dims();
let reshaped = output_seq.reshape([seq_len, batch_size, 2, #hidden_size]);
reshaped.swap_dims(1, 2)
}
}
}
} else {
quote! { output_seq.unsqueeze_dims::<4>(&[1]) }
};
// Build output assignments based on which outputs are used
// Use block scoping to contain temporary variables
match (output_y, output_y_h, output_y_c) {
(Some(y), Some(y_h), Some(y_c)) => {
quote! {
let (#y, #y_h, #y_c) = {
#forward_call
(
#y_output_expr,
#hidden_expr,
#cell_expr
)
};
}
}
(Some(y), Some(y_h), None) => {
quote! {
let (#y, #y_h) = {
#forward_call
(
#y_output_expr,
#hidden_expr
)
};
}
}
(Some(y), None, None) => {
quote! {
let #y = {
#forward_call
#y_output_expr
};
}
}
(None, Some(y_h), Some(y_c)) => {
quote! {
let (#y_h, #y_c) = {
#forward_call
(
#hidden_expr,
#cell_expr
)
};
}
}
_ => {
// Handle remaining cases - just run the forward pass
quote! {
{
#forward_call
}
}
}
}
}
fn register_imports(&self, imports: &mut BurnImports) {
// Check if we need to import ActivationConfig (for non-default activations)
let gate_act = to_burn_activation(self.config.gate_activation);
let cell_act = to_burn_activation(self.config.cell_activation);
let hidden_act = to_burn_activation(self.config.hidden_activation);
let needs_activation_import = !matches!(gate_act, ActivationConfig::Sigmoid)
|| !matches!(cell_act, ActivationConfig::Tanh)
|| !matches!(hidden_act, ActivationConfig::Tanh);
if needs_activation_import {
imports.register("burn::nn::ActivationConfig");
}
match self.config.direction {
LstmDirection::Forward | LstmDirection::Reverse => {
imports.register("burn::nn::Lstm");
imports.register("burn::nn::LstmConfig");
imports.register("burn::nn::LstmState");
}
LstmDirection::Bidirectional => {
imports.register("burn::nn::BiLstm");
imports.register("burn::nn::BiLstmConfig");
imports.register("burn::nn::LstmState");
}
}
}
}
#[cfg(test)]
mod tests {
use super::super::test_helpers::*;
use burn::tensor::DType;
use insta::assert_snapshot;
use onnx_ir::ir::{ArgType, Argument, TensorType};
use onnx_ir::lstm::{LstmActivationFunction, LstmConfig, LstmDirection, LstmNode};
fn create_lstm_node(
name: &str,
direction: LstmDirection,
batch_first: bool,
num_outputs: usize,
) -> LstmNode {
let config = LstmConfig::new(
4, // input_size
8, // hidden_size
direction,
true, // has_bias
false, // has_initial_h
false, // has_initial_c
false, // has_peephole
batch_first,
None, // clip
false, // input_forget
LstmActivationFunction::Sigmoid, // gate_activation
LstmActivationFunction::Tanh, // cell_activation
LstmActivationFunction::Tanh, // hidden_activation
);
let input = Argument::new(
"input",
ArgType::Tensor(TensorType::new(DType::F32, 3, None)),
);
let w = Argument::new("W", ArgType::Tensor(TensorType::new(DType::F32, 3, None)));
let r = Argument::new("R", ArgType::Tensor(TensorType::new(DType::F32, 3, None)));
let b = Argument::new("B", ArgType::Tensor(TensorType::new(DType::F32, 2, None)));
let mut outputs = vec![];
if num_outputs > 0 {
outputs.push(Argument::new(
"Y",
ArgType::Tensor(TensorType::new(DType::F32, 4, None)),
));
}
if num_outputs > 1 {
outputs.push(Argument::new(
"Y_h",
ArgType::Tensor(TensorType::new(DType::F32, 3, None)),
));
}
if num_outputs > 2 {
outputs.push(Argument::new(
"Y_c",
ArgType::Tensor(TensorType::new(DType::F32, 3, None)),
));
}
LstmNode {
name: name.to_string(),
inputs: vec![input, w, r, b],
outputs,
config,
}
}
#[test]
fn test_lstm_forward_basic() {
let node = create_lstm_node("lstm1", LstmDirection::Forward, false, 3);
let code = codegen_forward_default(&node);
assert_snapshot!(code, @r"
pub fn forward(
&self,
input: Tensor<B, 3>,
W: Tensor<B, 3>,
R: Tensor<B, 3>,
B: Tensor<B, 2>,
) -> (Tensor<B, 4>, Tensor<B, 3>, Tensor<B, 3>) {
let (Y, Y_h, Y_c) = {
let (output_seq, final_state) = self.lstm1.forward(input, None);
(
output_seq.unsqueeze_dims::<4>(&[1]),
final_state.hidden.unsqueeze_dims::<3>(&[0]),
final_state.cell.unsqueeze_dims::<3>(&[0]),
)
};
(Y, Y_h, Y_c)
}
");
}
#[test]
fn test_lstm_forward_bidirectional() {
let node = create_lstm_node("lstm1", LstmDirection::Bidirectional, false, 3);
let code = codegen_forward_default(&node);
assert_snapshot!(code, @r"
pub fn forward(
&self,
input: Tensor<B, 3>,
W: Tensor<B, 3>,
R: Tensor<B, 3>,
B: Tensor<B, 2>,
) -> (Tensor<B, 4>, Tensor<B, 3>, Tensor<B, 3>) {
let (Y, Y_h, Y_c) = {
let (output_seq, final_state) = self.lstm1.forward(input, None);
(
{
let [seq_len, batch_size, _] = output_seq.dims();
let reshaped = output_seq.reshape([seq_len, batch_size, 2, 8usize]);
reshaped.swap_dims(1, 2)
},
final_state.hidden,
final_state.cell,
)
};
(Y, Y_h, Y_c)
}
");
}
#[test]
fn test_lstm_forward_reverse() {
let node = create_lstm_node("lstm1", LstmDirection::Reverse, false, 3);
let code = codegen_forward_default(&node);
// Note: reverse is now handled by the LSTM module's config, not by flip() in codegen
assert_snapshot!(code, @r"
pub fn forward(
&self,
input: Tensor<B, 3>,
W: Tensor<B, 3>,
R: Tensor<B, 3>,
B: Tensor<B, 2>,
) -> (Tensor<B, 4>, Tensor<B, 3>, Tensor<B, 3>) {
let (Y, Y_h, Y_c) = {
let (output_seq, final_state) = self.lstm1.forward(input, None);
(
output_seq.unsqueeze_dims::<4>(&[1]),
final_state.hidden.unsqueeze_dims::<3>(&[0]),
final_state.cell.unsqueeze_dims::<3>(&[0]),
)
};
(Y, Y_h, Y_c)
}
");
}
}