Skip to content
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+CINN]Pybind SubGraphChecker and Add Python UT #59924

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ build_doc/
*.user
*.tmp
*.pyc
*.log

.vscode
.idea
Expand Down Expand Up @@ -96,6 +97,7 @@ paddle/phi/api/profiler/__init__.py
python/paddle/incubate/fleet/parameter_server/pslib/ps_pb2.py
paddle/phi/kernels/fusion/cutlass/conv2d/generated/*
paddle/phi/kernels/fusion/cutlass/conv2d/generated_tmp/*
paddle/phi/ops/compat/generated_*
python/paddle/base/incubate/fleet/parameter_server/pslib/ps_pb2.py
paddle/fluid/ir_adaptor/translator/op_compat_info.cc
paddle/phi/kernels/fusion/cutlass/cutlass_kernels/fpA_intB_gemm/autogen/*
Expand Down
9 changes: 7 additions & 2 deletions paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ set(PYBIND_DEPS
auto_parallel)

if(WITH_CINN)
set(PYBIND_DEPS ${PYBIND_DEPS} pir_transforms op_with_group_merge_pass
add_broadcast_to_elementwise_pass pd_to_cinn_pass)
set(PYBIND_DEPS
${PYBIND_DEPS} pir_transforms op_with_group_merge_pass
add_broadcast_to_elementwise_pass pd_to_cinn_pass sub_graph_checker)
endif()

if(WITH_PSCORE)
Expand Down Expand Up @@ -228,6 +229,10 @@ if(WITH_PSCORE)
set(PYBIND_SRCS fleet_py.cc ${PYBIND_SRCS})
endif()

if(WITH_CINN)
set(PYBIND_SRCS ${PYBIND_SRCS} test.cc)
endif()

if(WITH_RPC)
if(WITH_ARM_BRPC)
set(DISTRIBUTE_COMPILE_FLAGS
Expand Down
5 changes: 5 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ limitations under the License. */

#ifdef PADDLE_WITH_CINN
#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h"
#include "paddle/fluid/pybind/test.h"
#endif

#if defined(PADDLE_WITH_RPC)
Expand Down Expand Up @@ -3013,6 +3014,10 @@ All parameter, weight, gradient are variables in Paddle.
GetAllWorkerInfos(&m);
#endif

#if defined(PADDLE_WITH_CINN)
BindTest(&m);
#endif

BindPir(&m);
BindVjp(&m);
BindDecomp(&m);
Expand Down
46 changes: 46 additions & 0 deletions paddle/fluid/pybind/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

#include "paddle/fluid/pybind/test.h"

#include <Python.h>
#include "paddle/fluid/sub_graph/sub_graph_checker.h"

namespace py = pybind11;

namespace paddle::pybind {
using paddle::test::SubGraphChecker;
using pir::Program;

void BindTest(pybind11::module* module) {
auto test_module = module->def_submodule("test");
py::class_<SubGraphChecker, std::shared_ptr<SubGraphChecker>>
subgraph_checker(
*test_module,
"SubGraphChecker",
R"DOC(A class to check result and speed between PHI and CINN)DOC");
subgraph_checker
.def("__init__",
[](SubGraphChecker& self,
std::shared_ptr<Program> orig_program,
std::shared_ptr<Program> prim_program) {
new (&self) SubGraphChecker(orig_program, prim_program);
})
.def("check_result",
[](std::shared_ptr<SubGraphChecker> self) { self->CheckResult(); })
.def("check_speed",
[](std::shared_ptr<SubGraphChecker> self) { self->CheckSpeed(); });
}

} // namespace paddle::pybind
23 changes: 23 additions & 0 deletions paddle/fluid/pybind/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 <pybind11/pybind11.h>

namespace paddle::pybind {

void BindTest(pybind11::module *module);

} // namespace paddle::pybind
17 changes: 9 additions & 8 deletions test/ir/pir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ set(TEST_IR_SYSTEM_CASES
test_override_operator)
list(REMOVE_ITEM TEST_INTERP_CASES ${TEST_IR_SYSTEM_CASES})
list(REMOVE_ITEM TEST_INTERP_CASES test_subgraph_exporter)
py_test_modules(
test_subgraph_exporter
MODULES
test_subgraph_exporter
ENVS
MIN_GRAPH_SIZE=0
FLAGS_enable_pir_in_executor=1
FLAGS_pir_subgraph_saving_dir=${CMAKE_CURRENT_SOURCE_DIR})

foreach(target ${TEST_INTERP_CASES})
py_test_modules(${target} MODULES ${target} ENVS GLOG_v=1
Expand All @@ -36,4 +28,13 @@ endforeach()

set_tests_properties(test_pd_inplace_pass PROPERTIES TIMEOUT 60)

py_test_modules(
test_subgraph_exporter
MODULES
test_subgraph_exporter
ENVS
MIN_GRAPH_SIZE=0
FLAGS_enable_pir_in_executor=1
FLAGS_pir_subgraph_saving_dir=${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(fused_pass)
14 changes: 13 additions & 1 deletion test/ir/pir/cinn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ if(WITH_GPU)
GLOB CINN_PIR_TEST
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"test_*.py")
string(REPLACE ".py" "" CINN_PIR_TEST "${CINN_PIR_TEST}")

list(REMOVE_ITEM CINN_PIR_TEST test_subgraph_checker)

foreach(cinn_pir_test_name ${CINN_PIR_TEST})
string(REGEX REPLACE ".py" "" cinn_pir_test_name ${cinn_pir_test_name})
add_test(
NAME ${cinn_pir_test_name}
COMMAND
Expand All @@ -21,4 +23,14 @@ if(WITH_GPU)
"RUN_TYPE=CINN")
endforeach()

add_test(
NAME test_subgraph_checker
COMMAND
${CMAKE_COMMAND} -E env
PYTHONPATH=${CMAKE_BINARY_DIR}:${CMAKE_BINARY_DIR}/python/:$ENV{PYTHONPATH}
FLAGS_enable_pir_api=1 ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/test_subgraph_checker.py
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_tests_properties(test_subgraph_checker PROPERTIES LABELS "RUN_TYPE=CINN")

endif()
54 changes: 54 additions & 0 deletions test/ir/pir/cinn/test_subgraph_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 unittest

import paddle
from paddle.decomposition import decompose
from paddle.framework import core


class TestChecker(unittest.TestCase):
def setUp(self):
paddle.enable_static()

def create_program(self, enable_prim=False):
if enable_prim:
core._set_prim_forward_enabled(True)
out_name = 'cinn_out_0'
else:
core._set_prim_all_enabled(False)
out_name = 'phi_out_0'

main_program = paddle.static.Program()
with paddle.static.program_guard(main_program):
x = paddle.static.data(shape=[4, 4], name='input_0')
out = paddle.nn.functional.softmax(x)
fetch_out = paddle._pir_ops.fetch(out, out_name, 0)
fetch_out.persistable = True
decompose(main_program, [fetch_out])
return main_program

def test_check(self):
orig_program = self.create_program(enable_prim=False)
prim_program = self.create_program(enable_prim=True)
checker = paddle.base.libpaddle.test.SubGraphChecker(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因其为非用户相关的功能,故暂时没有在paddle/pir/__init__.py 等文件里默认import,保持封闭性

orig_program, prim_program
)
checker.check_result()
checker.check_speed()


if __name__ == "__main__":
unittest.main()