Skip to content
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

Introduce support for generic elementwise binary operations #1040

Draft
wants to merge 24 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9bb8313
Introduce support for generic elementwise binary operations
iksnagreb Apr 12, 2024
f61a538
[Streamline] Fix FoldQuantWeights input order and shape annotations
iksnagreb Nov 13, 2023
8691d3f
Make quantized activation handlers data layout aware
iksnagreb Nov 20, 2023
24acc18
[Streamline] Fix AbsorbAddIntoMultiThreshold assumed input order
iksnagreb Nov 13, 2023
e632328
Fix clipping range issue in RoundAndClipThresholds transformation
iksnagreb Mar 13, 2024
8dd85f4
Rework RoundAndClipThresholds to avoid range and type promotion issues
iksnagreb Apr 6, 2024
8b7c2eb
[Thresholding] Make sure the output of python simulation is float32
iksnagreb Apr 17, 2024
f01d02f
[Tests] Rework test-cases for reworked RoundAndClipThresholds
iksnagreb Apr 6, 2024
023d950
[Streamline] Check validity of broadcasting Add into MultiThreshold
iksnagreb Apr 17, 2024
945db12
[Streamline] Fix backwards-propagating shapes in MoveAddPastMul
iksnagreb Apr 17, 2024
3f13673
[Elementwise] Add InferElementwiseBinaryOperation transformation
iksnagreb Apr 18, 2024
6a6616a
[Tests] Add simple integration test for ElementwiseBinaryOperation
iksnagreb Apr 18, 2024
fd1aedd
[Elementwise] Some cleanup / simplification of generated code
iksnagreb Apr 19, 2024
f010d18
[Streamline] Fix shape propagation of MoveLinearPastEltwiseAdd
iksnagreb Apr 19, 2024
7aaf739
[Tests] Add missing streamlining for testing ElementwiseBinaryOperation
iksnagreb Apr 19, 2024
5268ffe
[Elementwise] Implement bit-width minimization for all specializations
iksnagreb Apr 19, 2024
4769d8e
[Elementwise] Add support for floating-point operations
iksnagreb Apr 19, 2024
87fc002
[Elementwise] Implement get_exp_cycles for ElementwiseBinaryOperation
iksnagreb May 3, 2024
efb1cc9
[Elementwise] Add support for ElementwiseBinaryOperation to SetFolding
iksnagreb May 3, 2024
f34dcfc
[Elementwise] Remove FIFO depths attribute overloads
iksnagreb May 10, 2024
e361cb9
[Elementwise] Add ARRAY_PARTITION and BIND_STORAGE directives
iksnagreb May 17, 2024
653673b
[Streamline] Prevent FactorOutMulSignMagnitude from handling join-nodes
iksnagreb Aug 8, 2024
de97911
[Streamline] Delete initializer datatype annotation after MoveAddPastMul
iksnagreb Aug 8, 2024
dd68078
[Elementwise] Reintroduce FIFO depths attribute overloads
iksnagreb Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ sections=FUTURE,STDLIB,TEST,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
default_section=THIRDPARTY
multi_line_output=3
profile=black
ignore_comments=true
ignore_whitespace=true
honor_noqa=true
47 changes: 47 additions & 0 deletions custom_hls/flatten.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef FLATTEN_HPP
#define FLATTEN_HPP

// HLS arbitrary precision types
#include <ap_int.h>

// Flattens an array of N elements of Type into a single bitvector
template<long unsigned N, class Type>
ap_uint<N * Type::width> flatten(const Type buffer[N]) {
// Inline this small piece of bit merging logic
#pragma HLS INLINE
// Fill a flat word of N times the bit-width of the element type
ap_uint<N * Type::width> flat;
// Merge all N chunks of the tile into the flat bitvector
for(unsigned j = 0; j < N; ++j) {
// Do the merging of all chunks in parallel
#pragma HLS UNROLL
// Insert the chunk into the right place of the
// bitvector
flat((j + 1) * Type::width - 1, j * Type::width) = buffer[j];
}
// Return the buffer flattened into a single bitvector
return flat;
}

// Flattens an array of N elements of float into a single bitvector
template<long unsigned N>
ap_uint<N * 32> flatten(const float buffer[N]) {
// Inline this small piece of bit merging logic
#pragma HLS INLINE
// Fill a flat word of N times the bit-width of the element type
ap_uint<N * 32> flat;
// Merge all N chunks of the tile into the flat bitvector
for(unsigned j = 0; j < N; ++j) {
// Do the merging of all chunks in parallel
#pragma HLS UNROLL
// Insert the chunk into the right place of the
// bitvector
flat((j + 1) * 32 - 1, j * 32) =
// Note: Reinterpret the float as a 32-bit unsigned bit-vector
*reinterpret_cast<const ap_uint<32>*>(&buffer[j]);
}
// Return the buffer flattened into a single bitvector
return flat;
}

#endif // FLATTEN_HPP
29 changes: 27 additions & 2 deletions src/finn/custom_op/fpgadataflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# The base class of all generic custom operations before specializing to either
# HLS or RTL backend
from finn.custom_op.fpgadataflow.hwcustomop import HWCustomOp

# Dictionary of HWCustomOp implementations
custom_op = dict()


# Registers a class into the custom_op dictionary
# Note: This must be defined first, before importing any custom op
# implementation to avoid "importing partially initialized module" issues.
def register_custom_op(cls):
# The class must actually implement HWCustomOp
assert issubclass(cls, HWCustomOp), f"{cls} must subclass {HWCustomOp}"
# Insert the class into the custom_op dictionary by its name
custom_op[cls.__name__] = cls # noqa: Some weird type annotation issue?
# Pass through the class unmodified
return cls


# flake8: noqa
# Disable linting from here, as all import will be flagged E402 and maybe F401


# Import the submodule containing specializations of ElementwiseBinaryOperation
# Note: This will automatically register all decorated classes into this domain
import finn.custom_op.fpgadataflow.elementwise_binary
from finn.custom_op.fpgadataflow.addstreams import AddStreams
from finn.custom_op.fpgadataflow.channelwise_op import ChannelwiseOp
from finn.custom_op.fpgadataflow.concat import StreamingConcat
Expand Down Expand Up @@ -55,8 +82,6 @@
from finn.custom_op.fpgadataflow.upsampler import UpsampleNearestNeighbour
from finn.custom_op.fpgadataflow.vectorvectoractivation import VVAU

custom_op = dict()

# make sure new HLSCustomOp subclasses are imported here so that they get
# registered and plug in correctly into the infrastructure
custom_op["MVAU"] = MVAU
Expand Down
Loading