-
Notifications
You must be signed in to change notification settings - Fork 1
add quality score script #69
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ec095b2
add quality score script
53c7eed
format script
0039354
remove min and max from normalize because std python buildin
40452c6
lookup special tokens from tokenizer.json rather than hardcoding them
2bb4095
update formatting
492e1e8
load tokenizer outside map function
c99f1c4
add more cpu to map
4a9674b
change sep to padding
2035333
output path from argv
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,66 @@ | ||
| import json | ||
| import sys | ||
|
|
||
| import datasets | ||
| import numpy as np | ||
|
|
||
|
|
||
| def map_tokens_to_dataset(dataset, tokenizer): | ||
| jtp_out_of_range_token = tokenizer['model']['vocab']['JUMP_ADDR_EXCEEDED'] | ||
| jtp_unknown = tokenizer['model']['vocab']['UNK_JUMP_ADDR'] | ||
| minlength = max(jtp_out_of_range_token, jtp_unknown) + 1 | ||
| token = np.asarray(dataset['input_ids']).ravel() | ||
| token_to_id = {t['content']: t['id'] for t in tokenizer['added_tokens']} | ||
| padding_token = token_to_id['[PAD]'] | ||
| token = token[token != padding_token] | ||
| bincount = np.bincount(token, minlength=minlength) | ||
|
|
||
| return { | ||
| 'len_cfg': len(token), | ||
| 'jtp_in_range': int(bincount[:512].sum()), | ||
| 'jtp_out_of_range': int(bincount[jtp_out_of_range_token]), # Jump target exceeded token (from tokenizer) | ||
| 'jtp_unknown': int(bincount[jtp_unknown]), # Jump Target Unknown token (from tokenizer) | ||
| } | ||
|
|
||
|
|
||
| def make_scorer(dataset): | ||
|
|
||
| cfg_info = { | ||
| col: {'min': min(dataset[col]), 'max': max(dataset[col])} | ||
| for col in ['len_cfg', 'jtp_in_range', 'jtp_unknown', 'jtp_out_of_range'] | ||
| } | ||
|
|
||
| def normalize(val, col): | ||
| mn, mx = cfg_info[col]['min'], cfg_info[col]['max'] | ||
| return (val - mn) / (mx - mn) if mx != mn else 0 | ||
|
|
||
| def add_score(example): | ||
| """ | ||
| Here weights can be added to features for their importance if this is deemed neccesairy. | ||
| """ | ||
| example['score'] = ( | ||
| normalize(example['len_cfg'], 'len_cfg') | ||
| + normalize(example['jtp_in_range'], 'jtp_in_range') | ||
| - normalize(example['jtp_out_of_range'], 'jtp_out_of_range') | ||
| - normalize(example['jtp_unknown'], 'jtp_unknown') | ||
| ) | ||
|
|
||
| return example | ||
|
|
||
| return add_score | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # Make sure tokenized dataset has been made With mktokenizer and tokenize_dataset.py | ||
| dataset_path, tokenizer_path, output_path = sys.argv[1:] | ||
| with open(tokenizer_path) as f: | ||
| tokenizer = json.load(f) | ||
|
|
||
| tokenized_dataset = datasets.load_from_disk(dataset_path) | ||
|
|
||
| # Need two passes of `map`` because to normalize we have to know min and max of values | ||
| tokenized_dataset = tokenized_dataset.map(map_tokens_to_dataset, fn_kwargs={'tokenizer': tokenizer}, num_proc=10) | ||
|
|
||
| # In this pass we determine quality_score | ||
| scored_dataset = tokenized_dataset.map(make_scorer(tokenized_dataset), num_proc=10) | ||
| scored_dataset.save_to_disk(output_path) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot get to grips with numpy logic, but as discussed offline, I think i get it.