Skip to content

Commit bf16b5a

Browse files
committed
Update docs
1 parent 353c33a commit bf16b5a

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,47 @@
22

33
PyTorch compiler and WebGPU runtime, capable of running LLMs like [LLama 3.2 3B](examples/llama-3.2-3b.py)
44

5+
## Example: Compile and run an LLM
6+
7+
```python
8+
from transformers import AutoModelForCausalLM, AutoTokenizer
9+
import torch
10+
from torch_webgpu.compiler.webgpu_compiler import webgpu_backend
11+
12+
model = AutoModelForCausalLM.from_pretrained(
13+
"Qwen/Qwen2.5-0.5B-Instruct", torch_dtype=torch.float32
14+
)
15+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
16+
model.eval()
17+
18+
compiled_model = torch.compile(model, backend=webgpu_backend)
19+
20+
with torch.no_grad():
21+
inputs = tokenizer("Hello, how are you?", return_tensors="pt")
22+
input_ids = inputs["input_ids"]
23+
generated_ids = input_ids.clone()
24+
outputs = compiled_model(input_ids)
25+
for _ in range(10):
26+
outputs = compiled_model(generated_ids)
27+
next_token = outputs.logits[0, -1].argmax().unsqueeze(0).unsqueeze(0)
28+
generated_ids = torch.cat([generated_ids, next_token], dim=1)
29+
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
30+
```
31+
32+
## Example: Tensor on WebGPU
33+
34+
```python
35+
import torch
36+
import torch_webgpu
37+
38+
# Use WebGPU as a device
39+
x = torch.tensor([1.0, 2.0, 3.0], device="webgpu")
40+
y = x * 2
41+
print(y) # tensor([2., 4., 6.], device='webgpu')
42+
```
43+
544
## Use
45+
646
In Python:
747

848
`from torch_webgpu import webgpu_backend`

docs/index.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
PyTorch compiler and WebGPU runtime, capable of running LLM inference
44

5-
## What is torch-webgpu?
5+
## Use
66

7-
torch-webgpu is an experimental WebGPU backend for PyTorch that allows you to:
7+
In Python:
88

9-
- **Run PyTorch on WebGPU** using `device="webgpu"`
10-
- **Compile PyTorch models** with `@torch.compile(backend="webgpu")`
11-
- **Run LLMs on WebGPU** - models like Llama 3.2B or Qwen 2.5 0.5B works today!
9+
`from torch_webgpu import webgpu_backend`
10+
11+
And now you can use `@torch.compile(backend=webgpu_backend)`, `device="webgpu"`, `to="webgpu"` to run and compile PyTorch on a real WebGPU!
12+
13+
## Installation
14+
15+
```bash
16+
pip install torch-webgpu
17+
```
1218

1319
## Why WebGPU?
1420

@@ -19,7 +25,7 @@ WebGPU is a modern graphics and compute API that:
1925
- Provides a unified API across different GPU vendors
2026
- I believe is the future of portable GPU computing
2127

22-
## Quick Example
28+
## Example: Tensor on WebGPU
2329

2430
```python
2531
import torch
@@ -31,18 +37,31 @@ y = x * 2
3137
print(y) # tensor([2., 4., 6.], device='webgpu')
3238
```
3339

34-
## Compile an LLM
40+
## Example: Compile and run an LLM
3541

3642
```python
37-
import torch
3843
from transformers import AutoModelForCausalLM, AutoTokenizer
44+
import torch
3945
from torch_webgpu.compiler.webgpu_compiler import webgpu_backend
4046

41-
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
47+
model = AutoModelForCausalLM.from_pretrained(
48+
"Qwen/Qwen2.5-0.5B-Instruct", torch_dtype=torch.float32
49+
)
50+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
51+
model.eval()
52+
4253
compiled_model = torch.compile(model, backend=webgpu_backend)
4354

44-
# Run inference on WebGPU!
45-
outputs = compiled_model(input_ids)
55+
with torch.no_grad():
56+
inputs = tokenizer("Hello, how are you?", return_tensors="pt")
57+
input_ids = inputs["input_ids"]
58+
generated_ids = input_ids.clone()
59+
outputs = compiled_model(input_ids)
60+
for _ in range(10):
61+
outputs = compiled_model(generated_ids)
62+
next_token = outputs.logits[0, -1].argmax().unsqueeze(0).unsqueeze(0)
63+
generated_ids = torch.cat([generated_ids, next_token], dim=1)
64+
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
4665
```
4766

4867
## Current Status

0 commit comments

Comments
 (0)