typedefstruct { const float **input;
int in_width;
int in_height;
int in_stride; const CNN_LAYER_CONFIG *layer_config;
float **output;
int out_stride;
int start_idx;
int th_step;
} CONVOLVE_OPS;
// The concatenated tensor goes into dst with first the channels in // original dst followed by the channels in the src staticbool concat_tensor(const TENSOR *src, TENSOR *dst) {
assert(src->width == dst->width);
assert(src->height == dst->height);
const int dst_channels = dst->channels; const int channels = dst->channels + src->channels; const int newallocsize = channels * dst->width * dst->height;
if (dst->allocsize < newallocsize) {
TENSOR t;
init_tensor(&t); // allocate new buffers and copy first the dst channels
if (!realloc_tensor(&t, channels, dst->width, dst->height)) return false;
copy_tensor(dst, dst->channels, 0, &t); // Swap the tensors and free the old buffers
swap_tensor(dst, &t);
free_tensor(&t);
}
for (int c = 1; c < channels; ++c)
dst->buf[c] = &dst->buf[0][c * dst->width * dst->height]; // Copy the channels in src after the first dst_channels channels.
copy_tensor(src, src->channels, dst_channels, dst); returntrue;
}
const int output_num = layer_config->output_num;
if (output_num != -1) { // Current layer is an output layer
out_width[output_num] = o_width;
out_height[output_num] = o_height;
out_channels[output_num] = channels_per_branch[layer_config->branch];
}
}
}
static inline int get_start_shift_convolve(int width, int filt_width,
int stride) { const int mod = (width % stride); const int filt_off = (filt_width - 1) / 2; const int dif = (mod ? mod - 1 : stride - 1); return AOMMIN((dif + (filt_width % 2)) / 2, filt_off);
}
void av1_cnn_add_c(float **output, int channels, int width, int height,
int stride, const float **add) {
for (int c = 0; c < channels; ++c) {
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
output[c][i * stride + j] += add[c][i * stride + j];
}
}
void av1_cnn_activate_c(float **output, int channels, int width, int height,
int stride, ACTIVATION layer_activation) {
if (layer_activation == RELU) {
for (int c = 0; c < channels; ++c) {
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
output[c][i * stride + j] = relu(output[c][i * stride + j]);
}
} else if (layer_activation == SOFTSIGN) {
for (int c = 0; c < channels; ++c) {
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
output[c][i * stride + j] = softsign(output[c][i * stride + j]);
}
} else if (layer_activation == SIGMOID) {
assert(0 && "Sigmoid has not been supported in CNN."); // TO DO
} else if (layer_activation != NONE) {
assert(0 && "Unknown activation type");
}
}
staticbool copy_active_tensor_to_branches(const TENSOR *layer_active_tensor, const CNN_LAYER_CONFIG *layer_config,
int branch, TENSOR branch_output[]) { const CNN_BRANCH_CONFIG *branch_config = &layer_config->branch_config;
for (int b = 0; b < CNN_MAX_BRANCHES; ++b) {
if ((branch_config->input_to_branches & (1 << b)) && b != branch) { // Copy layer's active tensor to output tensor of branch b if set in // mask. The output becomes the input of the first layer of the branch // because the layer of the branch is not the first layer.
int copy_channels = branch_config->channels_to_copy > 0
? branch_config->channels_to_copy
: layer_active_tensor->channels;
if (!realloc_tensor(&branch_output[b], copy_channels,
layer_active_tensor->width,
layer_active_tensor->height)) { return false;
}
copy_tensor(layer_active_tensor, copy_channels, 0, &branch_output[b]);
}
} returntrue;
}
// CNNConvolve specific to maxpool set as 1, either skip_width or skip_height // greater than 1 and padding equal to PADDING_SAME_ZERO. staticvoid convolve_maxpool_padding_zero( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config, float **output, int out_stride, const int cstep, const int filter_width_half, const int filter_height_half) {
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int h = 0, u = 0; h < in_height; h += layer_config->skip_height, ++u) {
for (int w = 0, v = 0; w < in_width; w += layer_config->skip_width, ++v) {
for (int hh = h; hh < AOMMIN(in_height, h + layer_config->skip_height);
++hh) {
for (int ww = w; ww < AOMMIN(in_width, w + layer_config->skip_width);
++ww) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int ii = hh + l - filter_height_half;
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int jj = ww + m - filter_width_half;
if (ii < 0 || ii >= in_height || jj < 0 || jj >= in_width) continue;
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
} const float a = sum;
if (h == hh && w == ww)
output[i][u * out_stride + v] = a; else
output[i][u * out_stride + v] =
AOMMAX(output[i][u * out_stride + v], a);
}
}
}
}
}
}
// CNNConvolve specific to maxpool set as 1, either skip_width or skip_height // greater than 1 and padding equal to PADDING_SAME_REPLICATE. staticvoid convolve_maxpool_padding_replicate( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config, float **output, int out_stride, const int cstep, const int filter_width_half, const int filter_height_half) {
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int h = 0, u = 0; h < in_height; h += layer_config->skip_height, ++u) {
for (int w = 0, v = 0; w < in_width; w += layer_config->skip_width, ++v) {
for (int hh = h; hh < AOMMIN(in_height, h + layer_config->skip_height);
++hh) {
for (int ww = w; ww < AOMMIN(in_width, w + layer_config->skip_width);
++ww) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int ii =
CLAMPINDEX(hh + l - filter_height_half, in_height);
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int jj =
CLAMPINDEX(ww + m - filter_width_half, in_width);
assert(ii >= 0 && ii < in_height && jj >= 0 && jj < in_width);
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
} const float a = sum;
if (h == hh && w == ww)
output[i][u * out_stride + v] = a; else
output[i][u * out_stride + v] =
AOMMAX(output[i][u * out_stride + v], a);
}
}
}
}
}
}
// CNNConvolve specific to maxpool set as 1, either skip_width or skip_height // greater than 1 and padding equal to PADDING_VALID. staticvoid convolve_maxpool_padding_valid( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config, float **output, int out_stride, const int cstep) {
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int h = 0, u = 0; h < in_height - layer_config->filter_height + 1;
h += layer_config->skip_height, ++u) {
for (int w = 0, v = 0; w < in_width - layer_config->filter_width + 1;
w += layer_config->skip_width, ++v) {
for (int hh = h; hh < AOMMIN(in_height, h + layer_config->skip_height);
++hh) {
for (int ww = w; ww < AOMMIN(in_width, w + layer_config->skip_width);
++ww) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int ii = hh + l;
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int jj = ww + m;
assert(ii >= 0 && ii < in_height && jj >= 0 && jj < in_width);
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
} const float a = sum;
if (h == hh && w == ww)
output[i][u * out_stride + v] = a; else
output[i][u * out_stride + v] =
AOMMAX(output[i][u * out_stride + v], a);
}
}
}
}
}
}
// CNNConvolve specific to maxpool set as 0 with filter_height and filter_width // equal to 1. staticvoid convolve_element_wise(const float **input, int in_width,
int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config,
float **output, int out_stride, int start_idx,
int step) { const int start_h = get_start_shift_convolve(
in_height, layer_config->filter_height, layer_config->skip_height); const int start_w =
get_start_shift_convolve(in_width, layer_config->filter_width,
layer_config->skip_width) +
start_idx * layer_config->skip_width; const int out_w_step = AOMMAX(step, 1); const int in_w_step = layer_config->skip_width * out_w_step;
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int h = start_h, u = 0; h < in_height;
h += layer_config->skip_height, ++u) { const int in_h = h * in_stride; const int out_h = u * out_stride + start_idx;
for (int w = start_w, out_index = out_h; w < in_width;
w += in_w_step, out_index += out_w_step) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
sum += layer_config->weights[k * layer_config->out_channels + i] *
input[k][in_h + w];
}
output[i][out_index] = sum;
}
}
}
}
// CNNConvolve specific to maxpool set as 0 and padding equal to // PADDING_SAME_ZERO. staticvoid convolve_no_maxpool_padding_zero( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config, float **output, int out_stride,
int start_idx, const int cstep, const int filter_width_half, const int filter_height_half, const int ii_shift, const int jj_shift, const int channel_step) { const int start_h = get_start_shift_convolve(
in_height, layer_config->filter_height, layer_config->skip_height); const int start_w = get_start_shift_convolve(
in_width, layer_config->filter_width, layer_config->skip_width); const int end_ii_shift = filter_height_half + 1; const int end_jj_shift = filter_width_half + 1; // *_filter_margin stores the number of pixels along a dimension in the // intersection of the complement of the image in the extended image // and the filter. const int top_filter_margin = layer_config->filter_width * ii_shift; const int right_filter_margin = end_jj_shift - in_width;
for (int i = start_idx; i < layer_config->out_channels; i += channel_step) {
for (int h = start_h, u = 0; h < in_height;
h += layer_config->skip_height, ++u) { const int out_h = u * out_stride; const int top_cstep =
AOMMAX(0, top_filter_margin - h * layer_config->filter_width) *
cstep +
i; const int start_ii = AOMMAX(0, h - ii_shift); const int end_ii = AOMMIN(in_height, h + end_ii_shift);
for (int w = start_w, out_index = out_h; w < in_width;
w += layer_config->skip_width, ++out_index) { const int left_cstep = AOMMAX(0, jj_shift - w) * cstep; const int right_cstep = AOMMAX(0, right_filter_margin + w) * cstep; const int start_jj = AOMMAX(0, w - jj_shift); const int end_jj = AOMMIN(in_width, w + end_jj_shift);
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + top_cstep;
for (int ii = start_ii; ii < end_ii; ++ii) {
off += left_cstep;
for (int jj = start_jj; jj < end_jj; ++jj, off += cstep) {
sum += layer_config->weights[off] * input[k][ii * in_stride + jj];
}
off += right_cstep;
}
}
output[i][out_index] = sum;
}
}
}
}
// CNNConvolve specific to maxpool set as 0 and padding equal to // PADDING_SAME_REPLICATE. staticvoid convolve_no_maxpool_padding_replicate( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *const layer_config, float **output, int out_stride,
int start_idx, const int cstep, const int ii_shift, const int jj_shift, const int channel_step) { // h and w are shifted to an offset coordinate system to reduce in-loop // computation. const int start_h =
get_start_shift_convolve(in_height, layer_config->filter_height,
layer_config->skip_height) -
ii_shift; const int start_w =
get_start_shift_convolve(in_width, layer_config->filter_width,
layer_config->skip_width) -
jj_shift; const int end_h = in_height - ii_shift; const int end_w = in_width - jj_shift;
for (int i = start_idx; i < layer_config->out_channels; i += channel_step) {
for (int h = start_h, u = 0; h < end_h;
h += layer_config->skip_height, ++u) { const int out_h = u * out_stride; const int upper_ii_index = layer_config->filter_height + h;
for (int w = start_w, out_index = out_h; w < end_w;
w += layer_config->skip_width, ++out_index) { const int upper_jj_index = layer_config->filter_width + w;
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int ii = h; ii < upper_ii_index; ++ii) { const int clamped_ii = CLAMPINDEX(ii, in_height);
for (int jj = w; jj < upper_jj_index; ++jj) { const int clamped_jj = CLAMPINDEX(jj, in_width);
assert(clamped_ii >= 0 && clamped_ii < in_height &&
clamped_jj >= 0 && clamped_jj < in_width);
sum += layer_config->weights[off] *
input[k][clamped_ii * in_stride + clamped_jj];
off += cstep;
}
}
}
output[i][out_index] = sum;
}
}
}
}
// CNNConvolve specific to maxpool set as 0 and padding equal to // PADDING_VALID. void av1_cnn_convolve_no_maxpool_padding_valid_c( const float **input, int in_width, int in_height, int in_stride, const CNN_LAYER_CONFIG *layer_config, float **output, int out_stride,
int start_idx, int cstep, int channel_step) {
assert((layer_config->skip_height == 1 && layer_config->skip_width == 1) ||
!layer_config->maxpool);
assert(layer_config->filter_height > 1 || layer_config->filter_width > 1);
assert(layer_config->pad == PADDING_VALID);
for (int i = start_idx; i < layer_config->out_channels; i += channel_step) {
for (int h = 0, u = 0; h < in_height - layer_config->filter_height + 1;
h += layer_config->skip_height, ++u) { const int out_h = u * out_stride; const int upper_ii_index = layer_config->filter_height + h;
for (int w = 0, out_index = out_h;
w < in_width - layer_config->filter_width + 1;
w += layer_config->skip_width, ++out_index) { const int upper_jj_index = layer_config->filter_width + w;
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int ii = h; ii < upper_ii_index; ++ii) {
for (int jj = w; jj < upper_jj_index; ++jj) {
assert(ii >= 0 && ii < in_height && jj >= 0 && jj < in_width);
sum += layer_config->weights[off] * input[k][ii * in_stride + jj];
off += cstep;
}
}
}
output[i][out_index] = sum;
}
}
}
}
staticvoid av1_cnn_convolve(const float **input, int in_width, int in_height,
int in_stride, const CNN_LAYER_CONFIG *layer_config,
float **output, int out_stride, int start_idx,
int step) {
assert(!layer_config->deconvolve); const int cstep = layer_config->in_channels * layer_config->out_channels; const int filter_height_half = layer_config->filter_height >> 1; const int filter_width_half = layer_config->filter_width >> 1; const int channel_step = AOMMAX(step, 1);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
image_row[col] =
ch_gamma * (image_row[col] - ch_mean) / ch_std + ch_beta;
}
image_row += stride;
}
}
}
void av1_cnn_deconvolve_c(const float **input, int in_width, int in_height,
int in_stride, const CNN_LAYER_CONFIG *layer_config,
float **output, int out_stride) {
assert(layer_config->deconvolve);
const int cstep = layer_config->in_channels * layer_config->out_channels;
int out_width = 0;
int out_height = 0;
av1_find_cnn_layer_output_size(in_width, in_height, layer_config, &out_width,
&out_height); switch (layer_config->pad) { case PADDING_SAME_ZERO:
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int u = 0; u < out_height; ++u) {
for (int v = 0; v < out_width; ++v) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int h =
u - l +
get_start_shift_deconvolve(layer_config->filter_height,
layer_config->skip_height);
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int w =
v - m +
get_start_shift_deconvolve(layer_config->filter_width,
layer_config->skip_width);
if ((h % layer_config->skip_height) != 0 ||
(w % layer_config->skip_width) != 0) continue; const int ii = h / layer_config->skip_height; const int jj = w / layer_config->skip_width;
if (ii < 0 || ii >= in_height || jj < 0 || jj >= in_width) continue;
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
}
output[i][u * out_stride + v] = sum;
}
}
} break; case PADDING_SAME_REPLICATE:
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int u = 0; u < out_height; ++u) {
for (int v = 0; v < out_width; ++v) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int h =
u - l +
get_start_shift_deconvolve(layer_config->filter_height,
layer_config->skip_height);
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int w =
v - m +
get_start_shift_deconvolve(layer_config->filter_width,
layer_config->skip_width);
if ((h % layer_config->skip_height) != 0 ||
(w % layer_config->skip_width) != 0) continue; const int ii =
CLAMPINDEX(h / layer_config->skip_height, in_height); const int jj =
CLAMPINDEX(w / layer_config->skip_width, in_width);
assert(ii >= 0 && ii < in_height && jj >= 0 && jj < in_width);
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
}
output[i][u * out_stride + v] = sum;
}
}
} break; case PADDING_VALID:
for (int i = 0; i < layer_config->out_channels; ++i) {
for (int u = 0; u < out_height; ++u) {
for (int v = 0; v < out_width; ++v) {
float sum = layer_config->bias[i];
for (int k = 0; k < layer_config->in_channels; ++k) {
int off = k * layer_config->out_channels + i;
for (int l = 0; l < layer_config->filter_height; ++l) { const int h = u - l;
for (int m = 0; m < layer_config->filter_width;
++m, off += cstep) { const int w = v - m;
if ((h % layer_config->skip_height) != 0 ||
(w % layer_config->skip_width) != 0) continue; const int ii = h / layer_config->skip_height; const int jj = w / layer_config->skip_width;
if (ii < 0 || ii >= in_height || jj < 0 || jj >= in_width) continue;
sum += layer_config->weights[off] *
input[k][ii * in_stride + jj];
}
}
}
output[i][u * out_stride + v] = sum;
}
}
} break; default: assert(0 && "Unknown padding type");
}
}
int i_width = in_width;
int i_height = in_height;
int o_width = 0, o_height = 0;
for (int b = 0; b < CNN_MAX_BRANCHES; ++b) {
init_tensor(&tensor1[b]);
init_tensor(&tensor2[b]);
}
const int *out_stride = output_struct->output_strides;
for (int layer = 0; layer < cnn_config->num_layers; ++layer) { const CNN_LAYER_CONFIG *layer_config = &cnn_config->layer_config[layer]; const int branch = layer_config->branch; const CNN_BRANCH_CONFIG *branch_config = &layer_config->branch_config;
// Allocate input tensor
if (layer == 0) { // First layer
assert(branch == 0); // First layer must be primary branch
assign_tensor(&tensor1[branch], (float **)input,
layer_config->in_channels, in_width, in_height, in_stride);
} else { // Non-first layer // Swap tensor1 and tensor2
swap_tensor(&tensor1[branch], &tensor2[branch]);
// If we are combining branches make sure that the branch to combine // is different from the current branch.
assert(IMPLIES(layer_config->branch_combine_type != BRANCH_NOC,
!(branch_config->branches_to_combine & (1 << branch))));
if (layer_config->branch_copy_type == BRANCH_INPUT) {
if (!copy_active_tensor_to_branches(&tensor1[branch], layer_config,
branch, tensor2)) {
goto Error;
}
} // Check consistency of input and output channels
assert(tensor1[branch].channels == layer_config->in_channels);
assert(tensor2[branch].channels == layer_config->out_channels);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.