File size: 2,216 Bytes
ab19986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""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()