Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ message(STATUS "CMAKE GENERATOR: ${CMAKE_GENERATOR}")
set(THIRD_PARTY_PATH "${PADDLE_BINARY_DIR}/third_party" CACHE STRING
"A path setting third party libraries download & build directories.")

# Simd configurations
include(simd)

if(WIN32)
include(os/windows)
endif()
Expand All @@ -51,12 +54,14 @@ lite_option(WITH_AVX "Compile PaddlePaddle with AVX in
lite_option(WITH_TESTING "Compile PaddlePaddle with unit testing" OFF)
lite_option(WITH_MKL "Compile PaddlePaddle with MKL support." ON IF ${AVX_FOUND})
lite_option(WITH_ARM_DOTPROD "Compile PaddlePaddle with ARM dot production" ON)
lite_option(WITH_LASX "Compile PaddlePaddle with LoongArch ASX intrinsics" ON IF ${LASX_FOUND})
lite_option(WITH_SYSTEM_BLAS "Use system blas library" OFF)
# for lite, both server and mobile framework.
lite_option(LITE_WITH_JAVA "Enable Java JNI lib in lite mode" OFF)
lite_option(LITE_WITH_STATIC_LIB "Enable static cplus lib in lite mode" OFF)
lite_option(LITE_WITH_PYTHON "Enable Python api lib in lite mode" OFF)
lite_option(LITE_WITH_X86 "Enable X86 in lite mode" ON)
lite_option(LITE_WITH_LOONGARCH "Enable LoongArch in lite mode" OFF)
lite_option(LITE_WITH_ARM "Enable ARM in lite mode" OFF)
lite_option(LITE_WITH_SW "Enable SW in lite mode" OFF)
lite_option(LITE_WITH_NNADAPTER "Enable NNAdapter in lite mode" OFF)
Expand Down Expand Up @@ -92,9 +97,6 @@ lite_option(LITE_WITH_ARM_DNN_LIBRARY "Use Arm DNN library instead of b
find_package(Git REQUIRED)
find_package(Threads REQUIRED)

# Simd configurations
include(simd)

# CMAKE_BUILD_TYPE
if(NOT CMAKE_BUILD_TYPE)
if(WIN32)
Expand Down
8 changes: 7 additions & 1 deletion cmake/backends/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ if(LITE_WITH_XPU)
include(backends/xpu)
endif()

include(backends/x86)
if(LITE_WITH_X86)
include(backends/x86)
endif()

if(LITE_WITH_LOONGARCH)
include(backends/loongarch)
endif()

# Add dependencies
include(generic) # simplify cmake module
Expand Down
36 changes: 36 additions & 0 deletions cmake/backends/loongarch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2021 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.

IF(NOT LITE_WITH_LOONGARCH)
RETURN()
ENDIF()

# We need C++17.
SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_STANDARD_REQUIRED True)
# But some warning should be omit.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=register")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=array-bounds")

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mlsx")
INCLUDE(external/xxhash) # download install xxhash

IF(LITE_WITH_OPENMP)
ADD_DEFINITIONS(-DWITH_OMP)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fopenmp")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fopenmp")
ENDIF()
4 changes: 4 additions & 0 deletions cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ if (LITE_WITH_X86)
add_definitions("-DLITE_WITH_X86")
endif()

if (LITE_WITH_LOONGARCH)
add_definitions("-DLITE_WITH_LOONGARCH")
endif()

if (LITE_WITH_ARM)
add_definitions("-DLITE_WITH_ARM")
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/mklml.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ ExternalProject_Add(
${CMAKE_COMMAND} -E copy_directory ${MKLML_DOWNLOAD_DIR}/lib ${MKLML_LIB_DIR}
)

IF(NOT WIN32 AND NOT LITE_WITH_SW)
IF(NOT WIN32 AND NOT LITE_WITH_SW AND NOT LITE_WITH_LOONGARCH)
add_compile_options(-m64)
ENDIF()
INCLUDE_DIRECTORIES(${MKLML_INC_DIR})
Expand Down
4 changes: 2 additions & 2 deletions cmake/external/openblas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ IF(NOT ${CBLAS_FOUND})

IF (NOT WIN32)
SET(OPENBLAS_CC "${CMAKE_C_COMPILER} -Wno-unused-but-set-variable -Wno-unused-variable")
SET(OPENBLAS_COMMIT "v0.2.20")
SET(OPENBLAS_COMMIT "v0.3.28")

IF(APPLE)
SET(OPENBLAS_CC "${CMAKE_C_COMPILER} -isysroot ${CMAKE_OSX_SYSROOT}")
Expand All @@ -47,7 +47,7 @@ IF(NOT ${CBLAS_FOUND})
ExternalProject_Add(
extern_openblas
${EXTERNAL_PROJECT_LOG_ARGS}
GIT_REPOSITORY https://github.com/xianyi/OpenBLAS.git
GIT_REPOSITORY https://github.com/OpenMathLib/OpenBLAS.git
GIT_TAG ${OPENBLAS_COMMIT}
PREFIX ${CBLAS_SOURCES_DIR}
INSTALL_DIR ${CBLAS_INSTALL_DIR}
Expand Down
2 changes: 1 addition & 1 deletion cmake/flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ set(GPU_COMMON_FLAGS
-Wno-error=array-bounds # Warnings in Eigen::array
-gencode arch=compute_62,code=sm_62
)
if(LITE_WITH_SW AND NOT EMSCRIPTEN)
if(LITE_WITH_SW AND NOT EMSCRIPTEN AND NOT LITE_WITH_LOONGARCH)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
endif()
endif(NOT WIN32)
Expand Down
4 changes: 4 additions & 0 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ macro(lite_option variable description value)
list(APPEND ${__varname} ${arg})
endif()
endforeach()
# If condition is null, condition shuold be FALSE
if("${__varname}" STREQUAL "__condition" AND __condition STREQUAL "")
set(__condition 1 GREATER 2)
endif()
unset(__varname)
if(__condition STREQUAL "")
set(__condition 2 GREATER 1)
Expand Down
11 changes: 10 additions & 1 deletion cmake/simd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

include(CheckCXXSourceRuns)
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)

if(IOS)
return ()
Expand Down Expand Up @@ -119,4 +120,12 @@ mark_as_advanced(MMX_FOUND SSE2_FOUND SSE3_FOUND AVX_FOUND AVX2_FOUND AVX512F_FO

if(WITH_AVX AND AVX_FOUND)
add_definitions(-DLITE_WITH_AVX)
endif()
endif()

if(LITE_WITH_LOONGARCH)
check_cxx_compiler_flag(-mlasx LASX_FOUND)
check_cxx_compiler_flag(-mlsx LSX_FOUND)
if(NOT LSX_FOUND)
message(FATAL_ERROR "At least LSX support!")
endif()
endif()
21 changes: 21 additions & 0 deletions lite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include(lite)

message(STATUS "LITE_WITH_X86:\t${LITE_WITH_X86}")
message(STATUS "LITE_WITH_ARM:\t${LITE_WITH_ARM}")
message(STATUS "LITE_WITH_LOONGARCH:\t${LITE_WITH_LOONGARCH}")
message(STATUS "LITE_WITH_SW:\t${LITE_WITH_SW}")
message(STATUS "LITE_WITH_OPENCL:\t${LITE_WITH_OPENCL}")
message(STATUS "LITE_WITH_METAL:\t${LITE_WITH_METAL}")
Expand Down Expand Up @@ -405,6 +406,26 @@ if (LITE_WITH_X86)
endif()
endif()

if (LITE_WITH_LOONGARCH)
add_custom_target(publish_inference_cxx_lib ${TARGET}
COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/bin"
COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
COMMAND cp "${PADDLE_SOURCE_DIR}/lite/api/paddle_*.h" "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
COMMAND cp "${PADDLE_BINARY_DIR}/lite/api/paddle_use_kernels.h" "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
COMMAND cp "${PADDLE_BINARY_DIR}/lite/api/paddle_use_ops.h" "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
COMMAND cp "${PADDLE_BINARY_DIR}/libpaddle_api_full_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
COMMAND cp "${PADDLE_BINARY_DIR}/libpaddle_api_light_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
COMMAND cp "${PADDLE_BINARY_DIR}/lite/api/*.so" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
)
add_dependencies(publish_inference_cxx_lib bundle_full_api)
add_dependencies(publish_inference_cxx_lib bundle_light_api)
add_dependencies(publish_inference_cxx_lib paddle_full_api_shared)
add_dependencies(publish_inference_cxx_lib paddle_light_api_shared)
add_dependencies(publish_inference publish_inference_cxx_lib)
add_dependencies(publish_inference test_model_bin)
endif()

if (LITE_WITH_ARM)
if (NOT LITE_ON_TINY_PUBLISH)
# add cxx lib
Expand Down
4 changes: 4 additions & 0 deletions lite/api/cxx_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ void Predictor::Build(const std::shared_ptr<cpp::ProgramDesc> &program_desc,
inner_places.insert(inner_places.begin(),
Place{TARGET(kX86), PRECISION(kInt8)});
}
if (valid_place.target == TARGET(kLoongArch)) {
inner_places.insert(inner_places.begin(),
Place{TARGET(kLoongArch), PRECISION(kInt8)});
}
}
}
// XPU target must make sure to insert in front of others.
Expand Down
15 changes: 15 additions & 0 deletions lite/api/cxx_api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
#endif
#include "lite/backends/x86/mklml.h"
#endif

#ifdef WITH_OMP
#include <omp.h>
#endif

namespace paddle {
namespace lite {

Expand Down Expand Up @@ -149,6 +154,16 @@ void CxxPaddleApiImpl::Init(const lite_api::CxxConfig &config) {
<< real_num_threads;
#endif

#if defined(WITH_OMP) && defined(LITE_WITH_LOONGARCH)
int num_threads = config.math_num_threads();
int max_num_threads = omp_get_max_threads();
int real_num_threads = std::min(num_threads, max_num_threads);
omp_set_num_threads(real_num_threads);
VLOG(3) << "math_num_threads() is set successfully and the "
"real number of threads is:"
<< real_num_threads;
#endif

#ifdef LITE_WITH_XPU
auto preferred_inputs = config.preferred_inputs_for_warmup();
for (auto &preferred_input : preferred_inputs) {
Expand Down
14 changes: 14 additions & 0 deletions lite/api/light_api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "lite/backends/x86/mklml.h"
#endif

#ifdef WITH_OMP
#include <omp.h>
#endif

namespace paddle {
namespace lite {

Expand Down Expand Up @@ -110,6 +114,16 @@ void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
"number of threads is:"
<< real_num_threads;
#endif

#if defined(WITH_OMP) && defined(LITE_WITH_LOONGARCH)
int num_threads = config.math_num_threads();
int max_num_threads = omp_get_max_threads();
int real_num_threads = std::min(num_threads, max_num_threads);
omp_set_num_threads(real_num_threads);
VLOG(3) << "math_num_threads() is set successfully and the "
"real number of threads is:"
<< real_num_threads;
#endif
}

LightPredictorImpl::~LightPredictorImpl() {
Expand Down
7 changes: 7 additions & 0 deletions lite/api/paddle_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ void ConfigBase::set_threads(int threads) {
mode_ = lite::DeviceInfo::Global().mode();
threads_ = lite::DeviceInfo::Global().threads();
#endif
#ifdef LITE_WITH_LOONGARCH
math_num_threads_ = threads >= 1 ? threads : 1;
#endif
}

void ConfigBase::set_metal_device(void *device) {
Expand Down Expand Up @@ -381,6 +384,10 @@ void ConfigBase::set_x86_math_num_threads(int threads) {
int ConfigBase::x86_math_num_threads() const { return x86_math_num_threads_; }
#endif

#ifdef LITE_WITH_LOONGARCH
int ConfigBase::math_num_threads() const { return math_num_threads_; }
#endif

void ConfigBase::set_subgraph_model_cache_buffers(
const std::string &key,
const std::vector<char> &cfg,
Expand Down
2 changes: 2 additions & 0 deletions lite/api/paddle_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class LITE_API ConfigBase {
std::map<std::string, std::vector<char>> nnadapter_model_cache_buffers_{};
int device_id_{0};
int x86_math_num_threads_ = 1;
int math_num_threads_ = 1;

std::string metal_path_;
bool metal_use_mps_{false};
Expand Down Expand Up @@ -418,6 +419,7 @@ class LITE_API ConfigBase {
// set x86_math_num_threads
void set_x86_math_num_threads(int threads);
int x86_math_num_threads() const;
int math_num_threads() const;

void set_metal_lib_path(const std::string& path);
void set_metal_use_mps(bool flag);
Expand Down
9 changes: 6 additions & 3 deletions lite/api/paddle_place.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ const std::string& TargetToStr(TargetType target) {
"imagination_nna",
"intel_fpga",
"metal",
"nnadapter"};
"nnadapter",
"loongarch"};
auto x = static_cast<int>(target);

CHECK_LT(x, static_cast<int>(TARGET(NUM)));
Expand Down Expand Up @@ -145,7 +146,8 @@ const std::string& TargetRepr(TargetType target) {
"kImaginationNNA",
"kIntelFPGA",
"kMetal",
"kNNAdapter"};
"kNNAdapter",
"kLoongArch"};
auto x = static_cast<int>(target);
CHECK_LT(x, static_cast<int>(TARGET(NUM)));
return target2string[x];
Expand Down Expand Up @@ -212,7 +214,8 @@ std::set<TargetType> ExpandValidTargets(TargetType target) {
TARGET(kImaginationNNA),
TARGET(kIntelFPGA),
TARGET(kMetal),
TARGET(kNNAdapter)});
TARGET(kNNAdapter),
TARGET(kLoongArch)});
if (target == TARGET(kAny)) {
return valid_set;
}
Expand Down
4 changes: 3 additions & 1 deletion lite/api/paddle_place.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once
#include <set>
#include <string>
#include <stdint.h>

// Generic helper definitions for shared library support
#if defined _WIN32 || defined __CYGWIN__
Expand Down Expand Up @@ -62,7 +63,8 @@ enum class TargetType : int {
kIntelFPGA = 16,
kMetal = 17,
kNNAdapter = 18,
NUM = 19, // number of fields.
kLoongArch = 19,
NUM = 20, // number of fields.
};
enum class PrecisionType : int {
kUnk = 0,
Expand Down
4 changes: 4 additions & 0 deletions lite/api/tools/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ set(TARGET "benchmark_bin")
lite_cc_binary(${TARGET} SRCS ${BENCHMARK_SRC}
DEPS gflags
CV_DEPS paddle_cv_arm)
IF("${CBLAS_PROVIDER}" STREQUAL "OPENBLAS")
TARGET_LINK_LIBRARIES(${TARGET} ${CBLAS_LIBRARIES})
ENDIF()


if(ARM_TARGET_OS STREQUAL "android")
# Validation Dataset
Expand Down
1 change: 1 addition & 0 deletions lite/backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(opencl)
add_subdirectory(arm)
add_subdirectory(x86)
add_subdirectory(loongarch)
add_subdirectory(host)
add_subdirectory(xpu)
add_subdirectory(metal)
Expand Down
Loading