1
1
import paddle .v2 .framework .core as core
2
+ import paddle .v2 .framework .proto .framework_pb2 as framework_pb2
2
3
from paddle .v2 .framework .framework import OpProtoHolder , Variable , Program , \
3
4
Operator
4
5
from paddle .v2 .framework .initializer import ConstantInitializer , \
5
6
NormalInitializer
6
7
from paddle .v2 .framework .layer_helper import LayerHelper , unique_name
7
8
import re
9
+ import cStringIO
8
10
9
11
__all__ = [
10
12
'fc' , 'data' , 'cross_entropy' , 'conv2d' , 'pool2d' , 'embedding' , 'concat' ,
@@ -240,6 +242,58 @@ def _convert_(name):
240
242
return re .sub ('([a-z0-9])([A-Z])' , r'\1_\2' , s1 ).lower ()
241
243
242
244
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 ('\n Args:\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 ('\n Returns:\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
+
243
297
def _create_op_func_ (op_type ):
244
298
"""
245
299
Create an Operator for a Function.
@@ -298,11 +352,6 @@ def infer_and_check_data_type(op_proto, **kwargs):
298
352
return dtype
299
353
300
354
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
- """
306
355
helper = LayerHelper (op_type , ** kwargs )
307
356
308
357
dtype = infer_and_check_data_type (op_proto , ** kwargs )
@@ -326,6 +375,7 @@ def func(**kwargs):
326
375
327
376
func .__name__ = op_type
328
377
globals ()[op_type ] = func
378
+ func .__doc__ = _generate_doc_string_ (op_proto )
329
379
global __all__
330
380
__all__ .append (op_type )
331
381
0 commit comments