events.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * \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

14
#include "megbrain/imperative/profiler.h"
M
Megvii Engine Team 已提交
15
#include "megbrain/utils/small_vector.h"
16

17
#include "../interpreter/stack_manager.h"
18 19 20 21 22
#include "../op_trait.h"

namespace mgb::imperative::profiler {

enum class TensorProp {
M
Megvii Engine Team 已提交
23 24 25 26 27 28
    InvalidProp,
    Device,
    Shape,
    DType,
    DevValue,
    HostValue,
29 30 31 32
};

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

M
Megvii Engine Team 已提交
33
}  // namespace mgb::imperative::profiler
34 35 36 37

namespace mgb::imperative {

template <>
M
Megvii Engine Team 已提交
38
struct ToStringTrait<profiler::TensorProp> {
39 40
    using TensorProp = profiler::TensorProp;
    std::string operator()(TensorProp prop) const {
M
Megvii Engine Team 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
        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";
54 55 56 57
        }
    }
};

M
Megvii Engine Team 已提交
58
}  // namespace mgb::imperative
59 60 61

namespace mgb::imperative::profiler {

62 63 64 65 66
using Trace = interpreter::intl::StackManager::Trace;

struct ProfileOperatorState;
struct ProfileTensorState;

67
#define DEF_EVENT(X, ...) struct X##Event __VA_ARGS__;
M
Megvii Engine Team 已提交
68 69 70
#define DEF_DUR_EVENT(X, ...)    \
    struct X##Event __VA_ARGS__; \
    struct X##FinishEvent __VA_ARGS__;
71 72 73 74 75 76 77

DEF_EVENT(OpDispatch, {
    uint64_t op_id;
    std::string op_name;
    std::function<OpParams()> op_params;
    SmallVector<uint64_t> inputs;
    SmallVector<uint64_t> outputs;
78
    Trace trace;
79 80 81 82 83 84 85 86 87 88 89 90 91 92
});

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;
93
    SmallVector<CompNode> device_list;
94
    std::string reason;
95 96
});

97
DEF_DUR_EVENT(KernelLaunch, {
98 99
    uint64_t op_id;
    uint64_t kernel_id;
100
    CompNode device;
101 102 103 104 105 106 107 108 109 110 111 112 113 114
});

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

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

M
Megvii Engine Team 已提交
115
DEF_EVENT(TensorUsage, { uint64_t tensor_id; });
116

M
Megvii Engine Team 已提交
117
DEF_EVENT(TensorRelease, { uint64_t tensor_id; });
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

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;
});

135
DEF_DUR_EVENT(TensorWaitProp, {
136 137 138 139 140 141 142 143 144 145 146 147 148
    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, {});

M
Megvii Engine Team 已提交
149
DEF_EVENT(ShapeInfer, { bool success; });
150

M
Megvii Engine Team 已提交
151
DEF_DUR_EVENT(Scope, { std::string name; });
152

M
Megvii Engine Team 已提交
153
DEF_DUR_EVENT(Sync, { Trace trace; });
154

M
Megvii Engine Team 已提交
155
DEF_DUR_EVENT(StartProfile, { size_t capture_count; });
156

M
Megvii Engine Team 已提交
157
DEF_DUR_EVENT(StopProfile, { size_t escape_count; });
158 159

enum class TensorCommandKind {
M
Megvii Engine Team 已提交
160 161 162 163 164 165 166 167
    Put,
    Del,
    SwapIn,
    SwapOut,
    Drop,
    ReGen,
    RecFree,
    GetValue
168 169
};

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

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

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

M
Megvii Engine Team 已提交
184
DEF_EVENT(RecordDevice, { std::shared_ptr<CompNode::Event> event; });
185 186 187 188 189 190 191 192

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

193 194 195
#undef DEF_EVENT
#undef DEF_DUR_EVENT

M
Megvii Engine Team 已提交
196
}  // namespace mgb::imperative::profiler