module_quantize.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. '''Copyright The Microsoft DeepSpeed Team'''
  2. import torch
  3. def quantize_transformer_layer(orig_layer_impl, model, megatron=False, preln=False):
  4. """ Quantize bert-style transformer layers with DeepSpeed's transformer layer
  5. Arguments:
  6. orig_layer_impl (torch.nn.Module): the original transformer layer implementation to look for,
  7. e.g., transformers.modeling_bert.BertLayer.
  8. model (torch.nn.Module): user's nn.module representing their model
  9. megatron (bool): megatron model-parallel implementation (this is supported for inference only)
  10. preln (bool): does the original layer implementation do pre or post layer norm?
  11. Note: For Bert kind of models, we inject based on the DeepSpeed-Example models, if not setting huggingface flag.
  12. Returns:
  13. Updated nn.module with quantized transformer layers
  14. """
  15. def quantize_weight(weight):
  16. return weight.to(torch.int8)
  17. def megatron_layer_quantize(layer):
  18. layer.attention.query_key_value.weight.data = quantize_weight(
  19. layer.attention.query_key_value.weight.data)
  20. layer.attention.dense.weight.data = quantize_weight(
  21. layer.attention.dense.weight.data)
  22. layer.mlp.dense_h_to_4h.weight.data = quantize_weight(
  23. layer.mlp.dense_h_to_4h.weight.data)
  24. layer.mlp.dense_4h_to_h.weight.data = quantize_weight(
  25. layer.mlp.dense_4h_to_h.weight.data)
  26. def bert_layer_quantize(layer):
  27. layer.attention.self.query.weight.data = quantize_weight(
  28. layer.attention.self.query.weight.data)
  29. layer.attention.self.key.weight.data = quantize_weight(
  30. layer.attention.self.key.weight.data)
  31. layer.attention.self.value.weight.data = quantize_weight(
  32. layer.attention.self.value.weight.data)
  33. layer.attention.output.dense.weight.data = quantize_weight(
  34. layer.attention.output.dense.weight.data)
  35. if preln:
  36. layer.intermediate.dense_act.weight.data = quantize_weight(
  37. layer.intermediate.dense_act.weight.data)
  38. else:
  39. layer.intermediate.dense.weight.data = quantize_weight(
  40. layer.intermediate.dense.weight.data)
  41. layer.output.dense.weight.data = quantize_weight(layer.output.dense.weight.data)
  42. def quantize_fn(child):
  43. if megatron:
  44. # Quantize megatron GPT2 / GPT3 trained model
  45. megatron_layer_quantize(child)
  46. else:
  47. # Quantize either DeepSpeed or HuggingFace trained model
  48. bert_layer_quantize(child)
  49. return child
  50. return quantize_module(model=model,
  51. orig_class=orig_layer_impl,
  52. quantize_fn=quantize_fn)
  53. def quantize_module(model, orig_class, quantize_fn):
  54. policy = {orig_class: quantize_fn}
  55. return _quantize_module(model, policy)
  56. def _quantize_module(model, policies):
  57. for name, child in model.named_children():
  58. if child.__class__ in policies:
  59. orig = repr(child)
  60. setattr(model, name, policies[child.__class__](child))
  61. new = getattr(model, name)
  62. else:
  63. _quantize_module(child, policies)
  64. return model