tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] areusch commented on a change in pull request #5484: [REFACTOR][RPC][PROCOTOL-CHANGE] Modularize the RPC infra
Date Mon, 04 May 2020 18:54:17 GMT

areusch commented on a change in pull request #5484:
URL: https://github.com/apache/incubator-tvm/pull/5484#discussion_r419654640



##########
File path: src/runtime/rpc/minrpc/minrpc_server.h
##########
@@ -0,0 +1,598 @@
+/*
+ * 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 minrpc_server.h
+ * \brief Minimum RPC server implementation,
+ *        redirects all the calls to C runtime API.
+ *
+ * \note This file do not depend on c++ std or c std,
+ *       and only depends on TVM's C runtime API.
+ */
+#ifndef TVM_RUNTIME_RPC_MINRPC_MINRPC_SERVER_H_
+#define TVM_RUNTIME_RPC_MINRPC_MINRPC_SERVER_H_
+
+#include <dmlc/endian.h>
+#include <tvm/runtime/c_runtime_api.h>
+#include "../rpc_protocol.h"
+#include "../../../support/arena.h"
+
+/*! \brief Whether or not to enable glog style DLOG */
+#ifndef TVM_MINRPC_ENABLE_LOGGING
+#define TVM_MINRPC_ENABLE_LOGGING 0
+#endif
+
+#ifndef MINRPC_CHECK
+#define MINRPC_CHECK(cond)                                      \
+  if (!(cond)) this->ThrowError(RPCServerStatus::kCheckError);
+#endif
+
+#if TVM_MINRPC_ENABLE_LOGGING
+#include <dmlc/logging.h>
+#endif
+
+
+namespace tvm {
+namespace runtime {
+
+/*!
+ * \brief A minimum RPC server that only depends on the tvm C runtime..
+ *
+ *  All the dependencies are provided by the io arguments.
+ *
+ * \tparam TIOHandler IO provider to provide io handling.
+ *         An IOHandler needs to provide the following functions:
+ *         - PosixWrite, PosixRead, Close: posix style, read, write, close API.
+ *         - Exit: exit with status code.
+ */
+template<typename TIOHandler>
+class MinRPCServer {
+ public:
+  /*!
+   * \brief Constructor.
+   * \param io The IO handler.
+   */
+  explicit MinRPCServer(TIOHandler io)
+      : io_(io), arena_(PageAllocator(io)) {}
+
+  /*! \brief Run the server loop until shutdown signal is received. */
+  void ServerLoop() {
+    RPCCode code;
+    uint64_t packet_len;
+
+    while (true) {
+      arena_.RecycleAll();
+      allow_clean_shutdown_ = true;
+
+      this->Read(&packet_len);
+      if (packet_len == 0) continue;
+      this->Read(&code);
+
+      allow_clean_shutdown_ = false;
+
+      if (code >= RPCCode::kSyscallCodeStart) {
+        this->HandleSyscallFunc(code);
+      } else {
+        switch (code) {
+          case RPCCode::kCallFunc: {
+            HandleNormalCallFunc();
+            break;
+          }
+          case RPCCode::kInitServer: {
+            HandleInitServer();
+            break;
+          }
+          case RPCCode::kCopyFromRemote: {
+            HandleCopyFromRemote();
+            break;
+          }
+          case RPCCode::kCopyToRemote: {
+            HandleCopyToRemote();
+            break;
+          }
+          case RPCCode::kShutdown: {
+            this->Shutdown();
+            return;
+          }
+          default: {
+            this->ThrowError(RPCServerStatus::kUnknownRPCCode);
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  void Shutdown() {
+    arena_.FreeAll();
+    io_.Close();
+  }
+
+  void HandleNormalCallFunc() {
+    uint64_t call_handle;
+    TVMValue* values;
+    int* tcodes;
+    int num_args;
+    TVMValue ret_value[3];
+    int ret_tcode[3];
+
+    this->Read(&call_handle);
+    RecvPackedSeq(&values, &tcodes, &num_args);
+
+    int call_ecode = TVMFuncCall(
+        reinterpret_cast<void*>(call_handle),
+        values, tcodes, num_args,
+        &(ret_value[1]), &(ret_tcode[1]));
+
+    if (call_ecode == 0) {
+      // Return value encoding as in LocalSession
+      int rv_tcode = ret_tcode[1];
+      ret_tcode[0] = kDLInt;
+      ret_value[0].v_int64 = rv_tcode;
+      if (rv_tcode == kTVMNDArrayHandle) {
+        ret_tcode[1] = kTVMDLTensorHandle;
+        ret_value[2].v_handle = ret_value[1].v_handle;
+        ret_tcode[2] = kTVMOpaqueHandle;
+        this->ReturnPackedSeq(ret_value, ret_tcode, 3);
+      } else if (rv_tcode == kTVMPackedFuncHandle ||
+                 rv_tcode == kTVMModuleHandle) {
+        ret_tcode[1] = kTVMOpaqueHandle;
+        this->ReturnPackedSeq(ret_value, ret_tcode, 2);
+      } else {
+        this->ReturnPackedSeq(ret_value, ret_tcode, 2);
+      }
+    } else {
+      this->ReturnLastTVMError();
+    }
+  }
+
+  void HandleCopyFromRemote() {
+    uint64_t handle, offset, num_bytes;
+    TVMContext ctx;
+    DLDataType type_hint;
+
+    this->Read(&handle);
+    this->Read(&offset);
+    this->Read(&num_bytes);
+    this->Read(&ctx);
+    this->Read(&type_hint);
+
+    char* data_ptr;
+    int call_ecode = 0;
+    if (ctx.device_type == kDLCPU) {

Review comment:
       I think here we don't need this check anymore, right? we always want the else case?
should we add a CHECK?




----------------------------------------------------------------
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



Mime
View raw message