VickyRDP commited on
Commit
2058ffa
·
verified ·
1 Parent(s): 7434dde

Add test-debug.html harness (temporary, for on-device dtype diagnosis)

Browse files
Files changed (1) hide show
  1. test-debug.html +204 -0
test-debug.html ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html><head><meta charset="UTF-8"><title>GM shim e2e test</title></head>
3
+ <body>
4
+ <pre id="log"></pre>
5
+ <script type="module">
6
+ window.__EVENTS = [];
7
+ window.__RESULT = null;
8
+ window.__DONE = false;
9
+ const L = (...a) => { const s = a.map(x => typeof x === 'string' ? x : JSON.stringify(x)).join(' ');
10
+ window.__EVENTS.push(s); console.log('[T]', s);
11
+ document.getElementById('log').textContent += s + '\n'; };
12
+
13
+ const params = new URLSearchParams(location.search);
14
+ const DEVICE = params.get('device') || 'wasm';
15
+ const SHIM = params.get('shim') !== '0';
16
+ const NTOK = parseInt(params.get('ntok') || '40');
17
+
18
+ try {
19
+ const { env, pipeline, TextStreamer } = await import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1');
20
+ L('tjs version', env.version, 'device', DEVICE, 'shim', SHIM, 'Float16Array', typeof Float16Array !== 'undefined');
21
+
22
+ const MODEL_ID = params.get('model') || 'VickyRDP/gpumart-qwen2.5-0.5b-instruct';
23
+ const SYSTEM = "You are the GPU Mart assistant for RDP GPU Mart (rdp.in/gpu-mart), India's datacenter-grade GPU marketplace by RDP Computers India Private Limited. Answer questions about GPU Mart products, series, pricing, policies and support accurately and concisely.";
24
+
25
+ const t0 = performance.now();
26
+ const generator = await pipeline('text-generation', MODEL_ID, {
27
+ device: DEVICE, dtype: 'fp16',
28
+ session_options: { graphOptimizationLevel: (params.get('opt') || 'disabled') },
29
+ progress_callback: p => { if (p.status === 'done') L('loaded:', p.file); }
30
+ });
31
+ L('pipeline ready in', Math.round(performance.now() - t0) + 'ms');
32
+
33
+ L('model.sessions keys:', Object.keys(generator.model.sessions || {}), 'hasModel:', !!generator.model);
34
+ // ---- PROBE ----
35
+ let runCount = 0;
36
+ for (const [name, sess] of Object.entries(generator.model.sessions || {})) {
37
+ const orig0 = sess.run.bind(sess);
38
+ sess.run = async (feeds, opts) => {
39
+ runCount++;
40
+ if (runCount <= 2) {
41
+ const ft = {};
42
+ for (const k in feeds) {
43
+ const t = feeds[k];
44
+ ft[k.replace(/past_key_values\.(\d+)\..*/, 'pkv.$1')] =
45
+ t.type + '/' + (t.data && t.data.constructor ? t.data.constructor.name : '?') + '/' + JSON.stringify(t.dims);
46
+ }
47
+ L('PROBE run#' + runCount, 'session=' + name, ft);
48
+ }
49
+ const out = await orig0(feeds, opts);
50
+ if (runCount <= 2) {
51
+ const ot = {};
52
+ for (const k in out) {
53
+ const t = out[k]; if (!t || !t.type) continue;
54
+ ot[k.replace(/present\.(\d+)\..*/, 'pres.$1')] =
55
+ t.type + '/' + (t.data && t.data.constructor ? t.data.constructor.name : '?');
56
+ }
57
+ L('PROBE out#' + runCount, ot);
58
+ }
59
+ return out;
60
+ };
61
+ }
62
+
63
+ if (params.get('shim') === '3') {
64
+ // ---- KV dtype adapter v3: learn expected dtypes by try/flip/retry ----
65
+ try {
66
+ const HasF16 = typeof Float16Array !== 'undefined';
67
+ let flips = 0;
68
+ for (const sess of Object.values(generator.model.sessions || {})) {
69
+ const orig = sess.run.bind(sess);
70
+ let want = null;
71
+ sess.run = async (feeds, opts) => {
72
+ let Tctor = null;
73
+ for (const k in feeds) if (feeds[k] && feeds[k].constructor) { Tctor = feeds[k].constructor; break; }
74
+ const conv = (t, ty) => {
75
+ if (ty === 'float16') { if (!HasF16) return t; return new Tctor('float16', Float16Array.from(t.data), t.dims); }
76
+ return new Tctor('float32', Float32Array.from(t.data), t.dims);
77
+ };
78
+ const isF = t => t && (t.type === 'float32' || t.type === 'float16');
79
+ if (want) {
80
+ for (const k in feeds) { const t = feeds[k], w = want[k];
81
+ if (isF(t) && w && t.type !== w) { feeds[k] = conv(t, w); flips++; } }
82
+ return orig(feeds, opts);
83
+ }
84
+ try {
85
+ const out = await orig(feeds, opts);
86
+ want = {}; for (const k in feeds) if (feeds[k] && feeds[k].type) want[k] = feeds[k].type;
87
+ return out;
88
+ } catch (e) {
89
+ if (!/input data type/i.test(String(e && e.message || e)) || !Tctor) throw e;
90
+ L('[GM] dtype mismatch — flipping float feeds and retrying');
91
+ for (const k in feeds) { const t = feeds[k];
92
+ if (isF(t)) { feeds[k] = conv(t, t.type === 'float32' ? 'float16' : 'float32'); flips++; } }
93
+ const out = await orig(feeds, opts);
94
+ want = {}; for (const k in feeds) if (feeds[k] && feeds[k].type) want[k] = feeds[k].type;
95
+ return out;
96
+ }
97
+ };
98
+ }
99
+ window.__CASTS = () => ({ flips });
100
+ L('[GM] kv dtype adapter v3 installed');
101
+ } catch (e) { L('[GM] adapter v3 install failed: ' + e.message); }
102
+ }
103
+
104
+ if (params.get('shim') === '2') {
105
+ // ---- KV dtype adapter (v2): align feed dtypes with session input metadata ----
106
+ try {
107
+ const HasF16 = typeof Float16Array !== 'undefined';
108
+ let conv = 0;
109
+ for (const sess of Object.values(generator.model.sessions || {})) {
110
+ const md = sess.inputMetadata;
111
+ const want = {};
112
+ const entries = Array.isArray(md) ? md : Object.entries(md || {}).map(([n, v]) => ({ name: n, ...v }));
113
+ for (const e of entries) if (e && e.name && e.type) want[e.name] = e.type;
114
+ L('adapter wants:', Object.entries(want).filter(([k]) => k.startsWith('past_key_values')).slice(0,1).map(([k,v]) => k + ':' + v));
115
+ const orig = sess.run.bind(sess);
116
+ sess.run = async (feeds, opts) => {
117
+ let Tctor = null;
118
+ for (const k in feeds) if (feeds[k] && feeds[k].constructor) { Tctor = feeds[k].constructor; break; }
119
+ for (const k in feeds) {
120
+ const t = feeds[k], w = want[k];
121
+ if (!t || !w || !Tctor || !t.type || t.type === w) continue;
122
+ if (t.type === 'float32' && w === 'float16' && HasF16) {
123
+ feeds[k] = new Tctor('float16', Float16Array.from(t.data), t.dims); conv++;
124
+ } else if (t.type === 'float16' && w === 'float32') {
125
+ feeds[k] = new Tctor('float32', Float32Array.from(t.data), t.dims); conv++;
126
+ }
127
+ }
128
+ return orig(feeds, opts);
129
+ };
130
+ }
131
+ window.__CASTS = () => ({ conv });
132
+ L('[GM] kv dtype adapter installed');
133
+ } catch (e) { L('[GM] adapter install failed: ' + e.message); }
134
+ }
135
+
136
+ if (SHIM && params.get('shim') !== '2' && params.get('shim') !== '3') {
137
+ // ---- fp16 I/O shim: VERBATIM copy of the code committed to the Space ----
138
+ try {
139
+ const _b = new ArrayBuffer(4), _f = new Float32Array(_b), _u = new Uint32Array(_b);
140
+ const f32h = v => { _f[0] = v; const x = _u[0]; const s = (x >>> 16) & 0x8000; let e = (x >>> 23) & 0xff, m = x & 0x7fffff;
141
+ if (e === 0xff) return s | 0x7c00 | (m ? 0x200 : 0); let E = e - 112;
142
+ if (E >= 0x1f) return s | 0x7c00;
143
+ if (E <= 0) { if (E < -10) return s; m = (m | 0x800000) >>> (1 - E); if (m & 0x1000) m += 0x2000; return s | (m >>> 13); }
144
+ if (m & 0x1000) { m += 0x2000; if (m & 0x800000) { m = 0; E += 1; if (E >= 0x1f) return s | 0x7c00; } }
145
+ return s | (E << 10) | (m >>> 13); };
146
+ const h2f = h => { const s = (h & 0x8000) << 16; let e = (h >>> 10) & 0x1f, m = h & 0x3ff;
147
+ if (e === 0) { if (m === 0) { _u[0] = s; return _f[0]; } while (!(m & 0x400)) { m <<= 1; e -= 1; } e += 1; m &= ~0x400; }
148
+ else if (e === 0x1f) { _u[0] = s | 0x7f800000 | (m << 13); return _f[0]; }
149
+ _u[0] = s | ((e + 112) << 23) | (m << 13); return _f[0]; };
150
+ const sessions = Object.values(generator.model.sessions || {});
151
+ let castsIn = 0, castsOut = 0;
152
+ for (const sess of sessions) {
153
+ const orig = sess.run.bind(sess);
154
+ sess.run = async (feeds, opts) => {
155
+ let Tctor = null;
156
+ for (const k in feeds) { if (feeds[k] && feeds[k].constructor) { Tctor = feeds[k].constructor; break; } }
157
+ for (const k in feeds) {
158
+ const t2 = feeds[k];
159
+ let wants16 = true;
160
+ try { const md = sess.inputMetadata; if (md) { const e = Array.isArray(md) ? md.find(x=>x.name===k) : md[k]; if (e && e.type && e.type !== 'float16') wants16 = false; } } catch(e3) {}
161
+ if (t2 && t2.type === 'float32' && k.startsWith('past_key_values') && wants16 && Tctor) {
162
+ const src = t2.data; const dst = new Uint16Array(src.length);
163
+ for (let i = 0; i < src.length; i++) dst[i] = f32h(src[i]);
164
+ feeds[k] = new Tctor('float16', dst, t2.dims);
165
+ castsIn++;
166
+ }
167
+ }
168
+ const out = await orig(feeds, opts);
169
+ try {
170
+ const lg = out.logits;
171
+ if (lg && lg.type === 'float16' && Tctor) {
172
+ const src = lg.data; const dst = new Float32Array(src.length);
173
+ for (let i = 0; i < src.length; i++) dst[i] = h2f(src[i]);
174
+ out.logits = new Tctor('float32', dst, lg.dims);
175
+ castsOut++;
176
+ }
177
+ } catch (e2) { console.warn('[GM] logits cast skipped', e2); }
178
+ return out;
179
+ };
180
+ }
181
+ window.__CASTS = () => ({ castsIn, castsOut });
182
+ L('[GM] fp16 I/O shim installed on ' + sessions.length + ' session(s)');
183
+ } catch (e) { L('[GM] fp16 shim install failed: ' + e.message); }
184
+ }
185
+
186
+ const msgs = [{ role: 'system', content: SYSTEM }, { role: 'user', content: 'What are the GPU Mart product series?' }];
187
+ let out = '';
188
+ const streamer = new TextStreamer(generator.tokenizer, {
189
+ skip_prompt: true, skip_special_tokens: true,
190
+ callback_function: t => { out += t; }
191
+ });
192
+ const t1 = performance.now();
193
+ const res = await generator(msgs, { max_new_tokens: NTOK, do_sample: false, repetition_penalty: 1.1, streamer });
194
+ const full = (res[0].generated_text.at(-1).content || out).trim();
195
+ L('generated in', Math.round(performance.now() - t1) + 'ms', 'runs=' + runCount, window.__CASTS ? window.__CASTS() : 'noshim');
196
+ L('ANSWER:', full);
197
+ window.__RESULT = { ok: true, answer: full, runs: runCount, casts: window.__CASTS ? window.__CASTS() : null };
198
+ } catch (e) {
199
+ L('FATAL: ' + (e.stack || e.message || String(e)));
200
+ window.__RESULT = { ok: false, error: String(e && e.message || e) };
201
+ }
202
+ window.__DONE = true;
203
+ </script>
204
+ </body></html>