Check whether the block already has fetch operators. Return false if the block does not have any fetch operators. If some fetch operators have been appended to the block, check that the info contained in these fetch operators matches the fetch_targets and fetch_holder_name. Raise ex
(
block, fetch_targets, fetch_holder_name, fetch_op='fetch'
)
| 378 | |
| 379 | |
| 380 | def has_fetch_operators( |
| 381 | block, fetch_targets, fetch_holder_name, fetch_op='fetch' |
| 382 | ): |
| 383 | """Check whether the block already has fetch operators. |
| 384 | |
| 385 | Return false if the block does not have any fetch operators. |
| 386 | If some fetch operators have been appended to the block, check that |
| 387 | the info contained in these fetch operators matches the fetch_targets |
| 388 | and fetch_holder_name. Raise exception when any mismatch is found. |
| 389 | Return true when the block has fetch operators with matching info. |
| 390 | |
| 391 | Args: |
| 392 | block: a block instance (typically global block of a program) |
| 393 | fetch_targets: a dictionary of {fetch_target_name: fetch_target_data} |
| 394 | fetch_holder_name: the name of the variable that holds the data of |
| 395 | all fetch targets. The type of this fetch_holder variable is |
| 396 | FETCH_LIST, which is essentially vector<DenseTensor>. |
| 397 | fetch_op: the operator name of fetch |
| 398 | |
| 399 | Return: |
| 400 | A boolean value that indicates whether a block has fetch operators |
| 401 | that match the info contained in fetch_targets and fetch_holder_name. |
| 402 | """ |
| 403 | |
| 404 | fetch_count = 0 |
| 405 | for op in block.ops: |
| 406 | if op.desc.type() == fetch_op: |
| 407 | fetch_count += 1 |
| 408 | assert op.desc.output('Out')[0] == fetch_holder_name |
| 409 | fetch_target_name = op.desc.input('X')[0] |
| 410 | if fetch_target_name not in [ |
| 411 | var.desc.name() for var in fetch_targets |
| 412 | ]: |
| 413 | raise Exception( |
| 414 | f"'fetch_targets' does not have {fetch_target_name} variable" |
| 415 | ) |
| 416 | idx = op.desc.attr('col') |
| 417 | assert fetch_target_name == fetch_targets[idx].desc.name() |
| 418 | if fetch_count > 0 and fetch_count != len(fetch_targets): |
| 419 | raise Exception( |
| 420 | "Fetch operators in program desc do not match 'fetch_targets'" |
| 421 | ) |
| 422 | return fetch_count > 0 |
| 423 | |
| 424 | |
| 425 | def has_fetch_operations_and_is_startup_program( |
no test coverage detected