Skip to content

Commit

Permalink
Merge pull request #376 from ZHUI/fix_math_api
Browse files Browse the repository at this point in the history
[BugFix] fix in_dygraph_mode api changes
  • Loading branch information
Yelrose authored Mar 28, 2022
2 parents 229873d + ed83ed5 commit 5cc4d81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
19 changes: 12 additions & 7 deletions pgl/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
]

import paddle
from paddle.fluid.framework import core, in_dygraph_mode
from paddle.fluid.layer_helper import LayerHelper, in_dygraph_mode
from paddle.fluid.framework import core
from paddle.fluid.layer_helper import LayerHelper
try:
from paddle.fluid.layer_helper import in_dygraph_mode as non_static_mode
except ImportError:
from paddle.fluid.layer_helper import _non_static_mode as non_static_mode

from paddle.fluid.data_feeder import check_variable_and_dtype
from pgl.utils.op import get_index_from_counts

Expand All @@ -34,7 +39,7 @@ def segment_pool(data, segment_ids, pool_type, name=None):
Segment Operator.
"""
pool_type = pool_type.upper()
if in_dygraph_mode():
if non_static_mode():
out, tmp = core.ops.segment_pool(data, segment_ids, 'pooltype',
pool_type)
return out
Expand Down Expand Up @@ -89,7 +94,7 @@ def segment_sum(data, segment_ids, name=None):
if paddle.__version__ >= '2.2.0':
return paddle.incubate.segment_sum(data, segment_ids, name)

if in_dygraph_mode():
if non_static_mode():
out, tmp = core.ops.segment_pool(data, segment_ids, 'pooltype', "SUM")
return out

Expand Down Expand Up @@ -144,7 +149,7 @@ def segment_mean(data, segment_ids, name=None):
if paddle.__version__ >= '2.2.0':
return paddle.incubate.segment_mean(data, segment_ids, name)

if in_dygraph_mode():
if non_static_mode():
out, tmp = core.ops.segment_pool(data, segment_ids, 'pooltype', "MEAN")
return out

Expand Down Expand Up @@ -197,7 +202,7 @@ def segment_min(data, segment_ids, name=None):
if paddle.__version__ >= '2.2.0':
return paddle.incubate.segment_min(data, segment_ids, name)

if in_dygraph_mode():
if non_static_mode():
out, tmp = core.ops.segment_pool(data, segment_ids, 'pooltype', "MIN")
return out

Expand Down Expand Up @@ -251,7 +256,7 @@ def segment_max(data, segment_ids, name=None):
if paddle.__version__ >= '2.2.0':
return paddle.incubate.segment_max(data, segment_ids, name)

if in_dygraph_mode():
if non_static_mode():
out, tmp = core.ops.segment_pool(data, segment_ids, 'pooltype', "MAX")
return out

Expand Down
13 changes: 9 additions & 4 deletions pgl/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
from paddle.fluid.layers import core
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype
from paddle.fluid.framework import Variable, in_dygraph_mode, convert_np_dtype_to_dtype_
import paddle.fluid.layers as L
from paddle.fluid.framework import Variable, convert_np_dtype_to_dtype_

try:
from paddle.fluid.layer_helper import in_dygraph_mode as non_static_mode
except ImportError:
from paddle.fluid.layer_helper import _non_static_mode as non_static_mode

import paddle.fluid.layers as L
from pgl.utils import op


Expand Down Expand Up @@ -99,7 +104,7 @@ def scatter(x, index, updates, overwrite=True, name=None):
# [2., 2.],
# [1., 1.]]
"""
if in_dygraph_mode():
if non_static_mode():
return core.ops.scatter(x, index, updates, 'overwrite', overwrite)

check_variable_and_dtype(
Expand Down Expand Up @@ -160,7 +165,7 @@ def maybe_num_nodes(edges):
def unique_segment(data, dtype="int64"):
"""Return Segment Id from data
"""
if in_dygraph_mode():
if non_static_mode():
attr_dtype = convert_np_dtype_to_dtype_(dtype)
unique, index, _ = core.ops.unique_with_counts(data, "dtype",
attr_dtype)
Expand Down

0 comments on commit 5cc4d81

Please sign in to comment.