Download arena-methods/predict.py from lisuo/PFN-DiD: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/lisuo/PFN-DiD/resolve/main/arena-methods/predict.py
- Command line
-
hf download hf://lisuo/PFN-DiD/arena-methods/predict.py
-
curl -L -o predict.py https://huggingface.co/lisuo/PFN-DiD/resolve/main/arena-methods/predict.py
4.53 kB
| """Observed-data-only PFN-DiD entry point for StatsArena integrations.""" | |
| import argparse | |
| from hashlib import sha256 | |
| import json | |
| from pathlib import Path | |
| from time import perf_counter | |
| import numpy as np | |
| import torch | |
| from safetensors.torch import load_file | |
| from runtime.model import PFNDiD | |
| from runtime.score import validate_table, drdid_panel | |
| ROOT = Path(__file__).resolve().parent | |
| def file_hash(path): | |
| digest = sha256() | |
| with Path(path).open('rb') as handle: | |
| for block in iter(lambda: handle.read(8 * 1024 * 1024), b''): | |
| digest.update(block) | |
| return digest.hexdigest() | |
| def read_observed(path): | |
| with np.load(path, allow_pickle=False) as data: | |
| if set(data.files) != {'X', 'D', 'delta_y'}: | |
| raise ValueError('Expected exactly X, D, delta_y; no truth or metadata arrays') | |
| return {key: np.array(data[key], copy=True) for key in data.files} | |
| class Predictor: | |
| """Initialize once, then reuse serially across independent observed tables. | |
| Inputs: numeric X (n,p), binary D (n,), delta_y (n,). X may have zero | |
| columns. This object is not thread-safe; use one instance per worker. | |
| """ | |
| def __init__(self, backbone, *, device='cpu', threads=4): | |
| self.config = json.loads((ROOT/'config.json').read_text()) | |
| self.device = torch.device(device) | |
| if self.device.type == 'musa': | |
| import torch_musa # noqa: F401; registers the device backend | |
| if threads < 1: | |
| raise ValueError('threads must be positive') | |
| torch.set_num_threads(threads) | |
| checkpoint = ROOT/'checkpoints/adapter.safetensors' | |
| for path, expected in [(backbone,self.config['backbone']['sha256']), | |
| (checkpoint,self.config['checkpoint_sha256'])]: | |
| if file_hash(path) != expected: | |
| raise ValueError(f'Weight checksum mismatch: {Path(path).name}') | |
| self.model = PFNDiD(weight_path=backbone, hidden_dim=8, dropout=.05) | |
| self.model.adapters.load_state_dict(load_file(str(checkpoint)), strict=True) | |
| # Preserve the contiguous projection used for the recorded evaluation. | |
| self.model.backbone.heads.output_projection.register_forward_pre_hook( | |
| lambda module, args: (args[0].contiguous(), *args[1:])) | |
| self.model.to(self.device).eval() | |
| def _synchronize(self): | |
| if self.device.type == 'cuda': | |
| torch.cuda.synchronize(self.device) | |
| elif self.device.type == 'musa': | |
| torch.musa.synchronize(self.device) | |
| def __call__(self, observed, *, fold_seed=39007, confidence=.95): | |
| if set(observed) != {'X', 'D', 'delta_y'}: | |
| raise ValueError('Expected exactly X, D, delta_y') | |
| if not 0 < confidence < 1: | |
| raise ValueError('confidence must lie between zero and one') | |
| self._synchronize() | |
| started = perf_counter() | |
| x,d,y = validate_table(observed['X'],observed['D'],observed['delta_y']) | |
| with torch.inference_mode(): | |
| output = self.model(x,d,y,n_splits=5,fold_seed=fold_seed) | |
| result = drdid_panel(y,d,output['propensity'].cpu().numpy(), | |
| output['outcome'].cpu().numpy(),.05,confidence) | |
| self._synchronize() | |
| return dict(estimate=result['att'], standard_error=result['se'], | |
| intervals={f'{confidence:g}':[result['lower'],result['upper']]}, | |
| estimand='ATT', confidence=confidence, | |
| clipped_fraction=result['clipped_fraction'], | |
| control_ess=result['control_ess'], | |
| inference_seconds=perf_counter()-started) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--observed',type=Path,required=True) | |
| parser.add_argument('--backbone',type=Path,required=True) | |
| parser.add_argument('--device',default='cpu') | |
| parser.add_argument('--threads',type=int,default=4) | |
| parser.add_argument('--fold-seed',type=int,default=39007) | |
| parser.add_argument('--confidence',type=float,default=.95) | |
| parser.add_argument('--output',type=Path) | |
| args = parser.parse_args() | |
| predictor=Predictor(args.backbone,device=args.device,threads=args.threads) | |
| result=predictor(read_observed(args.observed),fold_seed=args.fold_seed,confidence=args.confidence) | |
| text=json.dumps(result,indent=2,allow_nan=False)+'\n' | |
| if args.output: | |
| args.output.write_text(text) | |
| else: | |
| print(text,end='') | |
| if __name__ == '__main__': | |
| main() | |