Skip to content

Commit e00aa90

Browse files
authored
[Cherry-pick] Several bugs fix (#44991)
* fix device context init error (#43910) * Fix core so name mismatch error (#43977) * fix core avx soname error * remove print info * add clip_extra (#44008) * fix tensor stream error in custom op (#44500) * fix custom op attr names size error (#44938)
1 parent c5f4a9c commit e00aa90

File tree

7 files changed

+282
-140
lines changed

7 files changed

+282
-140
lines changed

paddle/fluid/platform/device_context.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ limitations under the License. */
5858
#endif
5959

6060
#ifdef PADDLE_WITH_MKLDNN
61-
#include "dnnl.hpp"
61+
#include "dnnl.hpp" // NOLINT
6262
#include "paddle/fluid/framework/data_layout.h"
6363
#endif
6464

@@ -902,6 +902,8 @@ class DeviceContextPool {
902902
return *pool;
903903
}
904904

905+
static bool IsInitialized() { return pool != nullptr; }
906+
905907
static void SetPool(DeviceContextPool* dev_pool) { pool = dev_pool; }
906908

907909
/*! \brief Return handle of single device context. */

paddle/fluid/pybind/eager_functions.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ static std::vector<paddle::any> CastAttrsToTragetType(
305305
attrs_names.size(), src.size()));
306306
for (size_t i = 0; i < src.size(); i++) {
307307
size_t end = attrs_names[i].find(": ");
308-
std::string type_name =
309-
attrs_names[i].substr(end + 2, attrs_names.size() - end - 2);
308+
std::string type_name = attrs_names[i].substr(end + 2);
310309
if (type_name == "int") {
311310
if (src[i].type() == typeid(bool)) {
312311
res.emplace_back(static_cast<int>(paddle::any_cast<bool>(src[i])));

paddle/phi/api/lib/CMakeLists.txt

Lines changed: 220 additions & 94 deletions
Large diffs are not rendered by default.

paddle/phi/api/lib/context_pool.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License. */
1717
#include "paddle/phi/backends/all_context.h"
1818
#include "paddle/phi/core/enforce.h"
1919

20+
#include "paddle/fluid/platform/init.h"
21+
2022
namespace paddle {
2123
namespace experimental {
2224

@@ -28,6 +30,9 @@ DeviceContextPool& DeviceContextPool::Instance() {
2830
const phi::DeviceContext* DeviceContextPool::Get(const Place& place) {
2931
auto it = context_map_.find(place);
3032
if (it == context_map_.end()) {
33+
if (!paddle::platform::DeviceContextPool::IsInitialized()) {
34+
paddle::framework::InitDevices();
35+
}
3136
// only when we need the specific DeviceContext, get and cache it
3237
auto* dev_ctx = paddle::platform::DeviceContextPool::Instance().Get(place);
3338
{

paddle/phi/api/lib/tensor.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ limitations under the License. */
2020

2121
#include "glog/logging.h"
2222

23+
#include "paddle/phi/api/include/context_pool.h"
2324
#include "paddle/phi/api/lib/utils/allocator.h"
25+
#include "paddle/phi/backends/gpu/gpu_context.h"
2426
#include "paddle/phi/backends/gpu/gpu_info.h"
2527
#include "paddle/phi/core/ddim.h"
2628
#include "paddle/phi/core/dense_tensor.h"
@@ -32,8 +34,7 @@ limitations under the License. */
3234
#include "paddle/phi/core/tensor_base.h"
3335
#include "paddle/phi/core/tensor_meta.h"
3436
#include "paddle/phi/core/tensor_utils.h"
35-
36-
#include "paddle/fluid/platform/stream/cuda_stream.h"
37+
// clang-format off
3738

3839
namespace paddle {
3940
namespace experimental {
@@ -305,7 +306,10 @@ void Tensor::set_impl(std::shared_ptr<phi::TensorBase> &&impl) {
305306

306307
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
307308
gpuStream_t Tensor::stream() const {
308-
return platform::stream::get_current_stream(-1)->raw_stream();
309+
int device_id = phi::backends::gpu::GetCurrentDeviceId();
310+
auto* gpu_context = DeviceContextPool::Instance()
311+
.Get<AllocationType::GPU>(GPUPlace(device_id));
312+
return gpu_context->stream();
309313
}
310314
#endif
311315

python/paddle/fluid/dygraph/jit.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def _extract_vars(inputs, result_list, err_tag='inputs'):
6464
_extract_vars(var, result_list, err_tag)
6565
else:
6666
raise TypeError(
67-
"The type of 'each element of {}' in fluid.dygraph.jit.TracedLayer.trace must be fluid.Variable, but received {}.".
68-
format(err_tag, type(inputs)))
67+
"The type of 'each element of {}' in fluid.dygraph.jit.TracedLayer.trace must be fluid.Variable, but received {}."
68+
.format(err_tag, type(inputs)))
6969

7070

7171
def extract_vars(inputs, err_tag='inputs'):
@@ -211,29 +211,28 @@ def decorated(python_func):
211211
_, python_func = unwrap_decorators(python_func)
212212

213213
# Step 2. copy some attributes from original python function.
214-
static_layer = copy_decorator_attrs(
215-
original_func=python_func,
216-
decorated_obj=StaticFunction(
217-
function=python_func,
218-
input_spec=input_spec,
219-
build_strategy=build_strategy))
214+
static_layer = copy_decorator_attrs(original_func=python_func,
215+
decorated_obj=StaticFunction(
216+
function=python_func,
217+
input_spec=input_spec,
218+
build_strategy=build_strategy))
220219

221220
return static_layer
222221

223222
build_strategy = build_strategy or BuildStrategy()
224223
if not isinstance(build_strategy, BuildStrategy):
225224
raise TypeError(
226-
"Required type(build_strategy) shall be `paddle.static.BuildStrategy`, but received {}".
227-
format(type(build_strategy).__name__))
225+
"Required type(build_strategy) shall be `paddle.static.BuildStrategy`, but received {}"
226+
.format(type(build_strategy).__name__))
228227

229228
# for usage: `declarative(foo, ...)`
230229
if function is not None:
231230
if isinstance(function, Layer):
232231
if isinstance(function.forward, StaticFunction):
233232
class_name = function.__class__.__name__
234233
logging_utils.warn(
235-
"`{}.forward` has already been decorated somewhere. It will be redecorated to replace previous one.".
236-
format(class_name))
234+
"`{}.forward` has already been decorated somewhere. It will be redecorated to replace previous one."
235+
.format(class_name))
237236
function.forward = decorated(function.forward)
238237
return function
239238
else:
@@ -284,6 +283,7 @@ def func(x):
284283

285284

286285
class _SaveLoadConfig(object):
286+
287287
def __init__(self):
288288
self._output_spec = None
289289
self._model_filename = None
@@ -371,7 +371,7 @@ def keep_name_table(self, value):
371371

372372

373373
def _parse_save_configs(configs):
374-
supported_configs = ['output_spec', "with_hook"]
374+
supported_configs = ['output_spec', "with_hook", "clip_extra"]
375375

376376
# input check
377377
for key in configs:
@@ -384,6 +384,7 @@ def _parse_save_configs(configs):
384384
inner_config = _SaveLoadConfig()
385385
inner_config.output_spec = configs.get('output_spec', None)
386386
inner_config.with_hook = configs.get('with_hook', False)
387+
inner_config.clip_extra = configs.get("clip_extra", False)
387388

388389
return inner_config
389390

@@ -622,6 +623,7 @@ def _remove_save_pre_hook(hook):
622623

623624

624625
def _run_save_pre_hooks(func):
626+
625627
def wrapper(layer, path, input_spec=None, **configs):
626628
global _save_pre_hooks
627629
for hook in _save_pre_hooks:
@@ -775,8 +777,8 @@ def fun(inputs):
775777
"The paddle.jit.save doesn't work when setting ProgramTranslator.enable to False."
776778
)
777779

778-
if not (isinstance(layer, Layer) or inspect.isfunction(layer) or isinstance(
779-
layer, StaticFunction)):
780+
if not (isinstance(layer, Layer) or inspect.isfunction(layer)
781+
or isinstance(layer, StaticFunction)):
780782
raise TypeError(
781783
"The input of paddle.jit.save should be 'Layer' or 'Function', but received input type is %s."
782784
% type(layer))
@@ -837,7 +839,7 @@ def fun(inputs):
837839
# parse configs
838840
configs = _parse_save_configs(configs)
839841
# whether outermost layer has pre/post hook, if does, we need also save
840-
# these operators in program.
842+
# these operators in program.
841843
with_hook = configs.with_hook
842844

843845
scope = core.Scope()
@@ -848,7 +850,9 @@ def fun(inputs):
848850
with_hook = True
849851
else:
850852
# layer is function
851-
functions = [layer, ]
853+
functions = [
854+
layer,
855+
]
852856
for attr_func in functions:
853857
if isinstance(layer, Layer):
854858
static_func = getattr(inner_layer, attr_func, None)
@@ -862,8 +866,8 @@ def fun(inputs):
862866
if inner_input_spec:
863867
inner_input_spec = pack_sequence_as(input_spec,
864868
inner_input_spec)
865-
static_forward = declarative(
866-
inner_layer.forward, input_spec=inner_input_spec)
869+
static_forward = declarative(inner_layer.forward,
870+
input_spec=inner_input_spec)
867871
concrete_program = static_forward.concrete_program_specify_input_spec(
868872
with_hook=with_hook)
869873
# the input_spec has been used in declarative, which is equal to
@@ -882,14 +886,14 @@ def fun(inputs):
882886
if inner_input_spec:
883887
inner_input_spec = pack_sequence_as(input_spec,
884888
inner_input_spec)
885-
static_function = declarative(
886-
attr_func, input_spec=inner_input_spec)
889+
static_function = declarative(attr_func,
890+
input_spec=inner_input_spec)
887891
concrete_program = static_function.concrete_program
888892

889893
if static_function._class_instance is None:
890894
warnings.warn(
891-
'`jit.save` will only save the `Program`, not the parameters. If you have to save the parameters, please make sure that {} is a member function of `paddle.nn.Layer` and the saved parameters are in `state_dict`'.
892-
format(layer))
895+
'`jit.save` will only save the `Program`, not the parameters. If you have to save the parameters, please make sure that {} is a member function of `paddle.nn.Layer` and the saved parameters are in `state_dict`'
896+
.format(layer))
893897

894898
dygraph_state_dict = None
895899
if isinstance(inner_layer, Layer):
@@ -922,8 +926,8 @@ def fun(inputs):
922926
param_or_buffer_tensor = scope.var(
923927
param_or_buffer.name).get_tensor()
924928
#src_tensor = param_or_buffer.value().get_tensor()
925-
src_tensor = state_var_dict[param_or_buffer.name].value(
926-
).get_tensor()
929+
src_tensor = state_var_dict[
930+
param_or_buffer.name].value().get_tensor()
927931
param_or_buffer_tensor._share_data_with(src_tensor)
928932
# record var info
929933
if param_or_buffer.name not in extra_var_info:
@@ -986,7 +990,7 @@ def fun(inputs):
986990
params_filename=params_filename,
987991
export_for_deployment=configs._export_for_deployment,
988992
program_only=configs._program_only,
989-
clip_extra=False)
993+
clip_extra=configs.clip_extra)
990994

991995
# NOTE(chenweihang): [ Save extra variable info ]
992996
# save_inference_model will lose some important variable information, including:
@@ -1534,14 +1538,16 @@ def forward(self, input):
15341538
"fluid.dygraph.jit.TracedLayer.save_inference_model")
15351539
if isinstance(feed, list):
15361540
for f in feed:
1537-
check_type(f, "each element of feed", int,
1538-
"fluid.dygraph.jit.TracedLayer.save_inference_model")
1541+
check_type(
1542+
f, "each element of feed", int,
1543+
"fluid.dygraph.jit.TracedLayer.save_inference_model")
15391544
check_type(fetch, "fetch", (type(None), list),
15401545
"fluid.dygraph.jit.TracedLayer.save_inference_model")
15411546
if isinstance(fetch, list):
15421547
for f in fetch:
1543-
check_type(f, "each element of fetch", int,
1544-
"fluid.dygraph.jit.TracedLayer.save_inference_model")
1548+
check_type(
1549+
f, "each element of fetch", int,
1550+
"fluid.dygraph.jit.TracedLayer.save_inference_model")
15451551
clip_extra = kwargs.get('clip_extra', False)
15461552
# path check
15471553
file_prefix = os.path.basename(path)
@@ -1575,12 +1581,11 @@ def get_feed_fetch(all_vars, partial_vars):
15751581
model_filename = file_prefix + INFER_MODEL_SUFFIX
15761582
params_filename = file_prefix + INFER_PARAMS_SUFFIX
15771583

1578-
save_inference_model(
1579-
dirname=dirname,
1580-
feeded_var_names=feeded_var_names,
1581-
target_vars=target_vars,
1582-
executor=self._exe,
1583-
main_program=self._program.clone(),
1584-
model_filename=model_filename,
1585-
params_filename=params_filename,
1586-
clip_extra=clip_extra)
1584+
save_inference_model(dirname=dirname,
1585+
feeded_var_names=feeded_var_names,
1586+
target_vars=target_vars,
1587+
executor=self._exe,
1588+
main_program=self._program.clone(),
1589+
model_filename=model_filename,
1590+
params_filename=params_filename,
1591+
clip_extra=clip_extra)

python/setup.py.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ if '${CMAKE_BUILD_TYPE}' == 'Release':
570570
commands = ["install_name_tool -id '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so']
571571
commands.append("install_name_tool -add_rpath '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so')
572572
else:
573-
commands = ["patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so']
573+
commands = ["patchelf --set-soname '${FLUID_CORE_NAME}.so' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so']
574+
commands.append("patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so')
574575
# The sw_64 not suppot patchelf, so we just disable that.
575576
if platform.machine() != 'sw_64' and platform.machine() != 'mips64':
576577
for command in commands:

0 commit comments

Comments
 (0)