Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Export to tflite int8 #9

Open
wants to merge 28 commits into
base: onnx_2_1
Choose a base branch
from
Open

[WIP] Export to tflite int8 #9

wants to merge 28 commits into from

Conversation

kyakuno
Copy link

@kyakuno kyakuno commented Dec 20, 2024

高速化のため、FULLY_INTEGER_QUANTIZATIONのモデルの構築の検討。

Windowsだとedge-ai-torchの依存ライブラリが入らないので、WSLが必要。

 python3 export_image_predictor.py --image_size 512 --framework tflite --accuracy int8

現段階では、下記の変換エラーが発生する。

   output = self._dispatch_impl(func, types, args, kwargs)
  File "/home/kyakuno/.local/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py", line 1757, in _dispatch_impl
    r = func(*args, **kwargs)
  File "/home/kyakuno/.local/lib/python3.10/site-packages/torch/_ops.py", line 667, in __call__
    return self_._op(*args, **kwargs)
  File "/home/kyakuno/.local/lib/python3.10/site-packages/torch/ao/quantization/fx/_decomposed.py", line 81, in quantize_per_tensor_meta
    assert input.dtype == torch.float32, f"Expecting input to have dtype torch.float32, but got dtype: {input.dtype}"
torch._dynamo.exc.TorchRuntimeError: Failed running call_function quantized_decomposed.quantize_per_tensor.default(*(FakeTensor(..., size=(), dtype=torch.int64), 0.007843137718737125, -128, -128, 127, torch.int8), **{}):
Expecting input to have dtype torch.float32, but got dtype: torch.int64

from user code:
   File "<eval_with_key>.12", line 1083, in forward
    quantize_per_tensor_default_261 = torch.ops.quantized_decomposed.quantize_per_tensor.default(_tensor_constant_9, 0.007843137718737125, -128, -128, 127, torch.int8);  _tensor_constant_9 = None

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information

@kyakuno
Copy link
Author

kyakuno commented Dec 20, 2024

エラーの_tensor_constant_9はMul -> div -> Powに繋がっている。
position_encoding.pyでdim_tを計算しているところで、torch.arrangeが定数としてグラフの入力になってしまい、量子化に失敗している。
dim_tを事前計算にすると、変換が行えるようになる。

@kyakuno
Copy link
Author

kyakuno commented Dec 20, 2024

キャリブレーション画像をtruck.jpgとした場合の出力。グラフの精度は足りそう。

int8

int8_output1_hiera_t_2 1_512

float

float_output1_hiera_t_2 1_512

@kyakuno
Copy link
Author

kyakuno commented Dec 20, 2024

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

Image Encoderはレンジのばらつきはあまりないようで、1枚の画像でキャリブレーションしても正常な絵が出力される。

スクリーンショット 2024-12-21 11 51 44

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

推論サンプル
axinc-ai/ailia-models-tflite#95

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

mask decoderは下記のエラーになる。

ValueError: num_scales must be 1 for per-layer quantization, or 256 for per-axis quantization, but got 128.Tensor 201 has invalid quantization parameters.num_scales must be 1 for per-layer quantization, or 256 for per-axis quantization, but got 128.Tensor 217 has invalid quantization parameters.num_scales must be 1 for per-layer quantization, or 256 for per-axis quantization, but got 128.Tensor 270 has invalid quantization parameters.

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

is_per_channel = Falseにした場合も、Convはper channelで量子化される。
FCで新しい規格のis_per_channelを使うかどうかの判定みたい。

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

キャリブレーション用のデータセットを作成する機能を足さないといけない。

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

下記の例では、torchを使用せずに、tensorflowの方で量子化している。
google-ai-edge/ai-edge-torch#345

@kyakuno
Copy link
Author

kyakuno commented Dec 21, 2024

tensorflowで量子化すると、layernormもint8になる。
https://storage.googleapis.com/ailia-models-tflite/segment-anything-2.1/image_encoder_hiera_t_2.1_512.tf_int8.tflite

torch (layernormがfloatになる)
スクリーンショット 2024-12-21 15 41 33

tensorflow (layernormがint8になる)

スクリーンショット 2024-12-21 17 14 49

@kyakuno
Copy link
Author

kyakuno commented Dec 22, 2024

全てのモデルにキャリブレーション機能を追加。とりあえず、COCOデータセットの100枚でキャリブレーションをかける。

@kyakuno
Copy link
Author

kyakuno commented Dec 22, 2024

テスト方法

./download_tflite_models.sh
python3 export_image_predictor.py --framework tflite --accuracy int8 --mode import --image_size 512
python3 export_video_predictor.py --framework tflite --accuracy int8 --mode import --image_size 512

出力

output/*.png

@kyakuno
Copy link
Author

kyakuno commented Dec 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant