Exemplo n.º 1
0
def run():
    assert isfile(MODEL_PB)
    g, gd = fropen()
    print(g)
    # totb(g)
    sym, params = from_tensorflow(gd)
    print(sym)
Exemplo n.º 2
0
def block1_correctness2():
    na = 0.8 * np.ones(shape=(1, 108, 21, 32))
    with tf.Session(graph=tf.Graph()) as sess:
        p = tf.placeholder(tf.float32, shape=(1, 108, 21, 32), name='a')
        o = block1_block_tf(p)
        gd = sess.graph.as_graph_def(add_shapes=True)
        sym, params = from_tensorflow(gd)
        r2 = with_nnvm(0, 1, [na], lambda a: sym, params)
        print(r2)
Exemplo n.º 3
0
def tvm_import(
    opt_level: int = 2,
    iname: str = MODEL_INPUT,
    oname: str = MODEL_OUTPUT
) -> Tuple[TVM_Graph, str, Params, TF_Tensor, TF_Tensor]:
    g, gd = fropen()
    sym, params = from_tensorflow(gd)

    i = g.get_tensor_by_name(iname + ':0')
    o = g.get_tensor_by_name(oname + ':0')
    i_shape_dict = {iname + ':0': i.shape.as_list()}
    i_dtype_dict = {iname + ':0': i.dtype.as_numpy_dtype()}

    with nnvm.compiler.build_config(opt_level=opt_level):
        graph, lib, params = nnvm.compiler.build(graph=sym,
                                                 target='llvm',
                                                 shape=i_shape_dict,
                                                 dtype=i_dtype_dict,
                                                 params=params)
        # print(graph.ir())
    return graph, lib, params, i, o
Exemplo n.º 4
0
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name="")
            graphdef = sess.graph.as_graph_def(add_shapes=True)
    return sess.graph, graphdef


def common_init(init_method, shape, dtype):
    if init_method == 'zeros':
        return np.zeros(shape=shape, dtype=dtype)
    elif init_method == 'std':
        return np.random.uniform(low=-50, high=51,
                                 size=shape).astype(dtype=dtype)
    else:
        raise ValueError("invalid 'init' argument")


MODEL_PB = join(environ['CWD'], "data/freeze.pb")
MODEL_INPUT = 'Rcnn_ctcV3/Inputs'

MODEL_OUTPUTS = [
    'Rcnn_ctcV3/expand_conv1/add_1/add', 'Rcnn_ctcV3/expand_conv2/add_7/add',
    'Rcnn_ctcV3/expand_conv3/add_13/add', 'Rcnn_ctcV3/expand_conv4/add_17/add',
    'Rcnn_ctcV3/conv2d_116/BiasAdd'
]
MODEL_OUTPUT = MODEL_OUTPUTS[-1]

DEF_LOG_DIR = './_logs'

MODEL_PARAMS = from_tensorflow(fropen()[1])[1]
Exemplo n.º 5
0
def nnvmS_run(opt_level: int = 2,
              nthreads: int = None,
              iname: str = MODEL_INPUT,
              oname: str = MODEL_OUTPUT,
              init_method='std',
              nwarmup: int = 0,
              nloops: int = 1,
              **kwargs) -> Result:
    """ Run staged NNVM model """
    r = Result()
    print("Warning: unused args:", kwargs) if kwargs != {} else None
    try:
        g, gd = fropen()
        _, params = from_tensorflow(
            gd)  # We still need from_tensorflow to get the parameters
        mo, savepoints = staged_model()
        nnvm_graph = nnvm.graph.create(savepoints[oname])
        print('synthesized')

        i = g.get_tensor_by_name(iname + ':0')
        o = g.get_tensor_by_name(oname + ':0')
        i_shape_dict = {iname + ':0': i.shape.as_list()}
        i_dtype_dict = {iname + ':0': i.dtype.as_numpy_dtype()}

        with nnvm.compiler.build_config(opt_level=opt_level):
            graph, lib, params = nnvm.compiler.build(graph=nnvm_graph,
                                                     target='llvm',
                                                     shape=i_shape_dict,
                                                     dtype=i_dtype_dict,
                                                     params=params)
            # print(graph.ir())

        if nthreads is None:
            nthreads = 20

        get_global_func('runtime.config_threadpool')(1, nthreads)
        m = graph_runtime.create(graph, lib, ctx=tvm.cpu(0))
        print('compiled')

        perfs: List[float] = []
        for it in range(nwarmup + nloops):
            i_data = common_init(init_method,
                                 shape=i.shape.as_list(),
                                 dtype=i.dtype.as_numpy_dtype())
            m.set_input(iname, tvm.nd.array(i_data))
            m.set_input(**params)

            b = perf_counter()
            m.run()
            e = perf_counter()
            o_data = m.get_output(
                0, tvm.nd.empty(o.shape.as_list(), o.dtype.name)).asnumpy()
            print('tvms', e - b)

            if it >= nwarmup:
                perfs.append(e - b)

        r.set_perfs(perfs)
        r.last_data = o_data
    except KeyboardInterrupt:
        raise
    except Exception as e:
        warn('exception: ' + str(e))
        r.err = e
    return r