MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / __init__

Method __init__

deepspeed/sequence/fpdt_layer.py:89–144  ·  view source on GitHub ↗
(self, tokens, labels, loss_mask, attention_mask, position_ids, args, sp_size, sp_rank)

Source from the content-addressed store, hash-verified

87 """
88
89 def __init__(self, tokens, labels, loss_mask, attention_mask, position_ids, args, sp_size, sp_rank) -> None:
90
91 super(FPDT_InputConstruct, self).__init__()
92 global_seq_len = tokens.shape[1]
93 batch_size = tokens.shape[0]
94 global_chunk_size = args.ds_sequence_parallel_fpdt_chunk_size
95 geometry = f"(S={global_seq_len}, C={global_chunk_size}, P={sp_size}, rank={sp_rank})"
96 if global_seq_len <= 0 or global_chunk_size <= 0 or sp_size <= 0:
97 raise ValueError(
98 f"FPDT sequence length, chunk size and sequence parallel size must be positive {geometry}")
99 if sp_rank < 0 or sp_rank >= sp_size:
100 raise ValueError(f"FPDT sequence parallel rank must be in [0, sp_size) {geometry}")
101 if global_chunk_size % sp_size != 0:
102 raise ValueError(f"FPDT chunk size must be divisible by the sequence parallel size {geometry}")
103
104 padding = (-global_seq_len) % global_chunk_size
105 if padding:
106 if loss_mask is None:
107 raise ValueError(f"FPDT padding requires a loss_mask to exclude padded tokens {geometry}")
108 if attention_mask is not None:
109 raise ValueError(f"FPDT padding requires attention_mask=None for causal attention {geometry}")
110
111 def append_zeros(tensor):
112 if tensor is None:
113 return None
114 shape = (tensor.shape[0], padding, *tensor.shape[2:])
115 return torch.cat((tensor, tensor.new_zeros(shape)), dim=1)
116
117 tokens = append_zeros(tokens)
118 # FPDT_LogitsLoss.backward directly indexes labels rather than honoring ignore_index=-100.
119 # Negative labels can select the wrong class or be out of range; label 0 with zero loss weight
120 # safely excludes padding.
121 labels = append_zeros(labels)
122 loss_mask = append_zeros(loss_mask)
123 if position_ids is not None:
124 offsets = torch.arange(1, padding + 1, dtype=position_ids.dtype, device=position_ids.device)
125 offsets = offsets.reshape(1, padding, *([1] * (position_ids.dim() - 2)))
126 position_ids = torch.cat((position_ids, position_ids[:, -1:] + offsets), dim=1)
127 global_seq_len += padding
128
129 self.tokens = tokens
130 self.labels = labels
131 self.loss_mask = loss_mask
132 self.attention_mask = attention_mask
133 self.position_ids = position_ids
134 num_chunk_per_gpu = global_seq_len // global_chunk_size
135 local_seq_len = global_seq_len // sp_size
136
137 self.num_chunk_per_gpu = num_chunk_per_gpu
138 self.chunk_size = local_seq_len // num_chunk_per_gpu
139 self.sp_size = sp_size
140 self.sp_rank = sp_rank
141 self.global_seq_len = global_seq_len
142 self.local_seq_len = local_seq_len
143 self.batch_size = batch_size
144 self.device = tokens.device
145
146 def generate(self):

Callers

nothing calls this directly

Calls 2

__init__Method · 0.45
reshapeMethod · 0.45

Tested by

no test coverage detected