use burn::store::burn_pack::Tensor as PackTensor;
use super::{
LayaAgentConfig,
LayaConfig,
LayaModel,
};
use crate::{
blocks::transformers::encoders::residual::ResidualEncoderAttentionBlock,
burner::module::{
CanonicalKeys,
Rekey,
},
data::formats::{
NamedTensorsApplied,
split_rows,
},
errors::BunsenResult,
kits::text::encoders::modernbert::{
ModernBertConfig,
weights::{
transformers_rekey as modernbert_rekey,
transformers_row_splits as modernbert_row_splits,
},
},
};
pub fn laya_config(
encoder: ModernBertConfig,
agent: &LayaAgentConfig,
) -> LayaConfig {
LayaConfig::new(encoder)
.with_head_layers(agent.head_layers)
.with_n_act(agent.n_act())
}
pub fn laya_rekey() -> Rekey {
Rekey::drop_rest()
.mount("encoder.", "encoder.", modernbert_rekey())
.mount_indexed(
"head.layers.",
"head.",
ResidualEncoderAttentionBlock::torch_rekey(),
)
.rename("type_emb.", "type_emb.")
.rename("scorer.0.", "scorer.norm.")
.rename("scorer.1.", "scorer.dense.")
.rename("scorer.3.", "scorer.out.")
.rename("act_head.0.", "act_head.dense.")
.rename("act_head.2.", "act_head.out.")
}
pub fn load_laya_tensors(
model: &mut LayaModel,
tensors: impl IntoIterator<Item = PackTensor>,
dtype: Option<burn::tensor::FloatDType>,
) -> BunsenResult<NamedTensorsApplied> {
let splits: Vec<_> = modernbert_row_splits()
.into_iter()
.chain(ResidualEncoderAttentionBlock::torch_row_splits())
.collect();
let tensors = split_rows(tensors, &splits)?;
let rekey = laya_rekey().then(model.canonical_rekey());
rekey.load(model, tensors, dtype)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_laya_rekey() {
let rekey = laya_rekey();
for (stored, bunsen) in [
(
"encoder.layers.3.attn.Wq.weight",
"encoder.stack.layers.3.attn.c_q.weight",
),
("encoder.final_norm.weight", "encoder.stack.norm.weight"),
(
"head.layers.1.self_attn.q_proj.bias",
"head.1.attn.query.bias",
),
(
"head.layers.0.self_attn.out_proj.weight",
"head.0.attn.output.weight",
),
("head.layers.0.norm1.weight", "head.0.attn_norm.weight"),
("head.layers.0.norm2.bias", "head.0.mlp_norm.bias"),
("head.layers.1.linear2.weight", "head.1.mlp.linear2.weight"),
("type_emb.weight", "type_emb.weight"),
("scorer.0.weight", "scorer.norm.weight"),
("scorer.3.bias", "scorer.out.bias"),
("act_head.2.weight", "act_head.out.weight"),
] {
assert_eq!(rekey.apply(stored).as_deref(), Some(bunsen), "{stored}");
}
assert_eq!(rekey.apply("temperature"), None);
}
}