MCPcopy Create free account
hub / github.com/kyegomez/BitNet / forward

Method forward

bitnet/bit_llama.py:464–499  ·  view source on GitHub ↗

Perform a forward pass through the Transformer model. Args: tokens (torch.Tensor): Input token indices. start_pos (int): Starting position for attention caching. Returns: torch.Tensor: Output logits after applying the Transformer model.

(self, tokens: torch.Tensor, start_pos: int)

Source from the content-addressed store, hash-verified

462
463 @torch.inference_mode()
464 def forward(self, tokens: torch.Tensor, start_pos: int):
465 """
466 Perform a forward pass through the Transformer model.
467
468 Args:
469 tokens (torch.Tensor): Input token indices.
470 start_pos (int): Starting position for attention caching.
471
472 Returns:
473 torch.Tensor: Output logits after applying the Transformer model.
474
475 """
476 _bsz, seqlen = tokens.shape
477 h = self.tok_embeddings(tokens)
478 self.freqs_cis = self.freqs_cis.to(h.device)
479 freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen]
480
481 mask = None
482 if seqlen > 1:
483 mask = torch.full((seqlen, seqlen), float("-inf"), device=tokens.device)
484
485 mask = torch.triu(mask, diagonal=1)
486
487 # When performing key-value caching, we compute the attention scores
488 # only for the new sequence. Thus, the matrix of scores is of size
489 # (seqlen, cache_len + seqlen), and the only masked entries are (i, j) for
490 # j > cache_len + i, since row i corresponds to token cache_len + i.
491 mask = torch.hstack(
492 [torch.zeros((seqlen, start_pos), device=tokens.device), mask]
493 ).type_as(h)
494
495 for layer in self.layers:
496 h = layer(h, start_pos, freqs_cis, mask)
497 h = self.norm(h)
498 output = self.output(h).float()
499 return output

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected