|
| 1 | +import numbers |
| 2 | + |
| 3 | +import hls4ml.backends as backends |
| 4 | +import hls4ml.model.attributes as attributes |
| 5 | +import hls4ml.model.layers as layers |
| 6 | + |
| 7 | + |
| 8 | +class AttrList: |
| 9 | + def __init__(self, cls_name, cls_attrs) -> None: |
| 10 | + self.cls_name = cls_name |
| 11 | + self.config_attrs = [attr for attr in cls_attrs if attr.configurable is True] |
| 12 | + self.type_attrs = [attr for attr in cls_attrs if attr.__class__.__name__ == 'TypeAttribute'] |
| 13 | + self.weight_attrs = [attr for attr in cls_attrs if attr.__class__.__name__ == 'WeightAttribute'] |
| 14 | + self.base_attrs = [attr for attr in cls_attrs if attr not in self.config_attrs + self.type_attrs + self.weight_attrs] |
| 15 | + self.backend_attrs = {} |
| 16 | + self.reverse_backend_attrs = [] # Will hold (attr, backend_name) pairs, used temporarily |
| 17 | + self.unique_backend_attrs = [] |
| 18 | + |
| 19 | + def add_backend_attrs(self, backend_name, backend_attrs): |
| 20 | + self.backend_attrs[backend_name] = backend_attrs |
| 21 | + |
| 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 | + |
| 41 | + |
| 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 = [] |
| 47 | + |
| 48 | + for layer_cls in all_layers: |
| 49 | + base_attrs = layer_cls.expected_attributes |
| 50 | + |
| 51 | + attr_list = AttrList(layer_cls.__name__, base_attrs) |
| 52 | + |
| 53 | + for backend_name in all_backends: |
| 54 | + backend = backends.get_backend(backend_name) |
| 55 | + |
| 56 | + backend_cls = backend.create_layer_class(layer_cls) |
| 57 | + backend_attrs = backend_cls.expected_attributes |
| 58 | + |
| 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) |
| 64 | + |
| 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 |
| 71 | + |
| 72 | + |
| 73 | +def print_attrs(attrs, file): |
| 74 | + for attr in attrs: |
| 75 | + if attr.value_type == numbers.Integral: |
| 76 | + vtype = 'int' |
| 77 | + elif attr.__class__ == attributes.ChoiceAttribute: |
| 78 | + choices = ','.join([str(c) for c in attr.choices]) |
| 79 | + vtype = f'list [{choices}]' |
| 80 | + else: |
| 81 | + vtype = attr.value_type.__name__ if hasattr(attr.value_type, '__name__') else str(attr.value_type) |
| 82 | + |
| 83 | + if attr.default is None: |
| 84 | + file.write('* ' + attr.name + ': ' + vtype + '\n\n') |
| 85 | + else: |
| 86 | + file.write('* ' + attr.name + ': ' + vtype + ' (Default: ' + str(attr.default) + ')\n\n') |
| 87 | + |
| 88 | + if attr.description is not None: |
| 89 | + file.write(' * ' + attr.description + '\n\n') |
| 90 | + |
| 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 | + |
| 145 | + |
| 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