# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.# SPDX-License-Identifier: BSD-3-ClauseimportmathfromtypingimportDictfromaimet_onnximportqtype,int16,float16,QuantizationSimModel
[docs]defflip_layers_to_higher_precision(sim:QuantizationSimModel,layer_sensitivity_dict:Dict[str,float],percent_to_flip:int=10,override_precision:qtype=float16,):""" Given a sim object and a layer-sensitivity dictionary, flip a given percentage of the layers to higher precision. :param sim: QuantizationSimModel instance initialized with the base precision :param layer_sensitivity_dict: Dict of (layer_name: sqnr_metric) that is output from analyze_per_layer_sensitivity :param percent_to_flip: Percentage of layers to flip :param override_precision: Precision to sets layers to. At present, either int16 (w16a16) or float16 are supported. """# Sanity checkifoverride_precisionnotin(int16,float16):raiseValueError("higher_precision must be int16 or float16")sqnr_list=sorted(layer_sensitivity_dict.items(),key=lambdaitem:item[1])sqnr_list=sqnr_list[:math.ceil(len(sqnr_list)*percent_to_flip/100)]cg_ops=sim.connected_graph.get_all_ops()forlayer_name,_insqnr_list:op=cg_ops[layer_name](input_quantizers,output_quantizers,param_quantizers,)=sim.get_op_quantizers(op)forqininput_quantizers+output_quantizers:ifoverride_precision==int16:q.set_bitwidth(16)else:q.enabled=Falsefor_,qinparam_quantizers.items():ifoverride_precision==int16:q.set_bitwidth(16)else:q.enabled=False