Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake治理]Compile ddim as separate dynamic library #58899

Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(PYTHON_TESTS_DIR
CACHE INTERNAL "python tests directory")

add_subdirectory(utils)
add_subdirectory(common)
add_subdirectory(pir)
add_subdirectory(scripts)
add_subdirectory(testing)
Expand Down
25 changes: 25 additions & 0 deletions paddle/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
file(GLOB common_srcs "*.cc")

if(WIN32)
set(COMMON_NAME
common.dll
CACHE INTERNAL "" FORCE)
elseif(APPLE)
set(COMMON_NAME
libcommon.dylib
CACHE INTERNAL "" FORCE)
else()
set(COMMON_NAME
libcommon.so
CACHE INTERNAL "" FORCE)
endif()

set(COMMON_LIB
"${CMAKE_CURRENT_BINARY_DIR}/${COMMON_NAME}"
CACHE FILEPATH "COMMON Library" FORCE)

set(COMMON_BUILD_TYPE
SHARED
CACHE INTERNAL "" FORCE)

cc_library(common ${COMMON_BUILD_TYPE} SRCS ${common_srcs})
142 changes: 142 additions & 0 deletions paddle/common/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2023 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

#include <cstdint>

#include "paddle/common/enforce.h"
#include "paddle/common/unroll_array_ops.h"

namespace common {

template <typename T, size_t N>
class Array {
public:
static constexpr size_t kSize = N;

HOSTDEVICE inline Array() {}

template <typename... Args>
HOSTDEVICE inline explicit Array(const T &val, Args... args) {
static_assert(N == sizeof...(Args) + 1, "Invalid argument");
UnrollVarArgsAssign<T>::Run(data_, val, args...);
}

HOSTDEVICE inline void Fill(const T &val) {
UnrollFillConstant<N>::Run(data_, val);
}

HOSTDEVICE inline const T *Get() const { return data_; }

HOSTDEVICE inline T *GetMutable() { return data_; }

HOSTDEVICE inline T &operator[](size_t i) { return *advance(data_, i); }

// Writing "return data_[i]" would cause compilation warning/error:
// "array subscript is above array bound" in Python 35 CI.
// It seems that it is a false warning of GCC if we do not check the bounds
// of array index. But for better performance, we do not check in operator[]
// like what is in STL. If users want to check the bounds, use at() instead
HOSTDEVICE inline const T &operator[](size_t i) const {
return *advance(data_, i);
}

HOSTDEVICE inline T &at(size_t i) {
#if !defined(__CUDA_ARCH__) && !defined(__HIPCC__)
COMMON_ENFORCE_LT(
i, N, common::errors::OutOfRange("Array index out of bounds."));
#endif
return (*this)[i];
}

HOSTDEVICE inline const T &at(size_t i) const {
#if !defined(__CUDA_ARCH__) && !defined(__HIPCC__)
COMMON_ENFORCE_LT(
i, N, common::errors::OutOfRange("Array index out of bounds."));
#endif
return (*this)[i];
}

HOSTDEVICE constexpr size_t size() const { return N; }

HOSTDEVICE inline bool operator==(const Array<T, N> &other) const {
return UnrollCompare<N>::Run(data_, other.data_);
}

HOSTDEVICE inline bool operator!=(const Array<T, N> &other) const {
return !(*this == other);
}

private:
template <typename U>
HOSTDEVICE static inline U *advance(U *ptr, size_t i) {
return ptr + i;
}

T data_[N] = {};
};

template <typename T>
class Array<T, 0> {
public:
static constexpr size_t kSize = 0;

HOSTDEVICE inline Array() {}

HOSTDEVICE inline void Fill(const T &val) {}

HOSTDEVICE inline constexpr T *Get() const { return nullptr; }

// Add constexpr to GetMutable() cause warning in MAC
HOSTDEVICE inline T *GetMutable() { return nullptr; }

HOSTDEVICE inline T &operator[](size_t) {
#if defined(__HIPCC__) || defined(__CUDA_ARCH__)
// HIP and CUDA will have compile error, if use "obj()"
// function declared in block scope cannot have 'static' storage class
static T obj{};
return obj;
#else
COMMON_THROW(common::errors::Unavailable("Array<T, 0> has no element."));
#endif
}

HOSTDEVICE inline const T &operator[](size_t) const {
#if defined(__HIPCC__) || defined(__CUDA_ARCH__)
// HIP and CUDA will have compile error, if use "obj()"
// function declared in block scope cannot have 'static' storage class
static const T obj{};
return obj;
#else
COMMON_THROW(common::errors::Unavailable("Array<T, 0> has no element."));
#endif
}

HOSTDEVICE inline T &at(size_t i) { return (*this)[i]; }

HOSTDEVICE inline const T &at(size_t i) const { return (*this)[i]; }

HOSTDEVICE constexpr size_t size() const { return 0; }

HOSTDEVICE constexpr bool operator==(const Array<T, 0> &other) const {
return true;
}

HOSTDEVICE constexpr bool operator!=(const Array<T, 0> &other) const {
return false;
}
};

} // namespace common
Loading