Skip to content

Commit

Permalink
New API rewrite to separate code from exprimental API. (#559)
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng, Penghui <[email protected]>
Signed-off-by: Lv, Liang1 <[email protected]>
Signed-off-by: Mengni Wang <[email protected]>
Signed-off-by: yiliu30 <[email protected]>
Signed-off-by: Xinyu Ye <[email protected]>
Signed-off-by: Zhang, Weiwei1 <[email protected]>
Signed-off-by: zehao-intel <[email protected]>
Signed-off-by: wenhuach21 <[email protected]>
  • Loading branch information
PenghuiCheng authored Feb 28, 2023
1 parent 73675ef commit 6e10efd
Show file tree
Hide file tree
Showing 100 changed files with 4,714 additions and 937 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def train(train_loader, model, criterion, optimizer, scheduler, compression_mana
loss=losses, top1=top1, scheduler=scheduler))

compression_manager.callbacks.on_epoch_end()
best_score = validate(val_loader, model, epoch + 1)
best_score = validate(val_loader, model, epoch + 1, accelerator)
# remember best prec@1 and save checkpoint
is_best = best_score > best_prec1
best_prec1 = max(best_score, best_prec1)
Expand Down
3 changes: 2 additions & 1 deletion neural_compressor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
from .conf.config import conf
from .conf.pythonic_config import config
from .config import DistillationConfig, PostTrainingQuantConfig, \
WeightPruningConfig, QuantizationAwareTrainingConfig
WeightPruningConfig, QuantizationAwareTrainingConfig, \
MixedPrecisionConfig
2 changes: 1 addition & 1 deletion neural_compressor/adaptor/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ..utils.utility import Statistics, GLOBAL_STATE, MODE, version1_lt_version2
from ..utils import logger
from ..conf.dotdict import deep_get
from ..experimental.data.dataloaders.base_dataloader import BaseDataLoader
from ..data.dataloaders.base_dataloader import BaseDataLoader
tf = LazyImport('tensorflow')

def _add_supported_quantized_objects(custom_objects):
Expand Down
2 changes: 1 addition & 1 deletion neural_compressor/adaptor/onnxrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, framework_specific_info):
self.device = framework_specific_info["device"]
self.static = framework_specific_info["approach"] == "post_training_static_quant"
self.dynamic = framework_specific_info["approach"] == "post_training_dynamic_quant"
self.domain = framework_specific_info["domain"]
self.domain = framework_specific_info.get("domain", "auto")
self.recipes = framework_specific_info["recipes"]
self.backend = PROVIDERS[framework_specific_info["backend"]]
self.performance_only = framework_specific_info.get("performance_only", False)
Expand Down
18 changes: 14 additions & 4 deletions neural_compressor/adaptor/ox_utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,20 @@ def cast_tensor(tensor, dtype): # pragma: no cover
new_val = float_to_bfloat16(val)
else:
raise ValueError('Expect fp16 or bf16 but get {}.'.format(dtype))
tensor.float_data[:] = []
tensor.int32_data[:] = []
tensor.raw_data = new_val.tostring()
tensor.data_type = dtype_mapping[dtype]
try:
new_tensor = helper.make_tensor(
name=tensor.name,
data_type=dtype_mapping[dtype],
dims=numpy_helper.to_array(tensor).shape if \
len(numpy_helper.to_array(tensor).shape) != 0 else [],
vals=new_val if \
len(numpy_helper.to_array(tensor)) != 0 else [numpy_helper.to_array(tensor)])
tensor.CopyFrom(new_tensor)
except:
tensor.float_data[:] = []
tensor.int32_data[:] = []
tensor.raw_data = new_val.tostring()
tensor.data_type = dtype_mapping[dtype]
return True
return False

Expand Down
3 changes: 1 addition & 2 deletions neural_compressor/adaptor/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ..utils.utility import Statistics
from ..utils import logger
from .query import QueryBackendCapability
from ..experimental.data.dataloaders.base_dataloader import BaseDataLoader
from ..data.dataloaders.base_dataloader import BaseDataLoader
from .torch_utils.smooth_quant import TorchSmoothQuant
torch = LazyImport("torch")
json = LazyImport("json")
Expand Down Expand Up @@ -1202,7 +1202,6 @@ def smooth_quant(self, model, dataloader, calib_iter, tune_cfg=None, alpha=0.5,
model: A modified fp32 model
"""
if not hasattr(self, 'sq') or force_re_smooth:
##self.sq = TorchSmoothQuant(model._model, dataloader=dataloader)
self.sq = TorchSmoothQuant(model._model, dataloader=dataloader)
args = {} ##different backends may have different default values
if op_types != None:
Expand Down
4 changes: 2 additions & 2 deletions neural_compressor/adaptor/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ..utils.utility import version1_lt_version2, version1_gte_version2, version1_eq_version2
from ..utils import logger
from ..conf.dotdict import deep_get
from ..experimental.data.dataloaders.base_dataloader import BaseDataLoader
from ..data.dataloaders.base_dataloader import BaseDataLoader

tensorflow = LazyImport('tensorflow')
spr_base_verions = ('2.11.0202242', '2.11.0202250')
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def _inspect_tensor_inference(self, inspect_node_dict, model, dataloader, itera
def inspect_activation(self, node_list, graph_def, graph_node_name_mapping, quantization_cfg,
dataloader, iteration_list, graph_info):
"""Inspect the activation."""
from neural_compressor.experimental.common import Model
from neural_compressor.model import Model
original_graph_node_mapping = {}
for node in graph_def.node:
original_graph_node_mapping[node.name] = node
Expand Down
2 changes: 1 addition & 1 deletion neural_compressor/adaptor/tf_utils/graph_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from neural_compressor.utils.utility import combine_histogram
from neural_compressor.utils.utility import CaptureOutputToFile
from neural_compressor.conf.dotdict import deep_get
from neural_compressor.experimental.common import Model
from neural_compressor.model import Model
from .transform_graph.insert_logging import InsertLogging
from .transform_graph.rerange_quantized_concat import RerangeQuantizedConcat
from .transform_graph.bias_correction import BiasCorrection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from tensorflow.python.platform import gfile
from neural_compressor.conf.dotdict import deep_get
from neural_compressor.experimental.common import Model
from neural_compressor.model import Model
from .transform_graph.rerange_quantized_concat import RerangeQuantizedConcat
from .transform_graph.bias_correction import BiasCorrection
from .quantize_graph.quantize_graph_for_intel_cpu import QuantizeGraphForIntel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get_optimized_model(self, itex_mode=False):
Returns:
[graphdef]: the optimized graphdef object.
"""
from neural_compressor.experimental.common import Model
from neural_compressor.model import Model

origin_model = Model(self.model._model, **self.model.kwargs)
origin_model.name = self.model.name
Expand Down
2 changes: 1 addition & 1 deletion neural_compressor/adaptor/tf_utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def is_equivalent_input(input_tensor_list_1, input_tensor_list_2):
# THIS API IS TO BE DEPRECATED!
def get_graph_def(model, outputs=[], auto_input_output=False):
"""Get the model's graph_def."""
from neural_compressor.experimental.common import Model as NCModel
from neural_compressor.model import Model as NCModel
if not isinstance(model, NCModel):
model = NCModel(model)
model.output_tensor_names = outputs
Expand Down
Loading

0 comments on commit 6e10efd

Please sign in to comment.