|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import contextlib |
| 13 | +import os |
| 14 | +import sys |
| 15 | + |
| 16 | +import atheris |
| 17 | +import numpy as np |
| 18 | +import openvino as ov |
| 19 | +from atheris import FuzzedDataProvider |
| 20 | + |
| 21 | +with atheris.instrument_imports(include=["nncf"]): |
| 22 | + import nncf |
| 23 | + |
| 24 | +# Disable logging for cleaner fuzzing output |
| 25 | +nncf.set_log_level(40) |
| 26 | + |
| 27 | +# To disable telemetry during fuzzing |
| 28 | +os.environ["NNCF_CI"] = "1" |
| 29 | + |
| 30 | + |
| 31 | +class MockDataset: |
| 32 | + def __init__(self): |
| 33 | + self.n = 0 |
| 34 | + |
| 35 | + def __iter__(self): |
| 36 | + return self |
| 37 | + |
| 38 | + def __next__(self): |
| 39 | + if self.n < 300: |
| 40 | + self.n += 1 |
| 41 | + return np.ones((1, 1, 1, 1), dtype=np.float32) |
| 42 | + raise StopIteration |
| 43 | + |
| 44 | + |
| 45 | +def check_quantize_api(fdp: FuzzedDataProvider) -> None: |
| 46 | + model = ov.Model([], []) |
| 47 | + dataset = nncf.Dataset(MockDataset()) |
| 48 | + r_mode = fdp.PickValueInList(list(nncf.QuantizationMode) + [None]) |
| 49 | + r_preset = fdp.PickValueInList(list(nncf.QuantizationPreset) + [None]) |
| 50 | + r_device = fdp.PickValueInList(list(nncf.TargetDevice)) |
| 51 | + r_subset_size = fdp.ConsumeIntInRange(-10, 500) |
| 52 | + r_fbc = fdp.ConsumeBool() |
| 53 | + r_model_type = fdp.PickValueInList(list(nncf.ModelType) + [None]) # Keeping it None for simplicity |
| 54 | + |
| 55 | + with contextlib.suppress(nncf.ParameterNotSupportedError, nncf.ValidationError): |
| 56 | + nncf.quantize( |
| 57 | + model, |
| 58 | + calibration_dataset=dataset, |
| 59 | + mode=r_mode, |
| 60 | + preset=r_preset, |
| 61 | + target_device=r_device, |
| 62 | + subset_size=r_subset_size, |
| 63 | + fast_bias_correction=r_fbc, |
| 64 | + model_type=r_model_type, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def check_compress_weights_api(fdp: FuzzedDataProvider) -> None: |
| 69 | + model = ov.Model([], []) |
| 70 | + dataset = nncf.Dataset(MockDataset()) |
| 71 | + |
| 72 | + r_mode = fdp.PickValueInList(list(nncf.CompressWeightsMode)) |
| 73 | + r_ratio = fdp.ConsumeFloatInRange(-0.1, 1.1) if fdp.ConsumeBool() else None |
| 74 | + r_group_size = fdp.ConsumeIntInRange(-10, 1000) if fdp.ConsumeBool() else None |
| 75 | + r_all_layers = fdp.PickValueInList([True, False, None]) |
| 76 | + r_dataset = dataset if fdp.ConsumeBool() else None |
| 77 | + r_sensitivity_metric = fdp.PickValueInList(list(nncf.SensitivityMetric) + [None]) |
| 78 | + r_subset_size = fdp.ConsumeIntInRange(-10, 400) |
| 79 | + r_awq = fdp.PickValueInList([True, False, None]) |
| 80 | + r_scale_estimation = fdp.PickValueInList([True, False, None]) |
| 81 | + r_gptq = fdp.PickValueInList([True, False, None]) |
| 82 | + r_lora_correction = fdp.PickValueInList([True, False, None]) |
| 83 | + r_backup_mode = fdp.PickValueInList(list(nncf.BackupMode) + [None]) |
| 84 | + r_compression_format = fdp.PickValueInList(list(nncf.CompressionFormat)) |
| 85 | + with contextlib.suppress(nncf.ParameterNotSupportedError, nncf.ValidationError): |
| 86 | + nncf.compress_weights( |
| 87 | + model, |
| 88 | + mode=r_mode, |
| 89 | + ratio=r_ratio, |
| 90 | + group_size=r_group_size, |
| 91 | + ignored_scope=None, |
| 92 | + dataset=r_dataset, |
| 93 | + sensitivity_metric=r_sensitivity_metric, |
| 94 | + all_layers=r_all_layers, |
| 95 | + subset_size=r_subset_size, |
| 96 | + awq=r_awq, |
| 97 | + scale_estimation=r_scale_estimation, |
| 98 | + gptq=r_gptq, |
| 99 | + lora_correction=r_lora_correction, |
| 100 | + backup_mode=r_backup_mode, |
| 101 | + compression_format=r_compression_format, |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def TestOneInput(data: bytes) -> None: |
| 106 | + fdp = FuzzedDataProvider(data) |
| 107 | + algo = fdp.PickValueInList(["ptq", "wc"]) |
| 108 | + if algo == "ptq": |
| 109 | + check_quantize_api(fdp) |
| 110 | + elif algo == "wc": |
| 111 | + check_compress_weights_api(fdp) |
| 112 | + |
| 113 | + |
| 114 | +def main() -> None: |
| 115 | + atheris.Setup(sys.argv, TestOneInput) |
| 116 | + atheris.Fuzz() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments