Download scripts/r_coloured_start.py from 77ethers/alpa: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/77ethers/alpa/resolve/main/scripts/r_coloured_start.py
- Command line
-
hf download hf://77ethers/alpa/scripts/r_coloured_start.py
-
curl -L -o r_coloured_start.py https://huggingface.co/77ethers/alpa/resolve/main/scripts/r_coloured_start.py
2.22 kB
| """Build the starting point of Alpa v0.1 from tiny-voca's published weights: the plain transplant of | |
| alpa.transplant, then the retroflex rows given a share of English "r". | |
| python scripts/r_coloured_start.py --source tiny-voca.pt --output start.pt [--share 0.3] | |
| English has no retroflex consonants. As plain copies of t, d, n and sh, the rows of ṭ ḍ ṇ ṣ did not separate from | |
| their dental partners in 3,000 training steps; started as (1 - share) of the partner plus share of English "r", | |
| they did (docs/report, Section 5.3). A retroflex and an English "r" are both made with the tongue curled back. | |
| """ | |
| import argparse | |
| import hashlib | |
| from pathlib import Path | |
| import torch | |
| from alpa.config import Config | |
| from alpa.text import SYMBOL_TO_ID | |
| from alpa.transplant import starting_checkpoint | |
| RETROFLEX = "wqRz" # Alpa's symbols for ṭ ḍ ṇ ṣ | |
| ENGLISH_R = "ɹ" | |
| def r_coloured(source, share, origin): | |
| checkpoint = starting_checkpoint(source, Config(), origin) | |
| table = checkpoint["system"]["model.tokens.weight"] | |
| r = source["model"]["tokens.weight"][source["frontend"]["symbols"].index(ENGLISH_R)] | |
| for symbol in RETROFLEX: | |
| row = SYMBOL_TO_ID[symbol] | |
| table[row] = (1 - share) * table[row] + share * r | |
| return checkpoint | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| parser.add_argument("--source", type=Path, required=True, help="tiny-voca's published inference checkpoint") | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--share", type=float, default=0.3, help='share of English "r" in each retroflex row') | |
| args = parser.parse_args() | |
| if not 0 <= args.share <= 1: | |
| parser.error("--share must be between 0 and 1") | |
| source = torch.load(args.source, map_location="cpu", weights_only=True) | |
| origin = {"model": "tiny-voca", "sha256": hashlib.sha256(args.source.read_bytes()).hexdigest()} | |
| torch.save(r_coloured(source, args.share, origin), args.output) | |
| print(f"wrote {args.output}: retroflex rows are {1 - args.share:g} of their partner plus {args.share:g} of English r") | |
| if __name__ == "__main__": | |
| main() | |