diff --git a/paddle/phi/infermeta/spmd_rules/numel.cc b/paddle/phi/infermeta/spmd_rules/numel.cc index d4a441426296e..76c41ba881ae4 100644 --- a/paddle/phi/infermeta/spmd_rules/numel.cc +++ b/paddle/phi/infermeta/spmd_rules/numel.cc @@ -35,7 +35,7 @@ SpmdInfo NumelInferSpmd(const DistMetaTensor& x) { "dims_mapping size [%d] are not matched.", x_ndim, x_dims_mapping.size())); - TensorDistAttr out_dist_attr; + TensorDistAttr out_dist_attr = CopyTensorDistAttrForOutput(x_dist_attr_src); out_dist_attr.set_dims_mapping({}); std::vector partial_on_dims; const auto& dim_mapping = x_dims_mapping; diff --git a/python/paddle/base/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py index d35383c056d2d..5102055c7bdb1 100644 --- a/python/paddle/base/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -942,6 +942,11 @@ def __nonzero__(self: Tensor) -> bool: assert ( numel == 1 ), "When Variable is used as the condition of if/while , Variable can only contain one element." + # resolve the error issue in scenario of pipeline parallel + # where some devices do not have this data, return True or False does not affect + # the execution result in those devices, so currently we return False + if self.is_dist() and not self._is_initialized(): + return False assert self._is_initialized(), "tensor not initialized" return bool(np.array(self) > 0)