No module named 'transformers.models.gpt'
Author: TPLong2002
Pull request: no
Labels: name: bug

### System Info

if True:
    from unsloth import FastLanguageModel
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "model", # YOUR MODEL YOU USED FOR TRAINING
        max_seq_length = 1024,
        dtype = None,
        load_in_4bit = True,        
    )

messages = [
    # {"role": "system", "content": "reasoning language: French\n\nYou are a helpful assistant that can solve mathematical problems."},
    {"role": "user", "content": "Xin chào"},
]
FastLanguageModel.for_inference(model)
inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt = True,
    return_tensors = "pt",
    return_dict = True,
    reasoning_effort = "low",
).to(model.device)
from transformers import TextStreamer
_ = model.generate(**inputs, max_new_tokens = 64, streamer = TextStreamer(tokenizer))

Error:
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[8], [line 3](vscode-notebook-cell:?execution_count=8&line=3)
      1 if True:
      2     from unsloth import FastLanguageModel
----> [3](vscode-notebook-cell:?execution_count=8&line=3)     model, tokenizer = FastLanguageModel.from_pretrained(
      4         model_name = "model", # YOUR MODEL YOU USED FOR TRAINING
      5         max_seq_length = 1024,
      6         dtype = None,
      7         load_in_4bit = True,        
      8     )
     10 messages = [
     11     # {"role": "system", "content": "reasoning language: French\n\nYou are a helpful assistant that can solve mathematical problems."},
     12     {"role": "user", "content": "Xin chào"},
     13 ]
     14 FastLanguageModel.for_inference(model)

File /opt/miniforge3/lib/python3.12/site-packages/unsloth/models/loader.py:347, in FastLanguageModel.from_pretrained(model_name, max_seq_length, dtype, load_in_4bit, load_in_8bit, full_finetuning, token, device_map, rope_scaling, fix_tokenizer, trust_remote_code, use_gradient_checkpointing, resize_model_vocab, revision, use_exact_model_name, fast_inference, gpu_memory_utilization, float8_kv_cache, random_state, max_lora_rank, disable_log_stats, *args, **kwargs)
    330     dispatch_model = FastQwen3Model if model_type == "qwen3" else FastQwen3MoeModel
    331 # elif model_type == "falcon_h1":
    332 #     dispatch_model = FastFalconH1Model
    333 #     if not SUPPORTS_FALCON_H1:
   (...)    345 #     dispatch_model = FastGraniteModel
    346 else:
--> [347](https://vscode-remote+ssh-002dremote-002b79-002e141-002e67-002e124.vscode-resource.vscode-cdn.net/opt/miniforge3/lib/python3.12/site-packages/unsloth/models/loader.py:347)     return FastModel.from_pretrained(
    348         model_name                 = model_name,
    349         max_seq_length             = max_seq_length,
    350         dtype                      = dtype,
    351         load_in_4bit               = load_in_4bit,
    352         load_in_8bit               = load_in_8bit,
    353         full_finetuning            = full_finetuning,
    354         token                      = token,
    355         device_map                 = device_map,
    356         rope_scaling               = rope_scaling, # [TODO] No effect
    357         fix_tokenizer              = fix_tokenizer, # [TODO] No effect
    358         trust_remote_code          = trust_remote_code,
    359         use_gradient_checkpointing = use_gradient_checkpointing,
    360         resize_model_vocab         = resize_model_vocab, # [TODO] No effect
    361         revision                   = revision,
    362         return_logits              = False, # Return logits
    363         fullgraph                  = True, # No graph breaks
    364         use_exact_model_name       = use_exact_model_name,
    365         *args, **kwargs,
    366     )
    367 pass
    369 if use_gradient_checkpointing == "unsloth":

File /opt/miniforge3/lib/python3.12/site-packages/unsloth/models/loader.py:[803](https://vscode-remote+ssh-002dremote-002b79-002e141-002e67-002e124.vscode-resource.vscode-cdn.net/opt/miniforge3/lib/python3.12/site-packages/unsloth/models/loader.py:803), in FastModel.from_pretrained(model_name, max_seq_length, dtype, load_in_4bit, load_in_8bit, full_finetuning, token, device_map, rope_scaling, fix_tokenizer, trust_remote_code, use_gradient_checkpointing, resize_model_vocab, revision, return_logits, fullgraph, use_exact_model_name, auto_model, whisper_language, whisper_task, unsloth_force_compile, *args, **kwargs)
    801 with redirector:
    802     patch_loss_functions(torch_compile = False)
--> 803     model_types, supports_sdpa = unsloth_compile_transformers(
    804         dtype                   = dtype,
    805         model_name              = model_name,
    806         model_types             = model_types,
    807         token                   = token,
    808         sdpa_dynamic_mask       = True,
    809         sdpa_bool_masks         = True,
    810         sdpa_gqa_replace        = True,
    811         sdpa_dynamic_compile    = True,
    812         compile_attention       = True,
    813         disable_causal_masks    = True,
    814         compile_torch_modules   = True,
    815         compile_custom_modules  = True,
    816         compile_function_calls  = True,
    817         fuse_lm_head            = True,
    818         gradient_checkpointing  = True,
    819         manual_replacements     = True,
    820         fast_lora_forwards      = True,
    821         fast_residual_stream    = False,
    822         accurate_accumulation   = True,
    823         epilogue_fusion         = True,
    824         max_autotune            = False,
    825         shape_padding           = True,
    826         cudagraphs              = False,
    827         debug                   = False,
    828         fullgraph               = fullgraph,
    829         import_from_cache       = False,
    830         disable                 = False,
    831         return_logits           = return_logits,
    832         trust_remote_code       = trust_remote_code,
    833         unsloth_force_compile   = unsloth_force_compile,
    834     )
    835 pass
    836 # Fix SDPA

File /opt/miniforge3/lib/python3.12/site-packages/unsloth/models/_utils.py:[1399](https://vscode-remote+ssh-002dremote-002b79-002e141-002e67-002e124.vscode-resource.vscode-cdn.net/opt/miniforge3/lib/python3.12/site-packages/unsloth/models/_utils.py:1399), in unsloth_compile_transformers(dtype, model_name, model_types, token, revision, trust_remote_code, sdpa_dynamic_mask, sdpa_bool_masks, sdpa_gqa_replace, sdpa_dynamic_compile, compile_attention, disable_causal_masks, compile_torch_modules, compile_custom_modules, compile_function_calls, fuse_lm_head, gradient_checkpointing, manual_replacements, fast_lora_forwards, fast_residual_stream, accurate_accumulation, epilogue_fusion, max_autotune, shape_padding, cudagraphs, debug, fullgraph, import_from_cache, disable, return_logits, unsloth_force_compile)
   1397 supports_sdpa = [True]
   1398 for model_type in model_types:
-> 1399     _unsloth_compile_transformers(
   1400         model_type,
   1401         sdpa_dynamic_mask      = sdpa_dynamic_mask,
   1402         sdpa_bool_masks        = sdpa_bool_masks,
   1403         sdpa_gqa_replace       = sdpa_gqa_replace,
   1404         sdpa_dynamic_compile   = sdpa_dynamic_compile,
   1405         compile_attention      = compile_attention,
   1406         disable_causal_masks   = disable_causal_masks,
   1407         compile_torch_modules  = compile_torch_modules,
   1408         compile_custom_modules = compile_custom_modules,
   1409         compile_function_calls = compile_function_calls,
   1410         fuse_lm_head           = fuse_lm_head,
   1411         gradient_checkpointing = gradient_checkpointing,
   1412         manual_replacements    = manual_replacements,
   1413         fast_lora_forwards     = fast_lora_forwards,
   1414         fast_residual_stream   = fast_residual_stream,
   1415         accurate_accumulation  = accurate_accumulation,
   1416         epilogue_fusion        = epilogue_fusion,
   1417         max_autotune           = max_autotune,
   1418         shape_padding          = shape_padding,
   1419         cudagraphs             = cudagraphs,
   1420         debug                  = debug,
   1421         fullgraph              = fullgraph,
   1422         import_from_cache      = import_from_cache,
   1423         disable                = disable,
   1424         return_logits          = return_logits,
   1425         supports_sdpa          = supports_sdpa,
   1426     )
   1427 pass
   1428 # Redo patches which override compiler

File /opt/miniforge3/lib/python3.12/site-packages/unsloth_zoo/compiler.py:[1913](https://vscode-remote+ssh-002dremote-002b79-002e141-002e67-002e124.vscode-resource.vscode-cdn.net/opt/miniforge3/lib/python3.12/site-packages/unsloth_zoo/compiler.py:1913), in unsloth_compile_transformers(model_type, sdpa_dynamic_mask, sdpa_bool_masks, sdpa_gqa_replace, sdpa_dynamic_compile, compile_attention, disable_causal_masks, compile_torch_modules, compile_custom_modules, compile_function_calls, fuse_lm_head, gradient_checkpointing, manual_replacements, fast_lora_forwards, fast_residual_stream, accurate_accumulation, epilogue_fusion, max_autotune, shape_padding, cudagraphs, debug, fullgraph, import_from_cache, disable, return_logits, supports_sdpa)
   1910 pass
   1912 model_location = f"transformers.models.{model_type}.modeling_{model_type}"
-> 1913 exec(f"import {model_location}", globals())
   1914 modeling_file = eval(model_location)
   1915 if hasattr(modeling_file, "__UNSLOTH_PATCHED__"): return

File <string>:1

ModuleNotFoundError: No module named 'transformers.models.gpt'

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

model.save_pretrained_merged("/root/test/model", tokenizer)

### Expected behavior

.

Comments:
I'll take a look, thank you for reporting this

@TPLong2002  Have run the script with no error, can you show me the model_name you use?

@vvvdwbvvv 
First of all, thank you for the quick response.
I fine-tuned the GPT OSS model as usual and saved it with save_pretrained_merged:

model.save_pretrained_merged("/root/test/model", tokenizer)


instead of save_pretrained. Then I loaded the model like this:

if True:
    from unsloth import FastLanguageModel
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "model", # YOUR MODEL YOU USED FOR TRAINING
        max_seq_length = 1024,
        dtype = None,
        load_in_4bit = True,
    )

messages = [
    # {"role": "system", "content": "reasoning language: French\n\nYou are a helpful assistant that can solve mathematical problems."},
    {"role": "user", "content": "Xin chào"},
]
FastLanguageModel.for_inference(model)
inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt = True,
    return_tensors = "pt",
    return_dict = True,
    reasoning_effort = "low",
).to(model.device)
from transformers import TextStreamer
_ = model.generate(**inputs, max_new_tokens = 64, streamer = TextStreamer(tokenizer))

With the folder name being model, I got the error:
No module named 'transformers.models.gpt'.

And one more question: after fine-tuning the GPT OSS model, why does the model sometimes give incorrect or irrelevant responses to simple questions like "Hello" — for example, it respond with a math formula or other unrelated content?


@TPLong2002  It seems like the model_type in config didn't map to the model_name, suggest check the config.json in the model path and check the import 

Hi, I’d like to try to fix this: should we alias gpt to an existing model path, or provide a module stub?

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.