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
//! Test helpers for node code generation tests
//!
//! This module provides common utilities for testing node code generation,
//! making tests more concise and maintainable.
use super::NodeCodegen;
use crate::burn::Scope;
use crate::burn::argument_helpers::{codegen_fn_params, codegen_return_expr, codegen_return_type};
use onnx_ir::ir::ArgType;
use quote::quote;
/// Generate forward pass code for a node with optional clone behavior
///
/// Generates a complete forward function signature with inputs, outputs, and body.
///
/// # Arguments
/// * `node` - The node to generate code for
/// * `input_idx` - Index of the input to register (default: 0)
/// * `with_clone` - Whether to register future use to trigger clone
/// * `node_position` - Position of the node in the graph (default: 1)
///
/// # Example
/// ```ignore
/// let node = create_my_node("test");
/// let code = codegen_forward(&node, 0, false, 1);
/// assert_snapshot!(code, @r#"
/// pub fn forward(&self, input: Tensor<B, 4>) -> Tensor<B, 4> {
/// let output = input.abs();
/// output
/// }
/// "#);
/// ```
pub fn codegen_forward<T>(
node: &T,
_input_idx: usize,
with_clone: bool,
node_position: usize,
) -> String
where
T: NodeCodegen,
{
let mut scope = Scope::default();
// Register all inputs as variables
for input in node.inputs().iter() {
// Skip non-dynamic inputs (constants, initializers)
if !(input.is_dynamic() || input.is_constant()) {
continue;
}
if !matches!(
input.ty,
ArgType::Tensor(_)
| ArgType::ScalarTensor(_)
| ArgType::ScalarNative(_)
| ArgType::Shape(_)
) {
continue;
}
scope.tensor_register_variable(input, 0);
if with_clone {
// Register two future uses to trigger clone
scope.tensor_register_future_use(input, node_position);
scope.tensor_register_future_use(input, node_position + 1);
}
}
// Generate code using the node's forward method with ScopeAtPosition
let mut scope_at_pos = scope.at_position(node_position);
let body = node.forward(&mut scope_at_pos);
// Filter inputs to only include dynamic inputs (not constants/initializers)
let dynamic_inputs: Vec<_> = node
.inputs()
.iter()
.filter(|arg| arg.is_dynamic() || arg.is_constant())
.filter(|arg| {
matches!(
arg.ty,
ArgType::Tensor(_)
| ArgType::ScalarTensor(_)
| ArgType::ScalarNative(_)
| ArgType::Shape(_)
)
})
.cloned()
.collect();
// Use shared helpers for generating function signature parts
let input_def = codegen_fn_params(&dynamic_inputs);
let return_type = codegen_return_type(node.outputs());
let return_expr = codegen_return_expr(node.outputs());
// Generate the full forward function
let forward_fn = quote! {
pub fn forward(&self, #input_def) -> #return_type {
#body
#return_expr
}
};
format_tokens(forward_fn)
}
/// Generate forward pass code with default parameters
///
/// Uses:
/// - input_idx: 0 (first input)
/// - with_clone: false (no clone)
/// - node_position: 1
///
/// # Example
/// ```ignore
/// let node = create_my_node("test");
/// let code = codegen_forward_default(&node);
/// assert_snapshot!(code, @r#"
/// pub fn forward(&self, input: Tensor<B, 4>) -> Tensor<B, 4> {
/// let output = input.abs();
/// output
/// }
/// "#);
/// ```
pub fn codegen_forward_default<T>(node: &T) -> String
where
T: NodeCodegen,
{
codegen_forward(node, 0, false, 1)
}
/// Generate forward pass code with clone enabled
///
/// Uses:
/// - input_idx: 0 (first input)
/// - with_clone: true (triggers clone)
/// - node_position: 1
///
/// # Example
/// ```ignore
/// let node = create_my_node("test");
/// let code = codegen_forward_with_clone(&node);
/// assert!(code.contains("clone"));
/// ```
pub fn codegen_forward_with_clone<T>(node: &T) -> String
where
T: NodeCodegen,
{
codegen_forward(node, 0, true, 1)
}
/// Generate field initialization code for a node
///
/// Returns the initialization code from the node's field() method.
///
/// # Example
/// ```ignore
/// let node = create_pool_node("pool1", true);
/// let code = codegen_field_init(&node);
/// assert!(code.contains(".with_ceil_mode(true)"));
/// ```
pub fn codegen_field_init<T>(node: &T) -> String
where
T: NodeCodegen,
{
if let Some(field) = node.field() {
format_statement(field.init)
} else {
String::new()
}
}
/// Format a TokenStream using PrettyPlease
fn format_tokens(tokens: proc_macro2::TokenStream) -> String {
use rust_format::{Config, Formatter, PostProcess, PrettyPlease};
let config = Config::new_str().post_proc(PostProcess::ReplaceMarkersAndDocBlocks);
let formatter = PrettyPlease::from_config(config);
let fallback = tokens.to_string();
formatter.format_tokens(tokens).unwrap_or(fallback)
}
/// Format a statement TokenStream by wrapping it in a function
fn format_statement(tokens: proc_macro2::TokenStream) -> String {
use rust_format::{Config, Formatter, PostProcess, PrettyPlease};
// Wrap in a function to make it valid syntax for PrettyPlease
let wrapped = quote! {
fn __wrapper() {
#tokens
}
};
let config = Config::new_str().post_proc(PostProcess::ReplaceMarkersAndDocBlocks);
let formatter = PrettyPlease::from_config(config);
match formatter.format_tokens(wrapped) {
Ok(formatted) => {
// Extract the body (remove the wrapper function)
let lines: Vec<&str> = formatted.lines().collect();
// Skip "fn __wrapper() {" and the closing "}"
let body_lines: Vec<&str> = lines
.iter()
.skip(1)
.take(lines.len().saturating_sub(2))
.copied()
.collect();
// Remove one level of indentation (4 spaces)
body_lines
.iter()
.map(|line| line.strip_prefix(" ").unwrap_or(*line))
.collect::<Vec<_>>()
.join("\n")
}
Err(_) => tokens.to_string(),
}
}