|
| 1 | +### Usage demo: |
| 2 | + |
| 3 | +#### two steps to get quantized model |
| 4 | + |
| 5 | +```diff |
| 6 | +import torch |
| 7 | ++ from neural_compressor.torch.quantization import FP8Config, convert, prepare, finalize_calibration |
| 8 | +import habana_frameworks.torch.core as htcore |
| 9 | + |
| 10 | +class M(torch.nn.Module): |
| 11 | + def __init__(self) -> None: |
| 12 | + super().__init__() |
| 13 | + self.fc1 = torch.nn.Linear(10, 5) |
| 14 | + self.fc2 = torch.nn.Linear(5, 10) |
| 15 | + |
| 16 | + def forward(self, inp): |
| 17 | + x1 = self.fc1(inp) |
| 18 | + x2 = self.fc2(x1) |
| 19 | + return x2 |
| 20 | + |
| 21 | +model = M().eval() |
| 22 | + |
| 23 | ++ config = FP8Config.from_json_file(args.quant_config) # args.quant_config is the path of json file |
| 24 | + |
| 25 | ++ if config.measure: |
| 26 | ++ model = prepare(model, config) |
| 27 | + |
| 28 | ++ if config.quantize: |
| 29 | ++ htcore.hpu_initialize() |
| 30 | ++ model = convert(model, config) |
| 31 | + |
| 32 | +# user code run |
| 33 | +with torch.no_grad(): |
| 34 | + model.to("hpu") |
| 35 | + output = model(torch.randn(1, 10).to("hpu")) |
| 36 | + print(output) |
| 37 | + |
| 38 | ++ if config.measure: |
| 39 | ++ finalize_calibration(model) |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +Whole script and config refer to [sample_two_steps.py](./sample_two_steps.py), [maxabs_measure.json](./maxabs_measure.json) and [maxabs_quant.json](./maxabs_quant.json). |
| 44 | + |
| 45 | +First, measure the tensor quantization statistic: |
| 46 | +```shell |
| 47 | +python sample_two_steps.py --quant_config=maxabs_measure.json |
| 48 | +``` |
| 49 | + |
| 50 | +Then quantize the model based on previous measurements: |
| 51 | +```shell |
| 52 | +python sample_two_steps.py --quant_config=maxabs_quant.json |
| 53 | +``` |
| 54 | + |
| 55 | +#### one step to get quantized model |
| 56 | + |
| 57 | +```diff |
| 58 | +import torch |
| 59 | ++ from neural_compressor.torch.quantization import FP8Config, convert, prepare, finalize_calibration |
| 60 | +import habana_frameworks.torch.core as htcore |
| 61 | + |
| 62 | +class M(torch.nn.Module): |
| 63 | + def __init__(self) -> None: |
| 64 | + super().__init__() |
| 65 | + self.fc1 = torch.nn.Linear(10, 5) |
| 66 | + self.fc2 = torch.nn.Linear(5, 10) |
| 67 | + |
| 68 | + def forward(self, inp): |
| 69 | + x1 = self.fc1(inp) |
| 70 | + x2 = self.fc2(x1) |
| 71 | + return x2 |
| 72 | + |
| 73 | +model = M().to("hpu") |
| 74 | + |
| 75 | ++ config = FP8Config.from_json_file(args.quant_config) # args.quant_config is the path of json file |
| 76 | ++ model = prepare(model, config) |
| 77 | + |
| 78 | +# user code run to do calibration |
| 79 | +with torch.no_grad(): |
| 80 | + output = model(torch.randn(1, 10).to("hpu")) |
| 81 | + print(output) |
| 82 | + |
| 83 | ++ finalize_calibration(model) |
| 84 | ++ model = convert(model) |
| 85 | + |
| 86 | +# user code to run benchmark for quantized model |
| 87 | +with torch.no_grad(): |
| 88 | + output = model(torch.randn(1, 10).to("hpu")) |
| 89 | + print(output) |
| 90 | +``` |
| 91 | + |
| 92 | +Whole script and config refer to [sample_one_step.py](./sample_one_step.py). |
| 93 | + |
| 94 | +```shell |
| 95 | +python sample_one_step.py --quant_config=quant_config.json |
| 96 | +``` |
0 commit comments