Best Python code snippet using hypothesis
fixed_point_quantization.py
Source:fixed_point_quantization.py  
1import numpy as np2import loss3import kl_divergence_quantization4# 1. Suppose that you have a layer with outputs in the range of [-a, a).5#    a is a real number could be float32 or float64.6# 2. Multiplying two 8-bit integers is a 16-bit integer7#    Multiplying two 32-bit float is a 64-bit float8# https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html#work-with-qat-networks9class FixedPointQuantizationStrategy():10    def __init__(self, from_dtype, to_dtype, verbose=0, scale_type='normal'):11        self.verbose = verbose12        self.from_dtype = from_dtype13        self.to_dtype = to_dtype14        self.scale_type = scale_type15        self.supported_to_dtype = ['int4', np.int8, np.uint8, np.int16, np.uint16, np.int32, np.uint32]16        if self.to_dtype not in self.supported_to_dtype:17            raise Exception("Conversion to dtype {} is not supported".format(self.to_dtype))18        self.quantization_dtype = ''19        self.scale = 120        self.values = None21        self.scale_factor = 022        self.zero_center = 023        self.values_quantized = None24        self.values_dequantized = None25        self.quantized = False26    def set_verbose(self, val):27        self.verbose = val28    def set_values(self, values):29        self.values = values30        # self.convert_zero_mean()31        max_tensor_value = np.max(abs(self.values))32        max_tensor_nearest_value = np.ceil(max_tensor_value)33        self.decide_scale()34        # This scale factor calculated bu analyzing the range of the real values35        # but this has to be done post training.36        # 1. Run the inference and find the best scale factor / range for real value37        # 2. Then try to use that scale factor instead of fixed real valued scale factor.38        if self.scale_type == 'kl':39            threshold, divergence = kl_divergence_quantization.kl_divergence_scale(self.values,40                                                           quantized_dtype='int8',41                                                           num_bins=8001,42                                                           eps=0.0001)43            self.scale_factor = self.scale / threshold44        else:45            self.scale_factor = self.scale / max_tensor_nearest_value46        print("Scale Factor :", self.scale_factor)47    def convert_zero_mean(self):48        self.zero_center = np.mean(self.values)49        self.values -= self.zero_center50    def decide_scale(self):51        if type(self.to_dtype) == str and self.to_dtype == 'int4':52            self.scale = 853            quantization_bits = 454            self.quantization_dtype = 'int4'55            self.to_dtype = 'int4'56        elif type(self.to_dtype) == str and self.to_dtype == 'uint4':57            self.scale = 1658            quantization_bits = 459            self.quantization_dtype = 'uint4'60            self.to_dtype = np.uint861        else:62            iinfo = np.iinfo(self.to_dtype)63            self.scale = max([abs(iinfo.min), abs(iinfo.max)])-164            quantization_bits = iinfo.bits65            self.quantization_dtype = iinfo.dtype66        finfo = np.finfo(self.from_dtype)67        if self.verbose:68            print("Quantization will be done as follows")69            print("============== From =================")70            print("Dtype: {} Bits: {}".format(finfo.dtype, finfo.bits))71            print("============== To =================")72            print("Dtype: {} Bits: {} Range: {}".format(self.quantization_dtype,73                                                        quantization_bits, self.scale))74            print("===================================")75    def quantize(self):76        if self.values is None:77            raise Exception("No value for quantization_research")78        if self.values.dtype != self.from_dtype:79            raise Exception("Dtype of from tensorflow value does not match to provided from dtype")80        scaled_values = self.values * self.scale_factor81        to_dtype = self.to_dtype82        if type(self.to_dtype) == str:83            to_dtype = np.int884        self.values_quantized = np.asarray(np.round(scaled_values), dtype=to_dtype)85        if self.verbose:86            print("Quantized value: ", self.values_quantized)87        self.quantized = True88        self.dequantize()89    def dequantize(self):90        if not self.quantized:91            raise Exception("Quantization is not performed")92        self.values_dequantized = self.values_quantized / self.scale_factor93    def information_loss(self):94        if not self.quantized:95            raise Exception("Quantization is not performed")96        loss.MAE(self.values, self.values_dequantized)97        loss.MSE(self.values, self.values_dequantized)98class Inference():99    def __init__(self, quant_weight_strategy: FixedPointQuantizationStrategy,100                 quant_activation_strategy: FixedPointQuantizationStrategy,101                 weights: np.ndarray,102                 activations: np.ndarray):103        self.quant_weight_strategy = quant_weight_strategy104        self.quant_activation_strategy = quant_activation_strategy105        self.quant_weight_strategy.set_values(weights)106        self.quant_activation_strategy.set_values(activations)107        dtypes_map = {108            'int4': np.int8,109            np.int8: np.int16,110            np.int16: np.int32,111            np.int32: np.int64112        }113        self.inference_quant_output_dtype = dtypes_map[self.quant_weight_strategy.to_dtype]114        iinfo = np.iinfo(self.inference_quant_output_dtype)115        self.descale_factor = max([abs(iinfo.min), abs(iinfo.max)]) // 2116        self.before_quantization_output = None117        self.after_quantization_output = None118        self.after_dequantization_output = None119    def forward_before_quantization(self):120        self.before_quantization_output = np.matmul(self.quant_weight_strategy.values,121                                                    self.quant_activation_strategy.values)122        print("Before quantization_research inference output \n", self.before_quantization_output)123    def quantize(self, verbose=0):124        self.quant_weight_strategy.set_verbose(verbose)125        self.quant_weight_strategy.quantize()126        self.quant_activation_strategy.set_verbose(verbose)127        self.quant_activation_strategy.quantize()128    def forward_after_quantization(self):129        if not (self.quant_weight_strategy.quantized and self.quant_activation_strategy.quantized):130            raise Exception("Quantization is not performed")131        self.after_quantization_output = np.matmul(self.quant_weight_strategy.values_quantized,132                                                   self.quant_activation_strategy.values_quantized,133                                                   dtype=self.inference_quant_output_dtype)134        dequantized_output = self.after_quantization_output / (self.quant_weight_strategy.scale_factor *135                                                               self.quant_activation_strategy.scale_factor)136        self.after_dequantization_output = dequantized_output137        print("After quantization_research inference output \n", self.after_dequantization_output)138    def information_loss(self, verbose=0):139        if self.before_quantization_output is None or \140                self.after_dequantization_output is None:141            raise Exception("Run the inference before and after quantization_research")142        if verbose:143            print("=========== Weight Quantization Information loss ============")144            self.quant_weight_strategy.information_loss()145            print("=========== Activation Quantization Information loss ============")146            self.quant_activation_strategy.information_loss()147        print("=========== Inference information loss ============")148        loss.MAE(self.before_quantization_output, self.after_dequantization_output)149        loss.MSE(self.before_quantization_output, self.after_dequantization_output)150def test(scale_type='kl'):151    from_dtype = np.float32152    # to_dtype = 'int4'153    to_dtype = np.int8154    quant_weight1_strategy = FixedPointQuantizationStrategy(from_dtype, to_dtype, scale_type=scale_type, verbose=1)155    quant_weight2_strategy = FixedPointQuantizationStrategy(from_dtype, to_dtype, scale_type=scale_type, verbose=1)156    weights1 = np.asarray([157        [-1.54, 0.22],158        [-0.26, 0.65]159    ], dtype=from_dtype)160    weights2 = np.asarray([161        [0.35],162        [-0.51]163    ], dtype=from_dtype)164    infer = Inference(quant_weight1_strategy,165                      quant_weight2_strategy,166                      weights1,167                      weights2)168    infer.forward_before_quantization()169    infer.quantize(0)170    infer.forward_after_quantization()171    infer.information_loss(1)172test('kl') # KL divergence to find the best threshold...test_motion_plan.py
Source:test_motion_plan.py  
...27@st.composite28def generate_coordinates(draw: st.DrawFn) -> Coordinates[str, np.float64]:29    """Create coordinates using Hypothesis."""30    coord = [31        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=500)),32        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=490)),33        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=300)),34        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=300)),35        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=300)),36        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0, max_value=300)),37    ]38    formatted: Iterator[np.float64] = (np.float64(i) for i in coord)39    return dict(zip(SIXAXES, formatted))40@st.composite41def generate_close_coordinates(42    draw: st.DrawFn, prev_coord: Coordinates[str, np.float64]43) -> Coordinates[str, np.float64]:44    """Create coordinates using Hypothesis."""45    diff: List[np.typing.NDArray[np.float64]] = [46        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),47        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),48        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),49        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),50        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),51        draw(hynp.from_dtype(np.dtype(np.float64), min_value=0.1, max_value=1.0)),52    ]53    coord: np.typing.NDArray[np.float64] = vectorize(prev_coord) + diff54    return dict(zip(SIXAXES, (np.float64(i) for i in coord)))55def reject_close_coordinates(56    a: Coordinates[str, np.float64], b: Coordinates[str, np.float64]57) -> bool:58    """Reject example if the coordinates are too close.59    Consecutive coordinates must be at least 1mm apart in one of the axes.60    """61    return not np.any(np.isclose(vectorize(b), vectorize(a), atol=1.0))62@st.composite63def generate_target_list(64    draw: st.DrawFn,65    elements: st.SearchStrategy[Coordinates[str, np.float64]] = generate_coordinates(),...compress_bin_data.py
Source:compress_bin_data.py  
...12# buff_int_size = buff_size // oasis_int_size13areaperil_int_relative_size = areaperil_int.itemsize // oasis_int_size14oasis_float_relative_size = oasis_float.itemsize // oasis_int_size15results_relative_size = 2 * oasis_float_relative_size16EventIndexBin = nb.from_dtype(np.dtype([('event_id', np.int32),17                                        ('offset', np.int64),18                                        ('size', np.int64)19                                        ]))20Index_type = nb.from_dtype(np.dtype([('start', np.int64),21                                     ('end', np.int64)22                                     ]))23Event = nb.from_dtype(np.dtype([('areaperil_id', areaperil_int),24                                ('intensity_bin_id', np.int32),25                                ('probability', oasis_float)26                                ]))27event_size = Event.size28footprint_offset = 829damagebindictionary =  nb.from_dtype(np.dtype([('bin_index', np.int32),30                                               ('bin_from', oasis_float),31                                               ('bin_to', oasis_float),32                                               ('interpolation', oasis_float),33                                               ('interval_type', np.int32),34                                               ]))35damagebindictionaryCsv =  nb.from_dtype(np.dtype([('bin_index', np.int32),36                                                  ('bin_from', oasis_float),37                                                  ('bin_to', oasis_float),38                                                  ('interpolation', oasis_float)]))39EventCSV =  nb.from_dtype(np.dtype([('event_id', np.int32),40                                    ('areaperil_id', areaperil_int),41                                    ('intensity_bin_id', np.int32),42                                    ('probability', oasis_float)43                                    ]))44Item = nb.from_dtype(np.dtype([('id', np.int32),45                               ('coverage_id', np.int32),46                               ('areaperil_id', areaperil_int),47                               ('vulnerability_id', np.int32),48                               ('group_id', np.int32)49                               ]))50Vulnerability = nb.from_dtype(np.dtype([('vulnerability_id', np.int32),51                                        ('intensity_bin_id', np.int32),52                                        ('damage_bin_id', np.int32),53                                        ('probability', oasis_float)54                                        ]))55VulnerabilityIndex = nb.from_dtype(np.dtype([('vulnerability_id', np.int32),56                                             ('offset', np.int64),57                                             ('size', np.int64),58                                             ('original_size', np.int64)59                                             ]))60VulnerabilityRow = nb.from_dtype(np.dtype([('intensity_bin_id', np.int32),61                                           ('damage_bin_id', np.int64),62                                           ('probability', oasis_float)63                                           ]))64vuln_offset = 465def compress_bin_file_command(size: int) -> None:66    with open("./data_config.json") as file:67        config_data = json.load(file)68    intensity_bins: int = config_data["num_intensity_bins"]69    # compress the footprint data70    subprocess.call(f"footprinttocsv -b ./data/{size}/static/footprint.bin -x ./data/{size}/static/footprint.idx | "71                    f"footprinttobin -z -u -b ./data/{size}/static/footprint.bin.z -x ./data/{size}/static/footprint.idx.z "72                    f"-i {intensity_bins}",73                    shell=True)74    # compress the vulnerability data...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
