executor.md 1.7 KB
Newer Older
Y
Yang Yang(Tony) 已提交
1 2 3 4 5
# Executor Design Doc

## Motivation

We use executor to do the runtime evaluation of a `ProgramDesc`.
Y
Yang Yang(Tony) 已提交
6 7 8

## Overview

Y
Yang Yang(Tony) 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
An executor takes a `ProgramDesc`, a `block_id` and a `Scope`.  The `ProgramDesc` is a list of blocks and each block contains the protobuf definition of all the parameters and operators. The `block_id` specifies the entrance block. And the `Scope` is the container of all the variable instance, which is persistent throughout different runs.

### What does executor do?

It evaluates all the operators in the `block_id`th block of a `ProgramDesc`.

### What does executor NOT do?

It does not do runtime optimization, meaning intelligently parse the dependency of each op a choose which one to be run and in which order they should be run.

It does not do graph partitioning, meaning dividing the `ProgramDesc` into several small pieces and executing them on different devices.

## Implementation

`Executor` evaluates a `ProgramDesc`. Essentially, it instantiates Variables and Operators, then run all the operators
Y
Yang Yang(Tony) 已提交
24 25

```c++
Y
Yang Yang(Tony) 已提交
26 27
void Executor::Run(const ProgramDesc& pdesc, Scope* scope, int block_id) {
  auto& block = pdesc.blocks(block_id);
Y
Yang Yang(Tony) 已提交
28 29 30 31 32 33 34 35 36
  auto& device = device_contexts_[0];

  // Instantiate all the vars in the global scope
  for (auto& var : block.vars()) {
    scope->NewVar(var.name());
  }

  // Run the block
  Scope& local_scope = scope->NewScope();
Y
Yang Yang(Tony) 已提交
37
  for (auto& op_desc : block.ops()) {
Y
Yang Yang(Tony) 已提交
38
    for (auto& var : op_desc.outputs()) {
Y
Yang Yang(Tony) 已提交
39
      for (auto& argu : var.arguments()) {
Y
Yang Yang(Tony) 已提交
40 41 42
        // Create temp variable in the local_scope
        if (local_scope.FindVar(argu) == nullptr) {
          local_scope.NewVar(argu);
Y
Yang Yang(Tony) 已提交
43 44 45
        }
      }
    }
Y
Yang Yang(Tony) 已提交
46
    auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yang Yang(Tony) 已提交
47
    op->Run(local_scope, *device);
Y
Yang Yang(Tony) 已提交
48 49 50
  }
}
```