bitsandbytes-rocm/bitsandbytes/nn/modules.py

314 lines
9.1 KiB
Python
Raw Normal View History

# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
2021-10-06 02:16:20 +00:00
# LICENSE file in the root directory of this source tree.
from typing import (
Any,
Callable,
Dict,
Iterator,
Mapping,
Optional,
Set,
Tuple,
TypeVar,
Union,
overload,
)
2021-10-06 02:16:20 +00:00
import torch
2021-10-06 02:16:20 +00:00
import torch.nn.functional as F
from torch import Tensor, device, dtype, nn
from torch.nn.parameter import Parameter
2021-10-06 02:16:20 +00:00
import bitsandbytes as bnb
2021-10-06 02:16:20 +00:00
from bitsandbytes.optim import GlobalOptimManager
T = TypeVar("T", bound="torch.nn.Module")
2022-07-22 21:41:05 +00:00
2021-10-06 02:16:20 +00:00
class StableEmbedding(torch.nn.Embedding):
def __init__(
self,
num_embeddings: int,
embedding_dim: int,
padding_idx: Optional[int] = None,
max_norm: Optional[float] = None,
norm_type: float = 2.0,
scale_grad_by_freq: bool = False,
sparse: bool = False,
_weight: Optional[Tensor] = None,
) -> None:
super(StableEmbedding, self).__init__(
num_embeddings,
embedding_dim,
padding_idx,
max_norm,
norm_type,
scale_grad_by_freq,
sparse,
_weight,
)
2021-10-06 02:16:20 +00:00
self.norm = torch.nn.LayerNorm(embedding_dim)
GlobalOptimManager.get_instance().register_module_override(
self, "weight", {"optim_bits": 32}
)
2021-10-06 02:16:20 +00:00
def reset_parameters(self) -> None:
torch.nn.init.xavier_uniform_(self.weight)
self._fill_padding_idx_with_zero()
""" !!! This is a redefinition of _fill_padding_idx_with_zero in torch.nn.Embedding
2021-10-06 02:16:20 +00:00
to make the Layer compatible with Pytorch < 1.9.
This means that if this changes in future PyTorch releases this need to change too
which is cumbersome. However, with this we can ensure compatibility with previous
PyTorch releases.
"""
2021-10-06 02:16:20 +00:00
def _fill_padding_idx_with_zero(self) -> None:
if self.padding_idx is not None:
with torch.no_grad():
self.weight[self.padding_idx].fill_(0)
def forward(self, input: Tensor) -> Tensor:
emb = F.embedding(
input,
self.weight,
self.padding_idx,
self.max_norm,
self.norm_type,
self.scale_grad_by_freq,
self.sparse,
)
2021-10-06 02:16:20 +00:00
return self.norm(emb)
class Embedding(torch.nn.Embedding):
def __init__(
self,
num_embeddings: int,
embedding_dim: int,
padding_idx: Optional[int] = None,
max_norm: Optional[float] = None,
norm_type: float = 2.0,
scale_grad_by_freq: bool = False,
sparse: bool = False,
_weight: Optional[Tensor] = None,
) -> None:
super(Embedding, self).__init__(
num_embeddings,
embedding_dim,
padding_idx,
max_norm,
norm_type,
scale_grad_by_freq,
sparse,
_weight,
)
GlobalOptimManager.get_instance().register_module_override(
self, "weight", {"optim_bits": 32}
)
def reset_parameters(self) -> None:
torch.nn.init.xavier_uniform_(self.weight)
self._fill_padding_idx_with_zero()
""" !!! This is a redefinition of _fill_padding_idx_with_zero in torch.nn.Embedding
to make the Layer compatible with Pytorch < 1.9.
This means that if this changes in future PyTorch releases this need to change too
which is cumbersome. However, with this we can ensure compatibility with previous
PyTorch releases.
"""
def _fill_padding_idx_with_zero(self) -> None:
if self.padding_idx is not None:
with torch.no_grad():
self.weight[self.padding_idx].fill_(0)
def forward(self, input: Tensor) -> Tensor:
emb = F.embedding(
input,
self.weight,
self.padding_idx,
self.max_norm,
self.norm_type,
self.scale_grad_by_freq,
self.sparse,
)
return emb
2022-07-22 21:41:05 +00:00
2022-07-22 21:41:05 +00:00
class Int8Params(torch.nn.Parameter):
def __new__(
cls,
data=None,
requires_grad=True,
has_fp16_weights=False,
CB=None,
SCB=None,
2022-08-23 20:39:54 +00:00
SCBt=None,
):
2022-07-22 21:41:05 +00:00
cls.has_fp16_weights = has_fp16_weights
cls.CB = None
cls.SCB = None
2022-08-23 20:39:54 +00:00
cls.SCBt = None
2022-07-22 21:41:05 +00:00
if data is None:
data = torch.empty(0)
return torch.Tensor._make_subclass(cls, data, requires_grad)
def cuda(self, device):
if self.has_fp16_weights:
return super().cuda(device)
else:
# we store the 8-bit rows-major weight
# we convert this weight to the turning/ampere weight during the first inference pass
B = self.data.contiguous().half().cuda(device)
CB, CBt, SCB, SCBt, coo_tensorB = bnb.functional.double_quant(B)
del CBt
self.data = CB
setattr(self, "CB", CB)
setattr(self, "SCB", SCB)
2022-08-23 20:39:54 +00:00
setattr(self, "SCBt", SCBt)
2022-07-22 21:41:05 +00:00
return self
@overload
def to(
self: T,
device: Optional[Union[int, device]] = ...,
dtype: Optional[Union[dtype, str]] = ...,
non_blocking: bool = ...,
) -> T:
2022-07-22 21:41:05 +00:00
...
@overload
def to(self: T, dtype: Union[dtype, str], non_blocking: bool = ...) -> T:
...
@overload
def to(self: T, tensor: Tensor, non_blocking: bool = ...) -> T:
...
def to(self, *args, **kwargs):
device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to(
*args, **kwargs
)
if (
device is not None
and device.type == "cuda"
and self.data.device.type == "cpu"
):
return self.cuda(device)
2022-07-22 21:41:05 +00:00
else:
new_param = Int8Params(
super().to(
device=device, dtype=dtype, non_blocking=non_blocking
),
requires_grad=self.requires_grad,
has_fp16_weights=self.has_fp16_weights,
)
2022-07-22 21:41:05 +00:00
new_param.CB = self.CB
new_param.SCB = self.SCB
2022-08-23 20:39:54 +00:00
new_param.SCB = self.SCBt
2022-07-22 21:41:05 +00:00
return new_param
class Linear8bitLt(nn.Linear):
def __init__(
self,
input_features,
output_features,
bias=True,
has_fp16_weights=True,
threshold=0.0,
index=None,
):
super(Linear8bitLt, self).__init__(
input_features, output_features, bias
)
2022-07-22 21:41:05 +00:00
self.state = bnb.MatmulLtState()
self.index = index
2022-07-22 21:41:05 +00:00
self.state.threshold = threshold
self.state.has_fp16_weights = has_fp16_weights
if threshold > 0.0 and not has_fp16_weights:
self.state.use_pool = True
2022-08-16 19:00:54 +00:00
self.weight = Int8Params(self.weight.data, has_fp16_weights=has_fp16_weights)
2022-07-22 21:41:05 +00:00
def init_8bit_state(self):
self.state.CB = self.weight.CB
self.state.SCB = self.weight.SCB
2022-08-23 20:39:54 +00:00
self.state.SCBt = self.weight.SCBt
2022-07-22 21:41:05 +00:00
self.weight.CB = None
self.weight.SCB = None
2022-08-23 20:39:54 +00:00
self.weight.SCBt = None
2022-07-22 21:41:05 +00:00
def forward(self, x):
self.state.is_training = self.training
if self.weight.CB is not None:
self.init_8bit_state()
# weights are cast automatically as Int8Params, but the bias has to be cast manually
if self.bias is not None and self.bias.dtype != torch.float16:
2022-08-16 19:00:54 +00:00
self.bias.data = self.bias.data.half()
2022-07-22 21:41:05 +00:00
2022-08-16 19:00:54 +00:00
out = bnb.matmul(x, self.weight, bias=self.bias, state=self.state)
2022-07-22 21:41:05 +00:00
2022-08-23 20:39:54 +00:00
# if not self.state.has_fp16_weights and self.state.CB is not None:
2022-07-22 21:41:05 +00:00
# we converted 8-bit row major to turing/ampere format in the first inference pass
# we no longer need the row-major weight
2022-08-23 20:39:54 +00:00
# del self.state.CB
# self.weight.data = self.state.CxB
2022-07-22 21:41:05 +00:00
return out
2022-07-22 21:41:05 +00:00
class Linear8bit(nn.Linear):
def __init__(
self,
input_features,
output_features,
bias=True,
quant_type="vector",
index=None,
args=None,
sparse_decomp=False,
):
2022-07-22 21:41:05 +00:00
super(Linear8bit, self).__init__(input_features, output_features, bias)
self.quant_type = quant_type
self.index = index
self.args = args
self.iter = 0
def forward(self, x):
self.iter += 1
if self.iter % self.args.clip_freq == 0:
with torch.no_grad():
maxval, maxidx = torch.topk(
torch.abs(self.weight.flatten()), k=self.args.clip_idx
)
2022-07-22 21:41:05 +00:00
if not dist.is_initialized() or dist.get_rank() == 0:
print("clip", maxval[-1].item())
2022-07-22 21:41:05 +00:00
self.weight.clip_(-maxval[-1], maxval[-1])
if self.args is not None:
out = bnb.nn.functional.sparse_decomposed_linear8bit(
x,
self.weight,
self.bias,
qval=self.args.sparse_decomp_val,
quant_type=self.args.quant_type,
)
2022-07-22 21:41:05 +00:00
else:
out = bnb.nn.functional.linear8bit(
x, self.weight, self.bias, quant_type=self.args.quant_type
)
2022-07-22 21:41:05 +00:00
return out