Skip to content

Commit 22662ae

Browse files
committed
Move paddle.v2.fluid.registery to layers
* registery is only used by layers * Rename it to layer_function_generator
1 parent c73f00f commit 22662ae

File tree

5 files changed

+43
-41
lines changed

5 files changed

+43
-41
lines changed

python/paddle/v2/fluid/layers/control_flow.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
#See the License for the specific language governing permissions and
1313
#limitations under the License.
14-
from ..layer_helper import LayerHelper, unique_name
15-
from ..framework import Program, Variable, Operator
16-
from .. import core
17-
from tensor import assign, fill_constant
1814
import contextlib
19-
from ..registry import autodoc
15+
16+
from layer_function_generator import autodoc
17+
from tensor import assign, fill_constant
18+
from .. import core
19+
from ..framework import Program, Variable, Operator
20+
from ..layer_helper import LayerHelper, unique_name
2021

2122
__all__ = [
2223
'split_lod_tensor', 'merge_lod_tensor', 'BlockGuard',
@@ -1457,7 +1458,7 @@ def _assert_in_rnn_block_(self, method):
14571458
method))
14581459

14591460

1460-
@autodoc
1461+
@autodoc()
14611462
def reorder_lod_tensor_by_rank(x, rank_table):
14621463
helper = LayerHelper('reorder_lod_tensor_by_rank', **locals())
14631464
helper.is_instance('x', Variable)

python/paddle/v2/fluid/layers/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
All util layers.
1616
"""
1717

18-
from ..layer_helper import LayerHelper
18+
from layer_function_generator import autodoc
1919
from ..framework import unique_name
20-
from ..registry import autodoc
20+
from ..layer_helper import LayerHelper
2121

2222
__all__ = ['get_places']
2323

2424

25-
@autodoc
25+
@autodoc()
2626
def get_places(device_count=None, device_type=None):
2727
helper = LayerHelper('get_places', **locals())
2828
out_places = helper.create_variable(name=unique_name(helper.name + ".out"))

python/paddle/v2/fluid/registry.py renamed to python/paddle/v2/fluid/layers/layer_function_generator.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
#See the License for the specific language governing permissions and
1313
#limitations under the License.
14-
import re
1514
import cStringIO
16-
import warnings
1715
import functools
18-
import inspect
16+
import re
17+
import warnings
1918

20-
import proto.framework_pb2 as framework_pb2
21-
from framework import OpProtoHolder, Variable, Program, Operator
22-
from paddle.v2.fluid.layer_helper import LayerHelper, unique_name
19+
from .. import proto
20+
21+
framework_pb2 = proto.framework_pb2
22+
23+
from ..framework import OpProtoHolder, Variable
24+
from ..layer_helper import LayerHelper
2325

2426
__all__ = [
2527
'deprecated',
26-
'register_layer',
28+
'generate_layer_fn',
2729
'autodoc',
2830
]
2931

@@ -96,7 +98,7 @@ def _type_to_str_(tp):
9698
return buf.getvalue()
9799

98100

99-
def register_layer(op_type):
101+
def generate_layer_fn(op_type):
100102
"""Register the Python layer for an Operator.
101103
102104
Args:
@@ -202,7 +204,10 @@ def func_wrapper(*args, **kwargs):
202204
return func_wrapper
203205

204206

205-
def autodoc(func):
206-
func.__doc__ = _generate_doc_string_(OpProtoHolder.instance().get_op_proto(
207-
func.__name__))
208-
return func
207+
def autodoc(comment=""):
208+
def __impl__(func):
209+
func.__doc__ = _generate_doc_string_(OpProtoHolder.instance(
210+
).get_op_proto(func.__name__)) + comment
211+
return func
212+
213+
return __impl__

python/paddle/v2/fluid/layers/ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
#See the License for the specific language governing permissions and
1313
#limitations under the License.
14-
from ..registry import register_layer
14+
from layer_function_generator import generate_layer_fn
1515

1616
__activations__ = [
1717
'sigmoid',
@@ -62,4 +62,4 @@
6262
] + __activations__
6363

6464
for _OP in set(__all__):
65-
globals()[_OP] = register_layer(_OP)
65+
globals()[_OP] = generate_layer_fn(_OP)
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
22
#
3-
#Licensed under the Apache License, Version 2.0 (the "License");
4-
#you may not use this file except in compliance with the License.
5-
#You may obtain a copy of the License at
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
66
#
77
# http://www.apache.org/licenses/LICENSE-2.0
88
#
9-
#Unless required by applicable law or agreed to in writing, software
10-
#distributed under the License is distributed on an "AS IS" BASIS,
11-
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
#See the License for the specific language governing permissions and
13-
#limitations under the License.
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
1414
import unittest
15-
import warnings
1615

1716
import paddle.v2.fluid as fluid
18-
import paddle.v2.fluid.framework as framework
19-
import paddle.v2.fluid.layers as layers
20-
import paddle.v2.fluid.registry as registry
17+
import numpy as np
18+
import decorators
2119

2220

2321
class TestRegistry(unittest.TestCase):
22+
@decorators.prog_scope()
2423
def test_registry_layer(self):
25-
self.layer_type = "mean"
26-
program = framework.Program()
27-
2824
x = fluid.layers.data(name='X', shape=[10, 10], dtype='float32')
29-
output = layers.mean(x)
25+
output = fluid.layers.mean(x=x)
26+
3027
place = fluid.CPUPlace()
3128
exe = fluid.Executor(place)
32-
3329
X = np.random.random((10, 10)).astype("float32")
34-
mean_out = exe.run(program, feed={"X": X}, fetch_list=[output])
30+
mean_out = exe.run(feed={"X": X}, fetch_list=[output])
3531
self.assertAlmostEqual(np.mean(X), mean_out)

0 commit comments

Comments
 (0)