-
Notifications
You must be signed in to change notification settings - Fork 472
Keras v3 Support #1116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Keras v3 Support #1116
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
47f3e3c
add keras v3 object parser
calad0i 5debe71
add keras v3 layer handlers
calad0i 755be89
expose kv3 parser to config interface
calad0i a1c2227
add kv3 converter test
calad0i 067ef9e
einsumdense and einsum
calad0i d8bb729
add einsum templates
calad0i 303db72
einsumdense test
calad0i 56c0731
support kv3 parsed batchnorm
calad0i fe3fcd0
fix einsum/einsum dense regression issue
calad0i 54a297e
preemptive distributed_arithmetic flag for einsum ops
calad0i 3509666
update doc for kv3
calad0i c81028e
more documentation
calad0i eed6330
backport validate einsum function
calad0i cda903e
docstring style
calad0i eccde4e
quote format
calad0i 6dfeb99
restore example-models version
calad0i 8284757
pre-commit update
calad0i 35e94d0
Merge branch 'main' into keras-v3
calad0i e5ad92c
kv3 handler update
calad0i 6aec7f6
force keras>=3.10
calad0i 64261aa
isolate merge handlers
calad0i b8ed033
rm abomination
calad0i 009ae8e
mv xpose config gen to utils
calad0i 3ea3490
attributes.attributes -> attributes
calad0i 5d4bdfe
isolate keras v2 and v3 to hls
calad0i 150a3f6
update tests for api changes
calad0i ec914d1
update docs
calad0i 312328e
mv einops to vivado backend, rm unused args
calad0i a15a353
Merge branch 'main' into keras-v3
calad0i 9c585aa
post merge fix
calad0i 8153522
quality-of-life changes
calad0i c4733b2
fix some qol changes
calad0i File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
================ | ||
Keras and QKeras | ||
================ | ||
================================ | ||
Keras and its quantized variants | ||
================================ | ||
|
||
Keras and the quantization library QKeras are well supported in ``hls4ml``. Currently, the Keras v2 (``tf.keras``) is the preferred version, and the future versions of ``hls4ml`` will expand support for Keras v3. The frontend is based on the parsing the serialized json representation of the model. | ||
Keras and the quantization library QKeras are well supported in ``hls4ml``. Both Keras v2 (``tf.keras``) and the new Keras v3 are supported. While the Keras v2 support is based on parsing the serialized json representation of the model, the Keras v3 support uses direct model inspection. | ||
|
||
Currently, ``hls4ml`` can parse most Keras layers, including core layers, convolutional layers, pooling layers, recurrent layers, merging/reshaping layers and activation layers, implemented either via sequential or functional API. Notably missing are the attention and normalization layers. The equivalent QKeras API and quantizers are also supported. The ``Lambda`` layers don't save their state in the serialized format and are thus impossible to parse. In this case, the ``Lambda`` layers can be implemented as custom layers and parsed via the :ref:`Extension API`. | ||
Currently, ``hls4ml`` can parse most Keras layers, including core layers, convolutional layers, pooling layers, recurrent layers, merging/reshaping layers and activation layers, implemented either via sequential or functional API. Notably missing are the attention and normalization layers. The ``Lambda`` layers don't save their state in the serialized format and are thus impossible to parse. In this case, the ``Lambda`` layers can be implemented as custom layers and parsed via the :ref:`Extension API`. | ||
|
||
The ``data_format='channels_first'`` parameter of Keras layers is supported, but not extensively tested. All HLS implementations in ``hls4ml`` are based on ``channels_last`` data format and need to be converted to that format before the HLS code can be emitted. We encourage users of ``channels_first`` to report their experiences to developers on GitHub. | ||
|
||
|
||
* `QKeras <https://github.com/fastmachinelearning/qkeras>`_ | ||
The equivalent QKeras API and its quantizers are also supported by ``hls4ml``. QKeras is not compatible with Keras v3. Currently, only HGQ2 is compatible with Keras v3 (see below). | ||
* `HGQ <https://github.com/calad0i/HGQ>`_ | ||
The equivalent HGQ API is also supported. HGQ is not compatible with Keras v3. See `advanced/HGQ <../advanced/hgq.html>`__ for more information. | ||
* `HGQ2 <https://github.com/calad0i/HGQ2>`_ | ||
HGQ2 is based on Keras v3. Its support in hls4ml is currently under development. | ||
|
||
The development team of ``hls4ml`` is currently exploring options for QKeras alternative and will provide a drop-in replacement API compatible with Keras v3. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from math import ceil | ||
|
||
from hls4ml.backends.backend import get_backend | ||
from hls4ml.backends.template import FunctionCallTemplate, LayerConfigTemplate | ||
from hls4ml.model.layers import Einsum | ||
from hls4ml.utils.transpose_utils import transpose_config_gen | ||
|
||
from .reshaping_templates import transpose_config_template | ||
|
||
# Shared Dense template | ||
# Einsum template | ||
|
||
einsum_config_template = ''' | ||
struct config{index} {{ | ||
typedef config{index}_tpose_inp0 tpose_inp0_config; | ||
typedef config{index}_tpose_inp1 tpose_inp1_config; | ||
typedef config{index}_tpose_out tpose_out_conf; | ||
|
||
typedef {accum_t.name} accum_t; | ||
|
||
// Layer Sizes | ||
static const unsigned n_free0 = {n_free0}; | ||
static const unsigned n_free1 = {n_free1}; | ||
static const unsigned n_contract = {n_contract}; | ||
static const unsigned n_inplace = {n_inplace}; | ||
|
||
// Resource reuse info | ||
static const unsigned io_type = nnet::{iotype}; | ||
static const unsigned strategy = nnet::{strategy}; | ||
static const unsigned reuse_factor = {reuse_factor}; | ||
static const unsigned multiplier_limit = {multiplier_limit}; | ||
static const bool store_weights_in_bram = false; // NOT USED | ||
|
||
template <class x_T, class y_T> | ||
using product = nnet::product::{product_type}<x_T, y_T>; | ||
}}; | ||
''' | ||
|
||
einsum_function_template = 'nnet::einsum<{input0_t}, {input1_t}, {output_t}, {config}>({input0}, {input1}, {output});' | ||
|
||
einsum_include_list = ['nnet_utils/nnet_einsum.h'] | ||
|
||
|
||
class EinsumConfigTemplate(LayerConfigTemplate): | ||
def __init__(self): | ||
super().__init__(Einsum) | ||
self.template = einsum_config_template | ||
|
||
def format(self, node: Einsum): | ||
default_params = self._default_config_params(node) | ||
|
||
strategy = node.attributes['strategy'] | ||
io_type = node.model.config.get_config_value('IOType') | ||
|
||
assert io_type == 'io_parallel', 'EinsumDense layer only supports io_parallel for now' | ||
assert strategy.lower() == 'latency', 'EinsumDense layer only supports Latency strategy for now' | ||
|
||
# EinsumDense config | ||
params = default_params.copy() | ||
params['strategy'] = strategy | ||
params['n_free0'] = node.attributes['n_free0'] | ||
params['n_free1'] = node.attributes['n_free1'] | ||
params['n_contract'] = node.attributes['n_contract'] | ||
params['n_inplace'] = node.attributes['n_inplace'] | ||
inp0_t = node.get_input_variable(node.inputs[0]).type.precision | ||
inp1_t = node.get_input_variable(node.inputs[1]).type.precision | ||
params['product_type'] = get_backend('vivado').product_type(inp0_t, inp1_t) | ||
|
||
total_mults = params['n_free0'] * params['n_free1'] * params['n_contract'] * params['n_inplace'] | ||
params['multiplier_limit'] = ceil(total_mults / params['reuse_factor']) | ||
|
||
einsum_conf = self.template.format(**params) | ||
|
||
# inp/out transpose config | ||
inp0_shape = node.attributes['inp0_shape'] | ||
inp1_shape = node.attributes['inp1_shape'] | ||
out_interpert_shape = node.attributes['out_interpert_shape'] | ||
inp0_tpose_idxs = node.attributes['inp0_tpose_idxs'] | ||
inp1_tpose_idxs = node.attributes['inp1_tpose_idxs'] | ||
out_tpose_idxs = node.attributes['out_tpose_idxs'] | ||
tpose_inp0_config_name = f'config{node.index}_tpose_inp0' | ||
tpose_inp1_config_name = f'config{node.index}_tpose_inp1' | ||
tpose_out_conf_name = f'config{node.index}_tpose_out' | ||
|
||
conf = transpose_config_gen(tpose_inp0_config_name, inp0_shape, inp0_tpose_idxs) | ||
inp0_tpose_conf = transpose_config_template.format(**conf) | ||
conf = transpose_config_gen(tpose_inp1_config_name, inp1_shape, inp1_tpose_idxs) | ||
inp1_tpose_conf = transpose_config_template.format(**conf) | ||
conf = transpose_config_gen(tpose_out_conf_name, out_interpert_shape, out_tpose_idxs) | ||
out_tpose_conf = transpose_config_template.format(**conf) | ||
|
||
return '\n\n'.join((inp0_tpose_conf, inp1_tpose_conf, out_tpose_conf, einsum_conf)) | ||
|
||
|
||
class EinsumFunctionTemplate(FunctionCallTemplate): | ||
def __init__(self): | ||
super().__init__(Einsum, include_header=einsum_include_list) | ||
self.template = einsum_function_template | ||
|
||
def format(self, node: Einsum): | ||
params = {} | ||
params['config'] = f'config{node.index}' | ||
params['input0_t'] = node.get_input_variable(node.inputs[0]).type.name | ||
params['input1_t'] = node.get_input_variable(node.inputs[1]).type.name | ||
params['output_t'] = node.get_output_variable().type.name | ||
params['input0'] = node.get_input_variable(node.inputs[0]).name | ||
params['input1'] = node.get_input_variable(node.inputs[1]).name | ||
params['output'] = node.get_output_variable().name | ||
return self.template.format(**params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from hls4ml.backends.backend import get_backend | ||
from hls4ml.backends.template import FunctionCallTemplate, LayerConfigTemplate | ||
from hls4ml.model.layers import EinsumDense | ||
from hls4ml.utils.transpose_utils import transpose_config_gen | ||
|
||
from .reshaping_templates import transpose_config_template | ||
|
||
# Shared Dense template | ||
|
||
dense_config_template = '''struct config{index}_dense : nnet::dense_config {{ | ||
static const unsigned n_in = {n_in}; | ||
static const unsigned n_out = {n_out}; | ||
static const unsigned reuse_factor = {reuse}; | ||
static const unsigned strategy = nnet::{strategy}; | ||
static const unsigned n_zeros = {nzeros}; | ||
static const unsigned multiplier_limit = DIV_ROUNDUP(n_in * n_out, reuse_factor) - n_zeros / reuse_factor; | ||
typedef {accum_t.name} accum_t; | ||
typedef {bias_t.name} bias_t; | ||
typedef {weight_t.name} weight_t; | ||
template<class data_T, class res_T, class CONFIG_T> | ||
using kernel = nnet::{dense_function}<data_T, res_T, CONFIG_T>; | ||
template<class x_T, class y_T> | ||
using product = nnet::product::{product_type}<x_T, y_T>; | ||
}};\n''' | ||
|
||
# EinsumDense template | ||
|
||
einsum_dense_config_template = ''' | ||
struct config{index} {{ | ||
typedef config{index}_tpose_inp tpose_inp_conf; | ||
typedef config{index}_tpose_out tpose_out_conf; | ||
{kernel_config}; | ||
|
||
typedef {accum_t.name} accum_t; | ||
typedef {bias_t.name} bias_t; | ||
|
||
// Layer Sizes | ||
static const unsigned n_free_data = {n_free_data}; | ||
static const unsigned n_free_kernel = {n_free_kernel}; | ||
static const unsigned n_contract = {n_contract}; | ||
static const unsigned n_inplace = {n_inplace}; | ||
|
||
// Resource reuse info | ||
static const unsigned io_type = nnet::{iotype}; | ||
static const unsigned strategy = nnet::{strategy}; | ||
static const unsigned reuse_factor = {reuse_factor}; | ||
static const unsigned parallelization_factor = {parallelization_factor}; // Only useful when n_inplace > 1 | ||
}}; | ||
''' | ||
|
||
einsum_dense_function_template = 'nnet::einsum_dense<{input_t}, {output_t}, {config}>({input}, {output}, {w}, {b});' | ||
einsum_dense_da_function_template = 'nnet::einsum_dense<{input_t}, {output_t}, {config}>({input}, {output}, {b});' | ||
|
||
einsum_dense_include_list = ['nnet_utils/nnet_einsum_dense.h', 'nnet_utils/nnet_dense.h'] | ||
|
||
|
||
class EinsumDenseConfigTemplate(LayerConfigTemplate): | ||
def __init__(self): | ||
super().__init__(EinsumDense) | ||
self.template = einsum_dense_config_template | ||
self.dense_template = dense_config_template | ||
|
||
def dense_config(self, node: EinsumDense): | ||
dense_params = self._default_config_params(node) | ||
strategy = node.attributes['strategy'] | ||
dense_params['strategy'] = strategy | ||
dense_params['n_in'] = node.attributes['n_contract'] | ||
dense_params['n_out'] = node.attributes['n_free_kernel'] | ||
if node.attributes['n_inplace'] == 1: | ||
dense_params['nzeros'] = node.get_weights('weight').nzeros # type: ignore | ||
else: | ||
dense_params['nzeros'] = '-1; // Not making sense when kernels are switching' | ||
dense_params['product_type'] = get_backend('vivado').product_type( | ||
node.get_input_variable().type.precision, node.get_weights('weight').type.precision # type: ignore | ||
) | ||
|
||
dense_params['dense_function'] = 'DenseLatency' # Latency only for now | ||
|
||
dense_config = self.dense_template.format(**dense_params) | ||
return dense_config | ||
|
||
def format(self, node: EinsumDense): | ||
default_params = self._default_config_params(node) | ||
|
||
strategy = node.attributes['strategy'] | ||
io_type = node.model.config.get_config_value('IOType') | ||
|
||
assert io_type == 'io_parallel', 'EinsumDense layer only supports io_parallel and distributed_arithmetic' | ||
|
||
# EinsumDense config | ||
params = default_params.copy() | ||
params['strategy'] = strategy | ||
params['n_free_data'] = node.attributes['n_free_data'] | ||
params['n_free_kernel'] = node.attributes['n_free_kernel'] | ||
params['n_contract'] = node.attributes['n_contract'] | ||
params['n_inplace'] = node.attributes['n_inplace'] | ||
if strategy.lower() == 'latency': | ||
params['kernel_config'] = f'typedef config{node.index}_dense dense_conf' | ||
else: | ||
assert strategy.lower() == 'distributed_arithmetic', 'EinsumDense layer only supports Latency strategy for now' | ||
inp_t = node.get_input_variable().type.name | ||
result_t = node.get_output_variable().type.name | ||
index = node.index | ||
conf = f'constexpr static auto da_kernel = nnet::einsum_dense{index}_da_kernel<{inp_t}, {result_t}>' | ||
params['kernel_config'] = conf | ||
pf = node.attributes['parallelization_factor'] | ||
if pf < 0: | ||
pf = params['n_inplace'] | ||
params['parallelization_factor'] = pf | ||
|
||
einsum_conf = self.template.format(**params) | ||
|
||
# inp/out transpose config | ||
inp_shape = node.attributes['inp_shape'] | ||
out_interpert_shape = node.attributes['out_interpert_shape'] | ||
inp_tpose_idxs = node.attributes['inp_tpose_idxs'] | ||
out_tpose_idxs = node.attributes['out_tpose_idxs'] | ||
tpose_inp_conf_name = f'config{node.index}_tpose_inp' | ||
tpose_out_conf_name = f'config{node.index}_tpose_out' | ||
|
||
conf = transpose_config_gen(tpose_inp_conf_name, inp_shape, inp_tpose_idxs) | ||
inp_tpose_conf = transpose_config_template.format(**conf) | ||
conf = transpose_config_gen(tpose_out_conf_name, out_interpert_shape, out_tpose_idxs) | ||
out_tpose_conf = transpose_config_template.format(**conf) | ||
|
||
if strategy.lower() == 'distributed_arithmetic': | ||
return '\n\n'.join((inp_tpose_conf, out_tpose_conf, einsum_conf)) | ||
|
||
dense_config = self.dense_config(node) | ||
return '\n\n'.join((inp_tpose_conf, out_tpose_conf, dense_config, einsum_conf)) | ||
|
||
|
||
class EinsumDenseFunctionTemplate(FunctionCallTemplate): | ||
def __init__(self): | ||
super().__init__(EinsumDense, include_header=einsum_dense_include_list) | ||
self.template = einsum_dense_function_template | ||
|
||
def format(self, node): | ||
params = self._default_function_params(node) | ||
params['b'] = node.get_weights('bias').name | ||
|
||
strategy = node.attributes['strategy'] | ||
if strategy == 'distributed_arithmetic': | ||
return einsum_dense_da_function_template.format(**params) | ||
|
||
params['w'] = node.get_weights('weight').name | ||
return einsum_dense_function_template.format(**params) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover comment