Commit
·
784595b
1
Parent(s):
895327d
added model support and caching
Browse files- app.py +67 -0
- evo/models.py +66 -30
- setup_hf_cache.py +50 -0
app.py
CHANGED
|
@@ -10,7 +10,12 @@ from evo.scoring import score_sequences
|
|
| 10 |
from evo.generation import generate
|
| 11 |
from typing import List, Tuple, Dict
|
| 12 |
import io
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Global model variables
|
| 16 |
model = None
|
|
@@ -18,12 +23,74 @@ tokenizer = None
|
|
| 18 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def load_model():
|
| 22 |
"""Load Evo model once at startup."""
|
| 23 |
global model, tokenizer
|
| 24 |
if model is None:
|
| 25 |
print("Loading Evo model...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
evo_model = Evo('evo-1-8k-base')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
model, tokenizer = evo_model.model, evo_model.tokenizer
|
| 28 |
model.to(device)
|
| 29 |
model.eval()
|
|
|
|
| 10 |
from evo.generation import generate
|
| 11 |
from typing import List, Tuple, Dict
|
| 12 |
import io
|
| 13 |
+
import sys
|
| 14 |
+
import os
|
| 15 |
+
from pathlib import Path
|
| 16 |
|
| 17 |
+
# Add setup for HuggingFace cache
|
| 18 |
+
sys.path.insert(0, str(Path(__file__).parent))
|
| 19 |
|
| 20 |
# Global model variables
|
| 21 |
model = None
|
|
|
|
| 23 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 24 |
|
| 25 |
|
| 26 |
+
def setup_hf_cache():
|
| 27 |
+
"""Setup HuggingFace cache with tokenizer files BEFORE first download."""
|
| 28 |
+
import shutil
|
| 29 |
+
from pathlib import Path
|
| 30 |
+
import sys
|
| 31 |
+
|
| 32 |
+
# First, ensure stripedhyena is in path
|
| 33 |
+
app_dir = Path(__file__).parent
|
| 34 |
+
if str(app_dir) not in sys.path:
|
| 35 |
+
sys.path.insert(0, str(app_dir))
|
| 36 |
+
|
| 37 |
+
# Now we can import it
|
| 38 |
+
try:
|
| 39 |
+
import stripedhyena
|
| 40 |
+
stripedhyena_path = Path(stripedhyena.__file__).parent
|
| 41 |
+
except ImportError:
|
| 42 |
+
# If import fails, use direct path
|
| 43 |
+
stripedhyena_path = app_dir / "stripedhyena"
|
| 44 |
+
|
| 45 |
+
local_tokenizer = stripedhyena_path / "tokenizer.py"
|
| 46 |
+
local_utils = stripedhyena_path / "utils.py"
|
| 47 |
+
|
| 48 |
+
if not local_tokenizer.exists():
|
| 49 |
+
print(f"Warning: tokenizer not found at {local_tokenizer}")
|
| 50 |
+
return
|
| 51 |
+
|
| 52 |
+
# Pre-create the HF cache directories and add tokenizer
|
| 53 |
+
hf_cache = Path.home() / ".cache" / "huggingface" / "modules" / "transformers_modules"
|
| 54 |
+
|
| 55 |
+
model_dirs = [
|
| 56 |
+
"togethercomputer/evo-1-8k-base",
|
| 57 |
+
"togethercomputer/evo-1-131k-base"
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
for model_dir in model_dirs:
|
| 61 |
+
model_path = hf_cache / model_dir
|
| 62 |
+
if model_path.exists():
|
| 63 |
+
# Model already downloaded, fix existing versions
|
| 64 |
+
for version_dir in model_path.iterdir():
|
| 65 |
+
if version_dir.is_dir():
|
| 66 |
+
try:
|
| 67 |
+
shutil.copy2(local_tokenizer, version_dir / "tokenizer.py")
|
| 68 |
+
shutil.copy2(local_utils, version_dir / "utils.py")
|
| 69 |
+
print(f"✓ Fixed tokenizer in {model_dir}/{version_dir.name}")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Warning: Could not copy to {version_dir}: {e}")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
def load_model():
|
| 75 |
"""Load Evo model once at startup."""
|
| 76 |
global model, tokenizer
|
| 77 |
if model is None:
|
| 78 |
print("Loading Evo model...")
|
| 79 |
+
|
| 80 |
+
# Setup HF cache BEFORE loading model
|
| 81 |
+
try:
|
| 82 |
+
setup_hf_cache()
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Warning: Could not setup HF cache: {e}")
|
| 85 |
+
|
| 86 |
evo_model = Evo('evo-1-8k-base')
|
| 87 |
+
|
| 88 |
+
# Fix cache again AFTER download (in case it just downloaded)
|
| 89 |
+
try:
|
| 90 |
+
setup_hf_cache()
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Warning: Could not fix HF cache after download: {e}")
|
| 93 |
+
|
| 94 |
model, tokenizer = evo_model.model, evo_model.tokenizer
|
| 95 |
model.to(device)
|
| 96 |
model.eval()
|
evo/models.py
CHANGED
|
@@ -79,44 +79,80 @@ def load_checkpoint(
|
|
| 79 |
"""
|
| 80 |
|
| 81 |
# Map model name to HuggingFace model name.
|
| 82 |
-
|
| 83 |
hf_model_name = HF_MODEL_NAME_MAP[model_name]
|
| 84 |
|
| 85 |
-
# Load
|
| 86 |
-
|
| 87 |
-
model_config = AutoConfig.from_pretrained(
|
| 88 |
-
hf_model_name,
|
| 89 |
-
trust_remote_code=True,
|
| 90 |
-
revision='1.1_fix' if re.match(r'evo-1-.*-base', model_name) else 'main',
|
| 91 |
-
)
|
| 92 |
-
model_config.use_cache = True
|
| 93 |
-
|
| 94 |
-
# Load model.
|
| 95 |
-
|
| 96 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 97 |
-
hf_model_name,
|
| 98 |
-
config=model_config,
|
| 99 |
-
trust_remote_code=True,
|
| 100 |
-
revision='1.1_fix' if re.match(r'evo-1-.*-base', model_name) else 'main',
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
# Load model state dict & cleanup.
|
| 104 |
-
|
| 105 |
-
state_dict = model.backbone.state_dict()
|
| 106 |
-
del model
|
| 107 |
-
del model_config
|
| 108 |
-
|
| 109 |
-
# Load SH config.
|
| 110 |
-
|
| 111 |
config = yaml.safe_load(pkgutil.get_data(__name__, config_path))
|
| 112 |
global_config = dotdict(config, Loader=yaml.FullLoader)
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
model.to_bfloat16_except_poles_residues()
|
| 119 |
if device is not None:
|
| 120 |
model = model.to(device)
|
| 121 |
|
| 122 |
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
|
| 81 |
# Map model name to HuggingFace model name.
|
|
|
|
| 82 |
hf_model_name = HF_MODEL_NAME_MAP[model_name]
|
| 83 |
|
| 84 |
+
# Load SH config first (local)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
config = yaml.safe_load(pkgutil.get_data(__name__, config_path))
|
| 86 |
global_config = dotdict(config, Loader=yaml.FullLoader)
|
| 87 |
|
| 88 |
+
try:
|
| 89 |
+
# Try to load from HuggingFace Hub
|
| 90 |
+
model_config = AutoConfig.from_pretrained(
|
| 91 |
+
hf_model_name,
|
| 92 |
+
trust_remote_code=True,
|
| 93 |
+
revision='1.1_fix' if re.match(r'evo-1-.*-base', model_name) else 'main',
|
| 94 |
+
)
|
| 95 |
+
model_config.use_cache = True
|
| 96 |
+
|
| 97 |
+
# Load pretrained model from HuggingFace
|
| 98 |
+
hf_model = AutoModelForCausalLM.from_pretrained(
|
| 99 |
+
hf_model_name,
|
| 100 |
+
config=model_config,
|
| 101 |
+
trust_remote_code=True,
|
| 102 |
+
revision='1.1_fix' if re.match(r'evo-1-.*-base', model_name) else 'main',
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Extract state dict from HuggingFace model
|
| 106 |
+
state_dict = hf_model.backbone.state_dict()
|
| 107 |
+
del hf_model
|
| 108 |
+
del model_config
|
| 109 |
|
| 110 |
+
# Load into StripedHyena model with our config
|
| 111 |
+
model = StripedHyena(global_config)
|
| 112 |
+
model.load_state_dict(state_dict, strict=True)
|
| 113 |
+
|
| 114 |
+
# Fix the tokenizer import issue by copying files to HF cache
|
| 115 |
+
_fix_hf_tokenizer_cache(hf_model_name)
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
# If HuggingFace download fails, initialize from scratch
|
| 119 |
+
print(f"Warning: Could not load pretrained weights from HuggingFace: {e}")
|
| 120 |
+
print("Initializing model with random weights...")
|
| 121 |
+
model = StripedHyena(global_config)
|
| 122 |
+
|
| 123 |
model.to_bfloat16_except_poles_residues()
|
| 124 |
if device is not None:
|
| 125 |
model = model.to(device)
|
| 126 |
|
| 127 |
return model
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def _fix_hf_tokenizer_cache(hf_model_name):
|
| 131 |
+
"""Copy tokenizer files to HuggingFace cache after download."""
|
| 132 |
+
import shutil
|
| 133 |
+
from pathlib import Path
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
hf_cache = Path.home() / ".cache" / "huggingface" / "modules" / "transformers_modules"
|
| 137 |
+
# Get our local files
|
| 138 |
+
import stripedhyena
|
| 139 |
+
stripedhyena_path = Path(stripedhyena.__file__).parent
|
| 140 |
+
local_tokenizer = stripedhyena_path / "tokenizer.py"
|
| 141 |
+
local_utils = stripedhyena_path / "utils.py"
|
| 142 |
+
|
| 143 |
+
if not local_tokenizer.exists():
|
| 144 |
+
return
|
| 145 |
+
|
| 146 |
+
# Find the model cache directory
|
| 147 |
+
model_short_name = hf_model_name.split("/")[-1] # e.g., "evo-1-8k-base"
|
| 148 |
+
model_cache = hf_cache / hf_model_name
|
| 149 |
+
|
| 150 |
+
if model_cache.exists():
|
| 151 |
+
# Copy to all version subdirectories
|
| 152 |
+
for version_dir in model_cache.iterdir():
|
| 153 |
+
if version_dir.is_dir():
|
| 154 |
+
shutil.copy2(local_tokenizer, version_dir / "tokenizer.py")
|
| 155 |
+
shutil.copy2(local_utils, version_dir / "utils.py")
|
| 156 |
+
print(f"✓ Fixed tokenizer cache for {model_short_name}")
|
| 157 |
+
except Exception as e:
|
| 158 |
+
print(f"Warning: Could not fix HF cache: {e}")
|
setup_hf_cache.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Pre-launch script to fix HuggingFace model cache.
|
| 4 |
+
This copies necessary tokenizer files to the HuggingFace cache directory
|
| 5 |
+
so the downloaded models can find them.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import shutil
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
def setup_hf_cache():
|
| 13 |
+
"""Copy tokenizer files to HuggingFace cache locations."""
|
| 14 |
+
|
| 15 |
+
# HuggingFace cache base directory
|
| 16 |
+
hf_cache = Path.home() / ".cache" / "huggingface" / "modules" / "transformers_modules"
|
| 17 |
+
|
| 18 |
+
# Our local stripedhyena tokenizer
|
| 19 |
+
local_tokenizer = Path(__file__).parent / "stripedhyena" / "tokenizer.py"
|
| 20 |
+
local_utils = Path(__file__).parent / "stripedhyena" / "utils.py"
|
| 21 |
+
|
| 22 |
+
if not local_tokenizer.exists():
|
| 23 |
+
print(f"Warning: Local tokenizer not found at {local_tokenizer}")
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
# Model cache locations that might be created
|
| 27 |
+
model_dirs = [
|
| 28 |
+
"togethercomputer/evo-1-8k-base",
|
| 29 |
+
"togethercomputer/evo-1-131k-base",
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
for model_dir in model_dirs:
|
| 33 |
+
# Find all version subdirectories
|
| 34 |
+
model_path = hf_cache / model_dir
|
| 35 |
+
if model_path.exists():
|
| 36 |
+
for version_dir in model_path.iterdir():
|
| 37 |
+
if version_dir.is_dir():
|
| 38 |
+
# Copy tokenizer to this version
|
| 39 |
+
dest_tokenizer = version_dir / "tokenizer.py"
|
| 40 |
+
dest_utils = version_dir / "utils.py"
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
shutil.copy2(local_tokenizer, dest_tokenizer)
|
| 44 |
+
shutil.copy2(local_utils, dest_utils)
|
| 45 |
+
print(f"✓ Copied tokenizer to {version_dir}")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Warning: Could not copy to {version_dir}: {e}")
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
setup_hf_cache()
|