dag.py 31.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing
import threading
import multiprocessing
import sys
B
barrierye 已提交
18
import copy
19 20 21 22 23 24 25 26
if sys.version_info.major == 2:
    import Queue
elif sys.version_info.major == 3:
    import queue as Queue
else:
    raise Exception("Error Python version")
import os
import logging
B
barrierye 已提交
27
import collections
T
TeslaZhao 已提交
28
import json
29 30

from .operator import Op, RequestOp, ResponseOp, VirtualOp
B
barrierye 已提交
31
from .channel import (ThreadChannel, ProcessChannel, ChannelData,
T
TeslaZhao 已提交
32 33
                      ChannelDataErrcode, ChannelDataType, ChannelStopError,
                      ProductErrCode)
B
barriery 已提交
34
from .profiler import TimeProfiler, PerformanceTracer
35
from .util import NameGenerator, ThreadIdGenerator, PipelineProcSyncManager
B
barriery 已提交
36
from .proto import pipeline_service_pb2
37

38
_LOGGER = logging.getLogger(__name__)
39 40 41


class DAGExecutor(object):
42 43 44 45
    """
    DAG Executor, the service entrance of DAG.
    """

46
    def __init__(self, response_op, server_conf, worker_idx):
47 48 49 50 51 52 53 54 55 56 57 58
        """
        Initialize DAGExecutor.

        Args:
            response_op: Response OP
            server_conf: server conf. config.yaml
            worker_idx: DAGExecutor index, PipelineServer creates many
                DAGExecutors when _build_dag_each_worker is true.

        Returns:
            None.
        """
B
barriery 已提交
59 60 61 62
        build_dag_each_worker = server_conf["build_dag_each_worker"]
        server_worker_num = server_conf["worker_num"]
        dag_conf = server_conf["dag"]

63 64 65 66
        self._retry = dag_conf["retry"]
        self._server_use_profile = dag_conf["use_profile"]
        channel_size = dag_conf["channel_size"]
        self._is_thread_op = dag_conf["is_thread_op"]
B
barrierye 已提交
67

B
barriery 已提交
68 69 70 71
        tracer_conf = dag_conf["tracer"]
        tracer_interval_s = tracer_conf["interval_s"]

        self.name = "@DAGExecutor"
B
barrierye 已提交
72
        self._profiler = TimeProfiler()
B
barrierye 已提交
73
        self._profiler.enable(True)
B
barrierye 已提交
74

B
barriery 已提交
75 76 77 78
        self._tracer = None
        if tracer_interval_s >= 1:
            self._tracer = PerformanceTracer(
                self._is_thread_op, tracer_interval_s, server_worker_num)
B
barriery 已提交
79

B
barrierye 已提交
80
        self._dag = DAG(self.name, response_op, self._server_use_profile,
W
wangjiawei04 已提交
81 82
                        self._is_thread_op, channel_size, build_dag_each_worker,
                        self._tracer)
B
barrierye 已提交
83 84
        (in_channel, out_channel, pack_rpc_func,
         unpack_rpc_func) = self._dag.build()
85 86 87
        self._dag.start()

        self._set_in_channel(in_channel)
88
        self._set_out_channel(out_channel)
89 90 91
        self._pack_rpc_func = pack_rpc_func
        self._unpack_rpc_func = unpack_rpc_func

B
barriery 已提交
92 93 94
        if self._tracer is not None:
            self._tracer.start()

95 96 97
        # generate id 
        # data_id: Server Unique ID, automatically generated by the framework
        # log_id: Trace one product request, can be empty, not unique.
98 99 100 101 102
        base_counter = 0
        gen_id_step = 1
        if build_dag_each_worker:
            base_counter = worker_idx
            gen_id_step = server_worker_num
B
barriery 已提交
103
        self._id_generator = ThreadIdGenerator(
104 105 106
            max_id=1000000000000000000,
            base_counter=base_counter,
            step=gen_id_step)
B
barriery 已提交
107

B
barrierye 已提交
108 109
        self._cv_pool = {}
        self._cv_for_cv_pool = threading.Condition()
110
        self._fetch_buffer = {}
111 112
        self._recive_func = None

B
barrierye 已提交
113 114 115
        self._client_profile_key = "pipeline.profile"
        self._client_profile_value = "1"

116
    def start(self):
117 118 119 120 121 122 123 124 125
        """
        Starting one thread for receiving data from the last channel background.

        Args:
            None

        Returns:
            None
        """
126 127
        self._recive_func = threading.Thread(
            target=DAGExecutor._recive_out_channel_func, args=(self, ))
B
barriery 已提交
128
        self._recive_func.daemon = True
129
        self._recive_func.start()
B
barriery 已提交
130
        _LOGGER.debug("[DAG Executor] Start recive thread")
131 132

    def stop(self):
133 134 135 136 137 138 139 140 141
        """
        Stopping DAG

        Args:
            None

        Returns:
            None
        """
142 143
        self._dag.stop()
        self._dag.join()
B
barriery 已提交
144
        _LOGGER.info("[DAG Executor] Stop")
145 146

    def _get_next_data_id(self):
147 148 149 150 151 152 153 154 155 156
        """
        Generate data_id incrementally and Uniquely
   
        Args:
            None

        Returns:
            data_id: uniq id
            cond_v: condition variable
        """
B
barriery 已提交
157
        data_id = self._id_generator.next()
B
bug fix  
barriery 已提交
158 159 160
        cond_v = threading.Condition()
        with self._cv_for_cv_pool:
            self._cv_pool[data_id] = cond_v
161
            self._fetch_buffer[data_id] = None
B
bug fix  
barriery 已提交
162
        return data_id, cond_v
163 164

    def _set_in_channel(self, in_channel):
165 166 167 168 169 170 171 172 173
        """
        Set in_channel of DAG

        Args:
            in_channel: input channel of DAG

        Returns:
            None 
        """
174
        if not isinstance(in_channel, (ThreadChannel, ProcessChannel)):
B
barriery 已提交
175 176 177
            _LOGGER.critical("[DAG Executor] Failed to set in_channel: "
                             "in_channel must be Channel type, but get {}".
                             format(type(in_channel)))
178
            os._exit(-1)
179

180
        self._in_channel = in_channel
181
        _LOGGER.info("[DAG] set in channel succ, name [{}]".format(self.name))
182 183

    def _set_out_channel(self, out_channel):
184 185 186 187 188 189 190 191 192
        """
        Set out_channel of DAG

        Args:
            out_channel: output channel of DAG

        Returns:
            None 
        """
193
        if not isinstance(out_channel, (ThreadChannel, ProcessChannel)):
B
barriery 已提交
194 195 196
            _LOGGER.critical("[DAG Executor] Failed to set out_channel: "
                             "must be Channel type, but get {}".format(
                                 type(out_channel)))
197
            os._exit(-1)
198 199 200 201
        out_channel.add_consumer(self.name)
        self._out_channel = out_channel

    def _recive_out_channel_func(self):
202 203 204 205 206 207 208 209 210 211 212
        """
        Receiving data from the output channel, and pushing data into 
        _fetch_buffer. Function _get_channeldata_from_fetch_buffer gets 
        data by retry time.

        Args:
            None

        Returns:
            None
        """
B
barrierye 已提交
213
        cv = None
B
barrierye 已提交
214 215 216 217
        while True:
            try:
                channeldata_dict = self._out_channel.front(self.name)
            except ChannelStopError:
B
barriery 已提交
218
                _LOGGER.info("[DAG Executor] Stop.")
B
barrierye 已提交
219 220 221
                with self._cv_for_cv_pool:
                    for data_id, cv in self._cv_pool.items():
                        closed_errror_data = ChannelData(
T
TeslaZhao 已提交
222
                            error_code=ChannelDataErrcode.CLOSED_ERROR.value,
B
barrierye 已提交
223 224 225
                            error_info="dag closed.",
                            data_id=data_id)
                        with cv:
226
                            self._fetch_buffer[data_id] = closed_errror_data
B
barrierye 已提交
227 228
                            cv.notify_all()
                break
229
            if len(channeldata_dict) != 1:
230
                _LOGGER.critical(
B
barriery 已提交
231 232
                    "[DAG Executor] Failed to fetch result: out_channel "
                    "cannot have multiple input ops")
233 234 235
                os._exit(-1)
            (_, channeldata), = channeldata_dict.items()
            if not isinstance(channeldata, ChannelData):
236
                _LOGGER.critical(
B
barriery 已提交
237 238
                    '[DAG Executor] Failed to fetch result: data in out_channel" \
                    " must be ChannelData type, but get {}'
B
barriery 已提交
239
                    .format(type(channeldata)))
B
barriery 已提交
240
                os._exit(-1)
B
barrierye 已提交
241 242

            data_id = channeldata.id
B
barriery 已提交
243 244
            _LOGGER.debug("(logid={}) [recive thread] Fetched data".format(
                data_id))
B
barrierye 已提交
245
            with self._cv_for_cv_pool:
246 247 248 249
                cond_v = self._cv_pool[data_id]
            with cond_v:
                self._fetch_buffer[data_id] = channeldata
                cond_v.notify_all()
250

B
bug fix  
barriery 已提交
251
    def _get_channeldata_from_fetch_buffer(self, data_id, cond_v):
252 253 254 255 256 257 258 259 260 261
        """
        Getting the channel data from _fetch_buffer.

        Args:
            data_id: search key
            cond_v: conditional variable

        Returns:
            ready_data: one channel data processed
        """
262 263
        ready_data = None

B
bug fix  
barriery 已提交
264
        with cond_v:
265 266 267 268 269 270 271 272 273 274 275 276 277
            with self._cv_for_cv_pool:
                if self._fetch_buffer[data_id] is not None:
                    # The requested data is already ready
                    ready_data = self._fetch_buffer[data_id]
                    self._cv_pool.pop(data_id)
                    self._fetch_buffer.pop(data_id)
            if ready_data is None:
                # Wait for data ready
                cond_v.wait()
                with self._cv_for_cv_pool:
                    ready_data = self._fetch_buffer[data_id]
                    self._cv_pool.pop(data_id)
                    self._fetch_buffer.pop(data_id)
278
        _LOGGER.debug("(data_id={}) [resp thread] Got data".format(data_id))
279
        return ready_data
280

B
barrierye 已提交
281
    def _pack_channeldata(self, rpc_request, data_id):
282 283 284 285 286 287 288 289 290 291
        """
        Unpacking data from RPC request. and creating one channelData.

        Args:
           rpc_request: one RPC request
           data_id: data id, unique

        Returns:
            ChannelData: one channel data to be processed
        """
292
        dictdata = None
T
TeslaZhao 已提交
293
        log_id = None
294
        try:
T
TeslaZhao 已提交
295 296
            dictdata, log_id, prod_errcode, prod_errinfo = self._unpack_rpc_func(
                rpc_request)
297
        except Exception as e:
B
barriery 已提交
298 299 300 301
            _LOGGER.error(
                "(logid={}) Failed to parse RPC request package: {}"
                .format(data_id, e),
                exc_info=True)
302
            return ChannelData(
T
TeslaZhao 已提交
303
                error_code=ChannelDataErrcode.RPC_PACKAGE_ERROR.value,
304
                error_info="rpc package error: {}".format(e),
T
TeslaZhao 已提交
305 306
                data_id=data_id,
                log_id=log_id)
307
        else:
T
TeslaZhao 已提交
308 309 310 311 312
            # because unpack_rpc_func is rewritten by user, we need to look
            # for product_errcode in returns, and  client_profile_key field
            # in rpc_request
            if prod_errcode is not None:
                # product errors occured
313 314
                _LOGGER.error("unpack_rpc_func prod_errcode:{}".format(
                    prod_errcode))
T
TeslaZhao 已提交
315 316 317 318 319 320 321 322
                return ChannelData(
                    error_code=ChannelDataErrcode.PRODUCT_ERROR.value,
                    error_info="",
                    prod_error_code=prod_errcode,
                    prod_error_info=prod_errinfo,
                    data_id=data_id,
                    log_id=log_id)

B
barrierye 已提交
323
            profile_value = None
T
TeslaZhao 已提交
324
            profile_value = dictdata.get(self._client_profile_key)
B
barriery 已提交
325
            client_need_profile = (profile_value == self._client_profile_value)
326 327 328
            return ChannelData(
                datatype=ChannelDataType.DICT.value,
                dictdata=dictdata,
B
barrierye 已提交
329
                data_id=data_id,
T
TeslaZhao 已提交
330
                log_id=log_id,
B
barriery 已提交
331
                client_need_profile=client_need_profile)
332 333

    def call(self, rpc_request):
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        """
        DAGExcutor enterance function. There are 5 steps:
        1._get_next_data_id: Generate an incremental ID
        2._pack_channeldata: pack the channel data from request.
        3.retry loop: 
            a. push channel_data into _in_channel
            b. get_channeldata_from_fetch_buffer: get results.
        4._pack_for_rpc_resp: pack RPC responses
        5.profile: generte profile string and pack into response.

        Args:
            rpc_request: one RPC request
   
        Returns:
            rpc_resp: one RPC response
        """
B
barriery 已提交
350 351
        if self._tracer is not None:
            trace_buffer = self._tracer.data_buffer()
B
barriery 已提交
352

B
bug fix  
barriery 已提交
353
        data_id, cond_v = self._get_next_data_id()
B
barriery 已提交
354

B
barriery 已提交
355
        start_call, end_call = None, None
B
barrierye 已提交
356
        if not self._is_thread_op:
B
barriery 已提交
357 358
            start_call = self._profiler.record("call_{}#DAG-{}_0".format(
                data_id, data_id))
B
barrierye 已提交
359
        else:
B
barriery 已提交
360
            start_call = self._profiler.record("call_{}#DAG_0".format(data_id))
B
barrierye 已提交
361

B
barrierye 已提交
362 363 364
        self._profiler.record("prepack_{}#{}_0".format(data_id, self.name))
        req_channeldata = self._pack_channeldata(rpc_request, data_id)
        self._profiler.record("prepack_{}#{}_1".format(data_id, self.name))
365

366 367 368 369
        log_id = req_channeldata.log_id
        _LOGGER.info("(data_id={} log_id={}) Succ Generate ID ".format(data_id,
                                                                       log_id))

370 371
        resp_channeldata = None
        for i in range(self._retry):
372
            _LOGGER.debug("(data_id={}) Pushing data into Graph engine".format(
B
barriery 已提交
373
                data_id))
B
barrierye 已提交
374
            try:
375 376 377 378 379 380 381 382 383 384
                if req_channeldata is None:
                    _LOGGER.critical(
                        "(data_id={} log_id={}) req_channeldata is None"
                        .format(data_id, log_id))
                if not isinstance(self._in_channel,
                                  (ThreadChannel, ProcessChannel)):
                    _LOGGER.critical(
                        "(data_id={} log_id={})[DAG Executor] Failed to "
                        "set in_channel: in_channel must be Channel type, but get {}".
                        format(data_id, log_id, type(self._in_channel)))
B
barrierye 已提交
385 386
                self._in_channel.push(req_channeldata, self.name)
            except ChannelStopError:
387 388
                _LOGGER.error("(data_id:{} log_id={})[DAG Executor] Stop".
                              format(data_id, log_id))
B
bug fix  
barriery 已提交
389 390
                with self._cv_for_cv_pool:
                    self._cv_pool.pop(data_id)
B
barrierye 已提交
391 392
                return self._pack_for_rpc_resp(
                    ChannelData(
T
TeslaZhao 已提交
393
                        error_code=ChannelDataErrcode.CLOSED_ERROR.value,
B
barrierye 已提交
394 395
                        error_info="dag closed.",
                        data_id=data_id))
396

397 398
            _LOGGER.debug("(data_id={} log_id={}) Wait for Graph engine...".
                          format(data_id, log_id))
B
bug fix  
barriery 已提交
399 400
            resp_channeldata = self._get_channeldata_from_fetch_buffer(data_id,
                                                                       cond_v)
401

T
TeslaZhao 已提交
402
            if resp_channeldata.error_code == ChannelDataErrcode.OK.value:
403 404
                _LOGGER.info("(data_id={} log_id={}) Succ predict".format(
                    data_id, log_id))
405
                break
B
barriery 已提交
406
            else:
407 408 409
                _LOGGER.error("(data_id={} log_id={}) Failed to predict: {}"
                              .format(data_id, log_id,
                                      resp_channeldata.error_info))
T
TeslaZhao 已提交
410
                if resp_channeldata.error_code != ChannelDataErrcode.TIMEOUT.value:
B
barriery 已提交
411 412
                    break

413
            if i + 1 < self._retry:
414 415 416
                _LOGGER.warning(
                    "(data_id={} log_id={}) DAGExecutor retry({}/{})"
                    .format(data_id, log_id, i + 1, self._retry))
417

418 419
        _LOGGER.debug("(data_id={} log_id={}) Packing RPC response package"
                      .format(data_id, log_id))
B
barrierye 已提交
420
        self._profiler.record("postpack_{}#{}_0".format(data_id, self.name))
421
        rpc_resp = self._pack_for_rpc_resp(resp_channeldata)
B
barrierye 已提交
422
        self._profiler.record("postpack_{}#{}_1".format(data_id, self.name))
B
barrierye 已提交
423
        if not self._is_thread_op:
B
barriery 已提交
424 425
            end_call = self._profiler.record("call_{}#DAG-{}_1".format(data_id,
                                                                       data_id))
B
barrierye 已提交
426
        else:
B
barriery 已提交
427
            end_call = self._profiler.record("call_{}#DAG_1".format(data_id))
B
barriery 已提交
428 429

        if self._tracer is not None:
B
barrierye 已提交
430
            trace_buffer.put({
B
barrierye 已提交
431 432
                "name": "DAG",
                "id": data_id,
T
TeslaZhao 已提交
433 434
                "succ":
                resp_channeldata.error_code == ChannelDataErrcode.OK.value,
B
barrierye 已提交
435 436 437 438
                "actions": {
                    "call_{}".format(data_id): end_call - start_call,
                },
            })
B
barrierye 已提交
439 440 441 442 443 444 445

        profile_str = self._profiler.gen_profile_str()
        if self._server_use_profile:
            sys.stderr.write(profile_str)

        # add profile info into rpc_resp
        if resp_channeldata.client_need_profile:
B
barrierye 已提交
446 447 448
            profile_set = resp_channeldata.profile_data_set
            profile_set.add(profile_str)
            profile_value = "".join(list(profile_set))
B
barriery 已提交
449 450
            rpc_resp.key.append(self._client_profile_key)
            rpc_resp.value.append(profile_value)
B
barrierye 已提交
451

452 453 454
        return rpc_resp

    def _pack_for_rpc_resp(self, channeldata):
455 456 457 458 459 460 461 462 463
        """
        Packing one RPC response

        Args:
            channeldata: one channel data to be packed

        Returns:
            resp: one RPC response
        """
B
barriery 已提交
464 465 466 467 468 469 470 471
        try:
            return self._pack_rpc_func(channeldata)
        except Exception as e:
            _LOGGER.error(
                "(logid={}) Failed to pack RPC response package: {}"
                .format(channeldata.id, e),
                exc_info=True)
            resp = pipeline_service_pb2.Response()
T
TeslaZhao 已提交
472 473
            resp.err_no = ChannelDataErrcode.RPC_PACKAGE_ERROR.value
            resp.err_msg = "rpc package error: {}".format(e)
B
barriery 已提交
474
            return resp
475 476 477


class DAG(object):
478 479 480 481
    """
    Directed Acyclic Graph(DAG) engine, builds one DAG topology.
    """

B
barrierye 已提交
482
    def __init__(self, request_name, response_op, use_profile, is_thread_op,
W
wangjiawei04 已提交
483
                 channel_size, build_dag_each_worker, tracer):
B
barrierye 已提交
484
        self._request_name = request_name
485
        self._response_op = response_op
B
barrierye 已提交
486
        self._use_profile = use_profile
B
barrierye 已提交
487
        self._is_thread_op = is_thread_op
488
        self._channel_size = channel_size
B
barriery 已提交
489
        self._build_dag_each_worker = build_dag_each_worker
B
barriery 已提交
490
        self._tracer = tracer
B
barrierye 已提交
491
        if not self._is_thread_op:
492
            self._manager = PipelineProcSyncManager()
B
barriery 已提交
493
        _LOGGER.info("[DAG] Succ init")
494

495
    @staticmethod
B
barriery 已提交
496
    def get_use_ops(response_op):
497 498 499 500 501 502 503 504 505 506 507 508
        """
        Starting from ResponseOp, recursively traverse the front OPs. Getting
        all used ops and the post op list of each op (excluding ResponseOp)

        Args:
            response_op: ResponseOp

        Returns:
            used_ops: used ops, set
            succ_ops_of_use_op: op and the next op list, dict.
            
        """
509
        unique_names = set()
510
        used_ops = set()
511 512 513 514 515 516 517 518 519 520
        succ_ops_of_use_op = {}  # {op_name: succ_ops}
        que = Queue.Queue()
        que.put(response_op)
        while que.qsize() != 0:
            op = que.get()
            for pred_op in op.get_input_ops():
                if pred_op.name not in succ_ops_of_use_op:
                    succ_ops_of_use_op[pred_op.name] = []
                if op != response_op:
                    succ_ops_of_use_op[pred_op.name].append(op)
521
                if pred_op not in used_ops:
522
                    que.put(pred_op)
523
                    used_ops.add(pred_op)
524 525
                    # check the name of op is globally unique
                    if pred_op.name in unique_names:
B
barriery 已提交
526 527
                        _LOGGER.critical("Failed to get used Ops: the"
                                         " name of Op must be unique: {}".
528 529
                                         format(pred_op.name))
                        os._exit(-1)
530
                    unique_names.add(pred_op.name)
531
        return used_ops, succ_ops_of_use_op
532 533

    def _gen_channel(self, name_gen):
534 535 536 537 538 539 540 541 542
        """
        Generate one ThreadChannel or ProcessChannel.

        Args:
            name_gen: channel name

        Returns:
            channel: one channel generated
        """
543
        channel = None
B
barrierye 已提交
544
        if self._is_thread_op:
545 546 547 548 549
            channel = ThreadChannel(
                name=name_gen.next(), maxsize=self._channel_size)
        else:
            channel = ProcessChannel(
                self._manager, name=name_gen.next(), maxsize=self._channel_size)
B
barriery 已提交
550
        _LOGGER.debug("[DAG] Generate channel: {}".format(channel.name))
551 552 553
        return channel

    def _gen_virtual_op(self, name_gen):
554 555 556 557 558 559 560 561 562
        """
        Generate one virtual Op

        Args:
            name_gen: Op name

        Returns:
            vir_op: one virtual Op object.
        """
B
barriery 已提交
563
        vir_op = VirtualOp(name=name_gen.next())
B
barriery 已提交
564
        _LOGGER.debug("[DAG] Generate virtual_op: {}".format(vir_op.name))
B
barriery 已提交
565
        return vir_op
566 567

    def _topo_sort(self, used_ops, response_op, out_degree_ops):
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        """
        Topological sort of DAG, creates inverted multi-layers views.

        Args:
            used_ops: op used in DAG
            response_op: response op
            out_degree_ops: Next op list for each op, dict. the output of 
                get_use_ops()

        Returns:
            dag_views: the inverted hierarchical topology list. examples:
                DAG :[A -> B -> C -> E]
                            \-> D /
                dag_views: [[E], [C, D], [B], [A]]
                         
            last_op:the last op front of ResponseOp
        """
585 586 587 588 589 590 591
        out_degree_num = {
            name: len(ops)
            for name, ops in out_degree_ops.items()
        }
        que_idx = 0  # scroll queue 
        ques = [Queue.Queue() for _ in range(2)]
        zero_indegree_num = 0
592
        for op in used_ops:
593 594 595
            if len(op.get_input_ops()) == 0:
                zero_indegree_num += 1
        if zero_indegree_num != 1:
B
barriery 已提交
596 597
            _LOGGER.critical("Failed to topo sort: DAG contains "
                             "multiple RequestOps")
598
            os._exit(-1)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
        last_op = response_op.get_input_ops()[0]
        ques[que_idx].put(last_op)

        # topo sort to get dag_views
        dag_views = []
        sorted_op_num = 0
        while True:
            que = ques[que_idx]
            next_que = ques[(que_idx + 1) % 2]
            dag_view = []
            while que.qsize() != 0:
                op = que.get()
                dag_view.append(op)
                sorted_op_num += 1
                for pred_op in op.get_input_ops():
                    out_degree_num[pred_op.name] -= 1
                    if out_degree_num[pred_op.name] == 0:
                        next_que.put(pred_op)
            dag_views.append(dag_view)
            if next_que.qsize() == 0:
                break
            que_idx = (que_idx + 1) % 2
621
        if sorted_op_num < len(used_ops):
B
barriery 已提交
622
            _LOGGER.critical("Failed to topo sort: not legal DAG")
623
            os._exit(-1)
624 625 626

        return dag_views, last_op

627
    def _build_dag(self, response_op):
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        """
        Building DAG, the most important function in class DAG. Core steps:
        1.get_use_ops: Getting used ops, and out degree op list for each op.
        2._topo_sort: Topological sort creates inverted multi-layers views.
        3.create channels and virtual ops.

        Args:
            response_op: ResponseOp

        Returns:
            actual_ops: all OPs used in DAG, including virtual OPs
            channels: all channels used in DAG 
            input_channel: the channel of first OP 
            output_channel: the channel of last OP
            pack_func: pack_response_package function of response_op
            unpack_func: unpack_request_package function of request_op
        """
645
        if response_op is None:
B
barriery 已提交
646 647
            _LOGGER.critical("Failed to build DAG: ResponseOp"
                             " has not been set.")
648
            os._exit(-1)
649
        used_ops, out_degree_ops = DAG.get_use_ops(response_op)
650
        if not self._build_dag_each_worker:
B
barrierye 已提交
651 652
            _LOGGER.info("================= USED OP =================")
            for op in used_ops:
B
barriery 已提交
653
                if not isinstance(op, RequestOp):
B
barrierye 已提交
654 655
                    _LOGGER.info(op.name)
            _LOGGER.info("-------------------------------------------")
656
        if len(used_ops) <= 1:
657
            _LOGGER.critical(
B
barriery 已提交
658 659
                "Failed to build DAG: besides RequestOp and ResponseOp, "
                "there should be at least one Op in DAG.")
660
            os._exit(-1)
B
barriery 已提交
661 662
        if self._build_dag_each_worker:
            _LOGGER.info("Because `build_dag_each_worker` mode is used, "
B
barriery 已提交
663 664
                         "Auto-batching is set to the default config: "
                         "batch_size=1, auto_batching_timeout=None")
B
barriery 已提交
665 666
            for op in used_ops:
                op.use_default_auto_batching_config()
667 668 669

        dag_views, last_op = self._topo_sort(used_ops, response_op,
                                             out_degree_ops)
B
barrierye 已提交
670
        dag_views = list(reversed(dag_views))
671
        if not self._build_dag_each_worker:
672
            _LOGGER.info("================== DAG ====================")
B
barrierye 已提交
673
            for idx, view in enumerate(dag_views):
674
                _LOGGER.info("(VIEW {})".format(idx))
B
barrierye 已提交
675
                for op in view:
676
                    _LOGGER.info("  [{}]".format(op.name))
B
barrierye 已提交
677
                    for out_op in out_degree_ops[op.name]:
678 679
                        _LOGGER.info("    - {}".format(out_op.name))
            _LOGGER.info("-------------------------------------------")
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

        # create channels and virtual ops
        virtual_op_name_gen = NameGenerator("vir")
        channel_name_gen = NameGenerator("chl")
        virtual_ops = []
        channels = []
        input_channel = None
        actual_view = None
        for v_idx, view in enumerate(dag_views):
            if v_idx + 1 >= len(dag_views):
                break
            next_view = dag_views[v_idx + 1]
            if actual_view is None:
                actual_view = view
            actual_next_view = []
            pred_op_of_next_view_op = {}
            for op in actual_view:
                # find actual succ op in next view and create virtual op
                for succ_op in out_degree_ops[op.name]:
                    if succ_op in next_view:
                        if succ_op not in actual_next_view:
                            actual_next_view.append(succ_op)
                        if succ_op.name not in pred_op_of_next_view_op:
                            pred_op_of_next_view_op[succ_op.name] = []
                        pred_op_of_next_view_op[succ_op.name].append(op)
                    else:
                        # create virtual op
                        virtual_op = self._gen_virtual_op(virtual_op_name_gen)
                        virtual_ops.append(virtual_op)
                        out_degree_ops[virtual_op.name] = [succ_op]
                        actual_next_view.append(virtual_op)
                        pred_op_of_next_view_op[virtual_op.name] = [op]
                        virtual_op.add_virtual_pred_op(op)
            actual_view = actual_next_view
            # create channel
            processed_op = set()
            for o_idx, op in enumerate(actual_next_view):
                if op.name in processed_op:
                    continue
                channel = self._gen_channel(channel_name_gen)
                channels.append(channel)
                op.add_input_channel(channel)
722
                _LOGGER.info("op:{} add input channel.".format(op.name))
723 724 725 726 727 728 729
                pred_ops = pred_op_of_next_view_op[op.name]
                if v_idx == 0:
                    input_channel = channel
                else:
                    # if pred_op is virtual op, it will use ancestors as producers to channel
                    for pred_op in pred_ops:
                        pred_op.add_output_channel(channel)
730 731
                        _LOGGER.info("pred_op:{} add output channel".format(
                            pred_op.name))
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
                processed_op.add(op.name)
                # find same input op to combine channel
                for other_op in actual_next_view[o_idx + 1:]:
                    if other_op.name in processed_op:
                        continue
                    other_pred_ops = pred_op_of_next_view_op[other_op.name]
                    if len(other_pred_ops) != len(pred_ops):
                        continue
                    same_flag = True
                    for pred_op in pred_ops:
                        if pred_op not in other_pred_ops:
                            same_flag = False
                            break
                    if same_flag:
                        other_op.add_input_channel(channel)
                        processed_op.add(other_op.name)
        output_channel = self._gen_channel(channel_name_gen)
        channels.append(output_channel)
        last_op.add_output_channel(output_channel)
751
        _LOGGER.info("last op:{} add output channel".format(last_op.name))
752 753 754 755 756

        pack_func, unpack_func = None, None
        pack_func = response_op.pack_response_package

        actual_ops = virtual_ops
757
        for op in used_ops:
758
            if len(op.get_input_ops()) == 0:
759 760 761
                #set special features of the request op. 
                #1.set unpack function.
                #2.set output channel. 
762
                unpack_func = op.unpack_request_package
763
                op.add_output_channel(input_channel)
764 765 766 767
                continue
            actual_ops.append(op)

        for c in channels:
B
barriery 已提交
768
            _LOGGER.debug("Channel({}):\n\t- producers: {}\n\t- consumers: {}"
B
barriery 已提交
769
                          .format(c.name, c.get_producers(), c.get_consumers()))
770 771 772 773

        return (actual_ops, channels, input_channel, output_channel, pack_func,
                unpack_func)

B
barriery 已提交
774 775 776
    def get_channels(self):
        return self._channels

777
    def build(self):
778 779 780 781 782 783 784 785 786 787 788 789
        """
        Interface for building one DAG outside.

        Args:
            None

        Returns:
            _input_channel: the channel of first OP
            _output_channel:  the channel of last OP
            _pack_func: pack_response_package function of response_op
            _unpack_func: unpack_request_package function of request_op
        """
790
        (actual_ops, channels, input_channel, output_channel, pack_func,
791
         unpack_func) = self._build_dag(self._response_op)
B
barriery 已提交
792
        _LOGGER.info("[DAG] Succ build DAG")
793 794 795 796 797 798 799 800

        self._actual_ops = actual_ops
        self._channels = channels
        self._input_channel = input_channel
        self._output_channel = output_channel
        self._pack_func = pack_func
        self._unpack_func = unpack_func

B
bug fix  
barrierye 已提交
801 802
        if self._tracer is not None:
            self._tracer.set_channels(self._channels)
B
barriery 已提交
803

804 805 806
        return self._input_channel, self._output_channel, self._pack_func, self._unpack_func

    def start(self):
807 808 809 810 811 812 813 814 815
        """
        Each OP starts a thread or process by _is_thread_op 

        Args:
            None

        Returns:
            _threads_or_proces: threads or process list.
        """
816 817
        self._threads_or_proces = []
        for op in self._actual_ops:
B
barrierye 已提交
818
            op.use_profiler(self._use_profile)
B
barriery 已提交
819
            op.set_tracer(self._tracer)
B
barrierye 已提交
820
            if self._is_thread_op:
W
wangjiawei04 已提交
821
                self._threads_or_proces.extend(op.start_with_thread())
822
            else:
W
wangjiawei04 已提交
823
                self._threads_or_proces.extend(op.start_with_process())
B
barriery 已提交
824 825
        _LOGGER.info("[DAG] start")

826 827 828 829
        # not join yet
        return self._threads_or_proces

    def join(self):
830 831 832 833 834 835 836 837 838
        """
        All threads or processes join.

        Args:
            None

        Returns:
            None
        """
839
        for x in self._threads_or_proces:
W
wangjiawei04 已提交
840 841
            if x is not None:
                x.join()
842 843

    def stop(self):
844 845 846 847 848 849 850 851 852
        """
        Stopping and cleanning all channels.

        Args:
            None

        Returns:
            None 
        """
853 854
        for chl in self._channels:
            chl.stop()
855 856 857
        for op in self._actual_ops:
            op.clean_input_channel()
            op.clean_output_channels()