1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20/*! 21 * \brief Sample golang application deployment over tvm. 22 * \file simple.go 23 */ 24 25package main 26 27import ( 28 "fmt" 29 "runtime" 30 "./gotvm" 31 "math/rand" 32) 33 34// NNVM compiled model paths. 35const ( 36 modLib = "./deploy.so" 37) 38 39// main 40func main() { 41 // Welcome 42 defer runtime.GC() 43 fmt.Printf("TVM Version : v%v\n", gotvm.TVMVersion) 44 fmt.Printf("DLPACK Version: v%v\n\n", gotvm.DLPackVersion) 45 46 // Import tvm module (so) 47 modp, _ := gotvm.LoadModuleFromFile(modLib) 48 fmt.Printf("Module Imported\n") 49 50 51 // Allocate Array for inputs and outputs. 52 // Allocation by explicit type and context. 53 tshapeIn := []int64{4} 54 inX, _ := gotvm.Empty(tshapeIn, "float32", gotvm.CPU(0)) 55 56 // Default allocation on CPU 57 inY, _ := gotvm.Empty(tshapeIn, "float32") 58 59 // Default allocation to type "float32" and on CPU 60 out, _ := gotvm.Empty(tshapeIn) 61 fmt.Printf("Input and Output Arrays allocated\n") 62 63 // Fill Input Data : inX , inY 64 inXSlice := make([]float32, 4) 65 inYSlice := make([]float32, 4) 66 for i := range inXSlice { 67 inXSlice[i] = rand.Float32() 68 inYSlice[i] = rand.Float32() 69 } 70 71 72 // Copy the data on target memory through runtime CopyFrom api. 73 inX.CopyFrom(inXSlice) 74 inY.CopyFrom(inYSlice) 75 fmt.Printf("X: %v\n", inXSlice) 76 fmt.Printf("Y: %v\n", inYSlice) 77 78 // Get function "myadd" 79 funp, _ := modp.GetFunction("myadd") 80 81 // Call function 82 funp.Invoke(inX, inY, out) 83 fmt.Printf("Module function myadd executed\n") 84 85 // Get the output tensor as an interface holding a slice through runtime CopyTo api. 86 outSlice, _ := out.AsSlice() 87 88 // Print results 89 fmt.Printf("Result:%v\n", outSlice.([]float32)) 90} 91