ruiite's picture
Add model.py
fd031f2 verified
Raw History Blame Contribute Delete
3.07 kB
"""Student scaffold for encoder-decoder transformer inference."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import torch
from torch import Tensor,nn
@dataclass(frozen=True)
class ModelConfig:
vocabulary_size:int; storage_vocabulary_size:int; width:int; expanded_width:int
heads:int; head_width:int; encoder_layers:int; decoder_layers:int
relative_buckets:int; relative_max_distance:int; norm_epsilon:float
pad_id:int; eos_id:int; decoder_start_id:int
@classmethod
def from_dict(cls,value:dict[str,Any])->"ModelConfig": raise NotImplementedError
@dataclass
class ModelOutput:
logits:Tensor
encoder_hidden_states:Tensor
decoder_hidden_states:Tensor
class RMSNorm(nn.Module):
def __init__(self,width:int,epsilon:float):
super().__init__(); raise NotImplementedError
def forward(self,value:Tensor)->Tensor: raise NotImplementedError
def relative_position_bucket(relative_position:Tensor,bidirectional:bool,buckets:int,max_distance:int)->Tensor: raise NotImplementedError
def position_bias(table:Tensor,query_length:int,key_length:int,*,bidirectional:bool,max_distance:int,device:torch.device,dtype:torch.dtype)->Tensor: raise NotImplementedError
def additive_mask(valid_keys:Tensor,query_length:int,dtype:torch.dtype,causal:bool=False)->Tensor: raise NotImplementedError
class Attention(nn.Module):
def __init__(self,config:ModelConfig):
super().__init__(); raise NotImplementedError
def forward(self,hidden:Tensor,key_value:Tensor,mask:Tensor,bias:Tensor)->Tensor: raise NotImplementedError
class EncoderUnit(nn.Module):
def __init__(self,config:ModelConfig):
super().__init__(); raise NotImplementedError
def forward(self,hidden:Tensor,mask:Tensor,bias:Tensor)->Tensor: raise NotImplementedError
class DecoderUnit(nn.Module):
def __init__(self,config:ModelConfig):
super().__init__(); raise NotImplementedError
def forward(self,hidden:Tensor,memory:Tensor,self_mask:Tensor,cross_mask:Tensor,self_bias:Tensor,cross_bias:Tensor)->Tensor: raise NotImplementedError
class AnonymousEncoderDecoder(nn.Module):
def __init__(self,config:ModelConfig):
super().__init__(); raise NotImplementedError
def encode(self,input_ids:Tensor,attention_mask:Tensor|None=None)->tuple[Tensor,Tensor]: raise NotImplementedError
def decode(self,decoder_input_ids:Tensor,memory:Tensor,memory_mask:Tensor,decoder_attention_mask:Tensor|None=None)->Tensor: raise NotImplementedError
def forward(self,input_ids:Tensor,decoder_input_ids:Tensor,attention_mask:Tensor|None=None,decoder_attention_mask:Tensor|None=None)->ModelOutput: raise NotImplementedError
@torch.inference_mode()
def generate(self,input_ids:Tensor,attention_mask:Tensor|None=None,max_new_tokens:int=32)->Tensor: raise NotImplementedError
@classmethod
def from_directory(cls,directory:str|Path,*,device:str|torch.device='cpu',dtype:torch.dtype|None=None)->"AnonymousEncoderDecoder": raise NotImplementedError