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
//! CLI argument definitions using clap
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "tensorlogic")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
/// Input expression or file path (required for compilation mode)
#[arg(value_name = "INPUT")]
pub input: Option<String>,
/// Input format
#[arg(short = 'f', long, value_enum, default_value = "expr")]
pub input_format: InputFormat,
/// Output file (stdout if not specified)
#[arg(short, long)]
pub output: Option<PathBuf>,
/// Output format
#[arg(short = 'F', long, value_enum, default_value = "graph")]
pub output_format: OutputFormat,
/// Compilation strategy
#[arg(short, long)]
pub strategy: Option<String>,
/// Domain definitions (name:size pairs, can be specified multiple times)
#[arg(short, long, value_parser = parse_domain)]
pub domains: Vec<(String, usize)>,
/// Enable validation
#[arg(long)]
pub validate: bool,
/// Enable debug output
#[arg(long)]
pub debug: bool,
/// Analyze graph and show metrics
#[arg(short, long)]
pub analyze: bool,
/// Quiet mode (minimal output)
#[arg(short, long)]
pub quiet: bool,
/// Disable colored output
#[arg(long)]
pub no_color: bool,
/// Don't load configuration file
#[arg(long)]
pub no_config: bool,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
pub enum Commands {
/// Start interactive REPL mode
Repl,
/// Batch process multiple expressions from files
Batch {
/// Input files containing expressions (one per line)
#[arg(required = true)]
files: Vec<PathBuf>,
},
/// Watch a file and recompile on changes
Watch {
/// File to watch
file: PathBuf,
},
/// Generate shell completion scripts
Completion {
/// Shell type
#[arg(value_enum)]
shell: Shell,
},
/// Configuration file management
Config {
#[command(subcommand)]
command: ConfigCommand,
},
/// Convert between formats
Convert {
/// Input file or expression
input: String,
/// Input format
#[arg(short = 'f', long, value_enum)]
from: ConvertFormat,
/// Output format
#[arg(short = 't', long, value_enum)]
to: ConvertFormat,
/// Output file (stdout if not specified)
#[arg(short, long)]
output: Option<PathBuf>,
/// Pretty-print the output
#[arg(short, long)]
pretty: bool,
},
/// Execute compiled expressions with backend selection
Execute {
/// Input expression or file path
input: String,
/// Input format
#[arg(short = 'f', long, value_enum, default_value = "expr")]
input_format: InputFormat,
/// Backend to use (cpu, simd, gpu, parallel, profiled)
#[arg(short, long, default_value = "cpu")]
backend: String,
/// Show performance metrics
#[arg(long)]
metrics: bool,
/// Show intermediate tensors
#[arg(long)]
intermediates: bool,
/// Enable execution tracing
#[arg(long)]
trace: bool,
/// Output format for results
#[arg(short = 'F', long, value_enum, default_value = "table")]
output_format: ExecutionOutputFormat,
},
/// Optimize compiled graphs
Optimize {
/// Input expression or file path
input: String,
/// Input format
#[arg(short = 'f', long, value_enum, default_value = "expr")]
input_format: InputFormat,
/// Optimization level (none, basic, standard, aggressive)
#[arg(short, long, default_value = "standard")]
level: String,
/// Show optimization statistics
#[arg(long)]
stats: bool,
/// Verbose output
#[arg(short, long)]
verbose: bool,
/// Output file (stdout if not specified)
#[arg(short, long)]
output: Option<PathBuf>,
/// Output format
#[arg(short = 'F', long, value_enum, default_value = "graph")]
output_format: OutputFormat,
},
/// List available backends and their capabilities
Backends,
/// Benchmark compilation and execution performance
Benchmark {
/// Input expression or file path
input: String,
/// Input format
#[arg(short = 'f', long, value_enum, default_value = "expr")]
input_format: InputFormat,
/// Number of iterations
#[arg(short = 'n', long, default_value = "10")]
iterations: usize,
/// Backend to benchmark (for execution benchmarks)
#[arg(short, long, default_value = "cpu")]
backend: String,
/// Include execution benchmarks
#[arg(long)]
execute: bool,
/// Include optimization benchmarks
#[arg(long)]
optimize: bool,
/// Show detailed timing for each iteration
#[arg(short, long)]
verbose: bool,
/// Export results as JSON
#[arg(long)]
json: bool,
},
/// Profile compilation with detailed phase breakdown
Profile {
/// Input expression or file path
input: String,
/// Input format
#[arg(short = 'f', long, value_enum, default_value = "expr")]
input_format: InputFormat,
/// Skip optimization in profile
#[arg(long)]
no_optimize: bool,
/// Optimization level (none, basic, standard, aggressive)
#[arg(long, default_value = "standard")]
opt_level: String,
/// Include validation in profile
#[arg(long)]
validate: bool,
/// Include execution profiling
#[arg(long)]
execute: bool,
/// Backend for execution profiling (cpu, parallel, profiled)
#[arg(long, default_value = "cpu")]
exec_backend: String,
/// Number of warmup runs
#[arg(long, default_value = "1")]
warmup: usize,
/// Number of profiling runs to average
#[arg(long, default_value = "3")]
runs: usize,
/// Export results as JSON
#[arg(long)]
json: bool,
},
/// Manage persistent compilation cache
Cache {
#[command(subcommand)]
command: CacheCommand,
},
/// Snapshot testing for output consistency
Snapshot {
#[command(subcommand)]
command: SnapshotCommand,
},
}
#[derive(Subcommand)]
pub enum CacheCommand {
/// Show cache statistics
Stats,
/// Clear the entire cache
Clear,
/// Enable caching
Enable,
/// Disable caching
Disable,
/// Show cache directory path
Path,
}
#[derive(Subcommand)]
pub enum ConfigCommand {
/// Show current configuration
Show,
/// Show configuration file path
Path,
/// Initialize default configuration file
Init,
/// Edit configuration file
Edit,
}
#[derive(Subcommand)]
pub enum SnapshotCommand {
/// Record a new snapshot
Record {
/// Test name
name: String,
/// Input expression
expression: String,
/// Compilation strategy
#[arg(short, long)]
strategy: Option<String>,
/// Domain definitions (name:size pairs)
#[arg(short, long, value_parser = parse_domain)]
domains: Vec<(String, usize)>,
},
/// Verify expression against recorded snapshot
Verify {
/// Test name
name: String,
/// Input expression
expression: String,
/// Compilation strategy
#[arg(short, long)]
strategy: Option<String>,
/// Domain definitions (name:size pairs)
#[arg(short, long, value_parser = parse_domain)]
domains: Vec<(String, usize)>,
},
/// Update an existing snapshot
Update {
/// Test name
name: String,
/// Input expression
expression: String,
/// Compilation strategy
#[arg(short, long)]
strategy: Option<String>,
/// Domain definitions (name:size pairs)
#[arg(short, long, value_parser = parse_domain)]
domains: Vec<(String, usize)>,
},
/// List all snapshots
List,
/// Show snapshot directory path
Path,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum InputFormat {
/// Direct expression string
Expr,
/// JSON file or stdin
Json,
/// YAML file
Yaml,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum OutputFormat {
/// Debug graph representation
Graph,
/// Graphviz DOT format
Dot,
/// JSON serialization
Json,
/// Statistics only
Stats,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ConvertFormat {
/// Expression string
Expr,
/// JSON format
Json,
/// YAML format
Yaml,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ExecutionOutputFormat {
/// Human-readable table
Table,
/// JSON format
Json,
/// CSV format
Csv,
/// NumPy text format
Numpy,
}
fn parse_domain(s: &str) -> Result<(String, usize), String> {
let parts: Vec<&str> = s.split(':').collect();
if parts.len() != 2 {
return Err(format!("Invalid domain format '{}'. Expected name:size", s));
}
let name = parts[0].to_string();
let size = parts[1]
.parse::<usize>()
.map_err(|e| format!("Invalid domain size: {}", e))?;
Ok((name, size))
}