| 468 | |
| 469 | |
| 470 | def _add_feed_fetch_ops( |
| 471 | program, feed, fetch_list, feed_var_name, fetch_var_name, use_fetch_v2=False |
| 472 | ): |
| 473 | tmp_program = program.clone() |
| 474 | |
| 475 | global_block = tmp_program.global_block() |
| 476 | |
| 477 | if feed_var_name in global_block.vars: |
| 478 | feed_var = global_block.var(feed_var_name) |
| 479 | else: |
| 480 | feed_var = global_block.create_var( |
| 481 | name=feed_var_name, |
| 482 | type=core.VarDesc.VarType.FEED_MINIBATCH, |
| 483 | persistable=True, |
| 484 | ) |
| 485 | |
| 486 | if fetch_var_name in global_block.vars: |
| 487 | fetch_var = global_block.var(fetch_var_name) |
| 488 | else: |
| 489 | fetch_var = global_block.create_var( |
| 490 | name=fetch_var_name, |
| 491 | type=core.VarDesc.VarType.FETCH_LIST, |
| 492 | persistable=True, |
| 493 | ) |
| 494 | |
| 495 | # prepend feed operators |
| 496 | if not has_feed_operators(global_block, feed, feed_var_name): |
| 497 | for i, name in enumerate(feed): |
| 498 | if global_block.has_var(name): |
| 499 | out = global_block.var(name) |
| 500 | global_block._prepend_op( |
| 501 | type='feed', |
| 502 | inputs={'X': [feed_var]}, |
| 503 | outputs={'Out': [out]}, |
| 504 | attrs={'col': i}, |
| 505 | ) |
| 506 | else: |
| 507 | warnings.warn( |
| 508 | f"The variable {name} is not found in program. It is not declared or is pruned." |
| 509 | ) |
| 510 | |
| 511 | if use_fetch_v2: |
| 512 | fetch_op = 'fetch_v2' |
| 513 | else: |
| 514 | fetch_op = 'fetch' |
| 515 | |
| 516 | # append fetch_operators |
| 517 | if not has_fetch_operators( |
| 518 | global_block, fetch_list, fetch_var_name, fetch_op |
| 519 | ): |
| 520 | for i, var in enumerate(fetch_list): |
| 521 | assert isinstance(var, (Variable, str)), ( |
| 522 | f"Wrong type for fetch_list[{i}]: {type(var)}" |
| 523 | ) |
| 524 | global_block.append_op( |
| 525 | type=fetch_op, |
| 526 | inputs={'X': [var]}, |
| 527 | outputs={'Out': [fetch_var]}, |