bunsen 0.21.0

bunsen is acceleration tooling for burn
Documentation
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
//! XML/XPath reflection layer for `burner` [`burn::module::Module`]s.
//!
//! ```rust
//! use burn::{
//!     module::ParamId,
//!     nn::{
//!         Linear,
//!         LinearConfig,
//!     },
//!     prelude::Backend,
//!     tensor::Shape,
//! };
//!
//! use bunsen::{
//!     errors::{
//!         BunsenError,
//!         BunsenResult,
//!     },
//!     burner::{
//!         descriptors::{
//!             TensorKindDesc,
//!             TensorParamDesc,
//!         },
//!         module::reflection::{
//!             XML_MODULE_TREE_VERSION,
//!             XmlModuleTree,
//!             XPathModuleQuery,
//!         },
//!     },
//! };
//!
//! type B = bunsen::support::testing::SetupTestBackend;
//! let device = Default::default();
//!
//! // Create a Linear module, with a bias:
//! // * `weight` - `Param<Tensor<B, 2>>` [d_input, d_output].
//! // * `bias` - `Option<Param<Tensor<B, 1>>>` [d_output].
//! let d_input = 2;
//! let d_output = 3;
//! let module: Linear<B> = LinearConfig::new(d_input, d_output).init(&device);
//!
//! // [`TensorParamDesc`] can describe a `Param<Tensor<B, R, K>>`:
//! let weight_desc: TensorParamDesc = TensorParamDesc::from(&module.weight);
//! let bias_ref = module.bias.as_ref().unwrap();
//! let bias_desc: TensorParamDesc = TensorParamDesc::from(bias_ref);
//!
//! // [`TensorParamDesc`] exposes the basic `Param` and `Tensor` metadata:
//! assert_eq!(weight_desc.param_id(), module.weight.id);
//!
//! // [`TensorKindDesc`] is an enum which describes the current kind variants:
//! assert_eq!(weight_desc.kind(), TensorKindDesc::Float);
//! assert_eq!(weight_desc.dtype(), module.weight.dtype());
//!
//! assert_eq!(weight_desc.shape(), &module.weight.shape());
//! assert_eq!(weight_desc.shape(), &Shape::new([d_input, d_output]));
//!
//! // [`TensorParamDesc`] also provides some convience methods:
//! assert_eq!(weight_desc.rank(), 2);
//! assert_eq!(weight_desc.num_elements(), 2 * 3);
//! assert_eq!(
//!     weight_desc.num_elements(),
//!     weight_desc.shape().num_elements()
//! );
//!
//! // This is a rough size-estimate of the buffer size used by the parameter.
//! assert_eq!(
//!     weight_desc.size_estimate(),
//!     module.weight.dtype().size() * 2 * 3
//! );
//!
//! // Build an XmlModuleTree from the module.
//! // As the XmlModuleTree holds a non-Send active query environment,
//! // it must be `mut` to be useful.
//! let mut mtree = XmlModuleTree::build(&module);
//!
//! // [`XmlModuleTree`] builds an XML meta-description of the module structure.
//! //
//! // This can be dumped directly to a `String` to examine the module structure.
//! //
//! // The XPath expressions used by query api all all written in terms of this
//! // structure; though they start with `/Module/Structure` as their implied
//! // context.
//! //
//! // The module structure is embedded in the wrapping elements to provide
//! // a pathway to future metadata extension.
//! //
//! // # @id - Document-Unique Id
//! // Every structural element has a document-unique id attribute, which can be
//! // used to reference the element in the XML.
//! //
//! // # <{NAME} class="{CLASS}"/> - Structural Element
//! // Structural elements are given a {NAME} and {CLASS} in the local namespace,
//! // derived from the [`burner::module::ModuleVisitor::enter_module`]
//! // `container_type`.
//! // * `{TYPE}` => NAME=TYPE, CLASS='builtin'
//! // * `{C}:{TYPE}` => NAME=TYPE, CLASS=lowercase(C)
//! //
//! // # @class - Element Class
//! // Structural elements derive their class from their `container_type`;
//! // while `Param` elements are (currently) always "tensor".
//! //
//! // # @name - The structural field name.
//! // If an element is a named field of a "struct"-class parent,
//! // then it will have a `@name` attribute.
//! assert_eq!(
//!     mtree.to_xml(true),
//!     indoc::formatdoc! {r#"
//!             <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()),
//!         }
//! );
//!
//! // [`XmlModuleTree`] has a Debug impl:
//! 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()),
//!         }
//! );
//!
//! // [`XmlModuleTree::param_ids`] iterates over all [`ParamId`]s.
//! //
//! // This is a useful way to get all the parameter ids in a module;
//! // but it is actually a wrapper over a series of more complex steps.
//! //
//! //   let ids: Vec<ParamId> = mtree
//! //       .query()
//! //       // .params() is implicit to [`XPathModuleQuery::to_param_ids`],
//! //       // equivalent to: .select("descendant-or-self::Param")
//! //       .to_param_ids()?
//! //       .collect();
//! let module_param_ids: Vec<ParamId> = mtree.param_ids()?;
//!
//! // IMPORTANT: Module Tree Ordering
//! //
//! // `burner` Modules order their children in a stable and specific order,
//! // determined by the order of their declaration in the source code,
//! // and the current semantics of the `Module` derive macro.
//! //
//! // Where possible, you should not rely upon this; and should prefer
//! // to use `HashSet<ParamId>` or similar to shield yourself from
//! // ordering variation; particularly as you'll generally be using
//! // this machinery when doing subset calculations.
//! assert_eq!(
//!     module_param_ids,
//!     [module.weight.id, module.bias.as_ref().unwrap().id]
//! );
//!
//! // [`XPathModuleQuery::to_param_ids`] iterates over [`ParamId`]s for
//! // each parameter in the subtree.
//! assert_eq!(
//!     &mtree.query().to_param_ids()?,
//!     &module_param_ids,
//! );
//!
//! // [`XmlModuleTree::param_descs`] iterates over descriptions of every parameter.
//! //
//! // This leverages the [`TensorParamDesc`] API to strip generics from
//! // the introspection api.
//! //
//! // Similar to [`XmlModuleTree::param_ids`], this is a wrapper over a series of more
//! // complex steps.
//! //
//! //   let descs: Vec<ParamDesc<TensorDesc>> = mtree
//! //       .query()
//! //       // .params() is implicit to [`XPathModuleQuery::to_param_descs`],
//! //       // equivalent to: .select("descendant-or-self::Param")
//! //       .to_param_descs()?
//! //       .collect();
//! let module_param_descs: Vec<TensorParamDesc> = mtree.param_descs()?;
//! assert_eq!(
//!     &module_param_descs,
//!     &vec![weight_desc.clone(), bias_desc.clone()]
//! );
//!
//! // [`XPathModuleQuery::to_param_descs`] iterates over
//! // [`TensorParamDesc`]s for each parameter in the subtree.
//! assert_eq!(
//!         &mtree
//!             .query()
//!             .to_param_descs()?,
//!         &module_param_descs,
//!     );
//!
//! // The query api is designed to be fluent and chainable.
//! //
//! // The [`XPathModuleQuery<'a>`] captures a borrow of the module tree,
//! // so you'll need to resolve the borrow before running another query.
//! let mut query: XPathModuleQuery<'_> = mtree.query();
//!
//! // We can introspect on the current XPath expression being accumulated
//! // by a query by calling `expr()`.
//! assert_eq!(query.expr(), "/XmlModuleTree/Structure");
//!
//! // [`XPathModuleQuery`] has a Debug impl:
//! assert_eq!(
//!     format!("{:#?}", query),
//!     indoc::formatdoc! {r#"
//!         XPathModuleQuery {{
//!             tree: 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>
//!             }},
//!             expr: "/XmlModuleTree/Structure",
//!         }}"#,
//!         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()),
//!         }
//! );
//!
//! // We can collect the current query results as XML fragments.
//! // This is primarily useful for debugging.
//! //
//! // Initially, this will be the root Module node.
//! assert_eq!(
//!         &query.to_fragments(true)?,
//!         &[indoc::formatdoc! {r#"
//!             <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>"#,
//!             weight_id = weight_desc.param_id(),
//!             weight_dtype = format!("{:?}", weight_desc.dtype()),
//!             bias_id = bias_desc.param_id(),
//!             bias_dtype = format!("{:?}", bias_desc.dtype()),
//!         },],
//!     );
//!
//! // The [`XPathModuleQuery::params`] method selects all the `Param` elements
//! // in the current subtree.
//! let mut query = mtree.query().params();
//! assert_eq!(
//!     query.expr(),
//!     "/XmlModuleTree/Structure/descendant-or-self::Param"
//! );
//! assert_eq!(
//!     &query.to_fragments(false)?,
//!     &[
//!         format!(
//!             r#"<Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/>"#,
//!             weight_id = weight_desc.param_id(),
//!             weight_dtype = format!("{:?}", weight_desc.dtype()),
//!         ),
//!         format!(
//!             r#"<Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/>"#,
//!             bias_id = bias_desc.param_id(),
//!             bias_dtype = format!("{:?}", bias_desc.dtype()),
//!         )
//!     ],
//! );
//!
//! // A full coverage of the XPath language cannot be included here.
//! // For more details, see: <https://en.wikipedia.org/wiki/XPath>
//!
//! // The structural elements start at '/XmlModuleTree/Structure/$Elem'.
//! // But there's only every exactly one root node (currently).
//! //
//! // We can select this using either:
//! // - the element selector (here, "Linear").
//! // - the wildcard selector ('*').
//! // - (a bunch of other, longer XPath operators).
//! //
//! // "Linear":
//! // - Select the root 'Linear' node,
//! let mut query = mtree.query().select("Linear");
//! assert_eq!(query.expr(), "/XmlModuleTree/Structure/Linear");
//! assert_eq!(
//!     &query.to_fragments(true)?,
//!     &[indoc::formatdoc! {r#"
//!         <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>"#,
//!         weight_id = weight_desc.param_id(),
//!         weight_dtype = format!("{:?}", weight_desc.dtype()),
//!         bias_id = bias_desc.param_id(),
//!         bias_dtype = format!("{:?}", bias_desc.dtype()),
//!     },],
//! );
//!
//! // Here's the same thing using the wildcard selector:
//! //
//! // "*":
//! // - Select the root's children, which is only the 'Linear' node
//! let mut query = mtree.query().select("*");
//! assert_eq!(query.expr(), "/XmlModuleTree/Structure/*");
//! assert_eq!(
//!     &query.to_fragments(true)?,
//!     &[indoc::formatdoc! {r#"
//!         <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>"#,
//!         weight_id = weight_desc.param_id(),
//!         weight_dtype = format!("{:?}", weight_desc.dtype()),
//!         bias_id = bias_desc.param_id(),
//!         bias_dtype = format!("{:?}", bias_desc.dtype()),
//!     },],
//! );
//!
//! // We can select specific names of structural elements by name,
//! // using an attribute predicated `[@name='name']`.
//! //
//! // "Linear/*[@name='weight']":
//! // - Select the root 'Linear' node,
//! // - Select all the children of 'Linear',
//! // - Filter those to elements with the attribte 'name' set to 'weight'.
//! //
//! // The "expr[predicate,...]" syntax is used to write filters,
//! // the selected values in `{expr}' are restricted to those where all
//! // of the predicates are true.
//! let mut query = mtree.query().select("Linear/*[@name='weight']");
//! assert_eq!(
//!     query.expr(),
//!     "/XmlModuleTree/Structure/Linear/*[@name='weight']"
//! );
//! assert_eq!(
//!     &query.to_fragments(false)?,
//!     &[format!(
//!         r#"<Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/>"#,
//!         weight_id = weight_desc.param_id(),
//!         weight_dtype = format!("{:?}", weight_desc.dtype()),
//!     ),],
//! );
//!
//! // We can also select the children of elements by their index.
//! // Note: XPath indexes from 1, not 0.
//! //
//! // The children of sequences in the "bulitins" class ('Tuple', 'Vec', 'Array')
//! // don't have names, but do have positional indices in XPath.
//! //
//! // "Linear/*[2]":
//! // - Select the root 'Linear' node,
//! // - Select all the children of 'Linear',
//! // - Select the 2nd (indexing from 1) child.
//! let mut query = mtree.query().select("Linear/*[2]");
//! assert_eq!(query.expr(), "/XmlModuleTree/Structure/Linear/*[2]");
//! assert_eq!(
//!     &query.to_fragments(false)?,
//!     &[format!(
//!         r#"<Param id="n:3" name="bias" param_id="{bias_id}" class="tensor" kind="Float" dtype="{bias_dtype}" shape="3" rank="1"/>"#,
//!         bias_id = bias_desc.param_id(),
//!         bias_dtype = format!("{:?}", bias_desc.dtype()),
//!     )],
//! );
//!
//! // In general:
//! // - `query.select(expr)` appends "/expr" to the current query expression.
//! // - `query.filter(expr)` appends "[expr]" to the current query expression.
//! //
//! // `query.params()` may seem superflous, as both `.to_param_ids()` and
//! // `.to_param_descs()` Implictly call `.params()`.
//! //
//! // However, when used in conjunction with `.filter()`, we can write powerful
//! // selection expressions.
//! let mut query = mtree.query().params().filter("@rank=2");
//! assert_eq!(
//!     query.expr(),
//!     "/XmlModuleTree/Structure/descendant-or-self::Param[@rank=2]"
//! );
//! assert_eq!(
//!     &query.to_fragments(false)?,
//!     &[format!(
//!         r#"<Param id="n:2" name="weight" param_id="{weight_id}" class="tensor" kind="Float" dtype="{weight_dtype}" shape="2 3" rank="2"/>"#,
//!         weight_id = weight_desc.param_id(),
//!         weight_dtype = format!("{:?}", weight_desc.dtype()),
//!     ),],
//! );
//!
//! // Many structural builtin components are also Modules.
//! let module = (
//!     LinearConfig::new(2, 3).init::<B>(&device),
//!     [LinearConfig::new(4, 5).init::<B>(&device)],
//!     vec![
//!         LinearConfig::new(6, 7).init::<B>(&device),
//!         LinearConfig::new(8, 9).init::<B>(&device),
//!     ],
//! );
//! let expected_dtype = module.0.weight.dtype();
//! let dtype_str = format!("{:?}", expected_dtype);
//! // So we can still walk these module:
//! let mut mtree = XmlModuleTree::build(&module);
//! assert_eq!(
//!         &mtree.query().to_fragments(true)?,
//!         &[indoc::formatdoc! {r#"
//!             <Structure>
//!               <Tuple id="n:1" class="builtin">
//!                 <Linear id="n:2" class="struct">
//!                   <Param id="n:3" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="2 3" rank="2"/>
//!                   <Param id="n:4" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="3" rank="1"/>
//!                 </Linear>
//!                 <Array id="n:5" class="builtin">
//!                   <Linear id="n:6" class="struct">
//!                     <Param id="n:7" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="4 5" rank="2"/>
//!                     <Param id="n:8" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="5" rank="1"/>
//!                   </Linear>
//!                 </Array>
//!                 <Vec id="n:9" class="builtin">
//!                   <Linear id="n:A" class="struct">
//!                     <Param id="n:B" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="6 7" rank="2"/>
//!                     <Param id="n:C" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="7" rank="1"/>
//!                   </Linear>
//!                   <Linear id="n:D" class="struct">
//!                     <Param id="n:E" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="8 9" rank="2"/>
//!                     <Param id="n:F" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="9" rank="1"/>
//!                   </Linear>
//!                 </Vec>
//!               </Tuple>
//!             </Structure>"#,
//!             module.0.weight.id,
//!             module.0.bias.as_ref().unwrap().id,
//!             module.1[0].weight.id,
//!             module.1[0].bias.as_ref().unwrap().id,
//!             module.2[0].weight.id,
//!             module.2[0].bias.as_ref().unwrap().id,
//!             module.2[1].weight.id,
//!             module.2[1].bias.as_ref().unwrap().id,
//!             dtype=dtype_str,
//!         }],
//!     );
//!
//! // We could select all the `Linear` descendants:
//! let mut query = mtree.select("*//Linear");
//! assert_eq!(query.expr(), "/XmlModuleTree/Structure/*//Linear");
//! assert_eq!(
//!     &query.to_fragments(true)?,
//!     &[
//!         indoc::formatdoc! {r#"
//!           <Linear id="n:2" class="struct">
//!             <Param id="n:3" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="2 3" rank="2"/>
//!             <Param id="n:4" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="3" rank="1"/>
//!           </Linear>"#,
//!             module.0.weight.id,
//!             module.0.bias.as_ref().unwrap().id,
//!             dtype=dtype_str,
//!         },
//!         indoc::formatdoc! {r#"
//!           <Linear id="n:6" class="struct">
//!             <Param id="n:7" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="4 5" rank="2"/>
//!             <Param id="n:8" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="5" rank="1"/>
//!           </Linear>"#,
//!             module.1[0].weight.id,
//!             module.1[0].bias.as_ref().unwrap().id,
//!             dtype=dtype_str,
//!         },
//!         indoc::formatdoc! {r#"
//!           <Linear id="n:A" class="struct">
//!             <Param id="n:B" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="6 7" rank="2"/>
//!             <Param id="n:C" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="7" rank="1"/>
//!           </Linear>"#,
//!             module.2[0].weight.id,
//!             module.2[0].bias.as_ref().unwrap().id,
//!             dtype=dtype_str,
//!         },
//!         indoc::formatdoc! {r#"
//!           <Linear id="n:D" class="struct">
//!             <Param id="n:E" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="8 9" rank="2"/>
//!             <Param id="n:F" name="bias" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="9" rank="1"/>
//!           </Linear>"#,
//!             module.2[1].weight.id,
//!             module.2[1].bias.as_ref().unwrap().id,
//!             dtype=dtype_str,
//!         },
//!     ],
//! );
//!
//! // Returning to the power of .params().filter("@rank=2"), we can see the effect
//! // on complex module:
//! let mut query = mtree.query().params().filter("@rank=2");
//! assert_eq!(
//!     query.expr(),
//!     "/XmlModuleTree/Structure/descendant-or-self::Param[@rank=2]"
//! );
//! assert_eq!(
//!         &query.to_fragments(false)?,
//!         &[
//!             format!(
//!                 r#"<Param id="n:3" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="2 3" rank="2"/>"#,
//!                 module.0.weight.id,
//!                 dtype = dtype_str,
//!             ),
//!             format!(
//!                 r#"<Param id="n:7" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="4 5" rank="2"/>"#,
//!                 module.1[0].weight.id,
//!                 dtype = dtype_str,
//!             ),
//!             format!(
//!                 r#"<Param id="n:B" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="6 7" rank="2"/>"#,
//!                 module.2[0].weight.id,
//!                 dtype = dtype_str,
//!             ),
//!             format!(
//!                 r#"<Param id="n:E" name="weight" param_id="{}" class="tensor" kind="Float" dtype="{dtype}" shape="8 9" rank="2"/>"#,
//!                 module.2[1].weight.id,
//!                 dtype = dtype_str,
//!             ),
//!         ],
//!     );
//!
//! Ok::<(), BunsenError>(())
//! ```

pub mod module_visitors;
pub mod xml_support;

mod xml_module_tree;
#[doc(inline)]
pub use xml_module_tree::*;