A Task is composed of an execution step and zero or more outputs. Tasks are executed in the context of a TaskGroup, which, in turn, can be run by a Session. Task outputs are fetched by the session at the end of the run. The recommended way of creating a task is by using `net_b
| 437 | |
| 438 | |
| 439 | class Task(context.Managed): |
| 440 | """ |
| 441 | A Task is composed of an execution step and zero or more outputs. |
| 442 | Tasks are executed in the context of a TaskGroup, which, in turn, can |
| 443 | be run by a Session. |
| 444 | |
| 445 | Task outputs are fetched by the session at the end of the run. |
| 446 | |
| 447 | The recommended way of creating a task is by using `net_builder.ops`. |
| 448 | Example: |
| 449 | |
| 450 | from net_builder import ops |
| 451 | with Node('trainer'), Task(name='my_task', num_instances=2): |
| 452 | with ops.task_init(): |
| 453 | globl = ops.Const(0) |
| 454 | with ops.task_instance_init(): |
| 455 | local = ops.Const(0) |
| 456 | with ops.loop(100): |
| 457 | ops.Copy(globl, local) |
| 458 | with ops.task_instance_exit(): |
| 459 | ops.Add([globl, local], [globl]) |
| 460 | with ops.task_exit(): |
| 461 | ops.Mul([globl, globl], [globl]) |
| 462 | |
| 463 | The task above will create 2 instances that will run in parallel. |
| 464 | Each instance will copy `local` to `globl` 100 times, Then Add `local` |
| 465 | to `globl` once. The `Mul` will only execute once, after all the instances |
| 466 | of the task have finished. |
| 467 | """ |
| 468 | |
| 469 | # TASK_SETUP runs once per task, before/after all |
| 470 | # concurrent task instances start/finish. |
| 471 | TASK_SETUP = 'task_setup' |
| 472 | # Setup will run once for each instance of the task. |
| 473 | TASK_INSTANCE_SETUP = 'task_instance_setup' |
| 474 | REPORT_STEP = 'report_step' |
| 475 | _global_names_used = set() |
| 476 | |
| 477 | @staticmethod |
| 478 | def _get_next_name(node, group, name): |
| 479 | basename = str(node) + '/' + str(name) |
| 480 | names_used = ( |
| 481 | Task._global_names_used |
| 482 | if group is None else |
| 483 | set(t.name for t in group._tasks_to_add)) |
| 484 | cur_name = basename |
| 485 | i = 0 |
| 486 | while cur_name in names_used: |
| 487 | i += 1 |
| 488 | cur_name = '%s:%d' % (basename, i) |
| 489 | return cur_name |
| 490 | |
| 491 | def __init__( |
| 492 | self, step=None, outputs=None, |
| 493 | workspace_type=None, group=None, node=None, name=None, |
| 494 | num_instances=None): |
| 495 | """ |
| 496 | Instantiate a Task and add it to the current TaskGroup and Node. |
no outgoing calls
searching dependent graphs…