File size: 6,613 Bytes
2e1dc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from dataclasses import dataclass, field
from typing import Dict, Tuple, Optional, List, Type, Set
from pydoc import locate
from frozendict import frozendict
from copy import deepcopy
from pathlib import Path

import config as cfg
from conditioning.condition_type import ConditionType
from conditioning.conditioning_method import ConditioningMethod
from conditioning import ConcreteEmbedder
# from conditioning.prompt_processor import InterleavedContextPromptProcessor
# from conditioning.t5embedder import T5EmbedderGPU
from data.stem import Stem
from utils.logging import to_loggable

import typing
if typing.TYPE_CHECKING:
    from conditioning.prompt_processor import PromptProcessor


class Loggable:

    def to_dict(self) -> Dict:
        return to_loggable(self.__dict__)  # type: ignore


# generic parameters for a model that can be instantiated
# from a hyperparameter class
@dataclass(unsafe_hash=True, kw_only=True)
class ModelParams(Loggable):
    model_class: str

    # def to_dict(self) -> Dict:
    #     return to_loggable(self.__dict__)  # type: ignore

    def instantiate(self):
        klass = locate(self.model_class)
        return klass(self)  # type: ignore


# --- ENCODEC ---
@dataclass(unsafe_hash=True)
class QuantizerParams:
    dimension: int
    n_q: int
    bins: int

    q_dropout: bool = False
    decay: float = 0.99
    kmeans_init: bool = True
    kmeans_iters: int = 50
    threshold_ema_dead_code: int = 2
    orthogonal_reg_weight: float = 0.0
    orthogonal_reg_active_codes_only: bool = False
    orthogonal_reg_max_codes: int | None = None


@dataclass(unsafe_hash=True)
class SeaNetParams:
    dimension: int
    n_filters: int
    ratios: Tuple[int, int, int, int]
    causal: bool
    true_skip: bool

    channels: int = 1
    n_residual_layers: int = 1
    activation: str = "ELU"
    activation_params: frozendict = field(default_factory=frozendict)
    norm: str = "weight_norm"
    norm_params: frozendict = field(default_factory=frozendict)
    kernel_size: int = 7
    last_kernel_size: int = 7
    residual_kernel_size: int = 3
    dilation_base: int = 2
    pad_mode: str = "reflect"
    compress: int = 2
    lstm: int = 2


@dataclass(unsafe_hash=True, kw_only=True)
class EncodecParams(ModelParams):
    sample_rate: int
    seanet_params: SeaNetParams
    quantizer_params: QuantizerParams
    sum_loss_mulitiplier: int
    weights: Optional[str] = None

    model_class: str = "stage.models.lightning_encodec.LightningEncodec"

    def to_dict(self) -> Dict:
        d = deepcopy(self.__dict__)
        for key, value in self.seanet_params.__dict__.items():
            d["seanet_" + key] = value
        for key, value in self.quantizer_params.__dict__.items():
            d["qt_" + key] = value
        del d["quantizer_params"]
        del d["seanet_params"]
        return d


# --- CONDITIONING ---
@dataclass(unsafe_hash=True)
class PromptProcessorParams(Loggable):
    keep_only_valid_steps: bool
    model_class: Type["PromptProcessor"]
    context_dropout: Optional[float] = None


@dataclass(unsafe_hash=True)
class ConditioningParams(Loggable):
    embedder_types: Dict[ConditionType, Type[ConcreteEmbedder]]
    conditioning_methods: Dict[ConditionType, ConditioningMethod]
    conditioning_dropout: float


# --- LM ---
@dataclass(unsafe_hash=True, kw_only=True)
class LmParams(ModelParams):
    dim: int
    n_layers: int
    n_heads: int
    card: int = 2048
    padding_token: Optional[int] = 2048
    sep_token: Optional[int] = None
    cross_attend: bool = True
    weights: Optional[str] = None
    model_class: str = "stage.models.musicgen_lm.MusicgenLm"


@dataclass(unsafe_hash=True, kw_only=True)
class PretrainedSmallLmParams(LmParams):
    dim: int = 1024
    n_layers: int = 24
    n_heads: int = 16
    weights: Optional[str] = str(cfg.weights_dir() / "lm-small-weights.pt")


@dataclass(unsafe_hash=True, kw_only=True)
class PretrainedLargeLmParams(LmParams):
    dim: int = 2048
    n_layers: int = 48
    n_heads: int = 32
    weights: Optional[str] = str(cfg.weights_dir() / "lm-large-weights.pt")


@dataclass(unsafe_hash=True, kw_only=True)
class PretrainedMelodyLmParams(LmParams):
    dim: int = 1536
    n_layers: int = 48
    n_heads: int = 24
    cross_attend: bool = False
    weights: Optional[str] = str(cfg.weights_dir() / "lm-melody-weights.pt")


@dataclass(unsafe_hash=True, kw_only=True)
class FioraSmallLmParams(PretrainedSmallLmParams):
    sep_token: Optional[int] = 2049


# --- LORA ---
@dataclass(unsafe_hash=True)
class LoraParams:
    r: int
    alpha: int
    dropout: float
    layers: List[str]


# --- MUSICGEN ---
@dataclass(unsafe_hash=True, kw_only=True)
class MusicgenParams(ModelParams):
    encodec_params: EncodecParams
    prompt_processor_params: PromptProcessorParams
    conditioning_params: ConditioningParams
    lm_params: LmParams
    lora_params: Optional[LoraParams] = None
    model_class: str = "stage.models.lightning_musicgen.LightningMusicgen"


# ------ DATA -------


@dataclass(unsafe_hash=True, kw_only=True)
class DatasetParams:
    datamodule_class: str
    clip_length_in_seconds: int
    sample_rate: int

    def to_dict(self) -> Dict:
        return self.__dict__

    def instantiate(self):
        klass = locate(self.datamodule_class)
        return klass(self)  # type: ignore


@dataclass(kw_only=True)
class StemmedDatasetParams(DatasetParams):
    root_dir: Path
    stems: Set[Stem] = field(
        default_factory=lambda: {
            Stem.DRUMS,
            Stem.BASS,
            Stem.GUITAR,
            Stem.KEYBOARD,
            Stem.PIANO,
            Stem.STRINGS,
            Stem.OTHER,
        })
    single_stem: bool
    target_stem: Stem
    min_context_seconds: int
    use_style_conditioning: bool
    use_beat_conditioning: bool
    type_of_context: str
    add_click: bool
    sync_chunks: bool
    bpm_in_caption: bool
    batch_size_train: int
    batch_size_test: int
    num_workers: int
    clip_length_in_seconds: int
    sample_rate: int
    speed_transform_p: float
    pitch_transform_p: float
    n_samples_per_epoch: int
    datamodule_class: str = "stage.data.stemmed_datamodule.StemmedDatamodule"


@dataclass(kw_only=True)
class MixDatasetParams(StemmedDatasetParams):
    ...


# --------- CONFIGURATIONS ---------
pretrained_encodec_meta_32khz_params: EncodecParams = EncodecParams(
    sample_rate=32_000,
    seanet_params=SeaNetParams(128, 64, (8, 5, 4, 4), False, True),
    quantizer_params=QuantizerParams(128, 4, 2048),
    sum_loss_mulitiplier=0,
    weights=str(cfg.weights_dir() / "encodec_32khz.pt"),
)