"""Decimal weight transformations, not model compression or inference."""
from decimal import Decimal,ROUND_HALF_EVEN
import json
if __name__=="__main__":
    weights=list(map(Decimal,[".12","-.04",".78","-.62"]))
    inputs=list(map(Decimal,["1","2","-1",".5"]))
    smallest=set(sorted(range(4),key=lambda i:(abs(weights[i]),i))[:2])
    pruned=[Decimal(0) if i in smallest else w for i,w in enumerate(weights)]
    step=Decimal('.25')
    quantized=[(w/step).to_integral_value(rounding=ROUND_HALF_EVEN)*step for w in weights]
    dot=lambda ws:sum(w*x for w,x in zip(ws,inputs))
    outputs=[dot(ws) for ws in [weights,pruned,quantized]]
    assert outputs==list(map(Decimal,["-1.05","-1.09","-1.00"]))
    assert len(pruned)==4 and sum(w!=0 for w in pruned)==2
    assert quantized==list(map(Decimal,["0","0",".75","-.50"]))
    print(json.dumps({"pruned_indices_zero_based":sorted(smallest),"dense_entries_before_after":[4,4],"nonzero_after_pruning":2,"dot_outputs":[str(x) for x in outputs],"real_packed_quantization":False},indent=2))
