openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / quant_int8 / reference.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | import numpy as np |
| 3 | |
| 4 | |
| 5 | def reference(p, x): |
| 6 | r, c = p["R"], p["C"] |
| 7 | v = x["x"].astype(np.float64).reshape(r, c) |
| 8 | amax = np.abs(v).max(axis=1) |
| 9 | s = amax / 127.0 |
| 10 | y = np.zeros_like(v) |
| 11 | nz = amax > 0 |
| 12 | y[nz] = 127.0 * v[nz] / amax[nz, None] |
| 13 | q = np.clip(np.rint(y), -127.0, 127.0) # rint: round half to even |
| 14 | return {"q": q, "s": s} |