use crate::backend_authoring::SupportsDType;
use crate::err::{Error, Result};
use crate::exec::catalog::op;
use crate::nn::init::Init;
use crate::nn::linear::LinearShape;
use crate::nn::optional::{False, True};
use crate::nn::param::{Frozen, TrainState, Trainable};
use crate::nn::{Linear, Module, VisitParameters};
use crate::shapes::shape::{DimCons, Nil};
use crate::shapes::{Dim, Dyn, DynShape, Shape, ShapeValue};
use crate::tensor::backend::Execute;
use crate::tensor::base::Tensor;
use crate::tensor::device::Device;
use crate::tensor::dtype::DType;
use alloc::string::ToString;
use alloc::vec::Vec;
type D2<A, B> = DimCons<A, DimCons<B, Nil>>;
type D3<A, B, C> = DimCons<A, DimCons<B, DimCons<C, Nil>>>;
pub trait LstmShape: Shape + DynShape {
type In: Dim;
type Out: Dim;
type IhShape: LinearShape<InF = Self::In, OutF = Self::Out>;
type HhShape: LinearShape<InF = Self::Out, OutF = Self::Out>;
}
impl<In: Dim, Out: Dim> LstmShape
for crate::shapes::shape::DimCons<
In,
crate::shapes::shape::DimCons<Out, crate::shapes::shape::Nil>,
>
{
type In = In;
type Out = Out;
type IhShape = crate::shapes::shape::DimCons<
In,
crate::shapes::shape::DimCons<Out, crate::shapes::shape::Nil>,
>;
type HhShape = crate::shapes::shape::DimCons<
Out,
crate::shapes::shape::DimCons<Out, crate::shapes::shape::Nil>,
>;
}
impl LstmShape for Dyn {
type In = usize;
type Out = usize;
type IhShape = Dyn;
type HhShape = Dyn;
}
pub struct LSTMCellBuilder<
S: LstmShape,
BiasIh: crate::nn::optional::OptionalField = True,
BiasHh: crate::nn::optional::OptionalField = True,
Train: TrainState = Trainable,
> {
pub shape: ShapeValue<S>,
pub input_weight_init: Init,
pub hidden_weight_init: Init,
pub input_bias_init: Init,
pub hidden_bias_init: Init,
pub _phantom: core::marker::PhantomData<(BiasIh, BiasHh, Train)>,
}
impl<
S: LstmShape,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
Train: TrainState,
> LSTMCellBuilder<S, BiasIh, BiasHh, Train>
{
pub fn shape(&self) -> &ShapeValue<S> {
&self.shape
}
pub fn no_input_bias(self) -> LSTMCellBuilder<S, False, BiasHh, Train> {
LSTMCellBuilder {
shape: self.shape,
input_weight_init: self.input_weight_init,
hidden_weight_init: self.hidden_weight_init,
input_bias_init: self.input_bias_init,
hidden_bias_init: self.hidden_bias_init,
_phantom: core::marker::PhantomData,
}
}
pub fn no_hidden_bias(self) -> LSTMCellBuilder<S, BiasIh, False, Train> {
LSTMCellBuilder {
shape: self.shape,
input_weight_init: self.input_weight_init,
hidden_weight_init: self.hidden_weight_init,
input_bias_init: self.input_bias_init,
hidden_bias_init: self.hidden_bias_init,
_phantom: core::marker::PhantomData,
}
}
pub fn no_bias(self) -> LSTMCellBuilder<S, False, False, Train> {
LSTMCellBuilder {
shape: self.shape,
input_weight_init: self.input_weight_init,
hidden_weight_init: self.hidden_weight_init,
input_bias_init: self.input_bias_init,
hidden_bias_init: self.hidden_bias_init,
_phantom: core::marker::PhantomData,
}
}
pub fn frozen(self) -> LSTMCellBuilder<S, BiasIh, BiasHh, Frozen> {
LSTMCellBuilder {
shape: self.shape,
input_weight_init: self.input_weight_init,
hidden_weight_init: self.hidden_weight_init,
input_bias_init: self.input_bias_init,
hidden_bias_init: self.hidden_bias_init,
_phantom: core::marker::PhantomData,
}
}
pub fn input_weight_init(mut self, init: Init) -> Self {
self.input_weight_init = init;
self
}
pub fn hidden_weight_init(mut self, init: Init) -> Self {
self.hidden_weight_init = init;
self
}
pub fn input_bias_init(mut self, init: Init) -> Self {
self.input_bias_init = init;
self
}
pub fn hidden_bias_init(mut self, init: Init) -> Self {
self.hidden_bias_init = init;
self
}
}
pub fn lstm_cell<S: LstmShape>(shape: ShapeValue<S>) -> LSTMCellBuilder<S> {
let init = crate::nn::init::kaiming_uniform();
LSTMCellBuilder {
shape,
input_weight_init: init,
hidden_weight_init: init,
input_bias_init: init,
hidden_bias_init: init,
_phantom: core::marker::PhantomData,
}
}
pub struct LSTMBuilder<
S: LstmShape,
BiasIh: crate::nn::optional::OptionalField = True,
BiasHh: crate::nn::optional::OptionalField = True,
Train: TrainState = Trainable,
> {
pub cell: LSTMCellBuilder<S, BiasIh, BiasHh, Train>,
}
impl<
S: LstmShape,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
Train: TrainState,
> LSTMBuilder<S, BiasIh, BiasHh, Train>
{
pub fn shape(&self) -> &ShapeValue<S> {
self.cell.shape()
}
pub fn no_input_bias(self) -> LSTMBuilder<S, False, BiasHh, Train> {
LSTMBuilder {
cell: self.cell.no_input_bias(),
}
}
pub fn no_hidden_bias(self) -> LSTMBuilder<S, BiasIh, False, Train> {
LSTMBuilder {
cell: self.cell.no_hidden_bias(),
}
}
pub fn no_bias(self) -> LSTMBuilder<S, False, False, Train> {
LSTMBuilder {
cell: self.cell.no_bias(),
}
}
pub fn frozen(self) -> LSTMBuilder<S, BiasIh, BiasHh, Frozen> {
LSTMBuilder {
cell: self.cell.frozen(),
}
}
pub fn input_weight_init(mut self, init: Init) -> Self {
self.cell = self.cell.input_weight_init(init);
self
}
pub fn hidden_weight_init(mut self, init: Init) -> Self {
self.cell = self.cell.hidden_weight_init(init);
self
}
pub fn input_bias_init(mut self, init: Init) -> Self {
self.cell = self.cell.input_bias_init(init);
self
}
pub fn hidden_bias_init(mut self, init: Init) -> Self {
self.cell = self.cell.hidden_bias_init(init);
self
}
}
pub fn lstm<S: LstmShape>(shape: ShapeValue<S>) -> LSTMBuilder<S> {
LSTMBuilder {
cell: lstm_cell(shape),
}
}
#[derive(Debug, Clone)]
pub struct LSTMCell<
S: LstmShape,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField = True,
BiasHh: crate::nn::optional::OptionalField = True,
K: DType = f32,
Train: TrainState = Trainable,
> where
S::IhShape: LinearShape,
S::HhShape: LinearShape,
{
pub wi_i: Linear<S::IhShape, B, BiasIh, K, Train>,
pub wi_f: Linear<S::IhShape, B, BiasIh, K, Train>,
pub wi_g: Linear<S::IhShape, B, BiasIh, K, Train>,
pub wi_o: Linear<S::IhShape, B, BiasIh, K, Train>,
pub wh_i: Linear<S::HhShape, B, BiasHh, K, Train>,
pub wh_f: Linear<S::HhShape, B, BiasHh, K, Train>,
pub wh_g: Linear<S::HhShape, B, BiasHh, K, Train>,
pub wh_o: Linear<S::HhShape, B, BiasHh, K, Train>,
}
impl<S, B, BiasIh, BiasHh, K: DType, Train: TrainState> LSTMCell<S, B, BiasIh, BiasHh, K, Train>
where
S: LstmShape,
B: crate::tensor::backend::VariableBackend
+ SupportsDType<K>
+ crate::tensor::backend::SupportsDType<K>
+ crate::nn::param::ParameterInit<K>,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
<K as DType>::Arg: Clone,
<S::In as Dim>::Arg: Clone,
<S::Out as Dim>::Arg: Clone,
<B::Device as Device>::Arg: Clone,
<BiasIh as crate::nn::optional::OptionalField>::Arg: Clone,
<BiasHh as crate::nn::optional::OptionalField>::Arg: Clone,
{
pub fn build<A>(args: A) -> Result<Self>
where
A: crate::tensor::arg_into::LayerArgInto<(
<S::In as Dim>::Arg,
<S::Out as Dim>::Arg,
<K as DType>::Arg,
<B::Device as Device>::Arg,
<BiasIh as crate::nn::optional::OptionalField>::Arg,
<BiasHh as crate::nn::optional::OptionalField>::Arg,
)>,
{
let (input, output, dtype, device, bias_ih, bias_hh) = args.into_layer_arg();
Ok(Self {
wi_i: Linear::<_, _, _, K, Train>::build_full(
input.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_ih.clone(),
)?,
wi_f: Linear::<_, _, _, K, Train>::build_full(
input.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_ih.clone(),
)?,
wi_g: Linear::<_, _, _, K, Train>::build_full(
input.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_ih.clone(),
)?,
wi_o: Linear::<_, _, _, K, Train>::build_full(
input,
output.clone(),
dtype.clone(),
device.clone(),
bias_ih,
)?,
wh_i: Linear::<_, _, _, K, Train>::build_full(
output.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_hh.clone(),
)?,
wh_f: Linear::<_, _, _, K, Train>::build_full(
output.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_hh.clone(),
)?,
wh_g: Linear::<_, _, _, K, Train>::build_full(
output.clone(),
output.clone(),
dtype.clone(),
device.clone(),
bias_hh.clone(),
)?,
wh_o: Linear::<_, _, _, K, Train>::build_full(
output.clone(),
output,
dtype,
device,
bias_hh,
)?,
})
}
pub fn freeze(self) -> LSTMCell<S, B, BiasIh, BiasHh, K, Frozen> {
LSTMCell {
wi_i: self.wi_i.freeze(),
wi_f: self.wi_f.freeze(),
wi_g: self.wi_g.freeze(),
wi_o: self.wi_o.freeze(),
wh_i: self.wh_i.freeze(),
wh_f: self.wh_f.freeze(),
wh_g: self.wh_g.freeze(),
wh_o: self.wh_o.freeze(),
}
}
pub fn unfreeze(self) -> LSTMCell<S, B, BiasIh, BiasHh, K, Trainable> {
LSTMCell {
wi_i: self.wi_i.unfreeze(),
wi_f: self.wi_f.unfreeze(),
wi_g: self.wi_g.unfreeze(),
wi_o: self.wi_o.unfreeze(),
wh_i: self.wh_i.unfreeze(),
wh_f: self.wh_f.unfreeze(),
wh_g: self.wh_g.unfreeze(),
wh_o: self.wh_o.unfreeze(),
}
}
}
impl<
In: Dim,
Out: Dim,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
> VisitParameters<B> for LSTMCell<D2<In, Out>, B, BiasIh, BiasHh, K, Train>
where
Linear<D2<In, Out>, B, BiasIh, K, Train>: VisitParameters<B>,
Linear<D2<Out, Out>, B, BiasHh, K, Train>: VisitParameters<B>,
{
fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
&self,
path: &crate::nn::StatePath,
visitor: &mut V,
) -> crate::err::Result<()> {
self.wi_i
.visit_parameters(&path.try_child("wi_i")?, visitor)?;
self.wi_f
.visit_parameters(&path.try_child("wi_f")?, visitor)?;
self.wi_g
.visit_parameters(&path.try_child("wi_g")?, visitor)?;
self.wi_o
.visit_parameters(&path.try_child("wi_o")?, visitor)?;
self.wh_i
.visit_parameters(&path.try_child("wh_i")?, visitor)?;
self.wh_f
.visit_parameters(&path.try_child("wh_f")?, visitor)?;
self.wh_g
.visit_parameters(&path.try_child("wh_g")?, visitor)?;
self.wh_o
.visit_parameters(&path.try_child("wh_o")?, visitor)
}
}
impl<
In: Dim,
Out: Dim,
Batch: Dim,
B: crate::tensor::backend::VariableBackend
+ Execute<op::Add>
+ Execute<op::Mul>
+ Execute<op::Sigmoid>
+ Execute<op::Tanh>,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
>
Module<(
Tensor<D2<Batch, In>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
)> for LSTMCell<D2<In, Out>, B, BiasIh, BiasHh, K, Train>
where
Linear<D2<In, Out>, B, BiasIh, K, Train>:
Module<Tensor<D2<Batch, In>, B, K>, Output = Tensor<D2<Batch, Out>, B, K>, Error = Error>,
Linear<D2<Out, Out>, B, BiasHh, K, Train>:
Module<Tensor<D2<Batch, Out>, B, K>, Output = Tensor<D2<Batch, Out>, B, K>, Error = Error>,
<B as Execute<op::Add>>::Output: Into<B::Storage<K>>,
<B as Execute<op::Mul>>::Output: Into<B::Storage<K>>,
<B as Execute<op::Sigmoid>>::Output: Into<B::Storage<K>>,
<B as Execute<op::Tanh>>::Output: Into<B::Storage<K>>,
{
type Output = (Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>);
type Error = Error;
#[inline]
fn forward(
&self,
(x, (h_prev, c_prev)): (
Tensor<D2<Batch, In>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
),
) -> core::result::Result<Self::Output, Error> {
let i = self
.wi_i
.forward(x.clone())?
.add_exact(&self.wh_i.forward(h_prev.clone())?)?
.sigmoid()?;
let f = self
.wi_f
.forward(x.clone())?
.add_exact(&self.wh_f.forward(h_prev.clone())?)?
.sigmoid()?;
let g = self
.wi_g
.forward(x.clone())?
.add_exact(&self.wh_g.forward(h_prev.clone())?)?
.tanh()?;
let o = self
.wi_o
.forward(x)?
.add_exact(&self.wh_o.forward(h_prev)?)?
.sigmoid()?;
let c = f.mul_exact(&c_prev)?.add_exact(&i.mul_exact(&g)?)?;
let h = o.mul_exact(&c.clone().tanh()?)?;
Ok((h, c))
}
}
#[derive(Debug, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub struct LSTM<
S: LstmShape,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField = True,
BiasHh: crate::nn::optional::OptionalField = True,
K: DType = f32,
Train: TrainState = Trainable,
> {
pub cell: LSTMCell<S, B, BiasIh, BiasHh, K, Train>,
}
impl<
S: LstmShape,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
> LSTM<S, B, BiasIh, BiasHh, K, Train>
{
pub fn new(cell: LSTMCell<S, B, BiasIh, BiasHh, K, Train>) -> Self {
Self { cell }
}
pub fn freeze(self) -> LSTM<S, B, BiasIh, BiasHh, K, Frozen>
where
LSTMCell<S, B, BiasIh, BiasHh, K, Train>: Sized,
S: LstmShape,
B: crate::tensor::backend::VariableBackend
+ SupportsDType<K>
+ crate::tensor::backend::SupportsDType<K>
+ crate::nn::param::ParameterInit<K>,
<K as DType>::Arg: Clone,
<S::In as Dim>::Arg: Clone,
<S::Out as Dim>::Arg: Clone,
<B::Device as Device>::Arg: Clone,
<BiasIh as crate::nn::optional::OptionalField>::Arg: Clone,
<BiasHh as crate::nn::optional::OptionalField>::Arg: Clone,
{
LSTM {
cell: self.cell.freeze(),
}
}
pub fn unfreeze(self) -> LSTM<S, B, BiasIh, BiasHh, K, Trainable>
where
LSTMCell<S, B, BiasIh, BiasHh, K, Train>: Sized,
S: LstmShape,
B: crate::tensor::backend::VariableBackend
+ SupportsDType<K>
+ crate::tensor::backend::SupportsDType<K>
+ crate::nn::param::ParameterInit<K>,
<K as DType>::Arg: Clone,
<S::In as Dim>::Arg: Clone,
<S::Out as Dim>::Arg: Clone,
<B::Device as Device>::Arg: Clone,
<BiasIh as crate::nn::optional::OptionalField>::Arg: Clone,
<BiasHh as crate::nn::optional::OptionalField>::Arg: Clone,
{
LSTM {
cell: self.cell.unfreeze(),
}
}
}
impl<
In: Dim,
Out: Dim,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
> VisitParameters<B> for LSTM<D2<In, Out>, B, BiasIh, BiasHh, K, Train>
where
LSTMCell<D2<In, Out>, B, BiasIh, BiasHh, K, Train>: VisitParameters<B>,
{
fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
&self,
path: &crate::nn::StatePath,
visitor: &mut V,
) -> crate::err::Result<()> {
self.cell
.visit_parameters(&path.try_child("cell")?, visitor)
}
}
impl<
In: Dim<Arg = ()>,
Out: Dim<Arg = ()>,
Batch: Dim<Arg = ()>,
Seq: Dim<Arg = ()>,
B: crate::tensor::backend::VariableBackend
+ Execute<op::StackExact>
+ Execute<op::Narrow>
+ Execute<op::SqueezeExact>
+ crate::exec::Capabilities,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
>
Module<(
Tensor<D3<Batch, Seq, In>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
)> for LSTM<D2<In, Out>, B, BiasIh, BiasHh, K, Train>
where
<B as Execute<op::StackExact>>::Output: Into<B::Storage<K>>,
<B as Execute<op::Narrow>>::Output: Into<B::Storage<K>>,
<B as Execute<op::SqueezeExact>>::Output: Into<B::Storage<K>>,
LSTMCell<D2<In, Out>, B, BiasIh, BiasHh, K, Train>: Module<
(
Tensor<D2<Batch, In>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
),
Output = (Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
Error = Error,
>,
{
type Output = (
Tensor<D3<Batch, Seq, Out>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
);
type Error = Error;
#[inline]
fn forward(
&self,
(x, (mut h, mut c)): (
Tensor<D3<Batch, Seq, In>, B, K>,
(Tensor<D2<Batch, Out>, B, K>, Tensor<D2<Batch, Out>, B, K>),
),
) -> core::result::Result<Self::Output, Error> {
let seq_len = Seq::static_size().map_err(Error::Shape)?;
let mut outputs = Vec::with_capacity(seq_len);
for i in 0..seq_len {
let x_step = x.clone().try_narrow(1isize, i, 1)?.try_squeeze(1isize)?;
let x_step_static: Tensor<D2<Batch, In>, B, K> = x_step.into_shape()?;
let (h_next, c_next) = self.cell.forward((x_step_static, (h, c)))?;
h = h_next;
c = c_next;
outputs.push(h.clone().into_shape::<Dyn>()?);
}
let refs: Vec<&Tensor<Dyn, B, K>> = outputs.iter().collect();
let stacked_dyn = crate::tensor::ops::manipulation::try_stack_tensors(&refs, 1)?;
let stacked: Tensor<D3<Batch, Seq, Out>, B, K> = stacked_dyn.into_shape()?;
Ok((stacked, (h, c)))
}
}
impl<
S: LstmShape,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
> crate::nn::module::NamedLayers for LSTMCell<S, B, BiasIh, BiasHh, K, Train>
where
Linear<S::IhShape, B, BiasIh, K, Train>: crate::nn::module::NamedLayers,
Linear<S::HhShape, B, BiasHh, K, Train>: crate::nn::module::NamedLayers,
{
fn layer_structure(&self, prefix: &str) -> Vec<crate::nn::module::LayerNode> {
let mut children = Vec::new();
let p_wi_i = if prefix.is_empty() {
alloc::string::String::from("wi_i")
} else {
format!("{}.wi_i", prefix)
};
children.extend(self.wi_i.layer_structure(&p_wi_i));
let p_wi_f = if prefix.is_empty() {
alloc::string::String::from("wi_f")
} else {
format!("{}.wi_f", prefix)
};
children.extend(self.wi_f.layer_structure(&p_wi_f));
let p_wi_g = if prefix.is_empty() {
alloc::string::String::from("wi_g")
} else {
format!("{}.wi_g", prefix)
};
children.extend(self.wi_g.layer_structure(&p_wi_g));
let p_wi_o = if prefix.is_empty() {
alloc::string::String::from("wi_o")
} else {
format!("{}.wi_o", prefix)
};
children.extend(self.wi_o.layer_structure(&p_wi_o));
let p_wh_i = if prefix.is_empty() {
alloc::string::String::from("wh_i")
} else {
format!("{}.wh_i", prefix)
};
children.extend(self.wh_i.layer_structure(&p_wh_i));
let p_wh_f = if prefix.is_empty() {
alloc::string::String::from("wh_f")
} else {
format!("{}.wh_f", prefix)
};
children.extend(self.wh_f.layer_structure(&p_wh_f));
let p_wh_g = if prefix.is_empty() {
alloc::string::String::from("wh_g")
} else {
format!("{}.wh_g", prefix)
};
children.extend(self.wh_g.layer_structure(&p_wh_g));
let p_wh_o = if prefix.is_empty() {
alloc::string::String::from("wh_o")
} else {
format!("{}.wh_o", prefix)
};
children.extend(self.wh_o.layer_structure(&p_wh_o));
let node_name = if prefix.is_empty() {
alloc::string::String::from("LSTMCell")
} else {
prefix.to_string()
};
vec![crate::nn::module::LayerNode {
name: node_name,
type_name: alloc::string::String::from("LSTMCell"),
shape_info: "".to_string(),
children,
}]
}
}
impl<
S: LstmShape,
B: crate::tensor::backend::VariableBackend,
BiasIh: crate::nn::optional::OptionalField,
BiasHh: crate::nn::optional::OptionalField,
K: DType,
Train: TrainState,
> crate::nn::module::NamedLayers for LSTM<S, B, BiasIh, BiasHh, K, Train>
where
LSTMCell<S, B, BiasIh, BiasHh, K, Train>: crate::nn::module::NamedLayers,
{
fn layer_structure(&self, prefix: &str) -> Vec<crate::nn::module::LayerNode> {
let mut children = Vec::new();
let p_cell = if prefix.is_empty() {
alloc::string::String::from("cell")
} else {
format!("{}.cell", prefix)
};
children.extend(self.cell.layer_structure(&p_cell));
let node_name = if prefix.is_empty() {
alloc::string::String::from("LSTM")
} else {
prefix.to_string()
};
vec![crate::nn::module::LayerNode {
name: node_name,
type_name: alloc::string::String::from("LSTM"),
shape_info: "".to_string(),
children,
}]
}
}