ImportError: cannot import name 'auto_class_factory'
Author: sz-lcw
Pull request: no

I installed layoutlmft from https://github.com/microsoft/unilm/tree/master/layoutlmft
and it import auto_class_factory in the source code of __init__.py:
```
from transformers.models.auto.modeling_auto import auto_class_factory
.....xxx...
AutoModelForTokenClassification = auto_class_factory(
    "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification"
)

AutoModelForRelationExtraction = auto_class_factory(
    "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction"
)

```
After I upgraded the transformers from v4.5.1 to v4.10.0.
And when I run the code as follow:
`from transformers.models.auto.modeling_auto import auto_class_factory`
I got the error below:

```
ImportError: cannot import name 'auto_class_factory' from 'transformers.models.auto.modeling_auto' 
(xxxxx/envs/huggingface/lib/python3.7/site-packages/transformers/models/auto/modeling_auto.py)
```
I couldn't find auto_class_factory in the source code and documents in v4.10.0.
How can I fixed this problem? Thanks.

Comments:
I fixed it by changing `auto_class_factory` to the new function `auto_class_update` and slightly adjusting the relevant code segments in **init.py**:

```
try:
    from transformers.models.auto.modeling_auto import auto_class_factory
except:
    from transformers.models.auto.modeling_auto import _BaseAutoModelClass, auto_class_update
```

```
try:
    AutoModelForTokenClassification = auto_class_factory(
        "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification")
except:
    cls = types.new_class("AutoModelForTokenClassification", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
    cls.__name__ = "AutoModelForTokenClassification"
    
    AutoModelForTokenClassification = auto_class_update(cls, head_doc="token classification")
```

> I fixed it by changing `auto_class_factory` to the new function `auto_class_update` and slightly adjusting the relevant code segments in **init.py**:
> 
> ```
> try:
>     from transformers.models.auto.modeling_auto import auto_class_factory
> except:
>     from transformers.models.auto.modeling_auto import _BaseAutoModelClass, auto_class_update
> ```
> 
> ```
> try:
>     AutoModelForTokenClassification = auto_class_factory(
>         "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification")
> except:
>     cls = types.new_class("AutoModelForTokenClassification", (_BaseAutoModelClass,))
>     cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
>     cls.__name__ = "AutoModelForTokenClassification"
>     
>     AutoModelForTokenClassification = auto_class_update(cls, head_doc="token classification")
> ```

Thanks for your reply!
It seems that the new version(v4.10.0) of transformers don't support AutoModelForRelationExtraction.
It didn't work when I add the code below:
```
    cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
    cls.__name__ = "AutoModelForRelationExtraction"
    AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")
```
The error is :
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "xxxx/unilm/layoutlmft/layoutlmft/__init__.py", line 66, in <module>
    AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")
  File "xxxx/envs/huggingface/lib/python3.7/site-packages/transformers/models/auto/auto_factory.py", line 420, in auto_class_update
    from_config = replace_list_option_in_docstrings(model_mapping._model_mapping, use_model_types=False)(from_config)
AttributeError: 'collections.OrderedDict' object has no attribute '_model_mapping'

```

You will need to do the following instead:

```
class AutoModelForRelationExtraction(_BaseAutoModelClass):
    _model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING


AutoModelForRelationExtraction = auto_class_update(AutoModelForRelationExtraction, head_doc="relation extraction")
```

> You will need to do the following instead:
> 
> ```
> class AutoModelForRelationExtraction(_BaseAutoModelClass):
>     _model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
> 
> 
> AutoModelForRelationExtraction = auto_class_update(AutoModelForRelationExtraction, head_doc="relation extraction")
> ```

Hi @sgugger , I did the instead, but it did fix this problem, the error still exists.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

 @sz-lcw, 
This code is still broken in version 4.12.5 so here is the fix that sgugger meant.  You will need to change the code to be this:
(Thanks to @alromb for doing most of the work and of course sgugger.)

```
....
try:
    from transformers.models.auto.modeling_auto import auto_class_factory
except:
    from transformers.models.auto.modeling_auto import _BaseAutoModelClass, auto_class_update
..
...
...
try:
    AutoModelForTokenClassification = auto_class_factory(
        "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification")
except:
    cls = types.new_class("AutoModelForTokenClassification", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
    cls.__name__ = "AutoModelForTokenClassification"

    AutoModelForTokenClassification = auto_class_update(cls, head_doc="token classification")

try:
    AutoModelForRelationExtraction = auto_class_update(
        "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction")
except:
    cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
    cls.__name__ = "AutoModelForRelationExtraction"

    AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")
```


> try:
>     AutoModelForTokenClassification = auto_class_factory(
>         "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification")
> except:
>     cls = types.new_class("AutoModelForTokenClassification", (_BaseAutoModelClass,))
>     cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
>     cls.__name__ = "AutoModelForTokenClassification"
> 
>     AutoModelForTokenClassification = auto_class_update(cls, head_doc="token classification")
> 
> try:
>     AutoModelForRelationExtraction = auto_class_update(
>         "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction")
> except:
>     cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
>     cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
>     cls.__name__ = "AutoModelForRelationExtraction"
> 
>     AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")

thank you so much. this fixed the problem.

try:
AutoModelForRelationExtraction = auto_class_update(
"AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction")
except:
cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
cls._model_mapping = **MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING**
cls.name = "AutoModelForRelationExtraction"


In relation also you have used a MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING that's the reason it's working.
We have to map it with MODEL_FOR_RELATION_EXTRACTION_MAPPING.

Please  type this 
try:
    AutoModelForRelationExtraction = auto_class_update(
        "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction")
except:
    cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
    cls.__name__ = "AutoModelForRelationExtraction"
    AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")

check error is still there.



Try the following code and see whether it works or not

```python
try:
    AutoModelForRelationExtraction = auto_class_update(
        "AutoModelForRelationExtraction",
        MODEL_FOR_RELATION_EXTRACTION_MAPPING,
        head_doc="relation extraction",
    )
except:

    class StupidProxy:
        _model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING

    cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
    cls._model_mapping = StupidProxy
    cls.__name__ = "AutoModelForRelationExtraction"

    AutoModelForRelationExtraction = auto_class_update(
        cls, head_doc="relation extraction"
    )

```

> Try the following code and see whether it works or not
> 
> ```python
> try:
>     AutoModelForRelationExtraction = auto_class_update(
>         "AutoModelForRelationExtraction",
>         MODEL_FOR_RELATION_EXTRACTION_MAPPING,
>         head_doc="relation extraction",
>     )
> except:
> 
>     class StupidProxy:
>         _model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
> 
>     cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
>     cls._model_mapping = StupidProxy
>     cls.__name__ = "AutoModelForRelationExtraction"
> 
>     AutoModelForRelationExtraction = auto_class_update(
>         cls, head_doc="relation extraction"
>     )
> ```
lib/python3.7/site-packages/transformers/models/auto/auto_factory.py", line 444, in from_pretrained
    elif type(config) in cls._model_mapping.keys():
AttributeError: type object 'StupidProxy' has no attribute 'keys'

this code also occur error


> > Try the following code and see whether it works or not
> > ```python
> > try:
> >     AutoModelForRelationExtraction = auto_class_update(
> >         "AutoModelForRelationExtraction",
> >         MODEL_FOR_RELATION_EXTRACTION_MAPPING,
> >         head_doc="relation extraction",
> >     )
> > except:
> > 
> >     class StupidProxy:
> >         _model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
> > 
> >     cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
> >     cls._model_mapping = StupidProxy
> >     cls.__name__ = "AutoModelForRelationExtraction"
> > 
> >     AutoModelForRelationExtraction = auto_class_update(
> >         cls, head_doc="relation extraction"
> >     )
> > ```
> 
> lib/python3.7/site-packages/transformers/models/auto/auto_factory.py", line 444, in from_pretrained elif type(config) in cls._model_mapping.keys(): AttributeError: type object 'StupidProxy' has no attribute 'keys'
> 
> this code also occur error

Finally I fixed it by modifying the source code.

see the `auto_class_update` function in transformes/models/auto/auto_factory.py https://github.com/huggingface/transformers/blob/d53b8ad78077bbcebf6727fce0280fea26e29c7f/src/transformers/models/auto/auto_factory.py#L606

change
```python
from_config = replace_list_option_in_docstrings(model_mapping._model_mapping, use_model_types=False)(from_config)
```
to
```python
from_config = replace_list_option_in_docstrings(model_mapping, use_model_types=False)(from_config)
```


finally found a solution to this sh-t. just comment all the lines with the error, the reason why this fundamentally does not work is because the original code adapts to layoutlm and layoutlmv2 ONLY.

I made the following modifications, and there are currently no errors in this section (although I still haven’t managed to run layoutlm successfully and am working on resolving other issues):

```python
import types

from transformers.models.auto.modeling_auto import _BaseAutoModelClass, auto_class_update

# AutoModelForTokenClassification = auto_class_factory(
#     "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification"
# )

# AutoModelForRelationExtraction = auto_class_factory(
#     "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction"
# )

try:
    AutoModelForTokenClassification = auto_class_update(
        "AutoModelForTokenClassification", MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, head_doc="token classification"
    )
except:
    cls = types.new_class("AutoModelForTokenClassification", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING
    cls.__name__ = "AutoModelForTokenClassification"
    AutoModelForTokenClassification = auto_class_update(cls, head_doc="token classification")

try:
    AutoModelForRelationExtraction = auto_class_update(
        "AutoModelForRelationExtraction", MODEL_FOR_RELATION_EXTRACTION_MAPPING, head_doc="relation extraction"
    )
except:
    cls = types.new_class("AutoModelForRelationExtraction", (_BaseAutoModelClass,))
    cls._model_mapping = MODEL_FOR_RELATION_EXTRACTION_MAPPING
    cls.__name__ = "AutoModelForRelationExtraction"
    AutoModelForRelationExtraction = auto_class_update(cls, head_doc="relation extraction")
```