rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
# RusTorch Python 바인딩 개요

Rust와 Python 간의 완벽한 상호 운용성을 위한 RusTorch Python 통합에 대한 포괄적인 개요입니다.

## 🌉 개요

RusTorch Python 바인딩을 통해 Python에서 직접 강력한 Rust 기반 딥러닝 라이브러리를 사용할 수 있습니다. 이 바인딩은 Rust의 성능과 안전성을 Python의 사용 편의성과 결합합니다.

## 📋 목차

- [아키텍처]#아키텍처
- [설치 및 설정]#설치-및-설정
- [핵심 기능]#핵심-기능
- [모듈 개요]#모듈-개요
- [고급 기능]#고급-기능
- [성능 최적화]#성능-최적화
- [상호 운용성]#상호-운용성
- [개발 가이드라인]#개발-가이드라인

## 🏗️ 아키텍처

### PyO3 통합

RusTorch는 Python-Rust 상호 운용성을 위해 PyO3를 사용합니다:

```rust
use pyo3::prelude::*;

#[pymodule]
fn rustorch_py(_py: Python, m: &PyModule) -> PyResult<()> {
    // 텐서 모듈 등록
    m.add_class::<PyTensor>()?;
    
    // 함수형 API
    m.add_function(wrap_pyfunction!(create_tensor, m)?)?;
    m.add_function(wrap_pyfunction!(tensor_operations, m)?)?;
    
    Ok(())
}
```

### 모듈형 구조

```
rustorch_py/
├── tensor/          # 기본 텐서 연산
├── autograd/        # 자동 미분
├── nn/              # 신경망 레이어
├── optim/           # 최적화 알고리즘
├── data/            # 데이터 처리 및 로딩
├── training/        # 훈련 루프 및 유틸리티
├── utils/           # 보조 함수
├── distributed/     # 분산 훈련
└── visualization/   # 플롯팅 및 시각화
```

## 🛠️ 설치 및 설정

### 사전 요구사항

- **Rust** (버전 1.70+)
- **Python** (버전 3.8+)
- **PyO3** (버전 0.24+)
- **Maturin** (빌드용)

### 빌드 프로세스

```bash
# Python 바인딩 컴파일
cargo build --features python

# Maturin을 이용한 개발 (개발 모드)
maturin develop --features python

# 릴리스 빌드
maturin build --release --features python
```

### Python 측 설치

```python
# 빌드 후
pip install target/wheels/rustorch_py-*.whl

# 또는 Maturin으로 직접
pip install maturin
maturin develop
```

## ⚡ 핵심 기능

### 1. 텐서 연산

```python
import rustorch_py

# 텐서 생성
tensor = rustorch_py.create_tensor([1, 2, 3, 4], shape=[2, 2])
print(f"텐서: {tensor}")

# 기본 연산
result = rustorch_py.tensor_add(tensor, tensor)
matrix_result = rustorch_py.tensor_matmul(tensor, tensor)
```

### 2. 자동 미분

```python
# 그래디언트 가능한 텐서
x = rustorch_py.create_variable([2.0, 3.0], requires_grad=True)
y = rustorch_py.create_variable([1.0, 4.0], requires_grad=True)

# 순전파
z = rustorch_py.operations.mul(x, y)
loss = rustorch_py.operations.sum(z)

# 역전파
rustorch_py.backward(loss)

print(f"x의 그래디언트: {x.grad}")
print(f"y의 그래디언트: {y.grad}")
```

### 3. 신경망

```python
# 레이어 정의
linear = rustorch_py.nn.Linear(input_size=784, output_size=128)
relu = rustorch_py.nn.ReLU()
dropout = rustorch_py.nn.Dropout(p=0.2)

# 순차 모델
model = rustorch_py.nn.Sequential([
    linear,
    relu,
    dropout,
    rustorch_py.nn.Linear(128, 10)
])

# 순전파
input_data = rustorch_py.create_tensor(data, shape=[batch_size, 784])
output = model.forward(input_data)
```

## 📦 모듈 개요

### Tensor 모듈

```python
import rustorch_py.tensor as tensor

# 텐서 생성
zeros = tensor.zeros([3, 4])
ones = tensor.ones([2, 2])
randn = tensor.randn([5, 5])

# 연산
result = tensor.add(a, b)
transposed = tensor.transpose(matrix, 0, 1)
reshaped = tensor.reshape(tensor_input, [6, -1])
```

### Autograd 모듈

```python
import rustorch_py.autograd as autograd

# 그래디언트 계산을 위한 변수
var = autograd.Variable(data, requires_grad=True)

# 그래디언트 계산
loss = compute_loss(var)
autograd.backward(loss)

# 그래디언트 수집 활성화/비활성화
with autograd.no_grad():
    prediction = model.forward(input_data)
```

### Neural Network 모듈

```python
import rustorch_py.nn as nn

# 기본 레이어
linear = nn.Linear(in_features, out_features)
conv2d = nn.Conv2d(in_channels, out_channels, kernel_size)
lstm = nn.LSTM(input_size, hidden_size, num_layers)

# 활성화 함수
relu = nn.ReLU()
sigmoid = nn.Sigmoid()
tanh = nn.Tanh()
gelu = nn.GELU()

# 손실 함수
mse_loss = nn.MSELoss()
cross_entropy = nn.CrossEntropyLoss()
```

### 최적화 모듈

```python
import rustorch_py.optim as optim

# 옵티마이저
adam = optim.Adam(model.parameters(), lr=0.001)
sgd = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

# 훈련 루프
for epoch in range(num_epochs):
    prediction = model.forward(input_data)
    loss = criterion(prediction, target)
    
    # 그래디언트 계산
    loss.backward()
    
    # 매개변수 업데이트
    optimizer.step()
    optimizer.zero_grad()
```

## 🚀 고급 기능

### GPU 가속

```python
# CUDA 지원
if rustorch_py.cuda.is_available():
    device = rustorch_py.device("cuda:0")
    tensor_gpu = tensor.to(device)
    
    # GPU 연산
    result = rustorch_py.cuda.matmul(tensor_gpu, tensor_gpu)

# Metal 지원 (macOS)
if rustorch_py.metal.is_available():
    metal_device = rustorch_py.device("metal:0")
    tensor_metal = tensor.to(metal_device)
```

### 분산 훈련

```python
import rustorch_py.distributed as dist

# 초기화
dist.init_process_group("nccl", rank=0, world_size=4)

# 다중 GPU 훈련
model = nn.DataParallel(model, device_ids=[0, 1, 2, 3])

# 그래디언트 동기화를 위한 All-Reduce
dist.all_reduce(tensor, op=dist.ReduceOp.SUM)
```

### 데이터 처리

```python
import rustorch_py.data as data

# Dataset 클래스
class CustomDataset(data.Dataset):
    def __init__(self, data, targets):
        self.data = data
        self.targets = targets
    
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        return self.data[idx], self.targets[idx]

# DataLoader
dataset = CustomDataset(train_data, train_targets)
dataloader = data.DataLoader(
    dataset, 
    batch_size=32, 
    shuffle=True,
    num_workers=4
)
```

## ⚡ 성능 최적화

### SIMD 최적화

```python
# SIMD 최적화 활성화
rustorch_py.set_simd_enabled(True)

# 병렬화 활성화
rustorch_py.set_num_threads(8)  # CPU 병렬화용
```

### 메모리 관리

```python
# 효율적인 할당을 위한 메모리 풀
rustorch_py.memory.enable_memory_pool()

# GPU 메모리 캐시 정리
if rustorch_py.cuda.is_available():
    rustorch_py.cuda.empty_cache()
```

### Just-in-Time 컴파일

```python
# 중요한 함수들을 위한 JIT 컴파일
@rustorch_py.jit.script
def optimized_function(x, y):
    return rustorch_py.operations.mul(x, y) + rustorch_py.operations.sin(x)

result = optimized_function(tensor1, tensor2)
```

## 🔄 상호 운용성

### NumPy 통합

```python
import numpy as np
import rustorch_py

# NumPy → RusTorch
numpy_array = np.array([[1, 2], [3, 4]], dtype=np.float32)
rust_tensor = rustorch_py.from_numpy(numpy_array)

# RusTorch → NumPy
numpy_result = rust_tensor.numpy()
```

### PyTorch 호환성

```python
# PyTorch 텐서 변환
import torch

# PyTorch → RusTorch
torch_tensor = torch.randn(3, 4)
rust_tensor = rustorch_py.from_torch(torch_tensor)

# RusTorch → PyTorch
pytorch_tensor = rust_tensor.to_torch()
```

### 콜백 시스템

```python
# 훈련용 Python 콜백
def training_callback(epoch, loss, accuracy):
    print(f"에포크 {epoch}: 손실={loss:.4f}, 정확도={accuracy:.4f}")

# 콜백 등록
rustorch_py.callbacks.register_training_callback(training_callback)

# 콜백과 함께 훈련
trainer = rustorch_py.training.Trainer(model, optimizer, criterion)
trainer.train(dataloader, epochs=100)
```

## 📊 시각화

```python
import rustorch_py.visualization as viz

# 훈련 히스토리 플롯
viz.plot_training_history(losses, accuracies)

# 텐서 시각화
viz.visualize_tensor(tensor, title="가중치 분포")

# 네트워크 아키텍처 그래프
viz.plot_model_graph(model)
```

## 🧪 개발 가이드라인

### 테스팅

```python
# 단위 테스트
import rustorch_py.testing as testing

def test_tensor_operations():
    a = rustorch_py.create_tensor([1, 2, 3])
    b = rustorch_py.create_tensor([4, 5, 6])
    
    result = rustorch_py.tensor_add(a, b)
    expected = [5, 7, 9]
    
    testing.assert_tensor_equal(result, expected)
```

### 디버깅

```python
# 디버그 모드 활성화
rustorch_py.set_debug_mode(True)

# 프로파일링
with rustorch_py.profiler.profile() as prof:
    result = model.forward(input_data)

prof.print_stats()
```

### 오류 처리

```python
try:
    tensor = rustorch_py.create_tensor(data, shape)
except rustorch_py.TensorError as e:
    print(f"텐서 오류: {e}")
except rustorch_py.DeviceError as e:
    print(f"디바이스 오류: {e}")
```

## 🔧 고급 설정

### 환경 변수

```bash
# Rust 특화 설정
export RUSTORCH_NUM_THREADS=8
export RUSTORCH_CUDA_DEVICE=0
export RUSTORCH_LOG_LEVEL=info

# Python 통합
export PYTHONPATH=$PYTHONPATH:./target/debug
```

### 런타임 설정

```python
# 전역 설정
rustorch_py.config.set_default_device("cuda:0")
rustorch_py.config.set_default_dtype(rustorch_py.float32)
rustorch_py.config.enable_fast_math(True)

# 스레드 풀 설정
rustorch_py.config.set_thread_pool_size(16)
```

## 🚀 미래 전망

### 계획된 기능

- **WebAssembly 통합**: WASM을 통한 브라우저 배포
- **모바일 지원**: iOS/Android 최적화
- **고급 분산 전략**: 파이프라인 병렬화
- **양자화**: INT8/FP16 추론 최적화
- **AutoML 통합**: 자동 하이퍼파라미터 최적화

### 커뮤니티 기여

- **플러그인 시스템**: 사용자 정의 연산을 위한 확장 가능한 아키텍처
- **벤치마킹 스위트**: 다른 프레임워크와의 성능 비교
- **튜토리얼 컬렉션**: 포괄적인 학습 리소스

더 많은 정보와 전체 API 참조는 [Python API 문서](python_api_reference.md)와 [Jupyter 가이드](jupyter-guide.md)를 참조하세요.