File size: 3,448 Bytes
7e2301f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Typed configs, loadable from YAML. Nested dataclasses mirror the YAML sections."""

from __future__ import annotations

import dataclasses
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, get_type_hints

import yaml


@dataclass
class CodecConfig:
    sample_rate: int = 24_000
    latent_dim: int = 64
    enc_channels: list[int] = field(default_factory=lambda: [32, 64, 128, 256, 512])
    enc_strides: list[int] = field(default_factory=lambda: [2, 4, 5, 6, 4])  # product = 960
    dec_dim: int = 512
    dec_layers: int = 8
    dec_upsample: int = 4  # 25 Hz -> 100 Hz before iSTFT
    n_fft: int = 960  # iSTFT at 100 Hz: hop = 960 / dec_upsample = 240
    kl_weight: float = 1e-4
    semantic_dim: int = 0  # >0 enables semantic regularization projector
    semantic_weight: float = 1.0


@dataclass
class BackboneConfig:
    pretrained: str | None = None  # e.g. "Qwen/Qwen3-0.6B"; None -> random init from fields below
    hidden_size: int = 1024
    num_layers: int = 28
    num_heads: int = 16
    num_kv_heads: int = 8
    intermediate_size: int = 3072
    max_position: int = 8192
    attn_implementation: str = "sdpa"
    gradient_checkpointing: bool = False


@dataclass
class HeadConfig:
    dim: int = 512
    layers: int = 6
    heads: int = 8
    n_ctx_patches: int = 2
    noise_samples: int = 4  # flow-matching noise draws per backbone state
    p_uncond: float = 0.1  # CFG dropout of h_t
    ctx_noise: float = 0.1  # noise augmentation of context patches (anti exposure-bias)


@dataclass
class SpeakerConfig:
    n_tokens: int = 32
    dim: int = 512
    layers: int = 4
    heads: int = 8
    p_drop: float = 0.1  # drop whole speaker conditioning (CFG / voice design)
    n_emotions: int = 0  # >0 enables gradient-reversal emotion adversary


@dataclass
class ClockConfig:
    enabled: bool = True
    frame_dim: int = 128  # per-frame features fed to the head
    p_drop: float = 0.3


@dataclass
class LossConfig:
    stop_weight: float = 0.5
    ctc_weight: float = 0.1
    ctc_upsample: int = 4  # CTC runs at 12.5 * 4 = 50 Hz so byte targets fit


@dataclass
class PhotonConfig:
    name: str = "photon-m"
    codec: CodecConfig = field(default_factory=CodecConfig)
    backbone: BackboneConfig = field(default_factory=BackboneConfig)
    head: HeadConfig = field(default_factory=HeadConfig)
    speaker: SpeakerConfig = field(default_factory=SpeakerConfig)
    clock: ClockConfig = field(default_factory=ClockConfig)
    loss: LossConfig = field(default_factory=LossConfig)
    tokenizer: str = "bytes"  # "bytes" or an HF tokenizer name
    patch: int = 2


def _build(cls, data: dict[str, Any] | None):
    data = dict(data or {})
    hints = get_type_hints(cls)
    kwargs = {}
    for f in dataclasses.fields(cls):
        if f.name not in data:
            continue
        val = data.pop(f.name)
        typ = hints[f.name]
        if dataclasses.is_dataclass(typ):
            val = _build(typ, val)
        kwargs[f.name] = val
    if data:
        raise ValueError(f"Unknown keys for {cls.__name__}: {sorted(data)}")
    return cls(**kwargs)


def load_config(path: str | Path) -> PhotonConfig:
    with open(path, encoding="utf-8") as f:
        return _build(PhotonConfig, yaml.safe_load(f))


def config_from_dict(d: dict[str, Any]) -> PhotonConfig:
    return _build(PhotonConfig, d)


def to_dict(cfg) -> dict[str, Any]:
    return dataclasses.asdict(cfg)