File size: 2,929 Bytes
4a9362b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Explicit user resource amendments, separate from frozen scientific protocols."""

from pathlib import Path

from . import common
from .common import Campaign, atomic_json, digest, read


class AuthorizedCampaign(Campaign):
    """Apply only the explicitly authorized RAM increase to the unchanged resource guard."""

    def __init__(self, protocol: Path) -> None:
        super().__init__(protocol)
        amendment = self.out / "RESOURCE_AUTHORIZATION.json"
        if not amendment.exists():
            return
        value = read(amendment)
        if (
            value["parent_policy_sha256"] != digest(self.policy_path)
            or value["parent_protocol_sha256"] != digest(self.out / "PROTOCOL.json")
            or value["max_tree_rss_bytes"] != 12_000_000_000
            or value["only_change"] != "RAM_limit"
        ):
            raise ValueError("RAM amendment is not bound to the original campaign authorization")
        self.policy = dict(self.policy)
        self.policy["max_tree_rss_gib"] = value["max_tree_rss_bytes"] / 1024**3
        atomic_json(
            self.out / "ACTIVE_RESOURCE_POLICY.json",
            {
                "amendment_sha256": digest(amendment),
                "max_tree_rss_bytes": value["max_tree_rss_bytes"],
                "active_policy": self.policy,
                "scientific_protocol_unchanged": True,
                "global_environment_modified": False,
            },
        )
        extra = self.out / "RESOURCE_AUTHORIZATION_V2.json"
        if extra.exists():
            extension = read(extra)
            if (
                extension["parent_amendment_sha256"] != digest(amendment)
                or extension["parent_protocol_sha256"] != digest(self.out / "PROTOCOL.json")
                or extension["max_tree_rss_bytes"] != 16_000_000_000
                or extension["CPU_preparation_workers"] != 4
                or extension["scientific_recipe_changed"]
            ):
                raise ValueError("additional resource authorization is not bound to this campaign")
            self.policy["max_tree_rss_gib"] = extension["max_tree_rss_bytes"] / 1024**3
            atomic_json(
                self.out / "ACTIVE_RESOURCE_POLICY.json",
                {
                    "amendment_sha256": digest(extra),
                    "previous_amendment_sha256": digest(amendment),
                    "max_tree_rss_bytes": extension["max_tree_rss_bytes"],
                    "CPU_preparation_workers": extension["CPU_preparation_workers"],
                    "active_policy": self.policy,
                    "scientific_protocol_unchanged": True,
                    "global_environment_modified": False,
                    "heavy_trainers": 1,
                },
            )


def install() -> None:
    """Bind new workers without editing the frozen historical/scientific source files."""
    common.Campaign = AuthorizedCampaign