File size: 14,181 Bytes
46d70d9 0cf2f43 46d70d9 | 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | import { useEffect, useRef, useState } from "react";
import {
Check,
ChevronDown,
Cpu,
ExternalLink,
RefreshCw,
Server,
Sparkles,
Zap,
} from "lucide-react";
import { useShallow } from "zustand/react/shallow";
import { useOstraStore } from "../../store/useOstraStore";
import {
resolveComputeTarget,
type ComputePreference,
} from "../../utils/browserCompute";
interface ComputeDeviceSelectorProps {
compact?: boolean;
disabled?: boolean;
}
/**
* Unified Compute / GPU Selector Dropdown listing ALL available compute targets:
* 1. Auto Cascade (WebGPU Metal/GPU -> Browser CPU WASM -> HF Space Server)
* 2. Every detected client WebGPU GPU adapter (Apple Metal, NVIDIA, AMD, Intel)
* 3. Client Browser CPU (WASM)
* 4. Every available Server backend (CUDA GPU, Apple MLX, PyTorch MPS, HF Space CPU)
*
* When running inside a Hugging Face Space iframe where the browser blocks WebGPU,
* surfaces a 1-click "Open Direct .hf.space Tab" link to unlock on-device Metal/WebGPU.
*/
export function ComputeDeviceSelector({
compact = false,
disabled = false,
}: ComputeDeviceSelectorProps) {
const {
hardware,
browserCompute,
computePref,
setComputePref,
refreshBrowserCompute,
} = useOstraStore(
useShallow((s) => ({
hardware: s.hardware,
browserCompute: s.browserCompute,
computePref: s.computePref,
setComputePref: s.setComputePref,
refreshBrowserCompute: s.refreshBrowserCompute,
}))
);
const [open, setOpen] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const onDocMouseDown = (ev: MouseEvent) => {
if (
containerRef.current &&
!containerRef.current.contains(ev.target as Node)
) {
setOpen(false);
}
};
document.addEventListener("mousedown", onDocMouseDown);
return () => document.removeEventListener("mousedown", onDocMouseDown);
}, [open]);
const resolved = resolveComputeTarget(computePref, browserCompute, hardware);
const isHfSpace = Boolean(hardware?.is_hf_space);
const toneClasses =
resolved.tone === "emerald"
? "bg-emerald-50 border-emerald-700 text-emerald-950 hover:bg-emerald-100"
: resolved.tone === "sky"
? "bg-sky-50 border-sky-700 text-sky-950 hover:bg-sky-100"
: "bg-[#f1f4f8] border-[#0f172a] text-[#0f172a] hover:bg-[#e8edf3]";
const pick = (next: ComputePreference) => {
setComputePref(next);
setOpen(false);
};
const handleRefresh = async () => {
setRefreshing(true);
try {
await refreshBrowserCompute();
} finally {
setRefreshing(false);
}
};
const serverOptions: Array<{
id: ComputePreference;
label: string;
detail: string;
isGpu: boolean;
}> = [];
if (hardware?.cuda_available) {
serverOptions.push({
id: "server:cuda",
label: isHfSpace ? "HF Space · CUDA GPU" : "Server · CUDA GPU",
detail: "PyTorch CUDA hardware acceleration on server",
isGpu: true,
});
}
if (hardware?.mlx_embeddings_available) {
serverOptions.push({
id: "server:mlx",
label: hardware.device_label || "Apple Silicon MLX",
detail: "Native local Apple Silicon MLX backend",
isGpu: true,
});
}
if (hardware?.mps_available) {
serverOptions.push({
id: "server:mps",
label: "Server · PyTorch MPS",
detail: "Local Apple Metal via PyTorch MPS",
isGpu: true,
});
}
serverOptions.push({
id: "server:cpu",
label: isHfSpace
? `HF Space · CPU (${hardware?.cpu_count ?? 2} vCPU)`
: `Server · CPU (${hardware?.cpu_count ?? 2} cores)`,
detail: isHfSpace
? "Hugging Face Space server fallback (works with all PyTorch models)"
: "PyTorch CPU backend on server",
isGpu: false,
});
const directSpaceUrl =
typeof window !== "undefined" ? window.location.href : "";
return (
<div ref={containerRef} className="relative">
<button
type="button"
disabled={disabled}
onClick={() => setOpen((o) => !o)}
title="Select Compute Device / GPU (WebGPU Metal, Browser WASM CPU, or HF Space Server)"
className={`inline-flex items-center justify-between gap-1.5 border-[1.5px] font-mono font-semibold cursor-pointer transition-colors disabled:opacity-60 ${toneClasses} ${
compact
? "h-7 px-2 text-[10px] max-w-[195px]"
: "w-full px-2.5 py-1.5 text-xs"
}`}
>
<span className="inline-flex items-center gap-1.5 min-w-0">
{resolved.isWebGPU ? (
<Zap className="w-3 h-3 shrink-0 text-emerald-700" />
) : resolved.isClient ? (
<Cpu className="w-3 h-3 shrink-0 text-sky-700" />
) : (
<Server className="w-3 h-3 shrink-0 text-[#475569]" />
)}
<span className="truncate">
{compact ? resolved.shortLabel : resolved.label}
</span>
</span>
<ChevronDown className="w-3 h-3 shrink-0 opacity-75" />
</button>
{open && (
<div
className={`absolute top-full mt-1 z-50 bg-white border-[1.5px] border-[#0f172a] shadow-[4px_4px_0_rgba(15,23,42,0.2)] flex flex-col text-left ${
compact ? "right-0 w-[330px]" : "left-0 right-0 min-w-[310px]"
}`}
>
{/* Header */}
<div className="px-2.5 py-1.5 bg-[#f1f4f8] border-b border-[#cbd5e1] flex items-center justify-between gap-2">
<span className="font-mono text-[9px] font-bold uppercase tracking-wider text-[#334155]">
Available GPUs & Compute Engines
</span>
<button
type="button"
onClick={() => void handleRefresh()}
disabled={refreshing}
title="Re-scan browser WebGPU adapters"
className="inline-flex items-center gap-1 px-1.5 py-0.5 bg-white border border-[#94a3b8] hover:border-[#0f172a] font-mono text-[9px] text-[#334155] cursor-pointer"
>
<RefreshCw
className={`w-2.5 h-2.5 ${refreshing ? "animate-spin" : ""}`}
/>
<span>Scan GPUs</span>
</button>
</div>
<div className="max-h-80 overflow-y-auto divide-y divide-[#e2e8f0]">
{/* 1. Auto Cascade */}
<button
type="button"
onClick={() => pick("auto")}
className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${
computePref === "auto" ? "bg-emerald-50" : "hover:bg-[#f8fafc]"
}`}
>
<div className="flex items-center justify-between gap-2">
<span className="font-mono text-[11px] font-bold text-[#0f172a] inline-flex items-center gap-1.5">
<Sparkles className="w-3 h-3 text-emerald-700" />
<span>Auto Cascade (Recommended)</span>
</span>
{computePref === "auto" && (
<Check className="w-3.5 h-3.5 text-emerald-700 shrink-0" />
)}
</div>
<div className="font-mono text-[9px] text-[#475569] mt-0.5">
{browserCompute?.webgpuAvailable
? `WebGPU (${browserCompute.isAppleMetal ? "Apple Metal" : "GPU"}) → Browser CPU → ${isHfSpace ? "HF Space" : "Server"}`
: browserCompute?.wasmAvailable
? `Browser CPU (WASM) → ${isHfSpace ? "HF Space" : "Server"}`
: hardware?.device_label || "Server Backend"}
</div>
</button>
{/* 2. Detected Consumer GPUs (WebGPU) */}
<div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-emerald-900">
Your Device GPUs (WebGPU / Metal)
</div>
{browserCompute && browserCompute.adapters.length > 0 ? (
browserCompute.adapters.map((adapter) => {
const selected = computePref === adapter.id;
return (
<button
key={adapter.id}
type="button"
onClick={() => pick(adapter.id)}
className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${
selected ? "bg-emerald-50" : "hover:bg-[#f8fafc]"
}`}
>
<div className="flex items-center justify-between gap-2">
<span className="font-mono text-[11px] font-bold text-emerald-950 inline-flex items-center gap-1.5 min-w-0">
<Zap className="w-3 h-3 text-emerald-700 shrink-0" />
<span className="truncate">{adapter.label}</span>
</span>
{selected && (
<Check className="w-3.5 h-3.5 text-emerald-700 shrink-0" />
)}
</div>
<div className="font-mono text-[9px] text-[#475569] mt-0.5 flex items-center gap-1.5 flex-wrap">
<span>{adapter.vendor}</span>
{adapter.architecture && (
<span>· {adapter.architecture}</span>
)}
{adapter.isAppleMetal && (
<span className="px-1 bg-emerald-100 border border-emerald-700 text-emerald-900 font-bold">
Metal
</span>
)}
<span>· Runs in Web Worker (0% Space CPU)</span>
</div>
</button>
);
})
) : (
<div className="px-2.5 py-2 space-y-1.5 bg-amber-50/70">
<div className="font-mono text-[10px] font-semibold text-amber-950">
No WebGPU adapter detected in this browser view
</div>
<div className="text-[10px] text-amber-900 leading-snug">
{browserCompute?.webgpuUnavailableReason ||
"Checking browser GPU capabilities…"}
</div>
{browserCompute?.webglRenderer && (
<div className="font-mono text-[9px] text-[#475569] truncate">
Detected WebGL GPU: {browserCompute.webglRenderer}
</div>
)}
{browserCompute?.inIframe && directSpaceUrl && (
<a
href={directSpaceUrl}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 px-2 py-1 bg-white border border-[#0f172a] font-mono text-[10px] font-bold text-[#0f172a] hover:bg-emerald-50"
>
<ExternalLink className="w-3 h-3 text-emerald-700" />
<span>Open Direct .hf.space Tab to Unlock WebGPU</span>
</a>
)}
</div>
)}
{/* 3. Consumer Browser CPU (WASM) */}
<div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-sky-900">
Your Device CPU (Browser WASM)
</div>
<button
type="button"
disabled={!browserCompute?.wasmAvailable}
onClick={() => pick("wasm")}
className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors disabled:opacity-50 ${
computePref === "wasm" ? "bg-sky-50" : "hover:bg-[#f8fafc]"
}`}
>
<div className="flex items-center justify-between gap-2">
<span className="font-mono text-[11px] font-bold text-sky-950 inline-flex items-center gap-1.5">
<Cpu className="w-3 h-3 text-sky-700" />
<span>Browser CPU (ONNX WASM SIMD)</span>
</span>
{computePref === "wasm" && (
<Check className="w-3.5 h-3.5 text-sky-700 shrink-0" />
)}
</div>
<div className="font-mono text-[9px] text-[#475569] mt-0.5">
Runs on your machine's CPU in a Web Worker · works inside HF Space iframes
</div>
</button>
{/* 4. Server GPUs & CPU */}
<div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-[#334155]">
{isHfSpace ? "Hugging Face Space Server" : "Local Backend Server"}
</div>
{serverOptions.map((opt) => {
const selected = computePref === opt.id;
return (
<button
key={opt.id}
type="button"
onClick={() => pick(opt.id)}
className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${
selected ? "bg-[#f1f4f8]" : "hover:bg-[#f8fafc]"
}`}
>
<div className="flex items-center justify-between gap-2">
<span className="font-mono text-[11px] font-bold text-[#0f172a] inline-flex items-center gap-1.5">
{opt.isGpu ? (
<Zap className="w-3 h-3 text-emerald-700" />
) : (
<Server className="w-3 h-3 text-[#475569]" />
)}
<span>{opt.label}</span>
</span>
{selected && (
<Check className="w-3.5 h-3.5 text-[#0f172a] shrink-0" />
)}
</div>
<div className="font-mono text-[9px] text-[#475569] mt-0.5">
{opt.detail}
</div>
</button>
);
})}
</div>
</div>
)}
</div>
);
}
|