Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for tokenizer.py #41

Merged
merged 18 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ml_mdm/language_models/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from mlx.data.core import CharTrie


def read_dictionary_bert(token_file):
def read_dictionary_bert(vocab_file):
trie_key_scores = []
trie = CharTrie()

f = open(token_file, "rb")
f = open(vocab_file, "rb")
sep = "\u2581".encode()

max_score = 0
Expand Down Expand Up @@ -42,11 +42,11 @@ def read_dictionary_bert(token_file):
return trie, trie_key_scores, eos, bos, pad


def read_dictionary_t5(token_file):
def read_dictionary_t5(vocab_file):
trie_key_scores = []
trie = CharTrie()

f = open(token_file, "rb")
f = open(vocab_file, "rb")
sep = "\u2581".encode()

max_score = 0
Expand Down Expand Up @@ -75,7 +75,7 @@ def read_dictionary_t5(token_file):
return trie, trie_key_scores, eos, bos, pad


def read_dictionary(token_file):
def read_dictionary(vocab_file):
trie_key_scores = []
trie = CharTrie()

Expand All @@ -85,7 +85,7 @@ def read_dictionary(token_file):
trie.insert(token)
trie_key_scores.append(0.0)

f = open(token_file, "rb")
f = open(vocab_file, "rb")
sep = "\u2581".encode()

max_score = 0
Expand Down Expand Up @@ -130,31 +130,31 @@ def read_dictionary(token_file):


class Tokenizer:
def __init__(self, token_file, mode=None):
def __init__(self, vocab_file, mode=None):
if mode == "t5":
(
self._trie,
self._trie_key_scores,
self.eos,
self.bos,
self.pad,
) = read_dictionary_t5(token_file)
) = read_dictionary_t5(vocab_file)
elif mode == "bert":
(
self._trie,
self._trie_key_scores,
self.eos,
self.bos,
self.pad,
) = read_dictionary_bert(token_file)
) = read_dictionary_bert(vocab_file)
else:
(
self._trie,
self._trie_key_scores,
self.eos,
self.bos,
self.pad,
) = read_dictionary(token_file)
) = read_dictionary(vocab_file)
self.vocab_size = self._trie.num_keys()

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ def test_config_cc12m_1024x1024():
mode="demo",
additional_config_paths=[f],
)
assert args
assert args
3 changes: 3 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ def test_process_text():
)
assert len(tokens) > 0
assert len(tokens[0]) > 0


test_get_dataset()
23 changes: 23 additions & 0 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All rights reserved.

import logging

from pathlib import Path
from ml_mdm.language_models.tokenizer import Tokenizer # Tokenizer class from tokenizer.py

def test_tokenizer_bert():
f = Path(__file__).parent/"data/bert.vocab" # To solve from relative to absolute import
assert Tokenizer(f, mode="bert")

def test_tokenizer_t5():
f = Path(__file__).parent/"data/t5.vocab"
assert Tokenizer(f, mode="tf")

def test_tokenizer():
f = Path(__file__).parent/"data/imagenet.vocab"
assert Tokenizer(f)

test_tokenizer_bert()
test_tokenizer_t5()
test_tokenizer()