How to create a config.json after saving a model
Hi, I am trying to convert my model to onnx format with the help of thisnotebookI got error , since config.json does not exist.My model is a custom model with extra layers, similar tothis,Now how can I create a config.json file for this?
2021-10-04T09:18:11Z
Normally, if you save your model using the.save_pretrained()method, it will save both the model weights and aconfig.jsonfile in the specified directory.
2021-10-04T09:46:33Z
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?
2021-10-04T09:51:28Z
You need to subclass it to have the save_pretrained methods available. So instead ofclass Mean_Pooling_Model(nn.Module):usefrom transformers.modeling_utils import PreTrainedModel
class Mean_Pooling_Model(PreTrainedModel):It will add extra functionality on top ofnn.Module.github.comhuggingface/transformers/blob/bcc3f7b6560c1ed427f051107c7755956a27a9f2/src/transformers/modeling_utils.py#L415exclude_embeddings (:obj:`bool`, `optional`, defaults to :obj:`True`):Whether or not to count embedding and softmax operations.Returns::obj:`int`: The number of floating-point operations."""return 6 * self.estimate_tokens(input_dict) * self.num_parameters(exclude_embeddings=exclude_embeddings)class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMixin):r"""Base class for all models.:class:`~transformers.PreTrainedModel` takes care of storing the configuration of the models and handles methodsfor loading, downloading and saving models as well as a few methods common to all models to:* resize the input embeddings,* prune heads in the self-attention heads.Class attributes (overridden by derived classes):
2021-10-04T09:55:44Z
Thank you I will try this!
2021-10-06T09:00:01Z
Is it possible to generate the configuration file for already trained model , i.e weights stored in normal pytorch model.bin
2022-02-18T11:43:38Z
Use model.config.to_json() method to generate config.json
2022-04-19T11:47:57Z
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
2022-04-19T12:35:30Z
Nope, I was not able to find a proper solution, I ended up writing the config.json manually
2022-04-28T15:35:48Z
You should be able to just callmodel.config.to_json_file("config.json")github.comhuggingface/transformers/blob/1be8d56ec6f7113810adc716255d371e78e8a1af/src/transformers/configuration_utils.py#L808Returns:`str`: String containing all the attributes that make up this configuration instance in JSON format."""if use_diff is True:config_dict = self.to_diff_dict()else:config_dict = self.to_dict()return json.dumps(config_dict, indent=2, sort_keys=True) + "\n"def to_json_file(self, json_file_path: Union[str, os.PathLike], use_diff: bool = True):"""Save this instance to a JSON file.Args:json_file_path (`str` or `os.PathLike`):Path to the JSON file in which this configuration instance's parameters will be saved.use_diff (`bool`, *optional*, defaults to `True`):If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`is serialized to JSON file."""cc@seanbenhur
2022-06-16T07:44:36Z
That only works for models that are transformer native and not nn.Module/pytorch native, sadly.
2022-06-16T09:03:52Z
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 PretrainedModelgithub.comhuggingface/transformers/blob/3981ee8650042e89d9c430ec34def2d58a2a12f7/src/transformers/modeling_utils.py#L955exclude_embeddings (`bool`, *optional*, defaults to `True`):Whether or not to count embedding and softmax operations.Returns:`int`: The number of floating-point operations."""return 6 * self.estimate_tokens(input_dict) * self.num_parameters(exclude_embeddings=exclude_embeddings)class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMixin):r"""Base class for all models.[`PreTrainedModel`] takes care of storing the configuration of the models and handles methods for loading,downloading and saving models as well as a few methods common to all models to:- resize the input embeddings,- prune heads in the self-attention heads.Class attributes (overridden by derived classes):And to use your own PretrainedConfig alongside of it.
2022-07-27T10:53:21Z
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.moduletoPreTrained) 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?
2022-08-04T03:25:30Z
I am not sure from the discussion above, what the solution is.  Can someone post their working example please?
2022-09-08T12:59:53Z
I am not sure whether this functionality exists at this moment.
2022-11-14T14:15:49Z
Folks,I am trying out the code atGitHub - aws-samples/aws-inferentia-huggingface-workshop: CMP314 Optimizing NLP models with Amazon EC2 Inf1 instances in Amazon Sagemaker.For inferentia instance, I get a config.json not found in the cloudwatch logs and inference fails. The config.json file is present in the traced model tar.gz file for inferentia.Please help me resolve this.ThanksAjayP.S the log message — W-9002-model_1-stdout MODEL_LOG - OSError: file /home/model-server/tmp/models/cb9491669c1f44c1a0763e8a62d9368e/config.json not found
2023-08-30T19:25:19Z
Was anyone able to resolve this issue ,i.e., converting a custom nn.Module to a huggingface compatible version?
2023-08-30T20:21:05Z
Did you find a solution/workaround for this issue?
2023-08-31T21:06:26Z
@HosnaYou can push the config via : customModel.pretrained_model.config.push_to_hub(repo_id)
2023-09-15T15:08:24Z
SO this worked for me , iimportedfrom transformers.modeling_utils import PreTrainedModel ,PretrainedConfigand then in my classclass TransformerLanguageModel(PreTrainedModel):
    def __init__(self, config):
        super(TransformerLanguageModel, self).__init__(config)
        self.token_embedding_table = nn.Embedding(config.vocab_size, config.hidden_size)
        self.position_embedding_table = nn.Embedding(config.block_size, config.hidden_size)
        self.transformer = nn.Transformer(
            d_model=config.hidden_size,
            nhead=config.num_attention_heads,
            num_encoder_layers=config.num_hidden_layers,
            num_decoder_layers=config.num_hidden_layers,
            dim_feedforward=4 * config.hidden_size,
            dropout=config.hidden_dropout_prob,
            activation='gelu'
        )
        self.ln1 = nn.LayerNorm(config.hidden_size)
        self.ln2 = nn.LayerNorm(config.hidden_size)
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size)after that you have to create a config variableCreate a configuration objectconfig = PretrainedConfig(
    vocab_size=1000,  # Specify your vocabulary size
    hidden_size=n_embd,  # Use your embedding dimension
    num_attention_heads=n_head,
    num_hidden_layers=n_layer,
    hidden_dropout_prob=dropout,
    block_size=block_size
)model = TransformerLanguageModel(config)
model.to(device)


now you can save the model 
model.save_pretrained('./path_to_model/')
No date available
No reply text available