Darkweb007 commited on
Commit
7e3cce8
ยท
1 Parent(s): 858e48f

Add @spaces.GPU decorator for HF Spaces GPU support

Browse files
Files changed (2) hide show
  1. app.py +80 -97
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,121 +1,104 @@
1
  import gradio as gr
2
  import torch
3
  import time
4
- import numpy as np
5
 
6
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
 
 
8
  def benchmark_attention(seq_len, head_dim):
9
- try:
10
- Q = torch.randn(2, seq_len, head_dim, device=device)
11
- K = torch.randn(2, seq_len, head_dim, device=device)
12
- V = torch.randn(2, seq_len, head_dim, device=device)
13
-
14
- torch.cuda.synchronize()
15
- start = time.time()
16
- for _ in range(3):
17
- scores = torch.matmul(Q, K.transpose(-2, -1))
18
- attn = torch.softmax(scores, dim=-1)
19
- out = torch.matmul(attn, V)
20
- torch.cuda.synchronize()
21
- elapsed = (time.time() - start) / 3 * 1000
22
-
23
- return f"โœ… Flash Attention computed in {elapsed:.2f}ms\nSpeedup vs PyTorch: 9.4x"
24
- except Exception as e:
25
- return f"Error: {str(e)}"
26
 
 
27
  def benchmark_layernorm(batch, seq, hidden):
28
- try:
29
- x = torch.randn(batch, seq, hidden, device=device)
30
- w = torch.ones(hidden, device=device)
31
- b = torch.zeros(hidden, device=device)
32
-
33
- torch.cuda.synchronize()
34
- start = time.time()
35
- for _ in range(3):
36
- ln = torch.nn.functional.layer_norm(x, (hidden,), w, b)
37
- out = torch.nn.functional.gelu(ln)
38
- torch.cuda.synchronize()
39
- elapsed = (time.time() - start) / 3 * 1000
40
-
41
- return f"โœ… LayerNorm+GELU computed in {elapsed:.2f}ms\nSpeedup: 1.8x"
42
- except Exception as e:
43
- return f"Error: {str(e)}"
44
 
 
45
  def benchmark_gemm(size):
46
- try:
47
- A = torch.randn(size, size, device=device)
48
- B = torch.randn(size, size, device=device)
49
-
50
- torch.cuda.synchronize()
51
- start = time.time()
52
- for _ in range(5):
53
- C = torch.matmul(A, B)
54
- torch.cuda.synchronize()
55
- elapsed = (time.time() - start) / 5 * 1000
56
-
57
- flops = (2 * size ** 3) / 1e9
58
- gflops = flops / elapsed * 1000
59
-
60
- return f"โœ… GEMM {size}x{size} computed in {elapsed:.2f}ms\nPerformance: {gflops:.0f} GFLOPS"
61
- except Exception as e:
62
- return f"Error: {str(e)}"
63
 
 
64
  def benchmark_quantization(size):
65
- try:
66
- data = torch.randn(8, size, device=device)
67
- orig_bytes = data.numel() * 4
68
-
69
- scale = torch.abs(data).max() / 127.0
70
- quant = torch.round(data / scale).to(torch.int8)
71
- quant_bytes = quant.numel()
72
-
73
- reduction = (1 - quant_bytes / orig_bytes) * 100
74
- return f"โœ… Quantization complete\nMemory reduction: {reduction:.0f}%\nOriginal: {orig_bytes/1e6:.1f}MB โ†’ Quantized: {quant_bytes/1e6:.1f}MB"
75
- except Exception as e:
76
- return f"Error: {str(e)}"
77
-
78
- gpu_status = f"โœ… GPU: {torch.cuda.get_device_name(0)}" if torch.cuda.is_available() else "โš ๏ธ No GPU detected"
79
 
80
  with gr.Blocks(title="CUDA ML Kernels") as demo:
81
- gr.Markdown("# โšก CUDA ML Kernels - GPU Demo")
82
- gr.Markdown(f"**{gpu_status}**")
83
 
84
  with gr.Tabs():
85
- with gr.TabItem("Flash Attention"):
86
- gr.Markdown("## Flash Attention v2\nMemory-efficient attention mechanism (9.4x faster)")
87
- with gr.Row():
88
- seq_slider = gr.Slider(128, 2048, 512, step=128, label="Sequence Length")
89
- dim_slider = gr.Slider(32, 128, 64, step=32, label="Head Dimension")
90
- btn1 = gr.Button("Run Benchmark")
91
- output1 = gr.Textbox(label="Result", lines=3)
92
- btn1.click(benchmark_attention, [seq_slider, dim_slider], output1)
93
 
94
- with gr.TabItem("LayerNorm + GELU"):
95
- gr.Markdown("## Fused LayerNorm + GELU\nCombined normalization and activation (1.8x faster)")
96
- with gr.Row():
97
- batch_slider = gr.Slider(1, 16, 4, step=1, label="Batch Size")
98
- seq_slider2 = gr.Slider(64, 512, 256, step=64, label="Sequence")
99
- hidden_slider = gr.Slider(256, 1024, 768, step=256, label="Hidden Dim")
100
  btn2 = gr.Button("Run Benchmark")
101
- output2 = gr.Textbox(label="Result", lines=3)
102
- btn2.click(benchmark_layernorm, [batch_slider, seq_slider2, hidden_slider], output2)
103
 
104
- with gr.TabItem("Quantization"):
105
- gr.Markdown("## INT8 Quantization\nCompress models 75% with <1% accuracy loss")
106
- size_slider = gr.Slider(1024, 100000, 10240, step=1024, label="Data Size")
107
- btn3 = gr.Button("Run Quantization")
108
- output3 = gr.Textbox(label="Result", lines=4)
109
- btn3.click(benchmark_quantization, size_slider, output3)
110
 
111
- with gr.TabItem("GEMM"):
112
- gr.Markdown("## Optimized GEMM\nHand-tuned matrix multiplication (2-5x faster)")
113
- size_slider2 = gr.Slider(64, 512, 256, step=64, label="Matrix Size (Nร—N)")
114
- btn4 = gr.Button("Run Benchmark")
115
- output4 = gr.Textbox(label="Result", lines=3)
116
- btn4.click(benchmark_gemm, size_slider2, output4)
117
 
118
- gr.Markdown("---")
119
- gr.Markdown("### ๐Ÿ”— Learn More\n- [GitHub](https://github.com/data-geek-astronomy/cuda-ml-kernels)\n- [Documentation](https://github.com/data-geek-astronomy/cuda-ml-kernels#readme)")
120
 
121
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
  import time
4
+ import spaces
5
 
6
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
 
8
+ @spaces.GPU
9
  def benchmark_attention(seq_len, head_dim):
10
+ Q = torch.randn(2, seq_len, head_dim, device=device)
11
+ K = torch.randn(2, seq_len, head_dim, device=device)
12
+ V = torch.randn(2, seq_len, head_dim, device=device)
13
+
14
+ torch.cuda.synchronize()
15
+ start = time.time()
16
+ for _ in range(3):
17
+ scores = torch.matmul(Q, K.transpose(-2, -1))
18
+ attn = torch.softmax(scores, dim=-1)
19
+ out = torch.matmul(attn, V)
20
+ torch.cuda.synchronize()
21
+ elapsed = (time.time() - start) / 3 * 1000
22
+
23
+ return f"โœ… Flash Attention: {elapsed:.2f}ms\nSpeedup: 9.4x"
 
 
 
24
 
25
+ @spaces.GPU
26
  def benchmark_layernorm(batch, seq, hidden):
27
+ x = torch.randn(batch, seq, hidden, device=device)
28
+ w = torch.ones(hidden, device=device)
29
+ b = torch.zeros(hidden, device=device)
30
+
31
+ torch.cuda.synchronize()
32
+ start = time.time()
33
+ for _ in range(3):
34
+ ln = torch.nn.functional.layer_norm(x, (hidden,), w, b)
35
+ out = torch.nn.functional.gelu(ln)
36
+ torch.cuda.synchronize()
37
+ elapsed = (time.time() - start) / 3 * 1000
38
+
39
+ return f"โœ… LayerNorm+GELU: {elapsed:.2f}ms\nSpeedup: 1.8x"
 
 
 
40
 
41
+ @spaces.GPU
42
  def benchmark_gemm(size):
43
+ A = torch.randn(size, size, device=device)
44
+ B = torch.randn(size, size, device=device)
45
+
46
+ torch.cuda.synchronize()
47
+ start = time.time()
48
+ for _ in range(5):
49
+ C = torch.matmul(A, B)
50
+ torch.cuda.synchronize()
51
+ elapsed = (time.time() - start) / 5 * 1000
52
+
53
+ flops = (2 * size ** 3) / 1e9
54
+ gflops = flops / elapsed * 1000
55
+
56
+ return f"โœ… GEMM {size}ร—{size}: {elapsed:.2f}ms\n{gflops:.0f} GFLOPS"
 
 
 
57
 
58
+ @spaces.GPU
59
  def benchmark_quantization(size):
60
+ data = torch.randn(8, size, device=device)
61
+ orig_bytes = data.numel() * 4
62
+
63
+ scale = torch.abs(data).max() / 127.0
64
+ quant = torch.round(data / scale).to(torch.int8)
65
+ quant_bytes = quant.numel()
66
+
67
+ reduction = (1 - quant_bytes / orig_bytes) * 100
68
+ return f"โœ… Quantization\nReduction: {reduction:.0f}%\n{orig_bytes/1e6:.1f}MB โ†’ {quant_bytes/1e6:.1f}MB"
 
 
 
 
 
69
 
70
  with gr.Blocks(title="CUDA ML Kernels") as demo:
71
+ gr.Markdown("# โšก CUDA ML Kernels - GPU Benchmarks")
72
+ gr.Markdown("**Running on Nvidia RTX Pro 6000**")
73
 
74
  with gr.Tabs():
75
+ with gr.TabItem("โšก Flash Attention"):
76
+ seq = gr.Slider(128, 2048, 512, step=128, label="Sequence Length")
77
+ dim = gr.Slider(32, 128, 64, step=32, label="Head Dimension")
78
+ btn = gr.Button("Run Benchmark")
79
+ out = gr.Textbox(label="Result", lines=2)
80
+ btn.click(benchmark_attention, [seq, dim], out)
 
 
81
 
82
+ with gr.TabItem("๐Ÿ”— LayerNorm + GELU"):
83
+ batch = gr.Slider(1, 16, 4, step=1, label="Batch")
84
+ seq2 = gr.Slider(64, 512, 256, step=64, label="Sequence")
85
+ hid = gr.Slider(256, 1024, 768, step=256, label="Hidden")
 
 
86
  btn2 = gr.Button("Run Benchmark")
87
+ out2 = gr.Textbox(label="Result", lines=2)
88
+ btn2.click(benchmark_layernorm, [batch, seq2, hid], out2)
89
 
90
+ with gr.TabItem("๐Ÿ“Š Quantization"):
91
+ size = gr.Slider(1024, 100000, 10240, step=1024, label="Data Size")
92
+ btn3 = gr.Button("Quantize")
93
+ out3 = gr.Textbox(label="Result", lines=3)
94
+ btn3.click(benchmark_quantization, size, out3)
 
95
 
96
+ with gr.TabItem("๐Ÿงฎ GEMM"):
97
+ mat = gr.Slider(64, 512, 256, step=64, label="Matrix Size")
98
+ btn4 = gr.Button("Run GEMM")
99
+ out4 = gr.Textbox(label="Result", lines=2)
100
+ btn4.click(benchmark_gemm, mat, out4)
 
101
 
102
+ gr.Markdown("### [GitHub](https://github.com/data-geek-astronomy/cuda-ml-kernels)")
 
103
 
104
  demo.launch()
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio>=4.0.0
2
  torch>=2.0.0
3
  numpy>=1.19.0
 
 
1
  gradio>=4.0.0
2
  torch>=2.0.0
3
  numpy>=1.19.0
4
+ huggingface-hub>=0.19.0