You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/07/03 12:55:05 UTC

[GitHub] [incubator-tvm] liangfu commented on a change in pull request #5921: µTVM CRT modifications for on-device RPC server

liangfu commented on a change in pull request #5921:
URL: https://github.com/apache/incubator-tvm/pull/5921#discussion_r446453520



##########
File path: include/tvm/runtime/crt/graph_runtime.h
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file graph_runtime.h
+ * \brief Tiny graph runtime that can run graph containing only tvm PackedFunc.
+ */
+#ifndef TVM_RUNTIME_CRT_GRAPH_RUNTIME_H_
+#define TVM_RUNTIME_CRT_GRAPH_RUNTIME_H_
+
+#include <dlpack/dlpack.h>
+#include <tvm/runtime/c_runtime_api.h>
+
+struct TVMPackedFunc;
+typedef struct TVMPackedFunc TVMPackedFunc;
+
+struct TVMModule;
+typedef struct TVMModule TVMModule;
+
+/*! \brief operator attributes about tvm op */
+typedef struct TVMOpParam {
+  char func_name[120];
+  uint32_t num_inputs;
+  uint32_t num_outputs;
+  uint32_t flatten_data;
+} TVMOpParam;
+
+// Graph attribute
+typedef struct TVMGraphRuntimeGraphAttr {
+  uint32_t storage_num_not_alloctaed;
+  uint32_t* storage_id;
+  uint32_t* device_index;
+  char* dltype;  // "int8", "int16", "float32"
+  uint32_t dltype_count;
+  int64_t* shape;
+  uint32_t* ndim;
+  uint32_t shape_count;
+} TVMGraphRuntimeGraphAttr;
+
+typedef DLTensor* DLTensorPtr;
+
+/*!
+ * \brief Tiny graph runtime.
+ *
+ *  This runtime can be acccesibly in various language via
+ *  TVM runtime PackedFunc API.
+ */
+/* class GraphRuntime : public ModuleNode { */
+typedef struct TVMGraphRuntimeAPI {

Review comment:
       Why do we need to add the "API" suffix?

##########
File path: src/runtime/crt/common/crt_runtime_api.c
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.
+ */
+
+// LINT_C_FILE
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <tvm/runtime/c_runtime_api.h>
+#include <tvm/runtime/crt/func_registry.h>
+#include <tvm/runtime/crt/internal/common/ndarray.h>
+#include <tvm/runtime/crt/internal/common/packed_func.h>
+#include <tvm/runtime/crt/internal/graph_runtime/graph_runtime.h>
+#include <tvm/runtime/crt/memory.h>
+#include <tvm/runtime/crt/platform.h>
+
+// Handle internal errors
+
+static char g_last_error[1024];
+
+void TVMAPISetLastError(const char* msg) { strncpy(g_last_error, msg, sizeof(g_last_error)); }
+
+__attribute__((format(printf, 1, 2))) int TVMAPIErrorf(const char* msg, ...) {
+  va_list args;
+  int to_return;
+
+  va_start(args, msg);
+  to_return = vsnprintf(g_last_error, sizeof(g_last_error), msg, args);
+  va_end(args);
+
+  return to_return;
+}
+
+const char* TVMGetLastError(void) { return g_last_error; }
+
+// Manipulate NDArray on target device
+
+int TVMArrayAlloc(const tvm_index_t* shape, int ndim, int dtype_code, int dtype_bits,
+                  int dtype_lanes, int device_type, int device_id, TVMArrayHandle* out) {
+  DLDataType dtype;
+  dtype.code = dtype_code;
+  dtype.bits = dtype_bits;
+  dtype.lanes = dtype_lanes;
+  DLContext ctx;
+  ctx.device_type = (DLDeviceType)device_type;
+  ctx.device_id = device_id;
+  TVMNDArray arr = TVMNDArray_Empty(ndim, shape, dtype, ctx);
+  **out = arr.dl_tensor;
+  return 0;
+}
+
+int TVMArrayFree(TVMArrayHandle handle) {
+  TVMNDArray arr;
+  arr.dl_tensor = *handle;
+  return TVMNDArray_Release(&arr);
+}
+
+int TVMDeviceAllocDataSpace(DLContext ctx, size_t nbytes, size_t alignment, DLDataType type_hint,
+                            void** out_data) {
+  if (alignment != 1) {
+    nbytes = (nbytes + alignment - 1) / alignment * alignment;
+  }
+
+  *out_data = vmalloc(nbytes);
+  return 0;
+}
+
+int TVMDeviceFreeDataSpace(TVMContext ctx, void* ptr) {
+  vfree(ptr);
+  return 0;
+}
+
+int TVMDeviceCopyDataFromTo(const void* from, size_t from_offset, void* to, size_t to_offset,
+                            size_t num_bytes, TVMContext ctx_from, TVMContext ctx_to,
+                            DLDataType type_hint, TVMStreamHandle stream) {
+  memcpy(((uint8_t*)to) + to_offset, ((uint8_t*)from) + from_offset, num_bytes);
+  return 0;
+}
+
+int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream) { return 0; }
+
+static TVMMutableFuncRegistry global_func_registry;
+
+int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f, int override) {
+  return TVMMutableFuncRegistry_Set(&global_func_registry, name, f, override != 0);
+}
+
+static const TVMModule* registered_modules[TVM_CRT_MAX_REGISTERED_MODULES];
+
+/*! \brief Passed as `module_index` to EncodeFunctionHandle. */
+static const tvm_module_index_t kGlobalFuncModuleIndex = TVM_CRT_MAX_REGISTERED_MODULES;
+
+static int DecodeModuleHandle(TVMModuleHandle handle, tvm_module_index_t* out_module_index) {
+  tvm_module_index_t module_index;
+
+  module_index = ((tvm_module_index_t)((uintptr_t)handle)) & ~0x8000;
+  if (module_index > TVM_CRT_MAX_REGISTERED_MODULES || registered_modules[module_index] == NULL) {
+    TVMAPIErrorf("invalid module handle: %08x", module_index);
+    return -1;
+  }
+
+  *out_module_index = module_index;
+  return 0;
+}
+
+static TVMModuleHandle EncodeModuleHandle(tvm_module_index_t module_index) {
+  return (TVMModuleHandle)((uintptr_t)(module_index | 0x8000));
+}
+
+static int _TVMModCreateFromCModule(const TVMModule* mod, TVMModuleHandle* out_handle) {

Review comment:
       consider remove the underscore prefix?

##########
File path: apps/bundle_deploy/build_model.py
##########
@@ -33,19 +34,21 @@ def build_module(opts):
     func = mod["main"]
     func = relay.Function(func.params, relay.nn.softmax(func.body), None, func.type_params, func.attrs)
 
-    with tvm.transform.PassContext(opt_level=3):
+    with tvm.transform.PassContext(opt_level=3, config={'tir.disable_vectorize': True}):
         graph, lib, params = relay.build(
-            func, 'llvm --system-lib', params=params)
+            func, 'c', params=params)

Review comment:
       why should we change this default behavior?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org