chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:40:42 +08:00
commit e25996e7db
15472 changed files with 3536181 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
file(
GLOB TEST_OPS
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
set(CUSTOM_ENVS
PADDLE_SOURCE_DIR=${PADDLE_SOURCE_DIR}
PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR}
CUSTOM_DEVICE_ROOT=${CMAKE_BINARY_DIR}/test)
foreach(TEST_OP ${TEST_OPS})
py_test(${TEST_OP} SRCS ${TEST_OP}.py ENVS ${CUSTOM_ENVS})
endforeach()
+51
View File
@@ -0,0 +1,51 @@
// 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.
#include "paddle/phi/core/kernel_registry.h"
namespace paddle {
namespace custom_kernel {
// Here we use dot <CPU, ANY, INT8> for test
// This test will fail when this kernel is supported in framework
template <typename T, typename Context>
void DotKernel(const Context& dev_ctx,
const phi::DenseTensor& x,
const phi::DenseTensor& y,
phi::DenseTensor* out) {
auto const *x_ptr = x.data<T>(), *x_ptr_ = &x_ptr[0];
auto const *y_ptr = y.data<T>(), *y_ptr_ = &y_ptr[0];
T* z = dev_ctx.template Alloc<T>(out);
// Loop over the total N elements of both operands while sum-reducing every
// B pairs along the way where B is the dimension of the least ordered axis
auto&& d = x.dims();
auto const N = x.numel();
auto const B = d[d.size() - 1];
for (int j = 0; j < N / B; j++) {
T ss = 0;
for (int i = 0; i < B; i++) ss += (*x_ptr_++) * (*y_ptr_++);
z[j] = ss;
}
}
} // namespace custom_kernel
} // namespace paddle
PD_REGISTER_BUILTIN_KERNEL(
dot, CPU, ALL_LAYOUT, paddle::custom_kernel::DotKernel, int8_t) {
kernel->OutputAt(0).SetDataType(phi::DataType::INT8);
}
+49
View File
@@ -0,0 +1,49 @@
// 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.
#include "paddle/phi/core/custom_phi_kernel.h"
namespace paddle {
namespace custom_kernel {
// Here we use dot <CPU, ANY, INT8> for test
// This test will fail when this kernel is supported in framework
template <typename T>
void DotKernel(const phi::Context& dev_ctx,
const phi::DenseTensor& x,
const phi::DenseTensor& y,
phi::DenseTensor* out) {
auto const *x_ptr = x.data<T>(), *x_ptr_ = &x_ptr[0];
auto const *y_ptr = y.data<T>(), *y_ptr_ = &y_ptr[0];
T* z = dev_ctx.template Alloc<T>(out);
// Loop over the total N elements of both operands while sum-reducing every
// B pairs along the way where B is the dimension of the least ordered axis
auto&& d = x.dims();
auto const N = x.numel();
auto const B = d[d.size() - 1];
for (int j = 0; j < N / B; j++) {
T ss = 0;
for (int i = 0; i < B; i++) ss += (*x_ptr_++) * (*y_ptr_++);
z[j] = ss;
}
}
} // namespace custom_kernel
} // namespace paddle
PD_BUILD_PHI_KERNEL(
dot, CPU, ALL_LAYOUT, paddle::custom_kernel::DotKernel, int8_t) {}
@@ -0,0 +1,79 @@
# 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.
import os
from distutils.sysconfig import get_python_lib
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
# refer: https://note.qidong.name/2018/03/setup-warning-strict-prototypes
# Avoid a gcc warning below:
# cc1plus: warning: command line option -Wstrict-prototypes is valid
# for C/ObjC but not for C++
class BuildExt(build_ext):
def build_extensions(self):
if '-Wstrict-prototypes' in self.compiler.compiler_so:
self.compiler.compiler_so.remove('-Wstrict-prototypes')
super().build_extensions()
# cc flags
paddle_extra_compile_args = [
'-std=c++14',
'-shared',
'-fPIC',
'-Wno-parentheses',
'-DPADDLE_WITH_CUSTOM_KERNEL',
]
# include path
site_packages_path = get_python_lib()
paddle_custom_kernel_include = [
os.path.join(site_packages_path, 'paddle', 'include'),
]
# include path third_party
compile_third_party_path = os.path.join(
os.environ['PADDLE_BINARY_DIR'], 'third_party'
)
paddle_custom_kernel_include += [
os.path.join(compile_third_party_path, 'install/gflags/include'), # gflags
os.path.join(compile_third_party_path, 'install/glog/include'), # glog
]
# libs path
paddle_custom_kernel_library_dir = [
os.path.join(site_packages_path, 'paddle', 'base'),
]
# libs
libs = [':libpaddle.so']
custom_kernel_dot_module = Extension(
'custom_kernel_dot',
sources=['custom_kernel_dot_c.cc'],
include_dirs=paddle_custom_kernel_include,
library_dirs=paddle_custom_kernel_library_dir,
libraries=libs,
extra_compile_args=paddle_extra_compile_args,
)
setup(
name='custom_kernel_dot_c',
version='1.0',
description='custom kernel fot compiling',
cmdclass={'build_ext': BuildExt},
ext_modules=[custom_kernel_dot_module],
)
@@ -0,0 +1,80 @@
# 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.
import os
import site
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
# refer: https://note.qidong.name/2018/03/setup-warning-strict-prototypes
# Avoid a gcc warning below:
# cc1plus: warning: command line option -Wstrict-prototypes is valid
# for C/ObjC but not for C++
class BuildExt(build_ext):
def build_extensions(self):
if '-Wstrict-prototypes' in self.compiler.compiler_so:
self.compiler.compiler_so.remove('-Wstrict-prototypes')
super().build_extensions()
# cc flags
paddle_extra_compile_args = [
'-std=c++14',
'-shared',
'-fPIC',
'-Wno-parentheses',
'-DPADDLE_WITH_CUSTOM_KERNEL',
]
# include path
site_packages_path = site.getsitepackages()
paddle_custom_kernel_include = [
os.path.join(path, 'paddle', 'include') for path in site_packages_path
]
# include path third_party
compile_third_party_path = os.path.join(
os.environ['PADDLE_BINARY_DIR'], 'third_party'
)
paddle_custom_kernel_include += [
os.path.join(compile_third_party_path, 'install/gflags/include'), # gflags
os.path.join(compile_third_party_path, 'install/glog/include'), # glog
]
# libs path
paddle_custom_kernel_library_dir = [
os.path.join(path, 'paddle', 'base') for path in site_packages_path
]
# libs
libs = [':libpaddle.so']
custom_kernel_dot_module = Extension(
'custom_kernel_dot',
sources=['custom_kernel_dot.cc'],
include_dirs=paddle_custom_kernel_include,
library_dirs=paddle_custom_kernel_library_dir,
libraries=libs,
extra_compile_args=paddle_extra_compile_args,
)
setup(
name='custom_kernel_dot',
version='1.0',
description='custom kernel fot compiling',
cmdclass={'build_ext': BuildExt},
ext_modules=[custom_kernel_dot_module],
)
@@ -0,0 +1,85 @@
# 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.
import os
import sys
import unittest
import numpy as np
# use dot <CPU, ANY, INT8> as test case.
class TestCustomKernelDot(unittest.TestCase):
def setUp(self):
# compile so and set to current path
cur_dir = os.path.dirname(os.path.abspath(__file__))
# --inplace to place output so file to current dir
cmd = f'cd {cur_dir} && {sys.executable} custom_kernel_dot_setup.py build_ext --inplace'
os.system(cmd)
def test_custom_kernel_dot_run(self):
# test dot run
x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
y_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
result = np.sum(x_data * y_data, axis=1).reshape([2, 1])
import paddle
paddle.set_device('cpu')
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
out = paddle.dot(x, y)
np.testing.assert_array_equal(
out.numpy(),
result,
err_msg=f'custom kernel dot out: {out.numpy()},\n numpy dot out: {result}',
)
class TestCustomKernelDotC(unittest.TestCase):
def setUp(self):
# compile so and set to current path
cur_dir = os.path.dirname(os.path.abspath(__file__))
# --inplace to place output so file to current dir
cmd = f'cd {cur_dir} && {sys.executable} custom_kernel_dot_c_setup.py build_ext --inplace'
os.system(cmd)
def test_custom_kernel_dot_run(self):
# test dot run
x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
y_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
result = np.sum(x_data * y_data, axis=1).reshape([2, 1])
import paddle
paddle.set_device('cpu')
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
out = paddle.dot(x, y)
np.testing.assert_array_equal(
out.numpy(),
result,
err_msg=f'custom kernel dot out: {out.numpy()},\n numpy dot out: {result}',
)
if __name__ == '__main__':
if os.name == 'nt' or sys.platform.startswith('darwin'):
# only support Linux now
sys.exit()
unittest.main()
@@ -0,0 +1,80 @@
# 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.
import os
import site
import sys
import unittest
import numpy as np
class TestCustomKernelLoad(unittest.TestCase):
def setUp(self):
# compile so and set to current path
cur_dir = os.path.dirname(os.path.abspath(__file__))
# --inplace to place output so file to current dir
cmd = f'cd {cur_dir} && {sys.executable} custom_kernel_dot_setup.py build_ext --inplace'
os.system(cmd)
# get paddle lib path and place so
paddle_lib_path = ''
site_dirs = site.getsitepackages()
for site_dir in site_dirs:
lib_dir = os.path.sep.join([site_dir, 'paddle', 'libs'])
if os.path.exists(lib_dir):
paddle_lib_path = lib_dir
break
if paddle_lib_path == '':
if hasattr(site, 'USER_SITE'):
lib_dir = os.path.sep.join([site.USER_SITE, 'paddle', 'libs'])
if os.path.exists(lib_dir):
paddle_lib_path = lib_dir
self.default_path = os.path.sep.join(
[paddle_lib_path, '..', '..', 'paddle_custom_device']
)
# copy so to default path
cmd = f'mkdir -p {self.default_path} && cp ./*.so {self.default_path}'
os.system(cmd) # wait
def test_custom_kernel_dot_load(self):
# test dot load
x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
y_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
result = np.sum(x_data * y_data, axis=1).reshape([2, 1])
import paddle
paddle.set_device('cpu')
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
out = paddle.dot(x, y)
np.testing.assert_array_equal(
out.numpy(),
result,
err_msg=f'custom kernel dot out: {out.numpy()},\n numpy dot out: {result}',
)
def tearDown(self):
cmd = f'rm -rf {self.default_path}'
os.system(cmd)
if __name__ == '__main__':
if os.name == 'nt' or sys.platform.startswith('darwin'):
# only support Linux now
sys.exit()
unittest.main()