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
# RusTorch WebGPU Chrome Demo
WebGPU加速テンソル演算のChrome ブラウザ向けデモンストレーション
## 🎯 Overview
This demo showcases RusTorch's WebGPU acceleration capabilities specifically optimized for Google Chrome browser. It demonstrates GPU-accelerated tensor operations including matrix multiplication, element-wise operations, and activation functions.
## 📋 Requirements
### Browser Requirements
- **Google Chrome 113+** (recommended: latest stable)
- **WebGPU enabled** in Chrome flags
- **Hardware GPU acceleration** enabled
- **Local web server** (not file:// protocol)
### System Requirements
- Modern GPU with compute shader support
- Sufficient GPU memory for tensor operations
- WebGPU-compatible graphics drivers
## 🔧 Setup Instructions
### 1. Enable WebGPU in Chrome
1. Open Chrome and navigate to: `chrome://flags/#enable-unsafe-webgpu`
2. Set "Unsafe WebGPU" to **Enabled**
3. Restart Chrome
### 2. Build the WASM Module
```bash
# Install wasm-pack (if not already installed)
cargo install wasm-pack
# Build RusTorch with WebGPU features for WASM
wasm-pack build --target web --features webgpu
# Alternative: Build specific example
cargo build --example webgpu_chrome_demo --target wasm32-unknown-unknown --features webgpu
```
### 3. Serve the Demo
```bash
# Using Python's built-in server
python3 -m http.server 8000
# Using Node.js http-server (npm install -g http-server)
http-server -p 8000
# Using Rust's basic-http-server (cargo install basic-http-server)
basic-http-server -a 127.0.0.1:8000
```
### 4. Open the Demo
1. Navigate to: `http://localhost:8000/examples/webgpu_demo.html`
2. Open Chrome Developer Console (F12) to see detailed logs
3. Click "Initialize WebGPU Engine" to start
## 🎮 Demo Features
### WebGPU Detection
- Automatic WebGPU support detection
- GPU adapter information display
- Chrome version compatibility check
### Tensor Operations
- **Addition**: Element-wise tensor addition with GPU acceleration
- **Matrix Multiplication**: Optimized matrix multiplication using compute shaders
- **Activation Functions**: ReLU and Sigmoid with GPU compute
- **Memory Management**: Efficient GPU buffer management
### Performance Testing
- Comparative performance analysis (GPU vs CPU estimates)
- Multiple tensor sizes testing (100, 1000, 10000 elements)
- Real-time performance metrics display
### Chrome Optimizations
- Workgroup size optimization for Chrome's WebGPU implementation
- Buffer size optimization for Chrome's memory management
- Chrome-specific GPU adapter selection
## 🚀 Performance Expectations
| Operation | Small Tensors | Large Tensors | Expected Speedup |
|-----------|---------------|---------------|------------------|
| Addition | 1.2x | 2.0x | Moderate |
| Multiplication | 1.2x | 2.0x | Moderate |
| Matrix Multiplication | 1.5x | 10.0x | Significant |
| ReLU | 1.5x | 3.0x | Good |
| Sigmoid | 1.5x | 3.0x | Good |
## 🐛 Troubleshooting
### Common Issues
**"WebGPU not supported"**
- Ensure Chrome 113+ with WebGPU flags enabled
- Check hardware GPU acceleration is available
- Try updating graphics drivers
**"Failed to initialize WebGPU engine"**
- Check GPU memory availability
- Close other GPU-intensive applications
- Try refreshing the page
**"WASM module failed to load"**
- Ensure serving from HTTP server (not file://)
- Check WASM build completed successfully
- Verify all dependencies are available
**"Demo functions not working"**
- Ensure WebGPU engine initialization completed
- Check browser console for detailed error messages
- Try the comprehensive demo function
### Performance Issues
**Slower than expected performance**
- Check if hardware acceleration is actually enabled
- Monitor GPU usage in Chrome Task Manager
- Ensure sufficient GPU memory is available
- Try smaller tensor sizes for testing
## 🔍 Debugging
### Chrome Developer Tools
1. Open DevTools (F12)
2. Go to Console tab for execution logs
3. Check Network tab for WASM loading
4. Use Performance tab for detailed profiling
### GPU Monitoring
1. Chrome Task Manager: `Shift+Esc` → GPU Process
2. Check GPU memory usage and utilization
3. Monitor for GPU compute activity during operations
## 📊 Understanding the Output
The demo provides detailed console output including:
- WebGPU adapter information (GPU name, backend type)
- Tensor operation input/output data
- Performance timing measurements
- Verification of computational accuracy
- Resource usage statistics
## 🔬 Technical Details
### WGSL Compute Shaders
The demo uses optimized WGSL (WebGPU Shading Language) compute shaders:
- Workgroup size optimization for Chrome
- Memory coalescing for efficient GPU memory access
- Bounds checking for safe GPU computation
### Buffer Management
- Automatic GPU buffer allocation and deallocation
- Efficient CPU-GPU data transfer
- Memory usage optimization for Chrome's WebGPU implementation
### Chrome-Specific Optimizations
- Power preference: High Performance
- Backend selection: Browser WebGPU (not Dawn)
- Memory hints: Performance-optimized
- Workgroup sizing tuned for Chrome's GPU scheduler
## 🆘 Support
If you encounter issues:
1. Check Chrome WebGPU compatibility: `chrome://gpu/`
2. Verify WASM build: Check for `pkg/` directory with generated files
3. Review browser console for detailed error messages
4. Test with smaller tensor sizes first
5. Report issues with system information and Chrome version