MCPcopy Create free account
hub / github.com/Niko1221/Strata / reference

Function reference

engine/src/kernels/gr_parity.cpp:71–135  ·  view source on GitHub ↗

`ref/gr.py::gr_read`, transcribed with switches for the rival readings.

Source from the content-addressed store, hash-verified

69
70/// `ref/gr.py::gr_read`, transcribed with switches for the rival readings.
71void reference(const std::vector<float>& R, const std::vector<float>& w_norm,
72 const std::vector<float>& w_down, const std::vector<float>& w_up,
73 const std::vector<float>& w_inject, float eps, long long n_embd, long long hc, long long hc_lr,
74 const Opts& o, std::vector<float>& mixed, std::vector<float>& inject) {
75 const long long hc_dim = hc * n_embd;
76 std::vector<float> xn((size_t) hc_dim);
77 if (o.per_stream_norm) {
78 for (long long c = 0; c < hc; ++c) {
79 double ms = 0;
80 for (long long d = 0; d < n_embd; ++d) ms += (double) R[(size_t) (c * n_embd + d)] * R[(size_t) (c * n_embd + d)];
81 ms /= (double) n_embd;
82 const float rs = (float) (1.0 / std::sqrt(ms + (double) eps));
83 for (long long d = 0; d < n_embd; ++d) {
84 const size_t i = (size_t) (c * n_embd + d);
85 xn[i] = R[i] * rs * w_norm[i];
86 }
87 }
88 } else {
89 double ms = 0;
90 for (long long i = 0; i < hc_dim; ++i) ms += (double) R[(size_t) i] * R[(size_t) i];
91 ms /= (double) hc_dim;
92 const float rs = (float) (1.0 / std::sqrt(ms + (double) eps));
93 for (long long i = 0; i < hc_dim; ++i) xn[(size_t) i] = R[(size_t) i] * rs * w_norm[(size_t) i];
94 }
95
96 std::vector<float> act((size_t) hc_dim);
97 for (long long i = 0; i < hc_dim; ++i) act[(size_t) i] = o.round_activation ? to_bf16(xn[(size_t) i]) : xn[(size_t) i];
98
99 // lo
100 std::vector<float> lo((size_t) hc_lr);
101 for (long long k = 0; k < hc_lr; ++k) {
102 double a = 0;
103 for (long long i = 0; i < hc_dim; ++i) a += (double) act[(size_t) i] * (double) w_down[(size_t) (k * hc_dim + i)];
104 const float p = (float) a;
105 if (o.sigmoid_on_gate) lo[(size_t) k] = o.scale_inside_silu ? (p / (float) hc) / (1.0f + std::exp(-(p / (float) hc)))
106 : (p / (1.0f + std::exp(-p))) / (float) hc;
107 else lo[(size_t) k] = o.scale_inside_silu ? 1.0f / (1.0f + std::exp(-(p / (float) hc)))
108 : (1.0f / (1.0f + std::exp(-p))) / (float) hc;
109 }
110 std::vector<float> lq((size_t) hc_lr);
111 for (long long k = 0; k < hc_lr; ++k)
112 lq[(size_t) k] = o.round_activation ? to_bf16(lo[(size_t) k]) : lo[(size_t) k];
113
114 // gate and the gated mean
115 mixed.assign((size_t) n_embd, 0.0f);
116 for (long long d = 0; d < n_embd; ++d) {
117 float m = 0.0f;
118 for (long long c = 0; c < hc; ++c) {
119 const long long i = c * n_embd + d;
120 double a = 0;
121 for (long long k = 0; k < hc_lr; ++k) a += (double) lq[(size_t) k] * (double) w_up[(size_t) (i * hc_lr + k)];
122 const float g = (float) a;
123 const float s = o.sigmoid_on_gate ? 1.0f / (1.0f + std::exp(-g)) : g / (1.0f + std::exp(-g));
124 m += xn[(size_t) i] * s;
125 }
126 mixed[(size_t) d] = o.mean_over_streams ? m / (float) hc : m;
127 }
128

Callers 3

mainFunction · 0.70
checkFunction · 0.70
mainFunction · 0.70

Calls 2

assignMethod · 0.80
to_bf16Function · 0.70

Tested by

no test coverage detected