# Exception in save\_pretrained due to recent changes

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexception-in-save-pretrained-due-to-recent-changes%2F93662%3E%5C
**Category:** 🤗Transformers\
**Created:** [June 23, 2024, 8:51pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexception-in-save-pretrained-due-to-recent-changes%2F93662 "2024-06-23T20:51:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![guygurari](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@guygurari](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fguygurari)%5C
**Post date:** [June 23, 2024, 8:51pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexception-in-save-pretrained-due-to-recent-changes%2F93662%2F1 "2024-06-23T20:51:02Z")

</div>

The error I’m getting:

```auto
Traceback (most recent call last):
  File "quantize_fp8.py", line 39, in <module>
    model.save_quantized(quantized_model_dir)
  File "/root/AutoFP8/auto_fp8/modeling.py", line 133, in save_quantized
    save_quantized_model(
  File "/root/AutoFP8/auto_fp8/quantize.py", line 342, in save_quantized_model
    model.save_pretrained(save_dir)
  File "/usr/local/lib/python3.10/dist-packages/transformers/modeling_utils.py", line 2661, in save_pretrained
    shard = {tensor: state_dict[tensor] for tensor in tensors}
  File "/usr/local/lib/python3.10/dist-packages/transformers/modeling_utils.py", line 2661, in <dictcomp>
    shard = {tensor: state_dict[tensor] for tensor in tensors}
NameError: free variable 'state_dict' referenced before assignment in enclosing scope

```

This seems to be due to 2 conflicting changes from the last two weeks, which cause the loop over shards in `save_pretrained` to look like this:

```auto
        # Save the model
        for shard_file, tensors in state_dict_split.filename_to_tensors.items():
            shard = {tensor: state_dict[tensor] for tensor in tensors} # state_dict is used here, but is deleted below
            # remake shard with onloaded parameters if necessary
            if module_map:
                ...     
                state_dict = {name: "" for name in shard} # overwrites state_dict
                ...     
                del state_dict # deletes state_dict

```

Screenshot of where the conflicting changes came from is attached.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/3/4/3427b85f7bf2a376b3bbc210c85165b44bb373d6.png)
