Download runtime/albert_inputs.py from florianvoss/kokoro-sima: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/florianvoss/kokoro-sima/resolve/main/runtime/albert_inputs.py
- Command line
-
hf download hf://florianvoss/kokoro-sima/runtime/albert_inputs.py
-
curl -L -o albert_inputs.py https://huggingface.co/florianvoss/kokoro-sima/resolve/main/runtime/albert_inputs.py
1.3 kB
| """CPU embedding lookup and static-mask contract for Kokoro ALBERT.""" | |
| import numpy as np | |
| def prepare_inputs(token_ids, embeddings, capacity=480): | |
| ids = np.asarray(token_ids) | |
| if ids.ndim != 1 or not np.issubdtype(ids.dtype, np.integer): | |
| raise ValueError("Expected a one-dimensional integer token sequence") | |
| if not 1 <= len(ids) <= capacity: | |
| raise ValueError(f"Expected 1..{capacity} tokens, including boundary tokens") | |
| if np.any(ids < 0) or np.any(ids >= len(embeddings['word'])): | |
| raise ValueError("Token ID outside vocabulary") | |
| padded = np.zeros(capacity, dtype=np.int64) | |
| padded[:len(ids)] = ids | |
| summed = embeddings['word'][padded] + embeddings['type'][0] | |
| summed = summed + embeddings['position'][:capacity] | |
| mask = np.full((1, capacity, 1, 1), -10000, dtype=np.float32) | |
| mask[:, :len(ids)] = 0 | |
| return {'embedding_sum': np.ascontiguousarray(summed.T[None, :, None, :]), | |
| 'attention_mask': mask} | |
| def unpack_output(hidden_states, valid_tokens): | |
| """Remove padding BEFORE the downstream CPU projection/LSTMs.""" | |
| if not 1 <= valid_tokens <= hidden_states.shape[-1]: | |
| raise ValueError("Invalid valid-token count") | |
| return np.ascontiguousarray(hidden_states[:, :, 0, :valid_tokens].transpose(0, 2, 1)) | |