from pathlib import Path import hashlib,json,sys,random,math import numpy as np import torch from torch import nn from torch.nn import functional as F from safetensors.torch import load_file,save_file ROOT=Path(__file__).resolve().parent;DATA=ROOT/"data";W=ROOT/"work";E=ROOT/"evidence";MODELS=ROOT/"models" SM="bge-small-en-v1.5";LG="bge-base-en-v1.5";VI="dinov2-small" def write(p,data):p.write_text(json.dumps(data,ensure_ascii=False,indent=2,allow_nan=False),encoding="utf8") def read(p):return json.loads(p.read_text(encoding="utf8")) def sha(p): with p.open("rb") as f:return hashlib.file_digest(f,"sha256").hexdigest() def device(): torch.set_num_threads(3);torch.backends.cuda.matmul.allow_tf32=False;torch.backends.cudnn.allow_tf32=False return torch.device("cuda" if torch.cuda.is_available() else "cpu") def stsave(p,sd):save_file({n:t.detach().cpu().clone().contiguous() for n,t in sd.items()},str(p)) def seed(n):torch.manual_seed(n);np.random.seed(n);random.seed(n)