qdq_util bug fix (#12647)

bugfix: when creating a temp infer file, an existing file maybe accidentally deleted
This commit is contained in:
Chen Fu 2022-08-22 09:32:43 -04:00 committed by GitHub
parent 2102b8f67c
commit 8456f5fd97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 11 deletions

View file

@ -103,7 +103,7 @@ def modify_model_output_intermediate_tensors(
name=reshape_output,
)
model.graph.node.append(reshape_node)
reshape_output_value_info = helper.make_tensor_value_info(reshape_output, TensorProto.FLOAT, [1])
reshape_output_value_info = helper.make_tensor_value_info(reshape_output, TensorProto.FLOAT, [-1])
model.graph.output.append(reshape_output_value_info)
return model
@ -144,8 +144,8 @@ def collect_activations(
intermediate_outputs = []
for input_d in input_reader:
intermediate_outputs.append(inference_session.run(None, input_d))
if not intermediate_outputs:
raise RuntimeError("No data is collected while running augmented model!")
if not intermediate_outputs:
raise RuntimeError("No data is collected while running augmented model!")
output_dict = {}
output_info = inference_session.get_outputs()
@ -344,7 +344,7 @@ def compute_signal_to_quantization_noice_ratio(
right = numpy.concatenate(ylist).flatten()
Ps = numpy.linalg.norm(left)
Pn = numpy.linalg.norm(left - right)
Pn = numpy.linalg.norm(left - right) + numpy.finfo("float").eps
return 20 * math.log10(Ps / Pn)

View file

@ -370,7 +370,7 @@ def generate_identified_filename(filename: Path, identifier: str) -> Path:
"""
Helper function to generate a identifiable filepath by concatenating the given identifier as a suffix.
"""
return filename.parent.joinpath(filename.stem + identifier).with_suffix(filename.suffix)
return filename.parent.joinpath(filename.stem + identifier + filename.suffix)
def apply_plot(hist, hist_edges):
@ -527,10 +527,10 @@ def model_has_infer_metadata(model):
def load_model_with_shape_infer(model_path: Path):
inferred_model_path = generate_identified_filename(model_path, "-inferred")
onnx.shape_inference.infer_shapes_path(str(model_path), str(inferred_model_path))
model = onnx.load(inferred_model_path.as_posix())
inferred_model_path.unlink()
with tempfile.TemporaryDirectory() as temp_dir:
inferred_model_path = str(Path(temp_dir) / (model_path.stem + "-inferred.onnx"))
onnx.shape_inference.infer_shapes_path(str(model_path), inferred_model_path)
model = onnx.load(inferred_model_path)
return model

View file

@ -210,12 +210,12 @@ class TestSaveActivations(unittest.TestCase):
for tensor_name in tensor_names:
self.assertGreater(
activations_error[tensor_name]["xmodel_err"],
0.1,
0.01,
f"{tensor_name} cross model error {activations_error[tensor_name]['xmodel_err']} exceeds threashold.",
)
self.assertGreater(
activations_error[tensor_name]["qdq_err"],
0.1,
0.01,
f"{tensor_name} qdq error {activations_error[tensor_name]['qdq_err']} exceeds threashold.",
)