build_model.py 1.8 KB
Newer Older
1
#!/usr/bin/env python3
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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.
18

19
"""Builds a simple graph for testing."""
nhynes committed
20 21 22 23 24

from os import path as osp

import numpy as np
import tvm
25
from tvm import te
26 27
from tvm import relay
from tvm.relay import testing
nhynes committed
28 29 30 31

CWD = osp.dirname(osp.abspath(osp.expanduser(__file__)))

def _get_model(dshape):
32 33 34 35 36 37
    data = relay.var('data', shape=dshape)
    fc = relay.nn.dense(data, relay.var("dense_weight"), units=dshape[-1]*2)
    fc = relay.nn.bias_add(data, relay.var("dense_bias"))
    left, right = relay.split(fc, indices_or_sections=2, axis=1)
    one = relay.const(1, dtype="float32")
    return relay.Tuple([(left + one), (right - one), fc])
nhynes committed
38 39 40 41 42


def main():
    dshape = (32, 16)
    net = _get_model(dshape)
43 44 45
    mod, params = testing.create_workload(net)
    graph, lib, params = relay.build(
        mod, 'llvm', params=params)
nhynes committed
46 47

    with open(osp.join(CWD, 'graph.json'), 'w') as f_resnet:
48
        f_resnet.write(graph)
nhynes committed
49
    with open(osp.join(CWD, 'graph.params'), 'wb') as f_params:
50
        f_params.write(relay.save_param_dict(params))
nhynes committed
51 52 53

if __name__ == '__main__':
    main()