Download quasar/training/migration.py from AtlasAI-org/Quasar: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/AtlasAI-org/Quasar/resolve/main/quasar/training/migration.py
- Command line
-
hf download hf://AtlasAI-org/Quasar/quasar/training/migration.py
-
curl -L -o migration.py https://huggingface.co/AtlasAI-org/Quasar/resolve/main/quasar/training/migration.py
1.38 kB
| """Explicit vocabulary expansion before any media training; preserve learned text.""" | |
| from dataclasses import replace | |
| import torch | |
| from ..model.transformer import Quasar | |
| from ..tokenizers.universal import UniversalVocab | |
| def expand_untrained_media(model: Quasar, old: UniversalVocab, new: UniversalVocab) -> Quasar: | |
| if old.text_size!=new.text_size or old.special_tokens!=new.special_tokens: | |
| raise ValueError('text and structural token identities must be preserved') | |
| if model.cfg.vocab_size!=old.size or new.visual_size<=old.visual_size: | |
| raise ValueError('expected explicit visual vocabulary expansion') | |
| migrated=Quasar(replace(model.cfg,vocab_size=new.size,visual_size=new.visual_size, | |
| visual_offset=new.visual_offset)).to(next(model.parameters()).device) | |
| old_params=dict(model.named_parameters()) | |
| for name,p in migrated.named_parameters(): | |
| if name!='embed.weight':p.copy_(old_params[name]) | |
| prefix=old.ranges['structure'][1] | |
| migrated.embed.weight[:prefix].copy_(model.embed.weight[:prefix]) | |
| for kind in ('audio','coordinate','index'): | |
| a,b=old.ranges[kind];c,d=new.ranges[kind] | |
| if b-a!=d-c:raise ValueError('nonvisual modality sizes must be preserved') | |
| migrated.embed.weight[c:d].copy_(model.embed.weight[a:b]) | |
| return migrated | |