pantheon_api.md 9.3 KB
Newer Older
Y
Yibing Liu 已提交
1
# 大规模可扩展知识蒸馏框架 Pantheon
W
whs 已提交
2

Y
Yibing Liu 已提交
3 4
## Teacher

Y
Yibing Liu 已提交
5
pantheon.Teacher() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L78)
Y
Yibing Liu 已提交
6 7 8 9 10 11 12 13 14 15 16 17

: The class defined for the teacher model. Generate knowledge data and transfer them to the student model.

**Args:**

- **out\_path (str|None)** - The path to dump knowledge data for offline mode.

- **out\_port (int|None)** - The IP port number to send out knowledge for online mode, should be unique when launching multiple teachers in the same node.

**Return:** An object of class Teacher


Y
Yibing Liu 已提交
18
pantheon.Teacher.start() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L133)
Y
Yibing Liu 已提交
19 20 21 22 23 24 25 26 27

: Start teacher service, sychronize with student and launch the thread
  to monitor commands from student.

**Args:** None

**Return:** None


Y
Yibing Liu 已提交
28
pantheon.Teacher.send(data) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L181)
Y
Yibing Liu 已提交
29 30 31 32 33 34 35 36 37 38

: Send one data object to student.

**Args:**

- **data (Python data):** - The data to be sent, can be any type of Python data object.

**Return:** None


Y
Yibing Liu 已提交
39
pantheon.Teacher.recv() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L196)
Y
Yibing Liu 已提交
40 41 42 43 44 45 46 47 48 49

: Recieve one data object from student.

**Args:** None

**Return:**

- The received data, can be any type of Python data object.


Y
Yibing Liu 已提交
50
pantheon.Teacher.dump(knowledge) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L214)
Y
Yibing Liu 已提交
51 52 53 54 55 56 57 58 59 60

: Dump one batch knowledge data into the output file, only used in the offline mode.

**Args:**

- **knowledge (dict):** - The knowledge data to be dumped.  

**Return:** None


Y
Yibing Liu 已提交
61
pantheon.Teacher.start\_knowledge\_service(feed\_list, schema, program, reader\_config, exe, buf\_size=10, times=1) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/teacher.py#L259)
Y
Yibing Liu 已提交
62 63 64 65 66 67 68 69

: Start the knowledge service to generate and transfer knowledge data. In GPU mode, the devices to execute knowledge prediction will be determined by the
  environment variable **FLAGS\_selected\_gpus**, or by **CUDA\_VISIBLE\_DEVICES** if it is not set, and by **CPU\_NUM** (default 1) in CPU mode. Only supported in static graph.

 **Args:**

 - **feed\_list (list):** - A list of feed Variables or their names for the
                              input teacher Program.
70
 - **schema (dict):** - A dict to specify keys and fetched Variables  
Y
Yibing Liu 已提交
71 72 73 74
                        to generate knowledge.
 - **program (fluid.Program):** - Inference Program of the teacher model.
 - **reader\_config (dict):** - The config for data reader. Support all the three types of generators used by [fluid.io.PyReader](https://www.paddlepaddle.org.cn/documentation/docs/en/api/io/PyReader.html) and [fluid.io.DataLoader](https://www.paddlepaddle.org.cn/documentation/docs/en/api/io/DataLoader.html#dataloader), and their configs contain the key-value pair of the generator type and a generator object, plus other necessary argument pairs. See the following:

Y
Yibing Liu 已提交
75
     1) **sample generator:**
Y
Yibing Liu 已提交
76

Y
Yibing Liu 已提交
77 78 79 80 81
     ```
     reader_config={"sample_generator": some_sample_generator,
                    "batch_size": batch_size, "drop_last": drop_last}
     # drop_last set to True by default
     ```
Y
Yibing Liu 已提交
82

Y
Yibing Liu 已提交
83
     2) **sample list generator:**
Y
Yibing Liu 已提交
84

Y
Yibing Liu 已提交
85 86 87
     ```
     reader_config={"sample_list_generator": some_sample_list_generator}
     ```
Y
Yibing Liu 已提交
88

Y
Yibing Liu 已提交
89
     3) **batch generator:**
Y
Yibing Liu 已提交
90

Y
Yibing Liu 已提交
91 92 93
     ```
     reader_config={"batch_generator": some_batch_genrator}
     ```
Y
Yibing Liu 已提交
94

Y
Yibing Liu 已提交
95
     The trial to parse config will be in the order of 1) -> 3), and any other unrelated keys in these configs will be ignored.
Y
Yibing Liu 已提交
96 97 98 99 100

- **exe (fluid.Executor):** The executor to run the input program.
- **buf\_size (int):** The size of buffers for data reader and knowledge
                            writer on each device.
- **times (int):** The maximum repeated serving times, default 1. Whenever
101
                         the public method **get\_knowledge\_generator()** in **Student**
Y
Yibing Liu 已提交
102
                         object called once, the serving times will be added one,
Y
Yibing Liu 已提交
103 104
                         until reaching the maximum and ending the service. Only
                         valid in online mode, and will be ignored in offline mode.
Y
Yibing Liu 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

**Return:** None

**Examples:**

```python
import paddle
import paddle.fluid as fluid
from paddleslim.pantheon import Teacher

startup = fluid.Program()
program = fluid.Program()
with fluid.program_guard(program, startup):
    images = fluid.data(
            name='pixel', shape=[None, 3 * 32 * 32], dtype='float32')
    labels = fluid.data(name='label', shape=[None, 1], dtype='int64')
    logits = fluid.layers.fc(input=images, size=10)
    loss = fluid.layers.softmax_with_cross_entropy(logits, labels)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(startup)

128
train_reader = paddle.fluid.io.batch(
Y
Yibing Liu 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        paddle.dataset.cifar.train10(), batch_size=32)

teacher = Teacher(out_path="example_knowledge.dat", # offline mode
                  #out_port=5000                    # online mode
                  )
teacher.start()

teacher.start_knowledge_service(
    feed_list=[images, labels],
    schema={"logits": logits,
            "labels": labels},
    program=program,
    reader_config={"sample_list_generator": train_reader},
    exe=exe)
```

Y
Yibing Liu 已提交
145 146 147
!!! note "Note"
    This example should be run with the example of class **Student**.

Y
Yibing Liu 已提交
148 149 150

## Student

Y
Yibing Liu 已提交
151
pantheon.Student(merge_strategy=None) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L34)
Y
Yibing Liu 已提交
152 153 154 155 156 157

: The class defined for the student model. Receive knowledge data from
    teacher model and carry out knowledge merging.  

 **Args:**

158
 - **merge\_strategy (dict|None):** - A dict whose keys are the common schemas shared by different teachers, and each corresponding value specifies the merging strategy for different schemas respectively, supporting **sum** and **mean** now.
Y
Yibing Liu 已提交
159 160 161 162

**Return:** An object of class Student.


Y
Yibing Liu 已提交
163
pantheon.Student.register\_teacher(in\_path=None, in\_address=None) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L72)
Y
Yibing Liu 已提交
164 165 166 167 168 169

: Register one teacher model and assign the order number to it as  its id, with the file path (offline mode) or IP address (online  mode) that the teacher model writes knowledge data to.

**Args:**

- **in\_path (str|None):** The input file path. Default None.
170
- **in\_address (str|None):** The input IP address, in the format "<IP\_address>:<IP\_port>" (e.g. "127.0.0.1:8080"). Default None.
Y
Yibing Liu 已提交
171 172 173 174

**Return:**  None


Y
Yibing Liu 已提交
175
pantheon.Student.start() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L213)
Y
Yibing Liu 已提交
176 177 178 179 180 181 182

: End teachers' registration and synchronize with all of them.

**Args:** None

**Return:**  None

Y
Yibing Liu 已提交
183
pantheon.Student.send(self, data, teacher_ids=None) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L240)
Y
Yibing Liu 已提交
184 185 186 187 188 189 190 191 192 193

: Send data to teachers.

**Args:**

- **data (Python data):** - A Python data object to be sent.
- **teacher_ids (list|None):** - A list of teacher ids to send data. If set to None, send the data to all teachers. Default None.

**Return:**  None

Y
Yibing Liu 已提交
194
pantheon.Student.recv(teacher_id) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L262)
Y
Yibing Liu 已提交
195 196 197 198 199 200 201 202 203 204 205

: Receive data from one teacher.

 **Args:**

- **teacher\_id (int):** - The id of teacher that receives data from.

**Return:**  

- The received data object.

Y
Yibing Liu 已提交
206
pantheon.Student.get\_knowledge\_desc() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L283)
Y
Yibing Liu 已提交
207 208 209 210 211 212 213 214 215 216

 : Get description for knowledge, including shape, data type and lod level for each schema.

 **Args:** None

 **Return:**  

 - Knowledge description, which is a dict.


Y
Yibing Liu 已提交
217
pantheon.Student.get\_knowledge\_qsize() [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L318)
Y
Yibing Liu 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230

 : Get the real-time size of knowledge queue. If this size is denoted as
   **qsize**, it means that there are **qsize** batch knowledge data
   already pushed into knowledge queue and waiting for the knowledge
   generator to pop out. It's dynamic and limited up to 100, the capacity
   of the knowledge queue.

 **Args:** None

 **Return:**  

 - The real-time size of knowledge queue.

Y
Yibing Liu 已提交
231
pantheon.Student.get\_knowledge\_generator(batch\_size, drop\_last=False) [source](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/pantheon/student.py#L334)
Y
Yibing Liu 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

: Get the generator for knowledge data, return None if last generator doesn't finish yet.

**Args:**

- **batch\_size (int):** - The batch size of returned knowledge data.
- **drop\_last (bool):** - Whether to drop the last batch if its size is less than batch size.

**Return:**

- The wrapper of knowledge data generator.

**Examples:**

```python
from paddleslim.pantheon import Student

student = Student()

student.register_teacher(in_path="example_knowledge.dat",  # offline mode
                         #in_address="127.0.0.1:5000"      # online mode
                         )
student.start()

knowledge_desc = student.get_knowledge_desc()
data_generator = student.get_knowledge_generator(
    batch_size=128, drop_last=True)

# get knowledge data
for knowledge in data_generator():
    print("knowledge queue size: {}".format(student.get_knowledge_qsize()))

    # do something else
```
Y
Yibing Liu 已提交
266 267 268

!!! note "Note"
    This example should be run with the example of class **Teacher**.