89 lines
4.5 KiB
Python
89 lines
4.5 KiB
Python
"""Audit a fixed set of original Greif geometry and immutable input arrays on CPU."""
|
|
|
|
import argparse
|
|
import ast
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import torch
|
|
|
|
import track_b_gino_greif_prepare as preparation
|
|
from track_b_gino_greif_adapter import COLUMNS, GRID, deposit, gather, stencil
|
|
from track_b_gino_shapenet_entry import BASE, checked, read, save, sha
|
|
|
|
|
|
def fixed_cases(pilot, folds):
|
|
selected = {}
|
|
for fold, data in folds.items():
|
|
for role in ('learning', 'test'):
|
|
cases = sorted(data['parts'][role], key=lambda case: (pilot['parts'][case]['nodes'], case))
|
|
for index in (0, len(cases) // 2, len(cases)-1):
|
|
selected.setdefault(cases[index], []).append((fold, role))
|
|
return selected
|
|
|
|
|
|
def geometry(pilot, case, roles):
|
|
row = pilot['parts'][case]
|
|
source = pilot['geometry'][case]
|
|
with np.load(checked(source['path'], source['sha256']), allow_pickle=False) as archive:
|
|
points, ids, raw_mass = (archive[name].copy() for name in ('points','raw_ids','mass'))
|
|
|
|
def inputs(name):
|
|
path = BASE / row['output'] / name
|
|
return np.load(checked(path, row['output_sha256'][name]), allow_pickle=False)
|
|
|
|
if len(ids) != row['nodes'] or not np.array_equal(ids, inputs('raw_ids.npy')) or not np.array_equal(raw_mass, inputs('mass.npy')):
|
|
raise ValueError('Frozen original node/mass identity differs for ' + case)
|
|
x = torch.from_numpy(np.asarray(inputs('features.npy'), dtype=np.float32))
|
|
mass = torch.from_numpy(raw_mass.astype(np.float32))
|
|
corner, weight = stencil(points, ids)
|
|
grid, cell_mass = deposit(x, mass, corner, weight)
|
|
reopened = gather(grid[:len(COLUMNS)], corner, weight)
|
|
if reopened.shape != x.shape or not bool(torch.isfinite(reopened).all()):
|
|
raise ValueError('Grid readout does not cover all original nodes')
|
|
if (weight.sum(1) - 1.).abs().max().item() > 2e-7:
|
|
raise ValueError('Incomplete trilinear node weights')
|
|
ref_mass = mass.double() / mass.double().sum()
|
|
source_integral = ref_mass @ x.double()
|
|
scale = (ref_mass @ x.double().abs()).clamp_min(1.)
|
|
grid_integral = grid[:len(COLUMNS)].reshape(len(COLUMNS),-1).double() @ cell_mass.double()
|
|
relative_integral_error = ((source_integral-grid_integral).abs()/scale).max().item()
|
|
relative_mass_error = abs(cell_mass.double().sum().item()-1.)
|
|
if relative_mass_error > 2e-5 or relative_integral_error > 2e-5:
|
|
raise ValueError('Mass-weighted transfer failed the numerical preflight')
|
|
error = (reopened - x).double()
|
|
column_rms = (ref_mass @ error.square()).sqrt().tolist()
|
|
return dict(case=case, examined_roles=roles, original_nodes=len(ids), occupied_cells=int((cell_mass>0).sum()),
|
|
mass_relative_error=relative_mass_error, field_integral_relative_error=relative_integral_error,
|
|
untrained_round_trip_rms_by_column=column_rms,
|
|
geometry_sha256=source['sha256'], feature_sha256=row['output_sha256']['features.npy'])
|
|
|
|
|
|
def audit():
|
|
if os.environ.get('CUDA_VISIBLE_DEVICES') != '':
|
|
raise PermissionError('Geometry preparation requires CUDA hidden')
|
|
torch.set_num_threads(1)
|
|
root = preparation.REVIEW_ROOT
|
|
candidate = read(root/'preparation-binding.json')
|
|
if candidate != preparation.binding() or (root/'geometry-audit.json').exists():
|
|
raise PermissionError('Only the new frozen preparation candidate')
|
|
source = Path(__file__).with_name('gre24_features_v2.py')
|
|
columns = next(ast.literal_eval(stmt.value) for stmt in ast.parse(source.read_text()).body
|
|
if isinstance(stmt, ast.Assign) and any(isinstance(x, ast.Name) and x.id == 'COLUMNS' for x in stmt.targets))
|
|
if columns != COLUMNS:
|
|
raise ValueError('Original 19-column layout differs')
|
|
pilot = read(checked(preparation.PILOT, preparation.PINS[str(preparation.PILOT)]))
|
|
cases = fixed_cases(pilot, candidate['folds'])
|
|
rows = [geometry(pilot, case, scope) for case, scope in cases.items()]
|
|
save(root/'geometry-audit.json', dict(status='PASS', candidate_sha256=sha(root/'preparation-binding.json'),
|
|
size=GRID, columns=list(COLUMNS), cases=len(rows),
|
|
selection='minimum, median and maximum node counts within each fold learning and test role, deduplicated',
|
|
actual_heldout_labels_read=0, actual_gpu_queries=0, rows=rows))
|
|
print(len(rows), sum(r['original_nodes'] for r in rows))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
argparse.ArgumentParser(description=__doc__).parse_args()
|
|
audit()
|