Quantization produces large scale coffiecient, which pervents the model from being loaded 
I was able to reproduce this issue. Please find this [gist](https://colab.research.google.com/gist/pjpratik/f71dec9bd1f702c1a62fb8505b7d4a50/62196.ipynb). Seems to be issue while converting pytorch->onnx->tf->tflite.

@pkgoogle Could you please check this?

Thanks.

 Thanks for verifying, @pjpratik and thanks for checking @pkgoogle .  I was able to reproduce w/ the same gist as above. @abattery, Can you please take a look? Thanks. If you accept a PR and might review it @abattery, I can also look into this again. Would be nice to identify the source of this bug and also to do a PR for tf.  Hi @FabianSchuetze, we will review any PR which may come in. For this issue, I'm not sure where the root issue is yet but I can give you some guesses or things to look at. At a high level you should probably start with this README: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/README.md and then probably review the passes: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/tf_tfl_passes.cc. We use [MLIR](https://mlir.llvm.org/) to convert arbitrary compute graphs. Hope that at least gets you started ... Ok, I've started working on this and your comments were very helpful indeed, @pkgoogle  - thanks.  Hi @pkgoogle 

Thanks for your help. I've been able to reproduce the quantization problem with the `tfl_quantizer` binary. The binary loads an annotated model and returns the quantized output (`bazel-bin/tensorflow/compiler/mlir/lite/quantization/lite/tfl_quantizer --debug ANNOTATED.TFLITE  > QUANTIZED.TFLITE 2>DEBUG`). The DEBUG file is extremely large (33GB) because it contains long hex numbers regularly, such as:
```
%cst_36 = arith.constant dense<"0xB3C0153CF426D73B24C8233CE29033BB4F10F33C42F6A53C....
```
If I remove these hex numbers, the DEBUG file is manageable. It contains a few suspicious values with seemingly erroneous quantization parameters, such as:
```
tfl.padv2 : (f32,i32,f32,) -> (!quant.uniform<i8:f32, 1.3344405750530544E+36:127>,)
```
but I'm not sure whether this is relevant and how to debug it. Otherwise, I also jumped into the `quantization_driver.cc` functions but could not identify anything relevant. Do you have any suggestions for narrowing the search or creating good debug output? (NB: I have compiled tensorflow/compiler/mlir/lite/quantization/lite:tfl_quantizer in debug modus with the llvm supplied by tensorflow.) Hi @FabianSchuetze I would try to use lldb/gdb with that binary (and build with debug info) and see if you can break down the conversion step by step (and try to figure out before/after which pass does that weird scaling take root). It might be worth it to see if you can reduce down your model to as few as ops as possible that will still show the issue.

Have you understood the Operation, Region, Block abstractions of MLIR? It's probably worth it to understand that abstraction first: https://www.youtube.com/watch?v=Y4SvqTtOIDk

Then you can start with the operation which represents your model, dump it https://mlir.llvm.org/doxygen/classmlir_1_1Operation.html (Op->dump();) within lldb/gdb, after every conversion redump it when possible etc. That should help you drill down a bit.

Probably not useful immediately but you may also want to review some other useful MLIR binaries:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/BUILD
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/BUILD

(search for tf_cc_binary's in those files)

I'm not an expert in those but they might be able to help in ways I can't see right now. Hope that helps.

  Thanks for your comment, @pkgoogle . I made a bit of progress and think I'm on a good way.  Hi @pkgoogle . Just a brief update: 

The segfault can be prevented by ensuring the scale coefficient is smaller than one. The scale coefficient can be restricted in [FakeQuantSupport.cc](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/quantization/ir/FakeQuantSupport.cc#L148) or, in case that the legacy float scale is enabled, in [DownCastScale](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/quantization/quantization_utils.cc#L675). 

I will now try to identify an appropriate location for interrupting the quantization process. During the quantization, several checks to ensure that the quantization parameters are within an admissible range are called. These checks might be an opportunity to interrupt the transformation.

Thanks also for your support regarding MLIR. It was a pleasure to work with it so far.  Hi @FabianSchuetze, Thanks for your help! That's really good progress, np, feel free to ask any more questions -- I don't know everything but I will try my best to help. MLIR has a huge activation energy so to speak but it's a pleasure once you get used to it, so it's great to see you were able to make progress. @pkgoogle : Thanks again for your help. I created a PR with a proposed solution. Maybe we can continue the discussion [there](https://github.com/tensorflow/tensorflow/pull/62605)
### 1. System information
Colab , as of 2023-10-23


### 2. Code
Please see the attached colab notebook  here
https://colab.research.google.com/drive/1yUD0nDu8oeeDtQBa7xCbQWx_w8PxS4UC?usp=sharing
to  reproduce the issue. It loads a pre-trained resnet18 from pytorch, converts  it to onnx, converts it to tensorflow, and then exports it to tf-lite.  ( The process is a bit convoluted, but I need a pretrained resnet18, and didn't find it in the tensorflow orbit so I used torchvision, hope that's ok.)

If you download the generated model (model_int8.tflite) and open it in netron.app and click on the first MaxPool2D op, you can see that the quantization scale is ` 1.3344405750530544e+36 `. See the attached image. 

![image](https://github.com/tensorflow/tensorflow/assets/10357496/60968bdd-ee50-48fd-9802-9b498ac619e4)

This scale parameter itself is of course implausible (impossible), but loading the model also produces an error here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/kernels/internal/quantization_util.cc#L117 


Does anybody know why the quantization parameter is that high, and what can be done to fix it? Furthermore, can I let the quantization fails explicitly when it generates such high values? 
