-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RNN/LSTM/GRU support for ONNX QuantSim (#2685)
Signed-off-by: Jokay Su <quic_chenzhen@quicinc.com>
- Loading branch information
1 parent
6263b42
commit 56b36f4
Showing
6 changed files
with
367 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- mode: python -*- | ||
# ============================================================================= | ||
# @@-COPYRIGHT-START-@@ | ||
# | ||
# Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# 3. Neither the name of the copyright holder nor the names of its contributors | ||
# may be used to endorse or promote products derived from this software | ||
# without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# | ||
# @@-COPYRIGHT-END-@@ | ||
# ============================================================================= | ||
import os | ||
|
||
import numpy as np | ||
import pytest | ||
import torch | ||
from onnx import load_model | ||
from torchaudio import models | ||
|
||
from aimet_onnx.utils import make_dummy_input | ||
from aimet_common.defs import QuantScheme, QuantizationDataType | ||
from aimet_onnx.quantsim import QuantizationSimModel | ||
from aimet_common.quantsim_config.utils import get_path_for_per_channel_config | ||
from torch_utils import get_librispeech_data_loaders, train_librispeech | ||
|
||
WORKING_DIR = '/tmp/quantsim' | ||
|
||
batch_size = 64 | ||
n_feature = 128 | ||
n_class = 29 | ||
|
||
|
||
def model_eval_onnx(session, val_loader, max_batches): | ||
""" | ||
:param model: model to be evaluated | ||
:param val_loader: dataloader for validation data | ||
:return: CTC Loss on validation data | ||
""" | ||
|
||
test_loss = 0 | ||
for (i, batch) in enumerate(val_loader): | ||
spectrograms, labels, input_lengths, label_lengths = batch | ||
x = spectrograms.numpy() | ||
|
||
in_tensor = {'input': x} | ||
out = session.run(None, in_tensor)[0] | ||
|
||
out = torch.Tensor(out).transpose(0, 1) | ||
criterion = torch.nn.CTCLoss(blank=28) | ||
loss = criterion(out, labels, input_lengths, label_lengths) | ||
test_loss += loss.item() / len(val_loader) | ||
|
||
if i+1 >= max_batches: | ||
break | ||
|
||
print(f'Test loss: {test_loss}') | ||
return test_loss | ||
|
||
|
||
class TestQuantizeAcceptance: | ||
""" Acceptance test for AIMET ONNX """ | ||
@pytest.mark.parametrize("config_file", [None, get_path_for_per_channel_config()]) | ||
@pytest.mark.cuda | ||
def test_quantized_accuracy(self, config_file): | ||
if not os.path.exists(WORKING_DIR): | ||
os.makedirs(WORKING_DIR) | ||
np.random.seed(0) | ||
torch.manual_seed(0) | ||
model = models.DeepSpeech(n_feature=n_feature, n_class=n_class) | ||
if torch.cuda.is_available(): | ||
device = torch.device('cuda:0') | ||
model.to(device) | ||
|
||
train_librispeech(model, 1, max_batches=30) | ||
|
||
train_loader, val_loader = get_librispeech_data_loaders(batch_size=batch_size, drop_last=False) | ||
|
||
torch.onnx.export(model, torch.rand(1, 1, 1, 128).cuda(), os.path.join(WORKING_DIR, 'deepspeech.onnx'), | ||
training=torch.onnx.TrainingMode.PRESERVE, | ||
input_names=['input'], output_names=['output'], | ||
dynamic_axes={ | ||
'input': {0: 'batch_size', 2: 'time'}, | ||
'output': {0: 'batch_size', 1: 'time'}, | ||
} | ||
) | ||
|
||
onnx_model = load_model(os.path.join(WORKING_DIR, 'deepspeech.onnx')) | ||
dummy_input = make_dummy_input(onnx_model) | ||
sim = QuantizationSimModel(onnx_model, dummy_input, quant_scheme=QuantScheme.post_training_tf, default_param_bw=8, | ||
default_activation_bw=8, use_cuda=True, config_file=config_file) | ||
|
||
def onnx_callback(session, iters): | ||
for i, batch in enumerate(train_loader): | ||
x = batch[0].detach().cpu().numpy() | ||
in_tensor = {'input': x} | ||
session.run(None, in_tensor) | ||
print(i, '/', iters) | ||
if i+1 >= iters: | ||
break | ||
|
||
sim.compute_encodings(onnx_callback, 1) | ||
|
||
onnx_qs_test_loss = model_eval_onnx(sim.session, val_loader, max_batches=1) | ||
|
||
assert onnx_qs_test_loss < 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.