MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / forward

Method forward

Language_Model/utils/embeddings.py:30–66  ·  view source on GitHub ↗

Embedding module for GPT-1. This module combines token and positional embeddings, to return the embeddings from a sequence of input tokens and positions. See Lecture 06, slides 30-32. Parameters ---------- tokens (`torch.LongTensor` of shape `(batch_

(self, tokens, positions)

Source from the content-addressed store, hash-verified

28 )
29
30 def forward(self, tokens, positions):
31 """Embedding module for GPT-1.
32
33 This module combines token and positional embeddings, to return the
34 embeddings from a sequence of input tokens and positions.
35 See Lecture 06, slides 30-32.
36
37 Parameters
38 ----------
39 tokens (`torch.LongTensor` of shape `(batch_size, sequence_length)`)
40 The input tensor containing the token sequences. All the tokens
41 must be integers in [0, vocabulary_size).
42
43 positions (`torch.LongTensor` of shape `(batch_size, sequence_length)`)
44 The tensor containing the position indices in the sequence. All
45 the positions must be integers in [0, sequence_length)
46
47 Returns
48 -------
49 embeddings (`torch.FloatTensor` of shape `(batch_size, sequence_length, embedding_size)`)
50 The tensor containing the embeddings. For example, `embeddings[0, 2]`
51 is the embedding vector for the token in 3rd position (index 2)
52 of the 1st sequence in the batch (index 0).
53 """
54 if torch.any(positions >= self.sequence_length):
55 raise RuntimeError(
56 "Some position indices are larger than the " "maximum sequence length."
57 )
58
59 if torch.any(tokens >= self.vocabulary_size):
60 raise RuntimeError(
61 "Some tokens are larger than the size of " "the vocabulary."
62 )
63
64 token_embeddings = self.tokens(tokens)
65 position_embeddings = self.position(positions)
66 return token_embeddings + position_embeddings
67
68 @classmethod
69 def load_embeddings_from(cls, filename):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected