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

Method encode

conftest.py:30–66  ·  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

28 eos_id = 2
29
30 def encode(
31 self,
32 string: str,
33 device: Optional[torch.device] = None,
34 bos: bool = False,
35 eos: bool = False,
36 max_length=-1,
37 ) -> torch.Tensor:
38 tokens = [ord(c) for c in string]
39 if bos:
40 bos_id = self.bos_id
41 if bos_id is None:
42 raise NotImplementedError(
43 'Attempted to add bos, but this tokenizer does not defined a bos token.'
44 )
45 tokens = [bos_id] + tokens
46
47 if max_length > 0:
48 if eos:
49 if self.eos_id is None:
50 raise NotImplementedError(
51 'Attempted to add eos, but this tokenizer does not defined an eos token'
52 )
53 if len(tokens) >= max_length:
54 tokens = tokens[:max_length]
55 tokens[-1] = self.eos_id
56 else:
57 tokens = tokens + [self.eos_id]
58 else:
59 if eos:
60 if self.eos_id is None:
61 raise NotImplementedError(
62 'Attempted to add eos, but this tokenizer does not defined an eos token'
63 )
64 tokens = tokens + [self.eos_id]
65
66 return torch.tensor(tokens)
67
68 def decode(self, tokens: torch.Tensor) -> str:
69 decoded = ''

Callers 13

mainFunction · 0.45
test_single_bos_eosMethod · 0.45
_build_dataMethod · 0.45
main.pyFile · 0.45
validateFunction · 0.45
__call__Method · 0.45
added_tokensMethod · 0.45
sentencepiece_tokensMethod · 0.45
added_tokensMethod · 0.45
hf_tokensMethod · 0.45
added_tokensMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected