-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pass all unit tests * fix float type * fix quantization test
- Loading branch information
Showing
6 changed files
with
267 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
import os | ||
from logging import getLogger | ||
from typing import List | ||
|
||
from sentencepiece import SentencePieceProcessor | ||
|
||
|
||
"""Only use decode to do accuacy varification""" | ||
class Tokenizer: | ||
"""tokenizing and encoding/decoding text using SentencePiece.""" | ||
def __init__(self, model_path: str): | ||
""" | ||
Initializes the Tokenizer with a SentencePiece model. | ||
Args: | ||
model_path (str): The path to the SentencePiece model file. | ||
""" | ||
# reload tokenizer | ||
print(f"model_path: {model_path}") | ||
assert os.path.isfile(model_path), model_path | ||
self.sp_model = SentencePieceProcessor(model_file=model_path) | ||
|
||
# BOS / EOS token IDs | ||
self.n_words: int = self.sp_model.vocab_size() | ||
self.bos_id: int = self.sp_model.bos_id() | ||
self.eos_id: int = self.sp_model.eos_id() | ||
self.pad_id: int = self.sp_model.pad_id() | ||
|
||
assert self.sp_model.vocab_size() == self.sp_model.get_piece_size() | ||
|
||
def decode(self, t: List[int]) -> str: | ||
""" | ||
Decodes a list of token IDs into a string. | ||
Args: | ||
t (List[int]): The list of token IDs to be decoded. | ||
Returns: | ||
str: The decoded string. | ||
""" | ||
return self.sp_model.decode(t) |
Oops, something went wrong.