File size: 2,954 Bytes
eca0c8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# MIT_LICENSE file in the root directory of this source tree.

from dataclasses import dataclass
from typing import List, Optional

from fairseq2.models.utils.arch_registry import ArchitectureRegistry
from fairseq2.typing import DataType, Device

from seamless_communication.models.generator.ecapa_tdnn import ECAPA_TDNN


@dataclass
class EcapaTDNNConfig:
    channels: List[int]
    kernel_sizes: List[int]
    dilations: List[int]
    attention_channels: int
    res2net_scale: int
    se_channels: int
    global_context: bool
    groups: List[int]
    embed_dim: int
    input_dim: int


ecapa_tdnn_archs = ArchitectureRegistry[EcapaTDNNConfig]("ecapa_tdnn")

ecapa_tdnn_arch = ecapa_tdnn_archs.decorator


@ecapa_tdnn_arch("base")
def _base_ecapa_tdnn() -> EcapaTDNNConfig:
    return EcapaTDNNConfig(
        channels=[512, 512, 512, 512, 1536],
        kernel_sizes=[5, 3, 3, 3, 1],
        dilations=[1, 2, 3, 4, 1],
        attention_channels=128,
        res2net_scale=8,
        se_channels=128,
        global_context=True,
        groups=[1, 1, 1, 1, 1],
        embed_dim=512,
        input_dim=80,
    )


class EcapaTDNNBuilder:
    """
    Builder module for ECAPA_TDNN model
    """

    config: EcapaTDNNConfig
    device: Optional[Device]
    dtype: Optional[DataType]

    def __init__(
        self,
        config: EcapaTDNNConfig,
        *,
        device: Optional[Device] = None,
        dtype: Optional[DataType] = None,
    ) -> None:
        """
        :param config:
            The configuration to use.
        :param devicev:
            The device on which to initialize modules.
        :param dtype:
            The data type of module parameters and buffers.
        """
        self.config = config

        self.device, self.dtype = device, dtype

    def build_model(self) -> ECAPA_TDNN:
        """Build a model."""
        model = ECAPA_TDNN(
            self.config.channels,
            self.config.kernel_sizes,
            self.config.dilations,
            self.config.attention_channels,
            self.config.res2net_scale,
            self.config.se_channels,
            self.config.global_context,
            self.config.groups,
            self.config.embed_dim,
            self.config.input_dim,
        )
        model.to(device=self.device, dtype=self.dtype)
        return model


def create_ecapa_tdnn_model(
    config: EcapaTDNNConfig,
    device: Optional[Device] = None,
    dtype: Optional[DataType] = None,
) -> ECAPA_TDNN:
    """Create a ECAPA_TDNN model.

    :param config:
        The configuration to use.
    :param device:
        The device on which to initialize modules.
    :param dtype:
        The data type of module parameters and buffers.
    """

    return EcapaTDNNBuilder(config, device=device, dtype=dtype).build_model()