190 lines
8.2 KiB
Python
190 lines
8.2 KiB
Python
"""H2-P worker using the original ShapeNet case-level training and scoring protocol."""
|
|
|
|
import argparse
|
|
import ctypes
|
|
from hashlib import sha256
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import signal
|
|
import sys
|
|
from tempfile import TemporaryDirectory
|
|
import time
|
|
from types import FunctionType, SimpleNamespace
|
|
from zipfile import ZipFile
|
|
|
|
import numpy as np
|
|
import torch
|
|
|
|
import track_b_gino_formal_worker as gino
|
|
import track_b_h2_pressure_entry as entry
|
|
from track_b_h2_pressure_prepare import PressureH2, surface
|
|
from track_b_gino_paper_keyword_patch import extract_patched_paper_code
|
|
from track_b_gino_shapenet_entry import checked, process_identity, read, save, sha
|
|
from track_b_gino_shapenet_worker import cuda_peaks, test_environment, train_seed
|
|
|
|
|
|
def mesh_graph(c, folder, archive, row, paper):
|
|
case = row['case']
|
|
member = archive.read(f'car-pressure-data/data/mesh_{case:03d}.ply')
|
|
if sha256(member).hexdigest() != row['mesh_sha256']:
|
|
raise ValueError(f'Original mesh hash differs for case {case}')
|
|
path = folder / f'mesh_{case:03d}.ply'
|
|
path.write_bytes(member)
|
|
try:
|
|
mesh = paper.load_mesh(path)
|
|
xyz = mesh.vertex.positions.numpy()
|
|
faces = mesh.triangle.indices.numpy()
|
|
finally:
|
|
path.unlink()
|
|
if len(xyz) != row['mesh_vertices'] or len(xyz) != 3586:
|
|
raise ValueError(f'Original vertex support differs for case {case}')
|
|
lower, upper = c['split']['raw_global_bounds']
|
|
margin = c['protocol']['bounds_expansion']
|
|
lo, hi = [v - margin for v in lower], [v + margin for v in upper]
|
|
graph = list(surface(xyz, faces, lo, hi))
|
|
graph[0] = paper.location_normalization(
|
|
torch.as_tensor(xyz, dtype=torch.float32),
|
|
torch.tensor(lo, dtype=torch.float32), torch.tensor(hi, dtype=torch.float32),
|
|
)
|
|
return tuple(graph), len(faces)
|
|
|
|
|
|
class SurfaceData:
|
|
def __init__(self, c, folder):
|
|
from neuralop.utils import UnitGaussianNormalizer
|
|
from src.data.cfd_datamodule import CFDSDFDataModule
|
|
|
|
self.c = c
|
|
self.folder = folder
|
|
self.paper = object.__new__(CFDSDFDataModule)
|
|
self.normalizer_class = UnitGaussianNormalizer
|
|
self.normalizer = None
|
|
self.train_ids = c['split']['train']
|
|
self.test_ids = c['split']['test']
|
|
self.rows = {r['case']: r for r in read(c['mapping_audit']['path'])['rows']}
|
|
|
|
def data(self, ids, role, target_log=None):
|
|
if role == 'train':
|
|
allowed = ids == self.train_ids and self.normalizer is None and target_log is None
|
|
else:
|
|
allowed = role == 'heldout' and ids == self.test_ids and self.normalizer is not None and target_log is not None
|
|
if not allowed:
|
|
raise PermissionError('Unexpected ShapeNet input or target scope')
|
|
graphs, pressures = [], []
|
|
folder = self.folder / role
|
|
folder.mkdir(exist_ok=False)
|
|
with ZipFile(self.c['paper_data']['path']) as archive:
|
|
for case in ids:
|
|
row = self.rows[case]
|
|
graph, _ = mesh_graph(self.c, folder, archive, row, self.paper)
|
|
graphs.append(graph)
|
|
if role == 'heldout':
|
|
target_log.write(json.dumps(dict(case=case, role=role, sha256=row['pressure_sha256'])) + '\n')
|
|
target_log.flush()
|
|
blob = archive.read(f'car-pressure-data/data/press_{case:03d}.npy')
|
|
if sha256(blob).hexdigest() != row['pressure_sha256']:
|
|
raise ValueError(f'Original pressure hash differs for case {case}')
|
|
raw = np.load(io.BytesIO(blob), allow_pickle=False).reshape(-1).astype(np.float32)
|
|
retained = np.concatenate((raw[:16], raw[112:]))
|
|
if (raw.size != 3682 or retained.size != 3586 or not np.isfinite(retained).all()
|
|
or sha256(retained.tobytes()).hexdigest() != row['retained_pressure_f32_sha256']):
|
|
raise ValueError(f'Original pressure mapping differs for case {case}')
|
|
pressures.append(torch.from_numpy(retained))
|
|
target = torch.stack(pressures)
|
|
if role == 'train':
|
|
self.normalizer = self.normalizer_class(
|
|
target, eps=self.c['protocol']['pressure_epsilon'], reduce_dim=[0, 1], verbose=False)
|
|
target = self.normalizer.encode(target)
|
|
return [dict(graph=graph, pressure=values) for graph, values in zip(graphs, target)]
|
|
|
|
|
|
class PressureModel(PressureH2):
|
|
def data_dict_to_input(self, item):
|
|
device = self.encoder[0].weight.device
|
|
return tuple(value[0].to(device) for value in item['graph']), None
|
|
|
|
def forward(self, graph, _unused=None):
|
|
return super().forward(*graph)
|
|
|
|
def loss_dict(self, item, loss_fn):
|
|
graph, _ = self.data_dict_to_input(item)
|
|
prediction = self(graph).reshape(1, -1)
|
|
truth = item['pressure'].to(prediction.device).reshape(1, -1)
|
|
return dict(loss=loss_fn(prediction, truth))
|
|
|
|
|
|
def paper_model(_c):
|
|
return PressureModel()
|
|
|
|
|
|
def attach(c, root, seed, phase):
|
|
ticket = read(root / 'dispatch.json')
|
|
owner = os.getppid()
|
|
if (ticket['coordinator'] != process_identity(owner)
|
|
or ticket['contract_sha256'] != sha(entry.ROOT / 'contract.json')
|
|
or (ticket['seed'], ticket['phase']) != (seed, phase)
|
|
or time.monotonic() >= ticket['start_by_monotonic']
|
|
or str(Path(sys.executable).absolute()) != c['environment']['python']):
|
|
raise PermissionError('Worker is not the exact admitted child')
|
|
if ctypes.CDLL(None, use_errno=True).prctl(1, signal.SIGKILL, 0, 0, 0) or os.getppid() != owner:
|
|
raise RuntimeError('Could not bind worker lifetime to the supervisor')
|
|
authorization = entry.authorize(c)
|
|
if (entry.DEVICES.get(ticket['ordinal']) != ticket['physical_uuid']
|
|
or ticket['ordinal'] not in authorization['eligible_ordinals']):
|
|
raise PermissionError('Unapproved physical V100')
|
|
return ticket
|
|
|
|
|
|
def fit(c, seed, data, root, records, ticket):
|
|
def progress(path, row):
|
|
save(path, row)
|
|
if seed == 0 and row['epoch'] == 1:
|
|
result = entry.first_epoch_projection(ticket, row, time.monotonic())
|
|
save(root / 'first-epoch-projection.json', result)
|
|
if not result['within_cap']:
|
|
raise RuntimeError('STOP_PROJECTED_CAP')
|
|
|
|
trainer = FunctionType(train_seed.__code__, dict(train_seed.__globals__, paper_model=paper_model,
|
|
save=progress), train_seed.__name__)
|
|
fit_from_gino = FunctionType(gino.train.__code__, dict(gino.train.__globals__, train_seed=trainer),
|
|
gino.train.__name__)
|
|
return fit_from_gino(c, seed, data, root, records, torch)
|
|
|
|
|
|
def score(c, seed, data, root):
|
|
scorer = FunctionType(gino.score.__code__,
|
|
dict(gino.score.__globals__, formal=entry, paper_model=paper_model), gino.score.__name__)
|
|
return scorer(c, seed, data, root, torch)
|
|
|
|
|
|
def run(seed, phase):
|
|
c = entry.contract()
|
|
root = entry.run_root(seed, phase)
|
|
ticket = attach(c, root, seed, phase)
|
|
test_environment(c)
|
|
torch.set_num_threads(c['limits']['torch_threads'])
|
|
torch.set_num_interop_threads(1)
|
|
binder = FunctionType(gino.bind_device.__code__, dict(gino.bind_device.__globals__,
|
|
formal=SimpleNamespace(DEVICES={str(seed): ticket['physical_uuid']})), gino.bind_device.__name__)
|
|
binder(root, c, ticket, torch)
|
|
torch.cuda.reset_peak_memory_stats()
|
|
torch.backends.cuda.matmul.allow_tf32 = False
|
|
torch.backends.cudnn.allow_tf32 = False
|
|
records = root / 'worker-records'
|
|
records.mkdir(exist_ok=False)
|
|
with TemporaryDirectory(prefix='h2-pressure-', dir=root) as work:
|
|
extract_patched_paper_code(c, work)
|
|
data = SurfaceData(c, Path(work))
|
|
result = fit(c, seed, data, root, records, ticket) if phase == 'fit' else score(c, seed, data, root)
|
|
save(records / 'result.json', dict(result, phase=phase, seed=seed, status='COMPLETE', **cuda_peaks(torch)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('seed', type=int, choices=(0, 1, 2))
|
|
parser.add_argument('phase', choices=('fit', 'score'))
|
|
args = parser.parse_args()
|
|
run(args.seed, args.phase)
|