# How to create a config.json after saving a model

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%3E%5C
**Category:** 🤗Transformers\
**Created:** [October 2, 2021, 12:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459 "2021-10-02T12:02:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanbenhur](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/seanbenhur/32/20411_2.png) [@seanbenhur](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fseanbenhur)%5C
**Post date:** [October 2, 2021, 12:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F1 "2021-10-02T12:02:42Z")

</div>

Hi, I am trying to convert my model to onnx format with the help of this [notebook](https://nbviewer.jupyter.org/github/huggingface/notebooks/blob/master/examples/onnx-export.ipynb)  
I got error , since config.json does not exist.  
My model is a custom model with extra layers, similar to [this,](https://github.com/seanbenhur/resusable_text_classification_template/blob/main/src/models/transformer_mean_pooler.py)

Now how can I create a config.json file for this?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [October 4, 2021, 9:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F2 "2021-10-04T09:18:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seanbenhur](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/seanbenhur/32/20411_2.png) [@seanbenhur](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fseanbenhur)%5C
**Post date:** [October 4, 2021, 9:46am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F3 "2021-10-04T09:46:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBramVanroy)%5C
**Post date:** [October 4, 2021, 9:51am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F4 "2021-10-04T09:51:28Z")

</div>

You need to subclass it to have the save\_pretrained methods available. So instead of

```auto
class Mean_Pooling_Model(nn.Module):

```

use

```auto
from transformers.modeling_utils import PreTrainedModel
class Mean_Pooling_Model(PreTrainedModel):

```

It will add extra functionality on top of `nn.Module`.

> <https://github.com/huggingface/transformers/blob/bcc3f7b6560c1ed427f051107c7755956a27a9f2/src/transformers/modeling_utils.py#L415>

---

<div class="post-metadata">

**Author:** ![seanbenhur](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/seanbenhur/32/20411_2.png) [@seanbenhur](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fseanbenhur)%5C
**Post date:** [October 4, 2021, 9:55am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F5 "2021-10-04T09:55:44Z")

</div>

Thank you I will try this!

---

<div class="post-metadata">

**Author:** ![seanbenhur](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/seanbenhur/32/20411_2.png) [@seanbenhur](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fseanbenhur)%5C
**Post date:** [October 6, 2021, 9:00am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F6 "2021-10-06T09:00:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avinash-chaluvadi](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@avinash-chaluvadi](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Favinash-chaluvadi)%5C
**Post date:** [February 18, 2022, 11:43am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F7 "2022-02-18T11:43:38Z")

</div>

Use model.config.to\_json() method to generate config.json

---

<div class="post-metadata">

**Author:** ![sophiaaez](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sophiaaez/32/4526_2.png) [@sophiaaez](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsophiaaez)%5C
**Post date:** [April 19, 2022, 11:47am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F9 "2022-04-19T11:47:57Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![seanbenhur](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/seanbenhur/32/20411_2.png) [@seanbenhur](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fseanbenhur)%5C
**Post date:** [April 19, 2022, 12:35pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F10 "2022-04-19T12:35:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBramVanroy)%5C
**Post date:** [April 28, 2022, 3:35pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F11 "2022-04-28T15:35:48Z")

</div>

You should be able to just call

```python
model.config.to_json_file("config.json")

```

> <https://github.com/huggingface/transformers/blob/1be8d56ec6f7113810adc716255d371e78e8a1af/src/transformers/configuration_utils.py#L808>

cc @seanbenhur

---

<div class="post-metadata">

**Author:** ![sophiaaez](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sophiaaez/32/4526_2.png) [@sophiaaez](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsophiaaez)%5C
**Post date:** [June 16, 2022, 7:44am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F12 "2022-06-16T07:44:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBramVanroy)%5C
**Post date:** [June 16, 2022, 9:03am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F13 "2022-06-16T09:03:52Z")

</div>

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

> <https://github.com/huggingface/transformers/blob/3981ee8650042e89d9c430ec34def2d58a2a12f7/src/transformers/modeling_utils.py#L955>

And to use your own PretrainedConfig alongside of it.

---

<div class="post-metadata">

**Author:** ![Hosna](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hosna/32/5510_2.png) [@Hosna](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FHosna)%5C
**Post date:** [July 27, 2022, 10:53am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F14 "2022-07-27T10:53:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tanyaroosta](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tanyaroosta](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftanyaroosta)%5C
**Post date:** [August 4, 2022, 3:25am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F15 "2022-08-04T03:25:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thepurpleowl](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@thepurpleowl](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fthepurpleowl)%5C
**Post date:** [September 8, 2022, 12:59pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F16 "2022-09-08T12:59:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nairajay2k](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@nairajay2k](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnairajay2k)%5C
**Post date:** [November 14, 2022, 2:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F17 "2022-11-14T14:15:49Z")

</div>

Folks,  
I am trying out the code at [GitHub - aws-samples/aws-inferentia-huggingface-workshop: CMP314 Optimizing NLP models with Amazon EC2 Inf1 instances in Amazon Sagemaker](https://github.com/aws-samples/aws-inferentia-huggingface-workshop) .

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.  
Thanks  
Ajay

P.S the log message — W-9002-model\_1-stdout MODEL\_LOG - OSError: file /home/model-server/tmp/models/cb9491669c1f44c1a0763e8a62d9368e/config.json not found

---

<div class="post-metadata">

**Author:** ![Kason123](https://avatars.discourse-cdn.com/v4/letter/k/54ee81/32.png) [@Kason123](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKason123)%5C
**Post date:** [August 30, 2023, 7:25pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F18 "2023-08-30T19:25:19Z")

</div>

Was anyone able to resolve this issue ,i.e., converting a custom nn.Module to a huggingface compatible version?

---

<div class="post-metadata">

**Author:** ![Kason123](https://avatars.discourse-cdn.com/v4/letter/k/54ee81/32.png) [@Kason123](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKason123)%5C
**Post date:** [August 30, 2023, 8:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F19 "2023-08-30T20:21:05Z")

</div>

Did you find a solution/workaround for this issue?

---

<div class="post-metadata">

**Author:** ![abdouaziiz](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/abdouaziiz/32/45758_2.png) [@abdouaziiz](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fabdouaziiz)%5C
**Post date:** [August 31, 2023, 9:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F20 "2023-08-31T21:06:26Z")

</div>

@Hosna You can push the config via : customModel.pretrained\_model.config.push\_to\_hub(repo\_id)

---

<div class="post-metadata">

**Author:** ![aframson](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/aframson/32/19531_2.png) [@aframson](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Faframson)%5C
**Post date:** [September 15, 2023, 3:08pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459%2F21 "2023-09-15T15:08:24Z")

</div>

SO this worked for me , i

# imported

`from transformers.modeling_utils import PreTrainedModel ,PretrainedConfig`

and then in my class

```auto
class 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 variable

# Create a configuration object

```auto
config = 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
)

```

```auto

model = TransformerLanguageModel(config)
model.to(device)

now you can save the model 
model.save_pretrained('./path_to_model/')

```

[Next page](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-create-a-config-json-after-saving-a-model%2F10459.md%3Fpage%3D2)
