space: bootstrap mamba-ssm/causal-conv1d at runtime for Nemotron-H
Browse files- space/app.py +36 -0
space/app.py
CHANGED
|
@@ -64,8 +64,44 @@ tok = None
|
|
| 64 |
model = None
|
| 65 |
WARDEN_ERR = "spaces package not present (not on a ZeroGPU Space)"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if spaces is not None:
|
| 68 |
try:
|
|
|
|
| 69 |
import torch
|
| 70 |
from transformers import (
|
| 71 |
AutoModelForCausalLM,
|
|
|
|
| 64 |
model = None
|
| 65 |
WARDEN_ERR = "spaces package not present (not on a ZeroGPU Space)"
|
| 66 |
|
| 67 |
+
def _ensure_mamba_kernels() -> None:
|
| 68 |
+
"""Nemotron-H's modeling code hard-imports mamba_ssm's triton kernels.
|
| 69 |
+
Neither mamba-ssm nor causal-conv1d can sit in requirements.txt (their
|
| 70 |
+
builds import torch, which pip's isolated build env doesn't have), so
|
| 71 |
+
bootstrap here: first try the full install — their setup.py fetches a
|
| 72 |
+
prebuilt wheel matching torch/cuda/python when one exists — then fall
|
| 73 |
+
back to a kernels-skipped mamba-ssm (pure triton, no causal-conv1d:
|
| 74 |
+
half-installed causal-conv1d would crash the modeling import, absent
|
| 75 |
+
causal-conv1d just disables the fast path)."""
|
| 76 |
+
import subprocess
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
from mamba_ssm.ops.triton.layernorm_gated import rmsnorm_fn # noqa: F401
|
| 80 |
+
|
| 81 |
+
return
|
| 82 |
+
except ImportError:
|
| 83 |
+
pass
|
| 84 |
+
base = [sys.executable, "-m", "pip", "install", "--no-build-isolation"]
|
| 85 |
+
full = subprocess.run(
|
| 86 |
+
base + ["causal-conv1d", "mamba-ssm"], capture_output=True, timeout=900
|
| 87 |
+
)
|
| 88 |
+
if full.returncode == 0:
|
| 89 |
+
return
|
| 90 |
+
subprocess.run(
|
| 91 |
+
[sys.executable, "-m", "pip", "uninstall", "-y", "causal-conv1d"],
|
| 92 |
+
capture_output=True,
|
| 93 |
+
)
|
| 94 |
+
subprocess.run(
|
| 95 |
+
base + ["mamba-ssm"],
|
| 96 |
+
capture_output=True,
|
| 97 |
+
timeout=900,
|
| 98 |
+
env={**os.environ, "MAMBA_SKIP_CUDA_BUILD": "TRUE"},
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
if spaces is not None:
|
| 103 |
try:
|
| 104 |
+
_ensure_mamba_kernels()
|
| 105 |
import torch
|
| 106 |
from transformers import (
|
| 107 |
AutoModelForCausalLM,
|