Skip to content

feat: Add some tokenizers and maximum token length flag - #657

Open
sjohn4 wants to merge 3 commits into
mainfrom
tokenizers
Open

feat: Add some tokenizers and maximum token length flag#657
sjohn4 wants to merge 3 commits into
mainfrom
tokenizers

Conversation

@sjohn4

@sjohn4 sjohn4 commented Apr 14, 2025

Copy link
Copy Markdown
Collaborator

Added the necessary code for the tokenizers, appropriate tests and the tokenizer length flag to decide sequence length in the pipeline.

@sjohn4
sjohn4 requested a review from MaxiBoether April 14, 2025 16:11
@github-actions

Copy link
Copy Markdown

Line Coverage: -% ( % to main)
Branch Coverage: -% ( % to main)

@sjohn4
sjohn4 force-pushed the tokenizers branch 2 times, most recently from 8fa124b to 240c06e Compare April 15, 2025 09:59
@codecov

codecov Bot commented Apr 15, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 97.95918% with 1 line in your changes missing coverage. Please review.

Project coverage is 85.62%. Comparing base (c961257) to head (6753cf2).

Files with missing lines Patch % Lines
...odyn/trainer_server/internal/dataset/data_utils.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #657      +/-   ##
==========================================
+ Coverage   85.58%   85.62%   +0.04%     
==========================================
  Files         258      261       +3     
  Lines       11378    11412      +34     
==========================================
+ Hits         9738     9772      +34     
  Misses       1640     1640              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MaxiBoether MaxiBoether left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks John. Here I had some more comments

Comment thread environment.yml
- evidently==0.4.27
- alibi-detect==0.12.*
- tenacity
- sentencepiece

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere in this PR?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The T5 tokenizer needs it to work, it is not used in the code but it is needed.

Comment on lines +9 to +12
Adapted from WildTime's initialize_distilbert_transform
Here you can find the original implementation:
https://github.com/huaxiuyao/Wild-Time/blob/main/wildtime/data/utils.py
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment should probably be moved to the new HFTokenizerTransform, since that one is based on this original impl

"""Tokenizer transform class for the T5 model.

Args:
max_token_length: Maximum length for tokenization (default: 300).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default 300 it says here but it is set to 128

Comment on lines +21 to +38
def __call__(self, sample: str) -> torch.Tensor:
"""
Tokenize the input sample and return a tensor with input_ids and attention_mask.
Args:
sample: Input string to tokenize.
Returns:
A torch.Tensor with shape (max_token_length, 2), where:
- dim 0 is the token sequence length.
- dim 1 contains input_ids and attention_mask.
"""
if not sample: # This is needed here since this tokenizer cannot handle an empty list
sample = self.tokenizer.eos_token
tokens = self.tokenizer(
sample, padding="max_length", truncation=True, max_length=self.max_token_length, return_tensors="pt"
)
data = torch.stack((tokens["input_ids"], tokens["attention_mask"]), dim=2)
data = torch.squeeze(data, dim=0)
return data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. GPT2 usses huggingface, so we should use the HFTokenizerTransform. The only difference I can see is the if not sample code path. Why is that needed? The tokenizer should never get an empty string. Even in that case, right here you set sample = token, and then pass this token to the tokenizer, which does not make sense. I don't understand why this extra case is needed, but if it's needed, it affects all HF tokenizers and should probably live in the parent class

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last, if it truly only affects this tokenizer and is needed, you can check sample and call the superclass's call function instead of re-implementing the tokenization call

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right it does not make sense to put the token in the tokenizer, I think you can tecnically recieve an empty string if you have a generative "target" that is missing, which could happen in some datasets. all other tokenizers have a token for an empty string but not gpt2, so it is the only one that needs this.

Comment thread modyn/protos/trainer_server.proto Outdated
bool enable_accurate_gpu_measurements = 25;
int64 record_loss_every = 26;
bool drop_last_batch = 27;
int64 tokenizer_sequence_length =28;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this as max_token_length and I guess we should name it that way. This flag does not guarantee padding, does it? I.e., if it is set to 2048 and I have a short string, are we guaranteed to have padding? It should probably also be an optional int64 since not every training uses a tokenizer

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does guarantee padding up to the tokenizer sequence length.

) -> torch.utils.data.DataLoader:
# TODO(#289): Replace inefficient per class dataloader
dataset = PerClassOnlineDataset(
dataset = PerClassOnlineDataset( # type: ignore # pylint: disable=too-many-function-args

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use type: ignore unless necessary. If there is a typing issue we need to fix the typing issue

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved, not sure how though

shuffle: bool,
tokenizer: str | None,
log_path: pathlib.Path | None,
sequence_length: int = 128,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to max_token_length if that is truly what this is doing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also probably this is int | None since not every training uses a tokenizer

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self.tokenizer: str | None = request.tokenizer.value if request.HasField("tokenizer") else None

self.offline_dataset_path = offline_dataset_path
self.tokenizer_seq_length: int = request.tokenizer_sequence_length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int | None? (handling similar to e.g. how self.tokenizer is set)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread modyn/protos/trainer_server.proto Outdated
bool enable_accurate_gpu_measurements = 25;
int64 record_loss_every = 26;
bool drop_last_batch = 27;
int64 tokenizer_sequence_length =28;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another question, I actually don't see this being used anywhere in the supervisor, i.e., all requests we send out right now don't set this. we should also (similar how you did it for grad clipping) add this to the pipeline config and forward it, no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey yes it seems like I deleted that line somehow, I put it back :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@MaxiBoether MaxiBoether changed the title Tokenizers and length flag added feat: Add some tokenizers and maximum token length flag Apr 23, 2025
@sjohn4
sjohn4 requested a review from MaxiBoether April 25, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants