events.h 4.8 KB
Newer Older
1 2
#pragma once

3
#include "megbrain/imperative/profiler.h"
M
Megvii Engine Team 已提交
4
#include "megbrain/utils/small_vector.h"
5

6
#include "../interpreter/stack_manager.h"
7
#include "../op_trait.h"
8
#include "megbrain/imperative/cpp_cupti.h"
9 10 11 12

namespace mgb::imperative::profiler {

enum class TensorProp {
M
Megvii Engine Team 已提交
13 14 15 16 17 18
    InvalidProp,
    Device,
    Shape,
    DType,
    DevValue,
    HostValue,
19 20 21 22
};

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

M
Megvii Engine Team 已提交
23
}  // namespace mgb::imperative::profiler
24 25 26 27

namespace mgb::imperative {

template <>
M
Megvii Engine Team 已提交
28
struct ToStringTrait<profiler::TensorProp> {
29 30
    using TensorProp = profiler::TensorProp;
    std::string operator()(TensorProp prop) const {
M
Megvii Engine Team 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
        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";
44 45 46 47
        }
    }
};

M
Megvii Engine Team 已提交
48
}  // namespace mgb::imperative
49 50 51

namespace mgb::imperative::profiler {

52 53 54 55 56
using Trace = interpreter::intl::StackManager::Trace;

struct ProfileOperatorState;
struct ProfileTensorState;

57
#define DEF_EVENT(X, ...) struct X##Event __VA_ARGS__;
M
Megvii Engine Team 已提交
58 59 60
#define DEF_DUR_EVENT(X, ...)    \
    struct X##Event __VA_ARGS__; \
    struct X##FinishEvent __VA_ARGS__;
61 62 63 64 65 66 67

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

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;
83
    SmallVector<CompNode> device_list;
84
    std::string reason;
85 86
});

87
DEF_DUR_EVENT(KernelLaunch, {
88 89
    uint64_t op_id;
    uint64_t kernel_id;
90
    CompNode device;
91 92 93 94 95 96 97 98 99 100 101 102 103 104
});

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 已提交
105
DEF_EVENT(TensorUsage, { uint64_t tensor_id; });
106

M
Megvii Engine Team 已提交
107
DEF_EVENT(TensorRelease, { uint64_t tensor_id; });
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

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

125
DEF_DUR_EVENT(TensorWaitProp, {
126 127 128
    uint64_t tensor_id;
    uint64_t wait_id;
    TensorProp prop;
129
    std::function<OpParams()> param;
130 131 132 133 134 135 136 137 138 139
});

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

DEF_EVENT(WorkerException, {});

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

142 143 144 145
DEF_DUR_EVENT(Scope, {
    std::string name;
    ScopeType type = ScopeType::DEFAULT;
});
146

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

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

M
Megvii Engine Team 已提交
151
DEF_DUR_EVENT(StopProfile, { size_t escape_count; });
152

153
enum class TensorCommandKind { Put, Del, Drop, ReGen, RecFree, GetValue };
154

155
DEF_DUR_EVENT(TensorCommand, {
156
    using Kind = TensorCommandKind;
157 158 159 160
    uint64_t tensor_id;
    Kind kind;
});

161 162 163 164 165
DEF_DUR_EVENT(AutoEvict, {});

DEF_DUR_EVENT(Custom, {
    std::string title;
    std::string content;
166
    CompNode device;
167 168
});

M
Megvii Engine Team 已提交
169
DEF_EVENT(RecordDevice, { std::shared_ptr<CompNode::Event> event; });
170 171 172 173 174 175 176 177

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

178 179
DEF_EVENT(StopStep, { CompNode device; });

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
// cupti events
DEF_EVENT(CUPTITimestamp, { cupti::clock::time_point timestamp; });

DEF_DUR_EVENT(CUPTIKernelLaunch, {
    uint32_t correlation_id;
    const char* name;
});

DEF_EVENT(CUPTIKernelExecute, {
    uint32_t correlation_id;
    const char* name;
    cupti::stream_t stream;
    cupti::time_point start;
    cupti::time_point end;
});

DEF_DUR_EVENT(CUPTIMemcpyLaunch, { uint32_t correlation_id; });

DEF_EVENT(CUPTIMemcpy, {
    uint32_t correlation_id;
    uint8_t src_kind;
    uint8_t dst_kind;
    uint64_t bytes;
    cupti::stream_t stream;
    cupti::time_point start;
    cupti::time_point end;
});

DEF_EVENT(CUPTIMemset, {
    uint32_t correlation_id;
    uint32_t value;
    uint64_t bytes;
    cupti::stream_t stream;
    cupti::time_point start;
    cupti::time_point end;
});

DEF_EVENT(CUPTIUnknownDevice, {});

DEF_DUR_EVENT(CUPTIRuntime, {
    uint32_t correlation_id;
    const char* name;
});

DEF_DUR_EVENT(CUPTIDriver, {
    uint32_t correlation_id;
    const char* name;
});

DEF_EVENT(CUPTIIdentifyStream, {
    cupti::stream_t stream;
    CompNode device;
});

234 235 236
#undef DEF_EVENT
#undef DEF_DUR_EVENT

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