Commit 01a10755 authored by yuguo-Jack's avatar yuguo-Jack
Browse files

2.5.2-dtk24.04

parent 63eb0da5
/* Copyright (c) 2013 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
#ifdef __GNUC__
#include <cxxabi.h> // for __cxa_demangle
#endif // __GNUC__
#include <exception>
#include <iostream>
#if !defined(_WIN32)
#include <dlfcn.h> // dladdr
#include <unistd.h> // sleep, usleep
#else // _WIN32
#ifndef NOMINMAX
#define NOMINMAX // msvc max/min macro conflict with std::min/max
#endif
#include <windows.h> // GetModuleFileName, Sleep
#endif
#include "paddle/common/macros.h"
#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL)
#include <execinfo.h>
#endif
// #define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with
// windows.h
#include "paddle/common/errors.h"
#include "paddle/utils/string/printf.h"
#include "paddle/utils/string/to_string.h"
#include "paddle/utils/test_macros.h"
#include "paddle/utils/variant.h"
namespace common {
class CommonNotMetException : public std::exception {
public:
explicit CommonNotMetException(const std::string& str) : err_str_(str) {}
const char* what() const noexcept override { return err_str_.c_str(); }
private:
std::string err_str_;
};
} // namespace common
namespace common {
namespace enforce {
#if !defined(_WIN32)
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
#else
// there is no equivalent intrinsics in msvc.
#define UNLIKELY(condition) (condition)
#endif
#if defined _WIN32 && defined PADDLE_ON_INFERENCE && defined PADDLE_NO_PYTHON
#define HANDLE_THE_ERROR try {
#define END_HANDLE_THE_ERROR \
} \
catch (const std::exception& e) { \
std::cout << e.what() << std::endl; \
throw; \
}
#else
#define HANDLE_THE_ERROR
#define END_HANDLE_THE_ERROR
#endif
template <typename T>
inline constexpr bool IsArithmetic() {
return std::is_arithmetic<T>::value;
}
template <typename T1, typename T2, bool kIsArithmetic /* = true */>
struct TypeConverterImpl {
using Type1 = typename std::common_type<T1, T2>::type;
using Type2 = Type1;
};
template <typename T1, typename T2>
struct TypeConverterImpl<T1, T2, false> {
using Type1 = T1;
using Type2 = T2;
};
template <typename T1, typename T2>
struct TypeConverter {
static constexpr bool kIsArithmetic =
IsArithmetic<T1>() && IsArithmetic<T2>();
using Type1 = typename TypeConverterImpl<T1, T2, kIsArithmetic>::Type1;
using Type2 = typename TypeConverterImpl<T1, T2, kIsArithmetic>::Type2;
};
template <typename T1, typename T2>
using CommonType1 = typename std::add_lvalue_reference<
typename std::add_const<typename TypeConverter<T1, T2>::Type1>::type>::type;
template <typename T1, typename T2>
using CommonType2 = typename std::add_lvalue_reference<
typename std::add_const<typename TypeConverter<T1, T2>::Type2>::type>::type;
#define COMMON_THROW(...) \
do { \
HANDLE_THE_ERROR \
throw common::CommonNotMetException( \
paddle::string::Sprintf("Error occured at: %s:%d :\n%s", \
__FILE__, \
__LINE__, \
paddle::string::Sprintf(__VA_ARGS__))); \
END_HANDLE_THE_ERROR \
} while (0)
#define __COMMON_BINARY_COMPARE(__VAL1, __VAL2, __CMP, __INV_CMP, ...) \
do { \
auto __val1 = (__VAL1); \
auto __val2 = (__VAL2); \
using __TYPE1__ = decltype(__val1); \
using __TYPE2__ = decltype(__val2); \
using __COMMON_TYPE1__ = \
common::enforce::CommonType1<__TYPE1__, __TYPE2__>; \
using __COMMON_TYPE2__ = \
::common::enforce::CommonType2<__TYPE1__, __TYPE2__>; \
bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \
static_cast<__COMMON_TYPE2__>(__val2)); \
if (UNLIKELY(!__is_not_error)) { \
auto __message__ = \
::paddle::string::Sprintf("%s\n [Hint: Expected %s " #__CMP \
" %s,but received %s " #__INV_CMP " %s.]", \
#__VAL1, \
#__VAL2, \
#__VAL1, \
#__VAL2); \
try { \
throw common::CommonNotMetException(__message__); \
} catch (const std::exception& e) { \
std::cout << e.what() << std::endl; \
throw; \
} \
} \
} while (0)
#define COMMON_ENFORCE_EQ(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, ==, !=, __VA_ARGS__)
#define COMMON_ENFORCE_NE(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, !=, ==, __VA_ARGS__)
#define COMMON_ENFORCE_GT(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, >, <=, __VA_ARGS__)
#define COMMON_ENFORCE_GE(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, >=, <, __VA_ARGS__)
#define COMMON_ENFORCE_LT(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, <, >=, __VA_ARGS__)
#define COMMON_ENFORCE_LE(__VAL0, __VAL1, ...) \
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, <=, >, __VA_ARGS__)
} // namespace enforce
} // namespace common
/* 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 <memory>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#include "paddle/utils/string/printf.h"
#include "paddle/utils/test_macros.h"
namespace common {
enum ErrorCode {
// Legacy error.
// Error type string: "Error"
LEGACY = 0,
// Client specified an invalid argument.
// Error type string: "InvalidArgumentError"
INVALID_ARGUMENT = 1,
// Some requested entity (e.g., file or directory) was not found.
// Error type string: "NotFoundError"
NOT_FOUND = 2,
// Operation tried to iterate past the valid input range. E.g., seeking or
// reading past end of file.
// Error type string: "OutOfRangeError"
OUT_OF_RANGE = 3,
// Some entity that we attempted to create (e.g., file or directory)
// already exists.
// Error type string: "AlreadyExistsError"
ALREADY_EXISTS = 4,
// Some resource has been exhausted, perhaps a per-user quota, or
// perhaps the entire file system is out of space.
// Error type string: "ResourceExhaustedError"
RESOURCE_EXHAUSTED = 5,
// Operation was rejected because the system is not in a state
// required for the operation's execution.
// Error type string: "PreconditionNotMetError"
PRECONDITION_NOT_MET = 6,
// The caller does not have permission to execute the specified
// operation.
// Error type string: "PermissionDeniedError"
PERMISSION_DENIED = 7,
// Deadline expired before operation could complete.
// Error type string: "ExecutionTimeout"
EXECUTION_TIMEOUT = 8,
// Operation is not implemented or not supported/enabled in this service.
// Error type string: "UnimplementedError"
UNIMPLEMENTED = 9,
// The service is currently unavailable. This is a most likely a
// transient condition and may be corrected by retrying with
// a backoff.
// Error type string: "UnavailableError"
UNAVAILABLE = 10,
// Fatal errors. Means some invariant expected by the underlying
// system has been broken. If you see one of these errors,
// something is very broken.
// Error type string: "FatalError"
FATAL = 11,
// Third-party library error.
// Error type string: "ExternalError"
EXTERNAL = 12,
};
class ErrorSummary {
public:
// Note(chenweihang): Final deprecated constructor
// This constructor is used to be compatible with
// current existing untyped PADDLE_ENFORCE_*
// PADDLE_ENFORCE
// Note(chenweihang): Windows openblas need this
// constructor for compiling PADDLE_ENFORCE in *.cu,
// this is a bug cause we can't remove this
// constructor now.
template <typename... Args>
explicit ErrorSummary(Args... args) {
code_ = common::ErrorCode::LEGACY;
msg_ = paddle::string::Sprintf(args...);
}
// Note(chenweihang): Only recommended constructor
// No longer supports PADDLE_ENFORCE without type or without error message
explicit ErrorSummary(ErrorCode code, std::string msg)
: code_(code), msg_(msg) {}
ErrorCode code() const { return code_; }
const std::string& error_message() const { return msg_; }
TEST_API std::string to_string() const;
private:
ErrorCode code_;
std::string msg_;
};
namespace errors {
#define REGISTER_ERROR(FUNC, CONST, ...) \
template <typename... Args> \
::common::ErrorSummary FUNC(Args... args) { \
return ::common::ErrorSummary(::common::CONST, \
::paddle::string::Sprintf(args...)); \
}
REGISTER_ERROR(InvalidArgument, ErrorCode::INVALID_ARGUMENT)
REGISTER_ERROR(NotFound, ErrorCode::NOT_FOUND)
REGISTER_ERROR(OutOfRange, ErrorCode::OUT_OF_RANGE)
REGISTER_ERROR(AlreadyExists, ErrorCode::ALREADY_EXISTS)
REGISTER_ERROR(ResourceExhausted, ErrorCode::RESOURCE_EXHAUSTED)
REGISTER_ERROR(PreconditionNotMet, ErrorCode::PRECONDITION_NOT_MET)
REGISTER_ERROR(PermissionDenied, ErrorCode::PERMISSION_DENIED)
REGISTER_ERROR(ExecutionTimeout, ErrorCode::EXECUTION_TIMEOUT)
REGISTER_ERROR(Unimplemented, ErrorCode::UNIMPLEMENTED)
REGISTER_ERROR(Unavailable, ErrorCode::UNAVAILABLE)
REGISTER_ERROR(Fatal, ErrorCode::FATAL)
REGISTER_ERROR(External, ErrorCode::EXTERNAL)
#undef REGISTER_ERROR
} // namespace errors
} // namespace common
/* 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 <iostream>
#include <sstream>
#include <string>
namespace common {
//////////////// Exception handling and Error Message /////////////////
#if !defined(_WIN32)
#define PD_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0))
#define PD_LIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 1))
#else
#define PD_UNLIKELY(expr) (expr)
#define PD_LIKELY(expr) (expr)
#endif
struct PD_Exception : public std::exception {
public:
template <typename... Args>
explicit PD_Exception(const std::string& msg,
const char* file,
int line,
const char* default_msg) {
std::ostringstream sout;
if (msg.empty()) {
sout << default_msg << "\n [" << file << ":" << line << "]";
} else {
sout << msg << "\n [" << file << ":" << line << "]";
}
err_msg_ = sout.str();
}
const char* what() const noexcept override { return err_msg_.c_str(); }
private:
std::string err_msg_;
};
class ErrorMessage {
public:
template <typename... Args>
explicit ErrorMessage(const Args&... args) {
build_string(args...);
}
void build_string() { oss << ""; }
template <typename T>
void build_string(const T& t) {
oss << t;
}
template <typename T, typename... Args>
void build_string(const T& t, const Args&... args) {
build_string(t);
build_string(args...);
}
std::string to_string() { return oss.str(); }
private:
std::ostringstream oss;
};
#define PD_CHECK(COND, ...) \
do { \
if (PD_UNLIKELY(!(COND))) { \
auto __message__ = ::common::ErrorMessage(__VA_ARGS__).to_string(); \
throw ::common::PD_Exception(__message__, \
__FILE__, \
__LINE__, \
"Expected " #COND \
", but it's not satisfied."); \
} \
} while (0)
#define PD_THROW(...) \
do { \
auto __message__ = ::common::ErrorMessage(__VA_ARGS__).to_string(); \
throw ::common::PD_Exception( \
__message__, __FILE__, __LINE__, "An error occurred."); \
} while (0)
} // namespace common
// 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
#ifdef __HIPCC__
#include <hip/hip_runtime.h>
#endif
#if defined(__xpu__)
#include <xpu/runtime.h>
#include "xpu/kernel/cluster_header.h"
#include "xpu/kernel/debug.h"
#include "xpu/kernel/math.h"
#endif
#if (defined(__CUDACC__) || defined(__HIPCC__) || defined(__xpu__))
#define HOSTDEVICE __host__ __device__
#define DEVICE __device__
#define HOST __host__
#else
#define HOSTDEVICE
#define DEVICE
#define HOST
#endif
/* 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
namespace common {
// Disable the copy and assignment operator for a class.
#define DISABLE_COPY_AND_ASSIGN(classname) \
private: \
classname(const classname&) = delete; \
classname(classname&&) = delete; \
classname& operator=(const classname&) = delete; \
classname& operator=(classname&&) = delete
#define PD_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg) \
_PD_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg)
#define _PD_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg) \
struct __test_global_namespace_##uniq_name##__ {}; \
static_assert(std::is_same<::__test_global_namespace_##uniq_name##__, \
__test_global_namespace_##uniq_name##__>::value, \
msg)
#ifdef __COUNTER__
#define PD_ID __COUNTER__
#else
#define PD_ID __LINE__
#endif
#if defined(_WIN32)
#define UNUSED
#define __builtin_expect(EXP, C) (EXP)
#else
#define UNUSED __attribute__((unused))
#endif
#define PD_CONCATENATE(arg1, arg2) PD_CONCATENATE1(arg1, arg2)
#define PD_CONCATENATE1(arg1, arg2) PD_CONCATENATE2(arg1, arg2)
#define PD_CONCATENATE2(arg1, arg2) arg1##arg2
#define PD_EXPAND(x) x
#if defined(__NVCC__) || defined(__HIPCC__)
#define PADDLE_RESTRICT __restrict__
#else
#define PADDLE_RESTRICT
#endif
#ifndef PADDLE_WITH_MUSL
#if defined(__FLT_MAX__)
#define FLT_MAX __FLT_MAX__
#endif // __FLT_MAX__
#endif // PADDLE_WITH_MUSL
} // namespace common
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment