Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-haiku-20241022· AnthropicHugging Face LMArena Human Preference 140K

I have fine-tuned a model from huggingface. Now I have my own model with some additional…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    I have fine-tuned a model from huggingface. Now I have my own model with some additional code that is required for this model. What is official procedure to integrate my code with transformers library, so that people can load my model and tokenizer with AutoModelForCausalLM, AutoTokenizer?

  2. Claude Assistant

    To integrate your custom model with the Hugging Face Transformers library, you'll need to follow these key steps:

    1. Create a Model Configuration
    • Subclass PretrainedConfig from transformers
    • Define your model's specific configuration parameters
    • Implement __init__ and to_json_file() methods

    Example:

    from transformers import PretrainedConfig
    
    class MyModelConfig(PretrainedConfig):
        model_type = "my_custom_model"
        
        def __init__(
            self, 
            vocab_size=50257, 
            n_positions=1024, 
            n_embd=768, 
            n_layer=12,
            **kwargs
        ):
            self.vocab_size = vocab_size
            self.n_positions = n_positions
            self.n_embd = n_embd
            self.n_layer = n_layer
            super().__init__(**kwargs)
    
    1. Create Model Architecture
    • Subclass appropriate base model class (e.g., PreTrainedModel)
    • Implement forward method
    • Register configuration with register_for_auto_class()

    Example:

    from transformers import PreTrainedModel
    import torch.nn as nn
    
    class MyModel(PreTrainedModel):
        config_class = MyModelConfig
        
        def __init__(self, config):
            super().__init__(config)
            self.transformer = nn.Transformer(...)
        
        def forward(self, input_ids, ...):
            # Implementation
            pass
    
    MyModel.register_for_auto_class()
    
    1. Create Tokenizer
    • Subclass PreTrainedTokenizer
    • Implement tokenization methods
    1. Prepare Model Repository
    • Create a model repository on Hugging Face Hub
    • Include:
      • Model weights
      • Configuration file
      • Tokenizer files
      • README with model details
    1. Add Model to Auto Classes
    • In your model's __init__.py, register with auto classes:
    from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
    
    AutoConfig.register("my_custom_model", MyModelConfig)
    AutoModelForCausalLM.register(MyModelConfig, MyModel)
    
    1. Push to Hugging Face Hub
    model.push_to_hub("username/my-model")
    tokenizer.push_to_hub("username/my-model")
    

    After these steps, users can load your model via:

    from transformers import AutoModelForCausalLM, AutoTokenizer
    
    model = AutoModelForCausalLM.from_pretrained("username/my-model")
    tokenizer = AutoTokenizer.from_pretrained("username/my-model")
    

    Key Recommendations:

    • Follow Transformers library conventions
    • Provide comprehensive documentation
    • Ensure compatibility with existing auto classes
    • Test thoroughly

    Would you like me to elaborate on any of these steps?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.