MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / encode

Method encode

lit_gpt/tokenizer.py:102–146  ·  view source on GitHub ↗
(
        self,
        string: str,
        device: Optional[torch.device] = None,
        bos: bool = False,
        eos: bool = False,
        max_length=-1,
    )

Source from the content-addressed store, hash-verified

100 return id_
101
102 def encode(
103 self,
104 string: str,
105 device: Optional[torch.device] = None,
106 bos: bool = False,
107 eos: bool = False,
108 max_length=-1,
109 ) -> torch.Tensor:
110 if self.backend == HUGGINGFACE:
111 # add_special_tokens=False as we want to manually handle it later.
112 tokens = self.processor.encode(string, add_special_tokens=False).ids
113 elif self.backend == SENTENCEPIECE:
114 raise RuntimeError
115 tokens = self.processor.encode(string)
116 else:
117 raise RuntimeError
118
119 if bos:
120 bos_id = self.bos_id
121 if bos_id is None:
122 raise NotImplementedError(
123 'Attempted to add bos, but this tokenizer does not defined a bos token.'
124 )
125 tokens = [bos_id] + tokens
126
127 if max_length > 0:
128 if eos:
129 if self.eos_id is None:
130 raise NotImplementedError(
131 'Attempted to add eos, but this tokenizer does not defined an eos token'
132 )
133 if len(tokens) >= max_length:
134 tokens = tokens[:max_length]
135 tokens[-1] = self.eos_id
136 else:
137 tokens = tokens + [self.eos_id]
138 else:
139 if eos:
140 if self.eos_id is None:
141 raise NotImplementedError(
142 'Attempted to add eos, but this tokenizer does not defined an eos token'
143 )
144 tokens = tokens + [self.eos_id]
145
146 return torch.tensor(tokens, dtype=torch.int, device=device)
147
148 def decode(
149 self,

Callers 4

mainFunction · 0.95
get_lit_inferencesFunction · 0.95
__getitem__Method · 0.45
__getitem__Method · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected