diff --git a/src/fpga/api.h b/src/fpga/api.h index 0823e19a7f9dfaba709b6ad2723e3228c27e2e0f..73db2bfbdac50c123bb0204ff63d4412a2dadfe7 100644 --- a/src/fpga/api.h +++ b/src/fpga/api.h @@ -174,5 +174,7 @@ int ComputeFpgaConv(const struct ConvArgs& args); int ComputeFpgaPool(const struct PoolingArgs& args); int ComputeFpgaEWAdd(const struct EWAddArgs& args); +static inline int align_to_x(int num, int x) { return (num + x - 1) / x * x; } + } // namespace fpga } // namespace paddle_mobile diff --git a/src/fpga/bias_scale.cpp b/src/fpga/bias_scale.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51c4a0ac73869c974332f37c7bae8186c28e63c3 --- /dev/null +++ b/src/fpga/bias_scale.cpp @@ -0,0 +1,65 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "bias_scale.h" +#include +#include "api.h" + +namespace paddle_mobile { +namespace fpga { +namespace bias_scale { + +void align_element(float **data_in, int num_per_div_before_alignment, int num) { + float *ptr_unaligned = *data_in; + int div_num = + (num + num_per_div_before_alignment - 1) / num_per_div_before_alignment; + int num_per_div_after_alignment = + align_to_x(num_per_div_before_alignment, BS_NUM_ALIGNMENT); + int num_element = + 2 * div_num * num_per_div_after_alignment; // including bias & scale + float *ptr_aligned = (float *)fpga_malloc(num_element * sizeof(float)); + + memset(ptr_aligned, 0, num_element * sizeof(float)); + + for (int i = 0; i < div_num; i++) { + memcpy(ptr_aligned + i * num_per_div_after_alignment, ptr_unaligned, + num_per_div_before_alignment * sizeof(float)); + } + + fpga_free(ptr_unaligned); + *data_in = ptr_aligned; +} + +void interleave(float **data_in, int num_after_alignment) { + // num_after_alignment: number of bias after alignment + + float *ptr_uninterleaved = *data_in; + float *ptr_interleaved = + (float *)fpga_malloc(2 * num_after_alignment * sizeof(float)); + int num = num_after_alignment / 4; + for (int i = 0; i < num; i++) { + memcpy(ptr_interleaved + 8 * i, ptr_uninterleaved + 4 * i, + 4 * sizeof(float)); + memcpy(ptr_interleaved + 8 * i + 4, + ptr_uninterleaved + num_after_alignment * sizeof(float) + 4 * i, + 4 * sizeof(float)); + } + + fpga_free(ptr_uninterleaved); + *data_in = ptr_interleaved; +} + +} // namespace bias_scale +} // namespace fpga +} // namespace paddle_mobile diff --git a/src/fpga/bias_scale.h b/src/fpga/bias_scale.h new file mode 100644 index 0000000000000000000000000000000000000000..ff56d4bcae374424b1fc1eabf59f4f0015256c7c --- /dev/null +++ b/src/fpga/bias_scale.h @@ -0,0 +1,28 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#define BS_NUM_ALIGNMENT 8 + +namespace paddle_mobile { +namespace fpga { +namespace bias_scale { + +void align_element(float** data_in, int num_per_div_before_alignment, int num); +void interleave(float** data_in, int num_after_alignment); + +} // namespace bias_scale +} // namespace fpga +} // namespace paddle_mobile diff --git a/src/fpga/filter.cpp b/src/fpga/filter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c71d75ca9b4383287e36e783ce8d3bc6760752cd --- /dev/null +++ b/src/fpga/filter.cpp @@ -0,0 +1,40 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ +#include "filter.h" +#include +#include "api.h" + +namespace paddle_mobile { +namespace fpga { +namespace filter { + +void convert_to_hwc(float** data_in, int num, int channel, int height, + int width) {} + +float find_max(float* data_in, int num) { return 0; } + +void quantize(float* data_in, int num) {} + +void align_element(float** data_in, int num, int chw) {} + +void align_num(float** data_in, int num_per_div_before_alignment, int num, + int chw) {} + +void reorder(float** data_in, int num_after_alignment, int chw) {} + +void interleave(float** data_in, int num_after_alignment, int chw) {} + +} // namespace filter +} // namespace fpga +} // namespace paddle_mobile diff --git a/src/fpga/filter.h b/src/fpga/filter.h new file mode 100644 index 0000000000000000000000000000000000000000..1936c225e10ae3e7cfdb673de14fdce791fe8d69 --- /dev/null +++ b/src/fpga/filter.h @@ -0,0 +1,35 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#define FILTER_NUM_ALIGNMENT 32 // Filter number aligned to 32 +#define FILTER_ELEMENT_ALIGNMENT 16 // Filter element number aligned to 16 + +namespace paddle_mobile { +namespace fpga { +namespace filter { +void convert_to_hwc(float** data_in, int num, int channel, int height, + int width); +float find_max(float* data_in, int num); +void quantize(float* data_in, int num); +void align_element(float** data_in, int num, int chw); +void align_num(float** data_in, int num_per_div_before_alignment, int num, + int chw); +void reorder(float** data_in, int num_after_alignment, int chw); +void interleave(float** data_in, int num_after_alignment, int chw); + +} // namespace filter +} // namespace fpga +} // namespace paddle_mobile diff --git a/src/fpga/image.cpp b/src/fpga/image.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bbf34eae933d69d00517c723326111901444ab0 --- /dev/null +++ b/src/fpga/image.cpp @@ -0,0 +1,13 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ diff --git a/src/fpga/image.h b/src/fpga/image.h new file mode 100644 index 0000000000000000000000000000000000000000..6bbf34eae933d69d00517c723326111901444ab0 --- /dev/null +++ b/src/fpga/image.h @@ -0,0 +1,13 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */