Classes | Typedefs | Functions
ML::Explainer Namespace Reference

Classes

class  TreePathInfo
 

Typedefs

using TreePathHandle = std::variant< std::shared_ptr< TreePathInfo< float > >, std::shared_ptr< TreePathInfo< double > >>
 
using FloatPointer = std::variant< float *, double * >
 

Functions

void kernel_dataset (const raft::handle_t &handle, float *X, int nrows_X, int ncols, float *background, int nrows_background, float *dataset, float *observation, int *nsamples, int len_nsamples, int maxsample, uint64_t seed=0ULL)
 
void kernel_dataset (const raft::handle_t &handle, float *X, int nrows_X, int ncols, double *background, int nrows_background, double *dataset, double *observation, int *nsamples, int len_nsamples, int maxsample, uint64_t seed=0ULL)
 
void permutation_shap_dataset (const raft::handle_t &handle, float *dataset, const float *background, int nrows_bg, int ncols, const float *row, int *idx, bool row_major)
 
void permutation_shap_dataset (const raft::handle_t &handle, double *dataset, const double *background, int nrows_bg, int ncols, const double *row, int *idx, bool row_major)
 
void shap_main_effect_dataset (const raft::handle_t &handle, float *dataset, const float *background, int nrows_bg, int ncols, const float *row, int *idx, bool row_major)
 
void shap_main_effect_dataset (const raft::handle_t &handle, double *dataset, const double *background, int nrows_bg, int ncols, const double *row, int *idx, bool row_major)
 
void update_perm_shap_values (const raft::handle_t &handle, float *shap_values, const float *y_hat, const int ncols, const int *idx)
 
void update_perm_shap_values (const raft::handle_t &handle, double *shap_values, const double *y_hat, const int ncols, const int *idx)
 
TreePathHandle extract_path_info (TreeliteModelHandle model)
 
void gpu_treeshap (TreePathHandle path_info, const FloatPointer data, std::size_t n_rows, std::size_t n_cols, FloatPointer out_preds, std::size_t out_preds_size)
 
void gpu_treeshap_interventional (TreePathHandle path_info, const FloatPointer data, std::size_t n_rows, std::size_t n_cols, const FloatPointer background_data, std::size_t background_n_rows, std::size_t background_n_cols, FloatPointer out_preds, std::size_t out_preds_size)
 
void gpu_treeshap_interactions (TreePathHandle path_info, const FloatPointer data, std::size_t n_rows, std::size_t n_cols, FloatPointer out_preds, std::size_t out_preds_size)
 
void gpu_treeshap_taylor_interactions (TreePathHandle path_info, const FloatPointer data, std::size_t n_rows, std::size_t n_cols, FloatPointer out_preds, std::size_t out_preds_size)
 

Typedef Documentation

◆ FloatPointer

using ML::Explainer::FloatPointer = typedef std::variant<float*, double*>

◆ TreePathHandle

using ML::Explainer::TreePathHandle = typedef std::variant<std::shared_ptr<TreePathInfo<float> >, std::shared_ptr<TreePathInfo<double> >>

Function Documentation

◆ extract_path_info()

TreePathHandle ML::Explainer::extract_path_info ( TreeliteModelHandle  model)

◆ gpu_treeshap()

void ML::Explainer::gpu_treeshap ( TreePathHandle  path_info,
const FloatPointer  data,
std::size_t  n_rows,
std::size_t  n_cols,
FloatPointer  out_preds,
std::size_t  out_preds_size 
)

◆ gpu_treeshap_interactions()

void ML::Explainer::gpu_treeshap_interactions ( TreePathHandle  path_info,
const FloatPointer  data,
std::size_t  n_rows,
std::size_t  n_cols,
FloatPointer  out_preds,
std::size_t  out_preds_size 
)

◆ gpu_treeshap_interventional()

void ML::Explainer::gpu_treeshap_interventional ( TreePathHandle  path_info,
const FloatPointer  data,
std::size_t  n_rows,
std::size_t  n_cols,
const FloatPointer  background_data,
std::size_t  background_n_rows,
std::size_t  background_n_cols,
FloatPointer  out_preds,
std::size_t  out_preds_size 
)

◆ gpu_treeshap_taylor_interactions()

void ML::Explainer::gpu_treeshap_taylor_interactions ( TreePathHandle  path_info,
const FloatPointer  data,
std::size_t  n_rows,
std::size_t  n_cols,
FloatPointer  out_preds,
std::size_t  out_preds_size 
)

◆ kernel_dataset() [1/2]

void ML::Explainer::kernel_dataset ( const raft::handle_t &  handle,
float *  X,
int  nrows_X,
int  ncols,
double *  background,
int  nrows_background,
double *  dataset,
double *  observation,
int *  nsamples,
int  len_nsamples,
int  maxsample,
uint64_t  seed = 0ULL 
)

◆ kernel_dataset() [2/2]

void ML::Explainer::kernel_dataset ( const raft::handle_t &  handle,
float *  X,
int  nrows_X,
int  ncols,
float *  background,
int  nrows_background,
float *  dataset,
float *  observation,
int *  nsamples,
int  len_nsamples,
int  maxsample,
uint64_t  seed = 0ULL 
)

Generates samples of dataset for kernel shap algorithm.

Parameters
[in]handlecuML handle
[in,out]Xgenerated data [on device] 1-0 (row major)
[in]nrows_Xnumber of rows in X
[in]ncolsnumber of columns in X, background and dataset
[in]backgroundbackground data [on device]
[in]nrows_backgroundnumber of rows in background dataset
[out]datasetgenerated data [on device] observation=background (row major)
[in]observationrow to scatter
[in]nsamplesvector with number of entries that are randomly sampled
[in]len_nsamplesnumber of entries to be sampled
[in]maxsamplesize of the biggest sampled observation
[in]seedSeed for the random number generator

Kernel distrubutes exact part of the kernel shap dataset Each block scatters the data of a row of observations into the (number of rows of background) in dataset, based on the row of X. So, given: background = [[0, 1, 2], [3, 4, 5]] observation = [100, 101, 102] X = [[1, 0, 1], [0, 1, 1]]

dataset (output): [[100, 1, 102], [100, 4, 102] [0, 101, 102], [3, 101, 102]] The first thread of each block calculates the sampling of k entries of observation to scatter into dataset. Afterwards each block scatters the data of a row of X into the (number of rows of background) in dataset. So, given: background = [[0, 1, 2, 3], [5, 6, 7, 8]] observation = [100, 101, 102, 103] nsamples = [3, 2]

X (output) [[1, 0, 1, 1], [0, 1, 1, 0]]

dataset (output): [[100, 1, 102, 103], [100, 6, 102, 103] [0, 101, 102, 3], [5, 101, 102, 8]]

◆ permutation_shap_dataset() [1/2]

void ML::Explainer::permutation_shap_dataset ( const raft::handle_t &  handle,
double *  dataset,
const double *  background,
int  nrows_bg,
int  ncols,
const double *  row,
int *  idx,
bool  row_major 
)

◆ permutation_shap_dataset() [2/2]

void ML::Explainer::permutation_shap_dataset ( const raft::handle_t &  handle,
float *  dataset,
const float *  background,
int  nrows_bg,
int  ncols,
const float *  row,
int *  idx,
bool  row_major 
)

Generates a dataset by tiling the background matrix into out, while adding a forward and backward permutation pass of the observation row on the positions defined by idx. Example:

background = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] idx = [2, 0, 1] row = [100, 101, 102] output: [[ 0, 1, 2] [ 3, 4, 5] [ 6, 7, 8] [ 0, 1, 102] [ 3, 4, 102] [ 6, 7, 102] [100, 1, 102] [100, 4, 102] [100, 7, 102] [100, 101, 102] [100, 101, 102] [100, 101, 102] [100, 101, 2] [100, 101, 5] [100, 101, 8] [ 0, 101, 2] [ 3, 101, 5] [ 6, 101, 8] [ 0, 1, 2] [ 3, 4, 5] [ 6, 7, 8]]

Parameters
[in]handlecuML handle
[out]datasetgenerated data in either row major or column major format, depending on the row_major parameter [on device] [dim = (2 * ncols * nrows_bg + nrows_bg) * ncols]
[in]backgroundbackground data [on device] [dim = ncols * nrows_bg]
[in]nrows_bgnumber of rows in background dataset
[in]ncolsnumber of columns
[in]rowrow to scatter in a permutated fashion [dim = ncols]
[in]idxpermutation indexes [dim = ncols]
[in]row_majorboolean to generate either row or column major data

◆ shap_main_effect_dataset() [1/2]

void ML::Explainer::shap_main_effect_dataset ( const raft::handle_t &  handle,
double *  dataset,
const double *  background,
int  nrows_bg,
int  ncols,
const double *  row,
int *  idx,
bool  row_major 
)

◆ shap_main_effect_dataset() [2/2]

void ML::Explainer::shap_main_effect_dataset ( const raft::handle_t &  handle,
float *  dataset,
const float *  background,
int  nrows_bg,
int  ncols,
const float *  row,
int *  idx,
bool  row_major 
)

Generates a dataset by tiling the background matrix into out, while adding a forward and backward permutation pass of the observation row on the positions defined by idx. Example:

background = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] idx = [2, 0, 1] row = [100, 101, 102] output: [[ 0, 1, 2] [ 3, 4, 5] [ 6, 7, 8] [ 0, 1, 102] [ 3, 4, 102] [ 6, 7, 102] [100, 1, 2] [100, 4, 5] [100, 7, 8] [ 0, 101, 2] [ 3, 101, 5] [ 6, 101, 8]]

Parameters
[in]handlecuML handle
[out]datasetgenerated data [on device] [dim = (2 * ncols * nrows_bg + nrows_bg) * ncols]
[in]backgroundbackground data [on device] [dim = ncols * nrows_bg]
[in]nrows_bgnumber of rows in background dataset
[in]ncolsnumber of columns
[in]rowrow to scatter in a permutated fashion [dim = ncols]
[in]idxpermutation indexes [dim = ncols]
[in]row_majorboolean to generate either row or column major data

◆ update_perm_shap_values() [1/2]

void ML::Explainer::update_perm_shap_values ( const raft::handle_t &  handle,
double *  shap_values,
const double *  y_hat,
const int  ncols,
const int *  idx 
)

◆ update_perm_shap_values() [2/2]

void ML::Explainer::update_perm_shap_values ( const raft::handle_t &  handle,
float *  shap_values,
const float *  y_hat,
const int  ncols,
const int *  idx 
)

Function that aggregates averages of the averatge of results of the model called with the permutation dataset, to estimate the SHAP values. It is equivalent to the Python code: for i,ind in enumerate(idx): shap_values[ind] += y_hat[i + 1] - y_hat[i] for i,ind in enumerate(idx): shap_values[ind] += y_hat[i + ncols] - y_hat[i + ncols + 1]

Parameters
[in]handlecuML handle
[out]shap_valuesArray where the results are aggregated [dim = ncols]
[in]y_hatResults to use for the aggregation [dim = ncols + 1]
[in]ncolsnumber of columns
[in]idxpermutation indexes [dim = ncols]