interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / own30b_decode_profile.c

148 lines · 6277 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Link-only recorder for OWN-30B decode. No production launcher changes.
18 * Link with -Wl,--wrap=omega_gpu_session_launch and this object.
19 * Measures HOST times: elapsed_ns is submit-to-second-marker, NOT GPU cycles.
20 * Only complete sequences of 48 x (qkv, attention, o, router, gu, down) + head
21 * following a head are retained. Prefill, controls and partial steps are rejected.
22 * No allocation or I/O in the measured path. Dump raw integer totals at exit.
23 */
24#include "omega_gpu_session.h"
25#include "omega_blackwell_codegen_moe.h"
26#include "omega_blackwell_codegen_attn.h"
27#include "omega_blackwell_codegen_router.h"
28#include "omega_blackwell_codegen_bf16.h"
29#include <inttypes.h>
30#include <stdio.h>
31#include <string.h>
32
33enum { QKV, ATTN, O, ROUTER, GU, DOWN, HEAD, OTHER, STAGES = OTHER };
34static const char *names[STAGES] = { "qkv", "attention", "o", "router", "gate_up", "down", "lm_head" };
35typedef struct { uint64_t calls, bytes, session_ns, submit_wait_ns, gap_before_ns; } Row;
36typedef struct { Row stage[STAGES]; uint64_t end_ns, start_ns; uint32_t pos; } Token;
37static Token pending, tokens[4096];
38static size_t used, dropped;
39static int armed, invalid;
40static uint64_t last_end;
41
42static int classify(const OmegaGpuLaunch *l, uint64_t *bytes) {
43 *bytes = 0;
44 if (!l || !l->args) return OTHER;
45 if (l->n_args == OMEGA_FP8_ARGS_WORDS) {
46 uint32_t n = l->args[8] / 4u, g = l->grid_y, k;
47 int s;
48 if (g == 1 && n == 5120) { s = QKV; k = 2048; }
49 else if (g == 1 && n == 2048) { s = O; k = 4096; }
50 else if (g == 8 && n == 1536) { s = GU; k = 2048; }
51 else if (g == 8 && n == 2048) { s = DOWN; k = 768; }
52 else return OTHER;
53 *bytes = (uint64_t)g * n * k;
54 return s;
55 }
56 if (l->n_args == OMEGA_DATTN_ARGS_WORDS && l->num_elements == 4096) {
57 *bytes = 2ull * 4 * ((uint64_t)l->args[18] + 1) * 256;
58 return ATTN;
59 }
60 if (l->n_args == OMEGA_ROUTER_ARGS_WORDS && l->num_elements == 2048) {
61 *bytes = 128ull * 2048 * 2;
62 return ROUTER;
63 }
64 if (l->n_args == OMEGA_BF16_ARGS_WORDS && l->num_elements == 151936) {
65 *bytes = 151936ull * 2048 * 2;
66 return HEAD;
67 }
68 return OTHER;
69}
70
71static void record(int s, uint64_t bytes, uint64_t begin, uint64_t end, uint64_t elapsed, int rc) {
72 if (armed) {
73 int expected = pending.pos < 288 ? (int)(pending.pos % 6) : HEAD;
74 if (rc || s != expected || pending.pos > 288) invalid = 1;
75 if (!invalid) {
76 Row *r = &pending.stage[s];
77 r->calls++;
78 r->bytes += bytes;
79 r->session_ns += end - begin;
80 r->submit_wait_ns += elapsed;
81 r->gap_before_ns += begin - last_end;
82 pending.pos++;
83 }
84 }
85 if (s == HEAD) {
86 if (armed && !invalid && pending.pos == 289) {
87 pending.end_ns = end;
88 if (used < sizeof tokens / sizeof tokens[0]) tokens[used++] = pending;
89 else dropped++;
90 }
91 memset(&pending, 0, sizeof pending);
92 pending.start_ns = end;
93 armed = !rc;
94 invalid = 0;
95 }
96 last_end = end;
97}
98
99#ifndef OWN30B_PROFILE_TEST
100int __real_omega_gpu_session_launch(const OmegaGpuLaunch *, uint64_t *, uint32_t *);
101int __wrap_omega_gpu_session_launch(const OmegaGpuLaunch *l, uint64_t *elapsed, uint32_t *marker) {
102 uint64_t bytes, local_elapsed = 0;
103 int s = classify(l, &bytes);
104 uint64_t begin = omega_gpu_session_now_ns();
105 int rc = __real_omega_gpu_session_launch(l, &local_elapsed, marker);
106 uint64_t end = omega_gpu_session_now_ns();
107 if (elapsed) *elapsed = local_elapsed;
108 record(s, bytes, begin, end, local_elapsed, rc);
109 return rc;
110}
111
112__attribute__((destructor)) static void dump(void) {
113 fprintf(stderr, "DECODE_PROFILE semantics=host_session_and_submit_wait weight_bytes_exclude_scales pure_gpu_time=UNKNOWN tokens=%zu dropped=%zu\n", used, dropped);
114 for (size_t t = 0; t < used; ++t) {
115 fprintf(stderr, "DECODE_TOKEN token=%zu wall_ns=%" PRIu64 " launches=%u\n", t, tokens[t].end_ns - tokens[t].start_ns, tokens[t].pos);
116 for (int s = 0; s < STAGES; ++s) {
117 Row *r = &tokens[t].stage[s];
118 fprintf(stderr, "DECODE_STAGE token=%zu stage=%s calls=%" PRIu64 " bytes=%" PRIu64 " session_ns=%" PRIu64 " submit_wait_ns=%" PRIu64 " gap_before_ns=%" PRIu64 "\n",
119 t, names[s], r->calls, r->bytes, r->session_ns, r->submit_wait_ns, r->gap_before_ns);
120 }
121 }
122}
123#else
124#include <assert.h>
125int main(void) {
126 uint32_t args[32] = {0};
127 OmegaGpuLaunch l = { .args = args, .n_args = OMEGA_FP8_ARGS_WORDS, .grid_y = 8 };
128 uint64_t bytes;
129 args[8] = 2048 * 4;
130 assert(classify(&l, &bytes) == DOWN && bytes == 8ull * 2048 * 768);
131 l.grid_y = 7;
132 assert(classify(&l, &bytes) == OTHER);
133 record(HEAD, 0, 0, 1, 1, 0);
134 for (unsigned i = 0; i < 289; i++) record(i < 288 ? i % 6 : HEAD, 10, 2 + i * 3, 4 + i * 3, 1, 0);
135 assert(used == 1 && tokens[0].pos == 289);
136 uint64_t sum = 0;
137 for (int s = 0; s < STAGES; s++) sum += tokens[0].stage[s].session_ns + tokens[0].stage[s].gap_before_ns;
138 assert(sum == tokens[0].end_ns - tokens[0].start_ns);
139 /* Swapped/missing/failed stages must never become a decode timing. */
140 record(O, 0, 1000, 1001, 1, 0);
141 record(HEAD, 0, 1002, 1003, 1, 0);
142 assert(used == 1);
143 record(QKV, 0, 1004, 1005, 1, -1);
144 record(HEAD, 0, 1006, 1007, 1, 0);
145 assert(used == 1);
146 printf("PROFILE_HOST_PASS sequence, accounting, malformed shape, missing stage, failed launch; %s\n", names[HEAD]);
147}
148#endif