1515import textwrap
1616from typing import ClassVar , Dict , List , Optional , Tuple , Union
1717
18- from pydantic import BaseModel , Field
18+ from pydantic import BaseModel , Field , computed_field
1919
2020
2121__all__ = [
3333PrintOrderType = ClassVar [List [str ]]
3434
3535
36- class PropertyBaseModel (BaseModel ):
37- """
38- https://github.com/samuelcolvin/pydantic/issues/935#issuecomment-1152457432
39-
40- Workaround for serializing properties with pydantic until
41- https://github.com/samuelcolvin/pydantic/issues/935
42- is solved
43- """
44-
45- @classmethod
46- def get_properties (cls ):
47- return [
48- prop
49- for prop in dir (cls )
50- if isinstance (getattr (cls , prop ), property )
51- and prop not in ("__values__" , "fields" )
52- ]
53-
54- def dict (
55- self ,
56- * ,
57- include : Union ["AbstractSetIntStr" , "MappingIntStrAny" ] = None , # noqa: F821
58- exclude : Union ["AbstractSetIntStr" , "MappingIntStrAny" ] = None , # noqa: F821
59- by_alias : bool = False ,
60- skip_defaults : bool = None ,
61- exclude_unset : bool = False ,
62- exclude_defaults : bool = False ,
63- exclude_none : bool = False ,
64- ) -> "DictStrAny" : # noqa: F821
65- attribs = super ().dict (
66- include = include ,
67- exclude = exclude ,
68- by_alias = by_alias ,
69- skip_defaults = skip_defaults ,
70- exclude_unset = exclude_unset ,
71- exclude_defaults = exclude_defaults ,
72- exclude_none = exclude_none ,
73- )
74- props = self .get_properties ()
75- # Include and exclude properties
76- if include :
77- props = [prop for prop in props if prop in include ]
78- if exclude :
79- props = [prop for prop in props if prop not in exclude ]
80-
81- # Update the attribute dict with the properties
82- if props :
83- attribs .update ({prop : getattr (self , prop ) for prop in props })
84-
85- return attribs
86-
87-
8836class NodeCounts (BaseModel ):
8937 """
9038 Pydantic model for specifying the number zero and non-zero operations and the
@@ -114,7 +62,7 @@ class NodeIO(BaseModel):
11462 )
11563
11664
117- class ZeroNonZeroParams (PropertyBaseModel ):
65+ class ZeroNonZeroParams (BaseModel ):
11866 """
11967 Pydantic model for specifying the number zero and non-zero operations and the
12068 associated sparsity
@@ -127,20 +75,22 @@ class ZeroNonZeroParams(PropertyBaseModel):
12775 description = "The number of parameters whose value is zero" , default = 0
12876 )
12977
78+ @computed_field (repr = True , return_type = Union [int , float ])
13079 @property
13180 def sparsity (self ):
13281 total_values = self .total
13382 if total_values > 0 :
13483 return self .zero / total_values
13584 else :
136- return 0
85+ return 0.0
13786
87+ @computed_field (repr = True , return_type = int )
13888 @property
13989 def total (self ):
14090 return self .non_zero + self .zero
14191
14292
143- class DenseSparseOps (PropertyBaseModel ):
93+ class DenseSparseOps (BaseModel ):
14494 """
14595 Pydantic model for specifying the number dense and sparse operations and the
14696 associated operation sparsity
@@ -155,6 +105,7 @@ class DenseSparseOps(PropertyBaseModel):
155105 default = 0 ,
156106 )
157107
108+ @computed_field (repr = True , return_type = Union [int , float ])
158109 @property
159110 def sparsity (self ):
160111 total_ops = self .sparse + self .dense
0 commit comments