Core Components
The IR consists of three main components:- Layer classes - Nodes in the model graph
- Attributes - Metadata and configuration for each layer
- Types and Variables - Precision information and data structures
Layer Class Architecture
Base Layer Class
All layers inherit from theLayer base class defined in hls4ml/model/layers.py:
hls4ml/model/layers.py:50-62
Layer Initialization
When a layer is created, it goes through several initialization steps:hls4ml/model/layers.py:85-104
Attribute System
The attribute system provides structured metadata for layers. Attributes are defined using theAttribute class and its subclasses.
Attribute Types
Attribute
Basic attribute with name, type, and default value
ConfigurableAttribute
User-modifiable attributes like
trace flagTypeAttribute
Stores precision types (e.g.,
result_t, accum_t)WeightAttribute
Stores weight variables for parameters
Expected Attributes
Each layer class defines its expected attributes:hls4ml/model/layers.py:490-498
Attribute Mappings
The IR provides specialized views of attributes through mapping classes:Layer Examples
Input Layer
hls4ml/model/layers.py:381-392
Dense Layer
hls4ml/model/layers.py:490-505
Convolutional Layer
hls4ml/model/layers.py:625-644
Variables and Types
Adding Output Variables
Layers create output variables to represent their outputs:hls4ml/model/layers.py:263-279
Adding Weight Variables
Layers with parameters add weight variables:hls4ml/model/layers.py:306-358
Accessing Layer Information
Getting Input/Output Nodes
Accessing Weights and Variables
hls4ml/model/layers.py:254-261
Layer Registration
Layers are registered in a globallayer_map that maps class names to layer classes. Backends can extend layer classes to add backend-specific functionality:
hls4ml/model/graph.py:570-572
Best Practices
Define Expected Attributes
Define Expected Attributes
Always define
_expected_attributes for custom layers to ensure proper validation and initialization.Use Attribute Types
Use Attribute Types
Use appropriate attribute subclasses (
TypeAttribute, WeightAttribute, etc.) to enable proper handling by the system.Initialize Variables
Initialize Variables
Call
add_output_variable() and add_weights_variable() in the initialize() method to create proper IR structures.Access Through Mappings
Access Through Mappings
Use specialized mappings (
layer.weights, layer.variables, layer.types) for type-safe attribute access.Related Documentation
Model Graph
Learn about graph operations and transformations
Optimization Flows
Understand the optimization pass system
