namespace internal

// structs

struct MULTINB_DATA

template <>
struct maxMinValue <double>

template <typename DT>
struct maxMinValue

// unions

template <typename MType>
union f_cast

template <>
union f_cast <double>

template <>
union f_cast <unsigned long long>

template <>
union f_cast <unsigned int>

template <>
union f_cast <float>

predictFun

#include "xf_data_analytics/classification/decision_tree_predict.hpp"
template <
    typename MType,
    unsigned WD,
    unsigned MAX_FEA_NUM,
    unsigned MAX_TREE_DEPTH,
    unsigned MAX_CAT_BITS
    >
void predictFun (
    ap_uint <WD> onesample [MAX_FEA_NUM],
    struct Node <MType> nodes [(MAX_TREE_DEPTH+1)/2+1][MAX_NODES_NUM],
    ap_uint <MAX_CAT_BITS>& cat_id
    )

predictFun implements the detailed predict functions.

This function read one sample, and output a category id

Parameters:

MType The data type of sample
WD The width of data type MType, can get by sizeof(MType)
MAX_FEA_NUM Max feature num function can support
MAX_TREE_DEPTH Max tree depth function can support
MAX_CAT_BITS The category max bit number, the max supported category number is (1<<MAX_CAT_BITS)
onesample One sample
nodes Decision tree (Decision Tree node array)
cat_id Category id

getPredictions

#include "xf_data_analytics/classification/decision_tree_predict.hpp"
template <
    typename MType,
    unsigned WD,
    unsigned MAX_FEA_NUM,
    unsigned MAX_TREE_DEPTH,
    unsigned MAX_CAT_BITS
    >
void getPredictions (
    hls::stream <ap_uint <WD>> dstrm_batch [MAX_FEA_NUM],
    hls::stream <bool>& estrm_batch,
    struct Node <MType> nodes [(MAX_TREE_DEPTH+1)/2+1][MAX_NODES_NUM],
    hls::stream <ap_uint <MAX_CAT_BITS>>& predictionsStrm,
    hls::stream <bool>& predictionsTag
    )

getPredictions wrap predictFun to implement batch samples predicion.

This function read sample streams, and output prediction result into a stream

Parameters:

MType The data type of sample
WD The width of data type MType, can get by sizeof(MType)
MAX_FEA_NUM Max feature num function can support
MAX_TREE_DEPTH Max tree depth function can support
MAX_CAT_BITS The category max bit number, the max supported category number is (1<<MAX_CAT_BITS)
dstrm_batch Input data streams of ap_uint<WD>
estrm_batch End flag stream for input data
nodes Decision tree (Decision Tree node array)
predictionsStrm Output data streams
predictionsTagStrm End flag stream for output

getTree

#include "xf_data_analytics/classification/decision_tree_predict.hpp"
template <
    typename MType,
    unsigned MAX_TREE_DEPTH
    >
void getTree (
    hls::stream <ap_uint <512>>& tree_strm,
    hls::stream <bool>& eTag,
    struct Node <MType> nodes [(MAX_TREE_DEPTH+1)/2+1][MAX_NODES_NUM]
    )

getTree loads 512-bit decision tree into a list of Node

Note that nodes is duplicated (max_tree_depth+1)/2+1 times for unroll each tree layer in getPredictions.

Parameters:

MType The data type of sample
MAX_TREE_DEPTH Max tree depth function can support
tree_strm Decision tree streams
eTag End flag stream for decision tree nodes
nodes Decision tree (Decision Tree node array)

initUram

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int PU>
void initUram (
    const int num_of_class,
    const int num_of_term,
    ap_uint <72>* lh_vector [PU],
    ap_uint <96>* prior_vector [PU+1]
    )

initUram, clear the content of given URAM entitiy

dispatchUnit

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int WL>
void dispatchUnit (
    hls::stream <ap_uint <64>>& i_data_strm,
    hls::stream <bool>& i_e_strm,
    hls::stream <MULTINB_DATA> o_data_strm [1<< WL],
    hls::stream <bool> o_e_strm [1<< WL]
    )

dispatchUnit, distribute one source data stream to multiple output stream based on its tag

merge8To1

#include "xf_data_analytics/classification/naive_bayes.hpp"
void merge8To1 (
    hls::stream <MULTINB_DATA>& i0_data_strm,
    hls::stream <MULTINB_DATA>& i1_data_strm,
    hls::stream <MULTINB_DATA>& i2_data_strm,
    hls::stream <MULTINB_DATA>& i3_data_strm,
    hls::stream <MULTINB_DATA>& i4_data_strm,
    hls::stream <MULTINB_DATA>& i5_data_strm,
    hls::stream <MULTINB_DATA>& i6_data_strm,
    hls::stream <MULTINB_DATA>& i7_data_strm,
    hls::stream <bool>& i0_e_strm,
    hls::stream <bool>& i1_e_strm,
    hls::stream <bool>& i2_e_strm,
    hls::stream <bool>& i3_e_strm,
    hls::stream <bool>& i4_e_strm,
    hls::stream <bool>& i5_e_strm,
    hls::stream <bool>& i6_e_strm,
    hls::stream <bool>& i7_e_strm,
    hls::stream <MULTINB_DATA>& o_data_strm,
    hls::stream <bool>& o_e_strm
    )

merge8To1, merge 8 source streams into one destination, read next stream until current stream is empty

counterUnit

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    typename DT,
    int DT_WIDTH,
    int WL
    >
void counterUnit (
    const int num_of_class,
    const int num_of_term,
    hls::stream <MULTINB_DATA>& i_data_strm,
    hls::stream <bool>& i_e_strm,
    hls::stream <MULTINB_DATA>& o_data_strm,
    hls::stream <bool>& o_e_strm,
    ap_uint <72>* lh_vector
    )

counterUnit, count the number of each term for each class, and then store into LH table

collectUnit

#include "xf_data_analytics/classification/naive_bayes.hpp"
void collectUnit (
    const int num_of_class,
    hls::stream <MULTINB_DATA>& i_data_strm,
    hls::stream <bool>& i_e_strm,
    hls::stream <ap_uint <96>>& o_prior_strm,
    hls::stream <ap_uint <32>>& o_dist_sum_strm,
    ap_uint <96>* prior_vector
    )

collectUnit, count the number of train sample, and get the maximum feature value

priorGather

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int PU>
void priorGather (
    const int num_of_class,
    hls::stream <ap_uint <96>> i_prior_strm [PU],
    hls::stream <ap_uint <32>> i_dist_sum_strm [PU],
    ap_uint <64>& term_sum,
    ap_uint <96>* prior_prob
    )

priorGather, calculate the total samples and the maximum feature

trainCore

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    typename DT,
    int WL,
    int PU
    >
void trainCore (
    const int num_of_class,
    const int num_of_term,
    hls::stream <ap_uint <64>> i_data_strm [PU],
    hls::stream <bool> i_e_strm [PU],
    ap_uint <64>& term_sum,
    ap_uint <72>* lh_vector [PU],
    ap_uint <96>* prior_vector [PU+1]
    )

trainCore, the top function of multinomial Naive Bayes training

trainWriteOut

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    typename DT,
    int DT_WIDTH,
    int PU
    >
void trainWriteOut (
    const int num_of_class,
    const int num_of_term,
    const ap_uint <64> term_sum,
    hls::stream <int>& o_terms_strm,
    hls::stream <ap_uint <64>> o_d0_strm [PU],
    hls::stream <ap_uint <64>> o_d1_strm [PU],
    ap_uint <72>* lh_vector [PU],
    ap_uint <96>* prior_vector
    )

trainWriteOut, calculate the log of prior probablity and likehood probablity, and then write into stream

loadTheta

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    int CH_NM,
    int GRP_NM
    >
void loadTheta (
    const int num_of_class,
    const int num_of_term,
    hls::stream <ap_uint <64>>& i_theta_strm,
    ap_uint <72>* lh_vector [GRP_NM][256/GRP_NM]
    )

loadTheta, likehood probability matrix loader, it will read num_of_class * num_of_term times

loadPrior

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int GRP_NM>
void loadPrior (
    const int num_of_class,
    hls::stream <ap_uint <64>>& i_prior_strm,
    ap_uint <64>* prior_vector [GRP_NM]
    )

loadPrior, prior probability vector loader, it will read num_of_class times

uramAccess

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    int CH_NM,
    int GRP_NM
    >
void uramAccess (
    const int num_of_class,
    const int num_of_term,
    hls::stream <ap_uint <32>> i_data_strm [CH_NM],
    hls::stream <bool>& i_e_strm,
    hls::stream <double> o_prob_strm [GRP_NM][CH_NM],
    hls::stream <ap_uint <32>> o_data_strm [GRP_NM][CH_NM],
    hls::stream <bool>& o_e_strm,
    ap_uint <72>* lh_vector [GRP_NM][256/GRP_NM]
    )

uramAccess, likehood probability is read line by line, GRP_NM*CH_NM per cycle

finalProb

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int GRP_NM>
void finalProb (
    const int num_of_class,
    hls::stream <double> i_data_strm [GRP_NM],
    hls::stream <bool>& i_e_strm,
    hls::stream <double> o_data_strm [GRP_NM],
    hls::stream <bool>& o_e_strm,
    ap_uint <64>* prior_vector [GRP_NM]
    )

finalProb, calculate the final probability by adding the prior probability for each sample

treeProcess

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    int CH_NM,
    int GRP_NM
    >
void treeProcess (
    const int num_of_class,
    const int num_of_term,
    hls::stream <double> i_prob_strm [GRP_NM][CH_NM],
    hls::stream <ap_uint <32>> i_data_strm [GRP_NM][CH_NM],
    hls::stream <bool>& i_e_strm,
    hls::stream <double> o_data_strm [GRP_NM],
    hls::stream <bool>& o_e_strm,
    ap_uint <64>* prior_vector [GRP_NM],
    ap_uint <64>* result_vector [GRP_NM]
    )

treeProcess, process core

argmaxClassifiler

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <int GRP_NM>
void argmaxClassifiler (
    const int num_of_class,
    hls::stream <double> i_data_strm [GRP_NM],
    hls::stream <bool>& i_e_strm,
    hls::stream <ap_uint <10>>& o_class_strm,
    hls::stream <bool>& o_e_strm
    )

argmaxClassifiler, argmax function

predictCore

#include "xf_data_analytics/classification/naive_bayes.hpp"
template <
    int CH_NM,
    int GRP_NM
    >
void predictCore (
    const int num_of_class,
    const int num_of_term,
    hls::stream <ap_uint <32>> i_data_strm [CH_NM],
    hls::stream <bool>& i_e_strm,
    hls::stream <ap_uint <10>>& o_class_strm,
    hls::stream <bool>& o_e_strm,
    ap_uint <72>* lh_vector [GRP_NM][256/GRP_NM],
    ap_uint <64>* prior_vector [GRP_NM],
    ap_uint <64>* result_vector [GRP_NM]
    )

predictCore, the top function of multinomial Naive Bayes Prediction

transPredictions

#include "xf_data_analytics/classification/svm_predict.hpp"
template <
    typename MType,
    unsigned WD
    >
void transPredictions (
    hls::stream <MType> retStrm [1],
    hls::stream <bool>& eRetStrm,
    hls::stream <ap_uint <1>>& predictionsStrm,
    hls::stream <bool>& predictionsTag
    )

transPredictions transform predicion type to ap_uint.

This function read prediction calculate result stream, and output prediction class tag into a stream

Parameters:

MType The data type of sample
WD The width of data type MType, can get by sizeof(MType)
retStrm Input calculate result streams of MType
eRetStrm End flag stream for input data
predictionsStrm Output class tag streams
predictionsTagStrm End flag stream for output

getPredictions

#include "xf_data_analytics/classification/svm_predict.hpp"
template <
    typename MType,
    unsigned WD,
    unsigned StreamN,
    unsigned SampleDepth
    >
void getPredictions (
    const int cols,
    hls::stream <MType> sample_strm [StreamN],
    hls::stream <bool>& e_sample_strm,
    MType weight [1][StreamN][SampleDepth],
    hls::stream <ap_uint <1>>& predictionsStrm,
    hls::stream <bool>& predictionsTag
    )

getPredictions implement samples predicion.

This function read sample streams, and output prediction result into a stream

Parameters:

MType The data type of sample
WD The width of data type MType, can get by sizeof(MType)
StreamN The stream number of input sample stream vector
SampleDepth stream depth number of one input sample
cols colum number of input data sample
sample_strm Input data streams of MType
e_sample_strm End flag stream for input data
weight svm weight array
predictionsStrm Output data streams
predictionsTagStrm End flag stream for output

getWeight

#include "xf_data_analytics/classification/svm_predict.hpp"
template <
    typename MType,
    unsigned StreamN,
    unsigned SampleDepth
    >
void getWeight (
    hls::stream <ap_uint <512>>& weight_strm,
    hls::stream <bool>& eTag,
    MType weight [1][StreamN][SampleDepth]
    )

getWeight loads 512-bit svm weight

Parameters:

MType The data type of sample
StreamN The stream number of input sample stream vector
SampleDepth stream depth number of one input sample
weight_strm svm weight streams
eTag End flag stream for svm weight
weight svm array)