MCPcopy Create free account
hub / github.com/OpenMOSS/MOSS / save_vocabulary

Method save_vocabulary

models/tokenization_moss.py:275–302  ·  view source on GitHub ↗
(self, save_directory: str, filename_prefix: Optional[str] = None)

Source from the content-addressed store, hash-verified

273 return text
274
275 def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
276 if not os.path.isdir(save_directory):
277 logger.error(f"Vocabulary path ({save_directory}) should be a directory")
278 return
279 vocab_file = os.path.join(
280 save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
281 )
282 merge_file = os.path.join(
283 save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]
284 )
285
286 with open(vocab_file, "w", encoding="utf-8") as f:
287 f.write(json.dumps(self.encoder, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
288
289 index = 0
290 with open(merge_file, "w", encoding="utf-8") as writer:
291 writer.write("#version: 0.2\n")
292 for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
293 if index != token_index:
294 logger.warning(
295 f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
296 " Please check that the tokenizer is not corrupted!"
297 )
298 index = token_index
299 writer.write(" ".join(bpe_tokens) + "\n")
300 index += 1
301
302 return vocab_file, merge_file
303
304 def prepare_for_tokenization(self, text, is_split_into_words=False, **kwargs):
305 add_prefix_space = kwargs.pop("add_prefix_space", self.add_prefix_space)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected