File size: 1,589 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
"""
    Abstract base classes for conditioning embedders.
    An embedder takes some conditioning data and embeds/encodes it into vectors
    that are then fed to the LM according to the condition strategy specified 
    in the config.
    
    Embedding vectors should have the same latent dimensionality of the 
    transformer, so every embedder takes as input to the constructor a 
    parameter `embedding_dim`, that should be the same as the hidden dim of 
    the transformer.
"""

from torch import nn
from abc import ABC, abstractmethod

from conditioning.embedded_condition import EmbeddedCondition


class Embedder(ABC, nn.Module):

    def __init__(self, input_dim: int, embedding_dim: int):
        super().__init__()
        self.input_dim: int = input_dim
        self.embedding_dim: int = embedding_dim

    @abstractmethod
    def forward(self, x, duplicate_for_cfg: bool) -> EmbeddedCondition:
        ...

    @abstractmethod
    def null_condition(self, batch_size: int) -> EmbeddedCondition:
        ...


class LinearProjectionEmbedder(ABC, nn.Module):

    def __init__(self, input_dim: int, embedding_dim: int):
        super().__init__()
        self.input_dim: int = input_dim
        self.embedding_dim: int = embedding_dim
        self.output_proj: nn.Linear = nn.Linear(self.input_dim,
                                                self.embedding_dim)

    @abstractmethod
    def forward(self, x, duplicate_for_cfg: bool) -> EmbeddedCondition:
        ...

    @abstractmethod
    def null_condition(self, batch_size: int) -> EmbeddedCondition:
        ...