Update README.md
Browse files
README.md
CHANGED
|
@@ -70,4 +70,67 @@ language:
|
|
| 70 |
- xh
|
| 71 |
- yo
|
| 72 |
- zu
|
| 73 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
- xh
|
| 71 |
- yo
|
| 72 |
- zu
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
## Usage
|
| 77 |
+
|
| 78 |
+
### ONNXRuntime
|
| 79 |
+
|
| 80 |
+
```py
|
| 81 |
+
from transformers import AutoConfig, AutoTokenizer
|
| 82 |
+
import onnxruntime
|
| 83 |
+
import numpy as np
|
| 84 |
+
|
| 85 |
+
# 1. Load config, processor, and model
|
| 86 |
+
model_id = "./path/to/model/"
|
| 87 |
+
config = AutoConfig.from_pretrained(model_id)
|
| 88 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 89 |
+
model_path = f"{model_id}/onnx/model.onnx"
|
| 90 |
+
decoder_session = onnxruntime.InferenceSession(model_path)
|
| 91 |
+
|
| 92 |
+
## Set config values
|
| 93 |
+
num_key_value_heads = config.num_key_value_heads
|
| 94 |
+
head_dim = config.hidden_size // config.num_attention_heads
|
| 95 |
+
num_hidden_layers = config.num_hidden_layers
|
| 96 |
+
eos_token_id = config.eos_token_id
|
| 97 |
+
|
| 98 |
+
# 2. Prepare inputs
|
| 99 |
+
messages = [{"role": "user", "content": "Explica en español qué significa la palabra japonesa 'ikigai' y da un ejemplo práctico."}]
|
| 100 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="np")
|
| 101 |
+
input_ids = inputs['input_ids']
|
| 102 |
+
attention_mask = inputs['attention_mask']
|
| 103 |
+
batch_size = input_ids.shape[0]
|
| 104 |
+
past_key_values = {
|
| 105 |
+
f'past_key_values.{layer}.{kv}': np.zeros([batch_size, num_key_value_heads, 0, head_dim], dtype=np.float32)
|
| 106 |
+
for layer in range(num_hidden_layers)
|
| 107 |
+
for kv in ('key', 'value')
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
# 3. Generation loop
|
| 111 |
+
max_new_tokens = 1024
|
| 112 |
+
generated_tokens = np.array([[]], dtype=np.int64)
|
| 113 |
+
for i in range(max_new_tokens):
|
| 114 |
+
logits, *present_key_values = decoder_session.run(None, dict(
|
| 115 |
+
input_ids=input_ids,
|
| 116 |
+
attention_mask=attention_mask,
|
| 117 |
+
**past_key_values,
|
| 118 |
+
))
|
| 119 |
+
|
| 120 |
+
## Update values for next generation loop
|
| 121 |
+
input_ids = logits[:, -1].argmax(-1, keepdims=True)
|
| 122 |
+
attention_mask = np.concatenate([attention_mask, np.ones_like(input_ids, dtype=np.int64)], axis=-1)
|
| 123 |
+
for j, key in enumerate(past_key_values):
|
| 124 |
+
past_key_values[key] = present_key_values[j]
|
| 125 |
+
|
| 126 |
+
generated_tokens = np.concatenate([generated_tokens, input_ids], axis=-1)
|
| 127 |
+
if np.isin(input_ids, eos_token_id).any():
|
| 128 |
+
break
|
| 129 |
+
|
| 130 |
+
## (Optional) Streaming
|
| 131 |
+
print(tokenizer.decode(input_ids[0]), end='', flush=True)
|
| 132 |
+
print()
|
| 133 |
+
|
| 134 |
+
# 4. Output result
|
| 135 |
+
print(tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0])
|
| 136 |
+
```
|