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

Function normalize_pir_program

python/paddle/static/pir_io.py:231–330  ·  view source on GitHub ↗

Normalize/Optimize a program according to feed_vars and fetch_vars. Args: program(Program): Specify a program you want to optimize. feed_vars(Tensor | list[Tensor]): Values needed by inference. fetch_vars(Tensor | list[Tensor]): Values returned by inference.

(program, feed_vars, fetch_vars, **kwargs)

Source from the content-addressed store, hash-verified

229
230
231def normalize_pir_program(program, feed_vars, fetch_vars, **kwargs):
232 """
233
234 Normalize/Optimize a program according to feed_vars and fetch_vars.
235
236 Args:
237 program(Program): Specify a program you want to optimize.
238 feed_vars(Tensor | list[Tensor]): Values needed by inference.
239 fetch_vars(Tensor | list[Tensor]): Values returned by inference.
240 kwargs: Supported keys including ``skip_prune_program``.
241 - skip_prune_program(bool): whether to skip pruning program. Defaults to False.
242
243 Returns:
244 Program: Normalized/Optimized program.
245
246 Examples:
247 .. code-block:: pycon
248
249 >>> import paddle
250
251 >>> paddle.enable_static()
252
253 >>> path_prefix = "./infer_model"
254
255 # User defined network, here a softmax regression example
256 >>> image = paddle.static.data(name='img', shape=[None, 28, 28], dtype='float32')
257 >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
258 >>> predict = paddle.static.nn.fc(image, 10, activation='softmax')
259
260 >>> loss = paddle.nn.functional.cross_entropy(predict, label)
261
262 >>> exe = paddle.static.Executor(paddle.CPUPlace())
263 >>> exe.run(paddle.static.default_startup_program())
264
265 # normalize main program.
266 >>> program = paddle.static.default_main_program()
267 >>> normalized_program = paddle.static.normalize_program(program, [image], [predict])
268
269 """
270 if not isinstance(program, paddle.static.Program):
271 raise TypeError(
272 f"program type must be `paddle.static.Program`, but received `{type(program)}`"
273 )
274 if not isinstance(feed_vars, list):
275 feed_vars = [feed_vars]
276 if not all(isinstance(v, pir.Value) for v in feed_vars):
277 raise TypeError("feed_vars type must be a Value or a list of Value.")
278 if not isinstance(fetch_vars, list):
279 fetch_vars = [fetch_vars]
280 if not all(isinstance(v, pir.Value) for v in fetch_vars):
281 raise TypeError("fetch_vars type must be a Value or a list of Value.")
282
283 if len(program.global_block().ops) == 0:
284 raise ValueError(
285 "program must not be empty. at least one operator is required!"
286 )
287
288 # remind users to set auc_states to 0 if auc op were found.

Callers 2

normalize_programFunction · 0.85
save_inference_model_pirFunction · 0.85

Calls 15

TypeErrorClass · 0.85
ValueErrorClass · 0.85
pir_prune_with_inputFunction · 0.85
_inference_optimizeFunction · 0.85
strFunction · 0.85
append_pir_feed_opsFunction · 0.85
append_pir_fetch_opsFunction · 0.85
global_blockMethod · 0.80
remove_opMethod · 0.80
has_attrMethod · 0.80
typeFunction · 0.50
allFunction · 0.50

Tested by

no test coverage detected