File size: 1,269 Bytes
4b10a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)