8
8
import os
9
9
import re
10
10
import sys
11
- from abc import ABC
12
11
from enum import IntEnum
13
12
from pathlib import Path
14
13
from hashlib import sha256
@@ -41,7 +40,7 @@ class SentencePieceTokenTypes(IntEnum):
41
40
AnyModel = TypeVar ("AnyModel" , bound = "type[Model]" )
42
41
43
42
44
- class Model ( ABC ) :
43
+ class Model :
45
44
_model_classes : dict [str , type [Model ]] = {}
46
45
47
46
dir_model : Path
@@ -62,6 +61,8 @@ class Model(ABC):
62
61
model_arch : gguf .MODEL_ARCH
63
62
64
63
def __init__ (self , dir_model : Path , ftype : int , fname_out : Path , is_big_endian : bool , use_temp_file : bool ):
64
+ if self .__class__ == Model :
65
+ raise TypeError (f"{ self .__class__ .__name__ !r} should not be directly instantiated" )
65
66
self .dir_model = dir_model
66
67
self .ftype = ftype
67
68
self .fname_out = fname_out
@@ -78,6 +79,13 @@ def __init__(self, dir_model: Path, ftype: int, fname_out: Path, is_big_endian:
78
79
self .tensor_map = gguf .get_tensor_name_map (self .model_arch , self .block_count )
79
80
self .tensors = dict (self .get_tensors ())
80
81
82
+ @classmethod
83
+ def __init_subclass__ (cls ):
84
+ # can't use an abstract property, because overriding it without type errors
85
+ # would require using decorated functions instead of simply defining the property
86
+ if "model_arch" not in cls .__dict__ :
87
+ raise TypeError (f"Missing property 'model_arch' for { cls .__name__ !r} " )
88
+
81
89
def find_hparam (self , keys : Iterable [str ], optional : bool = False ) -> Any :
82
90
key = next ((k for k in keys if k in self .hparams ), None )
83
91
if key is not None :
@@ -266,8 +274,6 @@ def register(cls, *names: str) -> Callable[[AnyModel], AnyModel]:
266
274
267
275
def func (modelcls : AnyModel ) -> AnyModel :
268
276
for name in names :
269
- if "model_arch" not in modelcls .__dict__ :
270
- raise TypeError (f"Missing property 'model_arch' for { modelcls .__name__ !r} " )
271
277
cls ._model_classes [name ] = modelcls
272
278
return modelcls
273
279
return func
0 commit comments