Skip to content

Commit

Permalink
[IR] Change IR from Static library to dynamic library (#54729)
Browse files Browse the repository at this point in the history
* new_ir to shared

* refine code

* add ir lib path to env

* refine type

* refine code

* fix bug

* fix bug

* refine code

* refine code

* close win

* refine code

* refine code

* refine code

* add win share

* refine code

* refie code

* refine code

* refine code

* refien code

* fix bug

* fix bug

* fix bug

* solve conflict

* solve conflict

* fix bug

* refine code

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* refine code

* fix interpretercore program bug

* delete unuse code

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix cinn bug

* fix cinn bug

* debug

* fix cinn bug

* delete unused code

* fix cinn bug

* fix cinn bug

* fix  ug

* test win openblas

* test win openblas

* fix win openblas bug

* polish code

* fix win open blas bug

* close win dll

* fix flag bug

* test for windows

* fix compile bug
  • Loading branch information
zhangbo9674 authored Jun 20, 2023
1 parent bce6764 commit 24a3cb5
Show file tree
Hide file tree
Showing 107 changed files with 627 additions and 190 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ option(WITH_CUDNN_FRONTEND
"Compile with CUDNN Frontend API support (experimental)" OFF)
option(WITH_CUDNN_DSO "Compile PaddlePaddle with cuDNN dynamic-link libraries"
OFF)
option(WITH_IR "Compile PaddlePaddle with NEWIR" ON)
option(WITH_SHARED_IR "Compile PaddlePaddle with SHARED LIB of IR" ON)

if(WITH_RECORD_BUILDTIME)
set_property(
Expand Down
6 changes: 2 additions & 4 deletions paddle/fluid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ add_subdirectory(pybind)
add_subdirectory(eager)
add_subdirectory(prim)
add_subdirectory(jit)
if(WITH_IR)
add_subdirectory(ir)
add_subdirectory(ir_adaptor)
endif()
add_subdirectory(ir)
add_subdirectory(ir_adaptor)
# NOTE: please add subdirectory inference at last.
add_subdirectory(inference)
2 changes: 2 additions & 0 deletions paddle/fluid/distributed/fleet_executor/interceptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "paddle/fluid/platform/errors.h"
#include "paddle/fluid/platform/macros.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/value.h"

namespace paddle {
namespace framework {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "paddle/fluid/eager/utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/imperative/tracer.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/fluid/prim/api/all.h"
#include "paddle/fluid/prim/api/composite_backward/composite_backward_api.h"
#include "paddle/fluid/prim/utils/utils.h"
Expand Down
8 changes: 8 additions & 0 deletions paddle/fluid/eager/auto_code_generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ if(WIN32)
list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/phi.dll)
endif()

if(WITH_SHARED_IR)
add_custom_command(
OUTPUT ${eager_generator_path}/ir.dll
COMMAND ${CMAKE_COMMAND} -E copy ${IR_LIB} ${eager_generator_path}
DEPENDS ir)
list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/ir.dll)
endif()

if(${CBLAS_PROVIDER} STREQUAL MKLML)
message("Copied libiomp5md.dll for Eager AutoCodeGen")
add_custom_command(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ class {} : public egr::GradNodeBase {{
#include "paddle/phi/api/all.h"
#include "paddle/fluid/eager/utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/eager/to_static/run_program_op_func.h"
#include "paddle/fluid/eager/api/manual/eager_manual/dygraph_forward_api.h"
using CPUPlace = phi::CPUPlace;
Expand Down
3 changes: 3 additions & 0 deletions paddle/fluid/eager/to_static/run_program_op_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
#include "paddle/fluid/eager/api/utils/global_utils.h"
#include "paddle/fluid/eager/grad_node_info.h"
#include "paddle/fluid/eager/tensor_wrapper.h"
#include "paddle/fluid/framework/new_executor/interpretercore.h"
#include "paddle/fluid/framework/variable_helper.h"
#include "paddle/fluid/operators/run_program_op.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/value.h"

namespace details {
using Tensor = paddle::Tensor;
Expand Down
4 changes: 3 additions & 1 deletion paddle/fluid/framework/executor_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
// limitations under the License.

#include "paddle/fluid/framework/executor_cache.h"

#include "paddle/fluid/framework/new_executor/interpretercore.h"
#include "paddle/fluid/framework/op_info.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/value.h"

namespace paddle {
namespace framework {
Expand Down
3 changes: 2 additions & 1 deletion paddle/fluid/framework/executor_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <utility>
#include <vector>

#include "paddle/fluid/framework/new_executor/interpretercore.h"
#include "paddle/fluid/framework/op_proto_maker.h"
#include "paddle/fluid/framework/parallel_executor.h"
#include "paddle/fluid/framework/program_desc.h"
Expand All @@ -36,6 +35,8 @@ namespace ir {
class Graph;
}

class InterpreterCore;

namespace details {
void AppendSkipDeletionVars(const std::vector<std::string>& append_vars,
std::vector<std::string>* all_vars);
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/new_executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(STANDALONE_EXECUTOR_DEPS
pd_op_to_kernel_pass
phi_kernel_adaptor
program_translator
new_ir)
ir)

cc_library(
standalone_executor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "paddle/fluid/framework/variable_helper.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/ir/core/program.h"

using AtomicVectorSizeT = std::vector<std::atomic<size_t>>;

Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/framework/new_executor/interpretercore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "paddle/fluid/framework/new_executor/new_ir_interpreter.h"
#include "paddle/fluid/framework/new_executor/program_interpreter.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/value.h"

PADDLE_DEFINE_EXPORTED_bool(
new_executor_serial_run,
Expand Down
5 changes: 1 addition & 4 deletions paddle/fluid/framework/new_executor/interpretercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

#include "paddle/fluid/framework/new_executor/interpreter_base_impl.h"

DECLARE_bool(new_executor_use_local_scope);
Expand All @@ -20,10 +21,6 @@ namespace ir {
class Program;
} // namespace ir

namespace ir {
class Program;
} // namespace ir

namespace paddle {
namespace framework {

Expand Down
3 changes: 3 additions & 0 deletions paddle/fluid/framework/paddle2cinn/cinn_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/ir/node.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/new_executor/interpretercore.h"
#include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h"
#include "paddle/fluid/framework/paddle2cinn/cinn_graph_symbolization.h"
#include "paddle/fluid/framework/paddle2cinn/transform_desc.h"
Expand All @@ -47,6 +48,8 @@
#include "paddle/fluid/operators/cinn/cinn_launch_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/string/string_helper.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/value.h"
#include "paddle/phi/core/flags.h"

PHI_DECLARE_bool(enable_pe_launch_cinn);
Expand Down
21 changes: 16 additions & 5 deletions paddle/fluid/inference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ endif()
# fluid_modules exclude API-interface of inference/api and inference/capi_exp
get_property(fluid_modules GLOBAL PROPERTY FLUID_MODULES)
get_property(phi_modules GLOBAL PROPERTY PHI_MODULES)
get_property(ir_targets GLOBAL PROPERTY IR_TARGETS)
set(utils_modules pretty_log string_helper benchmark utf8proc)

add_subdirectory(api)
Expand All @@ -58,15 +59,20 @@ set(KERNEL_LIST

#windows GPU static library over the limit, so not create_static_lib, and cc_library is dummy
if(WIN32 AND WITH_GPU)
cc_library(paddle_inference DEPS ${fluid_modules} new_ir
cc_library(paddle_inference DEPS ${fluid_modules} ${ir_targets}
${STATIC_INFERENCE_API} ${utils_modules})
else()
# message("${fluid_modules}")
# message("${STATIC_INFERENCE_API}")
# message("${utils_modules}")
# message("${phi_modules}")
create_static_lib(paddle_inference ${phi_modules} ${fluid_modules} new_ir
${STATIC_INFERENCE_API} ${utils_modules})
if(WIN32)
create_static_lib(paddle_inference ${phi_modules} ${fluid_modules}
${STATIC_INFERENCE_API} ${utils_modules})
else()
create_static_lib(paddle_inference ${phi_modules} ${fluid_modules}
${ir_targets} ${STATIC_INFERENCE_API} ${utils_modules})
endif()
endif()

if(NOT APPLE)
Expand Down Expand Up @@ -96,8 +102,13 @@ set(SHARED_INFERENCE_SRCS
# shared inference library deps
list(REMOVE_ITEM fluid_modules standalone_executor
interpretercore_garbage_collector)
set(SHARED_INFERENCE_DEPS phi new_ir ${fluid_modules} analysis_predictor
${utils_modules})
if(WIN32)
set(SHARED_INFERENCE_DEPS phi ${fluid_modules} analysis_predictor
${utils_modules})
else()
set(SHARED_INFERENCE_DEPS phi ${fluid_modules} ${ir_targets}
analysis_predictor ${utils_modules})
endif()

if(WITH_CRYPTO)
set(SHARED_INFERENCE_DEPS ${SHARED_INFERENCE_DEPS} paddle_crypto)
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/ir/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ file(GLOB PD_DIALECT_SRCS "*.cc")
cc_library(
pd_dialect
SRCS ${PD_DIALECT_SRCS} ${op_source_file}
DEPS new_ir framework_proto phi phi_utils)
DEPS framework_proto phi phi_utils pd_interface ir)
target_include_directories(pd_dialect PRIVATE ${PD_DIALECT_BINARY_DIR})
17 changes: 17 additions & 0 deletions paddle/fluid/ir/dialect/kernel_attribute.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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/ir/dialect/kernel_attribute.h"

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::KernelAttribute)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ class KernelAttribute : public ir::Attribute {

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::KernelAttribute)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ void PaddleKernelDialect::PrintAttribute(ir::Attribute attr,

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleKernelDialect)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_dialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ class PaddleKernelDialect : public ir::Dialect {

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleKernelDialect)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ void PhiKernelOp::Verify(const std::vector<ir::OpResult> &inputs,

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ class PhiKernelOp : public ir::Op<PhiKernelOp> {

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ const size_t& AllocatedDenseTensorType::offset() const {

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType)
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/kernel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ class AllocatedDenseTensorType : public ir::Type {

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType)
30 changes: 28 additions & 2 deletions paddle/fluid/ir/dialect/op_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@
#include "paddle/phi/core/infermeta_utils.h"
{input}
{declare_type_id}
#endif
"""

GET_OP_LIST_TEMPALTE = """{}
"""

DECLARE_OP_TYPE_ID = """
IR_DECLARE_EXPLICIT_TYPE_ID({op_name})
"""

OP_DECLARE_TEMPLATE = """
class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{
public:
Expand Down Expand Up @@ -98,6 +104,8 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{
#include "paddle/phi/api/lib/utils/allocator.h"
{input}
{define_type_id}
"""

OP_N_ATTRIBUTE_DEFINED_TEMPLATE = """
Expand Down Expand Up @@ -238,6 +246,10 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{
}}
"""

DEFINE_OP_TYPE_ID = """
IR_DEFINE_EXPLICIT_TYPE_ID({op_name})
"""


def to_phi_and_fluid_op_name(op_item):
# Templat: - op : phi_name (fluid_name)
Expand Down Expand Up @@ -1701,14 +1713,21 @@ def OpGenerator(
op_list_str = GET_OP_LIST_TEMPALTE.format(
", ".join(ops_name_with_namespace_list)
) # Add GET_OP_LIST

declare_type_id_str = ""
for op in ops_name_with_namespace_list:
declare_type_id_str += DECLARE_OP_TYPE_ID.format(op_name=op)

head_file_str = ""
head_file_str += "".join(ops_declare_list) # Add op class
for name in reversed(namespaces):
head_file_str = NAMESPACE_GARD_TEMPLATE.format(
namespace=name, input=head_file_str
) # Add namespaces
head_file_str = H_FILE_TEMPLATE.format(
op_declare=op_list_str, input=head_file_str
op_declare=op_list_str,
input=head_file_str,
declare_type_id=declare_type_id_str,
) # Add head

# (5) Generate source file str
Expand All @@ -1717,8 +1736,15 @@ def OpGenerator(
source_file_str = NAMESPACE_GARD_TEMPLATE.format(
namespace=name, input=source_file_str
) # Add namespaces

define_type_id_str = ""
for op in ops_name_with_namespace_list:
define_type_id_str += DEFINE_OP_TYPE_ID.format(op_name=op)

source_file_str = CC_FILE_TEMPLATE.format(
h_file=op_def_h_file[:-4], input=source_file_str
h_file=op_def_h_file[:-4],
input=source_file_str,
define_type_id=define_type_id_str,
) # Add head

# (5) Generate pd_op.h.tmp, pd_op.cc.tmp
Expand Down
6 changes: 6 additions & 0 deletions paddle/fluid/ir/dialect/pd_attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ phi::Scalar ScalarAttribute::data() {

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::IntArrayAttribute)
IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::ScalarAttribute)
IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DataTypeAttribute)
IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PlaceAttribute)
IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DataLayoutAttribute)
6 changes: 6 additions & 0 deletions paddle/fluid/ir/dialect/pd_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ class DataLayoutAttribute : public ir::Attribute {

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::IntArrayAttribute)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::ScalarAttribute)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DataTypeAttribute)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PlaceAttribute)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DataLayoutAttribute)
Loading

0 comments on commit 24a3cb5

Please sign in to comment.