Buckets:
| """Two dedicated H100 evaluators; each candidate retains the original judge.""" | |
| from __future__ import annotations | |
| import argparse | |
| from concurrent.futures import Future | |
| from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | |
| import json | |
| import os | |
| from pathlib import Path | |
| import socket | |
| import threading | |
| import time | |
| import remote_judge as base | |
| from fixed_judge import FixedJudge, Client | |
| from common import IMAGE, IMAGE_MANIFEST, atomic, read, sha | |
| class Pool: | |
| def __init__(self, root, gpus=(6, 7)): | |
| self.root = Path(root) | |
| self.gpus = tuple(gpus) | |
| self.available = set(gpus) | |
| self.condition = threading.Condition() | |
| self.jobs = {} | |
| self.judges = {} | |
| self.root.joinpath('routes').mkdir(parents=True, exist_ok=True) | |
| for gpu in gpus: | |
| sub = self.root / ('gpu' + str(gpu)) | |
| atomic(sub / 'control/image_ready.json', read(self.root / 'control/image_ready.json')) | |
| self.judges[gpu] = FixedJudge(sub, gpu) | |
| def health(self): | |
| return dict(image=IMAGE, canonical_image_config_sha256=IMAGE, | |
| image_manifest_sha256=IMAGE_MANIFEST, execution_seconds=530, | |
| gpus=list(self.gpus), states=[base.gpu_state(g) for g in self.gpus], | |
| node=socket.gethostname(), available=sorted(self.available)) | |
| def evaluate(self, code, request_id, timeout, gpu=None): | |
| if timeout != 530 or not isinstance(code, str) or len(code.encode()) > 2_000_000: | |
| raise base.InfrastructureError('Original budget/code limit required') | |
| if gpu is not None and gpu not in self.gpus: | |
| raise base.InfrastructureError('GPU outside the authorized pair') | |
| identity = sha(IMAGE + '\n' + code + '\n530') | |
| with self.condition: | |
| if request_id in self.jobs: | |
| previous, future = self.jobs[request_id] | |
| if previous != identity: | |
| raise base.InfrastructureError('Request ID reused for another program') | |
| owner = False | |
| else: | |
| future = Future() | |
| self.jobs[request_id] = (identity, future) | |
| owner = True | |
| if not owner: | |
| return future.result() | |
| chosen = None | |
| queued = time.monotonic() | |
| try: | |
| route = self.root / 'routes' / (request_id + '.json') | |
| if route.exists(): | |
| prior = read(route) | |
| if prior['identity'] != identity: | |
| raise base.InfrastructureError('Saved request identity mismatch') | |
| gpu = prior['gpu'] | |
| with self.condition: | |
| self.condition.wait_for(lambda: gpu in self.available if gpu is not None else bool(self.available)) | |
| chosen = gpu if gpu is not None else min(self.available) | |
| self.available.remove(chosen) | |
| if not route.exists(): | |
| atomic(route, dict(identity=identity, gpu=chosen, program_sha256=sha(code), time=time.time())) | |
| value = self.judges[chosen].evaluate(code, request_id, timeout) | |
| value = dict(value, pool_queue_seconds=time.monotonic() - queued - value.get('host_execution_seconds', 0), | |
| evaluator_gpus=list(self.gpus)) | |
| future.set_result(value) | |
| return value | |
| except BaseException as error: | |
| future.set_exception(error) | |
| raise | |
| finally: | |
| if chosen is not None: | |
| with self.condition: | |
| self.available.add(chosen) | |
| self.condition.notify_all() | |
| def serve(root, host, port, allowed_client): | |
| token = (root / 'control/judge_token.txt').read_text().strip() | |
| pool = Pool(root) | |
| class Handler(BaseHTTPRequestHandler): | |
| def log_message(self, *args): | |
| pass | |
| def reply(self, value, status=200): | |
| body = json.dumps(value, allow_nan=False).encode() | |
| self.send_response(status) | |
| self.send_header('Content-Type', 'application/json') | |
| self.send_header('Content-Length', str(len(body))) | |
| self.end_headers() | |
| self.wfile.write(body) | |
| def authorized(self): | |
| if self.client_address[0] != allowed_client or self.headers.get('Authorization') != 'Bearer ' + token: | |
| self.reply({'error': 'Forbidden'}, 403) | |
| return False | |
| return True | |
| def do_GET(self): | |
| if self.authorized(): | |
| self.reply(pool.health() if self.path == '/health' else {'error': 'Not found'}) | |
| def do_POST(self): | |
| if not self.authorized(): | |
| return | |
| size = int(self.headers.get('Content-Length', '0')) | |
| if self.path != '/evaluate' or not 0 < size < 3_000_000: | |
| return self.reply({'error': 'Invalid request'}, 400) | |
| try: | |
| item = json.loads(self.rfile.read(size)) | |
| rid = item['request_id'] | |
| if len(rid) != 32 or any(c not in '0123456789abcdef' for c in rid): | |
| raise ValueError('Invalid request ID') | |
| result = pool.evaluate(item['code'], rid, item['timeout'], item.get('gpu')) | |
| except Exception as error: | |
| result = {'infrastructure_error': type(error).__name__ + ': ' + str(error)} | |
| self.reply(result) | |
| atomic(root / 'control/server.json', dict(pid=os.getpid(), host=host, port=port, | |
| gpus=[6, 7], image=IMAGE, started=time.time(), allowed_client=allowed_client)) | |
| ThreadingHTTPServer((host, port), Handler).serve_forever() | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--root', type=Path, required=True) | |
| parser.add_argument('--host', required=True) | |
| parser.add_argument('--port', type=int, required=True) | |
| parser.add_argument('--allowed-client', required=True) | |
| args = parser.parse_args() | |
| serve(args.root, args.host, args.port, args.allowed_client) | |
Xet Storage Details
- Size:
- 6.06 kB
- Xet hash:
- 09413b6945af61a7dab4232906ee3d22e507b18a87cbd280064ad8645620405a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.