Skip to content

Commit

Permalink
Neural Insights fixes (#999)
Browse files Browse the repository at this point in the history
Signed-off-by: bmyrcha <[email protected]>
  • Loading branch information
bmyrcha authored Jun 17, 2023
1 parent a0fd7b1 commit 8983446
Show file tree
Hide file tree
Showing 27 changed files with 3,778 additions and 4,633 deletions.
7 changes: 0 additions & 7 deletions examples/onnxrt/nlp/bert/quantization/ptq_dynamic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ bash prepare_model.sh --input_dir=./MRPC \

## Diagnosis
Neural Compressor offers quantization and benchmark diagnosis. Adding `diagnosis` parameter to Quantization/Benchmark config will provide additional details useful in diagnostics.
### Quantization diagnosis
```
config = PostTrainingQuantConfig(
diagnosis=True,
...
)
```

### Benchmark diagnosis
```
Expand Down
10 changes: 0 additions & 10 deletions examples/onnxrt/nlp/bert/quantization/ptq_dynamic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ def result(self):
default=False,
help="whether quantize the model"
)
parser.add_argument(
'--diagnose',
dest='diagnose',
action='store_true',
help='use Neural Insights to diagnose tuning and benchmark.',
)
parser.add_argument(
'--output_model',
type=str,
Expand Down Expand Up @@ -379,8 +373,6 @@ def eval_func(model):
return metric.result()

if args.benchmark:
if args.diagnose and args.mode != "performance":
print("[ WARNING ] Diagnosis works only with performance benchmark.")
model = onnx.load(args.model_path)
if args.mode == 'performance':
from neural_compressor.benchmark import fit
Expand All @@ -389,7 +381,6 @@ def eval_func(model):
iteration=100,
cores_per_instance=4,
num_of_instance=1,
diagnosis=args.diagnose,
)
fit(model, conf, b_dataloader=dataloader)
elif args.mode == 'accuracy':
Expand Down Expand Up @@ -424,7 +415,6 @@ def eval_func(model):
from neural_compressor import quantization, PostTrainingQuantConfig
config = PostTrainingQuantConfig(
approach='dynamic',
diagnosis=args.diagnose,
)
q_model = quantization.fit(model,
config,
Expand Down
7 changes: 0 additions & 7 deletions examples/onnxrt/nlp/bert/quantization/ptq_static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ bash prepare_model.sh --input_dir=./MRPC \

## Diagnosis
Neural Compressor offers quantization and benchmark diagnosis. Adding `diagnosis` parameter to Quantization/Benchmark config will provide additional details useful in diagnostics.
### Quantization diagnosis
```
config = PostTrainingQuantConfig(
diagnosis=True,
...
)
```

### Benchmark diagnosis
```
Expand Down
9 changes: 0 additions & 9 deletions examples/onnxrt/nlp/bert/quantization/ptq_static/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ def result(self):
default=False,
help="whether quantize the model"
)
parser.add_argument(
'--diagnose',
dest='diagnose',
action='store_true',
help='use Neural Insights to diagnose tuning and benchmark.',
)
parser.add_argument(
"--output_model",
type=str,
Expand Down Expand Up @@ -386,8 +380,6 @@ def eval_func(model):
return metric.result()

if args.benchmark:
if args.diagnose and args.mode != "performance":
print("[ WARNING ] Diagnosis works only with performance benchmark.")
model = onnx.load(args.model_path)
if args.mode == "performance":
from neural_compressor.benchmark import fit
Expand All @@ -396,7 +388,6 @@ def eval_func(model):
iteration=100,
cores_per_instance=4,
num_of_instance=1,
diagnosis=args.diagnose,
)
fit(model, conf, b_dataloader=dataloader)
elif args.mode == "accuracy":
Expand Down
1 change: 1 addition & 0 deletions neural_compressor/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def fit(model, conf, b_dataloader=None, b_func=None):
profile(wrapped_model, conf, b_dataloader)
update_neural_insights_workload(ni_workload_id, "success")
except Exception as e:
logger.error(e)
update_neural_insights_workload(ni_workload_id, "failure")

logger.info("Start to run Benchmark.")
Expand Down
10 changes: 8 additions & 2 deletions neural_compressor/profiling/profiler/onnxrt_profiler/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,22 @@ def profile_model(
Returns:
None
"""
import numpy as np
import onnxruntime as ort

graph = self.model
onnx_options = create_onnx_config(ort, intra_num_of_threads, inter_num_of_threads)
# Create a profile session
sess_profile = ort.InferenceSession(graph.SerializePartialToString(), onnx_options)
input_tensor = sess_profile.get_inputs()[0]
input_tensors = sess_profile.get_inputs()

for _, (inputs, _) in enumerate(self.dataloader):
input_dict = {input_tensor.name: inputs}
if not isinstance(inputs, np.ndarray) and len(input_tensors) != len(inputs):
raise Exception("Input data number mismatch.")
if len(input_tensors) == 1:
input_dict = {input_tensors[0].name: inputs}
else:
input_dict = {input_tensor.name: input_data for input_tensor, input_data in zip(input_tensors, inputs)}
sess_profile.run(None, input_dict)
break

Expand Down
5 changes: 4 additions & 1 deletion neural_insights/components/diagnosis/diagnosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def load_quantization_config(self) -> dict:

def get_op_list(self) -> List[dict]:
"""Get OP list for model."""
check_module("numpy")
import numpy as np

minmax_file_path = os.path.join(
self.workload_location,
"inspect_saved",
Expand All @@ -92,7 +95,7 @@ def get_op_list(self) -> List[dict]:
for op_name, min_max in min_max_data.items():

mse = self.calculate_mse(op_name, input_model_tensors, optimized_model_tensors)
if mse is None:
if mse is None or np.isnan(mse):
continue
min = float(min_max.get("min", None))
max = float(min_max.get("max", None))
Expand Down
27 changes: 15 additions & 12 deletions neural_insights/components/graph/reader/onnxrt_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
"""Onnxrt Graph reader."""

import uuid
from typing import Any, Dict, List, Optional

from neural_insights.components.graph.attribute import Attribute
Expand Down Expand Up @@ -48,25 +49,27 @@ def read(self) -> Graph:
edges: dict = {}

for node_def in onnx_nodes:
node_name = node_def.name
if node_name == "":
node_name = f"{node_def.op_type}_{uuid.uuid4()}"

if self._should_hide_node(node_def):
self._hide_node(node_def)
self._hide_node(node_name)
continue

edges = self._add_edges_from_node(edges, node_def)

current_node_id = node_def.name
edges = self._add_edges_from_node(edges, node_def, node_name)

graph.add_node(
Node(
id=current_node_id,
id=node_name,
label=node_def.op_type,
highlight=False,
properties={
"name": node_def.name,
"name": node_name,
"type": node_def.op_type,
},
attributes=self._convert_attributes(node_def),
groups=self._get_group_names(current_node_id),
groups=self._get_group_names(node_name),
),
)

Expand Down Expand Up @@ -102,9 +105,9 @@ def _node_def_has_not_hidden_inputs(self, node_def: Any) -> bool:
)
return len(not_hidden_input_ids) > 0

def _hide_node(self, node: Any) -> None:
def _hide_node(self, node_name: str) -> None:
"""Mark node as hidden."""
self._hidden_node_ids[node.name] = True
self._hidden_node_ids[node_name] = True

def _is_node_id_hidden(self, node_id: str) -> bool:
"""Check if provided node_id is hidden."""
Expand Down Expand Up @@ -161,19 +164,19 @@ def _add_boundary_nodes(self, graph: Graph) -> Graph:
)
return graph

def _add_edges_from_node(self, edges: dict, node: Any) -> dict:
def _add_edges_from_node(self, edges: dict, node: Any, node_name: str) -> dict:
"""Add update edges from node data."""
for edge in node.input:
edge_data = edges.get(edge, {})
input_nodes = edge_data.get("inputs", [])
input_nodes.append(node.name)
input_nodes.append(node_name)
edge_data.update({"inputs": input_nodes})
edges.update({edge: edge_data})

for edge in node.output:
edge_data = edges.get(edge, {})
output_nodes = edge_data.get("outputs", [])
output_nodes.append(node.name)
output_nodes.append(node_name)
edge_data.update({"outputs": output_nodes})
edges.update({edge: edge_data})

Expand Down
Loading

0 comments on commit 8983446

Please sign in to comment.