Spaces:
Sleeping
Sleeping
Jaehee Kim commited on
Commit ·
6f47203
1
Parent(s): 8f0316f
Match Space inference to AlienLM training tokenizer
Browse files
README.md
CHANGED
|
@@ -18,15 +18,17 @@ using the public
|
|
| 18 |
[`dsba-lab/llama3-8b-instruct-alienlm-full`](https://huggingface.co/dsba-lab/llama3-8b-instruct-alienlm-full)
|
| 19 |
checkpoint.
|
| 20 |
|
| 21 |
-
The Space
|
| 22 |
-
checks
|
| 23 |
-
[`
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
The Space includes the alien tokenizer files required by the model. Access to
|
| 32 |
the gated `meta-llama/Meta-Llama-3-8B-Instruct` tokenizer is supplied through the
|
|
|
|
| 18 |
[`dsba-lab/llama3-8b-instruct-alienlm-full`](https://huggingface.co/dsba-lab/llama3-8b-instruct-alienlm-full)
|
| 19 |
checkpoint.
|
| 20 |
|
| 21 |
+
The Space uses the official repository's token-ID translation rule and startup
|
| 22 |
+
compatibility checks. Its inference path matches the
|
| 23 |
+
[`SingleAlienTokenizer`](https://github.com/KimJaehee0725/AlienLM/blob/icml/training/axolotl/tokenizers/single_alien_tokenizer.py)
|
| 24 |
+
used for training:
|
| 25 |
+
|
| 26 |
+
1. Render the natural-language chat with the original Llama 3 tokenizer, then
|
| 27 |
+
translate the rendered text into alien text.
|
| 28 |
+
2. Encode the alien text with the original tokenizer, matching the
|
| 29 |
+
`SingleAlienTokenizer` path used during training.
|
| 30 |
+
3. Decode generated token IDs with the original tokenizer, then translate that
|
| 31 |
+
alien response back to natural text.
|
| 32 |
|
| 33 |
The Space includes the alien tokenizer files required by the model. Access to
|
| 34 |
the gated `meta-llama/Meta-Llama-3-8B-Instruct` tokenizer is supplied through the
|
app.py
CHANGED
|
@@ -50,12 +50,20 @@ model.eval()
|
|
| 50 |
|
| 51 |
def plain2alien(text: str) -> str:
|
| 52 |
token_ids = base_tokenizer.encode(text, add_special_tokens=False)
|
| 53 |
-
return alien_tokenizer.decode(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
def alien2plain(text: str) -> str:
|
| 57 |
token_ids = alien_tokenizer.encode(text, add_special_tokens=False)
|
| 58 |
-
return base_tokenizer.decode(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
@spaces.GPU(duration=120)
|
|
@@ -82,17 +90,24 @@ def run_alienlm(
|
|
| 82 |
if not user_prompt.strip():
|
| 83 |
return "", "", "", "Please enter a prompt."
|
| 84 |
|
| 85 |
-
# Step 1: Alienize the user's prompt
|
| 86 |
alien_prompt = plain2alien(user_prompt)
|
| 87 |
|
| 88 |
-
# Step 2:
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
messages,
|
| 92 |
tokenize=False,
|
| 93 |
add_generation_prompt=True,
|
| 94 |
)
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Step 3: Generate with the AlienLM model
|
| 98 |
with torch.no_grad():
|
|
@@ -107,7 +122,12 @@ def run_alienlm(
|
|
| 107 |
|
| 108 |
# Extract only the generated tokens (skip the prompt)
|
| 109 |
generated_ids = output_ids[0][inputs["input_ids"].shape[1]:]
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# Step 4: Translate the alien response back to natural language
|
| 113 |
recovered_response = alien2plain(alien_response)
|
|
|
|
| 50 |
|
| 51 |
def plain2alien(text: str) -> str:
|
| 52 |
token_ids = base_tokenizer.encode(text, add_special_tokens=False)
|
| 53 |
+
return alien_tokenizer.decode(
|
| 54 |
+
token_ids,
|
| 55 |
+
skip_special_tokens=False,
|
| 56 |
+
clean_up_tokenization_spaces=False,
|
| 57 |
+
)
|
| 58 |
|
| 59 |
|
| 60 |
def alien2plain(text: str) -> str:
|
| 61 |
token_ids = alien_tokenizer.encode(text, add_special_tokens=False)
|
| 62 |
+
return base_tokenizer.decode(
|
| 63 |
+
token_ids,
|
| 64 |
+
skip_special_tokens=False,
|
| 65 |
+
clean_up_tokenization_spaces=False,
|
| 66 |
+
)
|
| 67 |
|
| 68 |
|
| 69 |
@spaces.GPU(duration=120)
|
|
|
|
| 90 |
if not user_prompt.strip():
|
| 91 |
return "", "", "", "Please enter a prompt."
|
| 92 |
|
| 93 |
+
# Step 1: Alienize the user's prompt for display.
|
| 94 |
alien_prompt = plain2alien(user_prompt)
|
| 95 |
|
| 96 |
+
# Step 2: Match the SingleAlienTokenizer used for training: render the
|
| 97 |
+
# plain chat, alienize the full rendered text, then encode it with the base
|
| 98 |
+
# tokenizer. Special tokens are preserved by the tokenizer bijection.
|
| 99 |
+
messages = [{"role": "user", "content": user_prompt}]
|
| 100 |
+
plain_input_text = base_tokenizer.apply_chat_template(
|
| 101 |
messages,
|
| 102 |
tokenize=False,
|
| 103 |
add_generation_prompt=True,
|
| 104 |
)
|
| 105 |
+
alien_input_text = plain2alien(plain_input_text)
|
| 106 |
+
inputs = base_tokenizer(
|
| 107 |
+
alien_input_text,
|
| 108 |
+
add_special_tokens=False,
|
| 109 |
+
return_tensors="pt",
|
| 110 |
+
).to("cuda")
|
| 111 |
|
| 112 |
# Step 3: Generate with the AlienLM model
|
| 113 |
with torch.no_grad():
|
|
|
|
| 122 |
|
| 123 |
# Extract only the generated tokens (skip the prompt)
|
| 124 |
generated_ids = output_ids[0][inputs["input_ids"].shape[1]:]
|
| 125 |
+
# The training wrapper decodes model IDs with the base tokenizer first.
|
| 126 |
+
alien_response = base_tokenizer.decode(
|
| 127 |
+
generated_ids,
|
| 128 |
+
skip_special_tokens=True,
|
| 129 |
+
clean_up_tokenization_spaces=False,
|
| 130 |
+
)
|
| 131 |
|
| 132 |
# Step 4: Translate the alien response back to natural language
|
| 133 |
recovered_response = alien2plain(alien_response)
|