pytorch に付属の量子化方法を使用します。
動的量子化方法は畳み込み層を量子化できません。コードはエラーを報告しませんが、量子化操作は実行しません。
quantized_model = torch.quantization.quantize_dynamic(model, {nn.Conv2d}, dtype=torch.qint8)
次のレイヤーのみクオンタイズできます
if dtype == torch.qint8:
qconfig_spec = {
nn.Linear : default_dynamic_qconfig,
nn.LSTM : default_dynamic_qconfig,
nn.GRU : default_dynamic_qconfig,
nn.LSTMCell : default_dynamic_qconfig,
nn.RNNCell : default_dynamic_qconfig,
nn.GRUCell : default_dynamic_qconfig,
}
モデルの変換ステップに静的量子化法を採用
model_int8 = torch.quantization.convert(model_fused_prepared)
深さ方向に分離可能な畳み込みモデルは次のように定義されるため、
def conv_dw(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(inp, oup, 1, 1, 0, bias=True),
nn.ReLU(inplace=True),
)
エラーを報告すると、深い畳み込み層のグループは 3 であるため、これは機能しません。
RuntimeError: Quantized cudnn conv2d is currenty limited to groups = 1; received groups =3