Transformers can not load dependency of tensorflow - No module named 'keras.engine'
### System Info

osX silicon M1

- python 3.8.16 (also tested with newer versions e.g. 3.9)
- tensorflow                2.11.0          eigen_py39h384437f_0 
(also tested with tensforflow 2.13 rc0)

tried conda and venv.
- transformers 4.28.1
also tested 4.29.1


```python
#sample code which causes the error below

from transformers import pipeline

summarizer = pipeline("summarization")

```


```
No model was supplied, defaulted to t5-small and revision d769bba (https://huggingface.co/t5-small).
Using a pipeline without specifying a model name and revision in production is not recommended.
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1146, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/models/t5/modeling_tf_t5.py", line 35, in <module>
    from ...modeling_tf_utils import (
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/modeling_tf_utils.py", line 69, in <module>
    from keras.engine import data_adapter
ModuleNotFoundError: No module named 'keras.engine'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "sumary_service.py", line 3, in <module>
    summarizer = pipeline("summarization")
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/pipelines/__init__.py", line 779, in pipeline
    framework, model = infer_framework_load_model(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/pipelines/base.py", line 238, in infer_framework_load_model
    _class = getattr(transformers_module, f"TF{architecture}", None)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1137, in __getattr__
    value = getattr(module, name)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1136, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/opt/homebrew/Caskroom/miniconda/base/envs/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1148, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.models.t5.modeling_tf_t5 because of the following error (look up to see its traceback):
No module named 'keras.engine'
```



### Who can help?
@gante and @Rocketknight1
_No response_

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Reproduction

- follow install example from official docs (https://huggingface.co/docs/transformers/installation)
- run sample code from model page https://huggingface.co/facebook/bart-large-cnn

`**error does not occur when using pytorch**`

### Expected behavior

transformer library does not raise exception
Hi @dcdieci, this issue is the result of some namespace moves inside TensorFlow which occurred because Keras was partly decoupled from TensorFlow and moved to its own repository. If you look at [our codebase](https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_tf_utils.py#L68-L77), you can see that we import these functions from `keras` for TF versions >= 2.11 and from `tensorflow.python.keras` below this. It seems like in your case you're using a newer version of TensorFlow, but you're missing a modern version of the standalone Keras library (which should be installed as a dependency of TensorFlow). Can you try `pip install --upgrade keras` to see if that resolves the issue?<|||||>@Rocketknight1 many thanks for getting back to me that fast (considering the amount of issues, unbelievable).
I changed to pytorch and everything is working fine. I am very new to the AI space and I was wondering if it does make sense to use tensorflow then at all, since pytorch is running?
<|||||>Both PyTorch and TensorFlow do basically the same thing (they're a framework for linear algebra + acceleration on GPU and TPU) - we support both, but you only need one! If PyTorch is easier to get working on your system then it's totally fine to just use it instead.<|||||>I ran into this too. I can't speak to the specific issue, but it's related to the latest pre-release of tensorflow (https://github.com/tensorflow/tensorflow/releases/tag/v2.13.0-rc0), which is installed when you do a `pip install tensorflow` (odd that a pre-release get's installed, but that's another story). There is some keras related breaking changes in that release. In any case, I was able to get around this by building+installing tensorflow 2.12.0 from source.<|||||>Thanks for the heads-up - we'll do some testing with TF 2.13 before it releases!<|||||>There are upcoming breaking changes to keras. Please see
https://github.com/keras-team/keras/issues/18141
Also see the release notes here https://github.com/tensorflow/tensorflow/releases/tag/v2.13.0-rc0 particularly the part in 'Breaking Changes' that talks about restricting access so that only public symbols are accessible.
This will need updates to transformers to resolve I think.<|||||>I've opened a PR at #23663 that should cover this issue as well as future-proof against other changes. My limited testing with 2.13rc0 on my local machine looked good, but if you get the chance please try it out with `pip install --upgrade git+https://github.com/huggingface/transformers.git@tf_future_proofing`

cc @dcdieci @elfringham @sanderpick<|||||>This has now been merged - if anyone else is having compatibility issues with `transformers` and TensorFlow 2.13 and finds this issue, please install transformers from `main` with `pip install --upgrade git+https://github.com/huggingface/transformers.git`. Once we release 2.30 (probably end of May / early June) you can go back to just `pip install --upgrade transformers`.

If anyone is still encountering this problem after installing the latest version, please reply or reopen this issue and let us know!<|||||>Hello! I think I'm still running into this issue. 
* python 3.8.16
* tensorflow 2.13.0rc1
* transformers from main

I'm on an M2 rather than an M1, and maybe I should try downgrading TF to 2.11? _Edit: After looking into this, I don't think I can actually downgrade, so I'm stuck on the current tensorflow. I was able to get code running with pytorch though!_<|||||>Hi! This regression was caused by a PR we merged yesterday and should be fixed as of about an hour ago. Please install the latest version from `main` and try again. Thanks again to @frostming for spotting that one so quickly!<|||||>We will also be making a proper release of version 4.30 later this week that should correctly support TF 2.13, so hopefully after that everyone can just `pip install --upgrade transformers` and stop installing from `main`.<|||||>Thank you so much! <|||||>After re-running the installation (from commit 12298cb65c7e9d615b749dde935a0b4966f4ae49) it still fails on my end, but github also seems to be having problems, so maybe that commit is behind.<|||||>@coolhannes Can you paste the error message you're getting?<|||||>Yep! Sorry. (Also you may see some references to pytorch, I switched to that in the meantime but this is the error I get from TF).

Running
```
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
```

and getting: 

```
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/utils/import_utils.py:1084, in _LazyModule._get_module(self, module_name)
   1083 try:
-> 1084     return importlib.import_module("." + module_name, self.__name__)
   1085 except Exception as e:

File ~/.pyenv/versions/3.8.16/lib/python3.8/importlib/__init__.py:127, in import_module(name, package)
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1014, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:991, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:975, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:671, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:843, in exec_module(self, module)

File <frozen importlib._bootstrap>:219, in _call_with_frames_removed(f, *args, **kwds)

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/distilbert/modeling_tf_distilbert.py:37
     29 from ...modeling_tf_outputs import (
     30     TFBaseModelOutput,
     31     TFMaskedLMOutput,
   (...)
     35     TFTokenClassifierOutput,
     36 )
---> 37 from ...modeling_tf_utils import (
     38     TFMaskedLanguageModelingLoss,
     39     TFModelInputType,
     40     TFMultipleChoiceLoss,
     41     TFPreTrainedModel,
     42     TFQuestionAnsweringLoss,
     43     TFSequenceClassificationLoss,
     44     TFTokenClassificationLoss,
     45     get_initializer,
     46     keras_serializable,
     47     unpack_inputs,
     48 )
     49 from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/modeling_tf_utils.py:77
     76     from keras.__internal__ import KerasTensor
---> 77     from keras.engine.base_layer_utils import call_context
     78 elif parse(tf.__version__).minor >= 11:

ModuleNotFoundError: No module named 'keras.engine'

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[4], line 7
      4 from torch.nn.parallel import DataParallel
      6 model_name = "distilbert-base-uncased-finetuned-sst-2-english"
----> 7 model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
      8 tokenizer = AutoTokenizer.from_pretrained(model_name)

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:483, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    479     return model_class.from_pretrained(
    480         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    481     )
    482 elif type(config) in cls._model_mapping.keys():
--> 483     model_class = _get_model_class(config, cls._model_mapping)
    484     return model_class.from_pretrained(
    485         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    486     )
    487 raise ValueError(
    488     f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
    489     f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping.keys())}."
    490 )

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:375, in _get_model_class(config, model_mapping)
    374 def _get_model_class(config, model_mapping):
--> 375     supported_models = model_mapping[type(config)]
    376     if not isinstance(supported_models, (list, tuple)):
    377         return supported_models

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:657, in _LazyAutoMapping.__getitem__(self, key)
    655 if model_type in self._model_mapping:
    656     model_name = self._model_mapping[model_type]
--> 657     return self._load_attr_from_module(model_type, model_name)
    659 # Maybe there was several model types associated with this config.
    660 model_types = [k for k, v in self._config_mapping.items() if v == key.__name__]

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:671, in _LazyAutoMapping._load_attr_from_module(self, model_type, attr)
    669 if module_name not in self._modules:
    670     self._modules[module_name] = importlib.import_module(f".{module_name}", "transformers.models")
--> 671 return getattribute_from_module(self._modules[module_name], attr)

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:616, in getattribute_from_module(module, attr)
    614 if isinstance(attr, tuple):
    615     return tuple(getattribute_from_module(module, a) for a in attr)
--> 616 if hasattr(module, attr):
    617     return getattr(module, attr)
    618 # Some of the mappings have entries model_type -> object of another model type. In that case we try to grab the
    619 # object at the top level.

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/utils/import_utils.py:1074, in _LazyModule.__getattr__(self, name)
   1072     value = self._get_module(name)
   1073 elif name in self._class_to_module.keys():
-> 1074     module = self._get_module(self._class_to_module[name])
   1075     value = getattr(module, name)
   1076 else:

File ~/.pyenv/versions/3.8.16/envs/opt-outs/lib/python3.8/site-packages/transformers/utils/import_utils.py:1086, in _LazyModule._get_module(self, module_name)
   1084     return importlib.import_module("." + module_name, self.__name__)
   1085 except Exception as e:
-> 1086     raise RuntimeError(
   1087         f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
   1088         f" traceback):\n{e}"
   1089     ) from e

RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
No module named 'keras.engine'
```<|||||>Hi @coolhannes, that error refers to code from an older commit - it looks like you might not have upgraded to the most recent version on `main`! Try `pip install --upgrade https://github.com/huggingface/transformers.git` and the error should go away.<|||||>Oh my god, I think I was installing this instead of uninstalling/upgrading -- but this is finally working, thank you @Rocketknight1!