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]): Variables needed by inference. fetch_vars(Tensor | list[Tensor]): Variables returned by inference.
(
program: Program,
feed_vars: Tensor | list[Tensor],
fetch_vars: Tensor | list[Tensor],
**kwargs: Unpack[_NormalizeProgramKwargs],
)
| 182 | |
| 183 | |
| 184 | def normalize_program( |
| 185 | program: Program, |
| 186 | feed_vars: Tensor | list[Tensor], |
| 187 | fetch_vars: Tensor | list[Tensor], |
| 188 | **kwargs: Unpack[_NormalizeProgramKwargs], |
| 189 | ) -> Program: |
| 190 | """ |
| 191 | |
| 192 | Normalize/Optimize a program according to feed_vars and fetch_vars. |
| 193 | |
| 194 | Args: |
| 195 | program(Program): Specify a program you want to optimize. |
| 196 | feed_vars(Tensor | list[Tensor]): Variables needed by inference. |
| 197 | fetch_vars(Tensor | list[Tensor]): Variables returned by inference. |
| 198 | kwargs: Supported keys including ``skip_prune_program``. |
| 199 | - skip_prune_program(bool): whether to skip pruning program. Defaults to False. |
| 200 | |
| 201 | Returns: |
| 202 | Program: Normalized/Optimized program. |
| 203 | |
| 204 | Examples: |
| 205 | .. code-block:: pycon |
| 206 | |
| 207 | >>> import paddle |
| 208 | |
| 209 | >>> paddle.enable_static() |
| 210 | |
| 211 | >>> path_prefix = "./infer_model" |
| 212 | |
| 213 | # User defined network, here a softmax regression example |
| 214 | >>> image = paddle.static.data(name='img', shape=[None, 28, 28], dtype='float32') |
| 215 | >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') |
| 216 | >>> predict = paddle.static.nn.fc(image, 10, activation='softmax') |
| 217 | |
| 218 | >>> loss = paddle.nn.functional.cross_entropy(predict, label) |
| 219 | |
| 220 | >>> exe = paddle.static.Executor(paddle.CPUPlace()) |
| 221 | >>> exe.run(paddle.static.default_startup_program()) |
| 222 | |
| 223 | # normalize main program. |
| 224 | >>> program = paddle.static.default_main_program() |
| 225 | >>> normalized_program = paddle.static.normalize_program(program, [image], [predict]) |
| 226 | |
| 227 | """ |
| 228 | if in_pir_mode(): |
| 229 | return normalize_pir_program(program, feed_vars, fetch_vars, **kwargs) |
| 230 | if not isinstance(program, Program): |
| 231 | raise TypeError( |
| 232 | f"program type must be `base.Program`, but received `{type(program)}`" |
| 233 | ) |
| 234 | if not isinstance(feed_vars, list): |
| 235 | feed_vars = [feed_vars] |
| 236 | if not all(isinstance(v, Variable) for v in feed_vars): |
| 237 | raise TypeError( |
| 238 | "feed_vars type must be a Variable or a list of Variable." |
| 239 | ) |
| 240 | if not isinstance(fetch_vars, list): |
| 241 | fetch_vars = [fetch_vars] |
no test coverage detected