torsh-cli 0.1.2

Command-line tools for the ToRSh deep learning framework
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
# torsh-cli

Command-line tools for the ToRSh deep learning framework with PyTorch-compatible CLI interface.

## Overview

This crate provides a comprehensive command-line interface for ToRSh, enabling model training, inference, conversion, benchmarking, and management directly from the terminal. It offers a familiar experience for users coming from PyTorch while leveraging Rust's performance and safety.

## Features

- **Model Training**: Train models with configuration files or command-line arguments
- **Inference**: Run predictions on trained models with various input formats
- **Model Conversion**: Convert between different model formats (ONNX, TorchScript, etc.)
- **Benchmarking**: Profile and benchmark model performance
- **Model Hub**: Download and manage pre-trained models
- **Dataset Management**: Download and prepare datasets for training
- **Quantization**: Quantize models for deployment
- **Profiling**: Analyze model performance and memory usage
- **Interactive Mode**: REPL for quick experimentation

## Installation

```bash
# Install from source
cargo install --path crates/torsh-cli

# Install with all features
cargo install --path crates/torsh-cli --features full
```

## Usage

### Basic Commands

```bash
# Display help
torsh --help

# Train a model
torsh train --config config.yaml

# Run inference
torsh infer --model model.torsh --input data.json

# Benchmark a model
torsh bench --model model.torsh --batch-size 32

# Convert model format
torsh convert --input model.pth --output model.onnx --format onnx

# Download pre-trained model from hub
torsh hub download --model resnet50 --output ./models/
```

### Training

#### From Configuration File

```bash
# Train with YAML config
torsh train --config training_config.yaml

# Override config parameters
torsh train --config config.yaml --epochs 100 --lr 0.001
```

Example `training_config.yaml`:

```yaml
model:
  type: resnet
  num_classes: 10
  pretrained: false

data:
  train_path: ./data/train
  val_path: ./data/val
  batch_size: 64
  num_workers: 4

training:
  epochs: 50
  learning_rate: 0.01
  optimizer: sgd
  momentum: 0.9
  weight_decay: 0.0001

scheduler:
  type: step
  step_size: 30
  gamma: 0.1

checkpoints:
  save_dir: ./checkpoints
  save_interval: 5
```

#### Direct Command-Line Arguments

```bash
torsh train \
  --model resnet50 \
  --data ./data/imagenet \
  --epochs 100 \
  --batch-size 128 \
  --lr 0.1 \
  --optimizer sgd \
  --device cuda:0
```

### Inference

```bash
# Single file inference
torsh infer \
  --model model.torsh \
  --input image.jpg \
  --output predictions.json

# Batch inference
torsh infer \
  --model model.torsh \
  --input-dir ./images/ \
  --output-dir ./predictions/ \
  --batch-size 32

# Streaming inference
torsh infer \
  --model model.torsh \
  --stream \
  --format json
```

### Benchmarking

```bash
# Benchmark model throughput
torsh bench \
  --model model.torsh \
  --batch-size 1,8,32,128 \
  --device cuda:0 \
  --warmup 10 \
  --iterations 100

# Profile memory usage
torsh bench \
  --model model.torsh \
  --profile memory \
  --output profile.json

# Compare multiple models
torsh bench \
  --models model1.torsh,model2.torsh,model3.torsh \
  --compare \
  --output comparison.csv
```

### Model Conversion

```bash
# Convert PyTorch to ONNX
torsh convert \
  --input model.pth \
  --output model.onnx \
  --format onnx \
  --opset 14

# Convert to TorchScript
torsh convert \
  --input model.pth \
  --output model.pt \
  --format torchscript

# Quantize during conversion
torsh convert \
  --input model.pth \
  --output model_int8.torsh \
  --quantize int8
```

### Model Hub

```bash
# List available models
torsh hub list

# Search for models
torsh hub search --query "resnet"

# Download model
torsh hub download \
  --model resnet50 \
  --variant imagenet \
  --output ./models/

# Upload model to hub
torsh hub upload \
  --model my_model.torsh \
  --name my-awesome-model \
  --description "My custom model"
```

### Dataset Management

```bash
# List available datasets
torsh data list

# Download dataset
torsh data download --dataset cifar10 --output ./data/

# Prepare custom dataset
torsh data prepare \
  --input ./raw_data/ \
  --output ./processed_data/ \
  --format imagefolder

# Split dataset
torsh data split \
  --input ./data/ \
  --train 0.8 \
  --val 0.1 \
  --test 0.1
```

### Quantization

```bash
# Quantize model to INT8
torsh quantize \
  --model model.torsh \
  --output model_int8.torsh \
  --precision int8 \
  --calibration-data ./calib_data/

# Dynamic quantization
torsh quantize \
  --model model.torsh \
  --output model_dynamic.torsh \
  --mode dynamic

# QAT (Quantization-Aware Training)
torsh quantize \
  --model model.torsh \
  --mode qat \
  --data ./data/ \
  --epochs 10
```

### Profiling

```bash
# Profile model execution
torsh profile \
  --model model.torsh \
  --input-shape 1,3,224,224 \
  --device cuda:0

# Generate detailed report
torsh profile \
  --model model.torsh \
  --report-format html \
  --output profile_report.html

# Layer-wise profiling
torsh profile \
  --model model.torsh \
  --layer-wise \
  --output layers.json
```

### Interactive Mode

```bash
# Start interactive REPL
torsh repl

# Load model in REPL
torsh repl --model model.torsh
```

Within the REPL:

```python
>>> import numpy as np
>>> x = tensor([[1, 2], [3, 4]])
>>> y = x @ x.t()
>>> print(y)
Tensor([[5, 11], [11, 25]], dtype=i32)

>>> model = load("model.torsh")
>>> output = model(x)
```

## Configuration

### Global Configuration

The CLI uses a global configuration file located at `~/.torsh/config.toml`:

```toml
[defaults]
device = "cuda:0"
dtype = "float32"
num_workers = 4

[hub]
cache_dir = "~/.torsh/hub"
api_url = "https://hub.torsh.ai"

[logging]
level = "info"
format = "text"

[performance]
num_threads = 8
enable_cudnn = true
```

### Environment Variables

```bash
# Set default device
export TORSH_DEVICE=cuda:0

# Set cache directory
export TORSH_CACHE_DIR=/path/to/cache

# Set log level
export TORSH_LOG=debug

# Number of threads
export TORSH_NUM_THREADS=8
```

## Advanced Features

### Custom Scripts

```bash
# Run Python-like script
torsh run script.torsh

# With arguments
torsh run script.torsh --arg1 value1 --arg2 value2
```

### Model Inspection

```bash
# Show model architecture
torsh info --model model.torsh

# Show detailed layer information
torsh info --model model.torsh --verbose

# Export to dot format for visualization
torsh info --model model.torsh --format dot --output model.dot
```

### Distributed Training

```bash
# Launch distributed training
torsh train \
  --config config.yaml \
  --distributed \
  --world-size 4 \
  --rank 0 \
  --master-addr localhost \
  --master-port 29500
```

## Shell Completion

Generate shell completion scripts:

```bash
# Bash
torsh completion bash > /etc/bash_completion.d/torsh

# Zsh
torsh completion zsh > ~/.zfunc/_torsh

# Fish
torsh completion fish > ~/.config/fish/completions/torsh.fish

# PowerShell
torsh completion powershell > torsh.ps1
```

## Examples

### Train ResNet on CIFAR-10

```bash
torsh data download --dataset cifar10 --output ./data/
torsh train \
  --model resnet18 \
  --data ./data/cifar10 \
  --epochs 100 \
  --batch-size 128 \
  --lr 0.1 \
  --optimizer sgd \
  --scheduler cosine
```

### Fine-tune Pre-trained Model

```bash
torsh hub download --model resnet50 --output ./models/
torsh train \
  --model ./models/resnet50.torsh \
  --fine-tune \
  --data ./custom_data/ \
  --epochs 20 \
  --lr 0.001
```

### Export for Production

```bash
torsh convert \
  --input model.torsh \
  --output model.onnx \
  --format onnx \
  --optimize \
  --opset 15

torsh quantize \
  --model model.onnx \
  --output model_int8.onnx \
  --precision int8
```

## Integration with SciRS2

This crate leverages the SciRS2 ecosystem for:

- High-performance tensor operations through `scirs2-core`
- Neural network implementations via `scirs2-neural`
- Optimization algorithms from `scirs2-optimize` and `optirs`
- Metrics and evaluation through `scirs2-metrics`

All operations follow the [SciRS2 POLICY](https://github.com/cool-japan/scirs/blob/master/SCIRS2_POLICY.md) for consistent, maintainable code.

## Development

### Building

```bash
# Build CLI
cargo build --package torsh-cli

# Build with all features
cargo build --package torsh-cli --all-features

# Release build
cargo build --package torsh-cli --release
```

### Testing

```bash
# Run tests
cargo test --package torsh-cli

# Integration tests
cargo test --package torsh-cli --test integration_tests
```

## License

Licensed under the Apache License, Version 2.0. See [LICENSE](../../LICENSE) for details.