interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_graph_residue.h

87 lines · 3844 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_GRAPH_RESIDUE_H
3#define OMEGA_GPU_GRAPH_RESIDUE_H
4#include "cudapp_native.h"
5#include <stdint.h>
6#include <stdlib.h>
7#include <string.h>
8/* The C oracle is present only in migration builds, OFF in the library. */
9#ifndef CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
10#define CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE 0
11#endif
12static inline uint64_t omega_graph_meta(unsigned mode, const uint64_t c[16], uint64_t d[16]) {
13 uint64_t result = 0;
14 if (omega_grmeta(&result, mode, c, 16, d, 16) != OMEGA_OK) abort();
15 return result;
16}
17static inline int omega_graph_budget(uint32_t peak, uint32_t limit, uint32_t max, uint32_t hw, int declared_ok) {
18#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
19 if (!limit) limit = max - 2u;
20 return limit <= hw && peak <= limit && declared_ok;
21#else
22 const uint64_t c[16] = {peak, limit, max, hw, (uint64_t)declared_ok}; uint64_t d[16];
23 return (int)omega_graph_meta(7, c, d);
24#endif
25}
26static inline int omega_graph_timeline(uint32_t used, uint32_t count, uint32_t cap, int *from) {
27#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
28 const uint32_t skip = used ? 1 : 0;
29 if (used > cap || (count > skip && count - skip > cap - used)) return 0;
30 *from = (int)skip; return 1;
31#else
32 const uint64_t c[16] = {used, count, cap}; uint64_t d[16];
33 if (!omega_graph_meta(6, c, d)) return 0;
34 *from = (int)d[0]; return 1;
35#endif
36}
37static inline int omega_graph_q38(const uint64_t c[16], uint64_t d[16]) {
38#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
39 uint64_t hd=c[0]?c[0]:5120, in=c[1]?c[1]:17408, qh=c[2]?c[2]:24, kv=c[3]?c[3]:4, gh=c[4]?c[4]:48;
40 if (hd%1024 || hd>5120 || !in || in%256 || in>17408 || qh>24 || qh%kv || qh/kv>8 ||
41 (gh!=32 && gh!=48) || qh*256!=gh*128) return 0;
42 d[0]=hd; d[1]=in; d[2]=qh; d[3]=kv; d[4]=gh;
43 d[5]=(32+gh)*128; d[6]=d[5]+gh*128; d[7]=2*gh;
44 d[8]=(qh+kv)*512; d[9]=qh*256+kv*512; d[10]=qh*256;
45 return 1;
46#else
47 return (int)omega_graph_meta(0, c, d);
48#endif
49}
50static inline uint32_t omega_graph_config(uint32_t layers, uint32_t maxlayers,
51 uint32_t vocab, uint32_t span, uint32_t heads, uint32_t maxheads, uint64_t mask, int usemask) {
52#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
53 if (!layers || layers > maxlayers || !vocab || vocab > span * span || !heads || heads > maxheads) return 0;
54 if (usemask && layers < 64 && mask >> layers) return 0;
55 return (vocab + span - 1) / span;
56#else
57 const uint64_t c[16] = {layers, maxlayers, vocab, span, heads, maxheads, mask, (uint64_t)usemask};
58 uint64_t d[16];
59 return omega_graph_meta(1, c, d) ? (uint32_t)d[0] : 0;
60#endif
61}
62static inline void omega_graph_layer(uint64_t mask, uint32_t layer, uint64_t counts[2], int *attn, uint32_t *index) {
63#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
64 *attn = (int)((mask >> layer) & 1u);
65 *index = (uint32_t)counts[*attn ? 0 : 1]++;
66#else
67 const uint64_t c[16] = {mask, layer, counts[0], counts[1]}; uint64_t d[16];
68 if (!omega_graph_meta(2, c, d)) abort();
69 *attn = (int)d[0]; *index = (uint32_t)d[1]; counts[0] = d[2]; counts[1] = d[3];
70#endif
71}
72/* Runtime-extent uint view writes directly into the real allocation. */
73static inline void omega_graph_table(unsigned mode, unsigned qh, unsigned kv,
74 uint32_t *dst, uint32_t n) {
75 uint64_t result = 0;
76 if (omega_grtable(&result, mode, qh, kv, dst, n) != OMEGA_OK || result != 1) abort();
77}
78static inline int omega_graph_shape(uint32_t ae, uint32_t an, uint32_t ak,
79 uint32_t e, uint32_t n, uint32_t k) {
80#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
81 return ae == e && an == n && ak == k;
82#else
83 const uint64_t c[16] = {ae, an, ak, e, n, k}; uint64_t d[16];
84 return (int)omega_graph_meta(5, c, d);
85#endif
86}
87#endif