vocab_parallel_embedding.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. from typing import Optional, Sequence
  2. import torch
  3. from torch.nn.parameter import Parameter
  4. from aphrodite.distributed import (
  5. divide,
  6. get_tensor_model_parallel_rank,
  7. get_tensor_model_parallel_world_size,
  8. tensor_model_parallel_all_reduce,
  9. )
  10. from aphrodite.modeling.layers.linear import UnquantizedLinearMethod
  11. from aphrodite.modeling.utils import set_weight_attrs
  12. DEFAULT_VOCAB_PADDING_SIZE = 64
  13. def pad_vocab_size(vocab_size: int,
  14. pad_to: int = DEFAULT_VOCAB_PADDING_SIZE) -> int:
  15. """Pad the vocab size to the given value."""
  16. return ((vocab_size + pad_to - 1) // pad_to) * pad_to
  17. def vocab_range_from_per_partition_vocab_size(per_partition_vocab_size: int,
  18. rank: int) -> Sequence[int]:
  19. index_f = rank * per_partition_vocab_size
  20. index_l = index_f + per_partition_vocab_size
  21. return index_f, index_l
  22. def vocab_range_from_global_vocab_size(global_vocab_size: int, rank: int,
  23. world_size: int) -> Sequence[int]:
  24. per_partition_vocab_size = divide(global_vocab_size, world_size)
  25. return vocab_range_from_per_partition_vocab_size(per_partition_vocab_size,
  26. rank)
  27. class VocabParallelEmbedding(torch.nn.Module):
  28. """Embedding parallelized in the vocabulary dimension.
  29. Adapted from torch.nn.Embedding, note that we pad the vocabulary size to
  30. make sure it is divisible by the number of model parallel GPUs.
  31. Args:
  32. num_embeddings: vocabulary size.
  33. embedding_dim: size of hidden state.
  34. params_dtype: type of the parameters.
  35. org_num_embeddings: original vocabulary size (without LoRA).
  36. padding_size: padding size for the vocabulary.
  37. """
  38. def __init__(self,
  39. num_embeddings: int,
  40. embedding_dim: int,
  41. params_dtype: Optional[torch.dtype] = None,
  42. linear_method=None,
  43. org_num_embeddings: Optional[int] = None,
  44. padding_size: int = DEFAULT_VOCAB_PADDING_SIZE,
  45. is_input_emb: bool = True):
  46. super().__init__()
  47. # Keep the input dimensions.
  48. self.num_embeddings = num_embeddings
  49. self.org_vocab_size = org_num_embeddings or num_embeddings
  50. self.num_embeddings_padded = pad_vocab_size(num_embeddings,
  51. padding_size)
  52. self.embedding_dim = embedding_dim
  53. if params_dtype is None:
  54. params_dtype = torch.get_default_dtype()
  55. self.tp_size = get_tensor_model_parallel_world_size()
  56. # Divide the weight matrix along the vocaburaly dimension.
  57. self.vocab_start_index, self.vocab_end_index = (
  58. vocab_range_from_global_vocab_size(
  59. self.num_embeddings_padded, get_tensor_model_parallel_rank(),
  60. self.tp_size))
  61. self.num_embeddings_per_partition = (self.vocab_end_index -
  62. self.vocab_start_index)
  63. idx = 0 if is_input_emb else 1
  64. if linear_method is None or not linear_method.quant_config.quant_vocab(
  65. )[idx]:
  66. linear_method = UnquantizedLinearMethod()
  67. self.linear_method = linear_method
  68. self.linear_method.create_weights(self, self.embedding_dim,
  69. [self.num_embeddings_per_partition],
  70. self.embedding_dim,
  71. self.num_embeddings_padded,
  72. params_dtype)
  73. def weight_loader(self, param: Parameter, loaded_weight: torch.Tensor):
  74. output_dim = getattr(param, "output_dim", None)
  75. packed_dim = getattr(param, "packed_dim", None)
  76. if output_dim is not None:
  77. shard_offset = self.vocab_start_index
  78. shard_size = min(self.vocab_end_index,
  79. self.org_vocab_size) - shard_offset
  80. if packed_dim == output_dim:
  81. shard_size = shard_size // param.pack_factor
  82. shard_offset = shard_offset // param.pack_factor
  83. loaded_weight = loaded_weight.narrow(output_dim, shard_offset,
  84. shard_size)
  85. if isinstance(param, torch.nn.parameter.UninitializedParameter):
  86. vocab_shape = list(loaded_weight.shape)
  87. if output_dim is not None:
  88. if packed_dim == output_dim:
  89. vocab_shape[output_dim] = (
  90. self.num_embeddings_per_partition // param.pack_factor)
  91. else:
  92. vocab_shape[output_dim] = self.num_embeddings_per_partition
  93. param.materialize(vocab_shape, dtype=loaded_weight.dtype)
  94. if output_dim is not None:
  95. param.data.narrow(
  96. output_dim, 0,
  97. loaded_weight.shape[output_dim]).copy_(loaded_weight)
  98. else:
  99. param.data.copy_(loaded_weight)
  100. def forward(self, input_):
  101. if self.tp_size > 1:
  102. # Build the mask.
  103. input_mask = ((input_ < self.vocab_start_index) |
  104. (input_ >= self.vocab_end_index))
  105. # Mask the input.
  106. masked_input = input_.clone() - self.vocab_start_index
  107. masked_input[input_mask] = 0
  108. else:
  109. masked_input = input_
  110. # Get the embeddings.
  111. output_parallel = self.linear_method.apply_embedding(
  112. self, masked_input)
  113. # output_parallel = F.embedding(masked_input, self.weight)
  114. # Mask the output embedding.
  115. if self.tp_size > 1:
  116. output_parallel[input_mask, :] = 0.0
  117. # Reduce across all the model parallel GPUs.
  118. output = tensor_model_parallel_all_reduce(output_parallel)
  119. return output
  120. class ParallelLMHead(VocabParallelEmbedding):
  121. """Parallelized LM head.
  122. Output logits weight matrices used in the Sampler. The weight and bias
  123. tensors are padded to make sure they are divisible by the number of
  124. model parallel GPUs.
  125. Args:
  126. num_embeddings: vocabulary size.
  127. embedding_dim: size of hidden state.
  128. bias: whether to use bias.
  129. params_dtype: type of the parameters.
  130. org_num_embeddings: original vocabulary size (without LoRA).
  131. padding_size: padding size for the vocabulary.
  132. """
  133. def __init__(self,
  134. num_embeddings: int,
  135. embedding_dim: int,
  136. bias: bool = False,
  137. params_dtype: Optional[torch.dtype] = None,
  138. linear_method=None,
  139. org_num_embeddings: Optional[int] = None,
  140. padding_size: int = DEFAULT_VOCAB_PADDING_SIZE):
  141. super().__init__(num_embeddings, embedding_dim, params_dtype,
  142. linear_method, org_num_embeddings, padding_size,
  143. False)
  144. if bias:
  145. self.bias = Parameter(
  146. torch.empty(self.num_embeddings_per_partition,
  147. dtype=params_dtype))
  148. set_weight_attrs(self.bias, {
  149. "output_dim": 0,
  150. "weight_loader": self.weight_loader
  151. })
  152. else:
  153. self.register_parameter("bias", None)
  154. def forward(self, input_):
  155. logits = self.linear_method.apply_weights(self, input_)
  156. if self.bias is not None:
  157. logits += self.bias
  158. return logits
  159. class ParallelTWEHead(torch.nn.Module):
  160. """Parallelized tie word embeddings head.
  161. Output logits weight matrices used in the Sampler. The weight and bias
  162. tensors are read from a VocabParallelEmbedding.
  163. Args:
  164. embeddings: the VocabParallelEmbedding to mirror
  165. """
  166. def __init__(self, embeddings: VocabParallelEmbedding):
  167. super().__init__()
  168. self.linear_method = embeddings.linear_method
  169. self.layer = embeddings
  170. def forward(self, input_):
  171. logits = self.linear_method.apply_weights(self.layer, input_)
  172. return logits