aprender 0.31.2

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI
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
<!-- PCU: examples-apr-cli-commands | contract: contracts/apr-page-examples-apr-cli-commands-v1.yaml -->
<!-- Example: cargo run -p aprender-core --example apr_cli_commands -->
<!-- Status: enforced -->

# Case Study: APR CLI Commands Demo

This case study demonstrates creating test models and using all 27 apr-cli commands for model inspection, validation, transformation, testing, and inference.

## The Problem

APR model files need comprehensive tooling for:

| Need | Traditional Approach | Problem |
|------|---------------------|---------|
| Inspection | Custom scripts | No standardization |
| Validation | Manual checksums | Incomplete coverage |
| Transformation | Framework-specific | Lock-in |
| Regression | Manual testing | Error-prone |

## The Solution: apr-cli

The `apr` CLI provides 29+ commands for complete model lifecycle management:

```bash
# Build the CLI
cargo build -p apr-cli

# Inspect model metadata
./target/debug/apr inspect model.apr --json

# Validate integrity (100-point QA)
./target/debug/apr validate model.apr --quality

# Quantize model
./target/debug/apr convert model.apr --quantize int8 -o model-int8.apr
```

## Complete Example

Run: `cargo run --example apr_cli_commands`

```rust,ignore
// Run this example:
//   cargo run --example apr_cli_commands
//
// See the CLI reference and source code in crates/ for implementation details.
```

## All Commands

### Model Inspection

#### 1. INSPECT - View Model Metadata

```bash
apr inspect model.apr              # Basic info
apr inspect model.apr --json       # JSON output
apr inspect model.apr --weights    # Include tensor info
```

Shows model type, framework, hyperparameters, and training info.

#### 2. TENSORS - List Tensor Info

```bash
apr tensors model.apr              # List all tensors
apr tensors model.apr --stats      # Include statistics
apr tensors model.apr --json       # JSON output
```

Lists tensor names, shapes, dtypes, and statistics.

#### 3. TRACE - Layer-by-Layer Analysis

```bash
apr trace model.apr                # Basic trace
apr trace model.apr --verbose      # Detailed trace
apr trace model.apr --json         # JSON output
```

Analyzes model layer by layer for debugging inference.

#### 4. DEBUG - Debug Output

```bash
apr debug model.apr                # Standard debug
apr debug model.apr --drama        # Detailed drama mode
apr debug model.apr --hex --limit 64  # Hex dump
```

Provides detailed tensor inspection for debugging.

### Quality & Validation

#### 5. VALIDATE - Check Model Integrity

```bash
apr validate model.apr             # Basic validation
apr validate model.apr --quality   # 100-point QA checklist
apr validate model.apr --strict    # Strict mode
```

Runs the 100-point quality assessment with grades A+ to F.

#### 6. LINT - Best Practices Check

```bash
apr lint model.apr                 # Check best practices
```

Static analysis for naming conventions, metadata completeness, and efficiency.

Checks:
- Standard tensor naming patterns (layer.0.weight, not l0_w)
- Required metadata (author, license, provenance)
- Tensor alignment (64-byte boundaries)
- Compression for large tensors (>1MB)

#### 7. DIFF - Compare Two Models

```bash
apr diff model_v1.apr model_v2.apr       # Compare models
apr diff model_v1.apr model_v2.apr --json  # JSON output
```

Shows metadata and tensor differences between model versions.

### Model Transformation

#### 8. CONVERT - Quantization/Optimization

```bash
apr convert model.apr --quantize int8 -o model-int8.apr
apr convert model.apr --quantize int4 -o model-int4.apr
apr convert model.apr --quantize fp16 -o model-fp16.apr
```

Applies quantization for reduced model size and faster inference.

| Quantization | Size Reduction | Accuracy Impact |
|--------------|----------------|-----------------|
| fp16 | 50% | Minimal |
| int8 | 75% | Small |
| int4 | 87.5% | Moderate |

#### 9. EXPORT - Export to Other Formats

```bash
apr export model.apr --format safetensors -o model.safetensors
apr export model.apr --format gguf -o model.gguf
```

Exports APR models to other ecosystems:
- **SafeTensors** - HuggingFace ecosystem
- **GGUF** - llama.cpp / local inference

#### 10. MERGE - Merge Models

```bash
apr merge model1.apr model2.apr --strategy average -o merged.apr
apr merge model1.apr model2.apr --strategy weighted -o merged.apr
```

Combines multiple models using different strategies:
- **average** - Simple tensor averaging
- **weighted** - Weighted combination

### Import & Interop

#### 11. IMPORT - Import External Models

```bash
apr import external.safetensors -o imported.apr
apr import hf://org/repo -o model.apr --arch whisper
```

Imports from SafeTensors, HuggingFace Hub, and other formats.

### Testing & Regression

#### 12. CANARY - Regression Testing

```bash
# Create canary from original model
apr canary create model.apr --input ref.wav --output canary.json

# Check optimized model against canary
apr canary check model-optimized.apr --canary canary.json
```

Captures tensor statistics for regression testing after transformations (quantization, pruning).

Canary data includes:
- Tensor shapes and counts
- Mean, std, min, max for each tensor
- Drift tolerance checking

#### 13. PROBAR - Visual Regression Testing

```bash
apr probar model.apr -o probar_output         # Create probar suite
apr probar model.apr -o output --format json  # JSON format
```

Exports model data for visual regression testing.

### Help & Documentation

#### 14. EXPLAIN - Get Explanations

```bash
apr explain E002                                            # Explain error code
apr explain --tensor encoder.conv1.weight                   # Explain tensor by convention
apr explain --tensor conv1 --file model.safetensors         # Look up in actual model
apr explain --file model.apr                                # Analyze architecture
apr explain --kernel llama                                  # Kernel pipeline for family
apr explain --kernel qwen2 --json                           # JSON output for tooling
apr explain --kernel /path/to/config.json --verbose         # Resolve from config.json
apr explain --kernel Qwen/Qwen2.5-Coder-0.5B-Instruct      # Resolve from HF repo
apr explain --kernel gemma --proof-status                   # Include proof status
```

Provides context-aware explanations for errors, tensors, model architectures, and kernel pipelines. When `--file` is provided with `--tensor`, looks up the tensor in the actual model via RosettaStone (supports APR, GGUF, SafeTensors). The `--kernel` flag explains which kernel equivalence class (A-F) a model uses, the architectural constraints that drive selection, and the kernel ops pipeline.

### Interactive

#### 15. TUI - Interactive Terminal UI

```bash
apr tui model.apr                          # Launch interactive UI
```

Interactive terminal interface for model exploration with four tabs:

| Tab | Key | Description |
|-----|-----|-------------|
| Overview | `1` | Model metadata, hyperparameters, training info |
| Tensors | `2` | Tensor list with shapes, dtypes, sizes |
| Stats | `3` | Tensor statistics (mean, std, min, max, zeros, NaNs) |
| Help | `?` | Keyboard shortcuts and navigation help |

**Keyboard Navigation:**
- `1`, `2`, `3`, `?` - Switch tabs directly
- `Tab` / `Shift+Tab` - Cycle through tabs
- `j` / `` - Next item in list
- `k` / `` - Previous item in list
- `q` / `Esc` - Quit

### Inference (requires `--features inference`)

Build with inference support:

```bash
cargo build -p apr-cli --features inference
```

#### 16. RUN - Run Model Inference

```bash
apr run model.apr --input "[1.0, 2.0]"       # JSON array input
apr run model.apr --input "1.0,2.0"          # CSV input
apr run model.apr --input "[1.0, 2.0]" --json  # JSON output
```

Runs inference on APR, SafeTensors, or GGUF models:

| Format | Inference Type |
|--------|----------------|
| APR (.apr) | Full ML inference via realizar |
| SafeTensors (.safetensors) | Tensor inspection |
| GGUF (.gguf) | Model inspection (mmap) |

**Input Formats:**
- JSON array: `"[1.0, 2.0, 3.0]"`
- CSV: `"1.0,2.0,3.0"`

#### 17. SERVE - Inference Server and Capacity Planning

**Serve Plan** — Pre-flight capacity planning (no weights loaded):
```bash
# Plan from local file
apr serve plan model.gguf --gpu

# Plan from HuggingFace repo (fetches only ~2KB config.json)
apr serve plan hf://Qwen/Qwen2.5-Coder-1.5B-Instruct --gpu --quant Q4_K_M

# JSON output for CI/tooling
apr serve plan microsoft/phi-2 --gpu --format json
```

**Serve Run** — Start inference server:
```bash
apr serve run model.apr --port 8080              # Start on port 8080
apr serve run model.apr --host 0.0.0.0 --port 3000  # Bind to all interfaces
```

Starts a REST API server for model inference:

**APR Models (full inference):**
```bash
# Health check
curl http://localhost:8080/health

# Run inference
curl -X POST http://localhost:8080/predict \
  -H "Content-Type: application/json" \
  -d '{"input": [1.0, 2.0]}'
```

**Server Features:**
- `/health` - Health check endpoint
- `/predict` - Inference endpoint (APR models)
- `/model` - Model info endpoint (GGUF/SafeTensors)
- `/tensors` - Tensor listing (SafeTensors)
- Graceful shutdown via Ctrl+C

### Chat & Comparison

#### 18. CHAT - Interactive Chat (LLM models)

```bash
apr chat model.gguf                                          # Interactive chat
apr chat model.gguf --system "You are a helpful assistant"   # Custom system prompt
```

#### 19. FLOW - Visualize Data Flow

```bash
apr flow model.safetensors            # Show data flow
apr flow model.gguf --json            # JSON output (architecture, groups)
apr flow model.apr --verbose           # Verbose with shapes
```

Detects architecture (Encoder-Decoder, Decoder-Only, Encoder-Only) and groups tensors by layer. Supports APR, GGUF, and SafeTensors.

#### 20. COMPARE-HF - Compare Against HuggingFace Source

```bash
apr compare-hf model.apr --hf openai/whisper-tiny              # APR format
apr compare-hf model.gguf --hf openai/whisper-tiny             # GGUF format
apr compare-hf model.safetensors --hf openai/whisper-tiny      # SafeTensors format
apr compare-hf model.apr --hf openai/whisper-tiny --json       # JSON output
```

Auto-detects local model format. Compares tensor-by-tensor against HuggingFace source.

### HuggingFace Hub

#### 21. PUBLISH - Push to HuggingFace Hub

```bash
apr publish model_dir/ org/model-name --dry-run
```

#### 22. PULL - Download Model

```bash
apr pull hf://Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF -o ./models/
```

### Benchmarking & QA

#### 23. QA - Falsifiable QA Checklist

```bash
apr qa model.gguf                     # Run 8-gate QA checklist
apr qa model.gguf --json              # JSON output
```

#### 24. QUALIFY - Cross-Subcommand Smoke Test

```bash
apr qualify model.gguf                              # Smoke test all 11 tools
apr qualify model.gguf --tier full                   # Full tier (+contracts +playbook)
apr qualify model.gguf --json                        # JSON output for CI
apr qualify model.gguf --skip validate,validate_quality  # Skip slow gates
```

Runs every diagnostic CLI tool against a model to verify no crashes. Three tiers: smoke (11 in-process gates), standard (+contract audit), full (+playbook check).

#### 25. SHOWCASE - Performance Benchmark

```bash
apr showcase model.gguf --warmup 3 --iterations 10
```

#### 26. PROFILE - Deep Performance Profiling

```bash
apr profile model.gguf --roofline
```

#### 27. BENCH - Run Benchmarks

```bash
apr bench model.gguf --iterations 100
```

## Example Output

Running the example creates demo models:

```
=== APR CLI Commands Demo ===

--- Part 1: Creating Demo Model ---
  Adding tensors...
  Model type: Linear Regression
  Tensors: 4
  Size: 1690 bytes
Created: /tmp/apr_cli_demo/demo_model.apr

--- Part 2: Creating Second Model (for diff) ---
  Model type: Linear Regression v2
  Tensors: 4
  Size: 1707 bytes
Created: /tmp/apr_cli_demo/demo_model_v2.apr
```

## Use Cases

### CI/CD Model Validation

```bash
# In CI pipeline
apr validate model.apr --strict --min-score 90 && apr lint model.apr
if [ $? -ne 0 ]; then
    echo "Model validation failed"
    exit 1
fi
```

### Model Optimization Pipeline

```bash
# Quantize for production
apr convert model.apr --quantize int8 -o model-int8.apr

# Verify no regression
apr canary create model.apr --input test.wav --output canary.json
apr canary check model-int8.apr --canary canary.json

# Export for deployment
apr export model-int8.apr --format gguf -o model.gguf
```

### Model Version Comparison

```bash
# Compare before/after optimization
apr diff original.apr quantized.apr --json | jq '.tensor_changes'
```

### Debugging Inference Issues

```bash
# Layer-by-layer trace
apr trace model.apr --verbose | grep -i "nan\|inf"

# Drama mode for detailed analysis
apr debug model.apr --drama
```

## Benefits

| Benefit | Description |
|---------|-------------|
| Standardized | Consistent CLI for all APR models |
| Comprehensive | 29+ commands cover full lifecycle |
| Scriptable | JSON output for automation |
| Debuggable | Deep inspection with drama mode |
| Validatable | 100-point QA with grades |
| Transformable | Quantization and format conversion |
| Testable | Canary regression testing |
| Inference | Run predictions and serve REST APIs |

## Related Resources

- [Case Study: APR with JSON Metadata]./apr-with-metadata.md
- [The .apr Format: A Five Whys Deep Dive]./apr-format-deep-dive.md
- [APR Loading Modes]./apr-loading-modes.md
- [apr (APR Model Operations CLI)]../tools/apr-cli.md