Skip to content

Commit

Permalink
Merge pull request #129 from foss-for-synopsys-dwc-arc-processors/mem…
Browse files Browse the repository at this point in the history
…_stride

[memory strides] add memory stride support to kernels
  • Loading branch information
JaccovG authored May 7, 2020
2 parents a89e019 + 28318a0 commit 5828486
Show file tree
Hide file tree
Showing 35 changed files with 4,321 additions and 5,237 deletions.
1 change: 1 addition & 0 deletions build/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ else
ifeq ($(TOOLCHAIN),mwdt)
# place to add MWDT-specific common settings
CFLAGS +=-Hon=Long_enums
CFLAGS +=-mllvm -gen-lpcc=false
DEPFLAGS =-Hdepend=$(BUILD_DIR)/ -MD
else
$(error ERROR - Unsupported toolchain. Supported toolchains are 'gnu' and 'mwdt', default one is 'gnu')
Expand Down
6 changes: 6 additions & 0 deletions include/mli_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@
*/
#if (ARC_PLATFORM == V2DSP_XY)
#define MLI_PTR(p) __xy p *
#define MLI_PTR_IS_XY true
#define MLI_OUT_PTR(p) __xy p *
#define MLI_OUT_PTR_IS_XY true
#define MLI_CONV_OUT_PTR(p) p *
#define MLI_CONV_OUT_PTR_IS_XY false
#else
#define MLI_PTR(p) p *
#define MLI_PTR_IS_XY false
#define MLI_OUT_PTR(p) p *
#define MLI_OUT_PTR_IS_XY false
#define MLI_CONV_OUT_PTR(p) p *
#define MLI_CONV_OUT_PTR_IS_XY false
#endif

#endif // _MLI_CONFIG_H_
39 changes: 16 additions & 23 deletions lib/gen/mli_krn_avepool_chw_func_body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
int padding_bot = cfg->padding_bottom;
int padding_left = cfg->padding_left;
int padding_right = cfg->padding_right;
int channels_num = in->shape[FMAP_C_DIM_CHW];
int kernel_height = cfg->kernel_height;
int kernel_width = cfg->kernel_width;

// Define Data dimensions
auto in_prv = mli_prv_get_tensor_chw<MLI_PTR($d_type), MLI_PTR_IS_XY>(in,
$channels); // channels

// assign hard coded values for this variation to some variables
#if $stride_w
MLI_CHECK_AND_FIX(stride_width, $stride_w);
Expand All @@ -31,20 +35,19 @@
#if $kernel_h
MLI_CHECK_AND_FIX(kernel_height, $kernel_h);
#endif
#if $channels
MLI_CHECK_AND_FIX(channels_num, $channels);
#endif

// Data pointers
MLI_PTR($d_type) in_ftrs = (MLI_PTR($d_type ))in->data;
MLI_OUT_PTR($d_type) out_ftrs = (MLI_OUT_PTR($d_type ))out->data;

// Define Data dimensions
const int in_height = in->shape[FMAP_H_DIM_CHW];
const int in_width = in->shape[FMAP_W_DIM_CHW];
const int out_width = CEIL_DIV(in_prv.width + padding_left + padding_right - kernel_width + 1, stride_width);
const int out_height = CEIL_DIV(in_prv.height + padding_top + padding_bot - kernel_height + 1, stride_height);

const int out_width = CEIL_DIV(in_width + padding_left + padding_right - kernel_width + 1, stride_width);
const int out_height = CEIL_DIV(in_height + padding_top + padding_bot - kernel_height + 1, stride_height);
// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_C_DIM_CHW] = in_prv.ch;
out->shape[FMAP_H_DIM_CHW] = out_height;
out->shape[FMAP_W_DIM_CHW] = out_width;
out->el_params.fx.frac_bits = in->el_params.fx.frac_bits;
const auto out_prv = mli_prv_get_tensor_chw<MLI_OUT_PTR($d_type), MLI_OUT_PTR_IS_XY>(out);

const int row_beg = 0;
const int row_end = out_height;
Expand All @@ -56,20 +59,10 @@
$core_name(
row_beg, row_end,
clmn_beg, clmn_end,
in_ftrs, out_ftrs,
channels_num, in_width, in_height,
out_width, out_height,
in_prv, out_prv,
kernel_height, kernel_width,
stride_height, stride_width,
padding_top, padding_left, padding_right, padding_bot);

// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_C_DIM_CHW] = channels_num;
out->shape[FMAP_H_DIM_CHW] = out_height;
out->shape[FMAP_W_DIM_CHW] = out_width;
out->el_params.fx.frac_bits = in->el_params.fx.frac_bits;

return MLI_STATUS_OK;
}
39 changes: 16 additions & 23 deletions lib/gen/mli_krn_avepool_hwc_func_body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
int padding_bot = cfg->padding_bottom;
int padding_left = cfg->padding_left;
int padding_right = cfg->padding_right;
int channels_num = in->shape[FMAP_C_DIM_HWC];
int kernel_height = cfg->kernel_height;
int kernel_width = cfg->kernel_width;

// Define Data dimensions
auto in_prv = mli_prv_get_tensor_hwc<MLI_PTR($d_type), MLI_PTR_IS_XY>(in,
$channels); // channels

// assign hard coded values for this variation to some variables
#if $stride_w
MLI_CHECK_AND_FIX(stride_width, $stride_w);
Expand All @@ -31,20 +35,19 @@
#if $kernel_h
MLI_CHECK_AND_FIX(kernel_height, $kernel_h);
#endif
#if $channels
MLI_CHECK_AND_FIX(channels_num, $channels);
#endif

// Data pointers
MLI_PTR($d_type) in_ftrs = (MLI_PTR($d_type ))in->data;
MLI_OUT_PTR($d_type) out_ftrs = (MLI_OUT_PTR($d_type ))out->data;

// Define Data dimensions
const int in_height = in->shape[FMAP_H_DIM_HWC];
const int in_width = in->shape[FMAP_W_DIM_HWC];
const int out_width = CEIL_DIV(in_prv.width + padding_left + padding_right - kernel_width + 1, stride_width);
const int out_height = CEIL_DIV(in_prv.height + padding_top + padding_bot - kernel_height + 1, stride_height);

const int out_width = CEIL_DIV(in_width + padding_left + padding_right - kernel_width + 1, stride_width);
const int out_height = CEIL_DIV(in_height + padding_top + padding_bot - kernel_height + 1, stride_height);
// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;
out->shape[FMAP_C_DIM_HWC] = in_prv.ch;
out->el_params.$el_params = in->el_params.$el_params;
const auto out_prv = mli_prv_get_tensor_hwc<MLI_OUT_PTR($d_type), MLI_OUT_PTR_IS_XY>(out);

const int row_beg = 0;
const int row_end = out_height;
Expand All @@ -56,20 +59,10 @@
$core_name(
row_beg, row_end,
clmn_beg, clmn_end,
in_ftrs, out_ftrs,
channels_num, in_width, in_height,
out_width, out_height,
in_prv, out_prv,
kernel_height, kernel_width,
stride_height, stride_width,
padding_top, padding_left, padding_right, padding_bot);

// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;
out->shape[FMAP_C_DIM_HWC] = channels_num;
out->el_params.$el_params = in->el_params.$el_params;

return MLI_STATUS_OK;
}
52 changes: 22 additions & 30 deletions lib/gen/mli_krn_conv2d_nhwc_func_body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
int padding_bot = cfg->padding_bottom;
int padding_left = cfg->padding_left;
int padding_right = cfg->padding_right;
int kernel_height = weights->shape[KRNL_H_DIM_HWC];
int kernel_width = weights->shape[KRNL_W_DIM_HWC];
int out_ch = weights->shape[KRNL_C_DIM_HWC];
int in_ch = in->shape[FMAP_C_DIM_HWC];

// Define Data dimensions
const auto in_prv = mli_prv_get_tensor_hwc<MLI_PTR($d_type), MLI_PTR_IS_XY>(in,
$channels); // channels
const auto w = mli_prv_get_conv2d_weights_tensor_nhwc<MLI_PTR($w_type), MLI_PTR_IS_XY>(weights,
$channels, // channels
$kernel_w, // kernel_width
$kernel_h); // kernel_height
__builtin_assume(in_prv.ch == w.in_ch);

// assign hard coded values for this variation to some variables
#if $stride_w
Expand All @@ -29,15 +34,6 @@
MLI_CHECK_AND_FIX(padding_left, $padding_left);
MLI_CHECK_AND_FIX(padding_right, $padding_right);
#endif
#if $kernel_w
MLI_CHECK_AND_FIX(kernel_width, $kernel_w);
#endif
#if $kernel_h
MLI_CHECK_AND_FIX(kernel_height, $kernel_h);
#endif
#if $channels
MLI_CHECK_AND_FIX(in_ch, $channels);
#endif

mli_minmax_t val_limit;
// fill output tensor el_type parameter
Expand All @@ -46,17 +42,18 @@
val_limit = mli_prv_get_relu_min_max(&cfg->relu, out);

// Data pointers
MLI_PTR($d_type) in_ftrs = (MLI_PTR($d_type ))in->data;
MLI_CONV_OUT_PTR($d_type) out_ftrs = (MLI_CONV_OUT_PTR($d_type ))out->data;
MLI_PTR($w_type) wt = (MLI_PTR($w_type ))weights->data;
MLI_PTR($b_type) bs = (MLI_PTR($b_type ))bias->data;

// Define Data dimensions
int in_height = in->shape[FMAP_H_DIM_HWC];
int in_width = in->shape[FMAP_W_DIM_HWC];
int out_width = CEIL_DIV(in_prv.width + padding_left + padding_right - w.kernel_width + 1, stride_width);
int out_height = CEIL_DIV(in_prv.height + padding_top + padding_bot - w.kernel_height + 1, stride_height);

int out_width = CEIL_DIV(in_width + padding_left + padding_right - kernel_width + 1, stride_width);
int out_height = CEIL_DIV(in_height + padding_top + padding_bot - kernel_height + 1, stride_height);
// fill output tensor parameters
out->rank = in->rank;
out->shape[FMAP_C_DIM_HWC] = w.out_ch;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;
const auto out_prv = mli_prv_get_tensor_hwc<MLI_CONV_OUT_PTR($d_type), MLI_CONV_OUT_PTR_IS_XY>(out);

// Define quantization specific params
s8asym_quant_specific_params params;
Expand All @@ -70,16 +67,11 @@
cent_area.clmn_end = out_width;

$core_name<$d_type, $w_type, $b_type, int32_t>(
in_ftrs, wt, bs, out_ftrs, &cent_area, params,
(int8_t)val_limit.min, (int8_t)val_limit.max, in_ch, in_width, in_height,
out_ch, out_width, out_height, kernel_height, kernel_width,
stride_height, stride_width, padding_top, padding_left, padding_bot, padding_right);

// fill output tensor parameters
out->rank = in->rank;
out->shape[FMAP_C_DIM_HWC] = out_ch;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;
in_prv, w, bs, out_prv,
&cent_area, params,
(int8_t)val_limit.min, (int8_t)val_limit.max,
stride_height, stride_width,
padding_top, padding_left, padding_bot, padding_right);

return MLI_STATUS_OK;
}
48 changes: 18 additions & 30 deletions lib/gen/mli_krn_depthwise_conv2d_hwcn_func_body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
int padding_bot = cfg->padding_bottom;
int padding_left = cfg->padding_left;
int padding_right = cfg->padding_right;
int kernel_height = weights->shape[KRNL_DW_H_DIM_HWC];
int kernel_width = weights->shape[KRNL_DW_W_DIM_HWC];
int out_ch = weights->shape[KRNL_DW_C_DIM_HWC];
int in_ch = in->shape[FMAP_C_DIM_HWC];

// Define Data dimensions
const auto in_prv = mli_prv_get_tensor_hwc<MLI_PTR($d_type), MLI_PTR_IS_XY>(in,
$channels); // channels
const auto w = mli_prv_get_conv2d_weights_tensor_1hwn<MLI_PTR($w_type), MLI_PTR_IS_XY>(
weights,
$kernel_w, // kernel_width
$kernel_h); // kernel_height

// assign hard coded values for this variation to some variables
#if $stride_w
Expand All @@ -29,15 +33,6 @@
MLI_CHECK_AND_FIX(padding_left, $padding_left);
MLI_CHECK_AND_FIX(padding_right, $padding_right);
#endif
#if $kernel_w
MLI_CHECK_AND_FIX(kernel_width, $kernel_w);
#endif
#if $kernel_h
MLI_CHECK_AND_FIX(kernel_height, $kernel_h);
#endif
#if $channels
MLI_CHECK_AND_FIX(in_ch, $channels);
#endif

mli_minmax_t val_limit;
// fill output tensor el_type parameter
Expand All @@ -46,18 +41,18 @@
val_limit = mli_prv_get_relu_min_max(&cfg->relu, out);

// Data pointers
MLI_PTR($d_type) in_ftrs = (MLI_PTR($d_type ))in->data;
MLI_CONV_OUT_PTR($d_type) out_ftrs = (MLI_CONV_OUT_PTR($d_type ))out->data;
MLI_PTR($w_type) wt = (MLI_PTR($w_type ))weights->data;
MLI_PTR($b_type) bs = (MLI_PTR($b_type ))bias->data;

// Define Data dimensions
int in_height = in->shape[FMAP_H_DIM_HWC];
int in_width = in->shape[FMAP_W_DIM_HWC];
int out_width = CEIL_DIV(in_prv.width + padding_left + padding_right - w.kernel_width + 1, stride_width);
int out_height = CEIL_DIV(in_prv.height + padding_top + padding_bot - w.kernel_height + 1, stride_height);


int out_width = CEIL_DIV(in_width + padding_left + padding_right - kernel_width + 1, stride_width);
int out_height = CEIL_DIV(in_height + padding_top + padding_bot - kernel_height + 1, stride_height);
// fill output tensor parameters
out->rank = in->rank;
out->shape[FMAP_C_DIM_HWC] = w.out_ch;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;
const auto out_prv = mli_prv_get_tensor_hwc<MLI_CONV_OUT_PTR($d_type), MLI_CONV_OUT_PTR_IS_XY>(out);

// Define quantization specific params
s8asym_quant_specific_params params;
Expand All @@ -71,16 +66,9 @@
cent_area.clmn_end = out_width;

$core_name<$d_type, $w_type, $b_type, int32_t>(
in_ftrs, wt, bs, out_ftrs, &cent_area, params,
(int8_t)val_limit.min, (int8_t)val_limit.max, in_ch, in_width, in_height,
out_ch, out_width, out_height, kernel_height, kernel_width,
in_prv, w, bs, out_prv, &cent_area, params,
(int8_t)val_limit.min, (int8_t)val_limit.max,
stride_height, stride_width, padding_top, padding_left, padding_bot, padding_right);

// fill output tensor parameters
out->rank = in->rank;
out->shape[FMAP_C_DIM_HWC] = out_ch;
out->shape[FMAP_H_DIM_HWC] = out_height;
out->shape[FMAP_W_DIM_HWC] = out_width;

return MLI_STATUS_OK;
}
39 changes: 16 additions & 23 deletions lib/gen/mli_krn_maxpool_chw_func_body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
int32_t padding_bot = cfg->padding_bottom;
int32_t padding_left = cfg->padding_left;
int32_t padding_right = cfg->padding_right;
int32_t channels_num = in->shape[FMAP_C_DIM_CHW];
int32_t kernel_height = cfg->kernel_height;
int32_t kernel_width = cfg->kernel_width;

// Define Data dimensions
auto in_prv = mli_prv_get_tensor_chw<MLI_PTR($d_type), MLI_PTR_IS_XY>(in,
$channels); // channels

// assign hard coded values for this variation to some variables
#if $stride_w
MLI_CHECK_AND_FIX(stride_width, $stride_w);
Expand All @@ -31,20 +35,19 @@
#if $kernel_h
MLI_CHECK_AND_FIX(kernel_height, $kernel_h);
#endif
#if $channels
MLI_CHECK_AND_FIX(channels_num, $channels);
#endif

// Data pointers
MLI_PTR($d_type) in_ftrs = (MLI_PTR($d_type ))in->data;
MLI_OUT_PTR($d_type) out_ftrs = (MLI_OUT_PTR($d_type ))out->data;

// Define Data dimensions
const int32_t in_height = in->shape[FMAP_H_DIM_CHW];
const int32_t in_width = in->shape[FMAP_W_DIM_CHW];
const int32_t out_width = CEIL_DIV(in_prv.width + padding_left + padding_right - kernel_width + 1, stride_width);
const int32_t out_height = CEIL_DIV(in_prv.height + padding_top + padding_bot - kernel_height + 1, stride_height);

const int32_t out_width = CEIL_DIV(in_width + padding_left + padding_right - kernel_width + 1, stride_width);
const int32_t out_height = CEIL_DIV(in_height + padding_top + padding_bot - kernel_height + 1, stride_height);
// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_C_DIM_CHW] = in_prv.ch;
out->shape[FMAP_H_DIM_CHW] = out_height;
out->shape[FMAP_W_DIM_CHW] = out_width;
out->el_params.fx.frac_bits = in->el_params.fx.frac_bits;
const auto out_prv = mli_prv_get_tensor_chw<MLI_OUT_PTR($d_type), MLI_OUT_PTR_IS_XY>(out);

const int32_t row_beg = 0;
const int32_t row_end = out_height;
Expand All @@ -54,23 +57,13 @@
mli_prv_fx_init_dsp_ctrl();

$core_name(
in_ftrs, out_ftrs,
in_prv, out_prv,
row_beg, row_end,
clmn_beg, clmn_end,
channels_num, in_width, in_height,
out_width, out_height,
kernel_height, kernel_width,
stride_height, stride_width,
padding_top, padding_bot, padding_left, padding_right,
$kernelpadding);

// fill output tensor parameters
out->el_type = in->el_type;
out->rank = in->rank;
out->shape[FMAP_C_DIM_CHW] = channels_num;
out->shape[FMAP_H_DIM_CHW] = out_height;
out->shape[FMAP_W_DIM_CHW] = out_width;
out->el_params.fx.frac_bits = in->el_params.fx.frac_bits;

return MLI_STATUS_OK;
}
Loading

0 comments on commit 5828486

Please sign in to comment.