Allow for try/except imports for custom code
### Feature request

When uploading a model with custom code, I wanted to try and use Flash Attention in one of the modules. However, to get around a case where people might use it on CPU, I added `try/except` block for the import. 

However, I get an error when downloading locally like `This modeling file requires the following packages that were not found in your environment` which seems to come from [this file](https://github.com/huggingface/transformers/blob/main/src/transformers/dynamic_module_utils.py#L112). 

### Motivation

I want to be able to write custom model code that allows for optional imports if possible (like FlashAttention)

### Your contribution

I could try a crack at a PR?
Indeed that sounds like a nice feature to have. I think the easiest way to deal with it would be to remove all try/except blocks from the content [here](https://github.com/huggingface/transformers/blob/37e0974afcbccdc85da59d51b44e1437b6b3caea/src/transformers/dynamic_module_utils.py#L117) before the tests of the imports. If you want to take a stab at it, happy to review a PR!<|||||>Yes I'll give it a go and send a PR out in a few!