MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / Linear8bitLt

Class Linear8bitLt

bitsandbytes/nn/modules.py:1018–1194  ·  view source on GitHub ↗

This class is the base module for the [LLM.int8()](https://arxiv.org/abs/2208.07339) algorithm. To read more about it, have a look at the paper. In order to quantize a linear layer one should first load the original fp16 / bf16 weights into the Linear8bitLt module, then call `int8_

Source from the content-addressed store, hash-verified

1016
1017
1018class Linear8bitLt(nn.Linear):
1019 """
1020 This class is the base module for the [LLM.int8()](https://arxiv.org/abs/2208.07339) algorithm.
1021 To read more about it, have a look at the paper.
1022
1023 In order to quantize a linear layer one should first load the original fp16 / bf16 weights into
1024 the Linear8bitLt module, then call `int8_module.to("cuda")` to quantize the fp16 weights.
1025
1026 Example:
1027
1028 ```python
1029 import torch
1030 import torch.nn as nn
1031
1032 import bitsandbytes as bnb
1033 from bitsandbytes.nn import Linear8bitLt
1034
1035 fp16_model = nn.Sequential(
1036 nn.Linear(64, 64),
1037 nn.Linear(64, 64)
1038 )
1039
1040 int8_model = nn.Sequential(
1041 Linear8bitLt(64, 64, has_fp16_weights=False),
1042 Linear8bitLt(64, 64, has_fp16_weights=False)
1043 )
1044
1045 int8_model.load_state_dict(fp16_model.state_dict())
1046 int8_model = int8_model.to(0) # Quantization happens here
1047 ```
1048 """
1049
1050 def __init__(
1051 self,
1052 input_features: int,
1053 output_features: int,
1054 bias=True,
1055 has_fp16_weights=True,
1056 threshold=0.0,
1057 index=None,
1058 device=None,
1059 ):
1060 """
1061 Initialize Linear8bitLt class.
1062
1063 Args:
1064 input_features (`int`):
1065 Number of input features of the linear layer.
1066 output_features (`int`):
1067 Number of output features of the linear layer.
1068 bias (`bool`, defaults to `True`):
1069 Whether the linear class uses the bias term as well.
1070 has_fp16_weights (`bool`, defaults to `True`):
1071 If False, weights are quantized to int8 on ``.to(device)``. If True,
1072 weights remain in fp16 and are quantized on-the-fly during each forward pass.
1073 threshold (`float`, defaults to `0.0`):
1074 Outlier threshold for mixed-precision decomposition (LLM.int8()). During the
1075 forward pass, activation columns where any value exceeds this threshold are

Calls

no outgoing calls