44import hls4ml .model .attributes as attributes
55import hls4ml .model .layers as layers
66
7- all_backends = backends .get_available_backends ()
8- # Removing duplicates but preserving order
9- all_layers = list (dict .fromkeys (layers .layer_map .values ()))
10-
117
128class AttrList :
139 def __init__ (self , cls_name , cls_attrs ) -> None :
@@ -17,29 +13,61 @@ def __init__(self, cls_name, cls_attrs) -> None:
1713 self .weight_attrs = [attr for attr in cls_attrs if attr .__class__ .__name__ == 'WeightAttribute' ]
1814 self .base_attrs = [attr for attr in cls_attrs if attr not in self .config_attrs + self .type_attrs + self .weight_attrs ]
1915 self .backend_attrs = {}
16+ self .reverse_backend_attrs = [] # Will hold (attr, backend_name) pairs, used temporarily
17+ self .unique_backend_attrs = []
2018
2119 def add_backend_attrs (self , backend_name , backend_attrs ):
2220 self .backend_attrs [backend_name ] = backend_attrs
2321
22+ for attr in backend_attrs :
23+ self .reverse_backend_attrs .append ((attr , backend_name ))
24+
25+ def sift_backend_attrs (self ):
26+ grouped_dict = {}
27+ for attr , backend_name in self .reverse_backend_attrs :
28+ if attr not in grouped_dict :
29+ grouped_dict [attr ] = []
30+ grouped_dict [attr ].append (backend_name )
31+
32+ for attr , backend_names in grouped_dict .items ():
33+ attr .available_in = backend_names
34+ self .unique_backend_attrs .append (attr )
35+
36+ @property
37+ def only_configurable (self ):
38+ all_attrs = self .config_attrs + self .type_attrs + self .unique_backend_attrs
39+ return [attr for attr in all_attrs if attr .configurable is True ]
40+
2441
25- attr_map = []
42+ def convert_to_attr_list ():
43+ all_backends = backends .get_available_backends ()
44+ # Removing duplicates but preserving order
45+ all_layers = list (dict .fromkeys (layers .layer_map .values ()))
46+ all_layers_attrs = []
2647
27- for layer_cls in all_layers :
28- base_attrs = layer_cls .expected_attributes
48+ for layer_cls in all_layers :
49+ base_attrs = layer_cls .expected_attributes
2950
30- attr_list = AttrList (layer_cls .__name__ , base_attrs )
51+ attr_list = AttrList (layer_cls .__name__ , base_attrs )
3152
32- for backend_name in all_backends :
33- backend = backends .get_backend (backend_name )
53+ for backend_name in all_backends :
54+ backend = backends .get_backend (backend_name )
3455
35- backend_cls = backend .create_layer_class (layer_cls )
36- backend_attrs = backend_cls .expected_attributes
56+ backend_cls = backend .create_layer_class (layer_cls )
57+ backend_attrs = backend_cls .expected_attributes
3758
38- diff_atts = [attr for attr in backend_attrs if attr not in base_attrs ] # Sets are faster, but don't preserve order
39- if len (diff_atts ) > 0 :
40- attr_list .add_backend_attrs (backend .name , diff_atts )
59+ diff_atts = [
60+ attr for attr in backend_attrs if attr not in base_attrs
61+ ] # Sets are faster, but don't preserve order
62+ if len (diff_atts ) > 0 :
63+ attr_list .add_backend_attrs (backend .name , diff_atts )
4164
42- attr_map .append (attr_list )
65+ all_layers_attrs .append (attr_list )
66+
67+ for attr_list in all_layers_attrs :
68+ attr_list .sift_backend_attrs ()
69+
70+ return all_layers_attrs
4371
4472
4573def print_attrs (attrs , file ):
@@ -60,40 +88,62 @@ def print_attrs(attrs, file):
6088 if attr .description is not None :
6189 file .write (' * ' + attr .description + '\n \n ' )
6290
91+ if hasattr (attr , 'available_in' ):
92+ file .write (' * Available in: ' + ', ' .join (attr .available_in ) + '\n \n ' )
93+
94+
95+ def write_all_attributes (all_layers_attrs ):
96+ with open ('attributes.rst' , mode = 'w' ) as file :
97+ file .write ('================\n ' )
98+ file .write ('Layer attributes\n ' )
99+ file .write ('================\n \n \n ' )
100+
101+ for attr_list in all_layers_attrs :
102+ file .write (attr_list .cls_name + '\n ' )
103+ file .write ('=' * len (attr_list .cls_name ) + '\n ' )
104+
105+ if len (attr_list .base_attrs ) > 0 :
106+ file .write ('Base attributes\n ' )
107+ file .write ('---------------\n ' )
108+ print_attrs (attr_list .type_attrs , file )
109+
110+ if len (attr_list .type_attrs ) > 0 :
111+ file .write ('Type attributes\n ' )
112+ file .write ('---------------\n ' )
113+ print_attrs (attr_list .base_attrs , file )
114+
115+ if len (attr_list .weight_attrs ) > 0 :
116+ file .write ('Weight attributes\n ' )
117+ file .write ('-----------------\n ' )
118+ print_attrs (attr_list .weight_attrs , file )
119+
120+ if len (attr_list .config_attrs ) > 0 :
121+ file .write ('Configurable attributes\n ' )
122+ file .write ('-----------------------\n ' )
123+ print_attrs (attr_list .config_attrs , file )
124+
125+ if len (attr_list .backend_attrs ) > 0 :
126+ file .write ('Backend-specific attributes\n ' )
127+ file .write ('---------------------------\n ' )
128+ print_attrs (attr_list .unique_backend_attrs , file )
129+
130+
131+ def write_only_configurable (all_layers_attrs ):
132+ with open ('attributes.rst' , mode = 'w' ) as file :
133+ file .write ('================\n ' )
134+ file .write ('Layer attributes\n ' )
135+ file .write ('================\n \n \n ' )
136+
137+ for attr_list in all_layers_attrs :
138+ file .write (attr_list .cls_name + '\n ' )
139+ file .write ('=' * len (attr_list .cls_name ) + '\n ' )
140+
141+ config_attrs = attr_list .only_configurable
142+ if len (config_attrs ) > 0 :
143+ print_attrs (config_attrs , file )
144+
63145
64- with open ('attributes.rst' , mode = 'w' ) as file :
65- file .write ('================\n ' )
66- file .write ('Layer attributes\n ' )
67- file .write ('================\n \n \n ' )
68-
69- for attr_list in attr_map :
70- file .write (attr_list .cls_name + '\n ' )
71- file .write ('=' * len (attr_list .cls_name ) + '\n ' )
72-
73- if len (attr_list .base_attrs ) > 0 :
74- file .write ('Base attributes\n ' )
75- file .write ('---------------\n ' )
76- print_attrs (attr_list .type_attrs , file )
77-
78- if len (attr_list .type_attrs ) > 0 :
79- file .write ('Type attributes\n ' )
80- file .write ('---------------\n ' )
81- print_attrs (attr_list .base_attrs , file )
82-
83- if len (attr_list .weight_attrs ) > 0 :
84- file .write ('Weight attributes\n ' )
85- file .write ('-----------------\n ' )
86- print_attrs (attr_list .weight_attrs , file )
87-
88- if len (attr_list .config_attrs ) > 0 :
89- file .write ('Configurable attributes\n ' )
90- file .write ('-----------------------\n ' )
91- print_attrs (attr_list .config_attrs , file )
92-
93- if len (attr_list .backend_attrs ) > 0 :
94- file .write ('Backend attributes\n ' )
95- file .write ('-----------------------\n ' )
96- for backend , backend_attrs in attr_list .backend_attrs .items ():
97- file .write (backend + '\n ' )
98- file .write ('^' * len (backend ) + '\n ' )
99- print_attrs (backend_attrs , file )
146+ if __name__ == '__main__' :
147+ all_layers_attrs = convert_to_attr_list ()
148+ write_all_attributes (all_layers_attrs )
149+ # write_only_configurable(all_layers_attrs)
0 commit comments