tvm
Loading...
Searching...
No Matches
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/container/shape.h>
77#include <tvm/ffi/function.h>
79#include <tvm/runtime/tensor.h>
80
81#include <mutex>
82#include <queue>
83#include <string>
84#include <utility>
85
86namespace tvm {
87namespace runtime {
88
97constexpr int32_t kRuntimeDiscoDRef = TVMFFITypeIndex::kTVMFFIDynObjectBegin - 14;
98
99static_assert(kRuntimeDiscoDRef >= TVMFFITypeIndex::kTVMFFIStaticObjectEnd &&
100 kRuntimeDiscoDRef < TVMFFITypeIndex::kTVMFFIDynObjectBegin,
101 "kRuntimeDiscoDRef must live in the static custom-index slot range");
102
106enum class DiscoAction : int32_t {
107 kShutDown = 0,
108 kKillReg = 1,
109 kGetGlobalFunc = 2,
110 kCallPacked = 3,
111 kSyncWorker = 4,
113 kCopyToWorker0 = 6,
116};
117
119inline std::string DiscoAction2String(DiscoAction action) {
120 switch (action) {
122 return "kShutDown";
124 return "kKillReg";
126 return "kGetGlobalFunc";
128 return "kCallPacked";
130 return "kSyncWorker";
132 return "kCopyFromWorker0";
134 return "kCopyToWorker0";
136 return "kDebugGetFromRemote";
138 return "kDebugSetRegister";
139 }
140 TVM_FFI_THROW(ValueError) << "Unknown DiscoAction: " << static_cast<int>(action);
141}
142
143class SessionObj;
144
151class DRefObj : public ffi::Object {
152 public:
154 inline ~DRefObj();
160 inline ffi::Any DebugGetFromRemote(int worker_id);
166 inline void DebugCopyFrom(int worker_id, ffi::AnyView source);
167
168 static constexpr const uint32_t _type_index = kRuntimeDiscoDRef;
169 static const constexpr bool _type_final = true;
170 static constexpr const bool _type_mutable = true;
171 TVM_FFI_DECLARE_OBJECT_INFO_STATIC("runtime.disco.DRef", DRefObj, ffi::Object);
172
174 int64_t reg_id;
176 ffi::ObjectRef session{nullptr};
177
178 private:
179 inline SessionObj* GetSession();
180};
181
187class DRef : public ffi::ObjectRef {
188 public:
189 explicit DRef(ffi::ObjectPtr<DRefObj> data) : ffi::ObjectRef(data) {
190 TVM_FFI_ICHECK(data != nullptr);
191 }
193};
194
199class SessionObj : public ffi::Object {
200 public:
201 virtual ~SessionObj() = default;
217 template <typename... Args>
218 TVM_FFI_INLINE DRef CallPacked(const DRef& func, Args&&... args);
225 TVM_RUNTIME_EXTRA_DLL virtual DRef CallWithPacked(const ffi::PackedArgs& args) = 0;
229 TVM_RUNTIME_EXTRA_DLL virtual DRef GetGlobalFunc(const std::string& name) = 0;
235 TVM_RUNTIME_EXTRA_DLL virtual void CopyFromWorker0(const ffi::Tensor& host_array,
236 const DRef& remote_array) = 0;
242 TVM_RUNTIME_EXTRA_DLL virtual void CopyToWorker0(const ffi::Tensor& host_array,
243 const DRef& remote_array) = 0;
251 TVM_RUNTIME_EXTRA_DLL virtual void SyncWorker(int worker_id) = 0;
253 TVM_RUNTIME_EXTRA_DLL virtual void Shutdown() = 0;
259 TVM_RUNTIME_EXTRA_DLL virtual void InitCCL(ffi::String ccl, ffi::Shape device_ids) = 0;
266 TVM_RUNTIME_EXTRA_DLL virtual ffi::Any DebugGetFromRemote(int64_t reg_id, int worker_id) = 0;
273 TVM_RUNTIME_EXTRA_DLL virtual void DebugSetRegister(int64_t reg_id, ffi::AnyView value,
274 int worker_id) = 0;
275
276 struct FFI;
277 friend struct SessionObj::FFI;
278 friend class DRefObj;
279
280 static constexpr const bool _type_mutable = true;
281 TVM_FFI_DECLARE_OBJECT_INFO("runtime.disco.Session", SessionObj, ffi::Object);
282
283 protected:
285 virtual void DeallocReg(int reg_id) = 0;
286};
287
292class Session : public ffi::ObjectRef {
293 public:
299 TVM_RUNTIME_EXTRA_DLL static Session ThreadedSession(int num_workers, int num_groups);
312 TVM_RUNTIME_EXTRA_DLL static Session ProcessSession(int num_workers, int num_groups,
313 ffi::String process_pool_creator,
314 ffi::String entrypoint);
315
316 explicit Session(ffi::UnsafeInit tag, ffi::ObjectPtr<SessionObj> n) : ffi::ObjectRef(tag) {
317 data_ = std::move(n);
318 }
319
321};
322
328 public:
329 virtual ~DiscoChannel() = default;
331 virtual void Send(const ffi::PackedArgs& args) = 0;
333 virtual ffi::PackedArgs Recv() = 0;
335 virtual void Reply(const ffi::PackedArgs& args) = 0;
337 virtual ffi::PackedArgs RecvReply() = 0;
338};
339
345 public:
350 std::queue<ffi::Tensor> host_arrays;
352 std::mutex queue_mutex_;
353};
354
355// Implementation details
356
357inline SessionObj* DRefObj::GetSession() {
358 return const_cast<SessionObj*>(static_cast<const SessionObj*>(session.get()));
359}
360
362 if (this->session.defined()) {
363 GetSession()->DeallocReg(reg_id);
364 }
365}
366
367ffi::Any DRefObj::DebugGetFromRemote(int worker_id) {
368 return GetSession()->DebugGetFromRemote(this->reg_id, worker_id);
369}
370
371void DRefObj::DebugCopyFrom(int worker_id, ffi::AnyView value) {
372 return GetSession()->DebugSetRegister(this->reg_id, value, worker_id);
373}
374
375template <typename... Args>
376DRef SessionObj::CallPacked(const DRef& func, Args&&... args) {
377 constexpr int offset = 3;
378 constexpr int kNumArgs = offset + sizeof...(Args);
379 ffi::AnyView packed_args[kNumArgs];
380 ffi::PackedArgs::Fill(packed_args,
381 /*.0=*/static_cast<int>(DiscoAction::kCallPacked), // action
382 /*.1=*/0, // reg_id, which will be updated by this->CallWithPacked
383 /*.2=*/func, // the function to be called
384 std::forward<Args>(args)...);
385 return this->CallWithPacked(ffi::PackedArgs(packed_args, kNumArgs));
386}
387
388} // namespace runtime
389} // namespace tvm
390#endif // TVM_RUNTIME_DISCO_SESSION_H_
Device-independent tensor allocation, copies, views and serialization.
An object that exists on all workers.
Definition session.h:151
~DRefObj()
Definition session.h:361
ffi::ObjectRef session
Back-pointer to the host controler session.
Definition session.h:176
TVM_FFI_DECLARE_OBJECT_INFO_STATIC("runtime.disco.DRef", DRefObj, ffi::Object)
static constexpr const bool _type_mutable
Definition session.h:170
static constexpr const uint32_t _type_index
Definition session.h:168
ffi::Any DebugGetFromRemote(int worker_id)
Get the value of a DRef from a remote worker.
Definition session.h:367
int64_t reg_id
The id of the register.
Definition session.h:174
void DebugCopyFrom(int worker_id, ffi::AnyView source)
Copy from the Tensor provided to a remote worker.
Definition session.h:371
static const constexpr bool _type_final
Definition session.h:169
Managed reference to DRefObj.
Definition session.h:187
DRef(ffi::ObjectPtr< DRefObj > data)
Definition session.h:189
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DRef, ffi::ObjectRef, DRefObj)
A bi-directional channel for controler-worker communication. This channel is primarily used to transf...
Definition session.h:327
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:199
virtual TVM_RUNTIME_EXTRA_DLL DRef CallWithPacked(const ffi::PackedArgs &args)=0
Call packed function on each worker using a packed sequence. The calling convention: The first elemen...
virtual TVM_RUNTIME_EXTRA_DLL void Shutdown()=0
Signal all the workers to shutdown.
virtual TVM_RUNTIME_EXTRA_DLL void SyncWorker(int worker_id)=0
Synchrnoize the controler with a worker, and it will wait until worker finishes executing this instru...
friend struct SessionObj::FFI
Definition session.h:277
virtual TVM_RUNTIME_EXTRA_DLL void InitCCL(ffi::String ccl, ffi::Shape device_ids)=0
Initialize the data plane between workers.
virtual void DeallocReg(int reg_id)=0
Deallocate a register id, kill it on all workers, and append it to free_regs_.
virtual TVM_RUNTIME_EXTRA_DLL DRef GetGlobalFunc(const std::string &name)=0
Get a global functions on workers.
TVM_FFI_DECLARE_OBJECT_INFO("runtime.disco.Session", SessionObj, ffi::Object)
virtual TVM_RUNTIME_EXTRA_DLL ffi::Any DebugGetFromRemote(int64_t reg_id, int worker_id)=0
Get the value of a register from a remote worker.
static constexpr const bool _type_mutable
Definition session.h:280
virtual TVM_RUNTIME_EXTRA_DLL int64_t GetNumWorkers()=0
Get the number of workers in the session.
virtual ~SessionObj()=default
virtual TVM_RUNTIME_EXTRA_DLL void CopyFromWorker0(const ffi::Tensor &host_array, const DRef &remote_array)=0
Copy an Tensor from worker-0 to the controler-side Tensor.
virtual TVM_RUNTIME_EXTRA_DLL void CopyToWorker0(const ffi::Tensor &host_array, const DRef &remote_array)=0
Copy the controler-side Tensor to worker-0.
TVM_FFI_INLINE DRef CallPacked(const DRef &func, Args &&... args)
Call a ffi::Function on workers providing variadic arguments.
virtual TVM_RUNTIME_EXTRA_DLL 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:292
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Session, ffi::ObjectRef, SessionObj)
static TVM_RUNTIME_EXTRA_DLL Session ThreadedSession(int num_workers, int num_groups)
Create a session backed by a thread pool of workers.
static TVM_RUNTIME_EXTRA_DLL Session ProcessSession(int num_workers, int num_groups, ffi::String process_pool_creator, ffi::String entrypoint)
Create a session backed by pipe-based multiprocessing.
Session(ffi::UnsafeInit tag, ffi::ObjectPtr< SessionObj > n)
Definition session.h:316
A special communication channel between controler and worker-0, assuming they are always collocated i...
Definition session.h:344
std::mutex queue_mutex_
The mutex that guards host_arrays
Definition session.h:352
std::queue< ffi::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:350
Parent runtime-extra symbol visibility.
#define TVM_RUNTIME_EXTRA_DLL
Definition extra_base.h:45
std::string DiscoAction2String(DiscoAction action)
Converts the enum class DiscoAction to string.
Definition session.h:119
DiscoAction
All possible kinds of Disco commands.
Definition session.h:106
constexpr int32_t kRuntimeDiscoDRef
Static FFI type index for runtime::disco::DRef.
Definition session.h:97
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41