File size: 12,487 Bytes
41d057e
 
0805dfd
 
41d057e
 
 
f95131d
41d057e
0805dfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41d057e
 
 
0805dfd
 
 
 
41d057e
 
0805dfd
 
 
 
 
 
 
 
 
2bb9931
0805dfd
 
 
2bb9931
 
0805dfd
2bb9931
0805dfd
 
2bb9931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0805dfd
 
 
2bb9931
0805dfd
 
 
 
 
 
 
 
 
 
2bb9931
0805dfd
 
2bb9931
0805dfd
2bb9931
 
 
 
 
0805dfd
2bb9931
 
0805dfd
 
 
 
 
 
 
2bb9931
 
 
0805dfd
 
 
 
 
 
2bb9931
 
 
 
 
0805dfd
 
2bb9931
 
 
0805dfd
 
2bb9931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
768779b
0805dfd
 
 
 
 
 
41d057e
0805dfd
 
 
 
 
 
41d057e
0805dfd
 
 
 
 
 
 
 
 
 
41d057e
 
0805dfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb9931
 
 
 
 
 
 
0805dfd
 
 
 
 
 
41d057e
0805dfd
 
 
 
41d057e
8819a05
0805dfd
41d057e
44f3242
41d057e
8819a05
41d057e
8819a05
0805dfd
 
 
41d057e
0805dfd
 
 
 
 
 
 
166c408
 
0805dfd
2bb9931
 
 
 
 
 
 
 
0805dfd
166c408
0805dfd
 
 
 
 
 
166c408
 
 
41d057e
0805dfd
41d057e
0805dfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41d057e
 
 
0805dfd
 
 
 
 
 
 
41d057e
 
 
0805dfd
166c408
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import gradio as gr
import torch
import time
import traceback
from threading import Thread
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer

model_id = "rahul7star/gemma-4-finetune"


def log(msg):
    print(f"[DEBUG] {msg}", flush=True)


log("Starting Gemma 4 debug app")
log(f"Model ID: {model_id}")
log(f"Torch version: {torch.__version__}")
log(f"CUDA available: {torch.cuda.is_available()}")

if torch.cuda.is_available():
    log(f"CUDA device count: {torch.cuda.device_count()}")
    log(f"CUDA device name: {torch.cuda.get_device_name(0)}")


# ============================================================
# Load Tokenizer
# ============================================================

log("Loading tokenizer...")

tokenizer = AutoTokenizer.from_pretrained(
    model_id,
    trust_remote_code=True,
)

log("Tokenizer loaded")
log(f"Tokenizer class: {tokenizer.__class__.__name__}")
log(f"Vocab size: {len(tokenizer)}")
log(f"EOS token: {tokenizer.eos_token} / {tokenizer.eos_token_id}")
log(f"PAD token: {tokenizer.pad_token} / {tokenizer.pad_token_id}")
log(f"Chat template exists: {tokenizer.chat_template is not None}")

if tokenizer.pad_token_id is None:
    tokenizer.pad_token = tokenizer.eos_token
    log("PAD token was missing, set PAD token = EOS token")


# ============================================================
# Load Model
# ============================================================

log("Loading model...")

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="cpu",
    low_cpu_mem_usage=True,
    torch_dtype=torch.bfloat16,
    trust_remote_code=True,
)

model.eval()

log("Model loaded")
log(f"Model class: {model.__class__.__name__}")
log(f"Model device: {model.device}")
log(f"Model dtype: {next(model.parameters()).dtype}")


# ============================================================
# Config Logs
# ============================================================

cfg = model.config
text_cfg = getattr(cfg, "text_config", None)
vision_cfg = getattr(cfg, "vision_config", None)

log("========== MAIN MODEL CONFIG ==========")
log(f"model_type: {getattr(cfg, 'model_type', None)}")
log(f"architectures: {getattr(cfg, 'architectures', None)}")
log(f"is_encoder_decoder: {getattr(cfg, 'is_encoder_decoder', None)}")
log(f"text_config exists: {text_cfg is not None}")
log(f"vision_config exists: {vision_cfg is not None}")
log("=======================================")

if text_cfg is not None:
    log("========== TEXT CONFIG ==========")
    log(f"model_type: {getattr(text_cfg, 'model_type', None)}")
    log(f"hidden_size: {getattr(text_cfg, 'hidden_size', None)}")
    log(f"intermediate_size: {getattr(text_cfg, 'intermediate_size', None)}")
    log(f"num_hidden_layers: {getattr(text_cfg, 'num_hidden_layers', None)}")
    log(f"num_attention_heads: {getattr(text_cfg, 'num_attention_heads', None)}")
    log(f"num_key_value_heads: {getattr(text_cfg, 'num_key_value_heads', None)}")
    log(f"head_dim: {getattr(text_cfg, 'head_dim', None)}")
    log(f"vocab_size: {getattr(text_cfg, 'vocab_size', None)}")
    log(f"max_position_embeddings: {getattr(text_cfg, 'max_position_embeddings', None)}")
    log(f"rope_theta: {getattr(text_cfg, 'rope_theta', None)}")
    log(f"rms_norm_eps: {getattr(text_cfg, 'rms_norm_eps', None)}")
    log(f"attention_bias: {getattr(text_cfg, 'attention_bias', None)}")
    log(f"use_cache: {getattr(text_cfg, 'use_cache', None)}")
    log(f"sliding_window: {getattr(text_cfg, 'sliding_window', None)}")
    log(f"query_pre_attn_scalar: {getattr(text_cfg, 'query_pre_attn_scalar', None)}")
    log(f"final_logit_softcapping: {getattr(text_cfg, 'final_logit_softcapping', None)}")
    log(f"attn_logit_softcapping: {getattr(text_cfg, 'attn_logit_softcapping', None)}")
    log("=================================")

if vision_cfg is not None:
    log("========== VISION CONFIG ==========")
    log(f"model_type: {getattr(vision_cfg, 'model_type', None)}")
    log(f"hidden_size: {getattr(vision_cfg, 'hidden_size', None)}")
    log(f"intermediate_size: {getattr(vision_cfg, 'intermediate_size', None)}")
    log(f"num_hidden_layers: {getattr(vision_cfg, 'num_hidden_layers', None)}")
    log(f"num_attention_heads: {getattr(vision_cfg, 'num_attention_heads', None)}")
    log(f"image_size: {getattr(vision_cfg, 'image_size', None)}")
    log(f"patch_size: {getattr(vision_cfg, 'patch_size', None)}")
    log("===================================")


# ============================================================
# Parameter Logs
# ============================================================

total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)

log(f"Total parameters: {total_params:,}")
log(f"Trainable parameters: {trainable_params:,}")


# ============================================================
# Module Listing
# ============================================================

log("========== TEXT MODEL MODULES ==========")

text_keywords = [
    "language_model",
    "text_model",
    "model.layers",
    "self_attn",
    "mlp",
    "input_layernorm",
    "post_attention_layernorm",
    "q_proj",
    "k_proj",
    "v_proj",
    "o_proj",
    "gate_proj",
    "up_proj",
    "down_proj",
    "rotary",
    "embed_tokens",
    "lm_head",
]

count = 0

for name, module in model.named_modules():
    lower = name.lower()

    if "vision_tower" in lower:
        continue

    if any(k in lower for k in text_keywords):
        log(f"{name} => {module.__class__.__name__}")
        count += 1

        if count >= 200:
            log("Stopped text module logging after 200 entries")
            break

log(f"Text modules logged: {count}")
log("========================================")


# ============================================================
# Deep Forward Hooks
# ============================================================

DEBUG_HOOKS = True
HOOK_EVERY_N_CALLS = 20
_hook_calls = {}


def tensor_stats(x):
    if not torch.is_tensor(x):
        return str(type(x))

    with torch.no_grad():
        xf = x.detach().float()
        return (
            f"shape={tuple(x.shape)}, "
            f"dtype={x.dtype}, "
            f"device={x.device}, "
            f"mean={xf.mean().item():.5f}, "
            f"std={xf.std().item():.5f}, "
            f"min={xf.min().item():.5f}, "
            f"max={xf.max().item():.5f}"
        )


def get_first_tensor(obj):
    if torch.is_tensor(obj):
        return obj

    if isinstance(obj, (list, tuple)):
        for item in obj:
            t = get_first_tensor(item)
            if t is not None:
                return t

    if isinstance(obj, dict):
        for item in obj.values():
            t = get_first_tensor(item)
            if t is not None:
                return t

    return None


def make_hook(name):
    def hook(module, inputs, output):
        if not DEBUG_HOOKS:
            return

        _hook_calls[name] = _hook_calls.get(name, 0) + 1

        if _hook_calls[name] % HOOK_EVERY_N_CALLS != 1:
            return

        inp = get_first_tensor(inputs)
        out = get_first_tensor(output)

        log(f"HOOK: {name}")

        if inp is not None:
            log(f"  input  -> {tensor_stats(inp)}")

        if out is not None:
            log(f"  output -> {tensor_stats(out)}")

    return hook


def attach_debug_hooks():
    wanted = [
        "self_attn",
        "q_proj",
        "k_proj",
        "v_proj",
        "o_proj",
        "mlp",
        "gate_proj",
        "up_proj",
        "down_proj",
        "input_layernorm",
        "post_attention_layernorm",
        "rotary_emb",
        "lm_head",
    ]

    attached = 0

    for name, module in model.named_modules():
        lower = name.lower()

        if "vision_tower" in lower:
            continue

        if any(w in lower for w in wanted):
            module.register_forward_hook(make_hook(name))
            attached += 1

    log(f"Attached debug hooks: {attached}")


#attach_debug_hooks()


# ============================================================
# Generation Function
# ============================================================

def generate_response(message, history):
    start_time = time.time()

    log("========== NEW GENERATION ==========")
    log(f"User message: {message}")
    log(f"History turns: {len(history)}")

    messages = []

    for item in history:
        try:
            user_msg, bot_msg = item
            messages.append({"role": "user", "content": user_msg})
            messages.append({"role": "assistant", "content": bot_msg})
        except Exception as e:
            log(f"History parse warning: {e}")
            log(f"Bad history item: {item}")

    messages.append({"role": "user", "content": message})

    log(f"Total chat messages: {len(messages)}")

    try:
        inputs = tokenizer.apply_chat_template(
            messages,
            return_tensors="pt",
            return_dict=True,
            add_generation_prompt=True,
        ).to(model.device)

        input_token_count = inputs["input_ids"].shape[-1]

        log(f"Input tensor shape: {inputs['input_ids'].shape}")
        log(f"Input tokens: {input_token_count}")
        log(f"Input device: {inputs['input_ids'].device}")

        log("========== TOKEN DEBUG ==========")
        ids = inputs["input_ids"][0].tolist()
        log(f"First 20 token ids: {ids[:20]}")
        log(f"Last 20 token ids: {ids[-20:]}")
        log(f"Decoded prompt preview: {tokenizer.decode(ids[-200:], skip_special_tokens=False)}")
        log("=================================")

    except Exception as e:
        log("Chat template/tokenization failed")
        log(traceback.format_exc())
        yield f"Tokenization error: {e}"
        return

    streamer = TextIteratorStreamer(
        tokenizer,
        timeout=420.0,
        skip_prompt=True,
        skip_special_tokens=True,
    )
   

    generate_kwargs = dict(
        **inputs,
        streamer=streamer,
        max_new_tokens=1024,
        temperature=0.7,
        do_sample=False,
        top_p=0.9,
        pad_token_id=tokenizer.pad_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )

    log("Generation kwargs:")
    log("max_new_tokens=1024")
    log("temperature=0.7")
    log("do_sample=True")
    log("top_p=0.9")

    def run_generation():
        try:
            log("Generation thread started")

            gen_start = time.time()

            with torch.no_grad():
                model.generate(**generate_kwargs)

            gen_time = time.time() - gen_start
            log(f"Generation thread finished in {gen_time:.2f}s")

        except Exception as e:
            log("Generation Error")
            log(traceback.format_exc())

            streamer.text_queue.put(
                f"\n[Generation thread crashed. Reason: {e}]"
            )
            streamer.end()

    t = Thread(target=run_generation)
    t.start()

    partial_text = ""
    token_chunks = 0

    try:
        for new_text in streamer:
            token_chunks += 1
            partial_text += new_text

            if token_chunks % 20 == 0:
                elapsed = time.time() - start_time
                log(
                    f"Streaming chunks: {token_chunks}, "
                    f"chars: {len(partial_text)}, "
                    f"elapsed: {elapsed:.2f}s"
                )

            yield partial_text

    except Exception as e:
        log("Streaming Error")
        log(traceback.format_exc())
        yield partial_text + f"\n\n[Streaming error: {e}]"

    finally:
        elapsed = time.time() - start_time
        log("========== GENERATION DONE ==========")
        log(f"Output chars: {len(partial_text)}")
        log(f"Streaming chunks: {token_chunks}")
        log(f"Elapsed seconds: {elapsed:.2f}")
        log("=====================================")


# ============================================================
# Gradio UI
# ============================================================

demo = gr.ChatInterface(
    fn=generate_response,
    title="Gemma 4 E4B - Debug",
    examples=[
        "Explain quantum entanglement simply.",
        "Write a Python function to add two numbers.",
        "Explain how RoPE works in transformer attention.",
    ],
    cache_examples=False,
)

if __name__ == "__main__":
    log("Launching Gradio app...")
    demo.launch()