Spaces:
Running on Zero

File size: 3,122 Bytes
0a96270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Model registry and resource policy for the hosted RIBOSPAN services."""

from __future__ import annotations

import os
from dataclasses import dataclass


@dataclass(frozen=True)
class ModelSpec:
    name: str
    repo_id: str
    trained_context_window: int
    revision: str


MODEL_REGISTRY = {
    "RIBOSPAN-1K-40": ModelSpec(
        name="RIBOSPAN-1K-40",
        repo_id="SII-GAIR-NLP/RIBOSPAN-1K-40",
        trained_context_window=1024,
        revision=os.environ.get("RIBOSPAN_1K40_REVISION", "main"),
    ),
    "RIBOSPAN-10K-15": ModelSpec(
        name="RIBOSPAN-10K-15",
        repo_id="SII-GAIR-NLP/RIBOSPAN-10K-15",
        trained_context_window=10240,
        revision=os.environ.get("RIBOSPAN_10K15_REVISION", "main"),
    ),
}

NATIVE_SHORT = 1024
NATIVE_LONG = 10240
SHORT_MODEL = "RIBOSPAN-1K-40"
LONG_MODEL = "RIBOSPAN-10K-15"
TASKS = ("embed", "fill", "variant")


def select_model(task: str, sequence_length: int) -> ModelSpec:
    """Select the task-appropriate checkpoint without extrapolating 1K models."""

    if task not in TASKS:
        raise ValueError(f"unsupported task {task!r}")
    if sequence_length < 1:
        raise ValueError("sequence_length must be positive")
    name = LONG_MODEL if task == "embed" or sequence_length > NATIVE_SHORT else SHORT_MODEL
    return MODEL_REGISTRY[name]


def _positive_int(name: str, default: int) -> int:
    value = int(os.environ.get(name, str(default)))
    if value < 1:
        raise ValueError(f"{name} must be positive")
    return value


@dataclass(frozen=True)
class ResourceLimits:
    max_records: int = 128
    max_total_nt: int = 1000000
    max_variants: int = 10000
    max_mask_patterns: int = 512
    max_forward_tokens: int = 262144
    max_attention_cells: int = 16 * (NATIVE_LONG + 2) ** 2
    max_gpu_seconds: int = 120
    max_masked_sites: int = 40000
    max_variant_chars: int = 2000000
    batch_size: int = 8
    file_ttl_seconds: int = 3600
    min_free_disk_bytes: int = 2 * 1024**3

    @classmethod
    def from_env(cls) -> ResourceLimits:
        return cls(
            max_records=_positive_int("RIBOSPAN_MAX_RECORDS", 128),
            max_total_nt=_positive_int("RIBOSPAN_MAX_TOTAL_NT", 1000000),
            max_variants=_positive_int("RIBOSPAN_MAX_VARIANTS", 10000),
            max_mask_patterns=_positive_int("RIBOSPAN_MAX_MASK_PATTERNS", 512),
            max_forward_tokens=_positive_int("RIBOSPAN_MAX_FORWARD_TOKENS", 262144),
            max_attention_cells=_positive_int("RIBOSPAN_MAX_ATTENTION_CELLS", 16 * (NATIVE_LONG + 2) ** 2),
            max_gpu_seconds=_positive_int("RIBOSPAN_MAX_GPU_SECONDS", 120),
            max_masked_sites=_positive_int("RIBOSPAN_MAX_MASKED_SITES", 40000),
            max_variant_chars=_positive_int("RIBOSPAN_MAX_VARIANT_CHARS", 2000000),
            batch_size=_positive_int("RIBOSPAN_BATCH_SIZE", 8),
            file_ttl_seconds=_positive_int("RIBOSPAN_FILE_TTL_SECONDS", 3600),
            min_free_disk_bytes=_positive_int("RIBOSPAN_MIN_FREE_DISK_BYTES", 2 * 1024**3),
        )


LIMITS = ResourceLimits.from_env()