[MRV2][Performance] Skip no-op FP32 logits materialization (#47711)
Signed-off-by: jesse <szxfml@gmail.com> Signed-off-by: Song Zhixin <szxfml@gmail.com> Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
co-authored by
Wentao Ye
Jee Jee Li
parent
27d7061ef6
commit
d2ca3002d9
@@ -152,6 +152,9 @@ class Sampler:
|
||||
expanded_local_pos: torch.Tensor,
|
||||
skip_top_k_top_p: bool = False,
|
||||
) -> torch.Tensor:
|
||||
if not self._requires_logits_processing(idx_mapping_np):
|
||||
return logits
|
||||
|
||||
# Copy logits to a new FP32 tensor.
|
||||
logits = torch.empty_like(logits, dtype=torch.float32).copy_(logits)
|
||||
|
||||
@@ -194,6 +197,24 @@ class Sampler:
|
||||
logits, expanded_idx_mapping, idx_mapping_np
|
||||
)
|
||||
|
||||
def _requires_logits_processing(self, idx_mapping_np: np.ndarray) -> bool:
|
||||
if np.any(self.logit_bias_state.use_logit_bias[idx_mapping_np]):
|
||||
return True
|
||||
if np.any(self.penalties_state.use_penalty[idx_mapping_np]):
|
||||
return True
|
||||
if np.any(self.bad_words_state.num_bad_words.np[idx_mapping_np] > 0):
|
||||
return True
|
||||
|
||||
states = self.sampling_states
|
||||
temperatures = states.temperature.np[idx_mapping_np]
|
||||
if np.any((temperatures != 0.0) & (temperatures != 1.0)):
|
||||
return True
|
||||
if np.any(states.min_p.np[idx_mapping_np] != 0.0):
|
||||
return True
|
||||
if np.any(states.top_k.np[idx_mapping_np] != states.vocab_size):
|
||||
return True
|
||||
return bool(np.any(states.top_p.np[idx_mapping_np] != 1.0))
|
||||
|
||||
def sample(
|
||||
self,
|
||||
logits: torch.Tensor,
|
||||
|
||||
Reference in New Issue
Block a user