-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from kyutai-labs/refacto
refacto [1/N]
- Loading branch information
Showing
20 changed files
with
324 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# moshi - pytorch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
# Copyright (c) Kyutai, all rights reserved. | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
""" | ||
Models for EnCodec, AudioGen, MusicGen, as well as the generic LMModel. | ||
Models for the compression model Moshi, | ||
""" | ||
|
||
# flake8: noqa | ||
from .encodec import ( | ||
from .compression import ( | ||
CompressionModel, | ||
EncodecModel, | ||
MimiModel, | ||
) | ||
from .lm import LMModel, LMGen | ||
from .moshi_ import get_encodec, get_lm | ||
from .loaders import get_mimi, get_moshi_lm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
# Copyright (c) Kyutai, all rights reserved. | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
"""Retrieves the pretrained models for Moshi and Mimi.""" | ||
from pathlib import Path | ||
|
||
from ..modules import SEANetEncoder, SEANetDecoder, transformer | ||
from .encodec import EncodecModel | ||
from huggingface_hub import hf_hub_download | ||
from safetensors.torch import load_model | ||
import sentencepiece | ||
import torch | ||
|
||
from .compression import MimiModel | ||
from .lm import LMModel | ||
from ..modules import SEANetEncoder, SEANetDecoder, transformer | ||
from ..quantization import SplitResidualVectorQuantizer | ||
import torch | ||
from safetensors.torch import load_model | ||
from pathlib import Path | ||
import typing as tp | ||
|
||
SAMPLE_RATE = 24000 | ||
FRAME_RATE = 12.5 | ||
HF_REPO = 'kmhf/msh-v0.1' | ||
MIMI_V0_1 = 'tokenizer-e351c8d8-checkpoint125.safetensors' | ||
MOSHIKO_V0_1 = '[email protected]' | ||
MOSHIKA_V0_1 = '[email protected]' | ||
TEXT_TOKENIZER_V0_1 = 'tokenizer_spm_32k_3.model' | ||
|
||
|
||
seanet_kwargs = { | ||
_seanet_kwargs = { | ||
"channels": 1, | ||
"dimension": 512, | ||
"causal": True, | ||
|
@@ -35,15 +43,15 @@ | |
"ratios": [8, 6, 5, 4], | ||
"true_skip": True, | ||
} | ||
quantizer_kwargs = { | ||
_quantizer_kwargs = { | ||
"dimension": 256, | ||
"n_q": 32, | ||
"bins": 2048, | ||
"input_dimension": seanet_kwargs["dimension"], | ||
"output_dimension": seanet_kwargs["dimension"], | ||
"input_dimension": _seanet_kwargs["dimension"], | ||
"output_dimension": _seanet_kwargs["dimension"], | ||
} | ||
transformer_kwargs = { | ||
"d_model": seanet_kwargs["dimension"], | ||
_transformer_kwargs = { | ||
"d_model": _seanet_kwargs["dimension"], | ||
"num_heads": 8, | ||
"num_layers": 8, | ||
"causal": True, | ||
|
@@ -55,17 +63,17 @@ | |
"norm": "layer_norm", | ||
"positional_embedding": "rope", | ||
"dim_feedforward": 2048, | ||
"input_dimension": seanet_kwargs["dimension"], | ||
"output_dimensions": [seanet_kwargs["dimension"]], | ||
"input_dimension": _seanet_kwargs["dimension"], | ||
"output_dimensions": [_seanet_kwargs["dimension"]], | ||
} | ||
|
||
lm_kwargs = { | ||
_lm_kwargs = { | ||
"dim": 4096, | ||
"text_card": 32000, | ||
"existing_text_padding_id": 3, | ||
"n_q": 16, | ||
"dep_q": 8, | ||
"card": quantizer_kwargs["bins"], | ||
"card": _quantizer_kwargs["bins"], | ||
"num_heads": 32, | ||
"num_layers": 32, | ||
"hidden_scale": 4.125, | ||
|
@@ -92,24 +100,40 @@ | |
} | ||
|
||
|
||
def _is_safetensors(filename: tp.Union[str, Path]) -> bool: | ||
filename = Path(filename) | ||
return filename.suffix in (".safetensors", ".sft", ".sfts") | ||
def _is_safetensors(path: Path | str) -> bool: | ||
return Path(path).suffix in (".safetensors", ".sft", ".sfts") | ||
|
||
|
||
def get_encodec(filename: tp.Union[str, Path], device): | ||
encoder = SEANetEncoder(**seanet_kwargs) | ||
decoder = SEANetDecoder(**seanet_kwargs) | ||
def resolve_model_checkpoint(name: str, hf_repo: str = HF_REPO, allow_local_file: bool = True) -> Path: | ||
"""Load a model checkpoint from HF. | ||
If `allow_local_file` is True, then if a file `name` exists, it will be used instead. | ||
""" | ||
if allow_local_file and Path(name).exists(): | ||
return Path(name) | ||
else: | ||
filename = name | ||
return Path(hf_hub_download(hf_repo, filename)) | ||
|
||
|
||
def get_text_tokenizer(filename: str | Path) -> sentencepiece.SentencePieceProcessor: | ||
return sentencepiece.SentencePieceProcessor(str(filename)) # type: ignore | ||
|
||
|
||
def get_mimi(filename: str | Path, | ||
device: torch.device | str = 'cpu') -> MimiModel: | ||
"""Return a pretrained Mimi model.""" | ||
encoder = SEANetEncoder(**_seanet_kwargs) | ||
decoder = SEANetDecoder(**_seanet_kwargs) | ||
encoder_transformer = transformer.ProjectedTransformer( | ||
device=device, **transformer_kwargs | ||
device=device, **_transformer_kwargs | ||
) | ||
decoder_transformer = transformer.ProjectedTransformer( | ||
device=device, **transformer_kwargs | ||
device=device, **_transformer_kwargs | ||
) | ||
quantizer = SplitResidualVectorQuantizer( | ||
**quantizer_kwargs, | ||
**_quantizer_kwargs, | ||
) | ||
model = EncodecModel( | ||
model = MimiModel( | ||
encoder, | ||
decoder, | ||
quantizer, | ||
|
@@ -126,21 +150,19 @@ def get_encodec(filename: tp.Union[str, Path], device): | |
if _is_safetensors(filename): | ||
load_model(model, filename) | ||
else: | ||
pkg = torch.load( | ||
filename, | ||
"cpu", | ||
) | ||
pkg = torch.load(filename, "cpu") | ||
model.load_state_dict(pkg["model"]) | ||
model.set_num_codebooks(8) | ||
return model | ||
|
||
|
||
def get_lm(filename: tp.Union[str, Path], device): | ||
def get_moshi_lm(filename: str | Path, | ||
device: torch.device | str = 'cpu') -> LMModel: | ||
dtype = torch.bfloat16 | ||
model = LMModel( | ||
device=device, | ||
dtype=dtype, | ||
**lm_kwargs, | ||
**_lm_kwargs, | ||
).to(device=device, dtype=dtype) | ||
model.eval() | ||
if _is_safetensors(filename): | ||
|
Oops, something went wrong.