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

Add convert_special_token_settings.py #15

Merged
merged 2 commits into from
Apr 14, 2024
Merged
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
5 changes: 5 additions & 0 deletions convert_mds-13b_to_hf_gpt2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ echo Phase 2: Copy Hugging Face Tokenizer files
echo cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo ==========================================
echo Phase 3: Update model config with Hugging Face Tokenizer special token settings
echo python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo
echo ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
Expand Down
5 changes: 5 additions & 0 deletions convert_mds-175b_to_hf_gpt2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ echo Phase 2: Copy Hugging Face Tokenizer files
echo cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo ==========================================
echo Phase 3: Update model config with Hugging Face Tokenizer special token settings
echo python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo
echo ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
Expand Down
5 changes: 5 additions & 0 deletions convert_megatron_to_hf_llama.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ echo Phase 4: Copy Hugging Face Tokenizer files
echo cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
cp ${SOURCE_HUGGINGFACE_TOKENIZER_DIR}/* ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo ==========================================
echo Phase 5: Update model config with Hugging Face Tokenizer special token settings
echo python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/
python convert_special_token_settings.py ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}/

echo
echo ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
ls -l ${OUTPUT_HUGGINGFACE_CHECKPOINT_DIR}
Expand Down
39 changes: 39 additions & 0 deletions convert_special_token_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import sys

from transformers import AutoTokenizer


def update_special_token_settings(tokenizer, model_config: dict):
if len(tokenizer) != model_config["vocab_size"]:
print(f'Might need checking: different vocab size definitions: {len(tokenizer)=} and {model_config["vocab_size"]=}', file=sys.stderr)
new_model_config = dict(model_config)
for token, id in [
("bos_token_id", tokenizer.bos_token_id),
("eos_token_id", tokenizer.eos_token_id),
("pad_token_id", tokenizer.pad_token_id),
]:
if token not in model_config:
print(f"no definition: model_config[{token}]", file=sys.stderr)
elif model_config[token] != id:
print(f"inconsistent: model_config[{token}]={model_config[token]}", file=sys.stderr)
else:
print(f"consistent: model_config[{token}]={model_config[token]}", file=sys.stderr)
continue
print(f"update {token}={id}", file=sys.stderr)
new_model_config[token] = id
return new_model_config


def main():
model_dir = sys.argv[1]
with open(f"{model_dir}/config.json", "r", encoding="utf8") as fin:
model_config = json.load(fin)
tokenizer = AutoTokenizer.from_pretrained(f"{model_dir}")
new_model_config = update_special_token_settings(tokenizer, model_config)
with open(f"{model_dir}/config.json", "w", encoding="utf8") as fout:
json.dump(new_model_config, fout, indent=4, ensure_ascii=True)


if __name__ == "__main__":
main()