Skip to content

Commit

Permalink
[hackathon 7th] PaddleSpeech line: fix when shape=() can not load (Pa…
Browse files Browse the repository at this point in the history
  • Loading branch information
enkilee authored Jan 3, 2025
1 parent 8957f22 commit 126d3c6
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python/paddle/static/quantization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def load_variable_data(scope, var_name):
'''
var_node = scope.find_var(var_name)
assert var_node is not None, "Cannot find " + var_name + " in scope."
return np.array(var_node.get_tensor())
tensor = np.array(var_node.get_tensor())
if tensor.shape == ():
return tensor.reshape(1)
else:
return tensor


def set_variable_data(scope, place, var_name, np_value):
Expand Down
146 changes: 146 additions & 0 deletions test/quantization/test_post_training_quantization_mobilenetv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,152 @@ def test_post_training_avg_mobilenetv1(self):
)


class TestPostTrainingavgForPwganCsmsc(TestPostTrainingQuantization):
def setUp(self):
self.int8_download = 'int8/download'
self.cache_folder = os.path.expanduser(
'~/.cache/paddle/dataset/' + self.int8_download
)
self.data_cache_folder = ''
data_urls = []
data_md5s = []
if os.environ.get('DATASET') == 'full':
data_urls.append(
'https://paddlespeech.bj.bcebos.com/tmp/csmsc_voc1.npy'
)
data_md5s.append('47950146167ca8d885a78d71e74f1a2b')
self.data_cache_folder = self.download_data(
data_urls, data_md5s, "full_data", False
)
else:
data_urls.append(
'https://paddlespeech.bj.bcebos.com/tmp/csmsc_voc1.npy'
)
data_md5s.append('47950146167ca8d885a78d71e74f1a2b')
self.data_cache_folder = self.download_data(
data_urls, data_md5s, "small_data", False
)

# reader/decorator.py requires the relative path to the data folder
if not os.path.exists("./data/BZNSYP"):
cmd = 'rm -rf {0} && ln -s {1} {0}'.format(
"data", self.data_cache_folder
)
os.system(cmd)

self.batch_size = 1 if os.environ.get('DATASET') == 'full' else 50
self.infer_iterations = (
50000 if os.environ.get('DATASET') == 'full' else 2
)

self.root_path = tempfile.TemporaryDirectory()
self.int8_model = os.path.join(
self.root_path.name, "post_training_quantization"
)

def cache_unzipping(self, target_folder, zip_path):
if not os.path.exists(target_folder):
if zip_path.endswith('.tar.gz'):
cmd = f'mkdir {target_folder} && tar xf {zip_path} -C {target_folder}'
elif zip_path.endswith('.zip'):
cmd = f'mkdir {target_folder} && unzip -o {zip_path} -d {target_folder}'
else:
cmd = f'mkdir {target_folder}'
os.system(cmd)

def generate_quantized_model(
self,
model_path,
model_filename,
params_filename,
quantizable_op_type,
batch_size,
algo="avg",
round_type="round",
is_full_quantize=False,
is_use_cache_file=False,
is_optimize_model=False,
batch_nums=1,
onnx_format=False,
deploy_backend=None,
feed_name="inputs",
):
try:
os.system("mkdir " + self.int8_model)
except Exception as e:
_logger.info(f"Failed to create {self.int8_model} due to {e}")
sys.exit(-1)

place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
val_dataset = "~/.cache/paddle/dataset/int8/download/csmsc_voc1.npy"
data_loader = paddle.io.DataLoader(
val_dataset,
places=place,
drop_last=False,
batch_size=2,
)
ptq = PostTrainingQuantization(
executor=exe,
data_loader=data_loader,
model_dir=model_path,
model_filename=model_filename,
params_filename=params_filename,
batch_size=batch_size,
batch_nums=batch_nums,
algo=algo,
quantizable_op_type=quantizable_op_type,
round_type=round_type,
is_full_quantize=is_full_quantize,
optimize_model=is_optimize_model,
onnx_format=onnx_format,
is_use_cache_file=is_use_cache_file,
deploy_backend=deploy_backend,
)
ptq.quantize()
ptq.save_quantized_model(
self.int8_model,
model_filename=model_filename,
params_filename=params_filename,
)


class TestPostTraininghistForNoneShape(TestPostTrainingavgForPwganCsmsc):
def test_post_training_avg_pwgancsmsc(self):
model = "pwg_baker_static_0.4"
algo = "avg"
round_type = "round"
data_urls = [
'https://paddlespeech.bj.bcebos.com/Parakeet/released_models/pwgan/pwg_baker_static_0.4.zip'
]
data_md5s = ['e3504aed9c5a290be12d1347836d2742']
quantizable_op_type = [
"conv2d",
"depthwise_conv2d",
"mul",
]
is_full_quantize = False
is_use_cache_file = False
is_optimize_model = True
diff_threshold = 0.05
self.run_test(
model,
'pwgan_csmsc.pdmodel',
'pwgan_csmsc.pdiparams',
algo,
round_type,
data_urls,
data_md5s,
"pwg_baker_static_0.4",
quantizable_op_type,
is_full_quantize,
is_use_cache_file,
is_optimize_model,
diff_threshold,
batch_nums=2,
)


class TestPostTraininghistForMobilenetv1(TestPostTrainingQuantization):
def test_post_training_hist_mobilenetv1(self):
model = "MobileNet-V1"
Expand Down

0 comments on commit 126d3c6

Please sign in to comment.