-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[PIR]Support inplace custom op in pir #60529
Merged
YuanRisheng
merged 12 commits into
PaddlePaddle:develop
from
YuanRisheng:pir_custom_op2
Jan 10, 2024
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8875679
support inplace in pir
YuanRisheng 7ddc180
fix inference ut
YuanRisheng 3bda901
fix win bugs
YuanRisheng b888da3
fix win bug
YuanRisheng 76c61a9
fix
YuanRisheng 14055c4
polish code
YuanRisheng b635a8d
polish code
YuanRisheng 84ae78b
print log
YuanRisheng e65bd22
print log
YuanRisheng 0c24827
debug
YuanRisheng 68cfe20
fix win bugs
YuanRisheng 0935d7e
fix windows
YuanRisheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 196 additions & 78 deletions
274
paddle/fluid/framework/new_executor/instruction/custom_kernel_instruction.cc
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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, | ||
// WIdata_tHOUdata_t WARRANdata_tIES OR CONDIdata_tIONS OF ANY KIND, either | ||
// express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "paddle/extension.h" | ||
|
||
#define CHECK_GPU_INPUT(x) PD_CHECK(x.is_gpu(), #x " must be a GPU Tensor.") | ||
|
||
template <typename data_t> | ||
__global__ void relu_cuda_forward_kernel(data_t* x, int64_t num) { | ||
int64_t gid = blockIdx.x * blockDim.x + threadIdx.x; | ||
for (int64_t i = gid; i < num; i += blockDim.x * gridDim.x) { | ||
x[i] = x[i] > static_cast<data_t>(0.) ? x[i] : static_cast<data_t>(0.); | ||
} | ||
} | ||
|
||
void ReluForwardInplace(paddle::Tensor& x) { // NOLINT | ||
CHECK_GPU_INPUT(x); | ||
|
||
PD_CHECK(x.place() == paddle::DefaultGPUPlace()); | ||
|
||
int64_t numel = x.numel(); | ||
int64_t block = 512; | ||
int64_t grid = (numel + block - 1) / block; | ||
PD_DISPATCH_FLOATING_AND_HALF_TYPES( | ||
x.type(), "relu_cuda_forward_kernel", ([&] { | ||
relu_cuda_forward_kernel<data_t> | ||
<<<grid, block, 0, x.stream()>>>(x.data<data_t>(), numel); | ||
})); | ||
} | ||
|
||
PD_BUILD_OP(custom_relu_inplace) | ||
.Inputs({"X"}) | ||
.Outputs({"Out"}) | ||
.SetInplaceMap({{"X", "Out"}}) | ||
.SetKernelFn(PD_KERNEL(ReluForwardInplace)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# 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. | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import numpy as np | ||
from utils import ( | ||
extra_cc_args, | ||
extra_nvcc_args, | ||
paddle_includes, | ||
) | ||
|
||
import paddle | ||
from paddle.inference import Config, create_predictor | ||
from paddle.utils.cpp_extension import get_build_directory, load | ||
from paddle.utils.cpp_extension.extension_utils import run_cmd | ||
|
||
# Because Windows don't use docker, the shared lib already exists in the | ||
# cache dir, it will not be compiled again unless the shared lib is removed. | ||
file = f'{get_build_directory()}\\infer_custom\\infer_custom.pyd' | ||
if os.name == 'nt' and os.path.isfile(file): | ||
cmd = f'del {file}' | ||
run_cmd(cmd, True) | ||
|
||
# Compile and load custom op Just-In-Time. | ||
custom_inplace = load( | ||
name='infer_custom', | ||
sources=['custom_inplace.cu'], | ||
extra_include_paths=paddle_includes, # add for Coverage CI | ||
extra_cxx_cflags=extra_cc_args, # test for cflags | ||
extra_cuda_cflags=extra_nvcc_args, # test for cflags | ||
verbose=True, | ||
) | ||
|
||
|
||
class TestInplaceNet(paddle.nn.Layer): | ||
def __init__(self): | ||
super().__init__() | ||
self.fc = paddle.nn.Linear(4, 4) | ||
|
||
def forward(self, x): | ||
fc_out = self.fc(x) | ||
out = custom_inplace.custom_relu_inplace(fc_out) | ||
mean_out = paddle.mean(out) | ||
return mean_out | ||
|
||
|
||
@unittest.skipIf( | ||
not paddle.is_compiled_with_cuda(), 'should compile with cuda.' | ||
) | ||
class TestPredictorRunWithTensor(unittest.TestCase): | ||
def setUp(self): | ||
self.temp_dir = tempfile.TemporaryDirectory() | ||
net = TestInplaceNet() | ||
model = paddle.jit.to_static( | ||
net, | ||
input_spec=[ | ||
paddle.static.InputSpec( | ||
shape=[None, 4], dtype='float32', name='x' | ||
), | ||
], | ||
) | ||
paddle.jit.save( | ||
model, | ||
os.path.join( | ||
self.temp_dir.name, 'test_predictor_run_model/inference' | ||
), | ||
) | ||
|
||
def tearDown(self): | ||
self.temp_dir.cleanup() | ||
|
||
def enable_pir(self, flag: bool): | ||
paddle.set_flags({'FLAGS_enable_pir_in_executor': flag}) | ||
|
||
def init_predictor(self): | ||
config = Config( | ||
os.path.join( | ||
self.temp_dir.name, | ||
'test_predictor_run_model/inference.pdmodel', | ||
), | ||
os.path.join( | ||
self.temp_dir.name, | ||
'test_predictor_run_model/inference.pdiparams', | ||
), | ||
) | ||
config.enable_use_gpu(256, 0) | ||
config.switch_ir_optim(False) | ||
config.enable_new_executor() | ||
predictor = create_predictor(config) | ||
return predictor | ||
|
||
def get_inputs(self): | ||
x = np.array([[1, 2, 3, 4], [2, 3, 4, 5]]).astype(np.float32) | ||
|
||
x_tensor = paddle.to_tensor(x) | ||
|
||
return [x_tensor] | ||
|
||
def get_outputs(self, predictor): | ||
[x_tensor] = self.get_inputs() | ||
|
||
input_names = predictor.get_input_names() | ||
x_tensor.name = input_names[0] | ||
|
||
# disorder | ||
inputs = [x_tensor] | ||
outputs = predictor.run(inputs) | ||
|
||
return outputs[0] | ||
|
||
def test_output(self): | ||
self.enable_pir(True) | ||
pir_predictor = self.init_predictor() | ||
pir_output = self.get_outputs(pir_predictor) | ||
self.enable_pir(False) | ||
predictor = self.init_predictor() | ||
output = self.get_outputs(predictor) | ||
np.testing.assert_allclose( | ||
output.numpy().flatten(), pir_output.numpy().flatten() | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2024,下同
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,3q