#![allow(unused_imports)]
use core::{
fmt::{
Debug,
Display,
},
str::FromStr,
};
use burn::{
module::{
Module,
ModuleVisitor,
ParamId,
},
nn::{
Linear,
LinearConfig,
},
};
use xee_xpath::{
Documents,
Itemable,
Queries,
Query,
error::{
ErrorValue,
Result as SpannedResult,
},
query::Convert,
};
use xot::{
NameId,
Node,
Xot,
};
use crate::{
burner::{
descriptors::TensorParamDesc,
module::reflection::{
module_visitors::XmlModuleTreeBuilder,
xml_support::{
adapt_xee_error,
names,
node_to_tensor_param_desc,
},
},
},
errors::BunsenResult,
};
pub const XML_MODULE_TREE_VERSION: &str = env!("CARGO_PKG_VERSION");
pub struct XmlModuleTree {
docs: Documents,
root: Node,
}
impl Debug for XmlModuleTree {
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
f.write_str("XmlModuleTree {")?;
if f.alternate() {
f.write_str("\n")?;
for line in self.to_xml(true).lines() {
writeln!(f, " {line}")?;
}
} else {
f.write_str(self.to_xml(false).as_str())?;
}
f.write_str("}")
}
}
impl XmlModuleTree {
pub fn build<M: Module>(module: &M) -> Self {
XmlModuleTreeBuilder::build(module)
}
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let mut docs = Documents::new();
let xot = docs.xot_mut();
let mtree_nid = xot.add_name(names::XML_MODULE_TREE_ELEM);
let root = xot.new_element(mtree_nid);
let _doc = xot.new_document_with_element(root).unwrap();
let version_nid = xot.add_name("version");
xot.set_attribute(root, version_nid, XML_MODULE_TREE_VERSION);
Self { docs, root }
}
pub fn to_xml(
&self,
pretty: bool,
) -> String {
self.docs
.xot()
.serialize_xml_string(
xot::output::xml::Parameters {
indentation: if pretty {
Some(Default::default())
} else {
None
},
..Default::default()
},
self.root,
)
.unwrap()
}
pub fn bind_local_name(
&mut self,
name: &str,
) -> NameId {
let xot = self.xot_mut();
xot.add_name(name)
}
pub fn bind_local_names<const N: usize>(
&mut self,
names: [&str; N],
) -> [NameId; N] {
names.map(|name| self.bind_local_name(name))
}
pub fn root(&self) -> Node {
self.root
}
pub fn docs(&self) -> &Documents {
&self.docs
}
pub fn docs_mut(&mut self) -> &mut Documents {
&mut self.docs
}
pub fn xot(&self) -> &Xot {
self.docs.xot()
}
pub fn xot_mut(&mut self) -> &mut Xot {
self.docs.xot_mut()
}
pub fn param_ids(&mut self) -> BunsenResult<Vec<ParamId>> {
self.query().to_param_ids()
}
pub fn param_descs(&mut self) -> BunsenResult<Vec<TensorParamDesc>> {
self.query().to_param_descs()
}
pub fn query<'a>(&'a mut self) -> XPathModuleQuery<'a> {
XPathModuleQuery::new(
self,
format!("/{}/{}", names::XML_MODULE_TREE_ELEM, names::STRUCTURE_ELEM),
)
}
pub fn select<'a>(
&'a mut self,
expr: &str,
) -> XPathModuleQuery<'a> {
self.query().select(expr)
}
pub fn try_select<'a>(
&'a mut self,
expr: &str,
) -> BunsenResult<XPathModuleQuery<'a>> {
self.query().try_select(expr)
}
pub fn select_params<'a>(
&'a mut self,
expr: &str,
) -> XPathModuleQuery<'a> {
self.select(expr).params()
}
pub fn try_select_params<'a>(
&'a mut self,
expr: &str,
) -> BunsenResult<XPathModuleQuery<'a>> {
Ok(self.try_select(expr)?.params())
}
pub fn select_param_ids(
&mut self,
expr: &str,
) -> BunsenResult<Vec<ParamId>> {
self.try_select_params(expr)?.to_param_ids()
}
}
#[derive(Debug)]
pub struct XPathModuleQuery<'a> {
tree: &'a mut XmlModuleTree,
expr: String,
}
impl<'a> XPathModuleQuery<'a> {
fn new(
tree: &'a mut XmlModuleTree,
expr: String,
) -> Self {
Self { tree, expr }
}
pub fn expr(&self) -> &String {
&self.expr
}
fn try_append_expr(
self,
expr: &str,
) -> BunsenResult<Self> {
let expr = format!("{}{}", self.expr, expr);
Queries::default()
.many(&expr, |_, _| Ok(()))
.map_err(|e| adapt_xee_error(e, Some(&expr)))?;
Ok(Self { expr, ..self })
}
pub fn xee_execute_many<V, F>(
&mut self,
f: F,
) -> BunsenResult<Vec<V>>
where
F: Convert<V>,
{
let expr = &self.expr;
let root = self.tree.root;
Queries::default()
.many(expr, f)
.map_err(|e| adapt_xee_error(e, Some(expr)))?
.execute(&mut self.tree.docs, root)
.map_err(|e| adapt_xee_error(e, Some(expr)))
}
pub fn select<S: AsRef<str>>(
self,
expr: S,
) -> XPathModuleQuery<'a> {
self.try_select(expr).unwrap_or_else(|e| panic!("{}", e))
}
pub fn try_select<S: AsRef<str>>(
self,
expr: S,
) -> BunsenResult<XPathModuleQuery<'a>> {
self.try_append_expr(format!("/{}", expr.as_ref()).as_str())
}
pub fn filter<S: AsRef<str>>(
self,
pred: S,
) -> XPathModuleQuery<'a> {
self.try_filter(pred).unwrap_or_else(|e| panic!("{}", e))
}
pub fn try_filter<S: AsRef<str>>(
self,
pred: S,
) -> BunsenResult<XPathModuleQuery<'a>> {
self.try_append_expr(format!("[{}]", pred.as_ref()).as_str())
}
pub fn children(self) -> XPathModuleQuery<'a> {
self.select("*")
}
pub fn named_children(
self,
name: &str,
) -> XPathModuleQuery<'a> {
self.select(format!("*[@name='{name}']"))
}
pub fn indexed_children(
self,
index: usize,
) -> XPathModuleQuery<'a> {
self.select(format!("*[{}]", index + 1))
}
pub fn subtree_elements(
self,
name: &str,
) -> XPathModuleQuery<'a> {
self.select(format!("descendant-or-self::{}", name))
}
pub fn params(self) -> Self {
self.subtree_elements(names::PARAM_ELEM)
}
pub fn rank(
self,
rank: usize,
) -> Self {
self.filter(format!("@rank={}", rank))
}
pub fn to_param_descs(self) -> BunsenResult<Vec<TensorParamDesc>> {
let mut query = self.params();
let nodes: Vec<Node> = query.xee_execute_many(|_, item| Ok(item.to_node()?))?;
let xot = query.tree.docs.xot();
nodes
.into_iter()
.map(|node| node_to_tensor_param_desc(xot, node))
.collect::<BunsenResult<Vec<TensorParamDesc>>>()
}
pub fn to_param_ids(self) -> BunsenResult<Vec<ParamId>> {
Ok(self
.to_param_descs()?
.iter()
.map(|d| d.param_id())
.collect())
}
pub fn to_fragments(
&mut self,
pretty: bool,
) -> BunsenResult<Vec<String>> {
use xee_xpath::Item;
let output_params = xot::output::xml::Parameters {
indentation: if pretty {
Some(Default::default())
} else {
None
},
..Default::default()
};
let fragments = self.xee_execute_many(
|docs: &mut Documents, item: &Item| -> SpannedResult<String> {
let xot: &xot::Xot = docs.xot();
match item {
Item::Node(node) => xot
.serialize_xml_string(output_params.clone(), *node)
.map(|s| s.trim().to_string())
.map_err(|e| ErrorValue::from(e).into()),
_ => Ok(item.string_value(xot)?),
}
},
)?;
Ok(fragments)
}
}
#[cfg(test)]
#[allow(unused)]
mod tests {
use burn::nn::{
Dropout,
DropoutConfig,
};
use super::*;
use crate::{
burner::descriptors::TensorParamDesc,
support::testing::cpu_device,
};
#[derive(Module, Debug)]
struct Block {
linear: Linear,
dropout: Dropout,
}
#[test]
fn test_flags_are_not_shown() {
let device = cpu_device();
let module = Block {
linear: LinearConfig::new(2, 3).init(&device),
dropout: DropoutConfig::new(0.1).init(),
};
let mut mtree = XmlModuleTree::build(&module);
assert_eq!(
mtree.to_xml(true),
indoc::formatdoc! {r#"
<XmlModuleTree version="{XML_MODULE_TREE_VERSION}">
<Structure>
<Block id="n:1" class="struct">
<Linear id="n:2" name="linear" class="struct">
<Param id="n:3" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{dtype}" shape="2 3" rank="2"/>
<Param id="n:4" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{dtype}" shape="3" rank="1"/>
</Linear>
<Dropout id="n:5" name="dropout" class="struct"/>
</Block>
</Structure>
</XmlModuleTree>
"#,
weight_id = module.linear.weight.id,
bias_id = module.linear.bias.as_ref().unwrap().id,
dtype = format!("{:?}", module.linear.weight.dtype()),
}
);
assert_eq!(
mtree.param_ids().unwrap(),
[
module.linear.weight.id,
module.linear.bias.as_ref().unwrap().id
]
);
assert!(mtree.select_param_ids("Block/Dropout").unwrap().is_empty());
}
#[test]
fn test_predicate_conjunction() {
let device = cpu_device();
let module: Linear = LinearConfig::new(2, 3).init(&device);
let mut mtree = XmlModuleTree::build(&module);
for expr in [
"Linear/*[@name='weight'][@rank=2]",
"Linear/*[@name='weight' and @rank=2]",
] {
assert_eq!(mtree.select_param_ids(expr).unwrap(), [module.weight.id]);
}
assert!(
mtree
.select_param_ids("Linear/*[@name='bias'][@rank=2]")
.unwrap()
.is_empty()
);
for expr in [
"Linear/*[@name='weight',@rank=2]",
"Linear/*[@name='bias',@rank=2]",
] {
assert!(mtree.try_select(expr).is_ok());
let err = mtree.select_param_ids(expr).unwrap_err();
assert!(err.to_string().contains("XPTY0004"), "{err}");
}
assert!(
mtree
.select_param_ids("NoSuchType/*[@name='weight',@rank=2]")
.unwrap()
.is_empty()
);
}
#[test]
fn test_debug() {
let device = cpu_device();
let module: Linear = LinearConfig::new(2, 3).init(&device);
let weight_desc: TensorParamDesc = TensorParamDesc::from(&module.weight);
let bias_ref = module.bias.as_ref().unwrap();
let bias_desc: TensorParamDesc = TensorParamDesc::from(bias_ref);
let mut mtree = XmlModuleTree::build(&module);
assert_eq!(
format!("{:#?}", mtree),
indoc::formatdoc! {r#"
XmlModuleTree {{
<XmlModuleTree version="{version}">
<Structure>
<Linear id="n:1" class="struct">
<Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/>
<Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/>
</Linear>
</Structure>
</XmlModuleTree>
}}"#,
version=XML_MODULE_TREE_VERSION,
weight_id = weight_desc.param_id(),
weight_dtype = format!("{:?}", weight_desc.dtype()),
bias_id = bias_desc.param_id(),
bias_dtype = format!("{:?}", bias_desc.dtype()),
}
);
assert_eq!(
format!("{:?}", mtree),
indoc::formatdoc! {r#"XmlModuleTree {{<XmlModuleTree version="{XML_MODULE_TREE_VERSION}"><Structure><Linear id="n:1" class="struct"><Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/><Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/></Linear></Structure></XmlModuleTree>}}"#,
weight_id = weight_desc.param_id(),
weight_dtype = format!("{:?}", weight_desc.dtype()),
bias_id = bias_desc.param_id(),
bias_dtype = format!("{:?}", bias_desc.dtype()),
}
);
}
#[test]
fn test_to_xml() {
let device = cpu_device();
let module: Linear = LinearConfig::new(2, 3).init(&device);
let weight_desc: TensorParamDesc = TensorParamDesc::from(&module.weight);
let bias_ref = module.bias.as_ref().unwrap();
let bias_desc: TensorParamDesc = TensorParamDesc::from(bias_ref);
let mut mtree = XmlModuleTree::build(&module);
assert_eq!(
mtree.to_xml(true),
indoc::formatdoc! {r#"
<XmlModuleTree version="{version}">
<Structure>
<Linear id="n:1" class="struct">
<Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/>
<Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/>
</Linear>
</Structure>
</XmlModuleTree>
"#,
version=XML_MODULE_TREE_VERSION,
weight_id = weight_desc.param_id(),
weight_dtype = format!("{:?}", weight_desc.dtype()),
bias_id = bias_desc.param_id(),
bias_dtype = format!("{:?}", bias_desc.dtype()),
}
);
assert_eq!(
mtree.to_xml(false),
indoc::formatdoc! {r#"<XmlModuleTree version="{version}"><Structure><Linear id="n:1" class="struct"><Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/><Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/></Linear></Structure></XmlModuleTree>"#,
version=XML_MODULE_TREE_VERSION,
weight_id = weight_desc.param_id(),
weight_dtype = format!("{:?}", weight_desc.dtype()),
bias_id = bias_desc.param_id(),
bias_dtype = format!("{:?}", bias_desc.dtype()),
}
);
}
}