tvm
session.h
Go to the documentation of this file.
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  */
73 #ifndef TVM_RUNTIME_DISCO_SESSION_H_
74 #define TVM_RUNTIME_DISCO_SESSION_H_
75 
76 #include <tvm/ffi/function.h>
77 #include <tvm/runtime/int_tuple.h>
78 #include <tvm/runtime/object.h>
79 #include <tvm/runtime/tensor.h>
80 
81 #include <mutex>
82 #include <queue>
83 #include <string>
84 #include <utility>
85 
86 namespace tvm {
87 namespace runtime {
88 
92 enum class DiscoAction : int32_t {
93  kShutDown = 0,
94  kKillReg = 1,
95  kGetGlobalFunc = 2,
96  kCallPacked = 3,
97  kSyncWorker = 4,
98  kCopyFromWorker0 = 5,
99  kCopyToWorker0 = 6,
101  kDebugSetRegister = 8,
102 };
103 
105 inline std::string DiscoAction2String(DiscoAction action) {
106  switch (action) {
108  return "kShutDown";
110  return "kKillReg";
112  return "kGetGlobalFunc";
114  return "kCallPacked";
116  return "kSyncWorker";
118  return "kCopyFromWorker0";
120  return "kCopyToWorker0";
122  return "kDebugGetFromRemote";
124  return "kDebugSetRegister";
125  }
126  TVM_FFI_THROW(ValueError) << "Unknown DiscoAction: " << static_cast<int>(action);
127 }
128 
129 class SessionObj;
130 
137 class DRefObj : public Object {
138  public:
140  inline ~DRefObj();
146  inline ffi::Any DebugGetFromRemote(int worker_id);
152  inline void DebugCopyFrom(int worker_id, ffi::AnyView source);
153 
154  static constexpr const uint32_t _type_index = TypeIndex::kRuntimeDiscoDRef;
155  static const constexpr bool _type_final = true;
156  static constexpr const bool _type_mutable = true;
157  TVM_FFI_DECLARE_OBJECT_INFO_STATIC("runtime.disco.DRef", DRefObj, Object);
158 
160  int64_t reg_id;
162  ObjectRef session{nullptr};
163 
164  private:
165  inline SessionObj* GetSession();
166 };
167 
173 class DRef : public ObjectRef {
174  public:
175  explicit DRef(ObjectPtr<DRefObj> data) : ObjectRef(data) { TVM_FFI_ICHECK(data != nullptr); }
177 };
178 
183 class SessionObj : public Object {
184  public:
185  virtual ~SessionObj() = default;
201  template <typename... Args>
202  DRef TVM_ALWAYS_INLINE CallPacked(const DRef& func, Args&&... args);
209  TVM_DLL virtual DRef CallWithPacked(const ffi::PackedArgs& args) = 0;
211  TVM_DLL virtual int64_t GetNumWorkers() = 0;
213  TVM_DLL virtual DRef GetGlobalFunc(const std::string& name) = 0;
219  TVM_DLL virtual void CopyFromWorker0(const Tensor& host_array, const DRef& remote_array) = 0;
225  TVM_DLL virtual void CopyToWorker0(const Tensor& host_array, const DRef& remote_array) = 0;
233  TVM_DLL virtual void SyncWorker(int worker_id) = 0;
235  TVM_DLL virtual void Shutdown() = 0;
241  TVM_DLL virtual void InitCCL(ffi::String ccl, IntTuple device_ids) = 0;
248  TVM_DLL virtual ffi::Any DebugGetFromRemote(int64_t reg_id, int worker_id) = 0;
255  TVM_DLL virtual void DebugSetRegister(int64_t reg_id, ffi::AnyView value, int worker_id) = 0;
256 
257  struct FFI;
258  friend struct SessionObj::FFI;
259  friend class DRefObj;
260 
261  static constexpr const bool _type_mutable = true;
262  TVM_FFI_DECLARE_OBJECT_INFO("runtime.disco.Session", SessionObj, Object);
263 
264  protected:
266  virtual void DeallocReg(int reg_id) = 0;
267 };
268 
273 class Session : public ObjectRef {
274  public:
280  TVM_DLL static Session ThreadedSession(int num_workers, int num_groups);
293  TVM_DLL static Session ProcessSession(int num_workers, int num_groups,
294  ffi::String process_pool_creator, ffi::String entrypoint);
295 
297 };
298 
304  public:
305  virtual ~DiscoChannel() = default;
307  virtual void Send(const ffi::PackedArgs& args) = 0;
309  virtual ffi::PackedArgs Recv() = 0;
311  virtual void Reply(const ffi::PackedArgs& args) = 0;
313  virtual ffi::PackedArgs RecvReply() = 0;
314 };
315 
321  public:
326  std::queue<Tensor> host_arrays;
328  std::mutex queue_mutex_;
329 };
330 
331 // Implementation details
332 
333 inline SessionObj* DRefObj::GetSession() {
334  return const_cast<SessionObj*>(static_cast<const SessionObj*>(session.get()));
335 }
336 
338  if (this->session.defined()) {
339  GetSession()->DeallocReg(reg_id);
340  }
341 }
342 
343 ffi::Any DRefObj::DebugGetFromRemote(int worker_id) {
344  return GetSession()->DebugGetFromRemote(this->reg_id, worker_id);
345 }
346 
347 void DRefObj::DebugCopyFrom(int worker_id, ffi::AnyView value) {
348  return GetSession()->DebugSetRegister(this->reg_id, value, worker_id);
349 }
350 
351 template <typename... Args>
352 DRef SessionObj::CallPacked(const DRef& func, Args&&... args) {
353  constexpr int offset = 3;
354  constexpr int kNumArgs = offset + sizeof...(Args);
355  ffi::AnyView packed_args[kNumArgs];
356  ffi::PackedArgs::Fill(packed_args,
357  /*.0=*/static_cast<int>(DiscoAction::kCallPacked), // action
358  /*.1=*/0, // reg_id, which will be updated by this->CallWithPacked
359  /*.2=*/func, // the function to be called
360  std::forward<Args>(args)...);
361  return this->CallWithPacked(ffi::PackedArgs(packed_args, kNumArgs));
362 }
363 
364 } // namespace runtime
365 } // namespace tvm
366 #endif // TVM_RUNTIME_DISCO_SESSION_H_
An object that exists on all workers.
Definition: session.h:137
TVM_FFI_DECLARE_OBJECT_INFO_STATIC("runtime.disco.DRef", DRefObj, Object)
~DRefObj()
Definition: session.h:337
static constexpr const bool _type_mutable
Definition: session.h:156
static constexpr const uint32_t _type_index
Definition: session.h:154
ffi::Any DebugGetFromRemote(int worker_id)
Get the value of a DRef from a remote worker.
Definition: session.h:343
int64_t reg_id
The id of the register.
Definition: session.h:160
void DebugCopyFrom(int worker_id, ffi::AnyView source)
Copy from the Tensor provided to a remote worker.
Definition: session.h:347
static constexpr const bool _type_final
Definition: session.h:155
ObjectRef session
Back-pointer to the host controler session.
Definition: session.h:162
Managed reference to DRefObj.
Definition: session.h:173
DRef(ObjectPtr< DRefObj > data)
Definition: session.h:175
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DRef, ObjectRef, DRefObj)
A bi-directional channel for controler-worker communication. This channel is primarily used to transf...
Definition: session.h:303
virtual void Reply(const ffi::PackedArgs &args)=0
Reply a packed sequence to the sender.
virtual ffi::PackedArgs Recv()=0
Receive a packed sequence from worker.
virtual ~DiscoChannel()=default
virtual void Send(const ffi::PackedArgs &args)=0
Send a packed sequence to the receiver.
virtual ffi::PackedArgs RecvReply()=0
Receive a reply from the worker.
A Disco interactive session. It allows users to interact with the Disco command queue with various ff...
Definition: session.h:183
virtual void CopyToWorker0(const Tensor &host_array, const DRef &remote_array)=0
Copy the controler-side Tensor to worker-0.
virtual DRef GetGlobalFunc(const std::string &name)=0
Get a global functions on workers.
friend struct SessionObj::FFI
Definition: session.h:257
virtual void Shutdown()=0
Signal all the workers to shutdown.
virtual DRef CallWithPacked(const ffi::PackedArgs &args)=0
Call packed function on each worker using a packed sequence. The calling convention: The first elemen...
virtual void DeallocReg(int reg_id)=0
Deallocate a register id, kill it on all workers, and append it to free_regs_.
virtual ffi::Any DebugGetFromRemote(int64_t reg_id, int worker_id)=0
Get the value of a register from a remote worker.
virtual void SyncWorker(int worker_id)=0
Synchrnoize the controler with a worker, and it will wait until worker finishes executing this instru...
static constexpr const bool _type_mutable
Definition: session.h:261
virtual int64_t GetNumWorkers()=0
Get the number of workers in the session.
virtual void CopyFromWorker0(const Tensor &host_array, const DRef &remote_array)=0
Copy an Tensor from worker-0 to the controler-side Tensor.
virtual ~SessionObj()=default
virtual void InitCCL(ffi::String ccl, IntTuple device_ids)=0
Initialize the data plane between workers.
TVM_FFI_DECLARE_OBJECT_INFO("runtime.disco.Session", SessionObj, Object)
DRef TVM_ALWAYS_INLINE CallPacked(const DRef &func, Args &&... args)
Call a ffi::Function on workers providing variadic arguments.
virtual void DebugSetRegister(int64_t reg_id, ffi::AnyView value, int worker_id)=0
Set the value of a register on a remote worker.
Managed reference to SessionObj.
Definition: session.h:273
static Session ProcessSession(int num_workers, int num_groups, ffi::String process_pool_creator, ffi::String entrypoint)
Create a session backed by pipe-based multiprocessing.
static Session ThreadedSession(int num_workers, int num_groups)
Create a session backed by a thread pool of workers.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Session, ObjectRef, SessionObj)
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:54
A special communication channel between controler and worker-0, assuming they are always collocated i...
Definition: session.h:320
std::mutex queue_mutex_
The mutex that guards host_arrays
Definition: session.h:328
std::queue< Tensor > host_arrays
The host-side arrays to passed to worker-0 for special uses, for example, copy-to-worker0 and copy-fr...
Definition: session.h:326
Defines tuple of integers.
std::string DiscoAction2String(DiscoAction action)
Converts the enum class DiscoAction to string.
Definition: session.h:105
DiscoAction
All possible kinds of Disco commands.
Definition: session.h:92
@ kRuntimeDiscoDRef
runtime::DRef for disco distributed runtime
Definition: object.h:65
ffi::Shape IntTuple
Definition: int_tuple.h:33
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
A managed object in the TVM runtime.
A device-independent managed Tensor abstraction.