From 752de2c5e0adbdd07dcd64a6be2372378db737d0 Mon Sep 17 00:00:00 2001 From: Frank Liu Date: Tue, 24 Aug 2021 20:55:20 -0700 Subject: [PATCH] Reformat .cc and .h file (#1191) Change-Id: Id8c933aacdfda9620f05868954047bbf93cee660 --- ...addlepaddle_jni_PaddleLibrary_inference.cc | 30 ++++++------- ...l_paddlepaddle_jni_PaddleLibrary_tensor.cc | 12 +++--- .../src/main/native/djl_paddle_utils.h | 18 ++++---- .../main/native/ai_djl_pytorch_jni_cache.h | 2 +- .../main/native/djl_pytorch_jni_exception.h | 25 ++++++----- .../src/main/native/djl_pytorch_utils.h | 42 ++++++++++--------- tools/gradle/cpp-formatter.gradle | 12 ++++-- 7 files changed, 74 insertions(+), 67 deletions(-) diff --git a/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_inference.cc b/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_inference.cc index debf476fc20..5b32d19d659 100644 --- a/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_inference.cc +++ b/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_inference.cc @@ -52,33 +52,33 @@ JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_loadExtraDir( } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_analysisConfigEnableMKLDNN( - JNIEnv* env, jobject jthis, jlong jhandle) { - auto* config_ptr = reinterpret_cast(jhandle); - config_ptr->EnableMKLDNN(); + JNIEnv* env, jobject jthis, jlong jhandle) { + auto* config_ptr = reinterpret_cast(jhandle); + config_ptr->EnableMKLDNN(); } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_analysisConfigDisableGLog( - JNIEnv* env, jobject jthis, jlong jhandle) { - auto* config_ptr = reinterpret_cast(jhandle); - config_ptr->DisableGlogInfo(); + JNIEnv* env, jobject jthis, jlong jhandle) { + auto* config_ptr = reinterpret_cast(jhandle); + config_ptr->DisableGlogInfo(); } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_analysisConfigCMLNumThreads( - JNIEnv* env, jobject jthis, jlong jhandle, jint num) { - auto* config_ptr = reinterpret_cast(jhandle); - config_ptr->SetCpuMathLibraryNumThreads(num); + JNIEnv* env, jobject jthis, jlong jhandle, jint num) { + auto* config_ptr = reinterpret_cast(jhandle); + config_ptr->SetCpuMathLibraryNumThreads(num); } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_analysisConfigSwitchIrOptim( - JNIEnv* env, jobject jthis, jlong jhandle, jboolean condition) { - auto* config_ptr = reinterpret_cast(jhandle); - config_ptr->SwitchIrOptim(condition); + JNIEnv* env, jobject jthis, jlong jhandle, jboolean condition) { + auto* config_ptr = reinterpret_cast(jhandle); + config_ptr->SwitchIrOptim(condition); } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_analysisConfigRemovePass( - JNIEnv* env, jobject jthis, jlong jhandle, jstring jpass) { - auto* config_ptr = reinterpret_cast(jhandle); - config_ptr->pass_builder()->DeletePass(djl::utils::jni::GetStringFromJString(env, jpass)); + JNIEnv* env, jobject jthis, jlong jhandle, jstring jpass) { + auto* config_ptr = reinterpret_cast(jhandle); + config_ptr->pass_builder()->DeletePass(djl::utils::jni::GetStringFromJString(env, jpass)); } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_deleteAnalysisConfig( diff --git a/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_tensor.cc b/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_tensor.cc index 2381b4c9bdc..028271bf6ac 100644 --- a/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_tensor.cc +++ b/paddlepaddle/paddlepaddle-native/src/main/native/ai_djl_paddlepaddle_jni_PaddleLibrary_tensor.cc @@ -69,13 +69,13 @@ JNIEXPORT jstring JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_getTensorNa } JNIEXPORT void JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_setTensorLoD( - JNIEnv* env, jobject jthis, jlong jhandle, jobjectArray j2dlongarray) { - auto tensor_ptr = reinterpret_cast(jhandle); - tensor_ptr->lod = djl::utils::jni::Get2DVecFrom2DLongArray(env, j2dlongarray); + JNIEnv* env, jobject jthis, jlong jhandle, jobjectArray j2dlongarray) { + auto tensor_ptr = reinterpret_cast(jhandle); + tensor_ptr->lod = djl::utils::jni::Get2DVecFrom2DLongArray(env, j2dlongarray); } JNIEXPORT jobjectArray JNICALL Java_ai_djl_paddlepaddle_jni_PaddleLibrary_getTensorLoD( - JNIEnv* env, jobject jthis, jlong jhandle) { - auto tensor_ptr = reinterpret_cast(jhandle); - return djl::utils::jni::Get2DLongArrayFrom2DVec(env, tensor_ptr->lod); + JNIEnv* env, jobject jthis, jlong jhandle) { + auto tensor_ptr = reinterpret_cast(jhandle); + return djl::utils::jni::Get2DLongArrayFrom2DVec(env, tensor_ptr->lod); } diff --git a/paddlepaddle/paddlepaddle-native/src/main/native/djl_paddle_utils.h b/paddlepaddle/paddlepaddle-native/src/main/native/djl_paddle_utils.h index e5bed1706fb..c651415e3c9 100644 --- a/paddlepaddle/paddlepaddle-native/src/main/native/djl_paddle_utils.h +++ b/paddlepaddle/paddlepaddle-native/src/main/native/djl_paddle_utils.h @@ -14,30 +14,30 @@ #ifndef DJL_PADDLE_DJL_PADDLE_JNI_UTILS_H #define DJL_PADDLE_DJL_PADDLE_JNI_UTILS_H +#include #include -#include #include -#include #include +#include namespace utils { -inline void GetZTensorFromTensor(paddle::ZeroCopyTensor* z_tensor, paddle::PaddleTensor* tensor) { +inline void GetZTensorFromTensor(paddle::ZeroCopyTensor *z_tensor, paddle::PaddleTensor *tensor) { z_tensor->Reshape(tensor->shape); z_tensor->SetLoD(tensor->lod); switch (tensor->dtype) { case paddle::PaddleDType::FLOAT32: - z_tensor->copy_from_cpu(static_cast(tensor->data.data())); + z_tensor->copy_from_cpu(static_cast(tensor->data.data())); break; case paddle::PaddleDType::INT32: - z_tensor->copy_from_cpu(static_cast(tensor->data.data())); + z_tensor->copy_from_cpu(static_cast(tensor->data.data())); break; case paddle::PaddleDType::INT64: - z_tensor->copy_from_cpu(static_cast(tensor->data.data())); + z_tensor->copy_from_cpu(static_cast(tensor->data.data())); break; case paddle::PaddleDType::UINT8: - z_tensor->copy_from_cpu(static_cast(tensor->data.data())); + z_tensor->copy_from_cpu(static_cast(tensor->data.data())); break; default: // TODO improve the error handling @@ -75,6 +75,6 @@ inline void GetTensorFromZTensor(paddle::ZeroCopyTensor *z_tensor, paddle::Paddl } } -} +} // namespace utils -#endif //DJL_PADDLE_DJL_PADDLE_JNI_UTILS_H +#endif // DJL_PADDLE_DJL_PADDLE_JNI_UTILS_H diff --git a/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_cache.h b/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_cache.h index b93c0bb92e2..96619c62555 100644 --- a/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_cache.h +++ b/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_cache.h @@ -28,4 +28,4 @@ extern jmethodID ERROR_METHOD; // the highest version Android JNI version is 1.6 static jint JNI_VERSION = JNI_VERSION_1_6; -#endif //DJL_TORCH_AI_DJL_PYTORCH_JNI_CACHE_H +#endif // DJL_TORCH_AI_DJL_PYTORCH_JNI_CACHE_H diff --git a/pytorch/pytorch-native/src/main/native/djl_pytorch_jni_exception.h b/pytorch/pytorch-native/src/main/native/djl_pytorch_jni_exception.h index b590e3e71fe..0d8769a2aa3 100644 --- a/pytorch/pytorch-native/src/main/native/djl_pytorch_jni_exception.h +++ b/pytorch/pytorch-native/src/main/native/djl_pytorch_jni_exception.h @@ -16,9 +16,9 @@ #include "ai_djl_pytorch_jni_cache.h" -#define DJL_CHECK_WITH_MSG(cond, error_msg) \ - if (!cond) { \ - env->ThrowNew(ENGINE_EXCEPTION_CLASS, error_msg); \ +#define DJL_CHECK_WITH_MSG(cond, error_msg) \ + if (!cond) { \ + env->ThrowNew(ENGINE_EXCEPTION_CLASS, error_msg); \ } /* @@ -27,16 +27,15 @@ * and finishes with API_END() */ #define API_BEGIN() try { - -#define API_END() \ - } \ - catch (const c10::Error& e) { \ - Log log(env); \ - log.debug(e.what()); \ - env->ThrowNew(ENGINE_EXCEPTION_CLASS, e.what_without_backtrace()); \ - } \ - catch (const std::exception& e_) { \ - env->ThrowNew(ENGINE_EXCEPTION_CLASS, e_.what()); \ +#define API_END() \ + } \ + catch (const c10::Error& e) { \ + Log log(env); \ + log.debug(e.what()); \ + env->ThrowNew(ENGINE_EXCEPTION_CLASS, e.what_without_backtrace()); \ + } \ + catch (const std::exception& e_) { \ + env->ThrowNew(ENGINE_EXCEPTION_CLASS, e_.what()); \ } // TODO refactor all jni functions to c style function which mean diff --git a/pytorch/pytorch-native/src/main/native/djl_pytorch_utils.h b/pytorch/pytorch-native/src/main/native/djl_pytorch_utils.h index cbf18f31992..5223e40ddf8 100644 --- a/pytorch/pytorch-native/src/main/native/djl_pytorch_utils.h +++ b/pytorch/pytorch-native/src/main/native/djl_pytorch_utils.h @@ -15,14 +15,13 @@ #include #include +#include +#include #include #include -#include #include -#include - #include "djl_pytorch_jni_log.h" // The file is utilities that are used for JNI @@ -31,13 +30,9 @@ namespace utils { #if !defined(__ANDROID__) // for image interpolation -typedef torch::variant< - torch::enumtype::kNearest, - torch::enumtype::kLinear, - torch::enumtype::kBilinear, - torch::enumtype::kBicubic, - torch::enumtype::kTrilinear, - torch::enumtype::kArea> mode_t; +typedef torch::variant + mode_t; #endif inline jint GetDTypeFromScalarType(const torch::ScalarType& type) { @@ -101,19 +96,26 @@ inline torch::Device GetDeviceFromJDevice(JNIEnv* env, jintArray jdevice) { #if !defined(__ANDROID__) inline mode_t GetInterpolationMode(jint jmode) { switch (jmode) { - case 0: return torch::kNearest; - case 1: return torch::kLinear; - case 2: return torch::kBilinear; - case 3: return torch::kBicubic; - case 4: return torch::kTrilinear; - case 5: return torch::kArea; + case 0: + return torch::kNearest; + case 1: + return torch::kLinear; + case 2: + return torch::kBilinear; + case 3: + return torch::kBicubic; + case 4: + return torch::kTrilinear; + case 5: + return torch::kArea; default: throw; } } #endif -inline std::vector CreateTensorIndex(JNIEnv* env, jlongArray jmin_indices, jlongArray jmax_indices, jlongArray jstep_indices) { +inline std::vector CreateTensorIndex( + JNIEnv* env, jlongArray jmin_indices, jlongArray jmax_indices, jlongArray jstep_indices) { const auto min_indices = djl::utils::jni::GetVecFromJLongArray(env, jmin_indices); const auto max_indices = djl::utils::jni::GetVecFromJLongArray(env, jmax_indices); const auto step_indices = djl::utils::jni::GetVecFromJLongArray(env, jstep_indices); @@ -121,7 +123,7 @@ inline std::vector CreateTensorIndex(JNIEnv* env, indices.reserve(min_indices.size()); for (size_t i = 0; i < min_indices.size(); ++i) { indices.emplace_back( - torch::indexing::TensorIndex(torch::indexing::Slice(min_indices[i], max_indices[i], step_indices[i]))); + torch::indexing::TensorIndex(torch::indexing::Slice(min_indices[i], max_indices[i], step_indices[i]))); } return indices; } @@ -131,8 +133,8 @@ inline torch::TensorOptions CreateTensorOptions( // it gets the device and collect jdevice memory const auto device = utils::GetDeviceFromJDevice(env, jdevice); auto options = torch::TensorOptions() - // for tensor creation API, MKLDNN layout is not supported - // the workaround is to create with Strided then call to_mkldnn() + // for tensor creation API, MKLDNN layout is not supported + // the workaround is to create with Strided then call to_mkldnn() .layout((jlayout != 1) ? torch::kStrided : torch::kSparse) .memory_format(torch::MemoryFormat::Contiguous) .device(device) diff --git a/tools/gradle/cpp-formatter.gradle b/tools/gradle/cpp-formatter.gradle index 58d071bf8a7..6d5cc3fb10c 100644 --- a/tools/gradle/cpp-formatter.gradle +++ b/tools/gradle/cpp-formatter.gradle @@ -13,7 +13,7 @@ class CppFormatterPlugin implements Plugin { } static def formatCpp(File f, File clang) { - if (!f.getName().endsWith(".cc") && !f.getName().endsWith(".h")) { + if (!f.getName().endsWith(".cc") && !f.getName().endsWith(".cpp") && !f.getName().endsWith(".h")) { return } ProcessBuilder pb = new ProcessBuilder("${clang.absolutePath}", @@ -34,7 +34,10 @@ class CppFormatterPlugin implements Plugin { Project rootProject = project.getRootProject() def clang = new File("${rootProject.projectDir}/.clang/clang-format") checkClang(clang) - def files = project.fileTree("src").include("**/*.cc") + def files = project.fileTree("src") + .include("**/*.cc") + .include("**/*.cpp") + .include("**/*.h") for (File f : files) { if (!f.isFile()) { continue @@ -53,7 +56,10 @@ class CppFormatterPlugin implements Plugin { Project rootProject = project.getRootProject() def clang = new File("${rootProject.projectDir}/.clang/clang-format") checkClang(clang) - def files = project.fileTree("src").include("**/*.cc") + def files = project.fileTree("src") + .include("**/*.cc") + .include("**/*.cpp") + .include("**/*.h") for (File f : files) { if (!f.isFile()) { continue