MCPcopy Create free account
hub / github.com/AnswerDotAI/gpu.cpp / gpt2_forward

Function gpt2_forward

experimental/kernels/gpt2_webgpu.cpp:307–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

305
306
307void gpt2_forward(Context& ctx, GPT2 *model, int* inputs, int* targets, size_t B, size_t T) {
308 // targets are optional and could be NULL
309
310 // ensure the model was initialized or error out
311 if (model->params_memory == NULL) {
312 printf("Error: model was not initialized properly.\n");
313 exit(1);
314 }
315
316 // convenience parameters (size_t to help prevent int overflow)
317 size_t V = model->config.vocab_size;
318 size_t Vp = model->config.padded_vocab_size;
319 size_t L = model->config.num_layers;
320 size_t NH = model->config.num_heads;
321 size_t C = model->config.channels;
322
323 // validate inputs, all indices must be in the range [0, V)
324 for(int i = 0; i < B * T; i++) {
325 assert(0 <= inputs[i] && inputs[i] < V);
326 if (targets != NULL) {
327 assert(0 <= targets[i] && targets[i] < V);
328 }
329 }
330
331 // allocate space for all the activations if needed (done here, lazily)
332 if(model->acts_memory == NULL) {
333 // record the current B,T as well
334 model->batch_size = B;
335 model->seq_len = T;
336 // and now allocate the space
337 fill_in_activation_sizes(model->act_sizes, model->config, B, T);
338 // TODO(avh): this is just a resource test for now, eventually deprecate CPU allocations
339 gpu_alloc(ctx, model->acts_.data, model->act_sizes, NUM_PARAMETER_TENSORS);
340 size_t num_activations = 0;
341 for (size_t i = 0; i < NUM_ACTIVATION_TENSORS; i++) {
342 num_activations += model->act_sizes[i];
343 }
344 printf("num_activations: %zu\n", num_activations);
345 model->num_activations = num_activations;
346 printf("Allocating %.2f MB for activations\n", num_activations * sizeof(float) / (1024.0f * 1024.0f));
347 model->acts_memory = malloc_and_point_activations(&model->acts, model->act_sizes);
348 // also create memory for caching inputs and targets
349 model->inputs = (int*)mallocCheck(B * T * sizeof(int));
350 model->targets = (int*)mallocCheck(B * T * sizeof(int)); // might be unused if we never have targets but it's small
351 } else {
352 // validate B,T is consistent with how we've allocated the memory before
353 // in principle we could get more clever here in the future, for now this is safest
354 if (B != model->batch_size || T != model->seq_len) {
355 printf("Model: B=%d T=%d, Desired: B=%d T=%d\n", model->batch_size, model->seq_len, (int)B, (int)T);
356 exit(EXIT_FAILURE);
357 }
358 }
359
360 printf("Cache inputs/targets\n");
361 // cache the inputs/targets
362 memcpy(model->inputs, inputs, B * T * sizeof(int));
363 if (targets != NULL) {
364 memcpy(model->targets, targets, B * T * sizeof(int));

Callers 1

mainFunction · 0.85

Calls 11

fill_in_activation_sizesFunction · 0.85
gpu_allocFunction · 0.85
encoder_forwardFunction · 0.85
layernorm_forwardFunction · 0.85
matmul_forwardFunction · 0.85
attention_forwardFunction · 0.85
residual_forwardFunction · 0.85
gelu_forwardFunction · 0.85
softmax_forwardFunction · 0.85
crossentropy_forwardFunction · 0.85

Tested by

no test coverage detected