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

Function gdn_case

engine/src/kernels/batch_rows_parity.cpp:186–254  ·  view source on GitHub ↗

---- 3. the GDN recurrence ----

Source from the content-addressed store, hash-verified

184
185// ---- 3. the GDN recurrence ----
186void gdn_case(const std::vector<int>& ntok, bool commit) {
187 const int S = 128, h_k = 16, h_v = 48; // the artifact's geometry
188 const int qk = S * h_k, C = 2 * qk + S * h_v, vd = S * h_v;
189 const float eps = 1e-6f;
190 int T = 0;
191 for (int n : ntok) T += n;
192 const int G = (int) ntok.size();
193 std::mt19937 rng(31 + T * 7 + G + commit);
194 std::normal_distribution<float> nd(0.f, 1.f);
195 std::vector<float> h((size_t) T * C), gate((size_t) T * h_v), beta((size_t) T * h_v), z((size_t) T * vd), gamma(S);
196 for (auto& v : h) v = nd(rng) * 0.1f;
197 for (auto& v : gate) v = -(0.5f + 3.0f * (float) (rng() % 100) / 100.0f);
198 for (auto& v : beta) v = (float) (rng() % 100) / 100.0f;
199 for (auto& v : z) v = nd(rng);
200 for (auto& v : gamma) v = 1.0f + 0.1f * nd(rng);
201 const size_t st_n = (size_t) S * h_v * S;
202 std::vector<std::vector<float>> st0(G);
203 std::vector<float*> st_g(G), st_r(G);
204 std::vector<int32_t*> keep(G);
205 k::GdnGroups gg;
206 gg.n = G;
207 int first = 0;
208 for (int i = 0; i < G; ++i) {
209 st0[i].resize(st_n);
210 for (auto& v : st0[i]) v = nd(rng) * 0.1f;
211 st_g[i] = up(st0[i]);
212 st_r[i] = up(st0[i]);
213 const int32_t kp = commit ? ntok[i] : 0; // a commit group keeps all of its rows here
214 keep[i] = up(std::vector<int32_t>{kp});
215 gg.state[i] = st_g[i];
216 gg.keep[i] = commit ? keep[i] : nullptr;
217 gg.first[i] = first;
218 gg.ntok[i] = ntok[i];
219 first += ntok[i];
220 }
221 const float* d_h = up(h);
222 const float* d_gate = up(gate);
223 const float* d_beta = up(beta);
224 const float* d_z = up(z);
225 const float* d_gm = up(gamma);
226 float *d_y = nullptr, *d_yr = nullptr;
227 ck(cudaMalloc(&d_y, (size_t) T * vd * 4), "malloc");
228 ck(cudaMalloc(&d_yr, (size_t) T * vd * 4), "malloc");
229 ck(cudaMemset(d_y, 0, (size_t) T * vd * 4), "memset");
230 ck(cudaMemset(d_yr, 0, (size_t) T * vd * 4), "memset");
231 k::gdn_step_norm_groups(gg, d_h, C, d_gate, d_beta, d_z, d_gm, eps, d_y, h_k, h_v, commit, nullptr);
232 for (int i = 0; i < G; ++i) {
233 const int f = gg.first[i];
234 // the engine's per-slot call: a window group runs its rows with all outputs; a commit group (1 row) keeps them
235 k::gdn_step_norm_multi(st_r[i], d_h + (size_t) f * C, C, d_gate + (size_t) f * h_v, d_beta + (size_t) f * h_v,
236 d_z + (size_t) f * vd, d_gm, eps, d_yr + (size_t) f * vd, h_k, h_v, ntok[i],
237 commit ? keep[i] : nullptr, nullptr, 0);
238 }
239 ck(cudaDeviceSynchronize(), "sync");
240 bool same = true;
241 for (int i = 0; i < G; ++i) {
242 const std::vector<float> a = down(st_g[i], st_n), b = down(st_r[i], st_n);
243 same = same && std::memcmp(a.data(), b.data(), st_n * 4) == 0;

Callers 1

mainFunction · 0.85

Calls 12

upFunction · 0.70
ckFunction · 0.70
downFunction · 0.70
reportFunction · 0.70
cudaMallocFunction · 0.50
cudaMemsetFunction · 0.50
gdn_step_norm_groupsFunction · 0.50
gdn_step_norm_multiFunction · 0.50
cudaDeviceSynchronizeFunction · 0.50
sizeMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected