# Copyright (C) 2023, Advanced Micro Devices, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of FINN nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# 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.

import numpy as np

from finn.custom_op.fpgadataflow.hlsbackend import HLSBackend
from finn.custom_op.fpgadataflow.streamingeltwise import StreamingEltwise


class StreamingEltwise_hls(StreamingEltwise, HLSBackend):
    """Class that corresponds to finn-hlslib StreamingEltwise function."""

    def __init__(self, onnx_node, **kwargs):
        super().__init__(onnx_node, **kwargs)

    def get_nodeattr_types(self):
        my_attrs = {}
        my_attrs.update(StreamingEltwise.get_nodeattr_types(self))
        my_attrs.update(HLSBackend.get_nodeattr_types(self))
        return my_attrs

    def verify_node(self):
        info_messages = []
        # verify that "backend" is set to "fpgadataflow"
        backend_value = self.get_nodeattr("backend")
        if backend_value == "fpgadataflow":
            info_messages.append("Attribute backend is set correctly")
        else:
            info_messages.append('Attribute backend should be set to "fpgadataflow"')

        # verify that all necessary attributes exist
        try:
            self.get_nodeattr("code_gen_dir_cppsim")
            self.get_nodeattr("executable_path")
            self.get_nodeattr("NumChannels")
            self.get_nodeattr("PE")
            self.get_nodeattr("inputDataType0")
            self.get_nodeattr("inputDataType1")
            self.get_nodeattr("eltwiseOp")
            info_messages.append("All necessary attributes exist")
        except Exception:
            info_messages.append("""The required StreamingEltwise attributes do not exist.""")

        return info_messages

    def execute_node(self, context, graph):
        HLSBackend.execute_node(self, context, graph)

    def global_includes(self):
        self.code_gen_dict["$GLOBALS$"] = [
            '#include "eltwise.hpp"',
            '#include "interpret.hpp"',
        ]

        self.code_gen_dict["$GLOBALS$"].extend(
            [
                "template<typename TI1, typename TI2, typename TO>",
                "struct absdiff {",
                "TO operator()(TI1 const &a, TI2 const &b) const {",
                "#pragma HLS inline",
                "return  a>b? a-b : b-a;",
                "}",
                "};",
                "template<typename TI1, typename TI2, typename TO>",
                "struct sub {",
                "TO operator()(TI1 const &a, TI2 const &b) const {",
                "#pragma HLS inline",
                "return  a-b;",
                "}",
                "};",
                "template<typename TI1, typename TI2, typename TO>",
                "struct add {",
                "TO operator()(TI1 const &a, TI2 const &b) const {",
                "#pragma HLS inline",
                "return  a+b;",
                "}",
                "};",
            ]
        )

    def defines(self, var):
        self.code_gen_dict["$DEFINES$"] = []

    def strm_decl(self):
        self.code_gen_dict["$STREAMDECLARATIONS$"] = []
        self.code_gen_dict["$STREAMDECLARATIONS$"].append(
            'hls::stream<ap_uint<{}>> in0_V ("in0_V");'.format(self.get_instream_width(0))
        )
        self.code_gen_dict["$STREAMDECLARATIONS$"].append(
            'hls::stream<ap_uint<{}>> in1_V ("in1_V");'.format(self.get_instream_width(1))
        )
        self.code_gen_dict["$STREAMDECLARATIONS$"].append(
            'hls::stream<ap_uint<{}>> out0_V ("out0_V");'.format(self.get_outstream_width())
        )

    def docompute(self):
        op = self.get_nodeattr("eltwiseOp")
        idt0 = self.get_input_datatype(0)
        idt1 = self.get_input_datatype(1)
        odt = self.get_output_datatype()
        elem_hls_type_0 = idt0.get_hls_datatype_str()
        elem_hls_type_1 = idt1.get_hls_datatype_str()
        out_hls_type = odt.get_hls_datatype_str()
        slice_in0 = "Slice<%s>" % elem_hls_type_0
        slice_in1 = "Slice<%s>" % elem_hls_type_1
        slice_out = "Slice<%s>" % out_hls_type
        eltwise_op_str = self.get_eltwise_op_lambda()
        "%sEltwiseFunction<%s, %s, %s>()" % (
            op,
            elem_hls_type_0,
            elem_hls_type_1,
            out_hls_type,
        )
        self.code_gen_dict["$DOCOMPUTE$"] = [
            """{}<{}, {}, {}, {}, {}, {}>(in0_V, in1_V, out0_V, {});""".format(
                "StreamingEltwise",
                self.get_nodeattr("NumChannels"),
                self.get_nodeattr("PE"),
                int(np.prod(self.get_folded_output_shape()[:-2])),
                slice_in0,
                slice_in1,
                slice_out,
                eltwise_op_str,
            )
        ]

    def blackboxfunction(self):
        self.code_gen_dict["$BLACKBOXFUNCTION$"] = [
            """void {}(hls::stream<ap_uint<{}>> &in0_V, hls::stream<ap_uint<{}>> &in1_V,
                hls::stream<ap_uint<{}>> &out0_V)""".format(
                self.onnx_node.name,
                self.get_nodeattr("PE") * self.get_input_datatype(0).bitwidth(),
                self.get_nodeattr("PE") * self.get_input_datatype(1).bitwidth(),
                self.get_nodeattr("PE") * self.get_output_datatype().bitwidth(),
            )
        ]

    def pragmas(self):
        self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0_V"]
        self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=in1_V")
        self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out0_V")
        self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE ap_ctrl_none port=return")
