Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  1. import torch
  2. from pytorch_quantization import nn as quant_nn
  3. from torch import nn
  4. from super_gradients.training.dataloaders import cifar10_train
  5. from super_gradients.training.utils.quantization.calibrator import QuantizationCalibrator
  6. from super_gradients.training.utils.quantization.core import SGQuantMixin
  7. from super_gradients.training.utils.quantization.export import export_quantized_module_to_onnx
  8. from super_gradients.training.utils.quantization.selective_quantization_utils import SelectiveQuantizer
  9. def e2e_example():
  10. class MyBlock(nn.Module):
  11. def __init__(self, in_feats, out_feats) -> None:
  12. super().__init__()
  13. self.in_feats = in_feats
  14. self.out_feats = out_feats
  15. self.flatten = nn.Flatten()
  16. self.linear = nn.Linear(in_feats, out_feats)
  17. def forward(self, x):
  18. return self.linear(self.flatten(x))
  19. class MyQuantizedBlock(SGQuantMixin):
  20. def __init__(self, in_feats, out_feats) -> None:
  21. super().__init__()
  22. self.flatten = nn.Flatten()
  23. self.linear = quant_nn.QuantLinear(in_feats, out_feats)
  24. def forward(self, x):
  25. return self.linear(self.flatten(x))
  26. class MyModel(nn.Module):
  27. def __init__(self, res, n_classes) -> None:
  28. super().__init__()
  29. self.my_block = MyBlock(3 * (res**2), n_classes)
  30. def forward(self, x):
  31. return self.my_block(x)
  32. res = 32
  33. n_clss = 10
  34. module = MyModel(res, n_clss)
  35. # QUANTIZE
  36. q_util = SelectiveQuantizer()
  37. q_util.register_quantization_mapping(layer_names={"my_block"}, quantized_target_class=MyQuantizedBlock)
  38. q_util.quantize_module(module)
  39. # CALIBRATE (PTQ)
  40. train_loader = cifar10_train()
  41. calib = QuantizationCalibrator()
  42. calib.calibrate_model(module, method=q_util.default_quant_modules_calib_method_inputs, calib_data_loader=train_loader)
  43. module.cuda()
  44. # SANITY
  45. x = torch.rand(1, 3, res, res, device="cuda")
  46. with torch.no_grad():
  47. y = module(x)
  48. torch.testing.assert_close(y.size(), (1, n_clss))
  49. print(module)
  50. # EXPORT TO ONNX
  51. export_quantized_module_to_onnx(module, "my_quantized_model.onnx", input_shape=(1, 3, res, res))
  52. if __name__ == "__main__":
  53. e2e_example()
Discard
Tip!

Press p or to see the previous file or, n or to see the next file