From 61d879aa4e418afa6137f7155de79068f1732b37 Mon Sep 17 00:00:00 2001 From: cchung100m Date: Wed, 20 Nov 2019 21:18:07 +0800 Subject: [PATCH 1/4] [Golang][Doc] improve the samples and doc --- golang/README.md | 8 ++++--- golang/sample/Makefile | 2 +- golang/sample/complex.go | 4 ++-- golang/sample/gen_mobilenet_lib.py | 38 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 golang/sample/gen_mobilenet_lib.py diff --git a/golang/README.md b/golang/README.md index fe0dc621a469..ee3ea8cc2e98 100644 --- a/golang/README.md +++ b/golang/README.md @@ -68,6 +68,8 @@ To Demonstrates sample TVM module compilation using python and deploy via golang To deploy a realtime module with lib, graph and param. ```bash +python3 gen_mobilenet_lib.py + ./complex ``` @@ -80,13 +82,13 @@ To demonstrate go function closure conversion to packed function handle. To demonstrate a packed function handle given as an argument. ```bash -pack_func_handle_arg +./pack_func_handle_arg ``` To register go function with runtime as a global function. ```bash -pack_func_register +./pack_func_register ``` To demonstrate function closure passed as argument to a function call. @@ -120,5 +122,5 @@ Please refer ```docker/install/ubuntu_install_golang.sh``` for the packages depe go compiler 1.10 on ubuntu doesn't install on standard path, hence an explicit export may be needed as shown below. ```bash -export PATH="/usr/lib/go-1.10/bin:$PATH"``` +export PATH="/usr/lib/go-1.10/bin:$PATH" ``` diff --git a/golang/sample/Makefile b/golang/sample/Makefile index 5d9781d05bd6..fd738b6f979f 100644 --- a/golang/sample/Makefile +++ b/golang/sample/Makefile @@ -31,4 +31,4 @@ all: $(EXECUTABLE) @go tool compile -pack -o $@ $< clean: - @rm -f $(EXECUTABLE) *.so *.o *.a + @rm -f $(EXECUTABLE) *.so *.o *.a *.json *.params diff --git a/golang/sample/complex.go b/golang/sample/complex.go index 96bab6adc2a8..4f0c182fd3b5 100644 --- a/golang/sample/complex.go +++ b/golang/sample/complex.go @@ -88,8 +88,8 @@ func main() { fmt.Printf("Graph runtime Created\n") // Array allocation attributes - tshapeIn := []int64{1, 224, 224, 3} - tshapeOut := []int64{1, 1001} + tshapeIn := []int64{1, 3, 224, 224} + tshapeOut := []int64{1, 1000} // Allocate input Array inX, err := gotvm.Empty(tshapeIn, "float32", gotvm.CPU(0)) diff --git a/golang/sample/gen_mobilenet_lib.py b/golang/sample/gen_mobilenet_lib.py new file mode 100644 index 000000000000..a7e34196e59b --- /dev/null +++ b/golang/sample/gen_mobilenet_lib.py @@ -0,0 +1,38 @@ +import tvm +from tvm import relay +import tvm.relay.testing + +###################################################################### +# Load Neural Network in Relay +#################################################### + +mod, params = relay.testing.mobilenet.get_workload(batch_size=1) + +# set show_meta_data=True if you want to show meta data +print(mod.astext(show_meta_data=False)) + +###################################################################### +# Compilation +#################################################### + +target = 'llvm' + +# Build with Relay +with relay.build_config(opt_level=0): + graph, lib, params = relay.build_module.build( + mod, target, params=params) + +###################################################################### +# Save and Load Compiled Module +# ----------------------------- +# We can also save the graph, lib and parameters into files +#################################################### + +lib.export_library("./mobilenet.so") +print('lib export succeefully') + +with open("./mobilenet.json", "w") as fo: + fo.write(graph) + +with open("./mobilenet.params", "wb") as fo: + fo.write(relay.save_param_dict(params)) From 309dd6834272da26fdfa622098bced8aefd2f53a Mon Sep 17 00:00:00 2001 From: cchung100m Date: Wed, 20 Nov 2019 21:29:06 +0800 Subject: [PATCH 2/4] [Golang][Doc] add asf header --- golang/sample/gen_mobilenet_lib.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/golang/sample/gen_mobilenet_lib.py b/golang/sample/gen_mobilenet_lib.py index a7e34196e59b..2ea194119f01 100644 --- a/golang/sample/gen_mobilenet_lib.py +++ b/golang/sample/gen_mobilenet_lib.py @@ -1,3 +1,20 @@ +# 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. + import tvm from tvm import relay import tvm.relay.testing From dcb1ab3cd7283a515f560c79253bca76f5b16ba9 Mon Sep 17 00:00:00 2001 From: cchung100m Date: Thu, 21 Nov 2019 23:25:23 +0800 Subject: [PATCH 3/4] [Golang][Doc] Improve the end to end example --- golang/sample/complex.go | 2 +- golang/sample/gen_mobilenet_lib.py | 66 +++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/golang/sample/complex.go b/golang/sample/complex.go index 4f0c182fd3b5..4dd21507cfa2 100644 --- a/golang/sample/complex.go +++ b/golang/sample/complex.go @@ -88,7 +88,7 @@ func main() { fmt.Printf("Graph runtime Created\n") // Array allocation attributes - tshapeIn := []int64{1, 3, 224, 224} + tshapeIn := []int64{1, 224, 224, 3} tshapeOut := []int64{1, 1000} // Allocate input Array diff --git a/golang/sample/gen_mobilenet_lib.py b/golang/sample/gen_mobilenet_lib.py index 2ea194119f01..6afa8a284319 100644 --- a/golang/sample/gen_mobilenet_lib.py +++ b/golang/sample/gen_mobilenet_lib.py @@ -15,35 +15,71 @@ # specific language governing permissions and limitations # under the License. -import tvm +import os from tvm import relay -import tvm.relay.testing +from tvm.contrib.download import download_testdata +import tflite.Model -###################################################################### + +################################################ +# Utils for downloading and extracting zip files +# ---------------------------------------------- +def extract(path): + import tarfile + if path.endswith("tgz") or path.endswith("gz"): + dir_path = os.path.dirname(path) + tar = tarfile.open(path) + tar.extractall(path=dir_path) + tar.close() + else: + raise RuntimeError('Could not decompress the file: ' + path) + + +######################################################### +# Download TFLite pre-trained model +# ------------------------------------------------------- + +model_url = "https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz" +model_path = download_testdata(model_url, "mobilenet_v2_1.4_224.tgz", module=['tf', 'official']) +model_dir = os.path.dirname(model_path) +extract(model_path) + +# now we have mobilenet_v2_1.4_224.tflite on disk +model_file = os.path.join(model_dir, "mobilenet_v2_1.4_224.tflite") + +# get TFLite model from buffer +tflite_model_buf = open(model_file, "rb").read() +tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) + + +############################## # Load Neural Network in Relay -#################################################### +# ---------------------------- -mod, params = relay.testing.mobilenet.get_workload(batch_size=1) +# TFLite input tensor name, shape and type +input_tensor = "input" +input_shape = (1, 224, 224, 3) +input_dtype = "float32" -# set show_meta_data=True if you want to show meta data -print(mod.astext(show_meta_data=False)) +# parse TFLite model and convert into Relay computation graph +mod, params = relay.frontend.from_tflite(tflite_model, + shape_dict={input_tensor: input_shape}, + dtype_dict={input_tensor: input_dtype}) -###################################################################### +############# # Compilation -#################################################### +# ----------- target = 'llvm' # Build with Relay -with relay.build_config(opt_level=0): +with relay.build_config(opt_level=3): graph, lib, params = relay.build_module.build( mod, target, params=params) -###################################################################### -# Save and Load Compiled Module -# ----------------------------- -# We can also save the graph, lib and parameters into files -#################################################### +########################################################### +# Save the graph, lib and parameters into files +# --------------------------------------------------------- lib.export_library("./mobilenet.so") print('lib export succeefully') From bc630a3ec03f7cd9ed43b135271ee3bdd26dacf5 Mon Sep 17 00:00:00 2001 From: cchung100m Date: Thu, 21 Nov 2019 23:31:46 +0800 Subject: [PATCH 4/4] [Golang][Doc] Improve the end to end example --- golang/sample/gen_mobilenet_lib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/golang/sample/gen_mobilenet_lib.py b/golang/sample/gen_mobilenet_lib.py index 6afa8a284319..4f6a615d14c9 100644 --- a/golang/sample/gen_mobilenet_lib.py +++ b/golang/sample/gen_mobilenet_lib.py @@ -35,9 +35,9 @@ def extract(path): raise RuntimeError('Could not decompress the file: ' + path) -######################################################### +################################### # Download TFLite pre-trained model -# ------------------------------------------------------- +# --------------------------------- model_url = "https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz" model_path = download_testdata(model_url, "mobilenet_v2_1.4_224.tgz", module=['tf', 'official']) @@ -77,9 +77,9 @@ def extract(path): graph, lib, params = relay.build_module.build( mod, target, params=params) -########################################################### +############################################### # Save the graph, lib and parameters into files -# --------------------------------------------------------- +# --------------------------------------------- lib.export_library("./mobilenet.so") print('lib export succeefully')