Skip to content

Commit 24faf9e

Browse files
authored
Support cross compilation for ARM (#16899)
Enable setting thread number in Anakin Config Fix code style Reduce cmake file changes test=release/1.4
1 parent e4e5bad commit 24faf9e

File tree

9 files changed

+1012
-0
lines changed

9 files changed

+1012
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
2626
"${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
2727
message(STATUS "AR tools: ${CMAKE_AR}")
2828

29+
option(WITH_ARM_CPU "Cross compile PaddlePaddle to support ARM CPU" OFF)
30+
if (WITH_ARM_CPU)
31+
add_subdirectory(paddle/fluid/inference/lite)
32+
return()
33+
endif()
34+
2935
if(WIN32)
3036
set(CMAKE_SUPPRESS_REGENERATION ON)
3137
set(CMAKE_STATIC_LIBRARY_PREFIX lib)

cmake/system.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# for instance, protobuf libs path is <install_dir>/lib64
2121
# on CentOS, but <install_dir>/lib on other systems.
2222

23+
if (WITH_ARM_CPU)
24+
return()
25+
endif()
26+
2327
IF(WIN32)
2428
SET(HOST_SYSTEM "win32")
2529
ELSE(WIN32)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
add_definitions(-DUSE_ARM_PLACE)
2+
set(CMAKE_CXX_FLAGS "-std=c++11 -pie -fPIE -Wno-attributes ${CMAKE_CXX_FLAGS}")
3+
if (NOT (${CMAKE_CXX_COMPILER} MATCHES "clang\\+\\+$"))
4+
set(CMAKE_CXX_FLAGS "-fopenmp ${CMAKE_CXX_FLAGS}")
5+
endif()
6+
if (ANDROID)
7+
set(CMAKE_CXX_FLAGS "-llog ${CMAKE_CXX_FLAGS}")
8+
endif()
9+
if (IOS)
10+
set(CMAKE_CXX_FLAGS "-fembed-bitcode ${CMAKE_CXX_FLAGS}")
11+
endif()
12+
13+
set(PADDLE_LITE_LIB paddle-lite)
14+
set(PADDLE_LITE_SRCS api.cc api_anakin_engine.cc)
15+
16+
set(PADDLE_LITE_PATH ${PADDLE_SOURCE_DIR}/paddle/fluid/inference/lite)
17+
include_directories(${CMAKE_SOURCE_DIR})
18+
include_directories(${PADDLE_LITE_PATH} ${PADDLE_LITE_PATH}/output
19+
${PADDLE_LITE_PATH}/output/saber)
20+
21+
if (BUILD_SHARED_LIBS)
22+
add_library(${PADDLE_LITE_LIB} SHARED ${PADDLE_LITE_SRCS})
23+
else()
24+
add_library(${PADDLE_LITE_LIB} STATIC ${PADDLE_LITE_SRCS})
25+
endif(BUILD_SHARED_LIBS)
26+
#target_link_libraries(${PADDLE_LITE_LIB} )
27+
28+
#add_library(anakin SHARED IMPORTED)
29+
#set_target_properties(anakin PROPERTIES IMPORTED_LOCATION
30+
# ${PADDLE_LITE_PATH}/output/libanakin.so)
31+
add_library(anakin STATIC IMPORTED)
32+
set_target_properties(anakin PROPERTIES IMPORTED_LOCATION
33+
${PADDLE_LITE_PATH}/output/libanakin_static.a)
34+
add_library(saber_common STATIC IMPORTED)
35+
set_target_properties(saber_common PROPERTIES IMPORTED_LOCATION
36+
${PADDLE_LITE_PATH}/output/libanakin_saber_common.a)
37+
add_library(protobuf STATIC IMPORTED)
38+
set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION
39+
${PADDLE_LITE_PATH}/output/protobuf/lib/libprotobuf.a)
40+
41+
add_executable(test-benchmark benchmark/benchmark.cc)
42+
target_link_libraries(test-benchmark paddle-lite "-Wl,--whole-archive"
43+
saber_common anakin "-Wl,--no-whole-archive" protobuf)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include <cassert>
17+
#include <memory>
18+
#include <string>
19+
#include <vector>
20+
21+
#include "paddle_api.h" // NOLINT
22+
#include "utils/logger/logger.h"
23+
24+
namespace paddle {
25+
namespace contrib {
26+
// Configurations for Anakin engine.
27+
struct AnakinConfig : public PaddlePredictor::Config {
28+
enum TargetType { ARM = 0, GPU };
29+
enum PrecisionType { FP32 = 0, FP16, INT8 };
30+
31+
std::string model_file;
32+
int max_batch_size = 1;
33+
int thread_num = 1;
34+
TargetType target_type = ARM;
35+
PrecisionType precision_type = FP32;
36+
};
37+
38+
} // namespace contrib
39+
} // namespace paddle

paddle/fluid/inference/lite/api.cc

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stdlib.h>
16+
#include <sstream>
17+
#include "paddle/fluid/inference/lite/paddle_api.h"
18+
19+
namespace paddle {
20+
21+
int PaddleDtypeSize(PaddleDType dtype) {
22+
switch (dtype) {
23+
case PaddleDType::FLOAT32:
24+
return sizeof(float);
25+
case PaddleDType::INT64:
26+
return sizeof(int64_t);
27+
case PaddleDType::INT32:
28+
return sizeof(int32_t);
29+
default:
30+
assert(false);
31+
return -1;
32+
}
33+
}
34+
35+
PaddleBuf::PaddleBuf(PaddleBuf &&other)
36+
: data_(other.data_),
37+
length_(other.length_),
38+
memory_owned_(other.memory_owned_) {
39+
other.memory_owned_ = false;
40+
other.data_ = nullptr;
41+
other.length_ = 0;
42+
}
43+
44+
PaddleBuf::PaddleBuf(const PaddleBuf &other) { *this = other; }
45+
46+
PaddleBuf &PaddleBuf::operator=(const PaddleBuf &other) {
47+
if (!other.memory_owned_) {
48+
data_ = other.data_;
49+
length_ = other.length_;
50+
memory_owned_ = other.memory_owned_;
51+
} else {
52+
Resize(other.length());
53+
memcpy(data_, other.data(), other.length());
54+
length_ = other.length();
55+
memory_owned_ = true;
56+
}
57+
return *this;
58+
}
59+
60+
PaddleBuf &PaddleBuf::operator=(PaddleBuf &&other) {
61+
// only the buffer with external memory can be copied
62+
data_ = other.data_;
63+
length_ = other.length_;
64+
memory_owned_ = other.memory_owned_;
65+
other.data_ = nullptr;
66+
other.length_ = 0;
67+
other.memory_owned_ = false;
68+
return *this;
69+
}
70+
71+
void PaddleBuf::Resize(size_t length) {
72+
// Only the owned memory can be reset, the external memory can't be changed.
73+
if (length_ >= length) return;
74+
if (memory_owned_) {
75+
Free();
76+
data_ = malloc(length);
77+
length_ = length;
78+
memory_owned_ = true;
79+
} else {
80+
// PADDLE_THROW("The memory is allocated externally, can not Resized");
81+
}
82+
}
83+
84+
void PaddleBuf::Reset(void *data, size_t length) {
85+
Free();
86+
memory_owned_ = false;
87+
data_ = data;
88+
length_ = length;
89+
}
90+
91+
void PaddleBuf::Free() {
92+
if (memory_owned_ && data_) {
93+
// PADDLE_ENFORCE_GT(length_, 0UL);
94+
free(static_cast<char *>(data_));
95+
data_ = nullptr;
96+
length_ = 0;
97+
}
98+
}
99+
100+
} // namespace paddle

0 commit comments

Comments
 (0)