Skip to content

Commit

Permalink
[CustomKernel] add custom kernel c api (PaddlePaddle#42986)
Browse files Browse the repository at this point in the history
* [CustomKernel] add custom kernel c api

* update

* update

* fix unable to export capi

Co-authored-by: ronny1996 <524019753@qq.com>
  • Loading branch information
2 people authored and sneaxiy committed Jun 27, 2022
1 parent 2c056e1 commit 3e88726
Show file tree
Hide file tree
Showing 40 changed files with 3,910 additions and 3 deletions.
6 changes: 5 additions & 1 deletion paddle/fluid/pybind/CMakeLists.txt
Expand Up @@ -123,6 +123,10 @@ set(PYBIND_SRCS
communication.cc
cuda_streams_py.cc)

if(WITH_CUSTOM_DEVICE)
set(PYBIND_DEPS ${PYBIND_DEPS} phi_capi)
endif()

if(NOT ON_INFER)
set(PYBIND_DEPS ${PYBIND_DEPS} processgroup eager_reducer)
if(WITH_NCCL)
Expand Down Expand Up @@ -491,7 +495,7 @@ if(WITH_PYTHON)
cc_library(
paddle_pybind SHARED
SRCS ${PYBIND_SRCS}
DEPS ${PYBIND_DEPS} ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS} ${GLOB_DEV_LIB})
DEPS ${PYBIND_DEPS} ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS})

if(NOT APPLE AND NOT WIN32)
target_link_libraries(paddle_pybind rt)
Expand Down
11 changes: 11 additions & 0 deletions paddle/fluid/pybind/imperative.cc
Expand Up @@ -1734,6 +1734,17 @@ void BindImperative(py::module *m_ptr) {
return new_var;
},
py::return_value_policy::copy)
.def(
"_copy_to",
[](const std::shared_ptr<imperative::VarBase> &self,
const platform::CustomPlace &place, bool blocking) {
auto new_var = self->NewVarBase(place, blocking);
if (!blocking) {
IncreaseVarbaseReferenceCountUntilCopyComplete(self, place);
}
return new_var;
},
py::return_value_policy::copy)
.def(
"_copy_to",
[](const std::shared_ptr<imperative::VarBase> &self,
Expand Down
4 changes: 4 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Expand Up @@ -147,6 +147,10 @@ limitations under the License. */
#include "paddle/fluid/platform/device/xpu/xpu_op_list.h"
#endif

#ifdef PADDLE_WITH_CUSTOM_DEVICE
#include "paddle/phi/capi/capi.h"
#endif

#include "paddle/fluid/platform/cuda_graph_with_memory_pool.h"

#ifdef PADDLE_WITH_IPU
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/CMakeLists.txt
Expand Up @@ -21,6 +21,10 @@ add_subdirectory(ops)
add_subdirectory(tools)
# phi tests
add_subdirectory(tests)
# phi capi
if(WITH_CUSTOM_DEVICE)
add_subdirectory(capi)
endif()

# make an unity target for compile deps
set(PHI_DEPS
Expand Down
6 changes: 4 additions & 2 deletions paddle/phi/backends/custom/custom_device.cc
Expand Up @@ -348,7 +348,8 @@ class CustomDevice : public DeviceInterface {
}
} else {
if (!pimpl_->memory_copy_p2p) {
std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
std::unique_ptr<uint8_t> tmp(
reinterpret_cast<uint8_t*>(new uint8_t[size]));
MemoryCopyD2H(src_dev_id, tmp.get(), src, size);
MemoryCopyH2D(dst_dev_id, dst, tmp.get(), size);
} else {
Expand Down Expand Up @@ -440,7 +441,8 @@ class CustomDevice : public DeviceInterface {
PADDLE_ENFORCE_CUSTOM_DEVICE_SUCCESS(
pimpl_->device_memory_set(device, ptr, value, size));
} else {
std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
std::unique_ptr<uint8_t> tmp(
reinterpret_cast<uint8_t*>(new uint8_t[size]));
memset(tmp.get(), value, size);
MemoryCopyH2D(dev_id, ptr, tmp.get(), size);
}
Expand Down
27 changes: 27 additions & 0 deletions paddle/phi/backends/device_ext.h
Expand Up @@ -25,6 +25,33 @@ extern "C" {
#define PADDLE_CUSTOM_RUNTIME_MINOR_VERSION 1
#define PADDLE_CUSTOM_RUNTIME_PATCH_VERSION 1

typedef enum {
UNDEFINED = 0,
BOOL,
UINT8,
UINT16,
UINT32,
UINT64,
INT8,
INT16,
INT32,
INT64,
FLOAT16,
FLOAT32,
FLOAT64,
BFLOAT16,
} C_DataType;

typedef enum {
ANY = 0,
NHWC,
NCHW,
NCDHW,
NDHWC,
NUM_DATA_LAYOUTS,
ALL_LAYOUT = ANY,
} C_DataLayout;

typedef enum {
C_SUCCESS = 0, // success
C_WARNING, // results may not meet expectation (such as an asynchronous
Expand Down
13 changes: 13 additions & 0 deletions paddle/phi/capi/CMakeLists.txt
@@ -0,0 +1,13 @@
add_subdirectory(lib)
cc_library(
phi_capi
SRCS all.cc
DEPS phi_c_data_type
phi_c_device_context
phi_c_int_array
phi_c_kernel_context
phi_c_kernel_factory
phi_c_kernel_registry
phi_c_place
phi_c_scalar
phi_c_tensor)
19 changes: 19 additions & 0 deletions paddle/phi/capi/all.cc
@@ -0,0 +1,19 @@
/* 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. */

#include "paddle/phi/capi/all.h"

namespace paddle {
namespace capi {} // namespace capi
} // namespace paddle
30 changes: 30 additions & 0 deletions paddle/phi/capi/all.h
@@ -0,0 +1,30 @@
// Copyright (c) 2022 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

#if !defined(_WIN32) && !defined(__APPLE__)

#include "paddle/phi/capi/include/c_data_type.h"
#include "paddle/phi/capi/include/c_device_context.h"
#include "paddle/phi/capi/include/c_int_array.h"
#include "paddle/phi/capi/include/c_kernel_context.h"
#include "paddle/phi/capi/include/c_kernel_factory.h"
#include "paddle/phi/capi/include/c_kernel_registry.h"
#include "paddle/phi/capi/include/c_place.h"
#include "paddle/phi/capi/include/c_scalar.h"
#include "paddle/phi/capi/include/c_tensor.h"
#include "paddle/phi/capi/include/data_type.h"
#include "paddle/phi/capi/include/kernel_registry.h"

#endif
30 changes: 30 additions & 0 deletions paddle/phi/capi/capi.h
@@ -0,0 +1,30 @@
// Copyright (c) 2022 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

#if !defined(_WIN32) && !defined(__APPLE__)

#include "paddle/phi/capi/include/common.h"

PD_DECLARE_CAPI(data_type);
PD_DECLARE_CAPI(device_context);
PD_DECLARE_CAPI(int_array);
PD_DECLARE_CAPI(kernel_context);
PD_DECLARE_CAPI(kernel_factory);
PD_DECLARE_CAPI(kernel_registry);
PD_DECLARE_CAPI(place);
PD_DECLARE_CAPI(scalar);
PD_DECLARE_CAPI(tensor);

#endif
56 changes: 56 additions & 0 deletions paddle/phi/capi/include/c_data_type.h
@@ -0,0 +1,56 @@
// Copyright (c) 2022 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

#if !defined(_WIN32) && !defined(__APPLE__)

#include <cstdint>

#include "paddle/phi/backends/device_ext.h"
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef C_Status PD_Status;

typedef C_DataType PD_DataType;

typedef C_DataLayout PD_DataLayout;

typedef struct {
size_t size;
void *data;
} PD_List;

void PD_DeletePointerList(PD_List list);

void PD_DeleteUInt8List(PD_List list);

void PD_DeleteInt64List(PD_List list);

void PD_DeleteInt32List(PD_List list);

void PD_DeleteFloat64List(PD_List list);

void PD_DeleteFloat32List(PD_List list);

#ifdef __cplusplus
} // extern "C"
#endif
#endif
42 changes: 42 additions & 0 deletions paddle/phi/capi/include/c_device_context.h
@@ -0,0 +1,42 @@
// Copyright (c) 2022 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

#if !defined(_WIN32) && !defined(__APPLE__)

#include "paddle/phi/capi/include/c_data_type.h"
#include "paddle/phi/capi/include/c_tensor.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct PD_DeviceContext PD_DeviceContext;

typedef C_Stream PD_Stream;

PD_Stream PD_DeviceContextGetStream(const PD_DeviceContext *ctx,
PD_Status *status);

void *PD_DeviceContextAllocateTensor(const PD_DeviceContext *ctx,
PD_Tensor *tensor,
size_t size,
PD_DataType dtype,
PD_Status *status);

#ifdef __cplusplus
} // extern "C"
#endif
#endif
34 changes: 34 additions & 0 deletions paddle/phi/capi/include/c_int_array.h
@@ -0,0 +1,34 @@
// Copyright (c) 2022 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

#if !defined(_WIN32) && !defined(__APPLE__)

#include "paddle/phi/capi/include/c_data_type.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct PD_IntArray PD_IntArray;

PD_List PD_IntArrayGetDataPointer(PD_IntArray *int_array);

size_t PD_IntArrayGetElementCount(PD_IntArray *int_array);

#ifdef __cplusplus
} // extern "C"
#endif
#endif

0 comments on commit 3e88726

Please sign in to comment.