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

transformers version forward compatibility #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ There's a few way to use these HuggingFace weights, all with different flavors:
2. [Pytorch Lighting in this repo](#loadweights)
3. [standalone](#standalone)

Thanks to friends at HuggingFace, we also have versions of these checkpoints
that can be loaded utilizing the transformers library `AutoModel` and `AutoTokenizer` classes! This makes it super easy to load HyenaDNA models to use in your own codebase. You can access our collection of these checkpoints [here!](https://huggingface.co/collections/LongSafari/hyenadna-models-654d0cbbe113b04ba5a0f638)

## Dependencies
<a name="dependencies"></a>

Expand Down
36 changes: 23 additions & 13 deletions src/dataloaders/datasets/hg38_char_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
CharacterTokenzier for Hugging Face Transformers.
This is heavily inspired from CanineTokenizer in transformers package.
"""

import json
import os
from pathlib import Path
Expand All @@ -13,7 +14,13 @@


class CharacterTokenizer(PreTrainedTokenizer):
def __init__(self, characters: Sequence[str], model_max_length: int, padding_side: str='left', **kwargs):
def __init__(
self,
characters: Sequence[str],
model_max_length: int,
padding_side: str = "left",
**kwargs
):
"""Character tokenizer for Hugging Face transformers.
Args:
characters (Sequence[str]): List of desired characters. Any character which
Expand Down Expand Up @@ -41,6 +48,18 @@ def __init__(self, characters: Sequence[str], model_max_length: int, padding_sid

mask_token = AddedToken("[MASK]", lstrip=True, rstrip=False)

self._vocab_str_to_int = {
"[CLS]": 0,
"[SEP]": 1,
"[BOS]": 2,
"[MASK]": 3,
"[PAD]": 4,
"[RESERVED]": 5,
"[UNK]": 6,
**{ch: i + 7 for i, ch in enumerate(characters)},
}
self._vocab_int_to_str = {v: k for k, v in self._vocab_str_to_int.items()}

super().__init__(
bos_token=bos_token,
eos_token=sep_token,
Expand All @@ -55,17 +74,8 @@ def __init__(self, characters: Sequence[str], model_max_length: int, padding_sid
**kwargs,
)

self._vocab_str_to_int = {
"[CLS]": 0,
"[SEP]": 1,
"[BOS]": 2,
"[MASK]": 3,
"[PAD]": 4,
"[RESERVED]": 5,
"[UNK]": 6,
**{ch: i + 7 for i, ch in enumerate(characters)},
}
self._vocab_int_to_str = {v: k for k, v in self._vocab_str_to_int.items()}
def get_vocab(self) -> Dict[str, int]:
return self._vocab_str_to_int

@property
def vocab_size(self) -> int:
Expand Down Expand Up @@ -146,4 +156,4 @@ def from_pretrained(cls, save_directory: Union[str, os.PathLike], **kwargs):
cfg_file = Path(save_directory) / "tokenizer_config.json"
with open(cfg_file) as f:
cfg = json.load(f)
return cls.from_config(cfg)
return cls.from_config(cfg)
Loading