|
| 1 | +""" |
| 2 | +PyTorch BLOOM model that implements several memory-efficient modes. |
| 3 | +Based on https://github.com/huggingface/transformers/commit/ca2a55e9dfb245527b5e1c954fec6ffbb7aef07b |
| 4 | +See commit history for authorship. |
| 5 | +""" |
| 6 | + |
| 7 | +import platform |
| 8 | + |
| 9 | +import psutil |
| 10 | +import torch |
| 11 | +import torch.nn.functional as F |
| 12 | +import torch.utils.checkpoint |
| 13 | +from hivemind import get_logger |
| 14 | +from torch import nn |
| 15 | +from transformers import BloomConfig |
| 16 | + |
| 17 | +logger = get_logger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class LMHead(nn.Module): |
| 21 | + """ |
| 22 | + The modified language modeling head which does not create extra tensor for the linear layer with weights tied to the input |
| 23 | + embeddings. Thus, it reduces initial memory consumption which might be crucial for large dictionaries. |
| 24 | + In addition, it provides an effcient way to deal with half-precision word embeddings on CPU. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, config: BloomConfig, word_embeddings: nn.Embedding): |
| 28 | + super().__init__() |
| 29 | + self.word_embeddings = word_embeddings |
| 30 | + |
| 31 | + self.use_chunked_forward = config.use_chunked_forward |
| 32 | + if self.use_chunked_forward == "auto": |
| 33 | + if platform.machine() == "x86_64": |
| 34 | + # Import of cpufeature may crash on non-x86_64 machines |
| 35 | + from cpufeature import CPUFeature |
| 36 | + |
| 37 | + # If the CPU supports AVX512, plain bfloat16 is ~10x faster than chunked_forward(). |
| 38 | + # Otherwise, it's ~8x slower. |
| 39 | + self.use_chunked_forward = not (CPUFeature["AVX512f"] and CPUFeature["OS_AVX512"]) |
| 40 | + else: |
| 41 | + self.use_chunked_forward = True |
| 42 | + self.chunked_forward_step = config.chunked_forward_step |
| 43 | + self._bf16_warning_shown = False |
| 44 | + |
| 45 | + @property |
| 46 | + def in_features(self) -> int: |
| 47 | + return self.word_embeddings.num_embeddings |
| 48 | + |
| 49 | + @property |
| 50 | + def out_features(self) -> int: |
| 51 | + return self.word_embeddings.embedding_dim |
| 52 | + |
| 53 | + @property |
| 54 | + def weight(self): |
| 55 | + return self.word_embeddings.weight |
| 56 | + |
| 57 | + @property |
| 58 | + def bias(self): |
| 59 | + return None |
| 60 | + |
| 61 | + def forward(self, hidden_states): |
| 62 | + word_embeddings = self.word_embeddings.weight |
| 63 | + |
| 64 | + if ( |
| 65 | + word_embeddings.dtype in [torch.float16, torch.bfloat16] |
| 66 | + and word_embeddings.device.type == "cpu" |
| 67 | + and self.use_chunked_forward |
| 68 | + ): |
| 69 | + lm_logits = self.chunked_forward(hidden_states) |
| 70 | + else: |
| 71 | + # Switch dtype in case word_embeddings are fp16/bf16 |
| 72 | + hidden_states = hidden_states.to(word_embeddings.dtype) |
| 73 | + lm_logits = F.linear(hidden_states, word_embeddings) |
| 74 | + return lm_logits |
| 75 | + |
| 76 | + def chunked_forward(self, hidden_states): |
| 77 | + """Splits word embeddings on chunks and iteratively casts them into fp32 to perform matmul more efficiently on CPU. |
| 78 | + chunked_forward_step: provides trade-off between efficiency and extra memory consumption. |
| 79 | + """ |
| 80 | + assert self.chunked_forward_step > 0, "Chunk size for chunked forward must be positive" |
| 81 | + |
| 82 | + if not self._bf16_warning_shown: |
| 83 | + if self.word_embeddings.weight.numel() * 4 < 0.9 * psutil.virtual_memory().total: |
| 84 | + logger.warning( |
| 85 | + "Running the client with dtype bfloat16 on CPU may be slow, since your CPU doesn't support AVX512. " |
| 86 | + "Consider loading the model with torch_dtype='float32'" |
| 87 | + ) |
| 88 | + self._bf16_warning_shown = True |
| 89 | + |
| 90 | + word_embeddings = self.word_embeddings.weight |
| 91 | + num_embeddings = self.word_embeddings.num_embeddings |
| 92 | + |
| 93 | + hidden_states = hidden_states.float() |
| 94 | + output = torch.empty(*hidden_states.shape[:-1], num_embeddings) |
| 95 | + |
| 96 | + for i in range(0, num_embeddings, self.chunked_forward_step): |
| 97 | + chunk = word_embeddings[i : i + self.chunked_forward_step].float() |
| 98 | + output[..., i : i + self.chunked_forward_step] = F.linear(hidden_states, chunk) |
| 99 | + return output |
0 commit comments