#!/bin/bash
# Add remaining debug timing around load_model_weights and model.to()
SD_PY="/home/fabian/ComfyUI/comfy/sd.py"

# Add before model.to(offload_device)
python3 -c "
with open('$SD_PY', 'r') as f:
    content = f.read()

old = '''    if not model_management.is_device_cpu(offload_device):
        model.to(offload_device)
    model.load_model_weights(new_sd, \"\", assign=model_patcher.is_dynamic())'''

new = '''    if not model_management.is_device_cpu(offload_device):
        logging.warning(f\"[BC250_DEBUG] Moving model to {offload_device}...\")  # BC250_DEBUG
        _td = _t.time()
        model.to(offload_device)
        logging.warning(f\"[BC250_DEBUG] Model moved in {_t.time()-_td:.1f}s\")  # BC250_DEBUG
    logging.warning(\"[BC250_DEBUG] Loading model weights (453 GGUF tensors)...\")  # BC250_DEBUG
    _tw = _t.time()
    model.load_model_weights(new_sd, \"\", assign=model_patcher.is_dynamic())
    logging.warning(f\"[BC250_DEBUG] Model weights loaded in {_t.time()-_tw:.1f}s\")  # BC250_DEBUG'''

content = content.replace(old, new, 1)
with open('$SD_PY', 'w') as f:
    f.write(content)
print('Done')
"

echo "=== Verify ==="
grep -n 'BC250_DEBUG' "$SD_PY"
