events.h 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * \file imperative/src/impl/interpreter/events.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * 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.
 */

#pragma once

#include "megbrain/utils/small_vector.h"
15
#include "megbrain/imperative/profiler.h"
16

17
#include "../interpreter/stack_manager.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include "../op_trait.h"

namespace mgb::imperative::profiler {

enum class TensorProp {
    InvalidProp, Device, Shape, DType, DevValue, HostValue,
};

using OpParams = std::unordered_map<std::string, std::string>;

}

namespace mgb::imperative {

template <>
struct ToStringTrait<profiler::TensorProp>{
    using TensorProp = profiler::TensorProp;
    std::string operator()(TensorProp prop) const {
        switch(prop) {
        case TensorProp::DType:
            return "dtype";
        case TensorProp::DevValue:
            return "dev_value";
        case TensorProp::Device:
            return "device";
        case TensorProp::HostValue:
            return "host_value";
        case TensorProp::Shape:
            return "shape";
        default:
            return "unknown";
        }
    }
};

}

namespace mgb::imperative::profiler {

57 58 59 60 61
using Trace = interpreter::intl::StackManager::Trace;

struct ProfileOperatorState;
struct ProfileTensorState;

62 63 64 65 66 67 68 69 70
#define DEF_EVENT(X, ...) struct X##Event __VA_ARGS__;
#define DEF_DUR_EVENT(X, ...) struct X##Event __VA_ARGS__; struct X##FinishEvent __VA_ARGS__;

DEF_EVENT(OpDispatch, {
    uint64_t op_id;
    std::string op_name;
    std::function<OpParams()> op_params;
    SmallVector<uint64_t> inputs;
    SmallVector<uint64_t> outputs;
71
    Trace trace;
72 73 74 75 76 77 78 79 80 81 82 83 84 85
});

DEF_DUR_EVENT(OpInput, {
    uint64_t tensor_id;
    TensorShape shape;
});

DEF_DUR_EVENT(OpOutput, {
    uint64_t tensor_id;
    TensorShape shape;
});

DEF_DUR_EVENT(OpExecute, {
    uint64_t op_id;
86
    SmallVector<CompNode> device_list;
87
    std::string reason;
88 89
});

90
DEF_DUR_EVENT(KernelLaunch, {
91 92
    uint64_t op_id;
    uint64_t kernel_id;
93
    CompNode device;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
});

DEF_EVENT(TensorDeclare, {
    uint64_t tensor_id;
    std::string name;
});

DEF_EVENT(TensorProduce, {
    uint64_t tensor_id;
    TensorLayout layout;
    CompNode device;
    void* ptr;
});

DEF_EVENT(TensorUsage, {
    uint64_t tensor_id;
});

DEF_EVENT(TensorRelease, {
    uint64_t tensor_id;
});

DEF_EVENT(TensorErase, {
    uint64_t tensor_id;
    size_t use_count;
});

DEF_EVENT(TensorGetProp, {
    uint64_t tensor_id;
    TensorProp prop;
});

DEF_EVENT(TensorNotifyProp, {
    uint64_t tensor_id;
    uint64_t wait_id;
    TensorProp prop;
});

132
DEF_DUR_EVENT(TensorWaitProp, {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    uint64_t tensor_id;
    uint64_t wait_id;
    TensorProp prop;
});

DEF_DUR_EVENT(SampleDevice, {
    CompNode device;
    size_t total_memory;
    size_t free_memory;
});

DEF_EVENT(WorkerException, {});

DEF_EVENT(ShapeInfer, {
    bool success;
});

DEF_DUR_EVENT(Scope, {
    std::string name;
});

154 155
DEF_DUR_EVENT(Sync, {
    Trace trace;
156 157 158 159 160 161 162 163 164 165
});

DEF_DUR_EVENT(StartProfile, {
    size_t capture_count;
});

DEF_DUR_EVENT(StopProfile, {
    size_t escape_count;
});

166 167 168 169 170

enum class TensorCommandKind {
    Put, Del, SwapIn, SwapOut, Drop, ReGen, RecFree, GetValue
};

171
DEF_DUR_EVENT(TensorCommand, {
172
    using Kind = TensorCommandKind;
173 174 175 176
    uint64_t tensor_id;
    Kind kind;
});

177 178 179 180 181
DEF_DUR_EVENT(AutoEvict, {});

DEF_DUR_EVENT(Custom, {
    std::string title;
    std::string content;
182
    CompNode device;
183 184
});

185 186 187 188 189 190 191 192 193 194 195
DEF_EVENT(RecordDevice, {
    std::shared_ptr<CompNode::Event> event;
});

DEF_DUR_EVENT(HostToDevice, {
    TensorLayout layout;
    CompNode device;
    void* host_ptr;
    void* device_ptr;
});

196 197 198 199
#undef DEF_EVENT
#undef DEF_DUR_EVENT

}