Skip to content

Commit 7c2725f

Browse files
committed
Fix LSTM clipping before h activation (Issue #27224)
- Add clipping of Ct before applying h activation function - Complies with ONNX spec requirement for clipping input of activations - Equation: Ht = ot (.) h(clip(Ct)) instead of Ht = ot (.) h(Ct)
1 parent a3e477e commit 7c2725f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

onnxruntime/core/providers/cpu/rnn/uni_directional_lstm.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,14 @@ void UniDirectionalLstm<T>::GateComputations(
578578
float* pC_prev_clipped = SafeRawPointer<T>(C_prev_clipped + b * hidden_size_, C_prev_clipped_end, hidden_size_);
579579
#endif
580580

581-
activation_h_.func(pC_cur, pC_prev_clipped, po, pH, hidden_size_, activation_h_.alpha, activation_h_.beta);
581+
// Clip Ct before applying h activation function (per ONNX spec)
582+
// Copy Ct to temporary buffer and apply clipping
583+
for (int i = 0; i < hidden_size_; ++i) {
584+
pC_prev_clipped[i] = pC_cur[i];
585+
}
586+
clip_with_bias_ptr_(clip_, nullptr, pC_prev_clipped, hidden_size_);
587+
588+
activation_h_.func(pC_prev_clipped, pC_prev_clipped, po, pH, hidden_size_, activation_h_.alpha, activation_h_.beta);
582589

583590
// DumpMatrix("H" + row_str, pH, 1, hidden_size_);
584591
}

0 commit comments

Comments
 (0)