@seanbenhur (Sean Benhur) posted a new topic, subject "How to create a config.json after saving a model". It read:

   Hi, I am trying to convert my model to onnx format with the help of this notebook  
    I got error , since config.json does not exist.  
    My model is a custom model with extra layers, similar to this,
    
    Now how can I create a config.json file for this?

After which, @nielsr (Niels Rogge) added

    Normally, if you save your model using the `.save_pretrained()` method, it will save both the model weights and a `config.json` file in the specified directory.

To which @seanbenhur (Sean Benhur) replied

    Yes, but this is a custom model that I have saved in pytorch style, since it consists of additional layers, is there anyway to generate confg.json file?

Then @BramVanroy (Bram Vanroy) said

    You need to subclass it to have the save_pretrained methods available. So instead of
    [code] 
        class Mean_Pooling_Model(nn.Module):
        
    [/code]
    
    use
    [code] 
        from transformers.modeling_utils import PreTrainedModel
        class Mean_Pooling_Model(PreTrainedModel):
        
    [/code]
    
    It will add extra functionality on top of `nn.Module`.
    
    github.com
    
    #### huggingface/transformers/blob/bcc3f7b6560c1ed427f051107c7755956a27a9f2/src/transformers/modeling_utils.py#L415
    [code] 
            
              
        
        
                  
          405.             exclude_embeddings (:obj:`bool`, `optional`, defaults to :obj:`True`):
        
                  
          406.                 Whether or not to count embedding and softmax operations.
        
                  
          407. 
                  
        
        
          408.         Returns:
        
                  
          409.             :obj:`int`: The number of floating-point operations.
        
                  
          410.         """
        
                  
          411. 
                  
        
        
          412.         return 6 * self.estimate_tokens(input_dict) * self.num_parameters(exclude_embeddings=exclude_embeddings)
        
                  
          413. 
                  
        
        
          414. 
                  
        
        
          415. class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMixin):
        
                  
          416.     r"""
        
                  
          417.     Base class for all models.
        
                  
          418. 
                  
        
        
          419.     :class:`~transformers.PreTrainedModel` takes care of storing the configuration of the models and handles methods
        
                  
          420.     for loading, downloading and saving models as well as a few methods common to all models to:
        
                  
          421. 
                  
        
        
          422.         * resize the input embeddings,
        
                  
          423.         * prune heads in the self-attention heads.
        
                  
          424. 
                  
        
        
          425.     Class attributes (overridden by derived classes):
        
              
        
            
          
    [/code]

To which @seanbenhur (Sean Benhur) replied

    Thank you I will try this!

@seanbenhur (Sean Benhur) replied

    Is it possible to generate the configuration file for already trained model , i.e weights stored in normal pytorch model.bin

Then @avinash-chaluvadi (Avinash Chaluvadi) said

    Use model.config.to_json() method to generate config.json

To which @sophiaaez (Sophia Zell) replied

    Did you end up finding a solution to getting a config.json from an already trained model?  I'm currently struggling with the same problem

After which, @seanbenhur (Sean Benhur) added

    Nope, I was not able to find a proper solution, I ended up writing the config.json manually

Then @BramVanroy (Bram Vanroy) said

    You should be able to just call
    [code] 
        model.config.to_json_file("config.json")
        
    [/code]
    
    github.com
    
    #### huggingface/transformers/blob/1be8d56ec6f7113810adc716255d371e78e8a1af/src/transformers/configuration_utils.py#L808
    [code] 
              
                
        
        
                    
          798. 
                    
        
        
          799.     Returns:
        
                    
          800.         `str`: String containing all the attributes that make up this configuration instance in JSON format.
        
                    
          801.     """
        
                    
          802.     if use_diff is True:
        
                    
          803.         config_dict = self.to_diff_dict()
        
                    
          804.     else:
        
                    
          805.         config_dict = self.to_dict()
        
                    
          806.     return json.dumps(config_dict, indent=2, sort_keys=True) + "\n"
        
                    
          807. 
                    
        
        
          808. def to_json_file(self, json_file_path: Union[str, os.PathLike], use_diff: bool = True):
        
                    
          809.     """
        
                    
          810.     Save this instance to a JSON file.
        
                    
          811. 
                    
        
        
          812.     Args:
        
                    
          813.         json_file_path (`str` or `os.PathLike`):
        
                    
          814.             Path to the JSON file in which this configuration instance's parameters will be saved.
        
                    
          815.         use_diff (`bool`, *optional*, defaults to `True`):
        
                    
          816.             If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`
        
                    
          817.             is serialized to JSON file.
        
                    
          818.     """
        
                
        
              
            
    [/code]
    
    cc @seanbenhur

To which @sophiaaez (Sophia Zell) replied

    That only works for models that are transformer native and not nn.Module/pytorch native, sadly.

So @BramVanroy (Bram Vanroy) replied

    What is your use-case that you are using Transformers but not Transformers models? If you want to use the HF Trainer alongside with your own PyTorch model, I recommended to subclass the relevant classes, similar to PretrainedModel
    
    github.com
    
    #### huggingface/transformers/blob/3981ee8650042e89d9c430ec34def2d58a2a12f7/src/transformers/modeling_utils.py#L955
    [code] 
              
                
        
        
                    
          945.             exclude_embeddings (`bool`, *optional*, defaults to `True`):
        
                    
          946.                 Whether or not to count embedding and softmax operations.
        
                    
          947. 
                    
        
        
          948.         Returns:
        
                    
          949.             `int`: The number of floating-point operations.
        
                    
          950.         """
        
                    
          951. 
                    
        
        
          952.         return 6 * self.estimate_tokens(input_dict) * self.num_parameters(exclude_embeddings=exclude_embeddings)
        
                    
          953. 
                    
        
        
          954. 
                    
        
        
          955. class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMixin):
        
                    
          956.     r"""
        
                    
          957.     Base class for all models.
        
                    
          958. 
                    
        
        
          959.     [`PreTrainedModel`] takes care of storing the configuration of the models and handles methods for loading,
        
                    
          960.     downloading and saving models as well as a few methods common to all models to:
        
                    
          961. 
                    
        
        
          962.         - resize the input embeddings,
        
                    
          963.         - prune heads in the self-attention heads.
        
                    
          964. 
                    
        
        
          965.     Class attributes (overridden by derived classes):
        
                
        
              
            
    [/code]
    
    And to use your own PretrainedConfig alongside of it.

Then @Hosna (Hosna) said

    I have a similar issue where I have my model's (nn.module) weights and I want to convert it to be huggingface compatible model so that I can use hugging face models (as `.generate`). From the discussions I can see that I either have to retrain again while changing (`nn.module` to `PreTrained`) or to define my config.json file. If I wrote my config.json file what should I do next to load my torch model as huggingface one?

After which, @tanyaroosta (Tanya Roosta) added

    I am not sure from the discussion above, what the solution is. Can someone post their working example please?

After which, @thepurpleowl (Surya Prakash Sahu) added

    I am not sure whether this functionality exists at this moment.


{'source': 'discourse', 'url': 'https://discuss.huggingface.co/t/how-to-create-a-config-json-after-saving-a-model/10459', 'title': 'How to create a config.json after saving a model', 'views': 3227, 'has_accepted_answer': True, 'word_count': 474, 'like_count': 4, 'reply_count': 12, 'participant_count': 8, 'posts': 15, 'tags': '', 'author': '@seanbenhur (Sean Benhur)', 'participants': '@seanbenhur (Sean Benhur), @nielsr (Niels Rogge), @BramVanroy (Bram Vanroy), @avinash-chaluvadi (Avinash Chaluvadi), @sophiaaez (Sophia Zell), @Hosna (Hosna), @tanyaroosta (Tanya Roosta), @thepurpleowl (Surya Prakash Sahu)', 'image_url': [None], 'created_at': '2021-10-02T12:02:42.702Z', 'last_posted_at': '2022-09-08T12:59:53.595Z'}