record.py 9.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# pylint: disable=superfluous-parens, redefined-outer-name, redefined-outer-name,pointless-string-statement
# pylint: disable=consider-using-enumerate,invalid-name
"""Tuning record and serialization format"""

import argparse
import base64
import logging
import multiprocessing
import pickle
import json
import time
from collections import OrderedDict

14
from .. import build, lower, target as _target
15 16

from . import task
17
from .task import ConfigEntity, ApplyHistoryBest
18 19 20
from .measure import MeasureInput, MeasureResult

AUTOTVM_LOG_VERSION = 0.1
21
logger = logging.getLogger('autotvm')
22 23 24 25 26 27

try:  # convert unicode to str for python2
    _unicode = unicode
except NameError:
    _unicode = ()

28 29 30 31 32
try:
    _long = long
except NameError:
    _long = int

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 62 63 64 65 66 67 68 69 70 71 72 73 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

def measure_str_key(inp, include_config=True):
    """ get unique str key for MeasureInput

    Parameters
    ----------
    inp: MeasureInput
        input for the measure
    include_config: bool, optional
        whether includes config in the str key

    Returns
    -------
    key: str
        The str representation of key
    """
    config_str = str(inp.config) if include_config else ""
    return "".join([str(inp.target), inp.task.name, str(inp.task.args),
                    str(inp.task.kwargs), config_str])


def encode(inp, result, protocol='json'):
    """encode (MeasureInput, MeasureResult) pair to a string

    Parameters
    ----------
    inp: autotvm.tuner.MeasureInput
    result: autotvm.tuner.MeasureResult
        pair of input/result
    protocol: str
        log protocol, json or pickle

    Returns
    -------
    row: str
        a row in the logger file
    """

    if protocol == 'json':
        json_dict = {
            "i": (str(inp.target),
                  inp.task.name, inp.task.args, inp.task.kwargs,
                  inp.task.workload,
                  inp.config.to_json_dict()),

            "r": (result.costs if result.error_no == 0 else (1e9,),
                  result.error_no,
                  result.all_cost,
                  result.timestamp),

            "v": AUTOTVM_LOG_VERSION
        }
        return json.dumps(json_dict)
    elif protocol == 'pickle':
        row = (str(inp.target),
               str(base64.b64encode(pickle.dumps([inp.task.name,
                                                  inp.task.args,
                                                  inp.task.kwargs,
                                                  inp.task.workload])).decode()),
               str(base64.b64encode(pickle.dumps(inp.config)).decode()),
               str(base64.b64encode(pickle.dumps(tuple(result))).decode()))
        return '\t'.join(row)
    else:
        raise RuntimeError("Invalid log protocol: " + protocol)


def decode(row, protocol='json'):
    """Decode encoded record string to python object

    Parameters
    ----------
    row: str
        a row in the logger file
    protocol: str
        log protocol, json or pickle

    Returns
    -------
    input: autotvm.tuner.MeasureInput
    result: autotvm.tuner.MeasureResult
    """
    # pylint: disable=unused-variable
    if protocol == 'json':
        row = json.loads(row)
        tgt, task_name, task_args, task_kwargs, workload, config = row['i']
118
        tgt = _target.create(str(tgt))
119 120

        def clean_json_to_python(x):
121 122
            """1. Convert all list in x to tuple (hashable)
               2. Convert unicode to str for python2
123 124 125 126 127
            """
            if isinstance(x, list):
                return tuple([clean_json_to_python(a) for a in x])
            if isinstance(x, _unicode):
                return str(x)
128 129
            if isinstance(x, (_long, int)):
                return int(x)
130 131 132 133 134 135 136 137 138 139 140
            return x

        tsk = task.Task(clean_json_to_python(task_name), clean_json_to_python(task_args))
        tsk.workload = clean_json_to_python(workload)
        config = ConfigEntity.from_json_dict(config)
        inp = MeasureInput(tgt, tsk, config)
        result = MeasureResult(*[tuple(x) if isinstance(x, list) else x for x in row["r"]])

        return inp, result
    elif protocol == 'pickle':
        items = row.split("\t")
141
        tgt = _target.create(items[0])
142 143 144 145 146 147 148 149 150 151
        task_tuple = pickle.loads(base64.b64decode(items[1].encode()))
        config = pickle.loads(base64.b64decode(items[2].encode()))
        result = pickle.loads(base64.b64decode(items[3].encode()))

        tsk = task.Task(task_tuple[0], task_tuple[1])
        tsk.workload = task_tuple[3]
        return MeasureInput(tgt, tsk, config), MeasureResult(*result)
    else:
        raise RuntimeError("Invalid log protocol: " + protocol)

152

153 154 155 156 157 158 159 160 161 162 163 164 165 166
def load_from_file(filename):
    """Generator: load records from file.
    This is a generator that yields the records.

    Parameters
    ----------
    filename: str

    Yields
    ------
    input: autotvm.tuner.MeasureInput
    result: autotvm.tuner.MeasureResult
    """
    for row in open(filename):
167 168
        if row and not row.startswith('#'):
            yield decode(row)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184


def split_workload(in_file, clean=True):
    """Split a log file into separate files, each of which contains only a single workload
    This function can also delete duplicated records in log file

    Parameters
    ----------
    in_file: str
        input filename
    clean: bool
        whether delete duplicated items
    """
    tic = time.time()
    lines = list(open(in_file).readlines())

185
    logger.info("start converting...")
186 187
    pool = multiprocessing.Pool()
    lines = pool.map(decode, lines)
188
    logger.info("map done %.2f", time.time() - tic)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    wkl_dict = OrderedDict()
    for inp, res in lines:
        wkl = measure_str_key(inp, False)
        if wkl not in wkl_dict:
            wkl_dict[wkl] = []
        wkl_dict[wkl].append([inp, res])

    if clean:
        for i, (k, v) in enumerate(wkl_dict.items()):
            # clean duplicated items
            added = set()
            cleaned = []
            for inp, res in v:
                str_key = measure_str_key(inp)
                if str_key in added:
                    continue
                added.add(str_key)
                cleaned.append([inp, res])

            # write to file
210
            logger.info("Key: %s\tValid: %d\tDup: %d\t", k, len(cleaned), len(v) - len(cleaned))
211 212 213 214 215
            with open(args.i + ".%03d.wkl" % i, 'w') as fout:
                for inp, res in cleaned:
                    fout.write(encode(inp, res) + '\n')
    else:
        for i, (k, v) in enumerate(wkl_dict.items()):
216
            logger.info("Key: %s\tNum: %d", k, len(v))
217 218 219 220
            with open(args.i + ".%03d.wkl" % i, 'w') as fout:
                for inp, res in v:
                    fout.write(encode(inp, res) + '\n')

221 222 223 224 225 226 227 228 229
def pick_best(in_file, out_file):
    """
    Pick best entries from a file and store it to another file.
    This distill the useful log entries from a large log file.

    Parameters
    ----------
    in_file: str
        The filename of input
230
    out_file: str or file
231 232 233 234 235 236 237 238 239 240 241
        The filename of output
    """
    best_context = ApplyHistoryBest(load_from_file(in_file))
    best_set = set()

    for v in best_context.best_by_model.values():
        best_set.add(measure_str_key(v[0]))

    for v in best_context.best_by_targetkey.values():
        best_set.add(measure_str_key(v[0]))

242
    logger.info("Extract %d best records from the %s", len(best_set), in_file)
243
    fout = open(out_file, 'w') if isinstance(out_file, str) else out_file
244 245 246 247

    for inp, res in load_from_file(in_file):
        if measure_str_key(inp) in best_set:
            fout.write(encode(inp, res) + "\n")
248
            best_set.remove(measure_str_key(inp))
249 250 251 252 253 254

"""
Usage:
This record executable module has three modes.

* Print log file in readable format
255
e.g. python -m autotvm.record --mode read --i collect_conv.log --begin 0 --end 5 --ir --code
256 257

* Extract history best from a large log file
258
e.g. python -m autotvm.record --mode pick --i collect.log
259 260

* Split a log file into separate files, each of which contains only a single wkl
261
e.g. python -m autotvm.record --mode split --i collect.log
262 263 264
"""
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
265
    parser.add_argument("--mode", choices=['read', 'pick', 'split'], default='read')
266 267 268 269 270 271 272 273
    parser.add_argument("--i", type=str, help="input file")
    parser.add_argument("--o", type=str, default=None, help='output file')
    parser.add_argument("--begin", type=int, default=0)
    parser.add_argument("--end", type=int, default=5)
    parser.add_argument("--ir", action='store_true')
    parser.add_argument("--code", action='store_true')

    args = parser.parse_args()
274
    logging.basicConfig(level=logger.INFO)
275

276 277 278
    if args.mode == 'pick':
        args.o = args.o or args.i + ".best.log"
        pick_best(args.i, args.o)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    elif args.mode == 'read':
        for i, (inp, result) in enumerate(load_from_file(args.i)):
            if args.begin <= i < args.end:
                with inp.target:
                    s, arg_bufs = inp.task.instantiate(inp.config)

                print("")
                print(inp.target, inp.task, inp.config)
                print(result)

                if args.ir:
                    with inp.target:
                        print(lower(s, arg_bufs, simple_mode=True))

                if args.code:
                    with inp.target:
                        func = build(s, arg_bufs)
                        print(func.imported_modules[0].get_source())
    elif args.mode == 'split':
        split_workload(args.i)