Skip to content

Commit 93c6e52

Browse files
authored
Automatically generated doc string for generated layers (#5585)
1 parent e602c70 commit 93c6e52

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

python/paddle/v2/framework/layers.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import paddle.v2.framework.core as core
2+
import paddle.v2.framework.proto.framework_pb2 as framework_pb2
23
from paddle.v2.framework.framework import OpProtoHolder, Variable, Program, \
34
Operator
45
from paddle.v2.framework.initializer import ConstantInitializer, \
56
NormalInitializer
67
from paddle.v2.framework.layer_helper import LayerHelper, unique_name
78
import re
9+
import cStringIO
810

911
__all__ = [
1012
'fc', 'data', 'cross_entropy', 'conv2d', 'pool2d', 'embedding', 'concat',
@@ -240,6 +242,58 @@ def _convert_(name):
240242
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
241243

242244

245+
def _generate_doc_string_(op_proto):
246+
"""
247+
Generate docstring by OpProto
248+
249+
Args:
250+
op_proto (framework_pb2.OpProto): a protobuf message typed OpProto
251+
252+
Returns:
253+
str: the document string
254+
"""
255+
256+
def _type_to_str_(tp):
257+
return framework_pb2.AttrType.Name(tp)
258+
259+
if not isinstance(op_proto, framework_pb2.OpProto):
260+
raise TypeError("OpProto should be `framework_pb2.OpProto`")
261+
262+
buf = cStringIO.StringIO()
263+
buf.write(op_proto.comment)
264+
buf.write('\nArgs:\n')
265+
for each_input in op_proto.inputs:
266+
line_begin = ' {0}: '.format(_convert_(each_input.name))
267+
buf.write(line_begin)
268+
buf.write(each_input.comment)
269+
buf.write('\n')
270+
buf.write(' ' * len(line_begin))
271+
buf.write('Duplicable: ')
272+
buf.write(str(each_input.duplicable))
273+
buf.write(' Optional: ')
274+
buf.write(str(each_input.dispensable))
275+
buf.write('\n')
276+
277+
for each_attr in op_proto.attrs:
278+
buf.write(' ')
279+
buf.write(each_attr.name)
280+
buf.write(' (')
281+
buf.write(_type_to_str_(each_attr.type))
282+
buf.write('): ')
283+
buf.write(each_attr.comment)
284+
buf.write('\n')
285+
286+
if len(op_proto.outputs) != 0:
287+
buf.write('\nReturns:\n')
288+
buf.write(' ')
289+
for each_opt in op_proto.outputs:
290+
if not each_opt.intermediate:
291+
break
292+
buf.write(each_opt.comment)
293+
294+
return buf.getvalue()
295+
296+
243297
def _create_op_func_(op_type):
244298
"""
245299
Create an Operator for a Function.
@@ -298,11 +352,6 @@ def infer_and_check_data_type(op_proto, **kwargs):
298352
return dtype
299353

300354
def func(**kwargs):
301-
"""
302-
This function implements the function for the operator. This process
303-
involves doing the sanity check (using the function above), reading
304-
inputs from protobuf and applying the activations on top.
305-
"""
306355
helper = LayerHelper(op_type, **kwargs)
307356

308357
dtype = infer_and_check_data_type(op_proto, **kwargs)
@@ -326,6 +375,7 @@ def func(**kwargs):
326375

327376
func.__name__ = op_type
328377
globals()[op_type] = func
378+
func.__doc__ = _generate_doc_string_(op_proto)
329379
global __all__
330380
__all__.append(op_type)
331381

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
import paddle.v2.framework.layers as layers
3+
4+
5+
class TestDocString(unittest.TestCase):
6+
def test_layer_doc_string(self):
7+
print layers.dropout.__doc__
8+
9+
10+
if __name__ == '__main__':
11+
unittest.main()

0 commit comments

Comments
 (0)