Spaces:
Sleeping
Sleeping
Commit ยท
7e3cce8
1
Parent(s): 858e48f
Add @spaces.GPU decorator for HF Spaces GPU support
Browse files- app.py +80 -97
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,121 +1,104 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import time
|
| 4 |
-
import
|
| 5 |
|
| 6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
|
|
|
| 8 |
def benchmark_attention(seq_len, head_dim):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 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 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 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
|
| 82 |
-
gr.Markdown(
|
| 83 |
|
| 84 |
with gr.Tabs():
|
| 85 |
-
with gr.TabItem("Flash Attention"):
|
| 86 |
-
gr.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 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.
|
| 96 |
-
|
| 97 |
-
|
| 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 |
-
|
| 102 |
-
btn2.click(benchmark_layernorm, [
|
| 103 |
|
| 104 |
-
with gr.TabItem("Quantization"):
|
| 105 |
-
gr.
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
btn3.click(benchmark_quantization, size_slider, output3)
|
| 110 |
|
| 111 |
-
with gr.TabItem("GEMM"):
|
| 112 |
-
gr.
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 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
|