File size: 1,052 Bytes
7c34c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Optional, Any
from abc import ABC, abstractmethod
from functools import cached_property

import torch.nn as nn
import torch


class Modality(ABC):
    @abstractmethod
    def build_projector(self, lm_hidden_size: int) -> nn.Module:
        pass

    @property
    @abstractmethod
    def name(self) -> str:
        pass

    @property
    @abstractmethod
    def token(self) -> str:
        pass

    @property
    @abstractmethod
    def data_key(self) -> str:
        pass

    @property
    @abstractmethod
    def token_width(self) -> int:
        pass

    @cached_property
    def token_idx(self) -> int:
        hash_ = sum(ord(c) ** i for i, c in enumerate(self.token))
        return -abs(hash_ % 10_000)

    @abstractmethod
    def preprocess_rows(self, rows: List[Dict]) -> List[Optional[Any]]:
        pass

    @abstractmethod
    def forward(self, encoded_values: List[Any]) -> List[torch.Tensor]:
        pass

    def to(self, dtype: torch.dtype, device: torch.device) -> "Modality":
        return self