MCPcopy Create free account
hub / github.com/PABannier/sam3.cpp / encode

Method encode

scripts/verify_tokenizer.py:120–137  ·  view source on GitHub ↗
(self, text, context_length=32)

Source from the content-addressed store, hash-verified

118 return result
119
120 def encode(self, text, context_length=32):
121 # Clean + lowercase
122 text = re.sub(r"\s+", " ", text).strip().lower()
123
124 bpe_tokens = []
125 for token in re.findall(self.pat, text):
126 encoded = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
127 bpe_tokens.extend(
128 self.encoder[bpe_token] for bpe_token in self.bpe(encoded).split(" ")
129 )
130
131 # Build result: [SOT, tokens..., EOT, 0, 0, ...]
132 result = [self.sot_token] + bpe_tokens + [self.eot_token]
133 if len(result) > context_length:
134 result = result[:context_length]
135 result[-1] = self.eot_token
136 result += [0] * (context_length - len(result))
137 return result
138
139
140TEST_STRINGS = [

Callers 7

mainFunction · 0.95
write_tensorFunction · 0.80
write_tensorFunction · 0.80
write_tensorFunction · 0.80
write_tokenizerFunction · 0.80
mainFunction · 0.80
tokenize_promptFunction · 0.80

Calls 1

bpeMethod · 0.95

Tested by

no test coverage detected