transformers - 💡(How to fix) Fix Loading kimik2 is taking forever [4 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44276Fetched 2026-04-08 00:29:24
View on GitHub
Comments
4
Participants
4
Timeline
10
Reactions
0
Timeline (top)
commented ×4mentioned ×3subscribed ×3

Code Example

The above is slow at - match_named_modules (compressed_tensors/utils/match.py:69) 
Even if i bypass this. there is another forloop due to which the execution appears stuck
RAW_BUFFERClick to expand / collapse
import json
import yaml
import argparse
from dotenv import load_dotenv
import torch
from pathlib import Path
from transformers import (
    AutoConfig,
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
)

model_name = os.path.expanduser("~/model/kimik2")
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)

The above is slow at - match_named_modules (compressed_tensors/utils/match.py:69) Even if i bypass this. there is another forloop due to which the execution appears stuck

Thread 0x7E57077D8740 (active+gil): "MainThread"                                                                            
    <dictcomp> (transformers/modeling_utils.py:5353)                                                                        
    _load_pretrained_model (transformers/modeling_utils.py:5352)

The version i am using is 4.57.6.

  1. Can someone please help to let me know how to enable fast loading of large models like kimik2?
  2. How to load a model that is natively in int4? https://huggingface.co/moonshotai/Kimi-K2-Thinking/tree/main. Will AutoModelForCausalLM.from_pretrained only work with bf16 and then use QLoRa to convert again to int4?

extent analysis

Problem Summary Fast loading of large models like kimik2 is slow due to the use of trust_remote_code=True and potential forloop issues.

Root Cause Analysis The root cause is likely due to the large size of the kimik2 model and the use of trust_remote_code=True, which can lead to slow loading times.

Fix Plan To enable fast loading of large models like kimik2, follow these steps:

1. Disable trust_remote_code=True

model = AutoModelForCausalLM.from_pretrained(model_name)

2. Use torch.load to load the model

Instead of using from_pretrained, try loading the model using torch.load:

model_state_dict = torch.load(model_name, map_location='cpu')
model = AutoModelForCausalLM.from_pretrained(model_name)
model.load_state_dict(model_state_dict)

3. Use fp16 or bf16 for faster loading

If you have a GPU with fp16 or bf16 support, you can use fp16 or bf16 to load the model faster:

from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)

4. Use torch.jit.load for faster loading

You can also use torch.jit.load to load the model faster:

model_jit = torch.jit.load(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
model.load_state_dict(model_jit.state_dict())

Verification To verify that the fix worked, check the loading time of the model. You can use the time module to measure the loading time:

import time
start_time = time.time()
model = AutoModel

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING