Skip to content

Commit a00f9c6

Browse files
committed
Use google style docstrings everywhere
1 parent 8f1f709 commit a00f9c6

File tree

6 files changed

+154
-221
lines changed

6 files changed

+154
-221
lines changed

docs/flows.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ New optimizers can be registered with the :py:func:`~hls4ml.model.optimizer.opti
4949

5050
Flows
5151
-----
52-
A :py:class:`~hls4ml.model.flow.flow.Flow` is an ordered set of optimizers that represent a single stage in the conversion process. The optimizers
52+
A :py:class:`~hls4ml.model.flow.flow.Flow` is an ordered set of optimizers that represents a single stage in the conversion process. The optimizers
5353
from a flow are applied in sequence until they no longer make changes to the model graph (controlled by the ``transform`` return value), after which
5454
the next flow (stage) can start. Flows may require that other flows are applied before them, ensuring the model graph is in a desired state before a
5555
flow starts. The function :py:func:`~hls4ml.model.flow.flow.register_flow` is used to register a new flow. Flows are applied on a model graph with

hls4ml/backends/vivado_accelerator/vivado_accelerator_backend.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def build(
5858
return parse_vivado_report(model.config.get_output_dir())
5959

6060
def make_xclbin(self, model, platform='xilinx_u250_xdma_201830_2'):
61-
"""
61+
"""Create the xclbin for the given model and target platform.
6262
63-
Parameters
64-
----------
65-
- model : compiled and built hls_model.
66-
- platform : development Target Platform, must be installed first. On the host machine is required only the
67-
deployment target platform, both can be found on the Getting Started section of the Alveo card.
63+
Args:
64+
model (ModelGraph): Compiled and build model.
65+
platform (str, optional): Development/Deployment target platform, must be installed first.
66+
The host machine only requires the deployment target platform. Refer to the Getting Started section of
67+
the Alveo guide. Defaults to 'xilinx_u250_xdma_201830_2'.
6868
"""
6969
curr_dir = os.getcwd()
7070
abs_path_dir = os.path.abspath(model.config.get_output_dir())

hls4ml/converters/__init__.py

Lines changed: 60 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def convert_from_keras_model(
194194
hls_config=None,
195195
**kwargs,
196196
):
197-
"""Convert to hls4ml model based on the provided configuration.
197+
"""Convert Keras model to hls4ml model based on the provided configuration.
198198
199199
Args:
200200
model: Keras model to convert
@@ -221,7 +221,7 @@ def convert_from_keras_model(
221221
kwargs** (dict, optional): Additional parameters that will be used to create the config of the specified backend
222222
223223
Raises:
224-
Exception: If precision and reuse factor are not present in 'hls_config'
224+
Exception: If precision and reuse factor are not present in 'hls_config'.
225225
226226
Returns:
227227
ModelGraph: hls4ml model.
@@ -256,54 +256,35 @@ def convert_from_pytorch_model(
256256
hls_config=None,
257257
**kwargs,
258258
):
259-
"""
259+
"""Convert PyTorch model to hls4ml model based on the provided configuration.
260+
261+
Args:
262+
model: PyTorch model to conert.
263+
input_shape (list): The shape of the input tensor.
264+
output_dir (str, optional): Output directory of the generated HLS project. Defaults to 'my-hls-test'.
265+
project_name (str, optional): Name of the HLS project. Defaults to 'myproject'.
266+
input_data_tb (str, optional): String representing the path of input data in .npy or .dat format that will be
267+
used during csim and cosim. Defaults to None.
268+
output_data_tb (str, optional): String representing the path of output data in .npy or .dat format that will be
269+
used during csim and cosim. Defaults to None.
270+
backend (str, optional): Name of the backend to use, e.g., 'Vivado' or 'Quartus'. Defaults to 'Vivado'.
271+
board (str, optional): One of target boards specified in `supported_board.json` file. If set to `None` a default
272+
device of a backend will be used. See documentation of the backend used.
273+
part (str, optional): The FPGA part. If set to `None` a default part of a backend will be used.
274+
See documentation of the backend used. Note that if `board` is specified, the part associated to that board
275+
will overwrite any part passed as a parameter.
276+
clock_period (int, optional): Clock period of the design.
277+
Defaults to 5.
278+
io_type (str, optional): Type of implementation used. One of
279+
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
280+
hls_config (dict, optional): The HLS config.
281+
kwargs** (dict, optional): Additional parameters that will be used to create the config of the specified backend.
282+
283+
Raises:
284+
Exception: If precision and reuse factor are not present in 'hls_config'.
260285
261-
Convert a Pytorch model to a hls model.
262-
263-
Parameters
264-
----------
265-
model : Pytorch model object.
266-
Model to be converted to hls model object.
267-
input_shape : @todo: to be filled
268-
output_dir (str, optional): Output directory of the generated HLS
269-
project. Defaults to 'my-hls-test'.
270-
project_name (str, optional): Name of the HLS project.
271-
Defaults to 'myproject'.
272-
input_data_tb (str, optional): String representing the path of input data in .npy or .dat format that will be
273-
used during csim and cosim.
274-
output_data_tb (str, optional): String representing the path of output data in .npy or .dat format that will be
275-
used during csim and cosim.
276-
backend (str, optional): Name of the backend to use, e.g., 'Vivado'
277-
or 'Quartus'.
278-
board (str, optional): One of target boards specified in `supported_board.json` file. If set to `None` a default
279-
device of a backend will be used. See documentation of the backend used.
280-
part (str, optional): The FPGA part. If set to `None` a default part of a backend will be used.
281-
See documentation of the backend used. Note that if `board` is specified, the part associated to that board
282-
will overwrite any part passed as a parameter.
283-
clock_period (int, optional): Clock period of the design.
284-
Defaults to 5.
285-
io_type (str, optional): Type of implementation used. One of
286-
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
287-
hls_config (dict, optional): The HLS config.
288-
kwargs** (dict, optional): Additional parameters that will be used to create the config of the specified backend
289-
290-
Returns
291-
-------
292-
ModelGraph : hls4ml model object.
293-
294-
See Also
295-
--------
296-
hls4ml.convert_from_keras_model, hls4ml.convert_from_onnx_model
297-
298-
Examples
299-
--------
300-
>>> import hls4ml
301-
>>> config = hls4ml.utils.config_from_pytorch_model(model, granularity='model')
302-
>>> hls_model = hls4ml.converters.convert_from_pytorch_model(model, hls_config=config)
303-
304-
Notes
305-
-----
306-
Only sequential Pytorch models are supported for now.
286+
Returns:
287+
ModelGraph: hls4ml model.
307288
"""
308289

309290
config = create_config(output_dir=output_dir, project_name=project_name, backend=backend, **kwargs)
@@ -335,49 +316,37 @@ def convert_from_onnx_model(
335316
hls_config=None,
336317
**kwargs,
337318
):
338-
"""
319+
"""Convert Keras model to hls4ml model based on the provided configuration.
320+
321+
Args:
322+
model: ONNX model to convert.
323+
output_dir (str, optional): Output directory of the generated HLS
324+
project. Defaults to 'my-hls-test'.
325+
project_name (str, optional): Name of the HLS project.
326+
Defaults to 'myproject'.
327+
input_data_tb (str, optional): String representing the path of input data in .npy or .dat format that will be
328+
used during csim and cosim.
329+
output_data_tb (str, optional): String representing the path of output data in .npy or .dat format that will be
330+
used during csim and cosim.
331+
backend (str, optional): Name of the backend to use, e.g., 'Vivado'
332+
or 'Quartus'.
333+
board (str, optional): One of target boards specified in `supported_board.json` file. If set to `None` a default
334+
device of a backend will be used. See documentation of the backend used.
335+
part (str, optional): The FPGA part. If set to `None` a default part of a backend will be used.
336+
See documentation of the backend used. Note that if `board` is specified, the part associated to that board
337+
will overwrite any part passed as a parameter.
338+
clock_period (int, optional): Clock period of the design.
339+
Defaults to 5.
340+
io_type (str, optional): Type of implementation used. One of
341+
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
342+
hls_config (dict, optional): The HLS config.
343+
kwargs** (dict, optional): Additional parameters that will be used to create the config of the specified backend
344+
345+
Raises:
346+
Exception: If precision and reuse factor are not present in 'hls_config'.
339347
340-
Convert an ONNX model to a hls model.
341-
342-
Parameters
343-
----------
344-
model : ONNX model object.
345-
Model to be converted to hls model object.
346-
output_dir (str, optional): Output directory of the generated HLS
347-
project. Defaults to 'my-hls-test'.
348-
project_name (str, optional): Name of the HLS project.
349-
Defaults to 'myproject'.
350-
input_data_tb (str, optional): String representing the path of input data in .npy or .dat format that will be
351-
used during csim and cosim.
352-
output_data_tb (str, optional): String representing the path of output data in .npy or .dat format that will be
353-
used during csim and cosim.
354-
backend (str, optional): Name of the backend to use, e.g., 'Vivado'
355-
or 'Quartus'.
356-
board (str, optional): One of target boards specified in `supported_board.json` file. If set to `None` a default
357-
device of a backend will be used. See documentation of the backend used.
358-
part (str, optional): The FPGA part. If set to `None` a default part of a backend will be used.
359-
See documentation of the backend used. Note that if `board` is specified, the part associated to that board
360-
will overwrite any part passed as a parameter.
361-
clock_period (int, optional): Clock period of the design.
362-
Defaults to 5.
363-
io_type (str, optional): Type of implementation used. One of
364-
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
365-
hls_config (dict, optional): The HLS config.
366-
kwargs** (dict, optional): Additional parameters that will be used to create the config of the specified backend
367-
368-
Returns
369-
-------
370-
ModelGraph : hls4ml model object.
371-
372-
See Also
373-
--------
374-
hls4ml.convert_from_keras_model, hls4ml.convert_from_pytorch_model
375-
376-
Examples
377-
--------
378-
>>> import hls4ml
379-
>>> config = hls4ml.utils.config_from_onnx_model(model, granularity='model')
380-
>>> hls_model = hls4ml.converters.convert_from_onnx_model(model, hls_config=config)
348+
Returns:
349+
ModelGraph: hls4ml model.
381350
"""
382351

383352
config = create_config(output_dir=output_dir, project_name=project_name, backend=backend, **kwargs)

hls4ml/converters/onnx_to_hls.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@ def __init__(self, model):
2929
def get_weights_data(self, layer_name, var_name):
3030
"""Extract weights data from ONNX model.
3131
32-
Parameters
33-
----------
34-
layer_name : string
35-
layer's name in the ONNX model
36-
var_name : string
37-
variable to be extracted
38-
39-
Returns
40-
-------
41-
data : numpy array
42-
extracted weights data
32+
Args:
33+
layer_name (str): Layer's name in the ONNX model.
34+
var_name (str): Variable to be extracted.
4335
36+
Returns:
37+
ndarray: Extracted weights data.
4438
"""
4539
# Get the node associated with the layer name
4640
node = next(node for node in self.model.graph.node if node.name == layer_name)
@@ -218,17 +212,15 @@ def get_out_layer_name(graph):
218212
def onnx_to_hls(config):
219213
"""Convert onnx model to hls model from configuration.
220214
221-
Parameters
222-
----------
223-
config: dict
224-
onnx configuration from yaml file or passed through API.
215+
Args:
216+
config (dict): ONNX configuration from yaml file or passed through API.
225217
226-
Returns
227-
-------
228-
ModelGraph : hls4ml model object
218+
Raises:
219+
Exception: Raised if an unsupported operation is found in the ONNX model.
229220
221+
Returns:
222+
ModelGraph: hls4ml model object
230223
"""
231-
232224
# This is a list of dictionaries to hold all the layer info we need to generate HLS
233225
layer_list = []
234226

hls4ml/model/profiling.py

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -422,31 +422,22 @@ def activations_torch(model, X, fmt='longform', plot='boxplot'):
422422

423423

424424
def numerical(model=None, hls_model=None, X=None, plot='boxplot'):
425-
"""
426-
Perform numerical profiling of a model
427-
428-
Parameters
429-
----------
430-
model : keras or pytorch model
431-
The model to profile
432-
hls_model : ModelGraph
433-
The ModelGraph to profile
434-
X : array-like, optional
435-
Test data on which to evaluate the model to profile activations
436-
Must be formatted suitably for the ``model.predict(X)`` method
437-
plot : str, optional
438-
The type of plot to produce.
439-
Options are: 'boxplot' (default), 'violinplot', 'histogram',
440-
'FacetGrid'
441-
442-
Returns
443-
-------
444-
tuple
445-
The quadruple of produced figures. First weights and biases
446-
for the pre- and post-optimization models respectively,
447-
then activations for the pre- and post-optimization models
448-
respectively. (Optimizations are applied to an ModelGraph by hls4ml,
449-
a post-optimization ModelGraph is a final model)
425+
"""Perform numerical profiling of a model.
426+
427+
Args:
428+
model (optional): Keras of PyTorch model. Defaults to None.
429+
hls_model (ModelGraph, optional): The ModelGraph to profile. Defaults to None.
430+
X (ndarray, optional): Test data on which to evaluate the model to profile activations.
431+
Must be formatted suitably for the ``model.predict(X)``. Defaults to None.
432+
plot (str, optional): The type of plot to produce. Options are: 'boxplot' (default), 'violinplot', 'histogram',
433+
'FacetGrid'. Defaults to 'boxplot'.
434+
435+
Returns:
436+
tuple: The quadruple of produced figures. First weights and biases
437+
for the pre- and post-optimization models respectively,
438+
then activations for the pre- and post-optimization models
439+
respectively. (Optimizations are applied to an ModelGraph by hls4ml,
440+
a post-optimization ModelGraph is a final model).
450441
"""
451442
wp, wph, ap, aph = None, None, None, None
452443

@@ -554,21 +545,15 @@ def _get_output(layer, X, model_input):
554545

555546

556547
def get_ymodel_keras(keras_model, X):
557-
"""
558-
Calculate each layer's ouput and put them into a dictionary
559-
560-
Parameters
561-
----------
562-
keras_model :
563-
a keras model
564-
X : array-like
565-
Test data on which to evaluate the model to profile activations.
566-
Must be formatted suitably for the ``model.predict(X)`` method.
567-
568-
Returns
569-
-------
570-
dictionary
571-
A dictionary in the form {"layer_name": ouput array of layer}
548+
"""Calculate each layer's ouput and put them into a dictionary.
549+
550+
Args:
551+
keras_model (_type_): A keras Model
552+
X (ndarray): Test data on which to evaluate the model to profile activations.
553+
Must be formatted suitably for the ``model.predict(X)``.
554+
555+
Returns:
556+
dict: A dictionary in the form {"layer_name": ouput array of layer}.
572557
"""
573558

574559
ymodel = {}
@@ -668,30 +653,20 @@ def _dist_diff(ymodel, ysim):
668653

669654

670655
def compare(keras_model, hls_model, X, plot_type="dist_diff"):
671-
"""
672-
Compare each layer's output in keras and hls model. Note that the hls_model should not be compiled before using this.
673-
674-
Parameters
675-
----------
676-
keras_model :
677-
original keras model
678-
hls_model :
679-
converted ModelGraph, with "Trace:True" in the configuration file.
680-
X : array-like
681-
Input for the model.
682-
plot_type : string
683-
different methods to visualize the y_model and y_sim differences.
684-
Possible options include:
685-
686-
- 'norm_diff' : square root of the sum of the squares of the differences
687-
between each output vectors
688-
- 'dist_diff' : The normalized distribution of the differences of the elements
689-
between two output vectors
690-
691-
Returns
692-
-------
693-
matplotlib figure
694-
plot object of the histogram depicting the difference in each layer's output
656+
"""Compare each layer's output in keras and hls model. Note that the hls_model should not be compiled before using this.
657+
658+
Args:
659+
keras_model: Original keras model.
660+
hls_model (ModelGraph): Converted ModelGraph, with "Trace:True" in the configuration file.
661+
X (ndarray): Input tensor for the model.
662+
plot_type (str, optional): Different methods to visualize the y_model and y_sim differences.
663+
Possible options include:
664+
- 'norm_diff':: square root of the sum of the squares of the differences between each output vectors.
665+
- 'dist_diff':: The normalized distribution of the differences of the elements between two output vectors.
666+
Defaults to "dist_diff".
667+
668+
Returns:
669+
matplotlib figure: Plot object of the histogram depicting the difference in each layer's output.
695670
"""
696671

697672
# Take in output from both models

0 commit comments

Comments
 (0)