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
//! TensorBoard integration for RusTorch
//! RusTorch用TensorBoard統合

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

pub mod event_writer;
pub mod summary;

use self::event_writer::EventWriter;
use self::summary::Summary;

/// TensorBoard writer for logging training metrics
/// 訓練メトリクスログ用TensorBoardライター
pub struct SummaryWriter {
    /// Log directory
    /// ログディレクトリ
    log_dir: PathBuf,
    /// Event writer
    /// イベントライター
    event_writer: EventWriter,
    /// Global step counter
    /// グローバルステップカウンタ
    global_step: usize,
    /// Flush interval
    /// フラッシュ間隔
    flush_interval: usize,
    /// Pending summaries
    /// 保留中のサマリー
    pending_summaries: Vec<Summary>,
}

impl SummaryWriter {
    /// Create a new summary writer
    /// 新しいサマリーライターを作成
    pub fn new(log_dir: impl AsRef<Path>) -> std::io::Result<Self> {
        let log_dir = log_dir.as_ref().to_path_buf();

        // Create log directory if it doesn't exist
        fs::create_dir_all(&log_dir)?;

        // Create event file
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        #[cfg(not(target_arch = "wasm32"))]
        let hostname = hostname::get()
            .unwrap_or_else(|_| std::ffi::OsString::from("unknown"))
            .to_string_lossy()
            .to_string();

        #[cfg(target_arch = "wasm32")]
        let hostname = "wasm-browser".to_string();

        let filename = format!("events.out.tfevents.{}.{}", timestamp, hostname);

        let event_path = log_dir.join(filename);
        let event_writer = EventWriter::new(event_path)?;

        Ok(Self {
            log_dir,
            event_writer,
            global_step: 0,
            flush_interval: 10,
            pending_summaries: Vec::new(),
        })
    }

    /// Add scalar value
    /// スカラー値を追加
    pub fn add_scalar(&mut self, tag: &str, value: f32, step: Option<usize>) {
        let step = step.unwrap_or(self.global_step);
        let summary = Summary::scalar(tag, value, step);
        self.pending_summaries.push(summary);

        if self.pending_summaries.len() >= self.flush_interval {
            self.flush();
        }
    }

    /// Add histogram
    /// ヒストグラムを追加
    pub fn add_histogram(&mut self, tag: &str, values: &[f32], step: Option<usize>) {
        let step = step.unwrap_or(self.global_step);
        let summary = Summary::histogram(tag, values, step);
        self.pending_summaries.push(summary);

        if self.pending_summaries.len() >= self.flush_interval {
            self.flush();
        }
    }

    /// Add image
    /// 画像を追加
    pub fn add_image(&mut self, tag: &str, image: &ImageData, step: Option<usize>) {
        let step = step.unwrap_or(self.global_step);
        let summary = Summary::image(tag, image, step);
        self.pending_summaries.push(summary);

        if self.pending_summaries.len() >= self.flush_interval {
            self.flush();
        }
    }

    /// Add text
    /// テキストを追加
    pub fn add_text(&mut self, tag: &str, text: &str, step: Option<usize>) {
        let step = step.unwrap_or(self.global_step);
        let summary = Summary::text(tag, text, step);
        self.pending_summaries.push(summary);

        if self.pending_summaries.len() >= self.flush_interval {
            self.flush();
        }
    }

    /// Add graph (computational graph)
    /// グラフ(計算グラフ)を追加
    pub fn add_graph(&mut self, graph: &GraphDef) {
        let summary = Summary::graph(graph);
        self.pending_summaries.push(summary);
        self.flush();
    }

    /// Add embedding projector data
    /// 埋め込みプロジェクタデータを追加
    pub fn add_embedding(
        &mut self,
        mat: &[Vec<f32>],
        metadata: Option<Vec<String>>,
        tag: Option<&str>,
    ) -> std::io::Result<()> {
        let tag = tag.unwrap_or("default");
        let projector_dir = self.log_dir.join("projector");
        fs::create_dir_all(&projector_dir)?;

        // Write tensor TSV
        let tensor_path = projector_dir.join(format!("{}_tensor.tsv", tag));
        let mut tensor_file = File::create(tensor_path)?;

        for vec in mat {
            let line: String = vec
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join("\t");
            writeln!(tensor_file, "{}", line)?;
        }

        // Write metadata TSV if provided
        if let Some(metadata) = metadata {
            let metadata_path = projector_dir.join(format!("{}_metadata.tsv", tag));
            let mut metadata_file = File::create(metadata_path)?;
            for label in metadata {
                writeln!(metadata_file, "{}", label)?;
            }
        }

        // Create projector config
        let config = ProjectorConfig {
            embeddings: vec![EmbeddingInfo {
                tensor_name: format!("{}_tensor.tsv", tag),
                metadata_path: Some(format!("{}_metadata.tsv", tag)),
            }],
        };

        let config_path = projector_dir.join("projector_config.pbtxt");
        let config_content = format_projector_config(&config);
        fs::write(config_path, config_content)?;

        Ok(())
    }

    /// Add PR curve
    /// PR曲線を追加
    pub fn add_pr_curve(
        &mut self,
        tag: &str,
        labels: &[bool],
        predictions: &[f32],
        step: Option<usize>,
    ) {
        let step = step.unwrap_or(self.global_step);
        let summary = Summary::pr_curve(tag, labels, predictions, step);
        self.pending_summaries.push(summary);

        if self.pending_summaries.len() >= self.flush_interval {
            self.flush();
        }
    }

    /// Flush pending summaries
    /// 保留中のサマリーをフラッシュ
    pub fn flush(&mut self) {
        for summary in self.pending_summaries.drain(..) {
            self.event_writer.write_summary(summary);
        }
        self.event_writer.flush();
    }

    /// Increment global step
    /// グローバルステップをインクリメント
    pub fn step(&mut self) {
        self.global_step += 1;
    }

    /// Close the writer
    /// ライターを閉じる
    pub fn close(mut self) {
        self.flush();
    }
}

/// Image data for TensorBoard
/// TensorBoard用画像データ
#[derive(Debug, Clone)]
pub struct ImageData {
    /// Height
    pub height: u32,
    /// Width
    pub width: u32,
    /// Channels (1 for grayscale, 3 for RGB, 4 for RGBA)
    pub channels: u32,
    /// Pixel data (flattened)
    pub data: Vec<u8>,
}

/// Graph definition for computational graphs
/// 計算グラフのグラフ定義
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphDef {
    /// Nodes in the graph
    pub nodes: Vec<NodeDef>,
    /// Edges in the graph
    pub edges: Vec<EdgeDef>,
}

/// Node definition in graph
/// グラフ内のノード定義
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeDef {
    /// Node name
    pub name: String,
    /// Operation type
    pub op: String,
    /// Input nodes
    pub inputs: Vec<String>,
    /// Node attributes
    pub attrs: HashMap<String, String>,
}

/// Edge definition in graph
/// グラフ内のエッジ定義
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgeDef {
    /// Source node
    pub source: String,
    /// Target node
    pub target: String,
    /// Edge label
    pub label: Option<String>,
}

/// Projector configuration
/// プロジェクタ設定
#[derive(Debug, Clone)]
struct ProjectorConfig {
    embeddings: Vec<EmbeddingInfo>,
}

/// Embedding information
/// 埋め込み情報
#[derive(Debug, Clone)]
struct EmbeddingInfo {
    tensor_name: String,
    metadata_path: Option<String>,
}

/// Format projector config to protobuf text format
/// プロジェクタ設定をprotobufテキスト形式にフォーマット
fn format_projector_config(config: &ProjectorConfig) -> String {
    let mut result = String::new();

    for embedding in &config.embeddings {
        result.push_str("embeddings {\n");
        result.push_str(&format!("  tensor_name: \"{}\"\n", embedding.tensor_name));

        if let Some(metadata_path) = &embedding.metadata_path {
            result.push_str(&format!("  metadata_path: \"{}\"\n", metadata_path));
        }

        result.push_str("}\n");
    }

    result
}

/// Python-compatible API for seamless integration
/// シームレス統合用Python互換API
pub mod python_compat {
    use super::*;

    /// Create writer with automatic directory naming
    /// 自動ディレクトリ命名でライターを作成
    pub fn create_writer(base_dir: Option<&str>) -> std::io::Result<SummaryWriter> {
        let base_dir = base_dir.unwrap_or("runs");
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let log_dir = format!("{}/experiment_{}", base_dir, timestamp);
        SummaryWriter::new(log_dir)
    }

    /// Quick logging function
    /// クイックログ関数
    pub fn log_scalar(writer: &mut SummaryWriter, tag: &str, value: f32) {
        writer.add_scalar(tag, value, None);
        writer.step();
    }
}

/// Macro for easy TensorBoard logging
/// 簡単なTensorBoardログ用マクロ
#[macro_export]
macro_rules! tb_log {
    ($writer:expr, scalar: $tag:expr, $value:expr) => {
        $writer.add_scalar($tag, $value, None);
    };

    ($writer:expr, histogram: $tag:expr, $values:expr) => {
        $writer.add_histogram($tag, $values, None);
    };

    ($writer:expr, text: $tag:expr, $text:expr) => {
        $writer.add_text($tag, $text, None);
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_summary_writer_creation() {
        let dir = tempdir().unwrap();
        let _writer = SummaryWriter::new(dir.path()).unwrap();

        // Check that event file was created
        let entries: Vec<_> = fs::read_dir(dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .collect();

        assert_eq!(entries.len(), 1);
        assert!(entries[0]
            .file_name()
            .to_string_lossy()
            .starts_with("events.out.tfevents"));
    }

    #[test]
    fn test_scalar_logging() {
        let dir = tempdir().unwrap();
        let mut writer = SummaryWriter::new(dir.path()).unwrap();

        writer.add_scalar("loss", 0.5, Some(0));
        writer.add_scalar("accuracy", 0.95, Some(0));
        writer.flush();

        // Verify file exists and has content
        let entries: Vec<_> = fs::read_dir(dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .collect();

        assert_eq!(entries.len(), 1);
        assert!(entries[0]
            .file_name()
            .to_string_lossy()
            .starts_with("events.out.tfevents"));

        // On some platforms (particularly Windows), file metadata might not be
        // immediately available or might report 0 size for recently written files.
        // Instead, verify that the file can be read and contains some data.
        let file_path = entries[0].path();
        let file_contents = fs::read(&file_path).unwrap();
        assert!(
            file_contents.len() > 0,
            "Event file should contain data after flush(), but was empty on platform: {}",
            std::env::consts::OS
        );
    }

    #[test]
    fn test_histogram_logging() {
        let dir = tempdir().unwrap();
        let mut writer = SummaryWriter::new(dir.path()).unwrap();

        let values = vec![0.1, 0.2, 0.3, 0.4, 0.5];
        writer.add_histogram("weights", &values, Some(0));
        writer.flush();
    }
}