model.py 680 Bytes
Newer Older
1
2
import torch

3
from abc import ABC, abstractmethod
4
from typing import List, Tuple, Optional, TypeVar, Type
5
from tokenizers import Tokenizer
6
7
8

from text_generation.models.types import Batch, GeneratedText

9
10
B = TypeVar("B", bound=Batch)

11

12
class Model(ABC):
13
14
15
16
17
    def __init__(self, tokenizer: Tokenizer, num_heads: int, device: torch.device):
        self.tokenizer = tokenizer
        self.num_heads = num_heads
        self.device = device

18
    @property
19
    @abstractmethod
20
    def batch_type(self) -> Type[B]:
21
        raise NotImplementedError
22

23
24
25
    @abstractmethod
    def generate_token(self, batch: B) -> Tuple[List[GeneratedText], Optional[B]]:
        raise NotImplementedError