File size: 2,731 Bytes
672ecc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import copy
import hashlib
import json
import math
from collections import defaultdict
from pathlib import Path
import torch
from torch import nn
from torch.nn import functional as F
from world import D
REVISION = 'world-update-v3-routed-suite-listwise-20261006-r2'
PUBLIC = 'per_public_test'
GLOBAL = 'whole_training_suite'

class SemanticExecutionWorld(nn.Module):

    def __init__(self, public_world):
        super().__init__()
        if public_world.mode != 'opf':
            raise ValueError('Extension requires the native OPF world')
        self.public_world = public_world
        self.suite_readout = copy.deepcopy(public_world.readout)
        old = self.suite_readout[-1]
        final = nn.Linear(old.in_features, 7, device=old.weight.device, dtype=old.weight.dtype)
        with torch.no_grad():
            final.weight[:6].copy_(old.weight)
            final.bias[:6].copy_(old.bias)
            final.weight[6].zero_()
            final.bias[6].zero_()
        self.suite_readout[-1] = final

    @torch.no_grad()
    def score_suite(self, contexts):
        self.eval()
        encoded = self.public_world.encode(contexts)
        (latent, _) = self.public_world.predict_latent(encoded)
        output = self.suite_readout(latent)
        probabilities = F.softmax(output[:, :5], dim=-1)
        partial = torch.sigmoid(output[:, 6])
        return dict(scope=GLOBAL, full_suite_pass_probability=probabilities[:, 0], predicted_pass_fraction=partial, utility=probabilities[:, 0] + 0.25 * partial, failure_status_probabilities=probabilities, predicted_mean_normalized_seconds=F.softplus(output[:, 5]), calibration_verified=False, semantic_correctness_guaranteed=False)

    @torch.no_grad()
    def score_public(self, contexts):
        return self.public_world.score(contexts)

    @classmethod
    def load(cls, path, base_path, device='cpu'):
        from artifacts import checkpoint_manifest
        from world import ExecutionWorld
        (manifest, identity) = checkpoint_manifest(path)
        if manifest['mode'] != 'semantic_opf':
            raise ValueError('Unexpected model architecture')
        world = ExecutionWorld.load(Path(path) / 'public_world', base_path, device=device)
        if manifest['arithmetic_precision'] == 'float32':
            world.float()
        elif manifest['arithmetic_precision'] != 'native_bfloat16_backbone_float32_domain':
            raise ValueError('Unknown model precision')
        model = cls(world)
        state = torch.load(Path(path) / 'suite_readout.pt', map_location=device, weights_only=True)
        model.suite_readout.load_state_dict(state, strict=True)
        model.identity = identity
        return model.to(device).requires_grad_(False).eval()