MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / name_scope

Function name_scope

python/paddle/base/framework.py:1251–1312  ·  view source on GitHub ↗

Generate hierarchical name prefix for the operators in Static Graph. Note: This should only used for debugging and visualization purpose. Don't use it for serious analysis such as graph/program transformations. Don't use it in dygraph, since it will cause memory le

(prefix: str | None = None)

Source from the content-addressed store, hash-verified

1249
1250@signature_safe_contextmanager
1251def name_scope(prefix: str | None = None) -> Generator[None, None, None]:
1252 """
1253
1254 Generate hierarchical name prefix for the operators in Static Graph.
1255
1256 Note:
1257 This should only used for debugging and visualization purpose.
1258 Don't use it for serious analysis such as graph/program transformations.
1259 Don't use it in dygraph, since it will cause memory leak.
1260
1261 Args:
1262 prefix(str, optional): prefix. Default is none.
1263
1264 Examples:
1265
1266 .. code-block:: pycon
1267
1268 >>> import paddle
1269 >>> paddle.enable_static()
1270 >>> with paddle.pir_utils.OldIrGuard():
1271 ... with paddle.static.name_scope("s1"):
1272 ... a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
1273 ... b = a + paddle.to_tensor(1)
1274 ... with paddle.static.name_scope("s2"):
1275 ... c = b * paddle.to_tensor(1)
1276 ... with paddle.static.name_scope("s3"):
1277 ... d = c / paddle.to_tensor(1)
1278 ... with paddle.static.name_scope("s1"):
1279 ... f = paddle.tensor.pow(d, paddle.to_tensor(2.0))
1280 ... with paddle.static.name_scope("s4"):
1281 ... g = f - paddle.to_tensor(1)
1282 ...
1283 ... # Op are created in the default main program.
1284 ... for op in paddle.static.default_main_program().block(0).ops:
1285 ... # elementwise_add is created in /s1/
1286 ... if op.type == 'elementwise_add':
1287 ... assert op.desc.attr("op_namescope") == '/s1/'
1288 ... # elementwise_mul is created in '/s1/s2'
1289 ... elif op.type == 'elementwise_mul':
1290 ... assert op.desc.attr("op_namescope") == '/s1/s2/'
1291 ... # elementwise_div is created in '/s1/s3'
1292 ... elif op.type == 'elementwise_div':
1293 ... assert op.desc.attr("op_namescope") == '/s1/s3/'
1294 ... # elementwise_sum is created in '/s4'
1295 ... elif op.type == 'elementwise_sub':
1296 ... assert op.desc.attr("op_namescope") == '/s4/'
1297 ... # pow is created in /s1_1/
1298 ... elif op.type == 'pow':
1299 ... assert op.desc.attr("op_namescope") == '/s1_1/'
1300 """
1301 # TODO(panyx0718): Only [0-9a-z].
1302 # in dygraph we don't need namescope since it will cause mem leak
1303 if in_dygraph_mode():
1304 yield
1305 else:
1306 assert prefix, "namescope prefix can not be empty."
1307 global _name_scope
1308 _name_scope = _name_scope.child(prefix)

Callers 5

__init__Method · 0.90
updateMethod · 0.90
apply_gradientsMethod · 0.90
_finish_updateMethod · 0.85

Calls 3

in_dygraph_modeFunction · 0.85
childMethod · 0.45
parentMethod · 0.45

Tested by

no test coverage detected