interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / layernorm / reference.py

31 lines · 984 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2import numpy as np
3
4U = 2.0 ** -24
5EPS = float(np.float32(1e-5)) # every entrant writes eps as the f32 constant 1e-5
6
7
8def _g(n):
9 return n * U / (1.0 - n * U)
10
11
12def reference(p, x):
13 D = p["D"]
14 v = x["x"].astype(np.float64)
15 g = x["gamma"].astype(np.float64)[None, :]
16 b = x["beta"].astype(np.float64)[None, :]
17 mu = v.mean(axis=1, keepdims=True)
18 c = v - mu
19 var = (c * c).mean(axis=1, keepdims=True)
20 vv = var + EPS
21 s = 1.0 / np.sqrt(vv)
22 y = c * s * g + b
23 # error bound; derivation in workload.json tolerance.why
24 em = _g(D + 1) * np.abs(v).sum(axis=1, keepdims=True) / D
25 dc = em + U * (np.abs(c) + em)
26 rho = (1.0 + em * em / vv) * (1.0 + _g(D + 5)) - 1.0
27 phi = (1.0 + rho) * (1.0 + 4.0 * U) - 1.0
28 ag = np.abs(g)
29 bound = ag * s * (dc * (1.0 + phi) + np.abs(c) * phi)
30 bound = bound + _g(3) * (ag * s * (np.abs(c) + dc) * (1.0 + phi) + np.abs(b))
31 return {"y": (y, bound)}