Skip to content

Commit 0125235

Browse files
committed
Reorganise /core directory.
1 parent 438f4ae commit 0125235

38 files changed

+458
-474
lines changed

larq_compute_engine/core/BUILD

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,112 +9,6 @@ cc_library(
99
],
1010
)
1111

12-
cc_library(
13-
name = "bitpack",
14-
hdrs = ["bitpack.h"] + select({
15-
"//larq_compute_engine:aarch64_build": [
16-
"bitpack_aarch64.h",
17-
],
18-
"@org_tensorflow//tensorflow:android_arm64": [
19-
"bitpack_aarch64.h",
20-
],
21-
"//conditions:default": [],
22-
}),
23-
deps = [
24-
":types",
25-
"@flatbuffers",
26-
"@org_tensorflow//tensorflow/lite/kernels/internal:types",
27-
"@ruy//ruy/profiler:instrumentation",
28-
],
29-
)
30-
31-
cc_library(
32-
name = "bitpack_utils",
33-
hdrs = ["bitpack_utils.h"],
34-
deps = [
35-
":bitpack",
36-
],
37-
)
38-
39-
cc_library(
40-
name = "padding_functor",
41-
hdrs = ["padding_functor.h"],
42-
)
43-
44-
cc_library(
45-
name = "bconv2d_output_transform",
46-
hdrs = [
47-
"bconv2d_output_transform.h",
48-
],
49-
deps = [
50-
":types",
51-
"@org_tensorflow//tensorflow/lite/kernels/internal:common",
52-
"@org_tensorflow//tensorflow/lite/kernels/internal:cppmath",
53-
],
54-
)
55-
56-
cc_library(
57-
name = "bgemm_kernels_common",
58-
hdrs = [
59-
"bgemm_kernels_common.h",
60-
],
61-
deps = [
62-
":bconv2d_output_transform",
63-
],
64-
)
65-
66-
cc_library(
67-
name = "bgemm_kernels_arm",
68-
hdrs = [
69-
"bgemm_kernels_arm.h",
70-
"bgemm_kernels_arm32.h",
71-
"bgemm_kernels_arm64.h",
72-
],
73-
deps = [
74-
":bgemm_kernels_common",
75-
"@ruy//ruy/profiler:instrumentation",
76-
],
77-
)
78-
79-
cc_library(
80-
name = "bgemm_kernels_ruy",
81-
hdrs = [
82-
"bgemm_kernels_arm.h",
83-
"bgemm_kernels_ruy.h",
84-
],
85-
deps = [
86-
":bgemm_kernels_arm",
87-
":bitpack",
88-
"@ruy//ruy/profiler:instrumentation",
89-
],
90-
)
91-
92-
cc_library(
93-
name = "bgemm_impl",
94-
hdrs = [
95-
"bgemm_impl.h",
96-
"bgemm_trmul_params.h",
97-
"ruy_pack.h",
98-
],
99-
deps = [
100-
":bgemm_kernels_ruy",
101-
"@org_tensorflow//tensorflow/lite/kernels:cpu_backend_context",
102-
"@org_tensorflow//tensorflow/lite/kernels:cpu_backend_gemm",
103-
"@ruy//ruy/profiler:instrumentation",
104-
],
105-
)
106-
107-
cc_library(
108-
name = "bconv2d_impl_ref",
109-
hdrs = [
110-
"bconv2d_impl_ref.h",
111-
],
112-
deps = [
113-
":bconv2d_output_transform",
114-
"@org_tensorflow//tensorflow/lite/kernels/internal:types",
115-
],
116-
)
117-
11812
cc_library(
11913
name = "bmaxpool",
12014
hdrs = [
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
licenses(["notice"]) # Apache 2.0
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
cc_library(
6+
name = "output_transform",
7+
hdrs = [
8+
"output_transform.h",
9+
],
10+
deps = [
11+
"//larq_compute_engine/core:types",
12+
"@org_tensorflow//tensorflow/lite/kernels/internal:common",
13+
"@org_tensorflow//tensorflow/lite/kernels/internal:cppmath",
14+
],
15+
)
16+
17+
cc_library(
18+
name = "padding_functor",
19+
hdrs = ["padding_functor.h"],
20+
)
21+
22+
cc_library(
23+
name = "reference",
24+
hdrs = [
25+
"reference.h",
26+
],
27+
deps = [
28+
":output_transform",
29+
"@org_tensorflow//tensorflow/lite/kernels/internal:types",
30+
],
31+
)
32+
33+
cc_library(
34+
name = "optimized",
35+
hdrs = [
36+
"optimized.h",
37+
],
38+
deps = [
39+
":padding_functor",
40+
"//larq_compute_engine/core/bgemm",
41+
"@org_tensorflow//tensorflow/lite/kernels:cpu_backend_context",
42+
"@org_tensorflow//tensorflow/lite/kernels:cpu_backend_gemm",
43+
"@org_tensorflow//tensorflow/lite/kernels:padding",
44+
"@org_tensorflow//tensorflow/lite/kernels/internal:optimized_base",
45+
"@ruy//ruy/profiler:instrumentation",
46+
],
47+
)

larq_compute_engine/tflite/kernels/bconv2d_impl.h renamed to larq_compute_engine/core/bconv2d/optimized.h

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
#ifndef COMPUTE_ENGINE_TFLITE_KERNELS_BCONV2D_IMPL_H_
2-
#define COMPUTE_ENGINE_TFLITE_KERNELS_BCONV2D_IMPL_H_
1+
#ifndef COMPUTE_ENGINE_CORE_BCONV2D_OPTIMIZED_H_
2+
#define COMPUTE_ENGINE_CORE_BCONV2D_OPTIMIZED_H_
33

4-
#include "larq_compute_engine/core/bgemm_impl.h"
5-
#include "larq_compute_engine/core/padding_functor.h"
4+
#include "larq_compute_engine/core/bconv2d/padding_functor.h"
5+
#include "larq_compute_engine/core/bgemm/bgemm.h"
66
#include "ruy/profiler/instrumentation.h"
77
#include "tensorflow/lite/kernels/cpu_backend_context.h"
88
#include "tensorflow/lite/kernels/cpu_backend_gemm_params.h"
99
#include "tensorflow/lite/kernels/internal/optimized/im2col_utils.h"
1010
#include "tensorflow/lite/kernels/internal/types.h"
1111
#include "tensorflow/lite/kernels/padding.h"
1212

13-
using namespace tflite;
14-
1513
namespace compute_engine {
14+
namespace core {
15+
namespace bconv2d {
1616

17-
namespace ce = compute_engine;
18-
19-
namespace tflite {
20-
21-
using ce::core::TBitpacked;
17+
using namespace tflite;
2218

2319
inline void im2col(const ConvParams& params, const RuntimeShape& input_shape,
2420
const TBitpacked* input_data,
@@ -77,15 +73,15 @@ const float* GetPostActivationMultiplier(
7773
}
7874

7975
template <typename AccumScalar, typename DstScalar>
80-
inline void BConv2D(const ConvParams& params, const RuntimeShape& input_shape,
81-
const TBitpacked* input_data,
82-
const RuntimeShape& filter_shape,
83-
const TBitpacked* packed_filter_data,
84-
const OutputTransform<DstScalar>& output_transform,
85-
const RuntimeShape& output_shape, DstScalar* output_data,
86-
const RuntimeShape& im2col_shape, TBitpacked* im2col_data,
87-
const float* padding_buffer, const int pad_value,
88-
CpuBackendContext* cpu_backend_context) {
76+
inline void BConv2DOptimized(
77+
const ConvParams& params, const RuntimeShape& input_shape,
78+
const TBitpacked* input_data, const RuntimeShape& filter_shape,
79+
const TBitpacked* packed_filter_data,
80+
const OutputTransform<DstScalar>& output_transform,
81+
const RuntimeShape& output_shape, DstScalar* output_data,
82+
const RuntimeShape& im2col_shape, TBitpacked* im2col_data,
83+
const float* padding_buffer, const int pad_value,
84+
CpuBackendContext* cpu_backend_context) {
8985
TF_LITE_ASSERT_EQ(input_shape.DimensionsCount(), 4);
9086
TF_LITE_ASSERT_EQ(filter_shape.DimensionsCount(), 4);
9187
TF_LITE_ASSERT_EQ(output_shape.DimensionsCount(), 4);
@@ -132,7 +128,7 @@ inline void BConv2D(const ConvParams& params, const RuntimeShape& input_shape,
132128
std::fill(
133129
output_data,
134130
output_data + FlatSizeSkipDim(output_shape, 3) *
135-
ce::core::GetBitpackedSize(output_shape.Dims(3)),
131+
bitpacking::GetBitpackedSize(output_shape.Dims(3)),
136132
TBitpacked(0));
137133
}
138134

@@ -162,8 +158,9 @@ inline void BConv2D(const ConvParams& params, const RuntimeShape& input_shape,
162158
dst_params.rows = n;
163159
dst_params.cols = m;
164160

165-
BGemm<AccumScalar>(lhs_params, lhs_data, rhs_params, rhs_data, dst_params,
166-
output_data, output_transform, cpu_backend_context);
161+
bgemm::BGemm<AccumScalar>(lhs_params, lhs_data, rhs_params, rhs_data,
162+
dst_params, output_data, output_transform,
163+
cpu_backend_context);
167164

168165
if (params.padding_type == PaddingType::kSame && pad_value == 0) {
169166
const int stride_width = params.stride_width;
@@ -180,9 +177,9 @@ inline void BConv2D(const ConvParams& params, const RuntimeShape& input_shape,
180177
const int output_width = output_shape.Dims(2);
181178
const int output_height = output_shape.Dims(1);
182179

183-
ce::core::PaddingFunctor padding_functor;
180+
PaddingFunctor padding_functor;
184181
{
185-
ruy::profiler::ScopeLabel label3("ZeroPaddingCorrection");
182+
ruy::profiler::ScopeLabel label("ZeroPaddingCorrection");
186183
padding_functor(
187184
batches, input_height, input_width, input_depth, nullptr,
188185
filter_height, filter_width, output_depth, stride_height,
@@ -193,7 +190,8 @@ inline void BConv2D(const ConvParams& params, const RuntimeShape& input_shape,
193190
}
194191
}
195192

196-
} // namespace tflite
193+
} // namespace bconv2d
194+
} // namespace core
197195
} // namespace compute_engine
198196

199-
#endif // COMPUTE_ENGINE_TFLITE_KERNELS_BCONV2D_IMPL_H_
197+
#endif // COMPUTE_ENGINE_CORE_BCONV2D_OPTIMIZED_H_

larq_compute_engine/core/bconv2d_output_transform.h renamed to larq_compute_engine/core/bconv2d/output_transform.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef COMPUTE_ENGINE_CORE_OUTPUT_TRANSFORM_H_
2-
#define COMPUTE_ENGINE_CORE_OUTPUT_TRANSFORM_H_
1+
#ifndef COMPUTE_ENGINE_CORE_BCONV2D_OUTPUT_TRANSFORM_H_
2+
#define COMPUTE_ENGINE_CORE_BCONV2D_OUTPUT_TRANSFORM_H_
33

44
#include <algorithm>
55
#include <cstdint>
@@ -10,10 +10,8 @@
1010
#include "tensorflow/lite/kernels/internal/cppmath.h"
1111

1212
namespace compute_engine {
13-
1413
namespace core {
15-
16-
using compute_engine::core::TBitpacked;
14+
namespace bconv2d {
1715

1816
// Clamp an int32 value to int8 range
1917
inline std::int8_t saturate(std::int32_t x) {
@@ -169,7 +167,8 @@ struct OutputTransform<TBitpacked, OutputTransformDetails::Default> {
169167
}
170168
};
171169

170+
} // namespace bconv2d
172171
} // namespace core
173172
} // namespace compute_engine
174173

175-
#endif // COMPUTE_ENGINE_CORE_OUTPUT_TRANSFORM_H_
174+
#endif // COMPUTE_ENGINE_CORE_BCONV2D_OUTPUT_TRANSFORM_H_

larq_compute_engine/core/padding_functor.h renamed to larq_compute_engine/core/bconv2d/padding_functor.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
#ifndef COMPUTE_ENGINE_KERNELS_PADDING_H_
2-
#define COMPUTE_ENGINE_KERNELS_PADDING_H_
1+
#ifndef COMPUTE_ENGINE_CORE_BCONV2D_PADDING_FUNCTOR_H_
2+
#define COMPUTE_ENGINE_CORE_BCONV2D_PADDING_FUNCTOR_H_
33

4-
#include "larq_compute_engine/core/bitpack.h"
4+
#include "larq_compute_engine/core/bitpacking/bitpack.h"
55
#include "larq_compute_engine/core/types.h"
66
#include "tensorflow/lite/kernels/op_macros.h"
77

88
namespace compute_engine {
99
namespace core {
10+
namespace bconv2d {
1011

11-
namespace ce = compute_engine;
12-
13-
//
1412
// Applies (in-place) corrections for zero-padding
1513
// Assumes that padding type is 'SAME'.
16-
//
1714
class PaddingFunctor {
1815
public:
1916
static std::size_t get_cache_size(const int filter_height,
@@ -109,7 +106,8 @@ class PaddingFunctor {
109106
for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
110107
// Sum over input channels
111108
int popcount = 0;
112-
int packed_channels = GetBitpackedSize(input_channels);
109+
int packed_channels =
110+
bitpacking::GetBitpackedSize(input_channels);
113111
for (int in_c = 0; in_c < packed_channels; ++in_c) {
114112
int filter_idx;
115113
// filter_data has shape
@@ -291,7 +289,8 @@ class PaddingFunctor {
291289
}
292290
};
293291

292+
} // namespace bconv2d
294293
} // namespace core
295294
} // namespace compute_engine
296295

297-
#endif // COMPUTE_ENGINE_KERNELS_PADDING_H_
296+
#endif // COMPUTE_ENGINE_CORE_BCONV2D_PADDING_FUNCTOR_H_

0 commit comments

Comments
 (0)