the-stack-go / stackgo /stackbuild /tokenizer.py
s0duku's picture
Add files using upload-large-folder tool
4b10a0d verified
Raw History Blame Contribute Delete
1.27 kB
"""DeepSeek V4 Flash tokenizer defaults."""
from __future__ import annotations
from functools import lru_cache
import os
from pathlib import Path
DEFAULT_TOKENIZER_ID = "deepseek-ai/DeepSeek-V4-Flash-Base"
DEFAULT_TOKENIZER_REVISION = "8855555deef230a27a21a8d6f294b7b7497759b6"
@lru_cache(maxsize=4)
def load_tokenizer(name_or_path: str = DEFAULT_TOKENIZER_ID, *, local_files_only: bool = False):
from transformers import AutoTokenizer
requested = name_or_path
if requested == DEFAULT_TOKENIZER_ID:
candidates = [
Path(os.environ["STACKBUILD_TOKENIZER_PATH"]).expanduser()
if "STACKBUILD_TOKENIZER_PATH" in os.environ else None,
Path(__file__).resolve().parents[2] / "tokenizers" / "deepseek-v4-flash-base",
]
local = next((path for path in candidates if path is not None and path.is_dir()), None)
source = str(local) if local is not None else requested
else:
source = str(Path(requested).expanduser()) if Path(requested).exists() else requested
kwargs = {"use_fast": True, "local_files_only": local_files_only}
if source == DEFAULT_TOKENIZER_ID:
kwargs["revision"] = DEFAULT_TOKENIZER_REVISION
return AutoTokenizer.from_pretrained(source, **kwargs)