trustformers_models/developer_tools/
template_engine.rs1use anyhow::{Error, Result};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use std::path::Path;
9
10pub struct TemplateEngine {
12 templates: HashMap<String, Template>,
13 variables: HashMap<String, String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct Template {
19 pub name: String,
21 pub content: String,
23 pub required_variables: Vec<String>,
25 pub optional_variables: HashMap<String, String>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct TemplateContext {
32 pub variables: HashMap<String, String>,
34 pub conditions: HashMap<String, bool>,
36 pub loops: HashMap<String, Vec<HashMap<String, String>>>,
38}
39
40impl TemplateEngine {
41 pub fn new() -> Self {
43 Self {
44 templates: HashMap::new(),
45 variables: HashMap::new(),
46 }
47 }
48
49 pub fn add_template(&mut self, template: Template) {
51 self.templates.insert(template.name.clone(), template);
52 }
53
54 pub fn set_variable(&mut self, key: String, value: String) {
56 self.variables.insert(key, value);
57 }
58
59 pub fn render(&self, template_name: &str, context: &TemplateContext) -> Result<String> {
61 let template = self
62 .templates
63 .get(template_name)
64 .ok_or_else(|| Error::msg(format!("Template '{}' not found", template_name)))?;
65
66 for required_var in &template.required_variables {
68 if !context.variables.contains_key(required_var)
69 && !self.variables.contains_key(required_var)
70 {
71 return Err(Error::msg(format!(
72 "Required variable '{}' not provided",
73 required_var
74 )));
75 }
76 }
77
78 let mut rendered = template.content.clone();
79
80 for (key, value) in &context.variables {
82 rendered = rendered.replace(&format!("{{{{{}}}}}", key), value);
83 }
84
85 for (key, value) in &self.variables {
87 rendered = rendered.replace(&format!("{{{{{}}}}}", key), value);
88 }
89
90 rendered = self.render_conditionals(rendered, &context.conditions)?;
92
93 rendered = self.render_loops(rendered, &context.loops)?;
95
96 Ok(rendered)
97 }
98
99 fn render_conditionals(
101 &self,
102 mut content: String,
103 conditions: &HashMap<String, bool>,
104 ) -> Result<String> {
105 for (condition, enabled) in conditions {
107 let start_tag = format!("{{{{#if {}}}}}", condition);
108 let end_tag = "{{/if}}";
109
110 while let Some(start_pos) = content.find(&start_tag) {
111 if let Some(end_pos) = content[start_pos..].find(end_tag) {
112 let actual_end_pos = start_pos + end_pos;
113 let block_content = &content[start_pos + start_tag.len()..actual_end_pos];
114
115 let replacement =
116 if *enabled { block_content.to_string() } else { String::new() };
117
118 content.replace_range(start_pos..actual_end_pos + end_tag.len(), &replacement);
119 } else {
120 return Err(Error::msg("Unclosed conditional block"));
121 }
122 }
123 }
124
125 Ok(content)
126 }
127
128 fn render_loops(
130 &self,
131 mut content: String,
132 loops: &HashMap<String, Vec<HashMap<String, String>>>,
133 ) -> Result<String> {
134 for (loop_name, items) in loops {
136 let start_tag = format!("{{{{#each {}}}}}", loop_name);
137 let end_tag = "{{/each}}";
138
139 while let Some(start_pos) = content.find(&start_tag) {
140 if let Some(end_pos) = content[start_pos..].find(end_tag) {
141 let actual_end_pos = start_pos + end_pos;
142 let template_content = &content[start_pos + start_tag.len()..actual_end_pos];
143
144 let mut rendered_items = Vec::new();
145 for item in items {
146 let mut item_content = template_content.to_string();
147 for (key, value) in item {
148 item_content = item_content.replace(&format!("{{{{{}}}}}", key), value);
149 }
150 rendered_items.push(item_content);
151 }
152
153 let replacement = rendered_items.join("");
154 content.replace_range(start_pos..actual_end_pos + end_tag.len(), &replacement);
155 } else {
156 return Err(Error::msg("Unclosed loop block"));
157 }
158 }
159 }
160
161 Ok(content)
162 }
163
164 pub fn load_templates_from_dir(&mut self, dir: &Path) -> Result<()> {
166 for entry in std::fs::read_dir(dir)? {
167 let entry = entry?;
168 let path = entry.path();
169
170 if path.extension().and_then(|s| s.to_str()) == Some("template") {
171 let content = std::fs::read_to_string(&path)?;
172 let name = path
173 .file_stem()
174 .and_then(|s| s.to_str())
175 .ok_or_else(|| Error::msg("Invalid template filename"))?
176 .to_string();
177
178 let template = Template {
179 name: name.clone(),
180 content: content.clone(),
181 required_variables: self.extract_required_variables(&content),
182 optional_variables: HashMap::new(),
183 };
184
185 self.add_template(template);
186 }
187 }
188
189 Ok(())
190 }
191
192 fn extract_required_variables(&self, content: &str) -> Vec<String> {
194 let mut variables = Vec::new();
195 let mut chars = content.chars().peekable();
196
197 while let Some(ch) = chars.next() {
198 if ch == '{' && chars.peek() == Some(&'{') {
199 chars.next(); let mut var_name = String::new();
201
202 while let Some(ch) = chars.next() {
203 if ch == '}' && chars.peek() == Some(&'}') {
204 chars.next(); if !var_name.is_empty()
206 && !var_name.starts_with('#')
207 && !var_name.starts_with('/')
208 {
209 variables.push(var_name);
210 }
211 break;
212 }
213 var_name.push(ch);
214 }
215 }
216 }
217
218 variables.sort();
219 variables.dedup();
220 variables
221 }
222}
223
224impl Default for TemplateEngine {
225 fn default() -> Self {
226 Self::new()
227 }
228}
229
230pub struct ModelTemplates;
232
233impl ModelTemplates {
234 pub fn transformer_model() -> Template {
236 Template {
237 name: "transformer_model".to_string(),
238 content: r#"//! {{model_name}} Model Implementation
239//!
240//! {{description}}
241
242use super::config::{{model_name}}Config;
243use trustformers_core::errors::Result;
244use trustformers_core::tensor::Tensor;
245use trustformers_core::layers::{Linear, LayerNorm, Embedding};
246{{#if use_attention}}
247use trustformers_core::layers::attention::MultiHeadAttention;
248{{/if}}
249
250/// {{model_name}} model structure
251#[derive(Debug, Clone)]
252pub struct {{model_name}}Model {
253 config: {{model_name}}Config,
254 {{#if use_embeddings}}
255 embeddings: Embedding,
256 {{/if}}
257 {{#each layers}}
258 {{name}}: {{layer_type}},
259 {{/each}}
260 {{#if use_output_projection}}
261 output_projection: Linear,
262 {{/if}}
263}
264
265impl {{model_name}}Model {
266 /// Create a new {{model_name}} model
267 pub fn new(config: {{model_name}}Config) -> Result<Self> {
268 {{#if use_embeddings}}
269 let embeddings = Embedding::new(config.vocab_size, config.hidden_size)?;
270 {{/if}}
271
272 {{#each layers}}
273 let {{name}} = {{layer_type}}::new({{parameters}})?;
274 {{/each}}
275
276 {{#if use_output_projection}}
277 let output_projection = Linear::new(config.hidden_size, config.vocab_size, true)?;
278 {{/if}}
279
280 Ok(Self {
281 config,
282 {{#if use_embeddings}}
283 embeddings,
284 {{/if}}
285 {{#each layers}}
286 {{name}},
287 {{/each}}
288 {{#if use_output_projection}}
289 output_projection,
290 {{/if}}
291 })
292 }
293
294 /// Forward pass
295 pub fn forward(&self, input: &Tensor) -> Result<Tensor> {
296 {{#if use_embeddings}}
297 let mut hidden_states = self.embeddings.forward(input)?;
298 {{else}}
299 let mut hidden_states = input.clone();
300 {{/if}}
301
302 {{#each layers}}
303 hidden_states = self.{{name}}.forward(&hidden_states)?;
304 {{/each}}
305
306 {{#if use_output_projection}}
307 let logits = self.output_projection.forward(&hidden_states)?;
308 Ok(logits)
309 {{else}}
310 Ok(hidden_states)
311 {{/if}}
312 }
313
314 /// Get model configuration
315 pub fn config(&self) -> &{{model_name}}Config {
316 &self.config
317 }
318}
319"#
320 .to_string(),
321 required_variables: vec!["model_name".to_string(), "description".to_string()],
322 optional_variables: HashMap::new(),
323 }
324 }
325
326 pub fn model_config() -> Template {
328 Template {
329 name: "model_config".to_string(),
330 content: r#"//! {{model_name}} Configuration
331//!
332//! Configuration parameters for {{model_name}} models.
333
334use serde::{Deserialize, Serialize};
335
336/// Configuration for {{model_name}} models
337#[derive(Debug, Clone, Serialize, Deserialize)]
338pub struct {{model_name}}Config {
339 {{#each parameters}}
340 /// {{description}}
341 pub {{name}}: {{param_type}},
342 {{/each}}
343}
344
345impl Default for {{model_name}}Config {
346 fn default() -> Self {
347 Self {
348 {{#each parameters}}
349 {{name}}: {{default_value}},
350 {{/each}}
351 }
352 }
353}
354
355impl {{model_name}}Config {
356 {{#each presets}}
357 /// {{description}}
358 pub fn {{name}}() -> Self {
359 Self {
360 {{#each parameters}}
361 {{name}}: {{value}},
362 {{/each}}
363 }
364 }
365 {{/each}}
366
367 /// Validate configuration parameters
368 pub fn validate(&self) -> Result<(), String> {
369 {{#each validations}}
370 if {{condition}} {
371 return Err("{{error_message}}".to_string());
372 }
373 {{/each}}
374
375 Ok(())
376 }
377}
378"#
379 .to_string(),
380 required_variables: vec!["model_name".to_string()],
381 optional_variables: HashMap::new(),
382 }
383 }
384
385 pub fn model_tests() -> Template {
387 Template {
388 name: "model_tests".to_string(),
389 content: r#"//! {{model_name}} Tests
390//!
391//! Comprehensive test suite for {{model_name}} model implementation.
392
393use super::{{{model_name}}Config, {{model_name}}Model}};
394use trustformers_core::tensor::Tensor;
395use approx::assert_abs_diff_eq;
396
397#[test]
398fn test_{{model_name_lower}}_creation() {
399 let config = {{model_name}}Config::default();
400 let model = {{model_name}}Model::new(config).unwrap_or_else(|e| {
401 panic!("Failed to create model: {:?}", e);
402 });
403
404 // Model should be created successfully
405 assert_eq!(model.config().vocab_size, {{default_vocab_size}});
406}
407
408#[test]
409fn test_{{model_name_lower}}_forward_pass() {
410 let config = {{model_name}}Config::default();
411 let model = {{model_name}}Model::new(config.clone()).unwrap_or_else(|e| {
412 panic!("Failed to create model: {:?}", e);
413 });
414
415 let batch_size = 2;
416 let seq_length = 10;
417 let input = Tensor::zeros(&[batch_size, seq_length]);
418
419 let output = model.forward(&input).unwrap_or_else(|e| {
420 panic!("Forward pass failed: {:?}", e);
421 });
422
423 // Verify output shape
424 match &output {
425 Tensor::F32(arr) => {
426 assert_eq!(arr.shape()[0], batch_size);
427 assert_eq!(arr.shape()[1], seq_length);
428 {{#if has_vocab_output}}
429 assert_eq!(arr.shape()[2], config.vocab_size);
430 {{/if}}
431 }
432 _ => panic!("Expected F32 tensor"),
433 }
434}
435
436#[test]
437fn test_{{model_name_lower}}_numerical_stability() {
438 let config = {{model_name}}Config::default();
439 let model = {{model_name}}Model::new(config).unwrap_or_else(|e| {
440 panic!("Failed to create model: {:?}", e);
441 });
442
443 let input = Tensor::randn(&[4, 16]);
444 let output = model.forward(&input).unwrap_or_else(|e| {
445 panic!("Forward pass failed: {:?}", e);
446 });
447
448 // Check for NaN or infinite values
449 match &output {
450 Tensor::F32(arr) => {
451 for &val in arr.iter() {
452 assert!(val.is_finite(), "Output contains non-finite values: {}", val);
453 }
454 }
455 _ => panic!("Expected F32 tensor"),
456 }
457}
458
459{{#each additional_tests}}
460#[test]
461fn test_{{test_name}}() {
462 {{test_body}}
463}
464{{/each}}
465"#
466 .to_string(),
467 required_variables: vec![
468 "model_name".to_string(),
469 "model_name_lower".to_string(),
470 "default_vocab_size".to_string(),
471 ],
472 optional_variables: HashMap::new(),
473 }
474 }
475
476 pub fn model_module() -> Template {
478 Template {
479 name: "model_module".to_string(),
480 content: r#"//! {{model_name}} Model Implementation
481//!
482//! {{description}}
483//!
484//! ## Features
485//!
486//! {{#each features}}
487//! - {{.}}
488//! {{/each}}
489//!
490//! ## Usage
491//!
492//! ```rust
493//! use trustformers_models::{{model_name_lower}}::{{{model_name}}Config, {{model_name}}Model}};
494//!
495//! // Create model with default configuration
496//! let config = {{model_name}}Config::default();
497//! let model = {{model_name}}Model::new(config)?;
498//!
499//! // Forward pass
500//! let input = Tensor::zeros(&[1, 512]);
501//! let output = model.forward(&input)?;
502//! ```
503
504pub mod config;
505pub mod model;
506{{#if has_tasks}}
507pub mod tasks;
508{{/if}}
509{{#if has_tests}}
510pub mod tests;
511{{/if}}
512
513pub use config::{{model_name}}Config;
514pub use model::{{{model_name}}Model, {{model_name}}Output};
515{{#if has_tasks}}
516{{#each task_heads}}
517pub use tasks::{{model_name}}For{{.}};
518{{/each}}
519{{/if}}
520"#
521 .to_string(),
522 required_variables: vec![
523 "model_name".to_string(),
524 "model_name_lower".to_string(),
525 "description".to_string(),
526 ],
527 optional_variables: HashMap::new(),
528 }
529 }
530}
531
532pub struct TemplateUtils;
534
535impl TemplateUtils {
536 pub fn transformer_context(
538 model_name: &str,
539 description: &str,
540 vocab_size: usize,
541 hidden_size: usize,
542 ) -> TemplateContext {
543 let mut variables = HashMap::new();
544 variables.insert("model_name".to_string(), model_name.to_string());
545 variables.insert("description".to_string(), description.to_string());
546 variables.insert("vocab_size".to_string(), vocab_size.to_string());
547 variables.insert("hidden_size".to_string(), hidden_size.to_string());
548 variables.insert("model_name_lower".to_string(), model_name.to_lowercase());
549
550 let mut conditions = HashMap::new();
551 conditions.insert("use_embeddings".to_string(), true);
552 conditions.insert("use_attention".to_string(), true);
553 conditions.insert("use_output_projection".to_string(), true);
554 conditions.insert("has_vocab_output".to_string(), true);
555
556 TemplateContext {
557 variables,
558 conditions,
559 loops: HashMap::new(),
560 }
561 }
562
563 pub fn test_context(model_name: &str, vocab_size: usize) -> TemplateContext {
565 let mut variables = HashMap::new();
566 variables.insert("model_name".to_string(), model_name.to_string());
567 variables.insert("model_name_lower".to_string(), model_name.to_lowercase());
568 variables.insert("default_vocab_size".to_string(), vocab_size.to_string());
569
570 let mut conditions = HashMap::new();
571 conditions.insert("has_vocab_output".to_string(), true);
572
573 TemplateContext {
574 variables,
575 conditions,
576 loops: HashMap::new(),
577 }
578 }
579}