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
//! Performance profiler demonstration
//! パフォーマンスプロファイラーのデモンストレーション
use rustorch::autograd::Variable;
use rustorch::nn::Linear;
use rustorch::profiler::{
clear_profiler, disable_profiler, enable_profiler, export_chrome_trace, print_profiler_report,
ProfileContext,
};
use rustorch::tensor::Tensor;
use std::thread;
use std::time::Duration;
fn main() {
println!("🔍 RusTorch Profiler Demo");
println!("=========================\n");
// Clear any previous profiling data
clear_profiler();
// Enable profiler
enable_profiler();
// Run different profiling scenarios (simplified)
profile_basic_operations();
profile_neural_network();
// Skip time-intensive demos
// profile_nested_operations();
// profile_parallel_operations();
// Disable profiler
disable_profiler();
// Print profiling report
println!("\n📊 Profiling Report:");
println!("====================");
print_profiler_report();
// Export Chrome trace
if let Some(trace) = export_chrome_trace() {
// Save to file for Chrome tracing
std::fs::write("profile_trace.json", trace).expect("Failed to write trace file");
println!("\n✅ Chrome trace saved to profile_trace.json");
println!(" Open chrome://tracing and load the file to visualize");
}
}
/// Profile basic tensor operations
fn profile_basic_operations() {
println!("1️⃣ Profiling Basic Tensor Operations");
println!("-------------------------------------");
{
let _ctx = ProfileContext::new("tensor_creation");
let tensor1 = Tensor::<f32>::randn(&[100, 100]);
let tensor2 = Tensor::<f32>::randn(&[100, 100]);
{
let _ctx = ProfileContext::new("tensor_addition");
let _result = &tensor1 + &tensor2;
}
{
let _ctx = ProfileContext::new("tensor_multiplication");
let _result = &tensor1 * &tensor2;
}
{
let _ctx = ProfileContext::new("matrix_multiplication");
let _result = tensor1.matmul(&tensor2);
}
}
println!("✓ Basic operations profiled\n");
}
/// Profile neural network operations
fn profile_neural_network() {
println!("2️⃣ Profiling Neural Network Operations");
println!("---------------------------------------");
let _ctx = ProfileContext::new("neural_network");
// Create simple layers
let layer1: Linear<f32> = Linear::new(784, 256);
let layer2: Linear<f32> = Linear::new(256, 128);
let layer3: Linear<f32> = Linear::new(128, 10);
// Forward pass
{
let _ctx = ProfileContext::new("forward_pass");
let input = Variable::new(Tensor::<f32>::randn(&[32, 784]), false);
{
let _ctx = ProfileContext::new("layer1_forward");
let hidden1 = layer1.forward(&input);
// Simulate additional processing
thread::sleep(Duration::from_millis(1));
{
let _ctx = ProfileContext::new("layer2_forward");
let hidden2 = layer2.forward(&hidden1);
// Removed sleep for faster execution
{
let _ctx = ProfileContext::new("layer3_forward");
layer3.forward(&hidden2);
// Removed sleep for faster execution
}
}
}
}
println!("✓ Neural network operations profiled\n");
}