interpreter_impl.cpp 48.1 KB
Newer Older
M
Megvii Engine Team 已提交
1
/**
2
 * \file imperative/src/impl/interpreter/interpreter_impl.cpp
M
Megvii Engine Team 已提交
3 4
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
M
Megvii Engine Team 已提交
6 7 8 9 10 11
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

12
#include "./interpreter_impl.h"
13

14 15
#include "range/v3/all.hpp"

16
#include "megbrain/common.h"
17 18
#include "megbrain/imperative/opr_utility.h"
#include "megbrain/imperative/ops/autogen.h"
19 20
#include "megbrain/imperative/ops/backward_graph.h"
#include "megbrain/imperative/ops/opr_attr.h"
21 22
#include "megbrain/imperative/utils/to_string.h"

23 24 25
#include "../event_pool.h"
#include "../op_trait.h"

26 27 28 29 30
using namespace mgb;
using namespace imperative;
using namespace interpreter;
using namespace interpreter::intl;

31
#define RECORD_EVENT(type, ...) \
32 33
    if (Profiler::is_profiling()) { \
        Profiler::record<type>(type{__VA_ARGS__}); \
34 35 36
    } \


37 38 39 40 41 42 43 44 45 46
namespace {
    auto tinfo_to_tid(SmallVector<TensorInfo*> tinfo) {
        SmallVector<uint64_t> tid;
        for (auto* ptinfo: tinfo) {
            tid.push_back(ptinfo->id);
        }
        return tid;
    };
}

47 48 49 50
namespace mgb {
    using namespace profiler;
}

51 52 53 54 55
#if defined(_WIN32) || defined(_WIN64)
#define SYMBOL_EXPORT __declspec(dllexport)
#else
#define SYMBOL_EXPORT __attribute__((visibility("default")))
#endif
56 57 58 59 60 61 62

namespace mgb {

/**
 * USAGE
 *
 *   header:
63
 *     namespace mgb { void imperative_log_profile(const char* message); }
64 65 66 67 68
 *
 *   code:
 *     mgb::imperative_log_profile("MY MESSAGE");
 *
 **/
69
SYMBOL_EXPORT
70 71 72 73
void imperative_log_profile_begin(const char* message) {
    RECORD_EVENT(CustomEvent, std::string{message});
}

74
SYMBOL_EXPORT
75 76 77 78
void imperative_log_profile_end(const char* message) {
    RECORD_EVENT(CustomFinishEvent, std::string{message});
}

79
SYMBOL_EXPORT
80 81 82 83 84 85 86
void imperative_log_profile(const char* message){
    imperative_log_profile_begin(message);
    imperative_log_profile_end(message);
}

}

87 88 89 90 91 92 93 94 95 96 97 98 99 100
std::thread::id ChannelImpl::get_worker_tid() {
    return m_worker_state.tid;
}

ChannelImpl::ChannelState& ChannelImpl::get_channel_state() {
    assert_in_channel();
    return m_channel_state;
}

ChannelImpl::WorkerState& ChannelImpl::get_worker_state() {
    assert_in_worker();
    return m_worker_state;
}

101
// Do not use m_xxx_state directly
102 103 104
#define m_channel_state
#define m_worker_state

105 106 107 108 109 110 111 112 113
std::unique_ptr<Interpreter::Channel> InterpreterImpl::create_channel() {
    return std::make_unique<ChannelImpl>();
}

Interpreter& Interpreter::inst() {
    static InterpreterImpl inst_;
    return inst_;
}

114
Handle ChannelImpl::put(const HostTensorND& value, bool no_cache) {
115
    MGB_LOCK_GUARD(m_spin);
116
    mgb_assert(check_available(), "Channel already closed");
117 118 119 120 121 122 123 124
    auto& state = get_channel_state();
    state.scopes.push("Put");
    auto info = put_impl(value, no_cache);
    state.scopes.pop("Put");
    return info;
}

TensorInfo* ChannelImpl::put_impl(const HostTensorND& value, bool no_cache) {
125
    auto info = alloc();
126
    init(info, {value.layout(), value.comp_node(), value.proxy_to_default_cpu()});
127
    info->mem_desc.id = StorageIdentifier::make(++m_storage_id);
128
    info->h_value = value;
129
    m_buffer.enqueue(Put{info, value, no_cache});
130
    if (m_async_level == 0) {
131
        sync_impl();
132 133
        info->desc.comp_node.sync();
    }
134 135 136
    return info;
}

137
Handle ChannelImpl::put(const DeviceTensorND& data) {
138
    MGB_LOCK_GUARD(m_spin);
139
    auto& state = get_channel_state();
140
    mgb_assert(check_available(), "Channel already closed");
141
    state.scopes.push("Put");
M
Megvii Engine Team 已提交
142
    auto info = alloc();
143 144
    RECORD_EVENT(TensorCommandEvent, info->id, TensorCommandEvent::Put);
    init(info, {data.layout(), data.comp_node()});
145
    info->mem_desc.id = StorageIdentifier::make(++m_storage_id);
M
Megvii Engine Team 已提交
146
    info->ptr = Tensor::make(data);
147 148 149 150
    RECORD_EVENT(TensorProduceEvent, info->id, info->desc.layout, info->desc.comp_node, data.raw_ptr());
    info->status = TensorInfo::Produced;
    RECORD_EVENT(TensorCommandFinishEvent, info->id, TensorCommandFinishEvent::Put);
    state.scopes.pop("Put");
M
Megvii Engine Team 已提交
151 152 153
    return info;
}

154
void ChannelImpl::del(Handle handle) {
155
    MGB_LOCK_GUARD(m_spin);
156 157 158
    if (!check_available()){
        return;
    }
159 160 161 162
    del_impl(handle);
}

void ChannelImpl::del_impl(Handle handle) {
163 164 165 166
    mgb_assert(m_valid_handle.count(handle), "invalid handle: %p", handle);
    auto* info = reinterpret_cast<TensorInfo*>(handle);
    m_valid_handle.erase(handle);
    m_buffer.enqueue(Del{info});
167 168
}

169
void ChannelImpl::swap_in(Handle handle) {
170
    MGB_LOCK_GUARD(m_spin);
171
    mgb_assert(check_available(), "Channel already closed");
172 173
    auto& state = get_channel_state();
    if (state.options.enable_swap) {
174 175
        mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
                "invalid handle: %p", handle);
176 177
        auto* info = reinterpret_cast<TensorInfo*>(handle);
        m_buffer.enqueue(SwapIn{info});
178 179 180
    }
}

181
void ChannelImpl::swap_out(Handle handle) {
182
    MGB_LOCK_GUARD(m_spin);
183
    mgb_assert(check_available(), "Channel already closed");
184 185
    auto& state = get_channel_state();
    if (state.options.enable_swap) {
186 187
        mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
                "invalid handle: %p", handle);
188 189
        auto* info = reinterpret_cast<TensorInfo*>(handle);
        m_buffer.enqueue(SwapOut{info});
190 191 192
    }
}

193
void ChannelImpl::drop(Handle handle) {
194
    MGB_LOCK_GUARD(m_spin);
195
    mgb_assert(check_available(), "Channel already closed");
196 197
    auto& state = get_channel_state();
    if (state.options.enable_drop) {
198 199
        mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
                "invalid handle: %p", handle);
200 201
        auto* info = reinterpret_cast<TensorInfo*>(handle);
        m_buffer.enqueue(Drop{info});
202 203 204
    }
}

205
void ChannelImpl::dispatch_default_cpu(
206
        std::shared_ptr<OpDef> op,
207 208 209
        const SmallVector<TensorInfo*>& input_infos,
        const SmallVector<LogicalTensorDesc>& input_descs,
        SmallVector<Handle>* outputs) {
210
    auto& state = get_channel_state();
211 212 213 214

    auto name = op->trait()->make_name(*op);
    state.scopes.push(name);

215
    auto [output_descs, validated] = OpDef::infer_output_attrs_fallible(*op, input_descs);
216
    RECORD_EVENT(ShapeInferEvent, validated);
217

218 219 220
    SmallVector<DeviceTensorND> input_tensornds;
    input_tensornds.reserve(input_descs.size());
    CompNode output_cn;
221 222
    {
        MGB_LOCK_GUARD(m_mutex);
223
        for (auto&& info : input_infos) {
224
            auto input_cn = info->desc.comp_node;
225
            if (!output_cn.valid()) {
226 227 228 229 230 231 232
                output_cn = input_cn;
            } else {
                mgb_assert(output_cn == input_cn, "cannot decide output comp node");
            }

            if (info->ptr && info->ptr->try_get_value()) {
                input_tensornds.emplace_back(info->ptr->get_value().proxy_to_default_cpu());
233
            } else {
234
                // It's OK for SwapOut. We assign h_value before drop ptr
235 236
                mgb_assert(!info->h_value.empty(), "inp->h_value is empty!");
                input_tensornds.emplace_back(info->h_value.proxy_to_default_cpu());
237 238 239 240 241 242 243 244 245 246 247 248 249 250
            }
        }
    }

    outputs->reserve(output_descs.size());
    SmallVector<DeviceTensorND> output_tensornds;
    output_tensornds.reserve(output_descs.size());
    for (auto&& desc : output_descs) {
        // TODO: may conflict with condtake, which need alloc inside
        mgb_assert(!desc.layout.is_empty());
        // use HostTensorND alloc_host for cuda pinned memory
        output_tensornds.emplace_back(HostTensorND(output_cn, desc.layout).proxy_to_default_cpu());
    }

251
    uint64_t op_id = Profiler::next_id();
252

253 254 255 256 257 258 259
    OpDef::apply_on_device_tensornd(*op, input_tensornds, &output_tensornds);

    SmallVector<TensorInfo*> output_infos;
    output_infos.reserve(output_descs.size());
    for (auto&& tensornd : output_tensornds) {
        HostTensorND host_tensornd = HostTensorND::make_proxy(tensornd)
            .proxy_to_comp_node(output_cn);
260
        // use `put` for consistency
261
        auto info = reinterpret_cast<TensorInfo*>(put_impl(host_tensornd, false));
262
        mgb_assert(info->desc.layout.ndim != 0);
263 264 265
        output_infos.push_back(info);
        outputs->push_back(info);
    }
266 267 268 269 270 271 272 273 274
    auto op_info_getter = [op]{
        std::unordered_map<std::string, std::string> op_info;
        auto props = OpDef::props(*op);
        for (auto&& [key, value]: props) {
            op_info[key] = value;
        }
        return op_info;
    };
    RECORD_EVENT(OpDispatchEvent, op_id, op->trait()->name, op_info_getter, tinfo_to_tid(input_infos), tinfo_to_tid(output_infos));
275 276

    state.scopes.pop(name);
277
}
278

279 280 281 282 283
void ChannelImpl::dispatch_kernel(
        std::shared_ptr<OpDef> op,
        const SmallVector<TensorInfo*>& input_infos,
        const SmallVector<LogicalTensorDesc>& input_descs,
        SmallVector<Handle>* outputs) {
284
    auto& state = get_channel_state();
285 286 287 288 289
    auto& options = state.options;

    auto name = op->trait()->make_name(*op);
    state.scopes.push(name);

290
    auto [output_descs, validated] = OpDef::infer_output_attrs_fallible(*op, input_descs);
291
    RECORD_EVENT(ShapeInferEvent, validated);
292

293
    ApplyOp cmd{Profiler::next_id(), std::move(op)};
294
    cmd.inputs = std::move(input_infos);
295
    cmd.outputs.reserve(output_descs.size());
296
    outputs->reserve(output_descs.size());
297 298
    for (int i = 0; i < output_descs.size(); ++i) {
        auto&& desc = output_descs[i];
299
        auto info = alloc();
300
        init(info, desc);
301 302 303 304 305
        // make sure desc's value is consistent with h_value
        if (!info->desc.value.empty()) {
            info->h_value = HostTensorND::make_proxy(desc.value)
                .proxy_to_comp_node(desc.comp_node);
        }
306
        cmd.outputs.push_back(info);
307
        outputs->push_back(info);
308
    }
309 310 311 312 313 314 315 316 317
    auto op_info_getter = [op=cmd.op]{
        std::unordered_map<std::string, std::string> op_info;
        auto props = OpDef::props(*op);
        for (auto&& [key, value]: props) {
            op_info[key] = value;
        }
        return op_info;
    };
    RECORD_EVENT(OpDispatchEvent, cmd.id, cmd.op->trait()->name, op_info_getter, tinfo_to_tid(cmd.inputs), tinfo_to_tid(cmd.outputs));
318
    m_buffer.enqueue(std::move(cmd));
319
    if (!validated && options.async_level == 1) {
320
        sync_impl();
321
    } else if (options.async_level == 0) {
322
        sync_impl();
323
        // check device error
324
        for (auto&& oup : *outputs) {
325 326
            auto info = reinterpret_cast<TensorInfo*>(oup);
            info->ptr->comp_node().sync();
327
        }
328
    }
329
    state.scopes.pop(name);
330 331 332 333 334
}

SmallVector<Handle> ChannelImpl::apply_op(
        std::shared_ptr<OpDef> op,
        const SmallVector<Handle>& inputs) {
335
    MGB_LOCK_GUARD(m_spin);
336
    mgb_assert(check_available(), "Channel already closed");
337
    auto& state = get_channel_state();
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    for (auto i : inputs) {
        mgb_assert(m_valid_handle.find(i) != m_valid_handle.end(),
                "invalid handle: %p", i);
    }
    SmallVector<TensorInfo*> input_infos;
    input_infos.reserve(inputs.size());
    SmallVector<LogicalTensorDesc> input_descs;
    input_descs.reserve(inputs.size());
    {
        MGB_LOCK_GUARD(m_mutex);
        for (auto i : inputs) {
            auto info = reinterpret_cast<TensorInfo*>(i);
            mgb_assert(!info->invalid, "Invalid tensor, unable to apply_op!");
            input_infos.push_back(info);
            input_descs.push_back(info->desc);
        }
    }

    SmallVector<Handle> outputs;
357
    DispatchMode dispatch_mode = state.options.enable_host_compute
358 359 360
            ? OpDef::decide_dispatch_mode(*op, input_descs)
            : DispatchMode::KERNEL;
    switch (dispatch_mode) {
361 362 363 364 365 366 367 368 369
        case DEFAULT_CPU: {
            dispatch_default_cpu(op, input_infos, input_descs, &outputs);
            break;
        }
        case KERNEL: {
            dispatch_kernel(op, input_infos, input_descs, &outputs);
            break;
        }
    }
370 371 372
    return outputs;
}

373
HostTensorND ChannelImpl::get_value(Handle handle) {
374
    MGB_LOCK_GUARD(m_spin);
375
    mgb_assert(check_available(), "Channel already closed");
376 377 378
    mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
               "invalid handle: %p", handle);
    auto info = reinterpret_cast<TensorInfo*>(handle);
379 380
    // donnot use info->value_fetched, it's unsafe
    mgb_assert(!info->invalid, "Invalid tensor, unable to get_value!");
381
    return wait_tensor(info, TensorProp::HostValue)->get_value();
382 383
}

384
TensorShape ChannelImpl::get_shape(Handle handle) {
385
    MGB_LOCK_GUARD(m_spin);
386
    mgb_assert(check_available(), "Channel already closed");
387 388 389 390 391 392
    mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
               "invalid handle: %p", handle);
    auto info = reinterpret_cast<TensorInfo*>(handle);
    if (info->desc.layout.ndim != 0) {
        return info->desc.layout;
    }
393
    TensorShape ret = wait_tensor(info, TensorProp::Shape)->layout();
394 395 396 397
    mgb_assert(ret.ndim != 0);
    return ret;
}

398
DType ChannelImpl::get_dtype(Handle handle) {
399
    MGB_LOCK_GUARD(m_spin);
400
    mgb_assert(check_available(), "Channel already closed");
401 402 403
    mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
               "invalid handle: %p", handle);
    auto info = reinterpret_cast<TensorInfo*>(handle);
404
    RECORD_EVENT(TensorGetPropEvent, info->id, TensorProp::DType);
405 406 407 408 409
    auto ret = info->desc.layout.dtype;
    mgb_assert(ret.valid());
    return ret;
}

410
CompNode ChannelImpl::get_device(Handle handle) {
411
    MGB_LOCK_GUARD(m_spin);
412
    mgb_assert(check_available(), "Channel already closed");
413 414 415
    mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
               "invalid handle: %p", handle);
    auto info = reinterpret_cast<TensorInfo*>(handle);
416
    RECORD_EVENT(TensorGetPropEvent, info->id, TensorProp::Device);
417 418 419 420 421
    auto ret = info->desc.comp_node;
    mgb_assert(ret.valid());
    return ret;
}

422
DeviceTensorND ChannelImpl::get_dev_tensor(Handle handle) {
423
    MGB_LOCK_GUARD(m_spin);
424
    mgb_assert(check_available(), "Channel already closed");
425 426 427
    mgb_assert(m_valid_handle.find(handle) != m_valid_handle.end(),
               "invalid handle: %p", handle);
    auto info = reinterpret_cast<TensorInfo*>(handle);
428
    return wait_tensor(info, TensorProp::DevValue)->dev_tensor();
429 430 431
}

void ChannelImpl::sync() {
432
    MGB_LOCK_GUARD(m_spin);
433
    mgb_assert(check_available(), "Channel already closed");
434 435 436 437
    sync_impl();
}

void ChannelImpl::sync_impl() {
438
    m_buffer.flush();
439 440 441 442 443 444
    m_worker.wait_all_task_finish();
    MGB_LOCK_GUARD(m_mutex);
    check_worker_exc_unsafe();
}

void ChannelImpl::close() {
445
    MGB_LOCK_GUARD(m_spin);
446 447 448 449 450
    if (!check_available()) {
        return;
    }
    std::vector<Handle> valid_handles(m_valid_handle.begin(), m_valid_handle.end());
    for (auto* handle: valid_handles) {
451
        del_impl(handle);
452 453 454
    }
    mgb_assert(m_valid_handle.empty());
    mgb_log_debug("%ld tensor exists before channel close", (long)valid_handles.size());
455
    sync_impl();
456
    m_closed = true;
457 458
}

459
size_t ChannelImpl::get_option(std::string name) {
460
    MGB_LOCK_GUARD(m_spin);
461
    mgb_assert(check_available(), "Channel already closed");
462 463
    auto& state = get_channel_state();
    return state.options.get_option(name);
464 465
}

466
void ChannelImpl::set_option(std::string name, size_t value) {
467
    MGB_LOCK_GUARD(m_spin);
468
    mgb_assert(check_available(), "Channel already closed");
469 470
    auto& state = get_channel_state();
    state.options.set_option(name, value);
471
    m_buffer.enqueue(SetOption{name, value});
472 473 474
}

TensorInfo* ChannelImpl::alloc() {
475
    auto& state = get_channel_state();
476 477 478 479 480 481 482 483
    auto info = [this]{
        MGB_LOCK_GUARD(m_mutex);
        return m_pool.alloc();
    }();
    info->id = Profiler::next_id();
    if (Profiler::is_profiling()) {
        info->name = state.scopes.next_tensor_name();
    }
484
    return info;
485 486
}

487 488 489 490 491
void ChannelImpl::init(TensorInfo* info, LogicalTensorDesc desc) {
    m_valid_handle.insert(info);
    RECORD_EVENT(TensorDeclareEvent, info->id, info->name);
    info->status = TensorInfo::Allocated;
    info->desc = std::move(desc);
492 493 494
    info->mem_desc.layout = info->desc.layout;
    info->mem_desc.cn = info->desc.comp_node;
    info->mem_desc.offset = 0;
495 496
}

497 498 499 500 501 502 503 504 505 506 507 508

void ChannelImpl::do_drop(TensorInfo* ptr, bool user=false) {
    if (!ptr->producer) {
        if (user) {
            mgb_log_warn("the input that produced tensor %p has been deleted, this drop operation will be ignored", ptr);
        }
        return;
    }
    if (ptr->evict_type != EvictType::NONE) {
        return;
    }
    ptr->evict_type = EvictType::DROP;
509
    ptr->status = TensorInfo::Dropped;
510 511 512
    release_tensor(ptr);
}

513
void ChannelImpl::free(TensorInfo* ptr) {
514 515
    auto& state = get_worker_state();
    if (state.options.enable_dtr_auto_drop) {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        // Evicting a tensor, rather than freeing it, can avoid pinning
        // potentially exploding amounts of memory and allow us to save
        // more memory.
        ptr->allow_delete = true;
        if (!ptr->ref_cnt) {
            recursive_free(ptr);
        } else {
            do_drop(ptr);
        }
    } else {
        real_free(ptr);
    }
}

void ChannelImpl::recursive_free(TensorInfo* ptr) {
531 532
    RECORD_EVENT(TensorCommandEvent, ptr->id, TensorCommandEvent::RecFree);
    SmallVector<TensorInfo*> inps;
533 534 535 536 537 538 539 540 541 542 543 544 545
    if (ptr->producer) {
        for (auto i : ptr->producer->inputs) {
            if (i && --i->ref_cnt == 0) {
                inps.push_back(i);
            }
        }
    }
    real_free(ptr);
    for (auto i : inps) {
        if (i->allow_delete) {
            recursive_free(i);
        }
    }
546
    RECORD_EVENT(TensorCommandFinishEvent, ptr->id, TensorCommandFinishEvent::RecFree);
547 548 549
}

void ChannelImpl::real_free(TensorInfo* ptr) {
550 551
    auto& state = get_worker_state();
    if (ptr->size_exceeds_thd(state.options.dtr_evictee_minimum_size)) {
552 553 554 555
        m_dtr.erase_candidate(ptr);
    }
    detach_users(ptr);
    ptr->detach_producer();
556 557 558 559 560 561
    bool has_value = ptr->ptr != nullptr;
    if (has_value) {
        RECORD_EVENT(TensorReleaseEvent, ptr->id);
    }
    RECORD_EVENT(TensorEraseEvent, ptr->id, ptr->ptr_use_count);
    ptr->status = TensorInfo::Deleted;
562
    MGB_LOCK_GUARD(m_mutex);
563 564 565
    m_pool.free(ptr);
}

566
ChannelImpl::ChannelImpl() : m_worker(this), m_buffer(this){}
567

568 569 570
ChannelImpl::~ChannelImpl() {
    close();
}
571

572
void ChannelImpl::produce_tensor(TensorInfo* dest, TensorPtr ptr) {
573
    auto& state = get_worker_state();
574
    MGB_LOCK_GUARD(m_mutex);
575
    m_dtr.update_used_time(dest);
576
    RECORD_EVENT(TensorProduceEvent, dest->id, ptr->layout(), ptr->comp_node(), ptr->dev_tensor().raw_ptr());
577 578 579
    // update tensor desc for static infer
    dest->desc.layout = ptr->layout();
    dest->desc.comp_node = ptr->comp_node();
580
    dest->memory = ptr->blob()->size();
581
    dest->ptr = std::move(ptr);
582
    dest->evict_type = EvictType::NONE;
583
    dest->status = TensorInfo::Produced;
584
    if (dest->size_exceeds_thd(state.options.dtr_evictee_minimum_size)) {
585 586
        m_dtr.insert_candidate(dest);
    }
587
    notify_tensor_unsafe(dest);
588 589
}

590
void ChannelImpl::release_tensor(TensorInfo* dest) {
591
    RECORD_EVENT(TensorReleaseEvent, dest->id);
592 593 594 595
    MGB_LOCK_GUARD(m_mutex);
    dest->ptr.reset();
}

596
void ChannelImpl::regenerate(TensorInfo* dest) {
597
    RECORD_EVENT(TensorCommandEvent, dest->id, TensorCommandEvent::ReGen);
598
    if (dest->evict_type == EvictType::DROP) {
599
        recompute(dest->producer);
600 601
    } else if (dest->evict_type == EvictType::SWAP) {
        produce_tensor(dest, Tensor::make(dest->h_value));
602
    }
603
    RECORD_EVENT(TensorCommandFinishEvent, dest->id, TensorCommandFinishEvent::ReGen);
604 605
}

606 607 608
void ChannelImpl::do_apply_op(const ApplyOp& cmd) {
    using namespace ranges;
    using namespace ranges::views;
609
    auto& state = get_worker_state();
610
    bool profiling_device = Profiler::is_profiling() && Profiler::get_option("profile_device", 0);
611 612
    uint64_t apply_id = cmd.id;
    SmallVector<TensorPtr> tensor_inputs;
613
    SmallVector<MemoryDesc> input_memory_desc;
614 615 616 617 618
    if (state.options.enable_dtr_auto_drop) {
        m_dtr.pin(cmd.inputs); 
    }
    for (auto i : cmd.inputs) {
        if (!i->ptr && i->evict_type != EvictType::NONE) {
619 620 621 622
            regenerate(i);
        }
        m_dtr.update_used_time(i);
    }
623 624 625 626
    tensor_inputs.reserve(cmd.inputs.size());
    // refcnt == 1, owners: [TensorInfo::ptr]
    for (auto i : cmd.inputs) {
        mgb_assert(i->ptr, "Invalid input tensor ptr!");
627
        mgb_assert(i->mem_desc.id, "Invalid input tensor mem desc!");
628
        // refcnt ++, owners: [i->ptr, tensor_inputs]
629
        tensor_inputs.push_back(i->ptr);
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        input_memory_desc.push_back(i->mem_desc);
    }
    // SmallVector<MemoryDesc> outputs_mem_desc;
    // SmallVector<TensorPtr> tensor_outputs, workspaces;
    auto [outputs_mem_desc, tensor_outputs, workspaces] = init_output_and_workspace(*cmd.op, tensor_inputs, input_memory_desc);
    if (outputs_mem_desc.size()) {
        for (size_t i = 0;i < outputs_mem_desc.size();i ++) {
            if (cmd.outputs[i]) {
                cmd.outputs[i]->mem_desc = outputs_mem_desc[i];
            }
        }
    } else {
        // fail to infer mem plan
        for (auto && out : cmd.outputs) {
            if (out) {
                out->mem_desc.id = StorageIdentifier::make();  
            }
        }
648
    }
649
    RECORD_EVENT(OpExecuteEvent, apply_id);
650
    // Begin profiling operator
651 652 653 654
    SmallVector<std::pair<CompNode, uint64_t>> kernels;
    if (profiling_device) {
        // Collecting devices
        SmallVector<CompNode> devices;
655 656 657
        for (auto&& i : concat(cmd.inputs, cmd.outputs)) {
            if (i != nullptr && count(devices, i->desc.comp_node) == 0) {
                devices.push_back(i->desc.comp_node);
658
                kernels.push_back({i->desc.comp_node, Profiler::next_id()});
659 660 661
            }
        }
    }
662 663 664 665 666 667 668 669 670
    for (auto* input: cmd.inputs) {
        auto input_id = input->id;
        RECORD_EVENT(OpInputEvent, input_id);
        RECORD_EVENT(TensorUsageEvent, input_id);
        RECORD_EVENT(OpInputFinishEvent, input_id);
    }
    // Fused by command buffer. @see: CommandBuffer::fuse_del
    // Now if dest is inplacable, it's refcnt would be decreased to 1 and owned by tensor_inputs after Del.
    // Note for exprs like 'y = x op x', inplace is unsupported yet but Del would be also fused.
671
    for (auto* del : cmd.dels) {
672 673 674 675
        // refcnt --, owners: [tensor_inputs]
        // if it's decreased to 1, would be detected at @see: proxy_graph_detail::apply_on_physical_tensor
        uint64_t del_id = del->id;
        RECORD_EVENT(OpDelEvent, del_id);
676
        free(del);
677
        RECORD_EVENT(OpDelFinishEvent, del_id);
678
    }
679 680 681 682 683
    // Before wait
    //TODO: split operator wait and execute so that OpWait could be corrected recorded.
    // Before execute
    for (auto&& [device, kernel_id]: kernels) {
        RECORD_EVENT(KernelExecuteEvent, apply_id, kernel_id, Timer::record_event(device));
684
    }
685
    if (state.options.enable_dtr_auto_drop && state.options.dtr_eviction_threshold > 0) {
686 687
        auto_evict();
    }
688 689
    // Apply op
    // Here std::move is REQUIRED for removing duplicated references.
690 691 692 693 694 695 696
    if (outputs_mem_desc.size()) {
        OpDef::execute(
            *cmd.op, std::move(tensor_inputs), tensor_outputs, std::move(workspaces));
    } else {
        tensor_outputs = OpDef::apply_on_physical_tensor(
            *cmd.op, std::move(tensor_inputs));
    }
697
    // After execute
698 699
    for (auto&& [device, kernel_id]: kernels) {
        RECORD_EVENT(KernelExecuteFinishEvent, apply_id, kernel_id, Timer::record_event(device));
700 701 702 703 704
    }
    // End profiling operator
    mgb_assert(tensor_outputs.size() == cmd.outputs.size());
    for (size_t i = 0; i < tensor_outputs.size(); ++i) {
        auto output = cmd.outputs[i];
705 706 707 708 709 710 711 712
        if (output == nullptr) {
            RECORD_EVENT(OpOutputEvent, 0);
            RECORD_EVENT(OpOutputFinishEvent, 0);
        } else if (output->ptr != nullptr) {
            RECORD_EVENT(OpOutputEvent, output->id);
            RECORD_EVENT(OpOutputFinishEvent, output->id);
        } else {
            RECORD_EVENT(OpOutputEvent, output->id);
713
            produce_tensor(output, tensor_outputs[i]);
714 715
            RECORD_EVENT(OpOutputFinishEvent, output->id);
            sample_on_device(output->desc.comp_node, false);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
        }
    }

    if (state.options.enable_dtr_auto_drop) {
        double estimate_compute_time = 0;
        for (auto i : cmd.inputs) {
            estimate_compute_time += i->memory;
        }
        for (auto i : tensor_outputs) {
            estimate_compute_time += i->blob()->size();
        }
        m_dtr.estimate_timestamp += estimate_compute_time / 1e8;
        for (auto i : cmd.outputs) {
            if (i != nullptr) {
                i->compute_time = estimate_compute_time;
            }
        }
        m_dtr.unpin(cmd.inputs);
    }
735 736
    RECORD_EVENT(OpExecuteFinishEvent, apply_id);
    // End profiling operator
737
}
738
        
739 740 741 742
void ChannelImpl::recompute(TensorInfo::ComputePath* path) {
    auto& state = get_worker_state();
    do_apply_op(ApplyOp{path->id, path->op, path->inputs, path->outputs, {}});
    for (size_t i = 0;i < path->outputs.size();i ++) {
743 744 745 746
        auto&& o = path->outputs[i];
        if (o) {
            o->recompute_times ++;
            if (!o->ptr) {
747
                if (state.options.enable_dtr_auto_drop) {
748 749 750 751
                    m_dtr.update_dsu_after_recompute(o);
                }
            }
        }
752
    }
753 754 755
}

void ChannelImpl::auto_evict() {
756
    auto& state = get_worker_state();
757 758 759 760
    if (!m_dtr.comp_node.valid()) {
        return;
    }
    size_t current_memory = m_dtr.comp_node.get_used_memory();
761
    while (current_memory > state.options.dtr_eviction_threshold) {
762
        RECORD_EVENT(AutoEvictEvent);
763
        sample_on_device(m_dtr.comp_node, false);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
        auto best = m_dtr.find_best_tensor();
        if (!best) {
            if (!m_dtr.warn_printed) {
                m_dtr.warn_printed = true;
                mgb_log_warn("No tensors on %s can be evicted automatically "
                             "when memory usage is %.0lfMB. Maybe memory "
                             "budget is too small.",
                              m_dtr.comp_node.to_string().c_str(),
                              current_memory / 1024.0 / 1024.0);
            }
            break;
        }
        if (best->ptr.unique() && best->ptr->blob().unique()) {
            current_memory -= best->memory;
        }
        do_drop(best);
        if (best->evict_type == EvictType::DROP) {
            m_dtr.update_dsu_after_evict(best);
782
        }
783
        sample_on_device(m_dtr.comp_node, false);
784
        RECORD_EVENT(AutoEvictFinishEvent);
785 786 787
    }
}

788 789 790
void ChannelImpl::detach_users(TensorInfo* dest) {
    SmallVector<TensorInfo::ComputePath*> users = dest->users;
    for (auto* user: users) {
791 792 793
        SmallVector<TensorInfo*> outputs = user->outputs;
        SmallVector<TensorInfo*> inputs = user->inputs;
        for (auto* output: outputs) {
794 795 796 797
        // When a `ComputePath` is detach from it's input,
        // there is no need to reserve it,
        // so we detach all output of this path
        // to decrease it's `ref_cnt` to zero.
798 799 800 801 802
            if (output == nullptr) {
                continue;
            }
            regenerate(output);
            output->detach_producer();
803 804 805
            for (auto* input: inputs) {
                input->ref_cnt --;
            }
806
        }
807
        // now user is dead
808
    }
809
    mgb_assert(dest->users.empty(), "ComputePath leaking");
810 811
}

812 813 814 815
bool ChannelImpl::check_available() {
    return !m_closed;
}

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
TensorPtr ChannelImpl::wait_tensor(TensorInfo* info, TensorProp prop) {
    m_buffer.flush();
    std::unique_lock<decltype(m_mutex)> lock(m_mutex);
    mgb_assert(!m_waitee, "duplicate waitee");
    m_waitee = info;
    m_waitee_id = Profiler::next_id();
    RECORD_EVENT(TensorWaitPropEvent, info->id, m_waitee_id, prop);
    bool require_host = prop == TensorProp::HostValue;
    bool value_fetching = false;
    m_cv.wait(lock, [&]() {
        check_worker_exc_unsafe();
        if (require_host) {
            if (info->ptr && info->ptr->value_fetched()) {
                return true;
            }
            if (!value_fetching) {
                m_buffer.enqueue(GetValue{info});
833
                m_buffer.flush();
834 835 836
                value_fetching = true;
            }
            return false;
837
        } else {
838
            return static_cast<bool>(info->ptr);
839
        }
840 841
    });
    RECORD_EVENT(TensorWaitPropFinishEvent, info->id, m_waitee_id, prop, m_waitee == nullptr);
842
    m_waitee = nullptr;
843 844 845 846 847 848 849
    return info->ptr;
}

void ChannelImpl::notify_tensor_unsafe(TensorInfo* info) {
    if (info == m_waitee) {
        RECORD_EVENT(TensorNotifyPropEvent, info->id);
        m_cv.notify_all();
850
    }
851 852 853 854 855 856 857
}

std::unordered_set<TensorInfo*> ChannelImpl::collect_valid_tensors() {
    std::unordered_set<TensorInfo*> valid_tensors;
    for (auto* handle: m_valid_handle) {
        auto* info = reinterpret_cast<TensorInfo*>(handle);
        valid_tensors.insert(info);
858
    }
859
    return valid_tensors;
860 861
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
std::tuple<SmallVector<MemoryDesc>, SmallVector<TensorPtr>, SmallVector<TensorPtr>> ChannelImpl::init_output_and_workspace(
        const OpDef& def,
        SmallVector<TensorPtr> inputs,
        SmallVector<MemoryDesc> inputs_mem_desc) {

    auto [outputs_desc, workspaces_desc] = OpDef::infer_output_mem_desc(def, inputs, inputs_mem_desc);
    if (!outputs_desc.size()) {
        // failed to infer memplan
        return {{}, {}, {}};
    }
    // refine storage id to make it unique
    for (auto&& desc : outputs_desc) {
        if (desc.id->is_sys_alloc()) {
            // TODO: there may be some outputs sharing the same storage id
            desc.id->id = ++ m_storage_id;
        }
    }
    auto alloc_storage = [&](SmallVector<MemoryDesc>& desc) {
        SmallVector<TensorPtr> tensors;
        for (size_t i = 0; i < desc.size(); i ++) {
            if (desc[i].id->is_sys_alloc()) {
                tensors.push_back(Tensor::make(desc[i].layout, desc[i].cn));
            } else if (desc[i].id->is_from_other()) {
                for (size_t j = 0; j < inputs_mem_desc.size();j ++) {
                    if (inputs_mem_desc[j].id->desc == desc[i].id->desc) {
                        tensors.push_back(inputs[j]->sub(desc[i].offset, desc[i].layout));
                        break;
                    }
                }
            } else if (desc[i].id->is_device_ptr()) {
                tensors.push_back(desc[i].id->ptr);
            } else {
                mgb_assert(0, "not implemented");
            }
        }
        return tensors;
    };
    
    return {outputs_desc, alloc_storage(outputs_desc), alloc_storage(workspaces_desc)};
}

903
void ChannelImpl::process_one_task(IdentifiedCommand& icmd) {
904 905
    using namespace ranges;
    using namespace ranges::views;
906
    auto& state = get_worker_state();
907
    auto& options = state.options;
908
    //TODO: remove std::visit for support osx 10.12
909 910
    auto cmd_visitor = [&](const auto& cmd) {
            using T = std::decay_t<decltype(cmd)>;
911
            if constexpr (std::is_same_v<T, Put>) {
912
                RECORD_EVENT(TensorCommandEvent, cmd.dest->id, TensorCommandEvent::Put);
913 914
                auto value = cmd.no_cache ? std::make_shared<Tensor>(cmd.value) : Tensor::make(cmd.value);
                produce_tensor(cmd.dest, std::move(value));
915 916
                RECORD_EVENT(TensorCommandFinishEvent, cmd.dest->id, TensorCommandFinishEvent::Put);
                sample_on_device(cmd.dest->desc.comp_node, false);
917
            } else if constexpr (std::is_same_v<T, ApplyOp>) {
918 919 920 921
                do_apply_op(cmd);
                for (size_t i = 0; i < cmd.outputs.size(); ++i) {
                    auto output = cmd.outputs[i];
                    if (output == nullptr) {
922 923
                        continue;
                    }
924
                    if (state.options.enable_dtr_auto_drop) {
925
                        output->dsu_ptr = std::make_shared<DsuNode>(output->compute_time);
926 927
                    }
                }
928 929 930 931 932 933
                if (state.options.enable_drop && state.options.record_computing_path) {
                    auto is_inplace = [](std::tuple<TensorInfo*, TensorInfo*> tuple2) {
                        auto& input = std::get<0>(tuple2);
                        auto& output = std::get<1>(tuple2);
                        if (!input->ptr || !output->ptr) {
                            return false;
934
                        }
935 936
                        return input->ptr->blob()->storage() == output->ptr->blob()->storage();
                    };
937 938 939 940 941 942 943
                    // FIXME: do not use opname as identifier
                    auto get_name = [](const OpDef& opdef) {
                        if (auto attr = opdef.try_cast_final<OprAttr>()) {
                            return attr->type.c_str();
                        }
                        return opdef.dyn_typeinfo()->name;
                    };
944 945 946 947 948 949 950

                    auto is_cross_cn = [comp_node=m_dtr.comp_node](TensorInfo* info){
                        return info->desc.comp_node != comp_node;
                    };

                    bool cross_cn = any_of(concat(cmd.inputs, cmd.outputs), is_cross_cn);
                    bool inplace = any_of(cartesian_product(cmd.inputs, cmd.outputs), is_inplace);
951

952 953
                    if (!inplace && !cross_cn && !m_dtr.is_bad_op(get_name(*cmd.op))) {
                        TensorInfo::ComputePath::make(cmd.id, cmd.op, cmd.inputs, cmd.outputs);
954 955
                        size_t detach_cnt = 0;
                        for (auto output : cmd.outputs) {
956
                            if (!output->size_exceeds_thd(state.options.dtr_evictee_minimum_size)) {
957 958 959 960 961 962 963 964
                                output->detach_producer();
                                detach_cnt ++;
                            }
                        }
                        for (auto input : cmd.inputs) {
                            input->ref_cnt -= detach_cnt;
                        }
                    }
965 966
                }
            } else if constexpr (std::is_same_v<T, Del>) {
967 968 969
                RECORD_EVENT(TensorCommandEvent, cmd.dest->id, TensorCommandEvent::Del);
                CompNode device = cmd.dest->desc.comp_node;
                uint64_t tensor_id = cmd.dest->id;
970
                free(cmd.dest);
971 972
                RECORD_EVENT(TensorCommandFinishEvent, tensor_id, TensorCommandFinishEvent::Del);
                sample_on_device(device, false);
973
            } else if constexpr (std::is_same_v<T, GetValue>) {
974
                imperative_log_profile_begin("GetValue");
975 976 977
                if (!cmd.dest->ptr && cmd.dest->evict_type != EvictType::NONE) {
                    regenerate(cmd.dest);
                }
978
                mgb_assert(cmd.dest->ptr, "Invalid tensor ptr!");
979 980
                cmd.dest->ptr->fetch_value();
                MGB_LOCK_GUARD(m_mutex);
981
                notify_tensor_unsafe(cmd.dest);
982
                imperative_log_profile_end("GetValue");
983
            } else if constexpr (std::is_same_v<T, SwapIn>) {
984
                RECORD_EVENT(TensorCommandEvent, cmd.dest->id, TensorCommandEvent::SwapIn);
985
                produce_tensor(cmd.dest, Tensor::make(cmd.dest->h_value));
986 987
                RECORD_EVENT(TensorCommandFinishEvent, cmd.dest->id, TensorCommandFinishEvent::SwapIn);
                sample_on_device(cmd.dest->desc.comp_node, false);
988
            } else if constexpr (std::is_same_v<T, SwapOut>) {
989
                RECORD_EVENT(TensorCommandEvent, cmd.dest->id, TensorCommandEvent::SwapOut);
990
                cmd.dest->h_value = cmd.dest->ptr->get_value();
991 992
                if (cmd.dest->evict_type == EvictType::NONE) {
                    cmd.dest->evict_type = EvictType::SWAP;
993 994
                    cmd.dest->status = TensorInfo::Swapped;
                    release_tensor(cmd.dest);
995
                }
996 997
                RECORD_EVENT(TensorCommandFinishEvent, cmd.dest->id, TensorCommandFinishEvent::SwapOut);
                sample_on_device(cmd.dest->desc.comp_node, false);
998
            } else if constexpr (std::is_same_v<T, Drop>) {
999
                RECORD_EVENT(TensorCommandEvent, cmd.dest->id, TensorCommandEvent::Drop);
1000
                do_drop(cmd.dest, true);
1001
                RECORD_EVENT(TensorCommandFinishEvent, cmd.dest->id, TensorCommandFinishEvent::Drop);
1002
            } else if constexpr (std::is_same_v<T, SetOption>) {
1003
                options.set_option(cmd.key, cmd.value);
1004
            } else if constexpr (std::is_same_v<T, StartProfile>) {
1005
                RECORD_EVENT(StartProfileEvent);
1006
                CompNode::sync_all();
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
                for (auto* info: cmd.capture_tensors) {
                    RECORD_EVENT(TensorDeclareEvent, info->id, info->name);
                    if (info->status == TensorInfo::Produced) {
                        // TODO: handle swap/drop
                        RECORD_EVENT(TensorProduceEvent, info->id, info->desc.layout, info->desc.comp_node, info->ptr->dev_tensor().raw_ptr());
                    }
                }
                CompNode::foreach([&](CompNode device){
                    if (Profiler::get_option("sample_rate", 0)) {
                        sample_on_device(device, true);
                    }
                });
                RECORD_EVENT(StartProfileFinishEvent);
1020
            } else if constexpr (std::is_same_v<T, StopProfile>) {
1021 1022 1023 1024 1025 1026 1027
                RECORD_EVENT(StopProfileEvent);
                for (auto* info: cmd.escape_tensors) {
                    bool has_value = info->status == TensorInfo::Produced;
                    if (has_value) {
                        RECORD_EVENT(TensorReleaseEvent, info->id);
                    }
                    RECORD_EVENT(TensorEraseEvent, info->id);
1028
                }
1029 1030 1031
                CompNode::foreach([&](CompNode device){
                    if (Profiler::get_option("sample_rate", 0)) {
                        sample_on_device(device, true);
1032
                    }
1033 1034
                });
                RECORD_EVENT(StopProfileFinishEvent);
1035
            } else if constexpr (std::is_same_v<T, PushScope>) {
1036
                RECORD_EVENT(ScopeEvent, cmd.scope_name);
1037
            } else if constexpr (std::is_same_v<T, PopScope>) {
1038
                RECORD_EVENT(ScopeFinishEvent, cmd.scope_name);
1039
            } else {
1040
                static_assert(!std::is_same_v<T, T>);
1041
            }
1042
    };
1043
    std::visit([&](const auto& cmd){
1044
        using T = std::decay_t<decltype(cmd)>;
1045
        if (!options.catch_worker_execption) {
1046 1047 1048 1049 1050
            cmd_visitor(cmd);
            return;
        }
        try {
            cmd_visitor(cmd);
1051 1052
        } catch (...) {
            MGB_LOCK_GUARD(m_mutex);
1053 1054 1055 1056 1057 1058 1059
            if constexpr (std::is_same_v<T, ApplyOp>) {
                for (auto oup : cmd.outputs) {
                    oup->invalid = true;
                }
            } else if constexpr (std::is_same_v<T, Put>) {
                cmd.dest->invalid = true;
            }
1060
            m_worker_exc = std::current_exception();
1061 1062 1063 1064
            RECORD_EVENT(WorkerExceptionEvent);
            if (m_waitee) {
                notify_tensor_unsafe(m_waitee);
            }
1065
        }
1066
    }, icmd.second);
1067 1068 1069 1070
}

void ChannelImpl::check_worker_exc_unsafe() {
    if (m_worker_exc) {
1071 1072
        // for reuse interpreter_for_py after some exception tests
        m_waitee = nullptr;
1073 1074 1075 1076 1077
        std::exception_ptr exc;
        std::swap(exc, m_worker_exc);
        std::rethrow_exception(exc);
    }
}
1078 1079 1080 1081 1082

void ChannelImpl::CommandBuffer::enqueue(Command cmd) {
    if (std::get_if<Del>(&cmd) && fuse_del(std::get<Del>(cmd))) {
        return;
    }
1083
    // mgb_log_debug("%s Enqueued", to_string(cmd).c_str());
1084 1085 1086 1087 1088
    m_commands.push_back(std::move(cmd));
    auto flush_pos = flush_pos_for(m_commands.back());
    flush(flush_pos);
}

1089 1090 1091 1092
void ChannelImpl::CommandBuffer::flush() {
    flush(m_commands.end());
}

1093 1094
void ChannelImpl::CommandBuffer::flush(Handle pos) {
    for (auto iter = m_commands.begin(); iter != pos; ++iter) {
1095 1096 1097 1098
        if (Profiler::is_profiling()) {
            mgb_log_debug("%s Flushed", to_string(*iter).c_str());
        }
        m_owner->m_worker.add_task(IdentifiedCommand{Profiler::next_id(), std::move(*iter)});
1099 1100 1101 1102 1103
    }
    m_commands.erase(m_commands.begin(), pos);
}

auto ChannelImpl::CommandBuffer::flush_pos_for(const Command& cmd) -> Handle {
1104
    auto& state = m_owner->get_channel_state();
1105
    return std::visit([this, &state](const auto& cmd) {
1106 1107 1108 1109 1110 1111 1112
        using T = std::decay_t<decltype(cmd)>;
        if constexpr (std::is_same_v<T, ApplyOp>) {
            auto* op_type = cmd.op->dyn_typeinfo();
            if (op_type == RemoteRecv::typeinfo() ||
                op_type == RemoteSend::typeinfo() ||
                op_type == CollectiveComm::typeinfo() ||
                op_type == opr::InputCallback::typeinfo() ||
1113
                op_type == opr::OutputCallback::typeinfo()) {
1114 1115 1116 1117 1118
                return m_commands.end();
            }
        } else if constexpr (std::is_same_v<T, GetValue>) {
            return m_commands.end();
        }
1119
        size_t buffer_length = state.options.buffer_length;
1120 1121
        if (m_commands.size() > buffer_length) {
            return m_commands.begin() + (m_commands.size() - buffer_length);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
        }
        return m_commands.begin();
    }, cmd);
}

/**
 * 1. Find ApplyOp(dest) in buffered commands
 * 2. Check if there are other usages between ApplyOp and Del, return false if not
 * 3. Fuse Del into ApplyOp, return true
 */
bool ChannelImpl::CommandBuffer::fuse_del(const Del& cmd) {
    auto* dest = cmd.dest;
    // TODO: eliminate Puts
    auto begin = m_commands.begin(), end = m_commands.end();
    auto apply_iter = std::find_if(begin, end, [dest](const Command& cmd){
        if (auto* apply = std::get_if<ApplyOp>(&cmd)) {
            return std::count(apply->inputs.begin(), apply->inputs.end(), dest) > 0;
        }
        return false;
    });
    if (apply_iter == end || find_last_usage(dest, {apply_iter+1, end}) != end) {
        return false;
    }
1145
    // mgb_log_debug("%s Fused", to_string(Command{cmd}).c_str());
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    std::get<ApplyOp>(*apply_iter).dels.push_back(dest);
    return true;
}

auto ChannelImpl::CommandBuffer::find_last_usage(TensorInfo* dest, Range range)
        -> Handle {
    auto found = range[1];
    for (auto iter = range[0]; iter != range[1]; ++iter) {
        std::visit([&](const auto& cmd) {
            using T = std::decay_t<decltype(cmd)>;
            if constexpr (std::is_same_v<T, ApplyOp>) {
                if (std::count(cmd.inputs.begin(), cmd.inputs.end(),
                               dest) > 0) {
                    found = iter;
                }
            } else if constexpr (std::is_same_v<T, GetValue>) {
                if (cmd.dest == dest) {
                    found = iter;
                }
            } else if constexpr (std::is_same_v<T, SwapIn> ||
                    std::is_same_v<T, SwapOut> ||
                    std::is_same_v<T, Drop>) {
                //TODO: ignore swap-like commands, just remove them from buffer
                if (cmd.dest == dest) {
                    found = iter;
                }
            }
        }, *iter);
    };
    return found;
}

auto ChannelImpl::CommandBuffer::find_produce(TensorInfo* dest, Range range)
        -> Handle {
    return std::find_if(range[0], range[1], [dest](auto& cmd) {
        return std::visit([dest](const auto& cmd){
            using T = std::decay_t<decltype(cmd)>;
            if constexpr (std::is_same_v<T, ApplyOp>) {
                return std::count(cmd.outputs.begin(), cmd.outputs.end(), dest) > 0;
            } else if constexpr (std::is_same_v<T, Put>) {
                return cmd.dest == dest;
            }
            return false;
        }, cmd);
    });
}
1192

1193
void ChannelImpl::start_profile() {
1194
    MGB_LOCK_GUARD(m_spin);
1195
    mgb_assert(check_available(), "Channel already closed");
1196 1197 1198 1199
    auto capture_tensors = collect_valid_tensors();
    if (capture_tensors.size() > 0) {
        m_buffer.enqueue(StartProfile{std::move(capture_tensors)});
    }
1200 1201
}

1202
void ChannelImpl::stop_profile() {
1203
    MGB_LOCK_GUARD(m_spin);
1204
    mgb_assert(check_available(), "Channel already closed");
1205
    m_buffer.flush();
1206 1207 1208 1209
    auto escape_tensors = collect_valid_tensors();
    if (escape_tensors.size() > 0) {
        m_buffer.enqueue(StopProfile{std::move(escape_tensors)});
    }
1210 1211 1212
}

void ChannelImpl::push_scope(std::string name) {
1213
    MGB_LOCK_GUARD(m_spin);
1214
    mgb_assert(check_available(), "Channel already closed");
1215
    auto& state = get_channel_state();
1216
    state.scopes.push(name);
1217
    RECORD_EVENT(ScopeEvent, name);
1218
    m_buffer.enqueue(PushScope{name});
1219 1220 1221
}

void ChannelImpl::pop_scope(std::string name) {
1222
    MGB_LOCK_GUARD(m_spin);
1223
    mgb_assert(check_available(), "Channel already closed");
1224
    auto& state = get_channel_state();
1225
    state.scopes.pop(name);
1226
    RECORD_EVENT(ScopeFinishEvent, name);
1227
    m_buffer.enqueue(PopScope{name});
1228 1229
}

1230 1231 1232 1233 1234 1235 1236 1237
void ChannelImpl::assert_in_channel() {
    mgb_assert(get_worker_tid() != std::this_thread::get_id(), "this method cannot be called in worker thread");
}

void ChannelImpl::assert_in_worker() {
    mgb_assert(get_worker_tid() == std::this_thread::get_id(), "this method can only be called in worker thread");
}

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
void ChannelImpl::sample_on_device(CompNode device, bool force) {
    if (!force) {
        thread_local int last_sample_id = 0;
        int sample_rate = Profiler::is_profiling() ? Profiler::get_option("sample_rate", 0) : 0;
        if (!sample_rate || ((++last_sample_id) % sample_rate != 0)) {
            return;
        }
    }
    RECORD_EVENT(SampleDeviceEvent, device);
    auto [total, free] = device.get_mem_status_bytes();
    RECORD_EVENT(SampleDeviceFinishEvent, device, total, free);
}

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
void ChannelImpl::DynamicSublinear::pin(const SmallVector<TensorInfo*>& vec) {
    for (auto i : vec) {
        i->pin();
    }
}

void ChannelImpl::DynamicSublinear::unpin(const SmallVector<TensorInfo*>& vec) {
    for (auto i : vec) {
        i->unpin();
    }
}

void ChannelImpl::DynamicSublinear::update_dsu_after_recompute(TensorInfo* ptr) {
    auto&& dsu_fa = find_father(ptr->dsu_ptr);
    dsu_fa->t -= ptr->compute_time;
    ptr->dsu_ptr->parent.reset();
    ptr->dsu_ptr->t = ptr->compute_time;
}

void ChannelImpl::DynamicSublinear::update_dsu_after_evict(TensorInfo* ptr) {
    for (auto i : ptr->producer->inputs) {
        if (i->evict_type == EvictType::DROP) {
            merge(i->dsu_ptr, ptr->dsu_ptr);
        }
    }
    for (auto i : ptr->producer->outputs) {
        if (i && i->evict_type == EvictType::DROP) {
            merge(ptr->dsu_ptr, i->dsu_ptr);
        }
    }
}

double ChannelImpl::DynamicSublinear::estimate_neighbor_cost(TensorInfo* ptr) {
    double cost = 0;
    for (auto i : ptr->producer->inputs) {
        if (i->evict_type == EvictType::DROP) {
            double t = find_father(i->dsu_ptr)->t;
            if (t < i->compute_time) {
                t = i->compute_time;
            }
            cost += t;
        }
    }
    for (auto i : ptr->producer->outputs) {
        if (i && i->evict_type == EvictType::DROP) {
            double t = find_father(i->dsu_ptr)->t;
            if (t < i->compute_time) {
                t = i->compute_time;
            }
            cost += t;
        }
    }
    return cost;
}

TensorInfo* ChannelImpl::DynamicSublinear::find_best_tensor() {
    double min_msps = -1;
    TensorInfo* best = nullptr;
    for (auto i : candidates) {
        if (i->producer && i->ptr && !i->pinned && i->evict_type == EvictType::NONE) {
            double neighbor_cost = estimate_neighbor_cost(i);
            size_t begin_ptr = reinterpret_cast<size_t>(i->ptr->blob()->storage().get());
            auto side_info = i->ptr->comp_node().get_free_left_and_right(begin_ptr, begin_ptr + i->ptr->blob()->size());
            double free_mem = side_info.first + side_info.second;
            double msps = i->eval_func(neighbor_cost, free_mem, estimate_timestamp, 1.0, 1.0, 1.0, 1.0001);
            if (min_msps < 0 || msps < min_msps) {
                min_msps = msps;
                best = i;
            }
        }
    }
    return best;
}

void ChannelImpl::DynamicSublinear::merge(std::shared_ptr<DsuNode> &x, std::shared_ptr<DsuNode> &y) {
    auto&& f_x = find_father(x);
    auto&& f_y = find_father(y);
    if (f_x.get() == f_y.get()) {
        return;
    }
    f_y->t += f_x->t;
    f_x->parent = f_y;
}

std::shared_ptr<DsuNode> ChannelImpl::DynamicSublinear::find_father(std::shared_ptr<DsuNode>& x) {
    if (x->is_root()) {
        return x;
    } else {
        auto&& fa = find_father(x->parent);
        return x->parent = fa;
    }
}

void ChannelImpl::DynamicSublinear::insert_candidate(TensorInfo* ptr) {
    candidates.insert(ptr);
    if (!comp_node.valid()) {
        comp_node = ptr->ptr->comp_node();
    }
}

void ChannelImpl::DynamicSublinear::erase_candidate(TensorInfo* ptr) {
    candidates.erase(ptr);
}

void ChannelImpl::DynamicSublinear::update_used_time(TensorInfo* ptr) {
    ptr->last_used_time = estimate_timestamp;
}