pretty_printers.py 6.1 KB
Newer Older
M
Megvii Engine Team 已提交
1 2 3 4 5
import gdb
import gdb.printing
import gdb.types


6 7 8 9 10 11
def dynamic_cast(val):
    assert val.type.code == gdb.TYPE_CODE_REF
    val = val.cast(val.dynamic_type)
    return val


M
Megvii Engine Team 已提交
12
def eval_on_val(val, eval_str):
13 14 15 16
    if val.type.code == gdb.TYPE_CODE_REF:
        val = val.referenced_value()
    address = val.address
    eval_str = "(*({}){}){}".format(address.type, int(address), eval_str)
M
Megvii Engine Team 已提交
17 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 57 58 59 60 61
    return gdb.parse_and_eval(eval_str)


class SmallVectorPrinter:
    def __init__(self, val):
        t = val.type.template_argument(0)
        self.begin = val['m_begin_ptr'].cast(t.pointer())
        self.end = val['m_end_ptr'].cast(t.pointer())
        self.size = self.end - self.begin
        self.capacity = val['m_capacity_ptr'].cast(t.pointer()) - val['m_begin_ptr'].cast(t.pointer())

    def to_string(self):
        return 'SmallVector of Size {}'.format(self.size)

    def display_hint(self):
        return 'array'

    def children(self):
        for i in range(self.size):
            yield "[{}]".format(i), (self.begin+i).dereference()


class MaybePrinter:
    def __init__(self, val):
        self.val = val['m_ptr']

    def to_string(self):
        if self.val:
            return 'Some {}'.format(self.val)
        else:
            return 'None'

    def display_hint(self):
        return 'array'

    def children(self):
        if self.val:
            yield '[0]', self.val.dereference()


class ToStringPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
62
        return eval_on_val(self.val, ".to_string().c_str()").string()
M
Megvii Engine Team 已提交
63 64 65 66 67 68 69


class ReprPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
70 71 72 73
        val = self.val
        if val.type.code == gdb.TYPE_CODE_REF:
            val = val.referenced_value()
        return eval_on_val(val, ".repr().c_str()").string()
M
Megvii Engine Team 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155


class HandlePrinter:
    def __init__(self, val):
        impl = gdb.lookup_type("mgb::imperative::interpreter::intl::TensorInfo")
        self.val = val.cast(impl.pointer())

    def to_string(self):
        if self.val:
            return 'Handle of TensorInfo at {}'.format(self.val)
        else:
            return 'Empty Handle'

    def display_hint(self):
        return 'array'

    def children(self):
        if self.val:
            yield '[0]', self.val.dereference()


def print_small_tensor(device_nd):
    size = device_nd["m_storage"]["m_size"]
    ndim = device_nd["m_layout"]["ndim"]
    dim0 = device_nd["m_layout"]["shape"][0]
    stride0 = device_nd["m_layout"]["stride"][0]
    dtype = device_nd["m_layout"]["dtype"]
    if size == 0:
        return "<empty>"
    if ndim > 1:
        return "<ndim > 1>"
    if dim0 > 64:
        return "<size tool large>"
    raw_ptr = device_nd["m_storage"]["m_data"]["_M_ptr"]
    dtype_name = dtype["m_trait"]["name"].string()
    dtype_map = {
        "Float32": (gdb.lookup_type("float"), float),
        "Int32": (gdb.lookup_type("int"), int),
    }
    if dtype_name not in dtype_map:
        return "<dtype unsupported>"
    else:
        ctype, pytype = dtype_map[dtype_name]
    ptr = raw_ptr.cast(ctype.pointer())
    array = []
    for i in range(dim0):
        array.append((pytype)((ptr + i * int(stride0)).dereference()))
    return str(array)


class LogicalTensorDescPrinter:
    def __init__(self, val):
        self.layout = val['layout']
        self.comp_node = val['comp_node']
        self.value = val['value']

    def to_string(self):
        return 'LogicalTensorDesc'

    def children(self):
        yield 'layout', self.layout
        yield 'comp_node', self.comp_node
        yield 'value', print_small_tensor(self.value)


class OpDefPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val.dynamic_type.name

    def children(self):
        concrete_val = self.val.address.cast(self.val.dynamic_type.pointer()).dereference()
        for field in concrete_val.type.fields():
            if field.is_base_class or field.artificial:
                continue
            if field.name == 'sm_typeinfo':
                continue
            yield field.name, concrete_val[field.name]


156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
class SpanPrinter:
    def __init__(self, val):
        self.begin = val['m_begin']
        self.end = val['m_end']
        self.size = self.end - self.begin

    def to_string(self):
        return 'Span of Size {}'.format(self.size)

    def display_hint(self):
        return 'array'

    def children(self):
        for i in range(self.size):
            yield "[{}]".format(i), (self.begin+i).dereference()


M
Megvii Engine Team 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185
pp = gdb.printing.RegexpCollectionPrettyPrinter("MegEngine")
# megdnn
pp.add_printer('megdnn::SmallVectorImpl', '^megdnn::SmallVector(Impl)?<.*>$', SmallVectorPrinter)
pp.add_printer('megdnn::TensorLayout', '^megdnn::TensorLayout$', ToStringPrinter)
pp.add_printer('megdnn::TensorShape', '^megdnn::TensorShape$', ToStringPrinter)
# megbrain
pp.add_printer('mgb::CompNode', '^mgb::CompNode$', ToStringPrinter)
pp.add_printer('mgb::Maybe', '^mgb::Maybe<.*>$', MaybePrinter)
# imperative
pp.add_printer('mgb::imperative::LogicalTensorDesc', '^mgb::imperative::LogicalTensorDesc$', LogicalTensorDescPrinter)
pp.add_printer('mgb::imperative::OpDef', '^mgb::imperative::OpDef$', OpDefPrinter)
pp.add_printer('mgb::imperative::Subgraph', '^mgb::imperative::Subgraph$', ReprPrinter)
pp.add_printer('mgb::imperative::EncodedSubgraph', '^mgb::imperative::EncodedSubgraph$', ReprPrinter)
186 187 188
# imperative dispatch
pp.add_printer('mgb::imperative::ValueRef', '^mgb::imperative::ValueRef$', ToStringPrinter)
pp.add_printer('mgb::imperative::Span', '^mgb::imperative::Span<.*>$', SpanPrinter)
M
Megvii Engine Team 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)


def override_pretty_printer_for(val):
    type = val.type.strip_typedefs()
    if type.code == gdb.TYPE_CODE_PTR:
        if not val:
            return None
        target_typename = str(type.target().strip_typedefs())
        if target_typename == "mgb::imperative::OpDef":
            return OpDefPrinter(val.dereference())
        if target_typename == "mgb::imperative::interpreter::Interpreter::HandleImpl":
            return HandlePrinter(val)


gdb.pretty_printers.append(override_pretty_printer_for)