record.py 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20 21 22 23 24 25 26 27
# 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
28 29
import os
import itertools
30 31
from collections import OrderedDict

32
from .. import build, lower, target as _target
33 34

from . import task
35
from .task import ConfigEntity, ApplyHistoryBest
36 37 38
from .measure import MeasureInput, MeasureResult

AUTOTVM_LOG_VERSION = 0.1
39
logger = logging.getLogger('autotvm')
40 41 42 43 44 45

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

46 47 48 49 50
try:
    _long = long
except NameError:
    _long = int

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

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)
104
    if protocol == 'pickle':
105 106 107 108 109 110 111 112
        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)
113 114

    raise RuntimeError("Invalid log protocol: " + protocol)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135


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']
136
        tgt = _target.create(str(tgt))
137 138

        def clean_json_to_python(x):
139 140
            """1. Convert all list in x to tuple (hashable)
               2. Convert unicode to str for python2
141 142 143 144 145
            """
            if isinstance(x, list):
                return tuple([clean_json_to_python(a) for a in x])
            if isinstance(x, _unicode):
                return str(x)
146 147
            if isinstance(x, (_long, int)):
                return int(x)
148 149 150 151 152 153 154 155 156
            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
157
    if protocol == 'pickle':
158
        items = row.split("\t")
159
        tgt = _target.create(items[0])
160 161 162 163 164 165 166
        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)
167 168

    raise RuntimeError("Invalid log protocol: " + protocol)
169

170

171 172 173 174 175 176 177 178 179 180 181 182 183 184
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):
185 186
        if row and not row.startswith('#'):
            yield decode(row)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202


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())

203
    logger.info("start converting...")
204 205
    pool = multiprocessing.Pool()
    lines = pool.map(decode, lines)
206
    logger.info("map done %.2f", time.time() - tic)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

    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
228
            logger.info("Key: %s\tValid: %d\tDup: %d\t", k, len(cleaned), len(v) - len(cleaned))
229 230 231 232 233
            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()):
234
            logger.info("Key: %s\tNum: %d", k, len(v))
235 236 237 238
            with open(args.i + ".%03d.wkl" % i, 'w') as fout:
                for inp, res in v:
                    fout.write(encode(inp, res) + '\n')

239 240 241 242
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.
243 244
    If out_file already exists, the best entries from both
    in_file and out_file will be saved.
245 246 247 248 249

    Parameters
    ----------
    in_file: str
        The filename of input
250
    out_file: str or file
251 252
        The filename of output
    """
253 254 255 256 257 258
    context = load_from_file(in_file)
    if os.path.isfile(out_file):
        out_context = load_from_file(out_file)
        context = itertools.chain(context, out_context)
    context, context_clone = itertools.tee(context)
    best_context = ApplyHistoryBest(context)
259 260 261 262 263 264 265 266
    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]))

267
    logger.info("Extract %d best records from the %s", len(best_set), in_file)
268
    fout = open(out_file, 'w') if isinstance(out_file, str) else out_file
269

270
    for inp, res in context_clone:
271 272
        if measure_str_key(inp) in best_set:
            fout.write(encode(inp, res) + "\n")
273
            best_set.remove(measure_str_key(inp))
274 275 276 277 278 279

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

* Print log file in readable format
280
e.g. python -m tvm.autotvm.record --mode read --i collect_conv.log --begin 0 --end 5 --ir --code
281 282

* Extract history best from a large log file
283
e.g. python -m tvm.autotvm.record --mode pick --i collect.log
284 285

* Split a log file into separate files, each of which contains only a single wkl
286
e.g. python -m tvm.autotvm.record --mode split --i collect.log
287 288 289
"""
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
290
    parser.add_argument("--mode", choices=['read', 'pick', 'split'], default='read')
291 292 293 294 295 296 297 298
    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()
299
    logging.basicConfig(level=logging.INFO)
300

301 302 303
    if args.mode == 'pick':
        args.o = args.o or args.i + ".best.log"
        pick_best(args.i, args.o)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    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)