Skip to content

Loading model from Hub #184

Closed
Closed

Description

@OsaCode

Hello there :)

After finetuning an adapter and saving it to the hub, is there a way to load it to unsloth for inference ?

Using just FastLanguageModel.from_pretrained with my hub repo I get this output:

RepositoryNotFoundError: 401 Client Error. (Request ID: Root=1-65d481b6-6b5d4c427892ef7b0fe2586f;61898954-e4bf-4447-bcd7-74d6790fa7f0)

Repository Not Found for url: https://huggingface.co/OsaCode/######/resolve/main/adapter_model.bin.
Please make sure you specified the correct repo_id and repo_type.
If you are trying to access a private or gated repo, make sure you are authenticated.
Invalid username or password.

Activity

  1. danielhanchen commented on Feb 20, 2024

    @danielhanchen
    Member

    @OsaCode When you load LoRA adapters, did you load only your name/model_name?

    Ie: danielhanchen/lora_model
    Or unsloth/mistral-7b-bnb-4bit

    After that, the below code is taken from the Mistral 7b notebook in the Readme:

    from unsloth import FastLanguageModel
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "lora_model", # YOUR MODEL YOU USED FOR TRAINING
        max_seq_length = max_seq_length,
        dtype = dtype,
        load_in_4bit = load_in_4bit,
    )
    FastLanguageModel.for_inference(model) # Enable native 2x faster inference
    alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
    
    ### Instruction:
    {}
    
    ### Input:
    {}
    
    ### Response:
    {}"""
    
    inputs = tokenizer(
    [
        alpaca_prompt.format(
            "What is a famous tall tower in Paris?", # instruction
            "", # input
            "", # output - leave this blank for generation!
        )
    ], return_tensors = "pt").to("cuda")
    
    outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)
    tokenizer.batch_decode(outputs)
  2. OsaCode commented on Feb 20, 2024

    @OsaCode
    Author

    Indeed, I tried to find if FastLanguageModel.from_pretrained had an argument "adapter" or "token" to let it query my private repo, but did not succeed.

    I am able to load the model I saved in local, but I wanted to do the same from the hub but the documentation does not show any example.

  3. OsaCode commented on Feb 20, 2024

    @OsaCode
    Author

    I was able to load the model and the adapter using HuggingFace base method :
    `from peft import PeftConfig, PeftModel
    from transformers import AutoModelForCausalLM, AutoTokenizer
    from unsloth import FastLanguageModel

    base_model_name = "unsloth/mistral-7b-bnb-4bit"
    adapter_model_name = "OsaCode/xxx"

    model = AutoModelForCausalLM.from_pretrained(base_model_name, token="xxx")
    model = PeftModel.from_pretrained(model, adapter_model_name, token="xxx")

    tokenizer = AutoTokenizer.from_pretrained(base_model_name)
    FastLanguageModel.for_inference(model) # Enable native 2x faster inference`

    But I am not sure Unsloth's faster inference took effect.

  4. danielhanchen commented on Feb 20, 2024

    @danielhanchen
    Member

    @OsaCode Oh for a token + your model OsaCode/xxx

    from unsloth import FastLanguageModel
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "OsaCode/xxx",
        max_seq_length = max_seq_length,
        dtype = dtype,
        load_in_4bit = load_in_4bit,
        token = "hf_...",
    )
    FastLanguageModel.for_inference(model)
  5. OsaCode commented on Feb 20, 2024

    @OsaCode
    Author

    I saved to the hub the adapter I trained using the example notebook.
    It's not a complete model.
    I get the error in my Issue message with the token (which is correct, I used it with PeftModel.from_pretrained).

  6. danielhanchen commented on Feb 20, 2024

    @danielhanchen
    Member

    @OsaCode I just updated Unsloth - maybe it might work now? Unsure though :( Sorry on the issue :(

  7. OsaCode commented on Feb 21, 2024

    @OsaCode
    Author

    You mean that it should be able to load the complete model just from the adapters ? It does not need to be given the name of the base model as well ?

    I will be updating and trying.

  8. danielhanchen commented on Feb 21, 2024

    @danielhanchen
    Member

    @OsaCode Yes exactly! I will auto detect it as an adapter, and I will do the patching for you internally :)

  9. OsaCode commented on Feb 21, 2024

    @OsaCode
    Author

    Ok updating solved the issue, thank you

  10. added a commit that references this issue on Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions