Download code_repair/codecontests/src/semantic_world.py from Gen-Verse/RSI-Anything: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/codecontests/src/semantic_world.py
- Command line
-
hf download hf://datasets/Gen-Verse/RSI-Anything/code_repair/codecontests/src/semantic_world.py
-
curl -L -o semantic_world.py https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/codecontests/src/semantic_world.py
2.73 kB
| 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 | |
| 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) | |
| def score_public(self, contexts): | |
| return self.public_world.score(contexts) | |
| 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() | |