Upload folder using huggingface_hub (part 33)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +12 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/socket_fmt.h +35 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/store/TCPStoreBackend.hpp +84 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemory.hpp +164 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryTypes.hpp +56 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryUtils.hpp +199 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/DMAConnectivity.hpp +43 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/NCCLSymmetricMemory.hpp +69 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp +238 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/env.hpp +22 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/intra_node_comm.hpp +98 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/macros.hpp +29 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp +43 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_devcomm_manager.hpp +282 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_ep.hpp +68 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_extension.hpp +57 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nvshmem_extension.hpp +98 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nvshmem_team_manager.hpp +179 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/ucc/UCCTracing.hpp +63 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/ucc/UCCUtils.hpp +192 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/python_placement.h +12 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/agent_utils.h +47 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/message.h +198 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/metrics/RpcMetricsHandler.h +45 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/profiler/remote_profiler_manager.h +60 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/profiler/server_process_global_profiler.h +134 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/py_rref.h +86 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_call.h +34 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_functions.h +71 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_remote_call.h +50 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_resp.h +28 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_rpc_handler.h +134 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback.h +37 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback_impl.h +66 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback_no_python.h +120 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc.h +14 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc_agent.h +354 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc_command_base.h +28 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_context.h +340 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_impl.h +426 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_proto.h +168 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_call.h +72 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_remote_call.h +59 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_resp.h +27 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/tensorpipe_agent.h +498 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/tensorpipe_utils.h +124 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/testing/faulty_tensorpipe_agent.h +109 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/testing/testing.h +14 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/torchscript_functions.h +42 -0
- .venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/types.h +75 -0
.gitattributes
CHANGED
|
@@ -582,3 +582,15 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 582 |
.venv/lib/python3.12/site-packages/torch/fx/__pycache__/graph.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 583 |
.venv/lib/python3.12/site-packages/torch/fx/experimental/__pycache__/proxy_tensor.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 584 |
.venv/lib/python3.12/site-packages/torch/fx/experimental/__pycache__/symbolic_shapes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
.venv/lib/python3.12/site-packages/torch/fx/__pycache__/graph.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 583 |
.venv/lib/python3.12/site-packages/torch/fx/experimental/__pycache__/proxy_tensor.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 584 |
.venv/lib/python3.12/site-packages/torch/fx/experimental/__pycache__/symbolic_shapes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 585 |
+
.venv/lib/python3.12/site-packages/torch/lib/libaoti_custom_ops.so filter=lfs diff=lfs merge=lfs -text
|
| 586 |
+
.venv/lib/python3.12/site-packages/torch/lib/libbackend_with_compiler.so filter=lfs diff=lfs merge=lfs -text
|
| 587 |
+
.venv/lib/python3.12/site-packages/torch/lib/libc10.so filter=lfs diff=lfs merge=lfs -text
|
| 588 |
+
.venv/lib/python3.12/site-packages/torch/lib/libgomp.so.1 filter=lfs diff=lfs merge=lfs -text
|
| 589 |
+
.venv/lib/python3.12/site-packages/torch/lib/libjitbackend_test.so filter=lfs diff=lfs merge=lfs -text
|
| 590 |
+
.venv/lib/python3.12/site-packages/torch/lib/libtorch_cpu.so filter=lfs diff=lfs merge=lfs -text
|
| 591 |
+
.venv/lib/python3.12/site-packages/torch/lib/libtorch_python.so filter=lfs diff=lfs merge=lfs -text
|
| 592 |
+
.venv/lib/python3.12/site-packages/torch/lib/libtorchbind_test.so filter=lfs diff=lfs merge=lfs -text
|
| 593 |
+
.venv/lib/python3.12/site-packages/torch/linalg/__pycache__/__init__.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 594 |
+
.venv/lib/python3.12/site-packages/torch/nested/_internal/__pycache__/ops.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 595 |
+
.venv/lib/python3.12/site-packages/torch/nn/__pycache__/functional.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 596 |
+
.venv/lib/python3.12/site-packages/torch/nn/attention/__pycache__/flex_attention.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/socket_fmt.h
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// (c) Meta Platforms, Inc. and affiliates.
|
| 3 |
+
// All rights reserved.
|
| 4 |
+
//
|
| 5 |
+
// This source code is licensed under the BSD-style license found in the
|
| 6 |
+
// LICENSE file in the root directory of this source tree.
|
| 7 |
+
|
| 8 |
+
#pragma once
|
| 9 |
+
|
| 10 |
+
/*
|
| 11 |
+
This file should not be included from other .h files and only used in cpp files
|
| 12 |
+
as it exposes the underlying platform specific socket headers.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
#include <string>
|
| 16 |
+
|
| 17 |
+
#ifdef _WIN32
|
| 18 |
+
#include <mutex>
|
| 19 |
+
|
| 20 |
+
#include <winsock2.h>
|
| 21 |
+
#include <ws2tcpip.h>
|
| 22 |
+
#else
|
| 23 |
+
#include <netinet/in.h>
|
| 24 |
+
#endif
|
| 25 |
+
|
| 26 |
+
namespace c10d::detail {
|
| 27 |
+
|
| 28 |
+
// Returns a human-readable representation of the given socket address.
|
| 29 |
+
std::string formatSockAddr(const struct ::sockaddr* addr, socklen_t len);
|
| 30 |
+
|
| 31 |
+
} // namespace c10d::detail
|
| 32 |
+
|
| 33 |
+
#else
|
| 34 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 35 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/store/TCPStoreBackend.hpp
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <thread>
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/distributed/c10d/TCPStore.hpp>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/socket.h>
|
| 8 |
+
|
| 9 |
+
#ifdef _WIN32
|
| 10 |
+
#include <io.h>
|
| 11 |
+
#include <winsock2.h>
|
| 12 |
+
#else
|
| 13 |
+
#include <poll.h>
|
| 14 |
+
#include <unistd.h>
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
namespace c10d::detail {
|
| 18 |
+
|
| 19 |
+
// Magic number for client validation.
|
| 20 |
+
static const uint32_t validationMagicNumber = 0x3C85F7CE;
|
| 21 |
+
|
| 22 |
+
enum class QueryType : uint8_t {
|
| 23 |
+
VALIDATE,
|
| 24 |
+
SET,
|
| 25 |
+
COMPARE_SET,
|
| 26 |
+
GET,
|
| 27 |
+
ADD,
|
| 28 |
+
CHECK,
|
| 29 |
+
WAIT,
|
| 30 |
+
GETNUMKEYS,
|
| 31 |
+
DELETE_KEY,
|
| 32 |
+
APPEND,
|
| 33 |
+
MULTI_GET,
|
| 34 |
+
MULTI_SET,
|
| 35 |
+
CANCEL_WAIT,
|
| 36 |
+
PING,
|
| 37 |
+
QUEUE_PUSH,
|
| 38 |
+
QUEUE_POP,
|
| 39 |
+
QUEUE_LEN,
|
| 40 |
+
LIST_KEYS,
|
| 41 |
+
BARRIER,
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
enum class CheckResponseType : uint8_t { READY, NOT_READY };
|
| 45 |
+
|
| 46 |
+
enum class WaitResponseType : uint8_t { STOP_WAITING, WAIT_CANCELED };
|
| 47 |
+
|
| 48 |
+
// Abstract base class to handle thread state for TCPStoreMasterDaemon.
|
| 49 |
+
// Contains the windows/unix implementations to signal a
|
| 50 |
+
// shutdown sequence for the thread
|
| 51 |
+
class BackgroundThread {
|
| 52 |
+
public:
|
| 53 |
+
explicit BackgroundThread();
|
| 54 |
+
|
| 55 |
+
virtual ~BackgroundThread() = 0;
|
| 56 |
+
virtual std::uint16_t port() const = 0;
|
| 57 |
+
|
| 58 |
+
void start();
|
| 59 |
+
bool stop_requested();
|
| 60 |
+
|
| 61 |
+
protected:
|
| 62 |
+
void dispose();
|
| 63 |
+
virtual void run() = 0;
|
| 64 |
+
virtual void stop() = 0;
|
| 65 |
+
bool is_running() {
|
| 66 |
+
return is_running_.load();
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
private:
|
| 70 |
+
std::atomic<bool> is_running_{false};
|
| 71 |
+
std::thread daemonThread_;
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
std::unique_ptr<BackgroundThread> create_tcpstore_backend(
|
| 75 |
+
const TCPStoreOptions& opts);
|
| 76 |
+
std::unique_ptr<BackgroundThread> create_libuv_tcpstore_backend(
|
| 77 |
+
const TCPStoreOptions& opts);
|
| 78 |
+
bool is_libuv_tcpstore_backend_available();
|
| 79 |
+
|
| 80 |
+
} // namespace c10d::detail
|
| 81 |
+
|
| 82 |
+
#else
|
| 83 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 84 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemory.hpp
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <c10/cuda/CUDAAllocatorConfig.h>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryTypes.hpp>
|
| 8 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 9 |
+
|
| 10 |
+
#include <shared_mutex>
|
| 11 |
+
|
| 12 |
+
namespace c10d::symmetric_memory {
|
| 13 |
+
|
| 14 |
+
// Resource wrapper that owns a (vaddr, allocation handle) pair. Upon
|
| 15 |
+
// destruction, it unmaps the vaddr and releases the allocation handle.
|
| 16 |
+
struct AllocationRef : public c10::intrusive_ptr_target {
|
| 17 |
+
void* ptr;
|
| 18 |
+
HandleType handle;
|
| 19 |
+
size_t block_size;
|
| 20 |
+
int device_idx;
|
| 21 |
+
bool is_multicast;
|
| 22 |
+
|
| 23 |
+
AllocationRef(
|
| 24 |
+
void* ptr,
|
| 25 |
+
HandleType handle,
|
| 26 |
+
size_t block_size,
|
| 27 |
+
int device_idx,
|
| 28 |
+
bool is_multicast = false);
|
| 29 |
+
|
| 30 |
+
~AllocationRef();
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
// Forward declaration of CUDAPeerAllocInfo
|
| 34 |
+
class CUDAPeerAllocInfo;
|
| 35 |
+
|
| 36 |
+
class CUDASymmetricMemory : public SymmetricMemory {
|
| 37 |
+
public:
|
| 38 |
+
// This is mostly a shallow copy that shares the pointer to
|
| 39 |
+
// `CUDAPeerAllocInfo` which corresponds to the base Block. The
|
| 40 |
+
// CUDASymmetricMemory handle is specified by the offset to the base ptr.
|
| 41 |
+
CUDASymmetricMemory(
|
| 42 |
+
const c10::intrusive_ptr<CUDAPeerAllocInfo>& pai,
|
| 43 |
+
size_t offset);
|
| 44 |
+
|
| 45 |
+
~CUDASymmetricMemory() override {};
|
| 46 |
+
|
| 47 |
+
std::vector<void*> get_buffer_ptrs() override;
|
| 48 |
+
std::vector<void*> get_signal_pad_ptrs() override;
|
| 49 |
+
void** get_buffer_ptrs_dev() override;
|
| 50 |
+
void** get_signal_pad_ptrs_dev() override;
|
| 51 |
+
size_t get_buffer_size() override;
|
| 52 |
+
size_t get_offset() override;
|
| 53 |
+
|
| 54 |
+
bool has_multicast_support() override;
|
| 55 |
+
void* get_multicast_ptr() override;
|
| 56 |
+
|
| 57 |
+
void barrier(int channel, size_t timeout_ms) override;
|
| 58 |
+
void put_signal(int dst_rank, int channel, size_t timeout_ms) override;
|
| 59 |
+
void wait_signal(int src_rank, int channel, size_t timeout_ms) override;
|
| 60 |
+
|
| 61 |
+
int get_rank() override;
|
| 62 |
+
int get_world_size() override;
|
| 63 |
+
c10::Device get_device() override;
|
| 64 |
+
bool world_within_direct_access() override;
|
| 65 |
+
|
| 66 |
+
private:
|
| 67 |
+
int local_device_idx_;
|
| 68 |
+
int rank_;
|
| 69 |
+
int world_size_;
|
| 70 |
+
c10::intrusive_ptr<CUDAPeerAllocInfo> pai_;
|
| 71 |
+
size_t offset_{0}; // in byte
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
// A class to hold the base pointers and signal pad pointers for a group of
|
| 75 |
+
// peers. One `CUDAPeerAllocInfo` object can be shared by multiple
|
| 76 |
+
// `CUDASymmetricMemory` objects when latter reside on the same allocation
|
| 77 |
+
// and rendezvous over the same group. (The `CUDASymmetricMemory` objects may
|
| 78 |
+
// have different offsets compared to the base address.)
|
| 79 |
+
class CUDAPeerAllocInfo : public c10::intrusive_ptr_target {
|
| 80 |
+
public:
|
| 81 |
+
CUDAPeerAllocInfo(
|
| 82 |
+
std::vector<c10::intrusive_ptr<AllocationRef>> alloc_refs,
|
| 83 |
+
std::vector<void*> buffers,
|
| 84 |
+
std::vector<void*> signal_pads,
|
| 85 |
+
HandleType mc_handle,
|
| 86 |
+
void* mc_addr,
|
| 87 |
+
size_t buffer_size,
|
| 88 |
+
int local_device_idx,
|
| 89 |
+
int rank,
|
| 90 |
+
int world_size,
|
| 91 |
+
std::string group_name);
|
| 92 |
+
|
| 93 |
+
private:
|
| 94 |
+
std::vector<c10::intrusive_ptr<AllocationRef>> alloc_refs_;
|
| 95 |
+
std::vector<void*> buffers_;
|
| 96 |
+
std::vector<void*> signal_pads_;
|
| 97 |
+
HandleType mc_handle_;
|
| 98 |
+
void* mc_addr_;
|
| 99 |
+
size_t buffer_size_;
|
| 100 |
+
int local_device_idx_;
|
| 101 |
+
int rank_;
|
| 102 |
+
int world_size_;
|
| 103 |
+
void** buffers_dev_;
|
| 104 |
+
void** signal_pads_dev_;
|
| 105 |
+
std::string group_name_;
|
| 106 |
+
|
| 107 |
+
friend class CUDASymmetricMemory;
|
| 108 |
+
};
|
| 109 |
+
|
| 110 |
+
// Metadata associated with each allocation performed by
|
| 111 |
+
// `CUDASymmetricMemoryAllocator`.
|
| 112 |
+
struct Block : public c10::intrusive_ptr_target {
|
| 113 |
+
c10::intrusive_ptr<AllocationRef> alloc_ref;
|
| 114 |
+
int device_idx;
|
| 115 |
+
size_t block_size;
|
| 116 |
+
size_t buffer_size;
|
| 117 |
+
// Byte offset from the allocation base (alloc_ref->ptr) to the start of the
|
| 118 |
+
// user buffer; the signal pad occupies [0, buffer_offset).
|
| 119 |
+
size_t buffer_offset;
|
| 120 |
+
std::optional<std::string> default_group_name;
|
| 121 |
+
std::map<std::string, c10::intrusive_ptr<CUDAPeerAllocInfo>> symm_mems;
|
| 122 |
+
|
| 123 |
+
Block(
|
| 124 |
+
c10::intrusive_ptr<AllocationRef> alloc_ref,
|
| 125 |
+
int device_idx,
|
| 126 |
+
size_t block_size,
|
| 127 |
+
size_t buffer_size,
|
| 128 |
+
size_t buffer_offset,
|
| 129 |
+
const std::optional<std::string>& group_name);
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
class CUDASymmetricMemoryAllocator : public SymmetricMemoryAllocator {
|
| 133 |
+
public:
|
| 134 |
+
void* alloc(
|
| 135 |
+
size_t size,
|
| 136 |
+
int device_idx,
|
| 137 |
+
const std::optional<std::string>& group_name) override;
|
| 138 |
+
|
| 139 |
+
void free(void* ptr) override;
|
| 140 |
+
size_t get_alloc_size(void* ptr) override;
|
| 141 |
+
c10::intrusive_ptr<SymmetricMemory> rendezvous(
|
| 142 |
+
void* ptr,
|
| 143 |
+
const std::optional<std::string>& group_name) override;
|
| 144 |
+
bool has_multicast_support(int device_idx) override;
|
| 145 |
+
bool has_allocation(void* ptr) override;
|
| 146 |
+
c10::DeviceType supported_device_type() override;
|
| 147 |
+
std::string name() override;
|
| 148 |
+
|
| 149 |
+
private:
|
| 150 |
+
c10::intrusive_ptr<Block> find_block(void* ptr);
|
| 151 |
+
c10::intrusive_ptr<Block> find_block_covering(void* ptr, size_t& offset);
|
| 152 |
+
|
| 153 |
+
std::shared_mutex mutex_;
|
| 154 |
+
std::unordered_map<void*, c10::intrusive_ptr<Block>> ptr_to_block_;
|
| 155 |
+
c10::cuda::CUDACachingAllocator::Expandable_Segments_Handle_Type
|
| 156 |
+
handle_type_ = c10::cuda::CUDACachingAllocator::
|
| 157 |
+
Expandable_Segments_Handle_Type::UNSPECIFIED;
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
} // namespace c10d::symmetric_memory
|
| 161 |
+
|
| 162 |
+
#else
|
| 163 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 164 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryTypes.hpp
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <cstddef>
|
| 5 |
+
#include <cstdint>
|
| 6 |
+
#include <string>
|
| 7 |
+
#include <utility>
|
| 8 |
+
|
| 9 |
+
#include <c10/util/hash.h>
|
| 10 |
+
|
| 11 |
+
#if defined(USE_ROCM)
|
| 12 |
+
#include <hip/hip_runtime_api.h>
|
| 13 |
+
#elif defined(PYTORCH_C10_DRIVER_API_SUPPORTED)
|
| 14 |
+
#include <cuda.h>
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
namespace c10d::symmetric_memory {
|
| 18 |
+
|
| 19 |
+
// Key type for the symmetric memory map. `void*` for tensor storage ptr,
|
| 20 |
+
// `std::string` for group name.
|
| 21 |
+
using SymmMemKey = std::pair<void*, std::string>;
|
| 22 |
+
// Hash function for the symmetric memory map. c10::hash has a std::pair
|
| 23 |
+
// specialization (line 323-329 of hash.h) that delegates to the tuple hasher
|
| 24 |
+
// which combines hashes of each element.
|
| 25 |
+
using SymmMemKeyHash = c10::hash<SymmMemKey>;
|
| 26 |
+
|
| 27 |
+
// Covers NVL72
|
| 28 |
+
constexpr int max_cuda_p2p_domain_size = 72;
|
| 29 |
+
// Maximum number of channels
|
| 30 |
+
constexpr int symm_max_nblocks = 32;
|
| 31 |
+
|
| 32 |
+
// Maximally, a rank will need to sync with all other ranks, over all
|
| 33 |
+
// channels. Each signal is 32 bits, which is the minimum unit for atomic cas.
|
| 34 |
+
// Default signal pad size, can be overridden via set_signal_pad_size().
|
| 35 |
+
constexpr size_t default_signal_pad_size =
|
| 36 |
+
symm_max_nblocks * max_cuda_p2p_domain_size * sizeof(uint32_t);
|
| 37 |
+
|
| 38 |
+
// The data buffer starts signal_pad_size bytes past the (aligned) allocation
|
| 39 |
+
// base, so the signal pad size is rounded up to this alignment to keep the data
|
| 40 |
+
// buffer aligned. 16 bytes is the correctness floor; 128 bytes targets best
|
| 41 |
+
// performance.
|
| 42 |
+
constexpr size_t signal_pad_alignment = 128;
|
| 43 |
+
|
| 44 |
+
#if !defined(USE_ROCM) && defined(PYTORCH_C10_DRIVER_API_SUPPORTED)
|
| 45 |
+
using HandleType = CUmemGenericAllocationHandle;
|
| 46 |
+
#elif defined(USE_ROCM)
|
| 47 |
+
using HandleType = hipMemGenericAllocationHandle_t;
|
| 48 |
+
#else
|
| 49 |
+
using HandleType = void*;
|
| 50 |
+
#endif
|
| 51 |
+
|
| 52 |
+
} // namespace c10d::symmetric_memory
|
| 53 |
+
|
| 54 |
+
#else
|
| 55 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 56 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryUtils.hpp
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <torch/csrc/distributed/c10d/ProcessGroup.hpp>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/symm_mem/CUDASymmetricMemoryTypes.hpp>
|
| 8 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 9 |
+
#include <cstring>
|
| 10 |
+
#include <type_traits>
|
| 11 |
+
#include <utility>
|
| 12 |
+
#include <vector>
|
| 13 |
+
|
| 14 |
+
namespace c10d {
|
| 15 |
+
namespace symmetric_memory {
|
| 16 |
+
|
| 17 |
+
bool device_has_multicast_support(int device_idx);
|
| 18 |
+
|
| 19 |
+
bool allow_overlapping_devices();
|
| 20 |
+
|
| 21 |
+
// Query environment variable to get the backend used for CUDA Symmetric Memory.
|
| 22 |
+
std::string getSymmMemBackendCUDA();
|
| 23 |
+
|
| 24 |
+
// All-gather a fixed-size byte payload through the given ProcessGroup.
|
| 25 |
+
// Uses ProcessGroup::all_gather_single (NCCL allgather for a NCCL-backed PG).
|
| 26 |
+
// The payload is staged through a uint8 tensor on `device_idx`. Returns a
|
| 27 |
+
// contiguous CPU tensor of world_size * nbytes uint8 elements.
|
| 28 |
+
at::Tensor pg_all_gather_bytes(
|
| 29 |
+
const c10::intrusive_ptr<c10d::ProcessGroup>& pg,
|
| 30 |
+
const void* data,
|
| 31 |
+
size_t nbytes,
|
| 32 |
+
int device_idx);
|
| 33 |
+
|
| 34 |
+
// Broadcast a fixed-size byte payload from rank `root` through the given
|
| 35 |
+
// ProcessGroup. Same staging scheme as `pg_all_gather_bytes`. Returns a
|
| 36 |
+
// contiguous CPU tensor of `nbytes` uint8 elements.
|
| 37 |
+
at::Tensor pg_broadcast_bytes(
|
| 38 |
+
const c10::intrusive_ptr<c10d::ProcessGroup>& pg,
|
| 39 |
+
const void* data,
|
| 40 |
+
size_t nbytes,
|
| 41 |
+
int device_idx,
|
| 42 |
+
int root);
|
| 43 |
+
|
| 44 |
+
// Blocking barrier over the given ProcessGroup, pinned to `device_idx` so the
|
| 45 |
+
// backend does not have to guess which device to barrier on.
|
| 46 |
+
void pg_barrier(
|
| 47 |
+
const c10::intrusive_ptr<c10d::ProcessGroup>& pg,
|
| 48 |
+
int device_idx);
|
| 49 |
+
|
| 50 |
+
// Templated wrapper around `pg_all_gather_bytes` matching the shape of
|
| 51 |
+
// `StoreExchange::all_gather` so rendezvous code can swap transports without
|
| 52 |
+
// caring about serialization.
|
| 53 |
+
template <typename T>
|
| 54 |
+
std::vector<T> pg_all_gather(
|
| 55 |
+
const c10::intrusive_ptr<c10d::ProcessGroup>& pg,
|
| 56 |
+
int device_idx,
|
| 57 |
+
const T& val) {
|
| 58 |
+
static_assert(
|
| 59 |
+
std::is_trivially_copyable_v<T>,
|
| 60 |
+
"pg_all_gather requires a trivially copyable type");
|
| 61 |
+
at::Tensor flat = pg_all_gather_bytes(pg, &val, sizeof(T), device_idx);
|
| 62 |
+
const auto world_size = pg->getSize();
|
| 63 |
+
const size_t expected = static_cast<size_t>(world_size) * sizeof(T);
|
| 64 |
+
TORCH_CHECK(
|
| 65 |
+
static_cast<size_t>(flat.numel()) == expected,
|
| 66 |
+
"pg_all_gather: expected ",
|
| 67 |
+
expected,
|
| 68 |
+
" bytes but got ",
|
| 69 |
+
flat.numel());
|
| 70 |
+
std::vector<T> out(world_size);
|
| 71 |
+
std::memcpy(out.data(), flat.data_ptr(), expected);
|
| 72 |
+
return out;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// Templated wrapper around `pg_broadcast_bytes`. On non-source ranks `val` is
|
| 76 |
+
// ignored and only used to size the payload.
|
| 77 |
+
template <typename T>
|
| 78 |
+
T pg_broadcast(
|
| 79 |
+
const c10::intrusive_ptr<c10d::ProcessGroup>& pg,
|
| 80 |
+
int device_idx,
|
| 81 |
+
int root,
|
| 82 |
+
const T& val) {
|
| 83 |
+
static_assert(
|
| 84 |
+
std::is_trivially_copyable_v<T>,
|
| 85 |
+
"pg_broadcast requires a trivially copyable type");
|
| 86 |
+
at::Tensor flat = pg_broadcast_bytes(pg, &val, sizeof(T), device_idx, root);
|
| 87 |
+
TORCH_CHECK(
|
| 88 |
+
static_cast<size_t>(flat.numel()) == sizeof(T),
|
| 89 |
+
"pg_broadcast: expected ",
|
| 90 |
+
sizeof(T),
|
| 91 |
+
" bytes but got ",
|
| 92 |
+
flat.numel());
|
| 93 |
+
T out{};
|
| 94 |
+
std::memcpy(&out, flat.data_ptr(), sizeof(T));
|
| 95 |
+
return out;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
class IpcChannel {
|
| 99 |
+
public:
|
| 100 |
+
IpcChannel();
|
| 101 |
+
~IpcChannel();
|
| 102 |
+
|
| 103 |
+
void send_fd(int dst_pid, int fd);
|
| 104 |
+
int recv_fd();
|
| 105 |
+
|
| 106 |
+
std::vector<int> all_gather_fds(
|
| 107 |
+
int rank,
|
| 108 |
+
const std::vector<int>& pids,
|
| 109 |
+
int fd);
|
| 110 |
+
|
| 111 |
+
int broadcast_fds(
|
| 112 |
+
int rank,
|
| 113 |
+
int src_rank,
|
| 114 |
+
const std::vector<int>& pids,
|
| 115 |
+
int fd);
|
| 116 |
+
|
| 117 |
+
private:
|
| 118 |
+
static std::string get_socket_name(int pid);
|
| 119 |
+
|
| 120 |
+
std::string socket_name_;
|
| 121 |
+
int socket_;
|
| 122 |
+
};
|
| 123 |
+
|
| 124 |
+
// A set of store-based exchange methods with a preset prefix typically type of
|
| 125 |
+
// the SymmetricMemory. Most used as static instances at respective
|
| 126 |
+
// SymmetricMemory implementation files.
|
| 127 |
+
class StoreExchange {
|
| 128 |
+
public:
|
| 129 |
+
StoreExchange(std::string store_prefix)
|
| 130 |
+
: store_prefix_(std::move(store_prefix)) {}
|
| 131 |
+
|
| 132 |
+
// Put template function in header file so that compiler can easily access it.
|
| 133 |
+
template <typename T>
|
| 134 |
+
std::vector<T> all_gather(
|
| 135 |
+
const c10::intrusive_ptr<c10d::Store>& store,
|
| 136 |
+
int rank,
|
| 137 |
+
int world_size,
|
| 138 |
+
T val) {
|
| 139 |
+
static_assert(std::is_trivially_copyable_v<T>);
|
| 140 |
+
|
| 141 |
+
std::vector<std::string> peer_keys;
|
| 142 |
+
peer_keys.reserve(world_size);
|
| 143 |
+
for (int r = 0; r < world_size; ++r) {
|
| 144 |
+
std::ostringstream oss;
|
| 145 |
+
oss << store_prefix_ << '/' << seq_id_ << '/' << r;
|
| 146 |
+
peer_keys.push_back(std::move(oss).str());
|
| 147 |
+
}
|
| 148 |
+
++seq_id_;
|
| 149 |
+
|
| 150 |
+
{
|
| 151 |
+
std::vector<uint8_t> payload(
|
| 152 |
+
reinterpret_cast<uint8_t*>(&val),
|
| 153 |
+
reinterpret_cast<uint8_t*>(&val) + sizeof(T));
|
| 154 |
+
store->set(peer_keys[rank], payload);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
auto payloads = store->multiGet(peer_keys);
|
| 158 |
+
|
| 159 |
+
std::vector<T> peer_vals;
|
| 160 |
+
peer_vals.reserve(world_size);
|
| 161 |
+
for (int r = 0; r < world_size; ++r) {
|
| 162 |
+
TORCH_CHECK(payloads[r].size() == sizeof(T));
|
| 163 |
+
T peer_val{};
|
| 164 |
+
std::memcpy(&peer_val, payloads[r].data(), sizeof(T));
|
| 165 |
+
peer_vals.push_back(peer_val);
|
| 166 |
+
}
|
| 167 |
+
return peer_vals;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
void barrier(
|
| 171 |
+
const c10::intrusive_ptr<c10d::Store>& store,
|
| 172 |
+
int rank,
|
| 173 |
+
int world_size) {
|
| 174 |
+
(void)rank;
|
| 175 |
+
std::ostringstream oss;
|
| 176 |
+
oss << store_prefix_ << '/' << seq_id_;
|
| 177 |
+
++seq_id_;
|
| 178 |
+
store->barrier(std::move(oss).str(), world_size);
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
private:
|
| 182 |
+
const std::string store_prefix_;
|
| 183 |
+
size_t seq_id_ = 0;
|
| 184 |
+
};
|
| 185 |
+
|
| 186 |
+
// Returns a pointer of virtual address that is mapped to the physical memory
|
| 187 |
+
// held by the handle.
|
| 188 |
+
void map_block(
|
| 189 |
+
void** ptr,
|
| 190 |
+
c10d::symmetric_memory::HandleType handle,
|
| 191 |
+
size_t size,
|
| 192 |
+
int device_idx);
|
| 193 |
+
|
| 194 |
+
} // namespace symmetric_memory
|
| 195 |
+
} // namespace c10d
|
| 196 |
+
|
| 197 |
+
#else
|
| 198 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 199 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/DMAConnectivity.hpp
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
|
| 6 |
+
namespace c10d {
|
| 7 |
+
|
| 8 |
+
struct TORCH_API DMAConnectivity : c10::intrusive_ptr_target {
|
| 9 |
+
c10::DeviceType device_type;
|
| 10 |
+
std::string connection_type;
|
| 11 |
+
|
| 12 |
+
// This is an NxN matrix representing the connectivity between N devices,
|
| 13 |
+
// where each element matrix[i][j] indicates the connectivity between device
|
| 14 |
+
// i and device j. A value of 0 denotes that there is no connection between
|
| 15 |
+
// device i and j. The meaning of non-zero values are specific to the
|
| 16 |
+
// connection type (e.g., for NVLink it represents the number of NVLinks).
|
| 17 |
+
std::vector<std::vector<int>> matrix;
|
| 18 |
+
|
| 19 |
+
explicit DMAConnectivity(
|
| 20 |
+
c10::DeviceType device_type,
|
| 21 |
+
std::string connection_type,
|
| 22 |
+
std::vector<std::vector<int>> matrix);
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
struct DMAConnectivityDetector : c10::intrusive_ptr_target {
|
| 26 |
+
virtual c10::intrusive_ptr<DMAConnectivity> detect() = 0;
|
| 27 |
+
~DMAConnectivityDetector() override = default;
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
C10_EXPORT void register_dma_connectivity_detector(
|
| 31 |
+
c10::DeviceType device_type,
|
| 32 |
+
const std::string& connection_type,
|
| 33 |
+
c10::intrusive_ptr<DMAConnectivityDetector> detector);
|
| 34 |
+
|
| 35 |
+
TORCH_API c10::intrusive_ptr<DMAConnectivity> detect_dma_connectivity(
|
| 36 |
+
c10::DeviceType device_type,
|
| 37 |
+
const std::string& connection_type);
|
| 38 |
+
|
| 39 |
+
} // namespace c10d
|
| 40 |
+
|
| 41 |
+
#else
|
| 42 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 43 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/NCCLSymmetricMemory.hpp
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 4 |
+
#include <torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp>
|
| 5 |
+
|
| 6 |
+
#ifdef NCCL_HAS_SYMMEM_SUPPORT
|
| 7 |
+
|
| 8 |
+
namespace c10d {
|
| 9 |
+
namespace symmetric_memory {
|
| 10 |
+
|
| 11 |
+
class NCCLPeerAllocInfo;
|
| 12 |
+
|
| 13 |
+
class NCCLSymmetricMemory : public SymmetricMemory {
|
| 14 |
+
public:
|
| 15 |
+
NCCLSymmetricMemory(c10::intrusive_ptr<NCCLPeerAllocInfo> pai, size_t offset);
|
| 16 |
+
|
| 17 |
+
~NCCLSymmetricMemory() override = default;
|
| 18 |
+
|
| 19 |
+
std::vector<void*> get_buffer_ptrs() override;
|
| 20 |
+
|
| 21 |
+
std::vector<void*> get_signal_pad_ptrs() override;
|
| 22 |
+
|
| 23 |
+
void** get_buffer_ptrs_dev() override;
|
| 24 |
+
|
| 25 |
+
void** get_signal_pad_ptrs_dev() override;
|
| 26 |
+
|
| 27 |
+
size_t get_buffer_size() override;
|
| 28 |
+
|
| 29 |
+
std::string get_group_name();
|
| 30 |
+
|
| 31 |
+
bool has_multicast_support() override;
|
| 32 |
+
|
| 33 |
+
void* get_multicast_ptr() override;
|
| 34 |
+
|
| 35 |
+
void barrier(int channel, size_t timeout_ms) override;
|
| 36 |
+
|
| 37 |
+
void put_signal(int dst_rank, int channel, size_t timeout_ms) override;
|
| 38 |
+
|
| 39 |
+
void wait_signal(int src_rank, int channel, size_t timeout_ms) override;
|
| 40 |
+
|
| 41 |
+
int get_rank() override;
|
| 42 |
+
|
| 43 |
+
int get_world_size() override;
|
| 44 |
+
|
| 45 |
+
c10::Device get_device() override;
|
| 46 |
+
|
| 47 |
+
ncclWindow_t get_window();
|
| 48 |
+
|
| 49 |
+
size_t get_offset() override;
|
| 50 |
+
|
| 51 |
+
// Byte offset of this handle's data within the NCCL window. The window
|
| 52 |
+
// starts at the signal pad, so this is buffer_offset + get_offset().
|
| 53 |
+
size_t get_window_offset();
|
| 54 |
+
|
| 55 |
+
private:
|
| 56 |
+
c10::intrusive_ptr<NCCLPeerAllocInfo> pai_;
|
| 57 |
+
size_t offset_;
|
| 58 |
+
int rank_;
|
| 59 |
+
int world_size_;
|
| 60 |
+
int device_idx_;
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
} // namespace symmetric_memory
|
| 64 |
+
} // namespace c10d
|
| 65 |
+
#endif // NCCL_HAS_SYMMEM_SUPPORT
|
| 66 |
+
|
| 67 |
+
#else
|
| 68 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 69 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <ATen/core/ivalue.h>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 7 |
+
|
| 8 |
+
namespace c10d::symmetric_memory {
|
| 9 |
+
|
| 10 |
+
// Validates a peer rank used to index the per-peer arrays of buffer/signal
|
| 11 |
+
// pad pointers. An out-of-range rank reads or writes through a wild pointer,
|
| 12 |
+
// or lands past the signal pad in the peer's tensor data.
|
| 13 |
+
inline void check_rank(int rank, int world_size) {
|
| 14 |
+
TORCH_CHECK(
|
| 15 |
+
rank >= 0 && rank < world_size,
|
| 16 |
+
"rank must be in [0, ",
|
| 17 |
+
world_size,
|
| 18 |
+
") (got ",
|
| 19 |
+
rank,
|
| 20 |
+
")");
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// SymmetricMemory represents symmetric allocations across a group of devices.
|
| 24 |
+
// The allocations represented by a SymmetricMemory object are accessible by
|
| 25 |
+
// all devices in the group. The class can be used for op-level custom
|
| 26 |
+
// communication patterns (via the get_buffer APIs and the synchronization
|
| 27 |
+
// primitives), as well as custom communication kernels (via the buffer and
|
| 28 |
+
// signal_pad device pointers).
|
| 29 |
+
//
|
| 30 |
+
// To acquire a SymmetricMemory object, each rank first allocates
|
| 31 |
+
// identical-sized memory via SymmetricMemoryAllocator::alloc(), then invokes
|
| 32 |
+
// SymmetricMemoryAllocator::rendezvous() on the memory to establish the
|
| 33 |
+
// association across peer buffers. The rendezvous is a one-time process, and
|
| 34 |
+
// the mapping between a local memory and the associated SymmetricMemory
|
| 35 |
+
// object is unique.
|
| 36 |
+
//
|
| 37 |
+
// NOTE [symmetric memory signal pad]
|
| 38 |
+
// Signal pads are P2P-accessible memory regions designated for
|
| 39 |
+
// synchronization. SymmetricMemory offers built-in synchronization primitives
|
| 40 |
+
// such as barriers, put_signal, and wait_signal, which are all based on signal
|
| 41 |
+
// pads. Users may utilize signal pads for their own synchronization logic,
|
| 42 |
+
// provided that the signal pads remain zero-filled following successful
|
| 43 |
+
// synchronization.
|
| 44 |
+
//
|
| 45 |
+
// NOTE [symmetric memory synchronization channel]
|
| 46 |
+
// Synchronization channels allow users to use a single SymmetricMemory object
|
| 47 |
+
// to perform isolated synchronizations on different streams. For example,
|
| 48 |
+
// consider the case in which two barriers are issued on two streams for
|
| 49 |
+
// different purposes. Without the concept of channels, we cannot guarantee the
|
| 50 |
+
// correctness of the barriers since signals issued from barrier on stream A
|
| 51 |
+
// can be received by the barrier on stream B. By specifying different channels
|
| 52 |
+
// for these two barriers, they can operate correctly in parallel.
|
| 53 |
+
class TORCH_API SymmetricMemory : public torch::CustomClassHolder {
|
| 54 |
+
public:
|
| 55 |
+
~SymmetricMemory() override = default;
|
| 56 |
+
|
| 57 |
+
virtual std::vector<void*> get_buffer_ptrs() = 0;
|
| 58 |
+
virtual std::vector<void*> get_signal_pad_ptrs() = 0;
|
| 59 |
+
|
| 60 |
+
// get_buffer_ptrs_dev() and get_signal_pad_ptrs_dev() each return a pointer
|
| 61 |
+
// to a device array of size world_size, containing buffer pointers and
|
| 62 |
+
// signal pad pointers, respectively.
|
| 63 |
+
virtual void** get_buffer_ptrs_dev() = 0;
|
| 64 |
+
virtual void** get_signal_pad_ptrs_dev() = 0;
|
| 65 |
+
virtual size_t get_buffer_size() = 0;
|
| 66 |
+
size_t get_signal_pad_size();
|
| 67 |
+
|
| 68 |
+
virtual size_t get_offset() = 0;
|
| 69 |
+
|
| 70 |
+
virtual bool has_multicast_support() = 0;
|
| 71 |
+
virtual void* get_multicast_ptr() = 0;
|
| 72 |
+
|
| 73 |
+
at::Tensor get_buffer(
|
| 74 |
+
int rank,
|
| 75 |
+
c10::IntArrayRef sizes,
|
| 76 |
+
c10::ScalarType dtype,
|
| 77 |
+
int64_t storage_offset);
|
| 78 |
+
|
| 79 |
+
at::Tensor get_signal_pad(
|
| 80 |
+
int rank,
|
| 81 |
+
c10::IntArrayRef sizes,
|
| 82 |
+
std::optional<c10::ScalarType> dtype = std::nullopt,
|
| 83 |
+
int64_t storage_offset = 0);
|
| 84 |
+
|
| 85 |
+
at::Tensor get_remote_tensor(
|
| 86 |
+
int peer,
|
| 87 |
+
c10::IntArrayRef sizes,
|
| 88 |
+
c10::ScalarType dtype);
|
| 89 |
+
|
| 90 |
+
virtual void barrier(int channel, size_t timeout_ms) = 0;
|
| 91 |
+
virtual void put_signal(int dst_rank, int channel, size_t timeout_ms) = 0;
|
| 92 |
+
virtual void wait_signal(int src_rank, int channel, size_t timeout_ms) = 0;
|
| 93 |
+
|
| 94 |
+
virtual int get_rank() = 0;
|
| 95 |
+
virtual int get_world_size() = 0;
|
| 96 |
+
virtual c10::Device get_device() = 0;
|
| 97 |
+
|
| 98 |
+
virtual const std::vector<int>& get_rank_to_global_rank() {
|
| 99 |
+
TORCH_CHECK(false, "NYI");
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
virtual int* get_rank_to_global_rank_dev() {
|
| 103 |
+
TORCH_CHECK(false, "NYI");
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// Returns true if *all* peers within the group are accessible via direct
|
| 107 |
+
// memory load and store.
|
| 108 |
+
virtual bool world_within_direct_access() {
|
| 109 |
+
TORCH_CHECK(false, "NYI");
|
| 110 |
+
}
|
| 111 |
+
};
|
| 112 |
+
|
| 113 |
+
class SymmetricMemoryAllocator : public c10::intrusive_ptr_target {
|
| 114 |
+
public:
|
| 115 |
+
~SymmetricMemoryAllocator() override = default;
|
| 116 |
+
|
| 117 |
+
virtual void* alloc(
|
| 118 |
+
size_t size,
|
| 119 |
+
int device_idx,
|
| 120 |
+
const std::optional<std::string>& group_name) = 0;
|
| 121 |
+
|
| 122 |
+
virtual void free(void* ptr) = 0;
|
| 123 |
+
virtual size_t get_alloc_size(void* ptr) = 0;
|
| 124 |
+
virtual c10::intrusive_ptr<SymmetricMemory> rendezvous(
|
| 125 |
+
void* ptr,
|
| 126 |
+
const std::optional<std::string>& group_name) = 0;
|
| 127 |
+
virtual bool has_multicast_support(int device_idx) = 0;
|
| 128 |
+
virtual c10::DeviceType supported_device_type() = 0;
|
| 129 |
+
virtual std::string name() = 0;
|
| 130 |
+
virtual bool has_allocation(void* ptr) {
|
| 131 |
+
return false;
|
| 132 |
+
}
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
C10_EXPORT bool is_finalizing();
|
| 136 |
+
|
| 137 |
+
C10_EXPORT void register_allocator(
|
| 138 |
+
c10::DeviceType device_type,
|
| 139 |
+
c10::intrusive_ptr<SymmetricMemoryAllocator> allocator);
|
| 140 |
+
|
| 141 |
+
C10_EXPORT void register_availability(
|
| 142 |
+
const std::string& name,
|
| 143 |
+
c10::intrusive_ptr<SymmetricMemoryAllocator> allocator);
|
| 144 |
+
|
| 145 |
+
C10_EXPORT bool has_allocator(c10::DeviceType device_type);
|
| 146 |
+
|
| 147 |
+
C10_EXPORT c10::intrusive_ptr<SymmetricMemoryAllocator> get_allocator(
|
| 148 |
+
c10::DeviceType device_type);
|
| 149 |
+
|
| 150 |
+
// Set a store for rendezvousing symmetric allocations on a group of devices
|
| 151 |
+
// identified by `group_name`. The concept of groups is logical; users can
|
| 152 |
+
// utilize predefined groups (e.g., a group of device identified by a
|
| 153 |
+
// ProcessGroup) or create custom ones. Note that a SymmetricMemoryAllocator
|
| 154 |
+
// backends might employ a more efficient communication channel for the actual
|
| 155 |
+
// rendezvous process and only use the store for bootstrapping purposes.
|
| 156 |
+
TORCH_API void set_group_info(
|
| 157 |
+
const std::string& group_name,
|
| 158 |
+
int rank,
|
| 159 |
+
int world_size,
|
| 160 |
+
c10::intrusive_ptr<Store> store);
|
| 161 |
+
|
| 162 |
+
struct GroupInfo {
|
| 163 |
+
int rank;
|
| 164 |
+
int world_size;
|
| 165 |
+
c10::intrusive_ptr<c10d::Store> store;
|
| 166 |
+
};
|
| 167 |
+
|
| 168 |
+
C10_EXPORT GroupInfo& get_group_info(const std::string& group_name);
|
| 169 |
+
|
| 170 |
+
// Identical to empty_strided, but allows symmetric memory access to be
|
| 171 |
+
// established for the allocated tensor via SymmetricMemory::rendezvous(). This
|
| 172 |
+
// function itself is not a collective operation. It invokes
|
| 173 |
+
// SymmetricMemoryAllocator::alloc() for the requested device under the hood.
|
| 174 |
+
//
|
| 175 |
+
// NOTE [symmetric memory persistent allocation]
|
| 176 |
+
// If an `alloc_id` is supplied, empty_strided_p2p will perform persistent
|
| 177 |
+
// allocation. This makes the function cache allocated memory and ensure that
|
| 178 |
+
// invocations with the same `alloc_id` receive tensors backed by the same
|
| 179 |
+
// memory address. For safety, if a previous persistent allocation is still
|
| 180 |
+
// active (i.e., the storage of the returned tensor is still alive), persistent
|
| 181 |
+
// allocations with the same `alloc_id` will fail. This determinism coupled
|
| 182 |
+
// with memory planning of communication buffers (e.g., by Inductor) allows
|
| 183 |
+
// communication algorithms to reliably reuse previously established remote
|
| 184 |
+
// memory access.
|
| 185 |
+
TORCH_API at::Tensor empty_strided_p2p(
|
| 186 |
+
c10::IntArrayRef size,
|
| 187 |
+
c10::IntArrayRef stride,
|
| 188 |
+
c10::ScalarType dtype,
|
| 189 |
+
c10::Device device,
|
| 190 |
+
const std::optional<std::string>& group_name,
|
| 191 |
+
std::optional<uint64_t> alloc_id);
|
| 192 |
+
|
| 193 |
+
// Establishes symmetric memory access on tensors allocated via
|
| 194 |
+
// empty_strided_p2p() and empty_strided_p2p_persistent(). rendezvous() is a
|
| 195 |
+
// one-time process, and the mapping between a local memory region and the
|
| 196 |
+
// associated SymmetricMemory object is unique. Subsequent calls to
|
| 197 |
+
// rendezvous() with the same tensor, or tensors allocated with
|
| 198 |
+
// empty_strided_p2p_persistent() using the same alloc_id, will receive the
|
| 199 |
+
// cached SymmetricMemory object.
|
| 200 |
+
//
|
| 201 |
+
// The function has a collective semantic and must be invoked simultaneously
|
| 202 |
+
// from all rendezvous participants.
|
| 203 |
+
TORCH_API c10::intrusive_ptr<SymmetricMemory> rendezvous(
|
| 204 |
+
const at::Tensor& tensor,
|
| 205 |
+
const std::optional<std::string>& group_name = std::nullopt);
|
| 206 |
+
|
| 207 |
+
TORCH_API bool has_multicast_support(
|
| 208 |
+
c10::DeviceType device_type,
|
| 209 |
+
int device_idx);
|
| 210 |
+
|
| 211 |
+
TORCH_API bool is_symm_mem_tensor(const at::Tensor& tensor);
|
| 212 |
+
|
| 213 |
+
TORCH_API void set_backend(const std::string& name);
|
| 214 |
+
|
| 215 |
+
TORCH_API std::optional<std::string> get_backend(c10::Device device);
|
| 216 |
+
|
| 217 |
+
// Get the current signal pad size for symmetric memory allocations.
|
| 218 |
+
// Returns the user-configured size if set, otherwise returns the default size.
|
| 219 |
+
TORCH_API size_t get_signal_pad_size();
|
| 220 |
+
|
| 221 |
+
// Set the signal pad size for future symmetric memory allocations.
|
| 222 |
+
// This must be called before any symmetric memory allocations are made.
|
| 223 |
+
// The size should be proportional to the number of blocks the user launches
|
| 224 |
+
// and the world size.
|
| 225 |
+
TORCH_API void set_signal_pad_size(size_t size);
|
| 226 |
+
|
| 227 |
+
C10_EXPORT void register_mempool_allocator(
|
| 228 |
+
c10::DeviceType device_type,
|
| 229 |
+
std::shared_ptr<c10::Allocator> allocator);
|
| 230 |
+
|
| 231 |
+
TORCH_API std::shared_ptr<c10::Allocator> get_mempool_allocator(
|
| 232 |
+
c10::Device device);
|
| 233 |
+
|
| 234 |
+
} // namespace c10d::symmetric_memory
|
| 235 |
+
|
| 236 |
+
#else
|
| 237 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 238 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/env.hpp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <c10/util/env.h>
|
| 3 |
+
|
| 4 |
+
namespace c10d::symmetric_memory {
|
| 5 |
+
|
| 6 |
+
static int getenv_nblocks() {
|
| 7 |
+
static int num_blocks = -1; // Uninitialized
|
| 8 |
+
if (num_blocks == -1) {
|
| 9 |
+
auto str = c10::utils::get_env("TORCH_SYMMMEM_NBLOCKS");
|
| 10 |
+
if (str.has_value()) {
|
| 11 |
+
num_blocks = std::stoi(str.value());
|
| 12 |
+
} else {
|
| 13 |
+
num_blocks = -2; // Not set
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
return num_blocks;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
} // namespace c10d::symmetric_memory
|
| 20 |
+
#else
|
| 21 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 22 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/intra_node_comm.hpp
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <c10/cuda/CUDAStream.h>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/Work.hpp>
|
| 8 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 9 |
+
|
| 10 |
+
namespace c10d::intra_node_comm {
|
| 11 |
+
|
| 12 |
+
using namespace c10d::symmetric_memory;
|
| 13 |
+
|
| 14 |
+
constexpr size_t kMaxDevices = 8;
|
| 15 |
+
constexpr size_t kDefaultBufferSize = 10ull * 1024 * 1024;
|
| 16 |
+
|
| 17 |
+
using NvlMesh = std::array<std::array<size_t, kMaxDevices>, kMaxDevices>;
|
| 18 |
+
|
| 19 |
+
enum class Topology : uint8_t {
|
| 20 |
+
UNKNOWN = 0,
|
| 21 |
+
FULLY_CONNECTED = 1,
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
enum class AllReduceAlgo : uint8_t {
|
| 25 |
+
NONE = 0,
|
| 26 |
+
ONE_SHOT = 1,
|
| 27 |
+
TWO_SHOT = 2,
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
// NOTE: this class will be removed soon in favor of SymmetricMemory
|
| 31 |
+
class TORCH_API IntraNodeComm : public c10::intrusive_ptr_target {
|
| 32 |
+
public:
|
| 33 |
+
IntraNodeComm(
|
| 34 |
+
c10::intrusive_ptr<c10d::Store> store,
|
| 35 |
+
size_t rank,
|
| 36 |
+
size_t worldSize,
|
| 37 |
+
std::optional<size_t> bufferSize = std::nullopt,
|
| 38 |
+
std::string groupName = "");
|
| 39 |
+
|
| 40 |
+
~IntraNodeComm() override;
|
| 41 |
+
|
| 42 |
+
static bool isEnabled();
|
| 43 |
+
|
| 44 |
+
/**
|
| 45 |
+
* Performs rendezvous.
|
| 46 |
+
* If rendezvous fails, the IntraNodeComm object will be in an invalid
|
| 47 |
+
* state and it is the caller's responsibility to dispose it.
|
| 48 |
+
*/
|
| 49 |
+
bool rendezvous();
|
| 50 |
+
|
| 51 |
+
/**
|
| 52 |
+
* Selects a AllReduceAlgo that we think will outperform nccl.
|
| 53 |
+
* Returns AllReduceAlgo::NONE if we don't think we can outperform nccl.
|
| 54 |
+
*/
|
| 55 |
+
AllReduceAlgo selectAllReduceAlgo(const at::Tensor& input);
|
| 56 |
+
|
| 57 |
+
at::Tensor allReduce(const at::Tensor& input, AllReduceAlgo algo);
|
| 58 |
+
|
| 59 |
+
private:
|
| 60 |
+
at::Tensor oneShotAllReduce(
|
| 61 |
+
const at::Tensor& input,
|
| 62 |
+
at::cuda::CUDAStream& stream);
|
| 63 |
+
|
| 64 |
+
at::Tensor twoShotAllReduce(
|
| 65 |
+
const at::Tensor& input,
|
| 66 |
+
at::cuda::CUDAStream& stream);
|
| 67 |
+
|
| 68 |
+
c10::intrusive_ptr<Store> store_;
|
| 69 |
+
size_t rank_;
|
| 70 |
+
size_t worldSize_;
|
| 71 |
+
size_t bufferSize_;
|
| 72 |
+
std::string groupName_;
|
| 73 |
+
|
| 74 |
+
/**
|
| 75 |
+
* Members initialized after rendezvous
|
| 76 |
+
*/
|
| 77 |
+
bool isInitialized_ = false;
|
| 78 |
+
int deviceIdx_{0};
|
| 79 |
+
Topology topology_ = Topology::UNKNOWN;
|
| 80 |
+
void* symmetricMemoryPtr_ = nullptr;
|
| 81 |
+
c10::intrusive_ptr<SymmetricMemory> symmetricMemory_ = nullptr;
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
class IntraNodeCommWork : public c10d::Work {
|
| 85 |
+
public:
|
| 86 |
+
bool wait(std::chrono::milliseconds timeout = kNoTimeout) override {
|
| 87 |
+
return true;
|
| 88 |
+
}
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
TORCH_API int64_t getIntraNodeCommUsageCounter();
|
| 92 |
+
|
| 93 |
+
bool isIntraNodeCommSupported();
|
| 94 |
+
} // namespace c10d::intra_node_comm
|
| 95 |
+
|
| 96 |
+
#else
|
| 97 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 98 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/macros.hpp
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Macros for type dispatch and common utilities for symmetric memory
|
| 3 |
+
#pragma once
|
| 4 |
+
|
| 5 |
+
#include <ATen/ATen.h>
|
| 6 |
+
|
| 7 |
+
// Convert ATen floating point types to NV floating point types
|
| 8 |
+
// at::kBFloat16 -> __nv_bfloat16
|
| 9 |
+
// at::kHalf -> __half
|
| 10 |
+
// Float is the same.
|
| 11 |
+
|
| 12 |
+
#define AT_DISPATCH_CASE_CONVERT(enum_type, scalar_type, ...) \
|
| 13 |
+
case enum_type: { \
|
| 14 |
+
AT_PRIVATE_CHECK_SELECTIVE_BUILD(enum_type); \
|
| 15 |
+
using scalar_t = scalar_type; \
|
| 16 |
+
return __VA_ARGS__(); \
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
#define AT_DISPATCH_NV_FLOATS(scalar_type, name, ...) \
|
| 20 |
+
AT_DISPATCH_SWITCH( \
|
| 21 |
+
scalar_type, \
|
| 22 |
+
name, \
|
| 23 |
+
AT_DISPATCH_CASE_CONVERT(at::kBFloat16, __nv_bfloat16, __VA_ARGS__); \
|
| 24 |
+
AT_DISPATCH_CASE_CONVERT(at::kHalf, __half, __VA_ARGS__); \
|
| 25 |
+
AT_DISPATCH_CASE(at::kFloat, __VA_ARGS__));
|
| 26 |
+
|
| 27 |
+
#else
|
| 28 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 29 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#if USE_NCCL
|
| 5 |
+
|
| 6 |
+
#include <nccl.h>
|
| 7 |
+
#include <torch/csrc/cuda/nccl.h>
|
| 8 |
+
|
| 9 |
+
#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 27, 0)
|
| 10 |
+
#define NCCL_HAS_SYMMEM_SUPPORT
|
| 11 |
+
#endif
|
| 12 |
+
|
| 13 |
+
#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0)
|
| 14 |
+
#if !defined(USE_ROCM)
|
| 15 |
+
#define NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 16 |
+
#include <nccl_device.h>
|
| 17 |
+
#endif
|
| 18 |
+
#endif
|
| 19 |
+
|
| 20 |
+
// Host-side device-communicator setup: ncclDevCommCreate together with
|
| 21 |
+
// ncclDevCommRequirements / NCCL_DEV_COMM_REQUIREMENTS_INITIALIZER. These land
|
| 22 |
+
// in NCCL 2.29 (verified: absent in 2.28.9, present in 2.29.2), later than the
|
| 23 |
+
// device-side kernel symbols, so ops that construct a ncclDevComm gate on this
|
| 24 |
+
// rather than NCCL_HAS_SYMMEM_DEVICE_SUPPORT.
|
| 25 |
+
#if defined(NCCL_HAS_SYMMEM_DEVICE_SUPPORT) && \
|
| 26 |
+
NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0)
|
| 27 |
+
#define NCCL_HAS_DEVCOMM
|
| 28 |
+
#endif
|
| 29 |
+
|
| 30 |
+
#if defined(NCCL_HAS_SYMMEM_DEVICE_SUPPORT) && \
|
| 31 |
+
NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0)
|
| 32 |
+
#define NCCL_HAS_ONE_SIDED_API
|
| 33 |
+
#endif
|
| 34 |
+
|
| 35 |
+
#if defined(NCCL_HAS_SYMMEM_DEVICE_SUPPORT) && \
|
| 36 |
+
NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 7)
|
| 37 |
+
#define NCCL_DEVICE_HAS_REDUCE_COPY
|
| 38 |
+
#endif
|
| 39 |
+
#endif // USE_NCCL
|
| 40 |
+
|
| 41 |
+
#else
|
| 42 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 43 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_devcomm_manager.hpp
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDAException.h>
|
| 5 |
+
#include <c10/cuda/CUDAGuard.h>
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/Logging.h>
|
| 8 |
+
#include <torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp>
|
| 9 |
+
#include <functional>
|
| 10 |
+
#include <mutex>
|
| 11 |
+
#include <optional>
|
| 12 |
+
#include <string>
|
| 13 |
+
#include <unordered_map>
|
| 14 |
+
|
| 15 |
+
#ifdef NCCL_HAS_SYMMEM_SUPPORT
|
| 16 |
+
|
| 17 |
+
namespace c10d::symmetric_memory {
|
| 18 |
+
|
| 19 |
+
// Manages NCCL device communicators for symmetric memory operations.
|
| 20 |
+
// This is a singleton class that maintains a registry of device communicators
|
| 21 |
+
// organized by process group name and an optional key (typically the caller
|
| 22 |
+
// function name). This allows different functions within the same process group
|
| 23 |
+
// to use different device communicators, which is useful for concurrent
|
| 24 |
+
// collective operations.
|
| 25 |
+
//
|
| 26 |
+
// The registry uses a two-level map structure:
|
| 27 |
+
// - First level: keyed by process group name
|
| 28 |
+
// - Second level: keyed by an optional key (defaults to caller function name)
|
| 29 |
+
//
|
| 30 |
+
// Device communicators are stored by value in the registry, but methods return
|
| 31 |
+
// references wrapped in std::optional for safe access.
|
| 32 |
+
class TORCH_API NCCLDevCommManager {
|
| 33 |
+
public:
|
| 34 |
+
// Constructor
|
| 35 |
+
// @param device The CUDA device this manager is associated with
|
| 36 |
+
explicit NCCLDevCommManager(const c10::Device device) : device_(device) {}
|
| 37 |
+
|
| 38 |
+
// Per-device singleton: lazily creates one manager per CUDA device so
|
| 39 |
+
// callers on different devices don't share state.
|
| 40 |
+
static NCCLDevCommManager& get(const c10::Device device) {
|
| 41 |
+
static std::mutex mu;
|
| 42 |
+
static std::
|
| 43 |
+
unordered_map<c10::DeviceIndex, std::unique_ptr<NCCLDevCommManager>>
|
| 44 |
+
managers;
|
| 45 |
+
std::lock_guard<std::mutex> lock(mu);
|
| 46 |
+
auto& slot = managers[device.index()];
|
| 47 |
+
if (!slot) {
|
| 48 |
+
slot =
|
| 49 |
+
std::unique_ptr<NCCLDevCommManager>(new NCCLDevCommManager(device));
|
| 50 |
+
LOG(INFO) << "[NCCLDevCommManager] created manager for device=" << device;
|
| 51 |
+
}
|
| 52 |
+
return *slot;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 56 |
+
// Get an NCCL device communicator for a group, for the caller function. By
|
| 57 |
+
// default, we search for the device communicator using the caller function
|
| 58 |
+
// name as the key. If you previously registered a device communicator with a
|
| 59 |
+
// different key, you should provide that key instead.
|
| 60 |
+
// Returns std::nullopt if the device communicator is not found.
|
| 61 |
+
// Example:
|
| 62 |
+
// void foo(const std::string& group_name) {
|
| 63 |
+
// // Try to get first.
|
| 64 |
+
// auto devcomm_opt = get_devcomm(group_name);
|
| 65 |
+
// if (!devcomm_opt) {
|
| 66 |
+
// // Not found, create then register.
|
| 67 |
+
// ncclDevComm devcomm = ncclDevCommCreate(...);
|
| 68 |
+
// devcomm_opt = register_devcomm(group_name, devcomm);
|
| 69 |
+
// }
|
| 70 |
+
// ncclDevComm& devcomm_ref = *devcomm_opt;
|
| 71 |
+
// // Use devcomm_ref
|
| 72 |
+
// }
|
| 73 |
+
std::optional<std::reference_wrapper<ncclDevComm>> get_devcomm(
|
| 74 |
+
const std::string& group_name,
|
| 75 |
+
const std::string& key = __builtin_FUNCTION()) {
|
| 76 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 77 |
+
// First, look up the group in the registry
|
| 78 |
+
auto group_it = devcomm_registry_.find(group_name);
|
| 79 |
+
if (group_it == devcomm_registry_.end()) {
|
| 80 |
+
return std::nullopt;
|
| 81 |
+
}
|
| 82 |
+
// Then, look up the key within that group's map
|
| 83 |
+
auto key_it = group_it->second.find(key);
|
| 84 |
+
if (key_it == group_it->second.end()) {
|
| 85 |
+
return std::nullopt;
|
| 86 |
+
}
|
| 87 |
+
// Return a reference wrapper to the device communicator
|
| 88 |
+
// Using reference_wrapper because std::optional cannot hold references
|
| 89 |
+
// directly
|
| 90 |
+
return std::make_optional(std::ref(key_it->second));
|
| 91 |
+
}
|
| 92 |
+
#endif // NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 93 |
+
|
| 94 |
+
// Get a host-side NCCL communicator for a group.
|
| 95 |
+
// This is the regular host-side communicator, not the device communicator.
|
| 96 |
+
// @param group_name The process group name
|
| 97 |
+
// @return The host-side NCCL communicator
|
| 98 |
+
// @throws TORCH_CHECK if the communicator is not found
|
| 99 |
+
ncclComm_t get_comm(const std::string& group_name) {
|
| 100 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 101 |
+
auto it = group_to_comm_.find(group_name);
|
| 102 |
+
if (it == group_to_comm_.end()) {
|
| 103 |
+
TORCH_CHECK(
|
| 104 |
+
false,
|
| 105 |
+
"NCCL host communicator for group ",
|
| 106 |
+
group_name,
|
| 107 |
+
" not found. Have you rendezvoused any tensor with this group?");
|
| 108 |
+
}
|
| 109 |
+
return it->second;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 113 |
+
// Register a device communicator for a group. If `key` is not
|
| 114 |
+
// specified, we use the caller function name as the default `key`, to
|
| 115 |
+
// distinguish between different collective functions within the same group.
|
| 116 |
+
// You can provide your own `key` if your function uses two different
|
| 117 |
+
// device communicators on the same group at the same time, for example,
|
| 118 |
+
// when concurrent collective operations are used.
|
| 119 |
+
// Returns a reference to the newly registered device communicator.
|
| 120 |
+
// @throws TORCH_CHECK if the device communicator is already registered for
|
| 121 |
+
// the given group and key combination.
|
| 122 |
+
// Example:
|
| 123 |
+
// void foo(const std::string& group_name) {
|
| 124 |
+
// // Try to get first.
|
| 125 |
+
// auto devcomm_opt = get_devcomm(group_name);
|
| 126 |
+
// if (!devcomm_opt) {
|
| 127 |
+
// // Not found, create then register.
|
| 128 |
+
// ncclDevComm devcomm = ncclDevCommCreate(...);
|
| 129 |
+
// devcomm_opt = register_devcomm(group_name, devcomm);
|
| 130 |
+
// }
|
| 131 |
+
// ncclDevComm& devcomm_ref = *devcomm_opt;
|
| 132 |
+
// // Use devcomm_ref
|
| 133 |
+
// }
|
| 134 |
+
// void bar(const std::string& group_name) {
|
| 135 |
+
// ncclDevComm devcomm0 = ncclDevCommCreate(...);
|
| 136 |
+
// ncclDevComm devcomm1 = ncclDevCommCreate(...);
|
| 137 |
+
// // You can provide your own `key` if you want to, for example, to
|
| 138 |
+
// // distinguish between concurrent collective operations.
|
| 139 |
+
// register_devcomm(group_name, devcomm0, "bar0");
|
| 140 |
+
// register_devcomm(group_name, devcomm1, "bar1");
|
| 141 |
+
// }
|
| 142 |
+
std::optional<std::reference_wrapper<ncclDevComm>> register_devcomm(
|
| 143 |
+
const std::string& group_name,
|
| 144 |
+
ncclDevComm devcomm,
|
| 145 |
+
const std::string& key = __builtin_FUNCTION()) {
|
| 146 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 147 |
+
// Ensure the group exists in the registry, creating an empty map if needed
|
| 148 |
+
auto [group_it, inserted] = devcomm_registry_.try_emplace(
|
| 149 |
+
group_name, std::unordered_map<std::string, ncclDevComm>());
|
| 150 |
+
auto& group_map = group_it->second;
|
| 151 |
+
// Try to insert the device communicator with the given key
|
| 152 |
+
// Use std::move to avoid copying the device communicator
|
| 153 |
+
auto [key_it, key_inserted] =
|
| 154 |
+
group_map.try_emplace(key, std::move(devcomm));
|
| 155 |
+
if (!key_inserted) {
|
| 156 |
+
// Already registered - this is a programming error, so throw
|
| 157 |
+
TORCH_CHECK(
|
| 158 |
+
false,
|
| 159 |
+
"NCCL device communicator for group ",
|
| 160 |
+
group_name,
|
| 161 |
+
" with key ",
|
| 162 |
+
key,
|
| 163 |
+
" already registered.");
|
| 164 |
+
}
|
| 165 |
+
// Return a reference to the newly registered device communicator
|
| 166 |
+
return std::make_optional(std::ref(key_it->second));
|
| 167 |
+
}
|
| 168 |
+
#endif // NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 169 |
+
|
| 170 |
+
// Register the host-side NCCL communicator for `group_name` on this
|
| 171 |
+
// manager's device. Last-write-wins so a successor PG can replace the
|
| 172 |
+
// entry before the prior PG's destructor runs (e.g. restart-after-error).
|
| 173 |
+
// Producers must call `unregister_comm` from their destructor.
|
| 174 |
+
void register_comm(const std::string& group_name, ncclComm_t comm) {
|
| 175 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 176 |
+
group_to_comm_[group_name] = comm;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Unregister `group_name` on this manager's device. Safe to call when
|
| 180 |
+
// nothing is registered. Does not destroy the host comm; lifetime stays
|
| 181 |
+
// with the producer.
|
| 182 |
+
//
|
| 183 |
+
// Producers are expected to skip already-aborted comms in their
|
| 184 |
+
// destructor (e.g. ProcessGroupNCCL guards on isAborted()). Without that
|
| 185 |
+
// discipline, a stale destructor could race with a successor PG that
|
| 186 |
+
// re-registered under the same `group_name` (e.g. restart-after-error)
|
| 187 |
+
// and silently drop the successor's entry.
|
| 188 |
+
void unregister_comm(const std::string& group_name) {
|
| 189 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 190 |
+
group_to_comm_.erase(group_name);
|
| 191 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 192 |
+
devcomm_registry_.erase(group_name);
|
| 193 |
+
#endif
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// Identity-safe unregister: drop the entry only if the currently-registered
|
| 197 |
+
// comm is still `comm`. A stale producer whose comm was already replaced by a
|
| 198 |
+
// successor under the same `group_name` becomes a no-op, so it cannot clobber
|
| 199 |
+
// the successor -- no isAborted()-style discipline needed at the call site.
|
| 200 |
+
void unregister_comm(const std::string& group_name, ncclComm_t comm) {
|
| 201 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 202 |
+
auto it = group_to_comm_.find(group_name);
|
| 203 |
+
if (it != group_to_comm_.end() && it->second == comm) {
|
| 204 |
+
group_to_comm_.erase(it);
|
| 205 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 206 |
+
devcomm_registry_.erase(group_name);
|
| 207 |
+
#endif
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// Destructor: Clean up all registered device communicators.
|
| 212 |
+
// This is a best-effort cleanup. If the CUDA context has already been
|
| 213 |
+
// destroyed, the cleanup will be skipped. All errors are caught and ignored
|
| 214 |
+
// to prevent exceptions from propagating during destruction.
|
| 215 |
+
~NCCLDevCommManager() noexcept {
|
| 216 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 217 |
+
// Best effort to destroy the device communicators. Skip if CUDA context has
|
| 218 |
+
// exited.
|
| 219 |
+
try {
|
| 220 |
+
c10::cuda::CUDAGuard guard(device_);
|
| 221 |
+
// Make sure all kernels have completed before destroying the device
|
| 222 |
+
// communicator. This is important to ensure no kernels are still using
|
| 223 |
+
// the device communicator when we destroy it.
|
| 224 |
+
C10_CUDA_CHECK(cudaDeviceSynchronize());
|
| 225 |
+
// Iterate through all groups and their device communicators
|
| 226 |
+
for (auto& [group_name, group_map] : devcomm_registry_) {
|
| 227 |
+
// Find the host communicator for the group.
|
| 228 |
+
// Device communicators need the host communicator for destruction.
|
| 229 |
+
auto comm_it = group_to_comm_.find(group_name);
|
| 230 |
+
if (comm_it != group_to_comm_.end()) {
|
| 231 |
+
// Destroy each device communicator in this group
|
| 232 |
+
for (auto& [_, devcomm] : group_map) {
|
| 233 |
+
// Destroy the device communicator using the host communicator
|
| 234 |
+
ncclDevCommDestroy(comm_it->second, &devcomm);
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
} catch (...) {
|
| 239 |
+
// Ignore the error - we're in a destructor and can't throw
|
| 240 |
+
// Log a warning for debugging purposes
|
| 241 |
+
LOG(WARNING)
|
| 242 |
+
<< "Failed to destroy the NCCL device communicator, skipping";
|
| 243 |
+
}
|
| 244 |
+
#endif // NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
private:
|
| 248 |
+
// Device where the NCCL device communicator manager is created.
|
| 249 |
+
// The manager is device-specific and cannot be used across multiple devices.
|
| 250 |
+
const c10::Device device_;
|
| 251 |
+
|
| 252 |
+
// Mutex to protect the registry maps.
|
| 253 |
+
std::mutex mutex_;
|
| 254 |
+
|
| 255 |
+
// A map from process group name to the host-side NCCL communicator.
|
| 256 |
+
// The host communicator is required for creating and destroying device
|
| 257 |
+
// communicators. It should be registered before any device communicators
|
| 258 |
+
// for the same group.
|
| 259 |
+
std::unordered_map<std::string, ncclComm_t> group_to_comm_;
|
| 260 |
+
|
| 261 |
+
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 262 |
+
// A two-level map for device communicators:
|
| 263 |
+
// - First level: keyed by process group name
|
| 264 |
+
// - Second level: keyed by an optional key (defaults to caller function name
|
| 265 |
+
// via __builtin_FUNCTION())
|
| 266 |
+
//
|
| 267 |
+
// This structure allows multiple device communicators per process group,
|
| 268 |
+
// which is useful when different functions need separate device communicators
|
| 269 |
+
// for concurrent operations. The key defaults to the caller's function name,
|
| 270 |
+
// but can be customized for cases where a single function needs multiple
|
| 271 |
+
// device communicators.
|
| 272 |
+
std::unordered_map<std::string, std::unordered_map<std::string, ncclDevComm>>
|
| 273 |
+
devcomm_registry_;
|
| 274 |
+
#endif // NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
| 275 |
+
};
|
| 276 |
+
|
| 277 |
+
} // namespace c10d::symmetric_memory
|
| 278 |
+
#endif // NCCL_HAS_SYMMEM_SUPPORT
|
| 279 |
+
|
| 280 |
+
#else
|
| 281 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 282 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_ep.hpp
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
#include <c10/util/intrusive_ptr.h>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/ProcessGroup.hpp>
|
| 8 |
+
#include <optional>
|
| 9 |
+
|
| 10 |
+
namespace c10d::nccl_ep {
|
| 11 |
+
|
| 12 |
+
struct NcclEpGroup : c10::intrusive_ptr_target {
|
| 13 |
+
void* group{nullptr}; // ncclEpGroup_t, opaque to avoid including nccl_ep.h
|
| 14 |
+
|
| 15 |
+
NcclEpGroup() = default;
|
| 16 |
+
~NcclEpGroup();
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
struct NcclEpHandle : c10::intrusive_ptr_target {
|
| 20 |
+
void* handle{nullptr}; // ncclEpHandle_t, opaque
|
| 21 |
+
// The library stashes topk_idx's device pointer on the handle (per nccl_ep.h:
|
| 22 |
+
// "User-owned (do not free). LL reads directly; HT uses cached
|
| 23 |
+
// hybridep.topk_idx"). recv_total_counter is allocated by us and read back
|
| 24 |
+
// by nccl_ep_handle_get_num_recv_tokens. Keep both alive for the handle's
|
| 25 |
+
// lifetime so nccl_ep can't read freed memory.
|
| 26 |
+
at::Tensor topk_idx;
|
| 27 |
+
at::Tensor recv_total_counter;
|
| 28 |
+
|
| 29 |
+
NcclEpHandle(void* handle, at::Tensor topk_idx, at::Tensor recv_total_counter)
|
| 30 |
+
: handle(handle),
|
| 31 |
+
topk_idx(std::move(topk_idx)),
|
| 32 |
+
recv_total_counter(std::move(recv_total_counter)) {}
|
| 33 |
+
~NcclEpHandle();
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
TORCH_API c10::intrusive_ptr<NcclEpGroup> nccl_ep_create_group(
|
| 37 |
+
const c10::intrusive_ptr<::c10d::ProcessGroup>& pg,
|
| 38 |
+
int64_t num_experts,
|
| 39 |
+
int64_t max_dispatch_tokens_per_rank,
|
| 40 |
+
int64_t max_recv_tokens_per_rank,
|
| 41 |
+
int64_t max_token_bytes);
|
| 42 |
+
|
| 43 |
+
TORCH_API c10::intrusive_ptr<NcclEpHandle> nccl_ep_create_handle(
|
| 44 |
+
const c10::intrusive_ptr<NcclEpGroup>& group,
|
| 45 |
+
const at::Tensor& topk_idx,
|
| 46 |
+
const std::optional<at::Tensor>& recv_expert_counter);
|
| 47 |
+
|
| 48 |
+
TORCH_API int64_t nccl_ep_handle_get_num_recv_tokens(
|
| 49 |
+
const c10::intrusive_ptr<NcclEpHandle>& handle);
|
| 50 |
+
|
| 51 |
+
TORCH_API void nccl_ep_dispatch(
|
| 52 |
+
const c10::intrusive_ptr<NcclEpHandle>& handle,
|
| 53 |
+
const at::Tensor& tokens,
|
| 54 |
+
const at::Tensor& topk_weights,
|
| 55 |
+
at::Tensor& out_tokens,
|
| 56 |
+
at::Tensor& out_topk_weights,
|
| 57 |
+
at::Tensor& out_topk_idx);
|
| 58 |
+
|
| 59 |
+
TORCH_API void nccl_ep_combine(
|
| 60 |
+
const c10::intrusive_ptr<NcclEpHandle>& handle,
|
| 61 |
+
const at::Tensor& expert_tokens,
|
| 62 |
+
at::Tensor& out_tokens);
|
| 63 |
+
|
| 64 |
+
} // namespace c10d::nccl_ep
|
| 65 |
+
|
| 66 |
+
#else
|
| 67 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 68 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nccl_extension.hpp
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 7 |
+
|
| 8 |
+
namespace c10d::nccl_extension {
|
| 9 |
+
|
| 10 |
+
TORCH_API bool is_nccl_symmem_available();
|
| 11 |
+
|
| 12 |
+
TORCH_API void nccl_put(at::Tensor& tensor, const int64_t peer);
|
| 13 |
+
|
| 14 |
+
TORCH_API void nccl_get(at::Tensor& tensor, const int64_t peer);
|
| 15 |
+
|
| 16 |
+
TORCH_API void nccl_get_out(
|
| 17 |
+
at::Tensor& dst,
|
| 18 |
+
const c10::intrusive_ptr<c10d::symmetric_memory::SymmetricMemory>& hdl,
|
| 19 |
+
int64_t offset,
|
| 20 |
+
int64_t size,
|
| 21 |
+
int64_t peer);
|
| 22 |
+
|
| 23 |
+
TORCH_API void nccl_wait_for_signal(at::Tensor& sigpad, int64_t signal);
|
| 24 |
+
|
| 25 |
+
TORCH_API void nccl_put_with_signal(
|
| 26 |
+
at::Tensor& tensor,
|
| 27 |
+
int64_t signal,
|
| 28 |
+
int64_t peer);
|
| 29 |
+
|
| 30 |
+
// Simultaneously reduce N blocks of a 2-D input tensor from a shared symmetric
|
| 31 |
+
// memory buffer, routing each to a specific destination rank. Blocks are
|
| 32 |
+
// described by inclusive-prefix-sum offsets along `dim` (0 or 1); all blocks
|
| 33 |
+
// must have equal size.
|
| 34 |
+
TORCH_API void nccl_reduce_scatter_offset(
|
| 35 |
+
const at::Tensor& input,
|
| 36 |
+
at::TensorList out,
|
| 37 |
+
const std::string& group_name,
|
| 38 |
+
int64_t dim,
|
| 39 |
+
std::optional<at::IntArrayRef> offsets,
|
| 40 |
+
std::optional<at::IntArrayRef> dst_ranks,
|
| 41 |
+
const std::string& red_op);
|
| 42 |
+
|
| 43 |
+
// Permute-free all-to-all: scatter shards along `scatter_dim`, gather peer
|
| 44 |
+
// chunks along `gather_dim`. Supported: (scatter_dim=1, gather_dim=0) -> [rows,
|
| 45 |
+
// p*lc] to [p, rows, lc]; (scatter_dim=0, gather_dim=1) -> [p*lr, cols] to [lr,
|
| 46 |
+
// p, cols].
|
| 47 |
+
TORCH_API void nccl_all_to_all_nd(
|
| 48 |
+
const at::Tensor& input,
|
| 49 |
+
at::Tensor& out,
|
| 50 |
+
int64_t scatter_dim,
|
| 51 |
+
int64_t gather_dim,
|
| 52 |
+
const std::string& group_name);
|
| 53 |
+
} // namespace c10d::nccl_extension
|
| 54 |
+
|
| 55 |
+
#else
|
| 56 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 57 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nvshmem_extension.hpp
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
| 7 |
+
|
| 8 |
+
#define NVSHMEM_CHECK(stmt, msg) \
|
| 9 |
+
do { \
|
| 10 |
+
int result = (stmt); \
|
| 11 |
+
TORCH_CHECK( \
|
| 12 |
+
result == 0, \
|
| 13 |
+
std::string(__FILE__) + ":" + std::to_string(__LINE__) + " " + msg + \
|
| 14 |
+
". Error code: " + std::to_string(result)); \
|
| 15 |
+
} while (0)
|
| 16 |
+
|
| 17 |
+
namespace c10d::nvshmem_extension {
|
| 18 |
+
|
| 19 |
+
// Check if NVSHMEM is available
|
| 20 |
+
TORCH_API bool is_nvshmem_available();
|
| 21 |
+
|
| 22 |
+
// Initializes the device state in CUmodule so that it’s able to perform NVSHMEM
|
| 23 |
+
// operations.
|
| 24 |
+
TORCH_API void nvshmemx_cumodule_init(uintptr_t module);
|
| 25 |
+
|
| 26 |
+
TORCH_API void nvshmem_put(at::Tensor& tensor, const int64_t peer);
|
| 27 |
+
|
| 28 |
+
TORCH_API void nvshmem_get(at::Tensor& tensor, const int64_t peer);
|
| 29 |
+
|
| 30 |
+
TORCH_API void nvshmem_get_out(
|
| 31 |
+
at::Tensor& dst,
|
| 32 |
+
const c10::intrusive_ptr<c10d::symmetric_memory::SymmetricMemory>& hdl,
|
| 33 |
+
int64_t offset,
|
| 34 |
+
int64_t size,
|
| 35 |
+
int64_t peer);
|
| 36 |
+
|
| 37 |
+
at::Tensor nvshmem_broadcast(
|
| 38 |
+
at::Tensor& input,
|
| 39 |
+
const int64_t root,
|
| 40 |
+
const std::string& group_name);
|
| 41 |
+
|
| 42 |
+
TORCH_API void nvshmem_wait_for_signal(
|
| 43 |
+
at::Tensor& sigpad,
|
| 44 |
+
int64_t signal,
|
| 45 |
+
int64_t peer);
|
| 46 |
+
|
| 47 |
+
TORCH_API void nvshmem_put_with_signal(
|
| 48 |
+
at::Tensor& tensor,
|
| 49 |
+
at::Tensor& sigpad,
|
| 50 |
+
int64_t signal,
|
| 51 |
+
int64_t peer);
|
| 52 |
+
|
| 53 |
+
at::Tensor nvshmem_all_to_all(
|
| 54 |
+
at::Tensor& input,
|
| 55 |
+
at::Tensor& out,
|
| 56 |
+
std::string group_name);
|
| 57 |
+
|
| 58 |
+
void all_to_all_vdev(
|
| 59 |
+
at::Tensor& input,
|
| 60 |
+
at::Tensor& out,
|
| 61 |
+
at::Tensor& in_splits,
|
| 62 |
+
at::Tensor& out_splits_offsets,
|
| 63 |
+
std::string group_name);
|
| 64 |
+
|
| 65 |
+
void all_to_all_vdev_2d(
|
| 66 |
+
at::Tensor& input,
|
| 67 |
+
at::Tensor& out,
|
| 68 |
+
at::Tensor& in_splits,
|
| 69 |
+
at::Tensor& out_splits_offsets,
|
| 70 |
+
std::string group_name,
|
| 71 |
+
std::optional<int64_t> major_align = std::nullopt);
|
| 72 |
+
|
| 73 |
+
void all_to_all_vdev_2d_offset(
|
| 74 |
+
at::Tensor& input,
|
| 75 |
+
at::Tensor& out,
|
| 76 |
+
at::Tensor& in_splits_offsets,
|
| 77 |
+
at::Tensor& out_splits_offsets,
|
| 78 |
+
std::string group_name);
|
| 79 |
+
|
| 80 |
+
void tile_reduce(
|
| 81 |
+
at::Tensor& in_tile,
|
| 82 |
+
at::Tensor& out_tile,
|
| 83 |
+
int64_t root,
|
| 84 |
+
std::string group_name,
|
| 85 |
+
std::string reduce_op = "sum");
|
| 86 |
+
|
| 87 |
+
void multi_root_tile_reduce(
|
| 88 |
+
at::ArrayRef<at::Tensor> in_tiles,
|
| 89 |
+
at::Tensor& out_tile,
|
| 90 |
+
at::ArrayRef<int64_t> roots,
|
| 91 |
+
std::string group_name,
|
| 92 |
+
std::string reduce_op = "sum");
|
| 93 |
+
|
| 94 |
+
} // namespace c10d::nvshmem_extension
|
| 95 |
+
|
| 96 |
+
#else
|
| 97 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 98 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/symm_mem/nvshmem_team_manager.hpp
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDACachingAllocator.h>
|
| 5 |
+
#include <c10/cuda/CUDAException.h>
|
| 6 |
+
#include <c10/cuda/CUDAGuard.h>
|
| 7 |
+
#include <c10/util/Exception.h>
|
| 8 |
+
#include <string>
|
| 9 |
+
#include <unordered_map>
|
| 10 |
+
#include <vector>
|
| 11 |
+
|
| 12 |
+
// Starting from NVSHMEM 3.3.9, nvshmem_host.h exists so that we can cleanly
|
| 13 |
+
// include only the nvshmem host library headers:
|
| 14 |
+
// #include <nvshmem_host.h>
|
| 15 |
+
// It translates into the following two lines:
|
| 16 |
+
#if !defined(USE_ROCM)
|
| 17 |
+
#include <host/nvshmem_api.h>
|
| 18 |
+
#include <host/nvshmemx_api.h>
|
| 19 |
+
#else
|
| 20 |
+
#include <rocshmem/rocshmem.hpp>
|
| 21 |
+
#endif
|
| 22 |
+
// For maximum compatibility, we use the "host/" style for now.
|
| 23 |
+
|
| 24 |
+
namespace c10d::nvshmem_extension {
|
| 25 |
+
|
| 26 |
+
// This corresponds to max nblocks
|
| 27 |
+
constexpr int MAX_N_TEAMS = 128;
|
| 28 |
+
|
| 29 |
+
// A pool of teams for each group. These are duplicate teams.
|
| 30 |
+
using TeamPool = std::vector<nvshmem_team_t>;
|
| 31 |
+
|
| 32 |
+
// Manage all the team business. Singleton.
|
| 33 |
+
class TeamManager {
|
| 34 |
+
public:
|
| 35 |
+
// Constructor
|
| 36 |
+
explicit TeamManager(const c10::Device device) : device_(device) {}
|
| 37 |
+
|
| 38 |
+
// Get single, global manager.
|
| 39 |
+
static TeamManager& get(const c10::Device device) {
|
| 40 |
+
static TeamManager manager(device);
|
| 41 |
+
TORCH_CHECK(
|
| 42 |
+
manager.device_ == device,
|
| 43 |
+
"Detected use of TeamManager on multiple devices. This is not supported.");
|
| 44 |
+
return manager;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Get a team for a group.
|
| 48 |
+
nvshmem_team_t get_team(
|
| 49 |
+
const std::string& group_name,
|
| 50 |
+
const std::vector<int>& global_ranks) {
|
| 51 |
+
auto [team_pool, pool_updated] =
|
| 52 |
+
group_to_team_pool(group_name, global_ranks, 1);
|
| 53 |
+
// Return the first available team
|
| 54 |
+
return team_pool[0];
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Get n teams for a group.
|
| 58 |
+
// The first element of the returned pair is the team pool on host side.
|
| 59 |
+
// The second element of the returned pair is the team pool on device side.
|
| 60 |
+
// This API must be call with a device guard.
|
| 61 |
+
std::pair<const TeamPool&, nvshmem_team_t*> get_n_teams(
|
| 62 |
+
const std::string& group_name,
|
| 63 |
+
const std::vector<int>& global_ranks,
|
| 64 |
+
const int need_n) {
|
| 65 |
+
// A device guard is required for malloc and memcpy below
|
| 66 |
+
c10::cuda::CUDAGuard guard(device_);
|
| 67 |
+
// Get the team pool with the requested number of teams
|
| 68 |
+
auto [team_pool, pool_updated] =
|
| 69 |
+
group_to_team_pool(group_name, global_ranks, need_n);
|
| 70 |
+
// Check if the pool already exists in device memory
|
| 71 |
+
nvshmem_team_t* team_pool_dev = nullptr;
|
| 72 |
+
constexpr auto pool_bytes = sizeof(nvshmem_team_t) * MAX_N_TEAMS;
|
| 73 |
+
auto it = team_pool_devptrs_.find(group_name);
|
| 74 |
+
if (it == team_pool_devptrs_.end()) {
|
| 75 |
+
// If not, allocate a new pool in device memory
|
| 76 |
+
team_pool_dev = reinterpret_cast<nvshmem_team_t*>(
|
| 77 |
+
c10::cuda::CUDACachingAllocator::raw_alloc(pool_bytes));
|
| 78 |
+
team_pool_devptrs_[group_name] = team_pool_dev;
|
| 79 |
+
} else {
|
| 80 |
+
team_pool_dev = it->second;
|
| 81 |
+
}
|
| 82 |
+
// Update the pool in device memory if host side pool is updated
|
| 83 |
+
if (pool_updated) {
|
| 84 |
+
TORCH_INTERNAL_ASSERT(team_pool.size() == MAX_N_TEAMS);
|
| 85 |
+
auto stream = at::cuda::getCurrentCUDAStream();
|
| 86 |
+
C10_CUDA_CHECK(cudaMemcpyAsync(
|
| 87 |
+
team_pool_dev,
|
| 88 |
+
team_pool.data(),
|
| 89 |
+
pool_bytes,
|
| 90 |
+
cudaMemcpyHostToDevice,
|
| 91 |
+
stream));
|
| 92 |
+
}
|
| 93 |
+
return std::make_pair(std::cref(team_pool), team_pool_dev);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
~TeamManager() noexcept {
|
| 97 |
+
// Free the team pools in device memory
|
| 98 |
+
// Note that we do it in a best effort manner because the team pool is
|
| 99 |
+
// managed by a static TeamManager and the destruction order of static
|
| 100 |
+
// objects is undetermined. If the destructor is called after the CUDA
|
| 101 |
+
// context is destroyed, cudaFree would fail.
|
| 102 |
+
try {
|
| 103 |
+
// cudaFree generally implies a device synchronization, meaning it will
|
| 104 |
+
// block until all preceding CUDA operations on the device have completed
|
| 105 |
+
// before freeing the memory. Thus we don't need to worry about freeing
|
| 106 |
+
// the memory before CUDA kernels complete.
|
| 107 |
+
for (auto& [_, team_pool_dev] : team_pool_devptrs_) {
|
| 108 |
+
c10::cuda::CUDACachingAllocator::raw_delete(team_pool_dev);
|
| 109 |
+
}
|
| 110 |
+
} catch (...) {
|
| 111 |
+
// Ignore the error
|
| 112 |
+
std::cerr << "Failed to free the team pool in device memory, skipping\n";
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
private:
|
| 117 |
+
// Get the team pool for a group. If the pool doesn't exist, create it. If the
|
| 118 |
+
// pool exists but is not large enough, create more teams.
|
| 119 |
+
// The first element of the returned pair is the team pool on host side.
|
| 120 |
+
// The second element of the returned pair is a boolean indicating if the pool
|
| 121 |
+
// is updated.
|
| 122 |
+
std::pair<const TeamPool&, bool> group_to_team_pool(
|
| 123 |
+
const std::string& group_name,
|
| 124 |
+
const std::vector<int>& global_ranks,
|
| 125 |
+
const int need_n) {
|
| 126 |
+
TORCH_CHECK(need_n < MAX_N_TEAMS, "Too many teams requested");
|
| 127 |
+
// Guarding the NVSHMEM API calls below just to be safe
|
| 128 |
+
c10::cuda::CUDAGuard guard(device_);
|
| 129 |
+
|
| 130 |
+
// Insert a new team pool if not exists
|
| 131 |
+
auto [it, inserted] = group_name_to_team_pool_.emplace(
|
| 132 |
+
group_name, TeamPool(MAX_N_TEAMS, NVSHMEM_TEAM_INVALID));
|
| 133 |
+
auto& team_pool = it->second;
|
| 134 |
+
bool pool_updated = inserted;
|
| 135 |
+
|
| 136 |
+
// Create new teams if what's requested is more than what we have
|
| 137 |
+
int stride = 0; // stride in globe, uninitialized
|
| 138 |
+
for (int i = 0; i < need_n; ++i) {
|
| 139 |
+
if (team_pool[i] != NVSHMEM_TEAM_INVALID) {
|
| 140 |
+
continue;
|
| 141 |
+
}
|
| 142 |
+
// Some checks before we create new teams
|
| 143 |
+
if (stride == 0) { // Check only once
|
| 144 |
+
TORCH_CHECK(global_ranks.size() > 1);
|
| 145 |
+
stride = global_ranks[1] - global_ranks[0];
|
| 146 |
+
for (size_t r = 1; r < global_ranks.size(); ++r) {
|
| 147 |
+
TORCH_CHECK(global_ranks[r] - global_ranks[r - 1] == stride);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
nvshmem_team_t team = NVSHMEM_TEAM_INVALID;
|
| 151 |
+
nvshmem_team_split_strided(
|
| 152 |
+
NVSHMEM_TEAM_WORLD,
|
| 153 |
+
global_ranks[0],
|
| 154 |
+
stride,
|
| 155 |
+
global_ranks.size(),
|
| 156 |
+
nullptr,
|
| 157 |
+
0,
|
| 158 |
+
&team);
|
| 159 |
+
TORCH_CHECK(team != NVSHMEM_TEAM_INVALID, "Failed to create a new team");
|
| 160 |
+
team_pool[i] = team;
|
| 161 |
+
pool_updated = true;
|
| 162 |
+
}
|
| 163 |
+
return std::make_pair(std::cref(team_pool), pool_updated);
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
private:
|
| 167 |
+
// Device where the team manager is created
|
| 168 |
+
const c10::Device device_;
|
| 169 |
+
// A map from group name to team pool for that group.
|
| 170 |
+
std::unordered_map<std::string, TeamPool> group_name_to_team_pool_;
|
| 171 |
+
// A map from group name to team pool array in device memory.
|
| 172 |
+
std::unordered_map<std::string, nvshmem_team_t*> team_pool_devptrs_;
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
} // namespace c10d::nvshmem_extension
|
| 176 |
+
|
| 177 |
+
#else
|
| 178 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 179 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/ucc/UCCTracing.hpp
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef USE_C10D_UCC
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/distributed/c10d/ucc/UCCUtils.hpp>
|
| 7 |
+
|
| 8 |
+
namespace c10d {
|
| 9 |
+
|
| 10 |
+
#define RECORD_COMMS_TRACE( \
|
| 11 |
+
_comms_tracer, _work, _opType, _rank, _comm_size, _inTensors, _outTensors) \
|
| 12 |
+
do { \
|
| 13 |
+
if (torch_ucc_config.enable_comms_logger) { \
|
| 14 |
+
_comms_tracer->recordComms( \
|
| 15 |
+
opTypeToString(_opType), \
|
| 16 |
+
(uintptr_t)_work.get(), \
|
| 17 |
+
_rank, \
|
| 18 |
+
_comm_size, \
|
| 19 |
+
_inTensors, \
|
| 20 |
+
_outTensors); \
|
| 21 |
+
} \
|
| 22 |
+
} while (0)
|
| 23 |
+
|
| 24 |
+
// interfaces to collect communication traces
|
| 25 |
+
class TORCH_API CommTraceLogger : public torch::CustomClassHolder {
|
| 26 |
+
private:
|
| 27 |
+
std::vector<std::string> comms_trace_;
|
| 28 |
+
std::vector<std::string> curBlocks_; /* unused */
|
| 29 |
+
std::vector<int64_t> curOutSplitSizes_;
|
| 30 |
+
std::vector<int64_t> curInSplitSizes_;
|
| 31 |
+
int curRoot_ = -1;
|
| 32 |
+
unsigned long seqnum = 0;
|
| 33 |
+
|
| 34 |
+
public:
|
| 35 |
+
void setCurBlock(const std::string& name); /* unused */
|
| 36 |
+
void popBlock(); /* unused */
|
| 37 |
+
// record root info if applicable, e.g., broadcast, gather, scatter
|
| 38 |
+
void recordOptionalInfo(int root = -1);
|
| 39 |
+
// record input/output splits of Alltoallv
|
| 40 |
+
void recordOptionalInfo(
|
| 41 |
+
const std::vector<int64_t>& outputSplitSizes = {},
|
| 42 |
+
const std::vector<int64_t>& inputSplitSizes = {});
|
| 43 |
+
// record essential comms information
|
| 44 |
+
void recordComms(
|
| 45 |
+
const std::string& collName,
|
| 46 |
+
const uintptr_t workReq = 0,
|
| 47 |
+
const int rank = -1,
|
| 48 |
+
const int world_size = -1,
|
| 49 |
+
const std::vector<at::Tensor>& inputTensors = {},
|
| 50 |
+
const std::vector<at::Tensor>& outputTensor = {});
|
| 51 |
+
// return collected comms traces
|
| 52 |
+
std::vector<std::string>& getCommsTrace() {
|
| 53 |
+
return comms_trace_;
|
| 54 |
+
}
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
} // namespace c10d
|
| 58 |
+
|
| 59 |
+
#endif // USE_C10D_UCC
|
| 60 |
+
|
| 61 |
+
#else
|
| 62 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 63 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/c10d/ucc/UCCUtils.hpp
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef USE_C10D_UCC
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/distributed/c10d/ProcessGroup.hpp>
|
| 7 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 8 |
+
#include <ucc/api/ucc.h>
|
| 9 |
+
|
| 10 |
+
namespace c10d {
|
| 11 |
+
|
| 12 |
+
// Macro to generate the error message on a non-successful UCC return value.
|
| 13 |
+
#define TORCH_UCC_GET_ERROR_MSG(_err, _error_msg, _result) \
|
| 14 |
+
do { \
|
| 15 |
+
_err = c10::str( \
|
| 16 |
+
"[", \
|
| 17 |
+
std::string(__FILE__), \
|
| 18 |
+
":", \
|
| 19 |
+
std::to_string(__LINE__), \
|
| 20 |
+
"] ", \
|
| 21 |
+
logger->getLogPrefix(), \
|
| 22 |
+
_error_msg, \
|
| 23 |
+
", error code ", \
|
| 24 |
+
_result, \
|
| 25 |
+
": ", \
|
| 26 |
+
ucc_status_string(_result), \
|
| 27 |
+
", system error code ", \
|
| 28 |
+
errno); \
|
| 29 |
+
} while (0)
|
| 30 |
+
|
| 31 |
+
// Macro to throw on a non-successful UCC return value.
|
| 32 |
+
#define TORCH_UCC_CHECK(_cmd, _error_msg) \
|
| 33 |
+
do { \
|
| 34 |
+
ucc_status_t result = _cmd; \
|
| 35 |
+
if (result != UCC_OK) { \
|
| 36 |
+
std::string err; \
|
| 37 |
+
TORCH_UCC_GET_ERROR_MSG(err, _error_msg, result); \
|
| 38 |
+
TORCH_CHECK(false, err); \
|
| 39 |
+
} \
|
| 40 |
+
} while (0)
|
| 41 |
+
|
| 42 |
+
// Macro and throw on a non-successful UCC return value and free its request.
|
| 43 |
+
#define TORCH_UCC_CHECK_REQUEST(_request, _cmd, _error_msg) \
|
| 44 |
+
do { \
|
| 45 |
+
ucc_status_t result = _cmd; \
|
| 46 |
+
if (result != UCC_OK) { \
|
| 47 |
+
std::string err; \
|
| 48 |
+
TORCH_UCC_GET_ERROR_MSG(err, _error_msg, result); \
|
| 49 |
+
if (_request != nullptr) { \
|
| 50 |
+
ucc_collective_finalize(_request); \
|
| 51 |
+
} \
|
| 52 |
+
TORCH_CHECK(false, err); \
|
| 53 |
+
} \
|
| 54 |
+
} while (0)
|
| 55 |
+
|
| 56 |
+
// Macros to print logs with unified format
|
| 57 |
+
#define TORCH_UCC_LOG_ERROR(_phase, _msg) \
|
| 58 |
+
LOG(ERROR) << logger->getLogPrefix(_phase) << "[ERROR] " << _msg;
|
| 59 |
+
#define TORCH_UCC_LOG_INFO(_phase, _msg) \
|
| 60 |
+
LOG(INFO) << logger->getLogPrefix(_phase) << "[INFO] " << _msg;
|
| 61 |
+
#define TORCH_UCC_LOG_DEBUG(_phase, _msg) \
|
| 62 |
+
VLOG(1) << logger->getLogPrefix(_phase) << "[DEBUG] " << _msg;
|
| 63 |
+
|
| 64 |
+
enum torch_ucc_phase_t {
|
| 65 |
+
TORCH_UCC_UNKNOWN = -1,
|
| 66 |
+
TORCH_UCC_INIT,
|
| 67 |
+
TORCH_UCC_HEALTH_CHECK,
|
| 68 |
+
TORCH_UCC_READY,
|
| 69 |
+
TORCH_UCC_COLL_POST,
|
| 70 |
+
TORCH_UCC_COLL_PROGRESS,
|
| 71 |
+
TORCH_UCC_FINALIZE,
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
const std::map<torch_ucc_phase_t, std::string> ucc_phase_map = {
|
| 75 |
+
{TORCH_UCC_UNKNOWN, "UNKNOWN"},
|
| 76 |
+
{TORCH_UCC_INIT, "INIT"},
|
| 77 |
+
{TORCH_UCC_HEALTH_CHECK, "HEALTH_CHECK"},
|
| 78 |
+
{TORCH_UCC_READY, "READY"},
|
| 79 |
+
{TORCH_UCC_COLL_POST, "COLL_POST"},
|
| 80 |
+
{TORCH_UCC_COLL_PROGRESS, "COLL_PROGRESS"},
|
| 81 |
+
{TORCH_UCC_FINALIZE, "FINALIZE"},
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
class CommTraceLogger;
|
| 85 |
+
|
| 86 |
+
class TORCH_API ProcessGroupUCCLogger : public torch::CustomClassHolder {
|
| 87 |
+
public:
|
| 88 |
+
ProcessGroupUCCLogger();
|
| 89 |
+
ProcessGroupUCCLogger(std::string log_prefix, torch_ucc_phase_t phase);
|
| 90 |
+
|
| 91 |
+
std::string getLogPrefix(torch_ucc_phase_t phase = TORCH_UCC_UNKNOWN);
|
| 92 |
+
void setLogPrefix(std::string log_prefix);
|
| 93 |
+
inline void setPhase(torch_ucc_phase_t phase) {
|
| 94 |
+
local_phase = phase;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
void initCommsTracer();
|
| 98 |
+
void flushComms(int rank, int world_size);
|
| 99 |
+
std::shared_ptr<CommTraceLogger> trace_generator = nullptr;
|
| 100 |
+
|
| 101 |
+
protected:
|
| 102 |
+
std::string log_prefix;
|
| 103 |
+
torch_ucc_phase_t local_phase = TORCH_UCC_UNKNOWN;
|
| 104 |
+
bool initialized_CommTraceLogger = false;
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
struct torch_ucc_oob_coll_info_t {
|
| 108 |
+
c10::intrusive_ptr<Store> store;
|
| 109 |
+
uint32_t comm_id;
|
| 110 |
+
int rank;
|
| 111 |
+
int size;
|
| 112 |
+
void* rbuf;
|
| 113 |
+
size_t msglen;
|
| 114 |
+
std::string getKey(std::string key) {
|
| 115 |
+
return std::to_string(comm_id) + key;
|
| 116 |
+
}
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
class CommBase {
|
| 120 |
+
public:
|
| 121 |
+
CommBase(const c10::intrusive_ptr<ProcessGroupUCCLogger>& logger_)
|
| 122 |
+
: logger(logger_) {}
|
| 123 |
+
virtual void progress() = 0;
|
| 124 |
+
virtual void free_request(ucc_coll_req_h request) = 0;
|
| 125 |
+
virtual ~CommBase() {}
|
| 126 |
+
c10::intrusive_ptr<ProcessGroupUCCLogger> logger;
|
| 127 |
+
};
|
| 128 |
+
class CommUCC : public CommBase {
|
| 129 |
+
public:
|
| 130 |
+
ucc_lib_h lib{nullptr};
|
| 131 |
+
ucc_context_h context{nullptr};
|
| 132 |
+
|
| 133 |
+
public:
|
| 134 |
+
void progress() override;
|
| 135 |
+
CommUCC(
|
| 136 |
+
std::shared_ptr<torch_ucc_oob_coll_info_t> oob,
|
| 137 |
+
const c10::intrusive_ptr<ProcessGroupUCCLogger>& logger);
|
| 138 |
+
void free_request(ucc_coll_req_h request) override;
|
| 139 |
+
~CommUCC();
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
+
ucc_status_t oob_allgather(
|
| 143 |
+
void* sbuf,
|
| 144 |
+
void* rbuf,
|
| 145 |
+
size_t msglen,
|
| 146 |
+
void* coll_info,
|
| 147 |
+
void** req);
|
| 148 |
+
|
| 149 |
+
ucc_status_t oob_allgather_test(void* req);
|
| 150 |
+
|
| 151 |
+
ucc_status_t oob_allgather_free(void* req);
|
| 152 |
+
|
| 153 |
+
// trim: remove spaces before and after the string view
|
| 154 |
+
// implementation borrowed from https://stackoverflow.com/a/17976541
|
| 155 |
+
inline std::string_view trim(std::string_view s) {
|
| 156 |
+
auto wsfront = std::find_if_not(
|
| 157 |
+
s.begin(), s.end(), [](int c) { return std::isspace(c); });
|
| 158 |
+
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) {
|
| 159 |
+
return std::isspace(c);
|
| 160 |
+
}).base();
|
| 161 |
+
return (
|
| 162 |
+
wsback <= wsfront ? "" : s.substr(wsfront - s.begin(), wsback - wsfront));
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
inline std::string tolower(std::string_view s) {
|
| 166 |
+
std::string result;
|
| 167 |
+
result.reserve(s.size());
|
| 168 |
+
for (auto c : s) {
|
| 169 |
+
result.push_back(std::tolower(c));
|
| 170 |
+
}
|
| 171 |
+
return result;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
inline std::vector<std::string> parse_list(std::string list) {
|
| 175 |
+
std::vector<std::string> result;
|
| 176 |
+
list = tolower(trim(list));
|
| 177 |
+
while (!list.empty()) {
|
| 178 |
+
const auto end_pos = list.find_first_of(',');
|
| 179 |
+
const auto token = trim(list.substr(0, end_pos));
|
| 180 |
+
result.push_back(std::string(token));
|
| 181 |
+
list = (end_pos != std::string_view::npos) ? list.substr(end_pos + 1) : "";
|
| 182 |
+
}
|
| 183 |
+
return result;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
} // namespace c10d
|
| 187 |
+
|
| 188 |
+
#endif // USE_C10D_UCC
|
| 189 |
+
|
| 190 |
+
#else
|
| 191 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 192 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/python_placement.h
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/utils/python_stub.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed {
|
| 7 |
+
void initPlacementBindings(PyObject* module);
|
| 8 |
+
} // namespace torch::distributed
|
| 9 |
+
|
| 10 |
+
#else
|
| 11 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 12 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/agent_utils.h
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/c10d/PrefixStore.hpp>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/utils.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// All RPC peers should call into this function at the same time. Each peer
|
| 10 |
+
// provides its own id and name, and this function uses the given Store to
|
| 11 |
+
// gather global name-to-id mapping on all peers.
|
| 12 |
+
TORCH_API std::unordered_map<std::string, worker_id_t> collectNames(
|
| 13 |
+
::c10d::PrefixStore store,
|
| 14 |
+
const worker_id_t selfId,
|
| 15 |
+
const std::string& selfName,
|
| 16 |
+
const int worldSize);
|
| 17 |
+
|
| 18 |
+
// Ranks in dynamic RPC groups will initially call into this to establish the
|
| 19 |
+
// name-to-id mapping for the current peers in the group. The current rank will
|
| 20 |
+
// put its own worker info in the store and discover all the ranks that came
|
| 21 |
+
// before it. NOTE: This needs to be called with the Dynamic RPC group
|
| 22 |
+
// membership management token held.
|
| 23 |
+
TORCH_API std::unordered_map<std::string, worker_id_t> collectCurrentNames(
|
| 24 |
+
::c10d::PrefixStore store,
|
| 25 |
+
const worker_id_t selfId,
|
| 26 |
+
const std::string& selfName);
|
| 27 |
+
|
| 28 |
+
// Remove name from Store, used in dynamic RPC groups.
|
| 29 |
+
// NOTE: This needs to be called with the Dynamic RPC group
|
| 30 |
+
// membership management token held.
|
| 31 |
+
TORCH_API void removeCurrentName(
|
| 32 |
+
::c10d::PrefixStore store,
|
| 33 |
+
const worker_id_t selfId,
|
| 34 |
+
const std::string& selfName);
|
| 35 |
+
|
| 36 |
+
// This performs a synchronization of all call counts by using store.
|
| 37 |
+
// All RPC peers wait for others to join to exit at the same time.
|
| 38 |
+
TORCH_API int syncCallCount(
|
| 39 |
+
::c10d::PrefixStore store,
|
| 40 |
+
const int worldSize,
|
| 41 |
+
int activeCalls = 0);
|
| 42 |
+
|
| 43 |
+
} // namespace torch::distributed::rpc
|
| 44 |
+
|
| 45 |
+
#else
|
| 46 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 47 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/message.h
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
#include <vector>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// An enum denoting common RPC errors to allow specific error handling for them.
|
| 10 |
+
// NOLINTNEXTLINE(performance-enum-size)
|
| 11 |
+
enum RPCErrorType {
|
| 12 |
+
UNKNOWN_ERROR = 0, /* Indicates that error type could not be parsed */
|
| 13 |
+
TIMEOUT = 1, /* Indicates that the RPC has timed out */
|
| 14 |
+
INTENTIONAL_FAILURE = 2 /* Deliberate failure, such as those injected by
|
| 15 |
+
FaultyAgent for testing */
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
// The enum values are bitwise ORed with MessageType
|
| 19 |
+
// They are bit flags starting from 0x100 and should have
|
| 20 |
+
// value such as 0x100, 0x200, 0x400, 0x800, 0xF00, etc.
|
| 21 |
+
// NOLINTNEXTLINE(performance-enum-size)
|
| 22 |
+
enum MessageTypeFlags {
|
| 23 |
+
REQUEST_TYPE = 0x100,
|
| 24 |
+
RESPONSE_TYPE = 0x200,
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
// Message types must have values between 0x00 to 0xff
|
| 28 |
+
// NOLINTNEXTLINE(performance-enum-size)
|
| 29 |
+
enum MessageType {
|
| 30 |
+
// messages for dist.rpc on builtin operators
|
| 31 |
+
SCRIPT_CALL = 0x00 | MessageTypeFlags::REQUEST_TYPE,
|
| 32 |
+
SCRIPT_RET = 0x01 | MessageTypeFlags::RESPONSE_TYPE,
|
| 33 |
+
|
| 34 |
+
// messages for dist.rpc on Python UDF
|
| 35 |
+
PYTHON_CALL = 0x02 | MessageTypeFlags::REQUEST_TYPE,
|
| 36 |
+
PYTHON_RET = 0x03 | MessageTypeFlags::RESPONSE_TYPE,
|
| 37 |
+
|
| 38 |
+
// messages for dist.remote on builtin operators and Python UDF
|
| 39 |
+
SCRIPT_REMOTE_CALL = 0x04 |
|
| 40 |
+
MessageTypeFlags::REQUEST_TYPE, // A remote call on a builtin operator
|
| 41 |
+
PYTHON_REMOTE_CALL =
|
| 42 |
+
0x05 | MessageTypeFlags::REQUEST_TYPE, // A remote call on a Python UDF
|
| 43 |
+
REMOTE_RET =
|
| 44 |
+
0x06 | MessageTypeFlags::RESPONSE_TYPE, // Response for remote calls for
|
| 45 |
+
// UDF, builtin, or script
|
| 46 |
+
|
| 47 |
+
// RRef related internal messages
|
| 48 |
+
SCRIPT_RREF_FETCH_CALL =
|
| 49 |
+
0x07 | MessageTypeFlags::REQUEST_TYPE, // A UserRRef<IValue> fetches value
|
| 50 |
+
// from owner
|
| 51 |
+
PYTHON_RREF_FETCH_CALL =
|
| 52 |
+
0x08 | MessageTypeFlags::REQUEST_TYPE, // A UserRRef<py::object> fetches
|
| 53 |
+
// value from owner
|
| 54 |
+
SCRIPT_RREF_FETCH_RET = 0x09 |
|
| 55 |
+
MessageTypeFlags::RESPONSE_TYPE, // An OwnerRRef sends ivalue to user
|
| 56 |
+
PYTHON_RREF_FETCH_RET = 0x0a |
|
| 57 |
+
MessageTypeFlags::RESPONSE_TYPE, // An OwnerRRef sends py::object to user
|
| 58 |
+
RREF_USER_DELETE = 0x0b |
|
| 59 |
+
MessageTypeFlags::REQUEST_TYPE, // A UserRRef tells the owner to deref
|
| 60 |
+
RREF_FORK_REQUEST =
|
| 61 |
+
0x0c | MessageTypeFlags::REQUEST_TYPE, // A child UserRRef tells the owner
|
| 62 |
+
// about itself
|
| 63 |
+
RREF_CHILD_ACCEPT =
|
| 64 |
+
0x0d | MessageTypeFlags::REQUEST_TYPE, // A child UserRRef tells parent
|
| 65 |
+
// that owner knows it
|
| 66 |
+
RREF_ACK =
|
| 67 |
+
0x0e | MessageTypeFlags::RESPONSE_TYPE, // ACK to internal RRef messages
|
| 68 |
+
|
| 69 |
+
// Messages with autograd info
|
| 70 |
+
FORWARD_AUTOGRAD_REQ = 0x0f | MessageTypeFlags::REQUEST_TYPE,
|
| 71 |
+
FORWARD_AUTOGRAD_RESP = 0x10 | MessageTypeFlags::RESPONSE_TYPE,
|
| 72 |
+
|
| 73 |
+
// Messages to propagate gradients on the backward pass.
|
| 74 |
+
BACKWARD_AUTOGRAD_REQ = 0x11 | MessageTypeFlags::REQUEST_TYPE,
|
| 75 |
+
BACKWARD_AUTOGRAD_RESP = 0x12 | MessageTypeFlags::RESPONSE_TYPE,
|
| 76 |
+
|
| 77 |
+
// Messages to tell workers to clean up their autograd context.
|
| 78 |
+
CLEANUP_AUTOGRAD_CONTEXT_REQ = 0x13 | MessageTypeFlags::REQUEST_TYPE,
|
| 79 |
+
CLEANUP_AUTOGRAD_CONTEXT_RESP = 0x14 | MessageTypeFlags::RESPONSE_TYPE,
|
| 80 |
+
|
| 81 |
+
// Messages that tell workers to run requests with profiling enabled.
|
| 82 |
+
RUN_WITH_PROFILING_REQ = 0x15 | MessageTypeFlags::REQUEST_TYPE,
|
| 83 |
+
RUN_WITH_PROFILING_RESP = 0x16 | MessageTypeFlags::RESPONSE_TYPE,
|
| 84 |
+
|
| 85 |
+
// Messages to support RRef.backward().
|
| 86 |
+
RREF_BACKWARD_REQ = 0x17 | MessageTypeFlags::REQUEST_TYPE,
|
| 87 |
+
RREF_BACKWARD_RESP = 0x18 | MessageTypeFlags::RESPONSE_TYPE,
|
| 88 |
+
|
| 89 |
+
// Other internal message types
|
| 90 |
+
EXCEPTION = 0x37 | MessageTypeFlags::RESPONSE_TYPE,
|
| 91 |
+
UNKNOWN = 0x3c
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
// A message to be sent/received by an RpcAgent.
|
| 95 |
+
//
|
| 96 |
+
// A Message object contains 4 fields:
|
| 97 |
+
// payload (std::vector<char>): a binary chunk of data.
|
| 98 |
+
// tensors (std::vector<torch::Tensor>): all tensors. Tensor data are not
|
| 99 |
+
// included in the payload, and it is up to the RpcAgent implementation
|
| 100 |
+
// to determine how to serialize them. This design is helpful for
|
| 101 |
+
// communicating super large tensors where serializing all the data at
|
| 102 |
+
// once leads to excessively large memory footprint. An implementation
|
| 103 |
+
// can then serialize and send tensors chunk-by-chunk, in the streaming
|
| 104 |
+
// fashion.
|
| 105 |
+
// type (MessageType): type of the message.
|
| 106 |
+
// id (int64_t): message id, this is used to match request and response.
|
| 107 |
+
// Other implementation can ignore it if they have their own
|
| 108 |
+
// ways to do matching.
|
| 109 |
+
//
|
| 110 |
+
// Layers above ``RpcAgent`` only converts ScriptCall, ScriptResp, PythonCall,
|
| 111 |
+
// and PythonResp into a Message, and it is up to the RpcAgent
|
| 112 |
+
// implementation to determine how to serialize a message.
|
| 113 |
+
class TORCH_API Message final : public torch::CustomClassHolder {
|
| 114 |
+
private:
|
| 115 |
+
// Keep these private in order to force users to go through make_intrusive and
|
| 116 |
+
// thus prevent creating a Message that's not held by an intrusive_ptr.
|
| 117 |
+
Message();
|
| 118 |
+
|
| 119 |
+
Message(
|
| 120 |
+
std::vector<char>&& payload,
|
| 121 |
+
std::vector<torch::Tensor>&& tensors,
|
| 122 |
+
MessageType type);
|
| 123 |
+
|
| 124 |
+
Message(
|
| 125 |
+
std::vector<char>&& payload,
|
| 126 |
+
std::vector<torch::Tensor>&& tensors,
|
| 127 |
+
MessageType type,
|
| 128 |
+
int64_t id);
|
| 129 |
+
|
| 130 |
+
friend c10::intrusive_ptr<Message>;
|
| 131 |
+
|
| 132 |
+
public:
|
| 133 |
+
Message(const Message& other) = delete;
|
| 134 |
+
Message(Message&& other) = delete;
|
| 135 |
+
Message& operator=(Message const& rhs) = delete;
|
| 136 |
+
Message& operator=(Message&& rhs) = delete;
|
| 137 |
+
~Message() override = default;
|
| 138 |
+
|
| 139 |
+
// Destructively retrieves the payload.
|
| 140 |
+
std::vector<char>&& movePayload() &&;
|
| 141 |
+
std::vector<torch::Tensor>&& moveTensors() &&;
|
| 142 |
+
|
| 143 |
+
std::vector<char>& payload();
|
| 144 |
+
const std::vector<char>& payload() const;
|
| 145 |
+
std::vector<torch::Tensor>& tensors();
|
| 146 |
+
const std::vector<torch::Tensor>& tensors() const;
|
| 147 |
+
MessageType type() const;
|
| 148 |
+
|
| 149 |
+
bool isRequest() const;
|
| 150 |
+
bool isResponse() const;
|
| 151 |
+
bool isShutdown() const;
|
| 152 |
+
|
| 153 |
+
// id is an optional field to match request/response. If an RpcAgent
|
| 154 |
+
// implementation is able to do the matching without using this id, it can be
|
| 155 |
+
// dropped during message serialization.
|
| 156 |
+
int64_t id() const;
|
| 157 |
+
void setId(int64_t id);
|
| 158 |
+
|
| 159 |
+
std::vector<c10::weak_intrusive_ptr<c10::StorageImpl>> getStorages() const;
|
| 160 |
+
|
| 161 |
+
private:
|
| 162 |
+
std::vector<char> payload_;
|
| 163 |
+
std::vector<torch::Tensor> tensors_;
|
| 164 |
+
MessageType type_ = MessageType::UNKNOWN;
|
| 165 |
+
int64_t id_ = -1;
|
| 166 |
+
};
|
| 167 |
+
|
| 168 |
+
// Create a response Message of type Exception.
|
| 169 |
+
// The exception string representation will be used as the message's payload.
|
| 170 |
+
// A message ID corresponding to the request that resulted in this response can
|
| 171 |
+
// be provided for matching requests/responses.
|
| 172 |
+
TORCH_API c10::intrusive_ptr<Message> createExceptionResponse(
|
| 173 |
+
const std::exception& e,
|
| 174 |
+
int64_t id);
|
| 175 |
+
|
| 176 |
+
// Create a response Message of type Exception.
|
| 177 |
+
// The passed in string representation will be used as the message's payload.
|
| 178 |
+
// A message ID corresponding to the request that resulted in this response can
|
| 179 |
+
// be provided for matching requests/responses.
|
| 180 |
+
TORCH_API c10::intrusive_ptr<Message> createExceptionResponse(
|
| 181 |
+
const std::string& exceptionStr,
|
| 182 |
+
int64_t id);
|
| 183 |
+
|
| 184 |
+
inline std::tuple<
|
| 185 |
+
c10::intrusive_ptr<Message>,
|
| 186 |
+
std::vector<c10::weak_intrusive_ptr<c10::StorageImpl>>>
|
| 187 |
+
withStorages(c10::intrusive_ptr<Message> message) {
|
| 188 |
+
auto storages = message->getStorages();
|
| 189 |
+
return std::make_tuple(std::move(message), std::move(storages));
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
using JitFuture = c10::ivalue::Future;
|
| 193 |
+
|
| 194 |
+
} // namespace torch::distributed::rpc
|
| 195 |
+
|
| 196 |
+
#else
|
| 197 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 198 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/metrics/RpcMetricsHandler.h
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/util/Registry.h>
|
| 4 |
+
#include <string>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed::rpc {
|
| 7 |
+
// All metrics are prefixed with the following key.
|
| 8 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
|
| 9 |
+
constexpr char kRpcMetricsKeyPrefix[] = "torch.distributed.rpc.";
|
| 10 |
+
// APIs for logging time-series metrics for RPC-based distributed
|
| 11 |
+
// training. Implementations of this class should provide thread safety so that
|
| 12 |
+
// metrics can be logged from multiple threads without the user needing to
|
| 13 |
+
// coordinate serialization.
|
| 14 |
+
class RpcMetricsHandler {
|
| 15 |
+
public:
|
| 16 |
+
// Accumulates the metric value specified by the name for purposes of
|
| 17 |
+
// computing aggregate statistics over time.
|
| 18 |
+
virtual void accumulateMetric(const std::string& name, double value) = 0;
|
| 19 |
+
// Increment a count for the metric given by the name.
|
| 20 |
+
virtual void incrementMetric(const std::string& name) = 0;
|
| 21 |
+
virtual ~RpcMetricsHandler() = default;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
// Configuration struct for metrics handling.
|
| 25 |
+
struct RpcMetricsConfig {
|
| 26 |
+
explicit RpcMetricsConfig(std::string handlerName, bool enabled)
|
| 27 |
+
: handlerName_(std::move(handlerName)), enabled_(enabled) {}
|
| 28 |
+
|
| 29 |
+
// Handler name
|
| 30 |
+
std::string handlerName_;
|
| 31 |
+
// Whether metrics exporting should be enabled or not.
|
| 32 |
+
bool enabled_;
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
// A registry for different implementations of RpcMetricsHandler. Classes
|
| 36 |
+
// implementing the above interface should use this to register implementations.
|
| 37 |
+
TORCH_DECLARE_REGISTRY(
|
| 38 |
+
RpcMetricsHandlerRegistry,
|
| 39 |
+
torch::distributed::rpc::RpcMetricsHandler);
|
| 40 |
+
|
| 41 |
+
} // namespace torch::distributed::rpc
|
| 42 |
+
|
| 43 |
+
#else
|
| 44 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 45 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/profiler/remote_profiler_manager.h
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 5 |
+
#include <mutex>
|
| 6 |
+
#include <optional>
|
| 7 |
+
#include <unordered_map>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
extern const std::string REMOTE_PROFILING_KEY_PREFIX;
|
| 11 |
+
|
| 12 |
+
class TORCH_API RemoteProfilerManager {
|
| 13 |
+
public:
|
| 14 |
+
// Retrieves the lazily-initialized RemoteProfilerManager singleton instance.
|
| 15 |
+
static RemoteProfilerManager& getInstance();
|
| 16 |
+
// Sets the current, thread-local profiling key.
|
| 17 |
+
void setCurrentKey(std::string key);
|
| 18 |
+
// Returns whether the current profiling key is set.
|
| 19 |
+
bool isCurrentKeySet() const;
|
| 20 |
+
// Unsets the current, thread-local profiling key to allow other RPCs to reset
|
| 21 |
+
// it.
|
| 22 |
+
void unsetCurrentKey();
|
| 23 |
+
// inserts a pair (globallyUniqueId, key) to an in-memory map. The
|
| 24 |
+
// corresponding ID is used in RPC deserialization to prefix remotely profiled
|
| 25 |
+
// events with the right key.
|
| 26 |
+
void saveRPCKey(
|
| 27 |
+
ProfilingId globallyUniqueId,
|
| 28 |
+
const std::string& rpcProfilingKey);
|
| 29 |
+
// Retrieves the profiling key corresponding to the given globallyUniqueId.
|
| 30 |
+
// Throws if it is not found.
|
| 31 |
+
std::string retrieveRPCProfilingKey(const ProfilingId& globallyUniqueId);
|
| 32 |
+
// Generates the next globally unique ID for profiling.
|
| 33 |
+
ProfilingId getNextProfilerId();
|
| 34 |
+
// Retrieves the currently set thread-local profiling key. Throws if it is not
|
| 35 |
+
// set.
|
| 36 |
+
std::string& getCurrentProfilingKey();
|
| 37 |
+
// erases the globallyUniqueId from the map. This can help save memory in the
|
| 38 |
+
// case that many RPCs are being profiled.
|
| 39 |
+
void eraseKey(const ProfilingId& globallyUniqueId);
|
| 40 |
+
|
| 41 |
+
RemoteProfilerManager(const RemoteProfilerManager& other) = delete;
|
| 42 |
+
RemoteProfilerManager operator=(const RemoteProfilerManager& other) = delete;
|
| 43 |
+
RemoteProfilerManager(RemoteProfilerManager&&) = delete;
|
| 44 |
+
RemoteProfilerManager& operator=(RemoteProfilerManager&&) = delete;
|
| 45 |
+
|
| 46 |
+
private:
|
| 47 |
+
RemoteProfilerManager();
|
| 48 |
+
~RemoteProfilerManager() = default;
|
| 49 |
+
local_id_t getNextLocalId();
|
| 50 |
+
std::unordered_map<ProfilingId, std::string, ProfilingId::Hash>
|
| 51 |
+
profiledRpcKeys_;
|
| 52 |
+
static thread_local std::optional<std::string> currentThreadLocalKey_;
|
| 53 |
+
std::mutex mutex_;
|
| 54 |
+
local_id_t currentLocalId_;
|
| 55 |
+
};
|
| 56 |
+
} // namespace torch::distributed::rpc
|
| 57 |
+
|
| 58 |
+
#else
|
| 59 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 60 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/profiler/server_process_global_profiler.h
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <shared_mutex>
|
| 5 |
+
#include <utility>
|
| 6 |
+
|
| 7 |
+
#include <torch/csrc/autograd/profiler.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc::profiler::processglobal {
|
| 10 |
+
|
| 11 |
+
using namespace torch::autograd::profiler;
|
| 12 |
+
|
| 13 |
+
// Process global profiler state.
|
| 14 |
+
//
|
| 15 |
+
// This class holds information about a profiling range, from "enable" to
|
| 16 |
+
// "disable".
|
| 17 |
+
// An instance of this ``State`` will be
|
| 18 |
+
// pushed into a global stack, so nested profiling range is supported.
|
| 19 |
+
//
|
| 20 |
+
// It has 2 members.
|
| 21 |
+
// One is ``autograd::profiler::ProfilerConfig``. It's set by user and
|
| 22 |
+
// will be copied to thread-local profiler state of RPC threads.
|
| 23 |
+
// The other is a container that aggregates recorded
|
| 24 |
+
// ``autograd::profiler::Event``s from all thread-local profilers on RPC
|
| 25 |
+
// threads.
|
| 26 |
+
class State {
|
| 27 |
+
public:
|
| 28 |
+
explicit State(ProfilerConfig config) : config_(std::move(config)) {}
|
| 29 |
+
~State() = default;
|
| 30 |
+
|
| 31 |
+
const ProfilerConfig& config() const {
|
| 32 |
+
return config_;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
void pushResult(thread_event_lists result) {
|
| 36 |
+
std::unique_lock<std::mutex> lock(resultsMutex_);
|
| 37 |
+
|
| 38 |
+
// NB: When a thread wants to push an entry into this container,
|
| 39 |
+
// main control logic might have exited the process-global profile range.
|
| 40 |
+
results_.emplace_back(std::move(result));
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
std::vector<thread_event_lists> results();
|
| 44 |
+
|
| 45 |
+
private:
|
| 46 |
+
// Each result comes from a profile range. In each profile range, there is a
|
| 47 |
+
// "__profiler_start" marker event that all following events calculate time
|
| 48 |
+
// relative to it, so it's required to call
|
| 49 |
+
// parse_cpu_trace(result) for results of all profile range.
|
| 50 |
+
std::mutex resultsMutex_;
|
| 51 |
+
std::vector<thread_event_lists> results_;
|
| 52 |
+
const ProfilerConfig config_ = ProfilerConfig(ProfilerState::Disabled);
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
class StateStackEntry;
|
| 56 |
+
|
| 57 |
+
#if defined(__MACH__)
|
| 58 |
+
// Compiler error: 'shared_timed_mutex' is unavailable: introduced in
|
| 59 |
+
// macOS 10.12
|
| 60 |
+
using mutexType = std::mutex;
|
| 61 |
+
// Compiler error: 'shared_lock' is unavailable: introduced in
|
| 62 |
+
// macOS 10.12
|
| 63 |
+
using rLockType = std::unique_lock<std::mutex>;
|
| 64 |
+
using wLockType = std::unique_lock<std::mutex>;
|
| 65 |
+
#else
|
| 66 |
+
using mutexType = std::shared_timed_mutex;
|
| 67 |
+
using rLockType = std::shared_lock<std::shared_timed_mutex>;
|
| 68 |
+
using wLockType = std::unique_lock<std::shared_timed_mutex>;
|
| 69 |
+
#endif
|
| 70 |
+
|
| 71 |
+
// This is the global stack of ``State``s.
|
| 72 |
+
TORCH_API extern std::shared_ptr<StateStackEntry> currentStateStackEntryPtr;
|
| 73 |
+
TORCH_API extern mutexType currentStateStackEntryMutex;
|
| 74 |
+
|
| 75 |
+
// This class is used to implement a stack of ``State``s.
|
| 76 |
+
// It has 2 members.
|
| 77 |
+
// One is `prevPtr`, a shared_ptr pointing to previous element in the
|
| 78 |
+
// stack.
|
| 79 |
+
// The other is ``statePtr``, a shared_ptr pointing to ``State``.
|
| 80 |
+
class StateStackEntry {
|
| 81 |
+
public:
|
| 82 |
+
StateStackEntry(
|
| 83 |
+
std::shared_ptr<StateStackEntry> prevPtr,
|
| 84 |
+
std::shared_ptr<State> statePtr)
|
| 85 |
+
: prevPtr_(std::move(prevPtr)), statePtr_(std::move(statePtr)) {}
|
| 86 |
+
|
| 87 |
+
static void pushRange(std::shared_ptr<State> profilerProcessGlobalStatePtr);
|
| 88 |
+
static std::shared_ptr<State> popRange();
|
| 89 |
+
|
| 90 |
+
static std::shared_ptr<StateStackEntry> current() {
|
| 91 |
+
rLockType rlock(currentStateStackEntryMutex);
|
| 92 |
+
|
| 93 |
+
return currentStateStackEntryPtr;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
std::shared_ptr<StateStackEntry> prevPtr() const {
|
| 97 |
+
return prevPtr_;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
std::shared_ptr<State> statePtr() const {
|
| 101 |
+
return statePtr_;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
private:
|
| 105 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 106 |
+
const std::shared_ptr<StateStackEntry> prevPtr_{nullptr};
|
| 107 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 108 |
+
const std::shared_ptr<State> statePtr_{nullptr};
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
// Push the result to ``State``s of current profile range and recursively outer
|
| 112 |
+
// profile ranges.
|
| 113 |
+
TORCH_API void pushResultRecursive(
|
| 114 |
+
std::shared_ptr<StateStackEntry> stateStackEntryPtr,
|
| 115 |
+
const thread_event_lists& result);
|
| 116 |
+
|
| 117 |
+
// User-facing API.
|
| 118 |
+
//
|
| 119 |
+
// Enter a server-side process-global profiling range.
|
| 120 |
+
// Profiling range can be nested, so it's ok to call this API for multiple
|
| 121 |
+
// times. This enables all RPC threads running server-side request callbacks.
|
| 122 |
+
TORCH_API void enableServer(const ProfilerConfig& new_config);
|
| 123 |
+
//
|
| 124 |
+
// Exit a server-side process-global profiling range.
|
| 125 |
+
// Profiling range can be nested, so it's possible that profiler is still on
|
| 126 |
+
// after calling this API.
|
| 127 |
+
// This enables all RPC threads running server-side request callbacks.
|
| 128 |
+
TORCH_API std::vector<thread_event_lists> disableServer();
|
| 129 |
+
|
| 130 |
+
} // namespace torch::distributed::rpc::profiler::processglobal
|
| 131 |
+
|
| 132 |
+
#else
|
| 133 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 134 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/py_rref.h
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/rref_impl.h>
|
| 5 |
+
#include <torch/csrc/python_headers.h>
|
| 6 |
+
#include <torch/csrc/utils/pybind.h>
|
| 7 |
+
|
| 8 |
+
namespace torch::distributed::rpc {
|
| 9 |
+
|
| 10 |
+
// NOLINTNEXTLINE(performance-enum-size)
|
| 11 |
+
enum RRefProxyType { RPC_SYNC, RPC_ASYNC, REMOTE };
|
| 12 |
+
|
| 13 |
+
// Python wrapper of an RRef shared_ptr that supports Python
|
| 14 |
+
// pickle and unpickle.
|
| 15 |
+
class PYBIND11_EXPORT PyRRef {
|
| 16 |
+
public:
|
| 17 |
+
// The first ctor can only be called while holding GIL. See its implementation
|
| 18 |
+
// for more explanations.
|
| 19 |
+
explicit PyRRef(const py::object& value, const py::object& type_hint);
|
| 20 |
+
explicit PyRRef(c10::intrusive_ptr<RRef> rref);
|
| 21 |
+
PyRRef(const PyRRef&) = default;
|
| 22 |
+
~PyRRef();
|
| 23 |
+
|
| 24 |
+
bool isOwner() const;
|
| 25 |
+
bool confirmedByOwner() const;
|
| 26 |
+
WorkerInfo owner() const;
|
| 27 |
+
std::string ownerName() const;
|
| 28 |
+
py::object toHere(
|
| 29 |
+
const float timeoutSeconds =
|
| 30 |
+
torch::distributed::rpc::kUnsetRpcTimeout) const;
|
| 31 |
+
py::object localValue() const;
|
| 32 |
+
std::string str() const;
|
| 33 |
+
py::tuple pickle() const;
|
| 34 |
+
static PyRRef unpickle(const py::tuple& t);
|
| 35 |
+
c10::IValue toIValue() const;
|
| 36 |
+
// Future that is associated with the creation of this RRef on the remote end.
|
| 37 |
+
// This is only used to get the future corresponding to the rref for profiling
|
| 38 |
+
// use cases.
|
| 39 |
+
c10::intrusive_ptr<JitFuture> getFuture() const;
|
| 40 |
+
// Keeps track of the future responsible for profiling owner creation
|
| 41 |
+
// acknowledgement
|
| 42 |
+
c10::intrusive_ptr<JitFuture> getProfilingFuture() const;
|
| 43 |
+
// Sets the future responsible for profiling owner creation acknowledgement.
|
| 44 |
+
// This future is set from python to be a future that returns when profiling
|
| 45 |
+
// callbacks have been run.
|
| 46 |
+
void setProfilingFuture(c10::intrusive_ptr<JitFuture> profilingFuture);
|
| 47 |
+
|
| 48 |
+
// create a proxy on this RRef, which can be used to launch RPC on the owner
|
| 49 |
+
// of this RRef to run functions on the object referenced by this RRef.
|
| 50 |
+
py::object createRRefProxy(
|
| 51 |
+
const RRefProxyType& mode,
|
| 52 |
+
float timeoutSeconds = rpc::kUnsetRpcTimeout) const;
|
| 53 |
+
|
| 54 |
+
// get the type of the data object referenced by this RRef. Timeout argument
|
| 55 |
+
// is only used in the first invocation of this function as an argument to the
|
| 56 |
+
// RPC to the owner node of the RRef.
|
| 57 |
+
py::object getRRefType(
|
| 58 |
+
float timeout = rpc::kUnsetRpcTimeout,
|
| 59 |
+
bool blocking = true);
|
| 60 |
+
|
| 61 |
+
// Run the backward pass with the RRef as the root.
|
| 62 |
+
void backward(int64_t autogradContextId, bool retainGraph);
|
| 63 |
+
|
| 64 |
+
// Helper static function to run backward on a given rref.
|
| 65 |
+
static void backward(
|
| 66 |
+
int64_t autogradContextId,
|
| 67 |
+
bool retainGraph,
|
| 68 |
+
const c10::intrusive_ptr<RRef>& rref);
|
| 69 |
+
|
| 70 |
+
// Specialization of backward if the rref is an OwnerRRef.
|
| 71 |
+
static void backwardOwnerRRef(
|
| 72 |
+
int64_t autogradContextId,
|
| 73 |
+
bool retainGraph,
|
| 74 |
+
IValue value);
|
| 75 |
+
|
| 76 |
+
private:
|
| 77 |
+
c10::intrusive_ptr<RRef> rref_;
|
| 78 |
+
std::optional<c10::intrusive_ptr<JitFuture>> profilingFuture_;
|
| 79 |
+
std::optional<py::object> type_;
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
} // namespace torch::distributed::rpc
|
| 83 |
+
|
| 84 |
+
#else
|
| 85 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 86 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_call.h
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// RPC call representing calling a Python function over RPC.
|
| 10 |
+
class TORCH_API PythonCall final : public RpcCommandBase {
|
| 11 |
+
public:
|
| 12 |
+
PythonCall(SerializedPyObj&& serializedPyObj, bool isAsyncExecution);
|
| 13 |
+
|
| 14 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 15 |
+
|
| 16 |
+
static std::unique_ptr<PythonCall> fromMessage(const Message& message);
|
| 17 |
+
|
| 18 |
+
const SerializedPyObj& serializedPyObj() const;
|
| 19 |
+
|
| 20 |
+
inline bool isAsyncExecution() const {
|
| 21 |
+
return isAsyncExecution_;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
private:
|
| 25 |
+
SerializedPyObj serializedPyObj_;
|
| 26 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 27 |
+
const bool isAsyncExecution_;
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
} // namespace torch::distributed::rpc
|
| 31 |
+
|
| 32 |
+
#else
|
| 33 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 34 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_functions.h
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/py_rref.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_agent.h>
|
| 6 |
+
#include <torch/csrc/jit/python/pybind_utils.h>
|
| 7 |
+
#include <torch/csrc/utils/pybind.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
|
| 11 |
+
// Converts an internal ivalue::Future of Message into a user-facing
|
| 12 |
+
// ivalue::Future of py::object type by creating a new ivalue::Future and call
|
| 13 |
+
// its markCompleted as a callback in the given ivalue::Future.
|
| 14 |
+
// If hasValue is true, the Message will be converted into a py::object and then
|
| 15 |
+
// wrap it with an IValue. If hasValue is false, this ivalue::Future is only
|
| 16 |
+
// used for signaling and launching callbacks. In this case, the message will be
|
| 17 |
+
// discarded and then set the ivalue::Future using an empty IValue or the given
|
| 18 |
+
// FutureError if there is an error.
|
| 19 |
+
c10::intrusive_ptr<JitFuture> toPyJitFuture(
|
| 20 |
+
const c10::intrusive_ptr<JitFuture>& messageJitFuture,
|
| 21 |
+
bool hasValue = true);
|
| 22 |
+
|
| 23 |
+
c10::intrusive_ptr<JitFuture> pyRpcBuiltin(
|
| 24 |
+
const WorkerInfo& dst,
|
| 25 |
+
const std::string& opName,
|
| 26 |
+
const py::args& args,
|
| 27 |
+
const py::kwargs& kwargs,
|
| 28 |
+
const float rpcTimeoutSeconds);
|
| 29 |
+
|
| 30 |
+
c10::intrusive_ptr<JitFuture> pyRpcPythonUdf(
|
| 31 |
+
const WorkerInfo& dst,
|
| 32 |
+
std::string& pickledPythonUDF,
|
| 33 |
+
std::vector<torch::Tensor>& tensors,
|
| 34 |
+
const float rpcTimeoutSeconds,
|
| 35 |
+
const bool isAsyncExecution);
|
| 36 |
+
|
| 37 |
+
c10::intrusive_ptr<JitFuture> pyRpcTorchscript(
|
| 38 |
+
const std::string& dstWorkerName,
|
| 39 |
+
const std::string& qualifiedNameStr,
|
| 40 |
+
const py::tuple& argsTuple,
|
| 41 |
+
const py::dict& kwargsDict,
|
| 42 |
+
const float rpcTimeoutSeconds,
|
| 43 |
+
const bool isAsyncExecution);
|
| 44 |
+
|
| 45 |
+
PyRRef pyRemoteBuiltin(
|
| 46 |
+
const WorkerInfo& dst,
|
| 47 |
+
const std::string& opName,
|
| 48 |
+
const float rpcTimeoutSeconds,
|
| 49 |
+
const py::args& args,
|
| 50 |
+
const py::kwargs& kwargs);
|
| 51 |
+
|
| 52 |
+
PyRRef pyRemotePythonUdf(
|
| 53 |
+
const WorkerInfo& dst,
|
| 54 |
+
std::string& pickledPythonUDF,
|
| 55 |
+
std::vector<torch::Tensor>& tensors,
|
| 56 |
+
const float rpcTimeoutSeconds,
|
| 57 |
+
const bool isAsyncExecution);
|
| 58 |
+
|
| 59 |
+
PyRRef pyRemoteTorchscript(
|
| 60 |
+
const std::string& dstWorkerName,
|
| 61 |
+
const std::string& qualifiedNameStr,
|
| 62 |
+
const float rpcTimeoutSeconds,
|
| 63 |
+
const bool isAsyncExecution,
|
| 64 |
+
const py::args& args,
|
| 65 |
+
const py::kwargs& kwargs);
|
| 66 |
+
|
| 67 |
+
} // namespace torch::distributed::rpc
|
| 68 |
+
|
| 69 |
+
#else
|
| 70 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 71 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_remote_call.h
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
class TORCH_API PythonRemoteCall : public RpcCommandBase {
|
| 10 |
+
public:
|
| 11 |
+
PythonRemoteCall(
|
| 12 |
+
SerializedPyObj&& serializedPyObj,
|
| 13 |
+
at::IValue retRRefId,
|
| 14 |
+
at::IValue retForkId,
|
| 15 |
+
const bool isAsyncExecution);
|
| 16 |
+
|
| 17 |
+
inline const SerializedPyObj& serializedPyObj() const {
|
| 18 |
+
return serializedPyObj_;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
inline const at::IValue& retRRefId() const {
|
| 22 |
+
return retRRefId_;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
inline const at::IValue& retForkId() const {
|
| 26 |
+
return retForkId_;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
inline bool isAsyncExecution() const {
|
| 30 |
+
return isAsyncExecution_;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 34 |
+
static std::unique_ptr<PythonRemoteCall> fromMessage(const Message& message);
|
| 35 |
+
|
| 36 |
+
private:
|
| 37 |
+
SerializedPyObj serializedPyObj_;
|
| 38 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 39 |
+
const at::IValue retRRefId_;
|
| 40 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 41 |
+
const at::IValue retForkId_;
|
| 42 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 43 |
+
const bool isAsyncExecution_;
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
} // namespace torch::distributed::rpc
|
| 47 |
+
|
| 48 |
+
#else
|
| 49 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 50 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_resp.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// RPC call representing the response of a Python UDF over RPC.
|
| 10 |
+
class TORCH_API PythonResp final : public RpcCommandBase {
|
| 11 |
+
public:
|
| 12 |
+
explicit PythonResp(SerializedPyObj&& serializedPyObj);
|
| 13 |
+
|
| 14 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 15 |
+
|
| 16 |
+
static std::unique_ptr<PythonResp> fromMessage(const Message& message);
|
| 17 |
+
|
| 18 |
+
const SerializedPyObj& serializedPyObj() const;
|
| 19 |
+
|
| 20 |
+
private:
|
| 21 |
+
SerializedPyObj serializedPyObj_;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} // namespace torch::distributed::rpc
|
| 25 |
+
|
| 26 |
+
#else
|
| 27 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 28 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/python_rpc_handler.h
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 6 |
+
#include <torch/csrc/jit/frontend/script_type_parser.h>
|
| 7 |
+
#include <torch/csrc/utils/pybind.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
|
| 11 |
+
// Singleton class provides interface to execute python UDF remote call
|
| 12 |
+
// and deserialize the returned results by running python function
|
| 13 |
+
// in internal_rpc_utilities.
|
| 14 |
+
// The singleton object is constructed at first when RPC agent is
|
| 15 |
+
// constructed, where the python function in
|
| 16 |
+
// torch/distributed/internal_rpc_utils.py are imported only once.
|
| 17 |
+
class PYBIND11_EXPORT PythonRpcHandler {
|
| 18 |
+
public:
|
| 19 |
+
struct RRefProxyFunctions {
|
| 20 |
+
py::object rrefProxyCtor_;
|
| 21 |
+
py::object rpcSync_;
|
| 22 |
+
py::object rpcAsync_;
|
| 23 |
+
py::object remote_;
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
struct RRefTypeFunctions {
|
| 27 |
+
py::object onOwner_;
|
| 28 |
+
py::object onUser_;
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
static PythonRpcHandler& getInstance();
|
| 32 |
+
|
| 33 |
+
// Run a pickled Python UDF and return the result py::object
|
| 34 |
+
py::object runPythonUdf(const py::object& pythonUdf);
|
| 35 |
+
|
| 36 |
+
// Serialized a py::object into a string
|
| 37 |
+
SerializedPyObj serialize(const py::object& obj);
|
| 38 |
+
|
| 39 |
+
// Deserialize a string into a py::object
|
| 40 |
+
py::object deserialize(const SerializedPyObj& serializedObj);
|
| 41 |
+
|
| 42 |
+
// Check if obj is RemoteException, then throw it
|
| 43 |
+
void handleException(const py::object& obj);
|
| 44 |
+
// Alternative if the caller is already holding the GIL.
|
| 45 |
+
void handleExceptionGILHeld(const py::object& obj);
|
| 46 |
+
// Check if obj is an RemoteException instance.
|
| 47 |
+
bool isRemoteException(const py::object& obj);
|
| 48 |
+
|
| 49 |
+
// Explicitly clean up py::objects to avoid segment faults when
|
| 50 |
+
// py::objects with CPython are cleaned up later at program exit
|
| 51 |
+
// See similar issues reported https://github.com/pybind/pybind11/issues/1598
|
| 52 |
+
// and https://github.com/pybind/pybind11/issues/1493
|
| 53 |
+
// Our local tests also caught this segment faults if py::objects are cleaned
|
| 54 |
+
// up at program exit. The explanation is: CPython cleans up most critical
|
| 55 |
+
// utilities before cleaning up PythonRpcHandler singleton, so when
|
| 56 |
+
// PythonRpcHandler singleton cleans up py::objects and call dec_ref(), it
|
| 57 |
+
// will crash.
|
| 58 |
+
// The solution is to clean up py::objects earlier when Rpc agent join().
|
| 59 |
+
// Note that py::objects cannot be cleaned up when Rpc agent is destroyed
|
| 60 |
+
// as well, as Rpc agent is global variable and it will have same issue as
|
| 61 |
+
// PythonRpcHandler.
|
| 62 |
+
void cleanup();
|
| 63 |
+
|
| 64 |
+
std::shared_ptr<torch::jit::CompilationUnit> jitCompilationUnit();
|
| 65 |
+
|
| 66 |
+
// Parse the string to recover the jit_type, this is used for RRef python
|
| 67 |
+
// pickling/unpickling type recovery. The type string inference rule is as
|
| 68 |
+
// follows:
|
| 69 |
+
// 1. first try to parse if this is primitive types.
|
| 70 |
+
// i.e. TensorType, IntType, PyObjectType, etc.
|
| 71 |
+
// 2. if not primitive type, we query the python_cu to see if it is a
|
| 72 |
+
// class type or interface type registered in python
|
| 73 |
+
// We use a ScriptTypeParser instance with custom PythonTypeResolver
|
| 74 |
+
// to resolve types according to the above rules.
|
| 75 |
+
TypePtr parseTypeFromStr(const std::string& typeStr);
|
| 76 |
+
|
| 77 |
+
// Return a set of Python functions for RRef helpers.
|
| 78 |
+
const RRefProxyFunctions& getRRefProxyFunctions() const;
|
| 79 |
+
|
| 80 |
+
// Return a set of Python functions to retrieve the type of the object
|
| 81 |
+
// referenced by a given RRef.
|
| 82 |
+
const RRefTypeFunctions& getRRefTypeFunctions() const;
|
| 83 |
+
|
| 84 |
+
PythonRpcHandler(const PythonRpcHandler&) = delete;
|
| 85 |
+
PythonRpcHandler& operator=(const PythonRpcHandler&) = delete;
|
| 86 |
+
PythonRpcHandler(PythonRpcHandler&&) = delete;
|
| 87 |
+
PythonRpcHandler& operator=(PythonRpcHandler&&) = delete;
|
| 88 |
+
|
| 89 |
+
private:
|
| 90 |
+
void init();
|
| 91 |
+
PythonRpcHandler();
|
| 92 |
+
~PythonRpcHandler() = default;
|
| 93 |
+
|
| 94 |
+
// Ref to `torch.distributed.rpc.internal._run_function`.
|
| 95 |
+
py::object pyRunFunction_;
|
| 96 |
+
|
| 97 |
+
// Ref to `torch.distributed.rpc.internal.serialize`.
|
| 98 |
+
py::object pySerialize_;
|
| 99 |
+
|
| 100 |
+
// Ref to `torch.distributed.rpc.internal.deserialize`.
|
| 101 |
+
py::object pyDeserialize_;
|
| 102 |
+
|
| 103 |
+
// Ref to 'torch.distributed.rpc.internal._handle_exception'
|
| 104 |
+
py::object pyHandleException_;
|
| 105 |
+
|
| 106 |
+
// Python functions for RRef proxy
|
| 107 |
+
RRefProxyFunctions rrefProxyFunctions_;
|
| 108 |
+
|
| 109 |
+
// Ref to 'torch.distributed.rpc.api._rref_typeof_on_'
|
| 110 |
+
RRefTypeFunctions rrefTypeFunctions_;
|
| 111 |
+
|
| 112 |
+
// Shared ptr to python compilation unit in jit, it is constructed in python
|
| 113 |
+
// side (see _python_cu = torch._C.CompilationUnit() in jit/__init__.py)
|
| 114 |
+
// and imported in C++ (see get_python_cu() in
|
| 115 |
+
// csrc/jit/python/pybind_utils.h). We import the compilation unit here only
|
| 116 |
+
// once for less cost and thread safety.
|
| 117 |
+
std::shared_ptr<torch::jit::CompilationUnit> jitCompilationUnit_;
|
| 118 |
+
|
| 119 |
+
// jit type parser to parse type_str back to TypePtr for RRef type
|
| 120 |
+
// recovery when pickling and unpickling RRef
|
| 121 |
+
std::shared_ptr<jit::ScriptTypeParser> typeParser_;
|
| 122 |
+
|
| 123 |
+
// Indicates whether or not we have properly initialized the handler.
|
| 124 |
+
bool initialized_{false};
|
| 125 |
+
|
| 126 |
+
// Lock to protect initialization.
|
| 127 |
+
std::mutex init_lock_;
|
| 128 |
+
};
|
| 129 |
+
|
| 130 |
+
} // namespace torch::distributed::rpc
|
| 131 |
+
|
| 132 |
+
#else
|
| 133 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 134 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback.h
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed::rpc {
|
| 7 |
+
|
| 8 |
+
// Functor which is invoked to process an RPC message. This is an abstract class
|
| 9 |
+
// with some common functionality across all request handlers. Users need to
|
| 10 |
+
// implement this interface to perform the actual business logic.
|
| 11 |
+
class TORCH_API RequestCallback {
|
| 12 |
+
public:
|
| 13 |
+
// Invoke the callback.
|
| 14 |
+
c10::intrusive_ptr<JitFuture> operator()(
|
| 15 |
+
Message& request,
|
| 16 |
+
std::vector<c10::Stream> streams) const;
|
| 17 |
+
|
| 18 |
+
virtual ~RequestCallback() = default;
|
| 19 |
+
|
| 20 |
+
protected:
|
| 21 |
+
// RpcAgent implementation should invoke ``RequestCallback`` to process
|
| 22 |
+
// received requests. There is no restriction on the implementation's
|
| 23 |
+
// threading model. This function takes an rvalue reference of the Message
|
| 24 |
+
// object. It is expected to return the future to a response message or
|
| 25 |
+
// message containing an exception. Different rpc agent implementations are
|
| 26 |
+
// expected to ensure delivery of the response/exception based on their
|
| 27 |
+
// implementation specific mechanisms.
|
| 28 |
+
virtual c10::intrusive_ptr<JitFuture> processMessage(
|
| 29 |
+
Message& request,
|
| 30 |
+
std::vector<c10::Stream> streams) const = 0;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
} // namespace torch::distributed::rpc
|
| 34 |
+
|
| 35 |
+
#else
|
| 36 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 37 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback_impl.h
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/request_callback_no_python.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 7 |
+
#include <torch/csrc/jit/python/pybind.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
|
| 11 |
+
class TORCH_API RequestCallbackImpl : public RequestCallbackNoPython {
|
| 12 |
+
public:
|
| 13 |
+
std::unique_ptr<RpcCommandBase> deserializePythonRpcCommand(
|
| 14 |
+
std::unique_ptr<RpcCommandBase> rpc,
|
| 15 |
+
const MessageType& messageType) const override;
|
| 16 |
+
|
| 17 |
+
c10::intrusive_ptr<JitFuture> processPythonCall(
|
| 18 |
+
RpcCommandBase& rpc,
|
| 19 |
+
const std::vector<c10::Stream>& streams) const override;
|
| 20 |
+
|
| 21 |
+
c10::intrusive_ptr<JitFuture> processScriptCall(
|
| 22 |
+
RpcCommandBase& rpc,
|
| 23 |
+
const std::vector<c10::Stream>& streams) const override;
|
| 24 |
+
|
| 25 |
+
c10::intrusive_ptr<JitFuture> processScriptRemoteCall(
|
| 26 |
+
RpcCommandBase& rpc,
|
| 27 |
+
const std::vector<c10::Stream>& streams) const override;
|
| 28 |
+
|
| 29 |
+
c10::intrusive_ptr<JitFuture> processPythonRemoteCall(
|
| 30 |
+
RpcCommandBase& rpc,
|
| 31 |
+
const std::vector<c10::Stream>& streams) const override;
|
| 32 |
+
|
| 33 |
+
c10::intrusive_ptr<JitFuture> processPythonRRefFetchCall(
|
| 34 |
+
RpcCommandBase& rpc) const override;
|
| 35 |
+
|
| 36 |
+
void handleRRefDelete(c10::intrusive_ptr<RRef>& rref) const override;
|
| 37 |
+
|
| 38 |
+
c10::intrusive_ptr<JitFuture> processRpcWithErrors(
|
| 39 |
+
RpcCommandBase& rpc,
|
| 40 |
+
const MessageType& messageType,
|
| 41 |
+
const std::vector<c10::Stream>& streams) const override;
|
| 42 |
+
|
| 43 |
+
bool cudaAvailable() const override;
|
| 44 |
+
|
| 45 |
+
c10::intrusive_ptr<JitFuture> processRRefBackward(
|
| 46 |
+
RpcCommandBase& rpc) const override;
|
| 47 |
+
|
| 48 |
+
// Helpers to run user-defined functions, operators and other computations.
|
| 49 |
+
|
| 50 |
+
c10::intrusive_ptr<JitFuture> runJitFunction(
|
| 51 |
+
const c10::QualifiedName& name,
|
| 52 |
+
std::vector<at::IValue>& stack,
|
| 53 |
+
const std::vector<c10::Stream>& streams,
|
| 54 |
+
bool isAsyncExecution) const;
|
| 55 |
+
|
| 56 |
+
c10::intrusive_ptr<JitFuture> runPythonFunction(
|
| 57 |
+
const py::object& function,
|
| 58 |
+
const std::vector<c10::Stream>& streams,
|
| 59 |
+
bool isAsyncExecution) const;
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
} // namespace torch::distributed::rpc
|
| 63 |
+
|
| 64 |
+
#else
|
| 65 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 66 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/request_callback_no_python.h
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/request_callback.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 7 |
+
#include <torch/csrc/distributed/rpc/rref_impl.h>
|
| 8 |
+
#include <torch/csrc/distributed/rpc/script_call.h>
|
| 9 |
+
#include <torch/csrc/distributed/rpc/script_remote_call.h>
|
| 10 |
+
|
| 11 |
+
namespace torch::distributed::rpc {
|
| 12 |
+
|
| 13 |
+
// RequestCallback implementation with no Python dependencies.
|
| 14 |
+
class TORCH_API RequestCallbackNoPython : public RequestCallback {
|
| 15 |
+
public:
|
| 16 |
+
c10::intrusive_ptr<JitFuture> processMessage(
|
| 17 |
+
Message& request,
|
| 18 |
+
std::vector<c10::Stream> streams) const override;
|
| 19 |
+
|
| 20 |
+
protected:
|
| 21 |
+
virtual std::unique_ptr<RpcCommandBase> deserializePythonRpcCommand(
|
| 22 |
+
std::unique_ptr<RpcCommandBase> rpc,
|
| 23 |
+
const MessageType& messageType) const;
|
| 24 |
+
|
| 25 |
+
virtual c10::intrusive_ptr<JitFuture> processScriptCall(
|
| 26 |
+
RpcCommandBase& rpc,
|
| 27 |
+
const std::vector<c10::Stream>& streams) const;
|
| 28 |
+
|
| 29 |
+
virtual c10::intrusive_ptr<JitFuture> processPythonCall(
|
| 30 |
+
RpcCommandBase& rpc,
|
| 31 |
+
const std::vector<c10::Stream>& streams) const;
|
| 32 |
+
|
| 33 |
+
c10::intrusive_ptr<JitFuture> assignOwnerRRef(
|
| 34 |
+
const RRefId& rrefId,
|
| 35 |
+
const RRefId& forkId,
|
| 36 |
+
const c10::intrusive_ptr<JitFuture>& valueFuture) const;
|
| 37 |
+
|
| 38 |
+
virtual c10::intrusive_ptr<JitFuture> processScriptRemoteCall(
|
| 39 |
+
RpcCommandBase& rpc,
|
| 40 |
+
const std::vector<c10::Stream>& streams) const;
|
| 41 |
+
|
| 42 |
+
virtual c10::intrusive_ptr<JitFuture> processPythonRemoteCall(
|
| 43 |
+
RpcCommandBase& rpc,
|
| 44 |
+
const std::vector<c10::Stream>& streams) const;
|
| 45 |
+
|
| 46 |
+
c10::intrusive_ptr<JitFuture> retrieveOwnerRRef(const RRefId& rrefId) const;
|
| 47 |
+
|
| 48 |
+
c10::intrusive_ptr<JitFuture> processScriptRRefFetchCall(
|
| 49 |
+
RpcCommandBase& rpc) const;
|
| 50 |
+
|
| 51 |
+
virtual c10::intrusive_ptr<JitFuture> processPythonRRefFetchCall(
|
| 52 |
+
RpcCommandBase& rpc) const;
|
| 53 |
+
|
| 54 |
+
c10::intrusive_ptr<JitFuture> processRRefUserDelete(
|
| 55 |
+
RpcCommandBase& rpc) const;
|
| 56 |
+
|
| 57 |
+
c10::intrusive_ptr<JitFuture> processRRefChildAccept(
|
| 58 |
+
RpcCommandBase& rpc) const;
|
| 59 |
+
|
| 60 |
+
c10::intrusive_ptr<JitFuture> processRRefForkRequest(
|
| 61 |
+
RpcCommandBase& rpc) const;
|
| 62 |
+
|
| 63 |
+
c10::intrusive_ptr<JitFuture> processForwardAutogradReq(
|
| 64 |
+
RpcCommandBase& rpc,
|
| 65 |
+
const std::vector<c10::Stream>& streams) const;
|
| 66 |
+
|
| 67 |
+
c10::intrusive_ptr<JitFuture> processBackwardAutogradReq(
|
| 68 |
+
RpcCommandBase& rpc,
|
| 69 |
+
const std::vector<c10::Stream>& streams) const;
|
| 70 |
+
|
| 71 |
+
c10::intrusive_ptr<JitFuture> processCleanupAutogradContextReq(
|
| 72 |
+
RpcCommandBase& rpc) const;
|
| 73 |
+
|
| 74 |
+
c10::intrusive_ptr<JitFuture> processRunWithProfilingReq(
|
| 75 |
+
RpcCommandBase& rpc) const;
|
| 76 |
+
|
| 77 |
+
virtual void handleRRefDelete(c10::intrusive_ptr<RRef>& rref) const;
|
| 78 |
+
|
| 79 |
+
c10::intrusive_ptr<JitFuture> processRpc(
|
| 80 |
+
RpcCommandBase& rpc,
|
| 81 |
+
const MessageType& messageType,
|
| 82 |
+
const std::vector<c10::Stream>& streams) const;
|
| 83 |
+
|
| 84 |
+
virtual c10::intrusive_ptr<JitFuture> processRpcWithErrors(
|
| 85 |
+
RpcCommandBase& rpc,
|
| 86 |
+
const MessageType& messageType,
|
| 87 |
+
const std::vector<c10::Stream>& streams) const;
|
| 88 |
+
|
| 89 |
+
c10::intrusive_ptr<Message> handleError(
|
| 90 |
+
const std::exception& e,
|
| 91 |
+
const MessageType messageType,
|
| 92 |
+
int64_t messageId) const;
|
| 93 |
+
|
| 94 |
+
virtual bool cudaAvailable() const;
|
| 95 |
+
|
| 96 |
+
virtual c10::intrusive_ptr<JitFuture> processRRefBackward(
|
| 97 |
+
RpcCommandBase& rpc) const;
|
| 98 |
+
|
| 99 |
+
// Helpers to run user-defined functions, operators and other computations.
|
| 100 |
+
|
| 101 |
+
c10::intrusive_ptr<JitFuture> runJitOperator(
|
| 102 |
+
const jit::Operator& op,
|
| 103 |
+
std::vector<at::IValue>& stack,
|
| 104 |
+
const std::vector<c10::Stream>& streams) const;
|
| 105 |
+
|
| 106 |
+
// Helpers to convert various kinds of objects into already-completed futures.
|
| 107 |
+
|
| 108 |
+
c10::intrusive_ptr<JitFuture> asFuture(IValue value, TypePtr type) const;
|
| 109 |
+
|
| 110 |
+
c10::intrusive_ptr<JitFuture> asFuture(
|
| 111 |
+
c10::intrusive_ptr<Message> message) const;
|
| 112 |
+
|
| 113 |
+
c10::intrusive_ptr<JitFuture> asFuture(std::exception_ptr err) const;
|
| 114 |
+
};
|
| 115 |
+
|
| 116 |
+
} // namespace torch::distributed::rpc
|
| 117 |
+
|
| 118 |
+
#else
|
| 119 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 120 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/python_headers.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed::rpc {
|
| 7 |
+
|
| 8 |
+
PyMethodDef* python_functions();
|
| 9 |
+
|
| 10 |
+
} // namespace torch::distributed::rpc
|
| 11 |
+
|
| 12 |
+
#else
|
| 13 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 14 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc_agent.h
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/request_callback.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 7 |
+
|
| 8 |
+
#include <cctype>
|
| 9 |
+
#include <chrono>
|
| 10 |
+
#include <condition_variable>
|
| 11 |
+
#include <mutex>
|
| 12 |
+
#include <thread>
|
| 13 |
+
|
| 14 |
+
#if defined(__cpp_lib_atomic_shared_ptr) && \
|
| 15 |
+
__cpp_lib_atomic_shared_ptr >= 201711L
|
| 16 |
+
#define TORCH_RPC_HAS_ATOMIC_SHARED_PTR 1
|
| 17 |
+
#endif
|
| 18 |
+
|
| 19 |
+
namespace torch::distributed::rpc {
|
| 20 |
+
|
| 21 |
+
using DeviceMap = std::unordered_map<c10::Device, c10::Device>;
|
| 22 |
+
|
| 23 |
+
// Default RPC timeout
|
| 24 |
+
constexpr float kDefaultRpcTimeoutSeconds = 60;
|
| 25 |
+
// Unset RPC timeout. This is the value agent::send() will have if user does not
|
| 26 |
+
// pass in a specific timeout, and indicates that we must use the default
|
| 27 |
+
// timeout for RPCs.
|
| 28 |
+
constexpr float kUnsetRpcTimeout = -1;
|
| 29 |
+
constexpr auto kDefaultInitMethod = "env://";
|
| 30 |
+
constexpr float kSecToMsConversion = 1000;
|
| 31 |
+
constexpr auto kRpcTimeoutErrorStr =
|
| 32 |
+
"RPC ran for more than set timeout ({} ms) and will now be marked with an error";
|
| 33 |
+
constexpr auto kDefaultNumWorkerThreads = 16;
|
| 34 |
+
|
| 35 |
+
using steady_clock_time_point =
|
| 36 |
+
std::chrono::time_point<std::chrono::steady_clock>;
|
| 37 |
+
// Input is qualified name string, output is JIT StrongTypePtr
|
| 38 |
+
// Same as jit::TypeResolver, did not import jit::TypeResolver to here
|
| 39 |
+
// because it could introduce cyclic dependencies.
|
| 40 |
+
using TypeResolver =
|
| 41 |
+
std::function<c10::StrongTypePtr(const c10::QualifiedName&)>;
|
| 42 |
+
|
| 43 |
+
struct TORCH_API RpcBackendOptions {
|
| 44 |
+
RpcBackendOptions()
|
| 45 |
+
: RpcBackendOptions(kDefaultRpcTimeoutSeconds, kDefaultInitMethod) {}
|
| 46 |
+
|
| 47 |
+
RpcBackendOptions(float rpcTimeoutSeconds, std::string initMethod)
|
| 48 |
+
: rpcTimeoutSeconds(rpcTimeoutSeconds),
|
| 49 |
+
initMethod(std::move(initMethod)) {
|
| 50 |
+
TORCH_CHECK(rpcTimeoutSeconds >= 0, "RPC Timeout must be non-negative");
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
float rpcTimeoutSeconds;
|
| 54 |
+
std::string initMethod;
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
// A globally unique ID to identify an RpcAgent
|
| 58 |
+
struct TORCH_API WorkerInfo : torch::CustomClassHolder {
|
| 59 |
+
WorkerInfo(std::string name, int64_t id);
|
| 60 |
+
|
| 61 |
+
WorkerInfo(std::string name, worker_id_t id);
|
| 62 |
+
|
| 63 |
+
bool operator==(const WorkerInfo& rhs) {
|
| 64 |
+
return (id_ == rhs.id_) && (name_ == rhs.name_);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
static constexpr size_t MAX_NAME_LEN = 128;
|
| 68 |
+
|
| 69 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 70 |
+
const std::string name_;
|
| 71 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 72 |
+
const worker_id_t id_;
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
struct TORCH_API RegisterWorkerInfoOnce {
|
| 76 |
+
RegisterWorkerInfoOnce();
|
| 77 |
+
};
|
| 78 |
+
|
| 79 |
+
TORCH_API std::ostream& operator<<(
|
| 80 |
+
std::ostream& os,
|
| 81 |
+
const WorkerInfo& workerInfo);
|
| 82 |
+
|
| 83 |
+
// Struct for options to configure the RPC Retry protocol.
|
| 84 |
+
struct TORCH_API RpcRetryOptions {
|
| 85 |
+
// Using a default constructor like all other Options structs in the RPC
|
| 86 |
+
// codebase. TORCH_CHECKs for input validation are done in the
|
| 87 |
+
// sendWithRetries function.
|
| 88 |
+
RpcRetryOptions() = default;
|
| 89 |
+
// Maximum number of times we will retry the RPC
|
| 90 |
+
int maxRetries{5};
|
| 91 |
+
// Initial duration between consecutive RPC send attempts
|
| 92 |
+
std::chrono::milliseconds rpcRetryDuration{std::chrono::milliseconds(1000)};
|
| 93 |
+
// Constant for exponential backoff used while calculating future wait
|
| 94 |
+
// durations
|
| 95 |
+
float retryBackoff{1.5};
|
| 96 |
+
};
|
| 97 |
+
|
| 98 |
+
// Struct that stores all the metadata needed to retry a given RPC.
|
| 99 |
+
struct TORCH_API RpcRetryInfo {
|
| 100 |
+
RpcRetryInfo(
|
| 101 |
+
const WorkerInfo& to,
|
| 102 |
+
c10::intrusive_ptr<Message> message,
|
| 103 |
+
c10::intrusive_ptr<JitFuture> originalFuture,
|
| 104 |
+
int retryCount,
|
| 105 |
+
RpcRetryOptions options)
|
| 106 |
+
: to_(to),
|
| 107 |
+
message_(std::move(message)),
|
| 108 |
+
originalFuture_(std::move(originalFuture)),
|
| 109 |
+
retryCount_(retryCount),
|
| 110 |
+
options_(options) {}
|
| 111 |
+
|
| 112 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 113 |
+
const WorkerInfo& to_;
|
| 114 |
+
c10::intrusive_ptr<Message> message_;
|
| 115 |
+
// Future that is returned to the caller of sendWithRetries().
|
| 116 |
+
c10::intrusive_ptr<JitFuture> originalFuture_;
|
| 117 |
+
// Number of send attempts completed so far.
|
| 118 |
+
int retryCount_;
|
| 119 |
+
RpcRetryOptions options_;
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
// ``RpcAgent`` is the base class for sending and receiving RPC messages. It
|
| 123 |
+
// provides a unified ``send`` API for both request and response messages, and
|
| 124 |
+
// will invoke the given ``RequestCallback`` to process received requests. It
|
| 125 |
+
// should immediately become ready to serve request and accept response after
|
| 126 |
+
// construction.
|
| 127 |
+
class TORCH_API RpcAgent {
|
| 128 |
+
public:
|
| 129 |
+
// `WorkerInfo` is the globally unique identifier for this RpcAgent instance.
|
| 130 |
+
// It contains a ``name_`` field and an ``id_`` field. ``name_`` is the
|
| 131 |
+
// globally unique name for this ``RpcAgent``. It is up to the ``RpcAgent``
|
| 132 |
+
// implementation to determine how to resolve names. ``id_`` is the globally
|
| 133 |
+
// unique ID for this ``RpcAgent``. This should be determined by the
|
| 134 |
+
// ``RpcAgent`` implementation.
|
| 135 |
+
// The ``RequestCallback`` will be invoked to handle received requests. This
|
| 136 |
+
// ``RpcAgent`` base class makes no assumption on the thread-safeness of the
|
| 137 |
+
// ``RequestCallback``. ``RpcAgent`` implementations need to make sure that
|
| 138 |
+
// its threading model conform to ``RequestCallback``'s requirement.
|
| 139 |
+
// NB: RpcAgent implementations should not start serving requests until
|
| 140 |
+
// ``start()`` is called, as there could be other contexts that have not been
|
| 141 |
+
// initialized yet at this time.
|
| 142 |
+
RpcAgent(
|
| 143 |
+
WorkerInfo id,
|
| 144 |
+
std::unique_ptr<RequestCallback> cb,
|
| 145 |
+
std::chrono::milliseconds rpcTimeout);
|
| 146 |
+
|
| 147 |
+
virtual ~RpcAgent();
|
| 148 |
+
|
| 149 |
+
// Send a message to the ``RpcAgent`` of id ``to`` and returns a
|
| 150 |
+
// ``JitFuture`` ptr. The implementation must be asynchronous, i.e., it
|
| 151 |
+
// cannot block until it receives the response.
|
| 152 |
+
//
|
| 153 |
+
// If ``message.isRequest()`` is true, the ``JitFuture`` will be
|
| 154 |
+
// completed when the response arrives. For other message types, the Future
|
| 155 |
+
// should be ignored by the caller.
|
| 156 |
+
virtual c10::intrusive_ptr<JitFuture> send(
|
| 157 |
+
const WorkerInfo& to,
|
| 158 |
+
c10::intrusive_ptr<Message> message,
|
| 159 |
+
const float rpcTimeoutSeconds = kUnsetRpcTimeout,
|
| 160 |
+
const DeviceMap& deviceMap = {}) = 0;
|
| 161 |
+
|
| 162 |
+
// Retries sending the message up to maxRetries times until an ACK is
|
| 163 |
+
// received. The duration between consecutive sends is increased over
|
| 164 |
+
// time using an exponential backoff algorithm.
|
| 165 |
+
//
|
| 166 |
+
// Sends ``message`` to the ``RpcAgent`` of id ``to`` and returns a
|
| 167 |
+
// ``JitFuture`` ptr, just like send(). Caller can specify the maximum
|
| 168 |
+
// number of retries for this RPC (default is 5), initial duration between
|
| 169 |
+
// sends (default is 1000ms), and backoff constant (default is 1.5) by
|
| 170 |
+
// passing in the RpcRetryOptions struct. This API might end up
|
| 171 |
+
// executing a method twice on the remote end (it does not guarantee
|
| 172 |
+
// exactly-once semantics). Therefore, the user must ensure their requests
|
| 173 |
+
// are idempotent.
|
| 174 |
+
c10::intrusive_ptr<JitFuture> sendWithRetries(
|
| 175 |
+
const WorkerInfo& to,
|
| 176 |
+
c10::intrusive_ptr<Message> message,
|
| 177 |
+
RpcRetryOptions retryOptions = RpcRetryOptions());
|
| 178 |
+
|
| 179 |
+
// Return a reference to the ``WorkerInfo`` of this RpcAgent.
|
| 180 |
+
// NB: not using ``std::optional<const std::string&>`` here because we might
|
| 181 |
+
// need to create a separate RPC API lib and avoid forcing all ``RpcAgent``
|
| 182 |
+
// implementations to depend on libtorch.
|
| 183 |
+
const WorkerInfo& getWorkerInfo() const;
|
| 184 |
+
|
| 185 |
+
// Return a reference to the ``WorkerInfo`` of the given ``workerName``.
|
| 186 |
+
virtual const WorkerInfo& getWorkerInfo(
|
| 187 |
+
const std::string& workerName) const = 0;
|
| 188 |
+
|
| 189 |
+
virtual const WorkerInfo& getWorkerInfo(worker_id_t id) const = 0;
|
| 190 |
+
|
| 191 |
+
virtual std::vector<WorkerInfo> getWorkerInfos() const = 0;
|
| 192 |
+
|
| 193 |
+
// Retrieve the timeout for all RPCs.
|
| 194 |
+
inline std::chrono::milliseconds getRpcTimeout() const {
|
| 195 |
+
return rpcTimeout_.load();
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
// Set the timeout for all RPCs
|
| 199 |
+
inline void setRpcTimeout(const std::chrono::milliseconds& rpcTimeout) {
|
| 200 |
+
rpcTimeout_.store(rpcTimeout);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
// Call sync and join all internal threads. This method should be called
|
| 204 |
+
// before every RPC process exits.
|
| 205 |
+
virtual void join(bool shutdown = false, float timeout = 0) = 0;
|
| 206 |
+
|
| 207 |
+
// Synchronize this process with other ``RpcAgent`` processes. Block until
|
| 208 |
+
// all ``RpcAgent``s reach this method and send all pending messages.
|
| 209 |
+
virtual void sync() = 0;
|
| 210 |
+
|
| 211 |
+
// Sets up backend-agnostic state for accepting requests. Currently, this
|
| 212 |
+
// entails setting rpcAgentRunning_ to true, creating the retry thread, and
|
| 213 |
+
// calling the backend's startImpl.
|
| 214 |
+
void start();
|
| 215 |
+
|
| 216 |
+
// Derived classes must override this function to start accepting requests.
|
| 217 |
+
// This is used to initialize any backend-specific state. Users must call
|
| 218 |
+
// start, not startImpl, to initialize the RPC Agent.
|
| 219 |
+
virtual void startImpl() = 0;
|
| 220 |
+
|
| 221 |
+
// Stop accepting requests and shutdown the RPC framework as soon as possible
|
| 222 |
+
// by terminating all RPC threads.
|
| 223 |
+
void shutdown();
|
| 224 |
+
|
| 225 |
+
// Derived classes must override this function to start accepting requests.
|
| 226 |
+
// This is used to clean up any backend-specific state. Users must call
|
| 227 |
+
// shutdown, not shutdownImpl, to shutdown the RPC Agent.
|
| 228 |
+
virtual void shutdownImpl() = 0;
|
| 229 |
+
|
| 230 |
+
// Check if current RPC agent is set.
|
| 231 |
+
static bool isCurrentRpcAgentSet();
|
| 232 |
+
|
| 233 |
+
// Retrieve the valid current RPC agent.
|
| 234 |
+
static std::shared_ptr<RpcAgent> getCurrentRpcAgent();
|
| 235 |
+
|
| 236 |
+
// Set the current RPC agent.
|
| 237 |
+
static void setCurrentRpcAgent(std::shared_ptr<RpcAgent> rpcAgent);
|
| 238 |
+
|
| 239 |
+
// Retrieve metrics as KV map
|
| 240 |
+
virtual std::unordered_map<std::string, std::string> getMetrics() = 0;
|
| 241 |
+
|
| 242 |
+
// Retrieve debug info in addition to metrics as KV map
|
| 243 |
+
virtual std::unordered_map<std::string, std::string> getDebugInfo();
|
| 244 |
+
|
| 245 |
+
// Flag to control whether GIL wait times
|
| 246 |
+
// should be profiled or not.
|
| 247 |
+
void enableGILProfiling(bool flag);
|
| 248 |
+
|
| 249 |
+
// Retrieve whether we should profile GIL wait times or not.
|
| 250 |
+
bool isGILProfilingEnabled();
|
| 251 |
+
|
| 252 |
+
// Set type resolver that will be passed to JIT pickler to resolver type Ptr
|
| 253 |
+
// based on type str.
|
| 254 |
+
void setTypeResolver(std::shared_ptr<TypeResolver> typeResolver);
|
| 255 |
+
|
| 256 |
+
// Get the type resolver
|
| 257 |
+
std::shared_ptr<TypeResolver> getTypeResolver();
|
| 258 |
+
|
| 259 |
+
// Retrieves the device map for the provided destination worker.
|
| 260 |
+
virtual DeviceMap getDeviceMap(const WorkerInfo& dst) const;
|
| 261 |
+
|
| 262 |
+
// Retrieve the (non-CPU) devices that are supported by the agent.
|
| 263 |
+
virtual const std::vector<c10::Device>& getDevices() const;
|
| 264 |
+
|
| 265 |
+
protected:
|
| 266 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 267 |
+
const WorkerInfo workerInfo_;
|
| 268 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 269 |
+
const std::unique_ptr<RequestCallback> cb_;
|
| 270 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 271 |
+
std::atomic<std::chrono::milliseconds> rpcTimeout_;
|
| 272 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 273 |
+
std::atomic<bool> profilingEnabled_;
|
| 274 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 275 |
+
std::shared_ptr<TypeResolver> typeResolver_;
|
| 276 |
+
// Atomic boolean indicating whether this agent is running. It controls
|
| 277 |
+
// whether several background threads should be running. It is set in
|
| 278 |
+
// RpcAgent::start() and unset in the derived class shutdown().
|
| 279 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 280 |
+
std::atomic<bool> rpcAgentRunning_;
|
| 281 |
+
|
| 282 |
+
private:
|
| 283 |
+
#if defined(TORCH_RPC_HAS_ATOMIC_SHARED_PTR)
|
| 284 |
+
static std::atomic<std::shared_ptr<RpcAgent>> currentRpcAgent_;
|
| 285 |
+
#else
|
| 286 |
+
static std::shared_ptr<RpcAgent> currentRpcAgent_;
|
| 287 |
+
#endif
|
| 288 |
+
// Add GIL wait time data point to metrics
|
| 289 |
+
virtual void addGilWaitTime(const std::chrono::microseconds gilWaitTime) = 0;
|
| 290 |
+
friend class PythonRpcHandler;
|
| 291 |
+
|
| 292 |
+
// Map that stores metadata for RPC's that may need to be re-tried as well as
|
| 293 |
+
// the timepoint at which we should re-try them.
|
| 294 |
+
std::map<
|
| 295 |
+
steady_clock_time_point,
|
| 296 |
+
std::unordered_set<std::shared_ptr<RpcRetryInfo>>>
|
| 297 |
+
rpcRetryMap_;
|
| 298 |
+
|
| 299 |
+
// Thread that checks for retryable RPC's in the rpcRetryMap_ and sleeps until
|
| 300 |
+
// the next unACKed RPC's timeout has expired.
|
| 301 |
+
std::thread rpcRetryThread_;
|
| 302 |
+
|
| 303 |
+
// Function that rpcRetryThread_ calls in a loop as long as RpcAgent is
|
| 304 |
+
// running.
|
| 305 |
+
void retryExpiredRpcs();
|
| 306 |
+
|
| 307 |
+
// This is the callback attached to futures corresponding to send retries.
|
| 308 |
+
// This handles 3 cases: 1). send was completed, 2). send failed with an
|
| 309 |
+
// error and we've done maxRetries failed send attempts, and 3). send
|
| 310 |
+
// failed with an error and we have more retries to go. In case 1, we mark
|
| 311 |
+
// the original future as complete. In case 2, we mark the future with an
|
| 312 |
+
// error and do not retry again. In case 3, we move the RpcRetryInfo struct
|
| 313 |
+
// to another time point in the map to schedule the RPC for a future send.
|
| 314 |
+
void rpcRetryCallback(
|
| 315 |
+
JitFuture& message,
|
| 316 |
+
steady_clock_time_point newTime,
|
| 317 |
+
std::shared_ptr<RpcRetryInfo> earliestRpc);
|
| 318 |
+
|
| 319 |
+
// Function that uses the exponential backoff algorithm to compute the next
|
| 320 |
+
// time point to retry a given RPC.
|
| 321 |
+
inline steady_clock_time_point computeNewRpcRetryTime(
|
| 322 |
+
RpcRetryOptions& options,
|
| 323 |
+
int retryCount) {
|
| 324 |
+
// The exponential backoff algorithm being used here is:
|
| 325 |
+
// newTime = timeNow + (retryDuration * (backoffConstant ^ retryCount)).
|
| 326 |
+
std::chrono::milliseconds timedelta =
|
| 327 |
+
std::chrono::duration_cast<std::chrono::milliseconds>(
|
| 328 |
+
options.rpcRetryDuration * pow(options.retryBackoff, retryCount));
|
| 329 |
+
return std::chrono::time_point_cast<std::chrono::milliseconds>(
|
| 330 |
+
std::chrono::steady_clock::now() + timedelta);
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// Condition Variable to signal when the rpcRetryMap_ has been populated.
|
| 334 |
+
std::condition_variable rpcRetryMapCV_;
|
| 335 |
+
|
| 336 |
+
// Mutex to protect RpcRetryMap_.
|
| 337 |
+
std::mutex rpcRetryMutex_;
|
| 338 |
+
};
|
| 339 |
+
|
| 340 |
+
} // namespace torch::distributed::rpc
|
| 341 |
+
|
| 342 |
+
namespace std {
|
| 343 |
+
template <>
|
| 344 |
+
struct hash<torch::distributed::rpc::WorkerInfo> {
|
| 345 |
+
std::size_t operator()(
|
| 346 |
+
const torch::distributed::rpc::WorkerInfo& worker_info) const noexcept {
|
| 347 |
+
return worker_info.id_;
|
| 348 |
+
}
|
| 349 |
+
};
|
| 350 |
+
} // namespace std
|
| 351 |
+
|
| 352 |
+
#else
|
| 353 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 354 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rpc_command_base.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// Base class for all RPC request and responses.
|
| 10 |
+
class RpcCommandBase {
|
| 11 |
+
public:
|
| 12 |
+
// Need to override this to serialize the RPC. This should destructively
|
| 13 |
+
// create a message for the RPC (Hence the &&).
|
| 14 |
+
c10::intrusive_ptr<Message> toMessage() && {
|
| 15 |
+
JitRRefPickleGuard jitPickleGuard;
|
| 16 |
+
return std::move(*this).toMessageImpl();
|
| 17 |
+
}
|
| 18 |
+
virtual c10::intrusive_ptr<Message> toMessageImpl() && = 0;
|
| 19 |
+
virtual ~RpcCommandBase() = 0;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
inline RpcCommandBase::~RpcCommandBase() = default;
|
| 23 |
+
|
| 24 |
+
} // namespace torch::distributed::rpc
|
| 25 |
+
|
| 26 |
+
#else
|
| 27 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 28 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_context.h
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_agent.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/rref_impl.h>
|
| 7 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 8 |
+
#include <torch/csrc/distributed/rpc/utils.h>
|
| 9 |
+
|
| 10 |
+
#include <atomic>
|
| 11 |
+
#include <optional>
|
| 12 |
+
|
| 13 |
+
namespace torch::distributed::rpc {
|
| 14 |
+
|
| 15 |
+
namespace callback {
|
| 16 |
+
// It's the callback for RemoteCall.
|
| 17 |
+
void TORCH_API
|
| 18 |
+
confirmPendingUser(const JitFuture& jitFuture, const ForkId& expectedForkId);
|
| 19 |
+
|
| 20 |
+
// It's the callback for finishing creating owner rref, it returned deletedRRef,
|
| 21 |
+
// so that the deletedRRef can be handled under GIL in python_functions.cpp if
|
| 22 |
+
// deletedRRef contains python object.
|
| 23 |
+
c10::intrusive_ptr<RRef> TORCH_API
|
| 24 |
+
finishCreatingOwnerRRef(const JitFuture& jitFuture, const RRefId& rrefId);
|
| 25 |
+
} // namespace callback
|
| 26 |
+
|
| 27 |
+
// Manages RRef lifetime and keeps track of RRef forks.
|
| 28 |
+
class TORCH_API RRefContext {
|
| 29 |
+
public:
|
| 30 |
+
static RRefContext& getInstance();
|
| 31 |
+
// NB: This method must be called before destructing RRefContext singleton.
|
| 32 |
+
// Similar to delForkOfOwner, this method returns a vector of OwnerRRefs that
|
| 33 |
+
// hold py::object. The call-site is also responsible for resetting those
|
| 34 |
+
// shared_ptr objects with a GIL. See comments at delForkOfOwner() for more
|
| 35 |
+
// details.
|
| 36 |
+
static std::vector<c10::intrusive_ptr<RRef>> destroyInstance(
|
| 37 |
+
bool ignoreRRefLeak = true);
|
| 38 |
+
|
| 39 |
+
static void handleException(const JitFuture& jitFuture);
|
| 40 |
+
|
| 41 |
+
// handle exception without throw ::c10::Error again
|
| 42 |
+
static void handleExceptionSilent(const JitFuture& jitFuture);
|
| 43 |
+
|
| 44 |
+
RRefContext(const RRefContext&) = delete;
|
| 45 |
+
RRefContext(RRefContext&& other) = delete;
|
| 46 |
+
void operator=(const RRefContext&) = delete;
|
| 47 |
+
RRefContext& operator=(RRefContext&& other) = delete;
|
| 48 |
+
|
| 49 |
+
~RRefContext();
|
| 50 |
+
|
| 51 |
+
// get the worker id of the current worker
|
| 52 |
+
inline worker_id_t getWorkerId() const {
|
| 53 |
+
return agent_->getWorkerInfo().id_;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// get the worker name of the current worker
|
| 57 |
+
inline const std::string& getWorkerName() const {
|
| 58 |
+
return agent_->getWorkerInfo().name_;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// generate a globally unique ID
|
| 62 |
+
inline GloballyUniqueId genGloballyUniqueId() {
|
| 63 |
+
return GloballyUniqueId(getWorkerId(), nextLocalId_++);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
inline const std::shared_ptr<RpcAgent>& agent() const {
|
| 67 |
+
return agent_;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// create a ``UserRRef`` owned by the worker ``ownerId``
|
| 71 |
+
c10::intrusive_ptr<UserRRef> createUserRRef(
|
| 72 |
+
worker_id_t ownerId,
|
| 73 |
+
const TypePtr& type);
|
| 74 |
+
|
| 75 |
+
// Convert an RRefForkData into an RRef. This RRef could be user or owner.
|
| 76 |
+
// This RRef could have already existed before, or could be created in this
|
| 77 |
+
// method, we pass type here to validate or help the rref creation.
|
| 78 |
+
c10::intrusive_ptr<RRef> getOrCreateRRef(
|
| 79 |
+
const RRefForkData& rfd,
|
| 80 |
+
const TypePtr& type);
|
| 81 |
+
|
| 82 |
+
// Get the ``OwnerRRef`` of id ``rrefId``. If it does not exist, create a new
|
| 83 |
+
// one. This function is called in two places:
|
| 84 |
+
// 1. when processing ``rpc.remote()``, i.e., ``SCRIPT_REMOTE_CALL``
|
| 85 |
+
// ``PYTHON_REMOTE_CALL``.
|
| 86 |
+
// 2. when unpickling ``OwnerRRef``.
|
| 87 |
+
// What's common in these two cases are, 1) the RRefId is already generated
|
| 88 |
+
// 2) the TypePtr is presented. So it can always create the ``OwnerRRef`` if
|
| 89 |
+
// it is not yet available.
|
| 90 |
+
c10::intrusive_ptr<OwnerRRef> getOrCreateOwnerRRef(
|
| 91 |
+
const RRefId& rrefId,
|
| 92 |
+
const TypePtr& type);
|
| 93 |
+
|
| 94 |
+
// Create an empty owner rref of type.
|
| 95 |
+
// This method is called to first time generate an ``OwnerRRef``, e.g.,
|
| 96 |
+
// 1) ``rpc.RRef(obj)``
|
| 97 |
+
// 2) create the ``OwnerRRef`` on `rpc.remote()` caller side.
|
| 98 |
+
// What's common in these two cases are, 1) the RRefId hasn't been generated
|
| 99 |
+
// 2) the TypePtr is presented.
|
| 100 |
+
c10::intrusive_ptr<OwnerRRef> createOwnerRRef(const TypePtr& type);
|
| 101 |
+
|
| 102 |
+
// Returns a Future of the OwnerRRef, which will be marked completed when
|
| 103 |
+
// ``OwnerRRef`` is created. This method is used when the TypePtr is not
|
| 104 |
+
// available, e.g., when processing to_here(). The forceCreated flag can be
|
| 105 |
+
// used to ensure that the rref is created on the owner, otherwise throw in
|
| 106 |
+
// cases where the user of this API expects this to return a completed future.
|
| 107 |
+
// Note that the return value is an intrusive_ptr to a c10::ivalue::Future
|
| 108 |
+
// that holds the RRef.
|
| 109 |
+
c10::intrusive_ptr<JitFuture> getOwnerRRef(
|
| 110 |
+
const RRefId& rrefId,
|
| 111 |
+
bool forceCreated = false);
|
| 112 |
+
|
| 113 |
+
// Adding the RRefId of an OwnerRRef into the forks_ map. This is useful when
|
| 114 |
+
// making a remote call to self, which as for now, still goes through serde
|
| 115 |
+
// and invokes request callback. In this case, the OwnerRRef has already been
|
| 116 |
+
// created on the send side, and we need to pass it to the receive side,
|
| 117 |
+
// instead of creating a new OwnerRRef. This is done by adding the OwnerRRef
|
| 118 |
+
// into owners_. However, that alone is not enough, as it could be deleted
|
| 119 |
+
// when all UserRRef die, which would then remove the OwnerRRef from owners_
|
| 120 |
+
// and this could happen before the self remote call finishes. To prevent
|
| 121 |
+
// that, this API adds the RRefId as a ForkId, which will then delete the
|
| 122 |
+
// ForkId when the self remote is done.
|
| 123 |
+
void addSelfAsFork(c10::intrusive_ptr<OwnerRRef>& rref);
|
| 124 |
+
|
| 125 |
+
// Register a fork of the ``OwnerRRef``, and inserts an intrusive_ptr of the
|
| 126 |
+
// ``OwnerRRef`` in a map to keep it alive.
|
| 127 |
+
void addForkOfOwner(const RRefId& rrefId, const ForkId& forkId);
|
| 128 |
+
// Performs the same function as addForkOfOwner but ignores duplicate
|
| 129 |
+
// requests. This idempotent function is used with RREF_FORK_REQUEST calls,
|
| 130 |
+
// whereas all other message types use the non-idempotent variant.
|
| 131 |
+
void addForkOfOwnerIfNotPresent(const RRefId& rrefId, const ForkId& forkId);
|
| 132 |
+
// Delete a fork of the ``OwnerRRef``. NB: this could trigger deletion on the
|
| 133 |
+
// IValue or py::object. For the later, this method will acquire GIL.
|
| 134 |
+
// NB: If this fork deletion triggered deleting OwnerRRef, this method will
|
| 135 |
+
// return a shared_ptr to the OwnerRRef, which is likely to be the last
|
| 136 |
+
// shared_ptr instance for it. Therefore, deleting this shared_ptr<OwnerRRef>
|
| 137 |
+
// will also trigger deleting the object it points to. If OwnerRRef holds a
|
| 138 |
+
// py::object, deleting it require GIL. The call site should guarded it with
|
| 139 |
+
// a GIL and reset the shared_ptr. The GIL-guarded deletion is intentionally
|
| 140 |
+
// left out of this function to avoid creating dependency on pybind.
|
| 141 |
+
c10::intrusive_ptr<RRef> delForkOfOwner(
|
| 142 |
+
const RRefId& rrefId,
|
| 143 |
+
const ForkId& forkId);
|
| 144 |
+
|
| 145 |
+
// Invoked when pickling an RRef to setup child/fork properly
|
| 146 |
+
RRefForkData prepareChildFork(const c10::intrusive_ptr<RRef>& rref);
|
| 147 |
+
// Invoked when unpickling an RRef to send RREF_FORK_REQUEST to owner and
|
| 148 |
+
// send RREF_CHILD_ACCEPT to the parent.
|
| 149 |
+
// NB: forkId is necessary here as the rref could be an OwnerRRef
|
| 150 |
+
void notifyOwnerAndParentOfFork(
|
| 151 |
+
const ForkId& forkId,
|
| 152 |
+
worker_id_t parent,
|
| 153 |
+
const c10::intrusive_ptr<RRef>& rref);
|
| 154 |
+
|
| 155 |
+
// When a UserRRef is forked to another worker (user or owner), it is added
|
| 156 |
+
// into pendingChildren_ to be held alive until it receives RREF_CHILD_ACCEPT
|
| 157 |
+
// from the child.
|
| 158 |
+
// NB: This is necessary for both user and owner child. As we do not have FIFO
|
| 159 |
+
// communication between workers, we need this strategy to make sure that all
|
| 160 |
+
// previously submitted rpc/remote calls are acked before sending out the
|
| 161 |
+
// RREF_USER_DELETE message. Otherwise, the OwnerRRef could be deleted too
|
| 162 |
+
// soon.
|
| 163 |
+
void addPendingChild(
|
| 164 |
+
const ForkId& forkId,
|
| 165 |
+
const c10::intrusive_ptr<RRef>& rref);
|
| 166 |
+
void delPendingChild(const ForkId& forkId);
|
| 167 |
+
|
| 168 |
+
// When a UserRRef is created, it is added into pendingUsers_ to be held alive
|
| 169 |
+
// until it receives RREF_USER_ACCEPT from the owner.
|
| 170 |
+
void addPendingUser(
|
| 171 |
+
const ForkId& forkId,
|
| 172 |
+
const c10::intrusive_ptr<RRef>& rref);
|
| 173 |
+
void delPendingUser(const ForkId& forkId);
|
| 174 |
+
void addConfirmedUser(
|
| 175 |
+
const ForkId& forkId,
|
| 176 |
+
const c10::intrusive_ptr<RRef>& rref);
|
| 177 |
+
|
| 178 |
+
// Retrieve a pending user given the fork ID. Throws if the user has already
|
| 179 |
+
// been confirmed (i.e. is no longer in the pendingUsers_ map).
|
| 180 |
+
c10::intrusive_ptr<RRef> getPendingUser(const ForkId& forkId);
|
| 181 |
+
|
| 182 |
+
// Start recording new pending UserRRefs. All pending UserRRefs introduced
|
| 183 |
+
// after this point will be put into the thread_local userTable_, which will
|
| 184 |
+
// then be consumed and cleared in waitForThreadLocalPendingRRefs().
|
| 185 |
+
void recordThreadLocalPendingRRefs();
|
| 186 |
+
// End recording new pending UserRRefs, and clear the thread_local userTable_.
|
| 187 |
+
// Returns a Future which will be marked as completed when all pending
|
| 188 |
+
// UserRRefs in the current userTable_ are confirmed by their owners. The bool
|
| 189 |
+
// value in the Future is unused.
|
| 190 |
+
// This method is useful to make sure RRefs in user function arguments are
|
| 191 |
+
// confirmed before launching user code.
|
| 192 |
+
// NB: Callers of this method does not need to keep the returned Future alive,
|
| 193 |
+
// because this Future is already captured in callbacks of the
|
| 194 |
+
// PendingUserState. If there is no pending UserRRefs, this method returns a
|
| 195 |
+
// completed future.
|
| 196 |
+
c10::intrusive_ptr<JitFuture> waitForThreadLocalPendingRRefs();
|
| 197 |
+
// Only call this function when there are errors during a recording session,
|
| 198 |
+
// and it is likely that waitForThreadLocalPendingRRefs() cannot be invoked
|
| 199 |
+
// properly.
|
| 200 |
+
// TODO: make this a context guard
|
| 201 |
+
void clearRecordedPendingRRefsOnError();
|
| 202 |
+
|
| 203 |
+
void delUser(
|
| 204 |
+
const worker_id_t owner,
|
| 205 |
+
const RRefId& rrefId,
|
| 206 |
+
const ForkId& forkId);
|
| 207 |
+
void delAllUsersAndUnforkedOwners(std::chrono::milliseconds timeoutMillis);
|
| 208 |
+
|
| 209 |
+
std::unordered_map<std::string, std::string> getDebugInfo();
|
| 210 |
+
|
| 211 |
+
private:
|
| 212 |
+
struct PendingUserState {
|
| 213 |
+
PendingUserState(c10::intrusive_ptr<RRef> rref)
|
| 214 |
+
: rref_(std::move(rref)),
|
| 215 |
+
confirmationFuture_(c10::make_intrusive<JitFuture>(BoolType::get())) {
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
inline void confirm() {
|
| 219 |
+
c10::static_intrusive_pointer_cast<UserRRef>(rref_)->confirm();
|
| 220 |
+
confirmationFuture_->markCompleted();
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
c10::intrusive_ptr<RRef> rref_;
|
| 224 |
+
// Use Future.wait() and Future.markCompleted() to block and unblock user
|
| 225 |
+
// functions. The bool value wrapped by the future_ is not used.
|
| 226 |
+
c10::intrusive_ptr<JitFuture> confirmationFuture_;
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
RRefContext(std::shared_ptr<RpcAgent> /*agent*/);
|
| 230 |
+
|
| 231 |
+
c10::intrusive_ptr<UserRRef> createUserRRef(
|
| 232 |
+
worker_id_t ownerId,
|
| 233 |
+
const RRefId& rrefId,
|
| 234 |
+
const ForkId& forkId,
|
| 235 |
+
const TypePtr& type);
|
| 236 |
+
|
| 237 |
+
void finishForkRequest(const ForkId& forkId, worker_id_t parent);
|
| 238 |
+
|
| 239 |
+
// If there is any leak on any RRef, this method will throw an error.
|
| 240 |
+
void checkRRefLeaks(bool ignoreRRefLeak);
|
| 241 |
+
|
| 242 |
+
static std::atomic<local_id_t> nextLocalId_;
|
| 243 |
+
|
| 244 |
+
const std::shared_ptr<RpcAgent> agent_;
|
| 245 |
+
mutable std::mutex mutex_;
|
| 246 |
+
// Keep OwnerRRefs alive until there is no living UserRRefs.
|
| 247 |
+
std::unordered_map<RRefId, c10::intrusive_ptr<RRef>, RRefId::Hash> owners_;
|
| 248 |
+
// A map to track OwnerRRefs that are requested but not yet created. This can
|
| 249 |
+
// happen if the to_here() message is processed on the owner before the
|
| 250 |
+
// corresponding creator rpc.remote() message. If this happens, instead of
|
| 251 |
+
// to_here() RPC thread to block waiting for the OwnerRRef creation, the
|
| 252 |
+
// RRefContext returns a Future, so that the RPC request processing logic can
|
| 253 |
+
// attach subsequent code as a callback to that Future.
|
| 254 |
+
// NB: the OwnerRRefs in this map must be cleared when the corresponding
|
| 255 |
+
// OwnerRRef is created. Note that the values in this map are intrusive_ptrs
|
| 256 |
+
// to c10::ivalue::Future that will be marked completed with the owner RRef.
|
| 257 |
+
std::unordered_map<RRefId, c10::intrusive_ptr<JitFuture>, RRefId::Hash>
|
| 258 |
+
pendingOwners_;
|
| 259 |
+
// Tracks known living UserRRefs of an OwnerRRef
|
| 260 |
+
std::unordered_map<
|
| 261 |
+
RRefId,
|
| 262 |
+
std::unordered_set<ForkId, ForkId::Hash>,
|
| 263 |
+
RRefId::Hash>
|
| 264 |
+
forks_;
|
| 265 |
+
|
| 266 |
+
// This cond var is used by deleteAllUsers(), a event notification is sent if
|
| 267 |
+
// number of pending UserRRef or UserRRef children is reduced, or
|
| 268 |
+
// number of owned OwnerRRef is reduced.
|
| 269 |
+
std::condition_variable deleteAllUsersCV_;
|
| 270 |
+
// The follow 3 maps keep UserRRefs alive by holding an intrusive_ptr to the
|
| 271 |
+
// RRef instances. A UserRRef must be added into this map if any of the
|
| 272 |
+
// following two conditions is true:
|
| 273 |
+
//
|
| 274 |
+
// (1) A UserRRef has not been accepted by owner yet.
|
| 275 |
+
//
|
| 276 |
+
// It can be used or shared, but cannot be deleted, and hence kept alive
|
| 277 |
+
// in this map. A message of type RREF_USER_ACCEPT will move the
|
| 278 |
+
// corresponding RRef from pendingUsers_ map to confirmedUsers_ map.
|
| 279 |
+
std::unordered_map<ForkId, std::shared_ptr<PendingUserState>, ForkId::Hash>
|
| 280 |
+
pendingUsers_;
|
| 281 |
+
// UserRRefs are added into this map when it is confirmed by the owner.
|
| 282 |
+
// When destroying RRefContext this map helps to find local UserRRefs
|
| 283 |
+
// and send delete messages if they are still not deleted by Python
|
| 284 |
+
// garbage collection.
|
| 285 |
+
std::unordered_map<ForkId, c10::weak_intrusive_ptr<RRef>, ForkId::Hash>
|
| 286 |
+
confirmedUsers_;
|
| 287 |
+
|
| 288 |
+
// (2) A UserRRef has forked a child UserRRef which has not been accepted by
|
| 289 |
+
// the owner yet.
|
| 290 |
+
//
|
| 291 |
+
// In this case, this UserRRef cannot send out RREF_USER_DELETE message,
|
| 292 |
+
// as it could potentially trigger the OwnerRRef been deleted before the
|
| 293 |
+
// owner learns about the forked child.
|
| 294 |
+
std::unordered_map<ForkId, c10::intrusive_ptr<RRef>, ForkId::Hash>
|
| 295 |
+
pendingChildren_;
|
| 296 |
+
|
| 297 |
+
// The RRef context performs its operations through async RPC requests, in
|
| 298 |
+
// order to not block the user code. Therefore the RRef context's state may be
|
| 299 |
+
// lagging a bit behind what it is intended to be, while it waits for these
|
| 300 |
+
// requests to complete. To allow syncing when needed, we store the count of
|
| 301 |
+
// these pending requests, so that users can wait for it to reach zero.
|
| 302 |
+
std::atomic<int64_t> numPendingFutures_{0};
|
| 303 |
+
|
| 304 |
+
std::mutex destroyedMutex_;
|
| 305 |
+
bool destroyed_{false};
|
| 306 |
+
|
| 307 |
+
// Thread local states to keep UserRRefs deserialized from user function
|
| 308 |
+
// arguments.
|
| 309 |
+
static thread_local std::vector<std::shared_ptr<PendingUserState>> userTable_;
|
| 310 |
+
// A flag indicating whether subsequently created UserRRefs should be added to
|
| 311 |
+
// the thread_local userTable_. The flag is set to true before serializing
|
| 312 |
+
// RPC arguments and then set to false before running the corresponding
|
| 313 |
+
// user code. See addPendingUser and delPendingUser for more details.
|
| 314 |
+
// NB: The reason for having this flag is because addPendingUser are called in
|
| 315 |
+
// two cases, and we only want to track the 2nd case.
|
| 316 |
+
// (1) RRef as the return value: when calling rpc.remote, the UserRRef on the
|
| 317 |
+
// caller side is added to the context using addPendingUser.
|
| 318 |
+
// (2) RRef as an argument: When running an RPC using RRefs as arguments, the
|
| 319 |
+
// RRef is forwarded to the callee as new UserRRefs (if the callee is not
|
| 320 |
+
// the owner). In this case, we block running the user function until all
|
| 321 |
+
// UserRRefs are confirmed by the owner.
|
| 322 |
+
// This contract guarantees that no UserRRefs can be used remotely without
|
| 323 |
+
// confirmation. Note that, however, the UserRRef created by rpc.remote can
|
| 324 |
+
// still be passed to local functions as arguments and used there. This is by
|
| 325 |
+
// design, because this feature is especially useful when, say a master node
|
| 326 |
+
// creates multiple UserRRefs in a loop and then shares them with other nodes.
|
| 327 |
+
// Blocking every iteration in the loop until RRefs are confirmed will slow
|
| 328 |
+
// this down. This nuance on UserRRef can be interpreted as we only make
|
| 329 |
+
// exceptions for UserRRef creators. And using the UserRRef on its creator
|
| 330 |
+
// without confirmation is OK, because the creator would either call to_here
|
| 331 |
+
// or forward the UserRRef, and both would then require confirmations from the
|
| 332 |
+
// owner.
|
| 333 |
+
static thread_local bool recording_;
|
| 334 |
+
};
|
| 335 |
+
|
| 336 |
+
} // namespace torch::distributed::rpc
|
| 337 |
+
|
| 338 |
+
#else
|
| 339 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 340 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_impl.h
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/jit_type.h>
|
| 5 |
+
#include <ATen/core/rref_interface.h>
|
| 6 |
+
#include <c10/core/Event.h>
|
| 7 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 8 |
+
#include <torch/csrc/distributed/rpc/rpc_agent.h>
|
| 9 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 10 |
+
#include <optional>
|
| 11 |
+
|
| 12 |
+
#include <atomic>
|
| 13 |
+
|
| 14 |
+
namespace torch::distributed::rpc {
|
| 15 |
+
|
| 16 |
+
class RRef;
|
| 17 |
+
class RRefContext;
|
| 18 |
+
class UserRRef;
|
| 19 |
+
|
| 20 |
+
constexpr int OWNER_IDX = 0; // index of ownerId in the tuple
|
| 21 |
+
constexpr int RREFID_ON_IDX = 1; // index of RRefId.createdOn_ in the tuple
|
| 22 |
+
constexpr int RREFID_ID_IDX = 2; // index of RRefId.localId_ in the tuple
|
| 23 |
+
constexpr int FORKID_ON_IDX = 3; // index of ForkId.createdOn_ in the tuple
|
| 24 |
+
constexpr int FORKID_ID_IDX = 4; // index of ForkId.localId_ in the tuple
|
| 25 |
+
constexpr int PARENT_IDX = 5; // index of parent in the tuple
|
| 26 |
+
constexpr int TYPE_IDX = 6; // index of parent in the tuple
|
| 27 |
+
|
| 28 |
+
// NB: if more fields are added, make sure this field is also bumped
|
| 29 |
+
constexpr int RFD_TUPLE_SIZE = 7; // number of RRefForkData fields in py::tuple
|
| 30 |
+
|
| 31 |
+
// Represents fork of an RRef to be sent over the wire.
|
| 32 |
+
struct TORCH_API RRefForkData {
|
| 33 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 34 |
+
const worker_id_t ownerId_;
|
| 35 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 36 |
+
const RRefId rrefId_;
|
| 37 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 38 |
+
const ForkId forkId_;
|
| 39 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 40 |
+
const worker_id_t parent_;
|
| 41 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 42 |
+
const std::string typeStr_;
|
| 43 |
+
|
| 44 |
+
RRefForkData(
|
| 45 |
+
worker_id_t ownerId,
|
| 46 |
+
const RRefId& rrefId,
|
| 47 |
+
const ForkId& forkId,
|
| 48 |
+
worker_id_t parent,
|
| 49 |
+
std::string typeStr);
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
// Note [RRef Protocol]
|
| 53 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 54 |
+
//
|
| 55 |
+
// [Background]
|
| 56 |
+
//
|
| 57 |
+
// RRef stands for Remote REFerence. Each RRef is owned by a single worker
|
| 58 |
+
// (i.e., owner) and can be used by multiple users. The owner stores the real
|
| 59 |
+
// data referenced by its RRefs. RRef needs to support fast and scalable RPC.
|
| 60 |
+
// Hence, in the design, we avoid using a single global master to keep RRef
|
| 61 |
+
// states, instead owners will keep track of the global reference counts
|
| 62 |
+
// for its RRefs. Every RRef can be uniquely identified by a global RRefId,
|
| 63 |
+
// which is assigned at the time it is first created either on a user or on the
|
| 64 |
+
// owner.
|
| 65 |
+
//
|
| 66 |
+
// On the owner worker, there is only one OwnerRRef instance, which contains the
|
| 67 |
+
// real data, while on user workers, there can be as many UserRRefs as
|
| 68 |
+
// necessary, and UserRRef does not hold the data. All usage on the OwnerRRef
|
| 69 |
+
// should retrieve the unique OwnerRRef instance using the globally unique
|
| 70 |
+
// RRefId. //A UserRRef will be created when it is used as an argument or return
|
| 71 |
+
// value in dist.rpc or dist.remote call, but RRef forking and reference
|
| 72 |
+
// counting (RC) are completely transparent to applications. Every UserRRef will
|
| 73 |
+
// also have its globally unique ForkId.
|
| 74 |
+
//
|
| 75 |
+
// [Assumptions]
|
| 76 |
+
//
|
| 77 |
+
// 1. Transient Network Failures
|
| 78 |
+
//
|
| 79 |
+
// TODO: current RRef implementation does not tolerate failures
|
| 80 |
+
//
|
| 81 |
+
// The RRef design handles transient network failures by retrying
|
| 82 |
+
// messages. Node crashes or permanent network partition is beyond the scope.
|
| 83 |
+
// When those incidents occur, the application may take down all workers, revert
|
| 84 |
+
// to the previous checkpoint, and resume training.
|
| 85 |
+
//
|
| 86 |
+
// 2. Non-idempotent UDFs
|
| 87 |
+
//
|
| 88 |
+
// We assume UDFs are not idempotent and therefore cannot be retried. However,
|
| 89 |
+
// internal RRef control messages are idempotent and retried upon message
|
| 90 |
+
// failure.
|
| 91 |
+
//
|
| 92 |
+
// TODO: RRef internal messages are not yet idempotent
|
| 93 |
+
//
|
| 94 |
+
// 3. Out of Order Message Delivery
|
| 95 |
+
//
|
| 96 |
+
// We do not assume message delivery order between any pair of nodes, because
|
| 97 |
+
// both sender and receiver are using multiple threads. There is no guarantee on
|
| 98 |
+
// which message will be processed first.
|
| 99 |
+
//
|
| 100 |
+
// [RRef Lifetime]
|
| 101 |
+
//
|
| 102 |
+
// The goal of the protocol is to delete an OwnerRRef at an appropriate time.
|
| 103 |
+
// The right time to delete an OwnerRRef is when there are no living UserRRefs
|
| 104 |
+
// and Python GC also agrees to delete the OwnerRRef instance on the owner. The
|
| 105 |
+
// tricky part is to determine if there are any living UserRRefs.
|
| 106 |
+
//
|
| 107 |
+
// A user can get a UserRRef in three situations:
|
| 108 |
+
//
|
| 109 |
+
// (1). Receiving a UserRRef from the owner.
|
| 110 |
+
// (2). Receiving a UserRRef from another user.
|
| 111 |
+
// (3). Creating a new UserRRef owned by another worker.
|
| 112 |
+
//
|
| 113 |
+
// (1) is the simplest case where the owner initiates the fork, and hence it can
|
| 114 |
+
// easily increment local RC. The only requirement is that any UserRRef must
|
| 115 |
+
// notify the owner before destruction. Hence, we need the first guarantee:
|
| 116 |
+
//
|
| 117 |
+
// G1. The owner will be notified when any UserRRef is deleted.
|
| 118 |
+
//
|
| 119 |
+
// As messages might come delayed or out-of-order, we need more one guarantee to
|
| 120 |
+
// make sure the delete message is not sent out too soon. Let us first introduce
|
| 121 |
+
// a new concept. If A sends an RPC to B that involves an RRef, we call the RRef
|
| 122 |
+
// on A the parent RRef and the RRef on B the child RRef.
|
| 123 |
+
//
|
| 124 |
+
// G2. Parent RRef cannot be deleted until the child RRef is confirmed by the
|
| 125 |
+
// owner.
|
| 126 |
+
//
|
| 127 |
+
// Under (1), where the caller is UserRRef and callee is OwnerRRef, it simply
|
| 128 |
+
// means that the user will not send out the delete message until all previous
|
| 129 |
+
// messages are ACKed. Note that ACKed does not mean the owner finishes
|
| 130 |
+
// executing the function, instead, it only means the owner has retrieved its
|
| 131 |
+
// local OwnerRRef and about to pass it to the function, which is sufficient to
|
| 132 |
+
// keep the OwnerRRef alive even if the delete message from the user arrives at
|
| 133 |
+
// the owner before the function finishes execution.
|
| 134 |
+
//
|
| 135 |
+
// With (2) and (3), it is possible that the owner only partially knows the RRef
|
| 136 |
+
// fork graph or not even knowing it at all. For example, the RRef could be
|
| 137 |
+
// constructed on a user, and before the owner receives the RPC call, the
|
| 138 |
+
// creator user might have already shared the RRef with other users, and those
|
| 139 |
+
// users could further share the RRef. One invariant is that the fork graph of
|
| 140 |
+
// any RRef is always a tree rooted at the owner, because forking an RRef always
|
| 141 |
+
// creates a new RRef instance, and hence every RRef has a single parent. One
|
| 142 |
+
// nasty detail is that when an RRef is created on a user, technically the owner
|
| 143 |
+
// is not its parent but we still consider it that way and it does not break the
|
| 144 |
+
// argument below.
|
| 145 |
+
//
|
| 146 |
+
// The owner's view on any node (fork) in the tree has three stages:
|
| 147 |
+
//
|
| 148 |
+
// 1) unknown -> 2) known -> 3) deleted.
|
| 149 |
+
//
|
| 150 |
+
// The owner's view on the entire tree keeps changing. The owner deletes its
|
| 151 |
+
// OwnerRRef instance when it thinks there are no living UserRRefs, i.e., when
|
| 152 |
+
// OwnerRRef is deleted, all UserRRefs could be either indeed deleted or
|
| 153 |
+
// unknown. The dangerous case is when some forks are unknown and others are
|
| 154 |
+
// deleted.
|
| 155 |
+
//
|
| 156 |
+
// G2 trivially guarantees that no parent UserRRef Y can be deleted before the
|
| 157 |
+
// owner knows all of Y's children UserRRefs.
|
| 158 |
+
//
|
| 159 |
+
// However, it is possible that the child UserRRef Z may be deleted before the
|
| 160 |
+
// owner knows its parent Y. More specifically, this can happen when all of Z's
|
| 161 |
+
// messages are processed by the owner before all messages from Y, including the
|
| 162 |
+
// delete message. Nevertheless, this does not cause any problem. Because, at
|
| 163 |
+
// least one of Y's ancestor will be alive, and it will prevent the owner from
|
| 164 |
+
// deleting the OwnerRRef. Consider the following example: (NB: this scenario
|
| 165 |
+
// will no longer relevant when we block UDF until all RRefs are confirmed by
|
| 166 |
+
// the owner)
|
| 167 |
+
//
|
| 168 |
+
// OwnerRRef -> A -> Y -> Z
|
| 169 |
+
//
|
| 170 |
+
// OwnerRRef forks to A, then A forks to Y, and Y forks to Z. Z can be deleted
|
| 171 |
+
// without OwnerRRef knowing Y. However, the OwnerRRef will at least know A, as
|
| 172 |
+
// the owner directly forks the RRef to A. A won't die before the owner knows Y.
|
| 173 |
+
//
|
| 174 |
+
// Things get a little trickier if the RRef is created on a user:
|
| 175 |
+
//
|
| 176 |
+
// OwnerRRef
|
| 177 |
+
// ^
|
| 178 |
+
// |
|
| 179 |
+
// A -> Y -> Z
|
| 180 |
+
//
|
| 181 |
+
// If Z calls to_here on the UserRRef, the owner at least knows A when Z is
|
| 182 |
+
// deleted, because otherwise to_here wouldn't finish. If Z does not call
|
| 183 |
+
// to_here, it is possible that the owner receives all messages from Z before
|
| 184 |
+
// any message from A and Y. In this case, as the real data of the OwnerRRef has
|
| 185 |
+
// not been created yet, there is nothing to be deleted either. It is the same
|
| 186 |
+
// as Z does not exist at all Hence, it's still OK.
|
| 187 |
+
//
|
| 188 |
+
// See #26759 for more details and discussions.
|
| 189 |
+
//
|
| 190 |
+
// TODO: make RRef an IValue, and edit createStackForSchema accordingly
|
| 191 |
+
// TODO: make RRef system messages idempotent and retry on failures.
|
| 192 |
+
//
|
| 193 |
+
// ``RRef`` is the base type for both ``UserRRef`` and ``OwnerRRef``.
|
| 194 |
+
// Each ``RRef`` has a globally unique ``RRefId``.
|
| 195 |
+
class TORCH_API RRef : public RRefInterface {
|
| 196 |
+
public:
|
| 197 |
+
// RRef is made NOT copyable NOT movable to prevent messing up reference
|
| 198 |
+
// counting.
|
| 199 |
+
explicit RRef(const RRef& other) = delete;
|
| 200 |
+
explicit RRef(RRef&& other) = delete;
|
| 201 |
+
RRef& operator=(RRef&& other) = delete;
|
| 202 |
+
|
| 203 |
+
~RRef() override = default;
|
| 204 |
+
|
| 205 |
+
// returns the worker id of the owner
|
| 206 |
+
inline worker_id_t owner() const override {
|
| 207 |
+
return ownerId_;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// returns the worker name of the owner
|
| 211 |
+
inline std::string ownerName() const override {
|
| 212 |
+
return RpcAgent::getCurrentRpcAgent()->getWorkerInfo(ownerId_).name_;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// returns the worker info of the owner
|
| 216 |
+
inline WorkerInfo ownerWorkerInfo() const {
|
| 217 |
+
return RpcAgent::getCurrentRpcAgent()->getWorkerInfo(ownerId_);
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// Returns the globally unique RRefId of this RRef
|
| 221 |
+
inline const RRefId& rrefId() const {
|
| 222 |
+
return rrefId_;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
inline bool isPyObj() const {
|
| 226 |
+
return type_ == PyObjectType::get();
|
| 227 |
+
}
|
| 228 |
+
inline const TypePtr type() const override {
|
| 229 |
+
return type_;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
// Save the future corresponding to the creation of this RRef on a remote
|
| 233 |
+
// node. Note that this is only set when processing requests invoked with
|
| 234 |
+
// rpc.remote. This is only used to get the future corresponding to the rref
|
| 235 |
+
// for profiling use cases.
|
| 236 |
+
inline void registerOwnerCreationFuture(c10::intrusive_ptr<JitFuture> fut) {
|
| 237 |
+
ownerCreationFuture_ = std::move(fut);
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
// Get the future corresponding to the creation of this rref.
|
| 241 |
+
inline c10::intrusive_ptr<JitFuture> getOwnerCreationFuture() const {
|
| 242 |
+
return ownerCreationFuture_;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// Check if creation of this RRef on owner node has timed out.
|
| 246 |
+
inline bool getTimedOut() const {
|
| 247 |
+
return timedOut_.load();
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
// Dispatches an error to the correct handler based on its RPCErrorType.
|
| 251 |
+
void handleError(RPCErrorType errorType, const JitFuture& JitFuture);
|
| 252 |
+
|
| 253 |
+
// Send delete UserRRef request to Owner,
|
| 254 |
+
// if the request hasn't been sent yet.
|
| 255 |
+
// There are 2 cases to call it,
|
| 256 |
+
// 1, Python GC decides end of UserRRef lifetime, calling destructor.
|
| 257 |
+
// 2, RPC module graceful shutdown calls it on all UserRRefs tracked
|
| 258 |
+
// in the RRefContext.
|
| 259 |
+
virtual void tryDel() {}
|
| 260 |
+
|
| 261 |
+
protected:
|
| 262 |
+
// Indicates that the creation of this RRef on owner node has timed out.
|
| 263 |
+
inline void setTimedOut() {
|
| 264 |
+
timedOut_ = true;
|
| 265 |
+
}
|
| 266 |
+
friend class RRefContext;
|
| 267 |
+
|
| 268 |
+
RRef(worker_id_t ownerId, const RRefId& rrefId, TypePtr type);
|
| 269 |
+
|
| 270 |
+
virtual RRefForkData fork() const;
|
| 271 |
+
|
| 272 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 273 |
+
const worker_id_t ownerId_;
|
| 274 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 275 |
+
const RRefId rrefId_;
|
| 276 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 277 |
+
std::atomic<bool> timedOut_{false};
|
| 278 |
+
|
| 279 |
+
// type field to denote the type of the element that the RRef is holding
|
| 280 |
+
// it could be any TypePtr that JIT support, including PyObjectType
|
| 281 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 282 |
+
const TypePtr type_;
|
| 283 |
+
// Future corresponding to request to create RRef on remote node.
|
| 284 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 285 |
+
c10::intrusive_ptr<JitFuture> ownerCreationFuture_;
|
| 286 |
+
};
|
| 287 |
+
|
| 288 |
+
// ``UserRRef`` represents a user of an RRef. Besides the ``RRefId``, each user
|
| 289 |
+
// also has a globally unique ``ForkId`` to identify this user. ``UserRRef``
|
| 290 |
+
// never owns the real value, the only way to get the value of the ``RRef`` is
|
| 291 |
+
// to call ``to_here()`` and get a copy..
|
| 292 |
+
class TORCH_API UserRRef final : public RRef {
|
| 293 |
+
public:
|
| 294 |
+
UserRRef(const UserRRef& other) = delete;
|
| 295 |
+
UserRRef(UserRRef&& other) = delete;
|
| 296 |
+
UserRRef& operator=(const UserRRef& other) = delete;
|
| 297 |
+
UserRRef& operator=(UserRRef&& other) = delete;
|
| 298 |
+
|
| 299 |
+
UserRRef(
|
| 300 |
+
worker_id_t ownerId,
|
| 301 |
+
const RRefId& rrefId,
|
| 302 |
+
const ForkId& forkId,
|
| 303 |
+
TypePtr type);
|
| 304 |
+
|
| 305 |
+
inline bool isOwner() const override {
|
| 306 |
+
return false;
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
inline bool confirmedByOwner() const override {
|
| 310 |
+
return confirmedByOwner_;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
// Returns the globally unique ForkId of this RRef
|
| 314 |
+
const ForkId& forkId() const;
|
| 315 |
+
|
| 316 |
+
// Get of copy of the value from the ``OwnerRRef``. If the value is not ready
|
| 317 |
+
// yet, this call will block.
|
| 318 |
+
IValue toHere(
|
| 319 |
+
const float timeoutSeconds =
|
| 320 |
+
torch::distributed::rpc::kUnsetRpcTimeout) const;
|
| 321 |
+
|
| 322 |
+
void tryDel() override;
|
| 323 |
+
|
| 324 |
+
// Will be called when refcount reaches 0.
|
| 325 |
+
// Upon destruction, this ``UserRRef`` will tell the owner to deref.
|
| 326 |
+
void release_resources() override;
|
| 327 |
+
|
| 328 |
+
// Will be called when both refcount and weakcount reach 0. See
|
| 329 |
+
// https://github.com/pytorch/pytorch/blob/9116f02bebf3a5260feef5732d36c54ecb3b4033/c10/util/intrusive_ptr.h#L204
|
| 330 |
+
// This is called on destructing the wrapping intrusive_ptr_target instance
|
| 331 |
+
// and its data members.
|
| 332 |
+
~UserRRef() override;
|
| 333 |
+
|
| 334 |
+
private:
|
| 335 |
+
friend class RRefContext;
|
| 336 |
+
|
| 337 |
+
RRefForkData fork() const override;
|
| 338 |
+
inline void confirm() {
|
| 339 |
+
confirmedByOwner_ = true;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
const ForkId forkId_;
|
| 343 |
+
|
| 344 |
+
// Indicates if this user has sent delete message to its owner.
|
| 345 |
+
// Note, thread safety is needed because delete message could be sent by
|
| 346 |
+
// either the destructor called by Python garbage collection or RRefContext
|
| 347 |
+
// proactive cleanup on RPC graceful shutdown.
|
| 348 |
+
std::mutex deletedOnOwnerMutex_;
|
| 349 |
+
bool deletedOnOwner_{false};
|
| 350 |
+
// Indicating whether this UserRRef has been confirmed by its owner.
|
| 351 |
+
std::atomic<bool> confirmedByOwner_;
|
| 352 |
+
};
|
| 353 |
+
|
| 354 |
+
// Keep the template only on the derived class because ``RRefContext`` needs to
|
| 355 |
+
// erase the type on ``RRef`` and keep them in one map.
|
| 356 |
+
class TORCH_API OwnerRRef final : public RRef {
|
| 357 |
+
public:
|
| 358 |
+
OwnerRRef(const OwnerRRef& other) = delete;
|
| 359 |
+
OwnerRRef(OwnerRRef&& other) = delete;
|
| 360 |
+
OwnerRRef& operator=(const OwnerRRef& other) = delete;
|
| 361 |
+
OwnerRRef& operator=(OwnerRRef&& other) = delete;
|
| 362 |
+
|
| 363 |
+
OwnerRRef(
|
| 364 |
+
worker_id_t ownerId,
|
| 365 |
+
const RRefId& rrefId,
|
| 366 |
+
TypePtr type,
|
| 367 |
+
std::vector<c10::Device> devices);
|
| 368 |
+
|
| 369 |
+
OwnerRRef(
|
| 370 |
+
worker_id_t ownerId,
|
| 371 |
+
const RRefId& rrefId,
|
| 372 |
+
TypePtr type,
|
| 373 |
+
std::optional<IValue> value,
|
| 374 |
+
std::vector<c10::Device> devices);
|
| 375 |
+
|
| 376 |
+
inline bool isOwner() const override {
|
| 377 |
+
return true;
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
// OwnerRRef is always confirmed, while UserRRef is only confirmed when the
|
| 381 |
+
// owner knows about it.
|
| 382 |
+
inline bool confirmedByOwner() const override {
|
| 383 |
+
return true;
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
// Get a constant reference of the real value. This method will block if the
|
| 387 |
+
// value is not ready. This method does not need GIL as it does not create
|
| 388 |
+
// any new py::object. It will throw if there is an error.
|
| 389 |
+
const IValue& getValue() const;
|
| 390 |
+
|
| 391 |
+
// Set the value of this ``OwnerRRef``. This method does not need GIL as it
|
| 392 |
+
// does not create any new py::object.
|
| 393 |
+
void setValue(IValue&& value);
|
| 394 |
+
// Sets the value of this ``OwnerRRef`` to contain an exception.
|
| 395 |
+
void setError(std::exception_ptr eptr);
|
| 396 |
+
|
| 397 |
+
// Has a value or error been set?
|
| 398 |
+
bool hasValue() const;
|
| 399 |
+
// Gets a future that is satisfied when the value or error is set.
|
| 400 |
+
c10::intrusive_ptr<JitFuture> getFuture();
|
| 401 |
+
|
| 402 |
+
private:
|
| 403 |
+
friend class RRefContext;
|
| 404 |
+
|
| 405 |
+
c10::intrusive_ptr<JitFuture> future_;
|
| 406 |
+
};
|
| 407 |
+
|
| 408 |
+
TORCH_API std::ostream& operator<<(std::ostream& os, const RRef& rref);
|
| 409 |
+
|
| 410 |
+
// Helper function that casts from c10::RRefInterface to OwnerRRef
|
| 411 |
+
inline TORCH_API c10::intrusive_ptr<OwnerRRef> fromRRefInterface(
|
| 412 |
+
const c10::intrusive_ptr<c10::RRefInterface>& rrefInterface) {
|
| 413 |
+
return c10::static_intrusive_pointer_cast<OwnerRRef>(rrefInterface);
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
// Helper function that casts from OwnerRRef to c10::RRefInterface
|
| 417 |
+
inline TORCH_API c10::intrusive_ptr<c10::RRefInterface> fromOwnerRRef(
|
| 418 |
+
const c10::intrusive_ptr<RRef>& ownerRRef) {
|
| 419 |
+
return c10::static_intrusive_pointer_cast<c10::RRefInterface>(ownerRRef);
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
} // namespace torch::distributed::rpc
|
| 423 |
+
|
| 424 |
+
#else
|
| 425 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 426 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/rref_proto.h
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 6 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 7 |
+
#include <torch/csrc/jit/runtime/operator.h>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch::distributed::rpc {
|
| 11 |
+
|
| 12 |
+
// Temporary solution of RRef operations.
|
| 13 |
+
// TODO: Remove all these messages and use rpc + registered functions instead.
|
| 14 |
+
class TORCH_API RRefMessageBase : public RpcCommandBase {
|
| 15 |
+
public:
|
| 16 |
+
RRefMessageBase(const RRefId& rrefId, MessageType type)
|
| 17 |
+
: rrefId_(rrefId), type_(type) {}
|
| 18 |
+
|
| 19 |
+
const RRefId& rrefId();
|
| 20 |
+
|
| 21 |
+
protected:
|
| 22 |
+
// NOLINTNEXTLINE(cppcoreguidelines*)
|
| 23 |
+
const RRefId rrefId_;
|
| 24 |
+
// NOLINTNEXTLINE(cppcoreguidelines*)
|
| 25 |
+
const MessageType type_;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
class TORCH_API ForkMessageBase : public RRefMessageBase {
|
| 29 |
+
public:
|
| 30 |
+
ForkMessageBase(const RRefId& rrefId, const ForkId& forkId, MessageType type)
|
| 31 |
+
: RRefMessageBase(rrefId, type), forkId_(forkId) {}
|
| 32 |
+
|
| 33 |
+
const ForkId& forkId();
|
| 34 |
+
|
| 35 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 36 |
+
static std::pair<RRefId, ForkId> fromMessage(
|
| 37 |
+
const Message& message,
|
| 38 |
+
MessageType type);
|
| 39 |
+
|
| 40 |
+
protected:
|
| 41 |
+
// NOLINTNEXTLINE(cppcoreguidelines*)
|
| 42 |
+
const ForkId forkId_;
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
// UserRRef uses this message to fetch the remote RRef value from the owner.
|
| 46 |
+
class TORCH_API ScriptRRefFetchCall final : public RRefMessageBase {
|
| 47 |
+
public:
|
| 48 |
+
ScriptRRefFetchCall(worker_id_t fromWorkerId, const RRefId& rrefId)
|
| 49 |
+
: RRefMessageBase(rrefId, MessageType::SCRIPT_RREF_FETCH_CALL),
|
| 50 |
+
fromWorkerId_(fromWorkerId) {}
|
| 51 |
+
|
| 52 |
+
inline worker_id_t fromWorkerId() const {
|
| 53 |
+
return fromWorkerId_;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 57 |
+
static std::unique_ptr<ScriptRRefFetchCall> fromMessage(
|
| 58 |
+
const Message& message);
|
| 59 |
+
|
| 60 |
+
private:
|
| 61 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 62 |
+
const worker_id_t fromWorkerId_;
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
class TORCH_API PythonRRefFetchCall final : public RRefMessageBase {
|
| 66 |
+
public:
|
| 67 |
+
PythonRRefFetchCall(worker_id_t fromWorkerId, const RRefId& rrefId)
|
| 68 |
+
: RRefMessageBase(rrefId, MessageType::PYTHON_RREF_FETCH_CALL),
|
| 69 |
+
fromWorkerId_(fromWorkerId) {}
|
| 70 |
+
|
| 71 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 72 |
+
static std::unique_ptr<PythonRRefFetchCall> fromMessage(
|
| 73 |
+
const Message& message);
|
| 74 |
+
|
| 75 |
+
private:
|
| 76 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 77 |
+
const worker_id_t fromWorkerId_;
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
// OwnerRRef uses this message to send the RRef value to a remote UserRRef
|
| 81 |
+
class TORCH_API RRefFetchRet : public RpcCommandBase {
|
| 82 |
+
public:
|
| 83 |
+
RRefFetchRet(std::vector<at::IValue> values, MessageType type)
|
| 84 |
+
: values_(std::move(values)), type_(type) {}
|
| 85 |
+
|
| 86 |
+
const std::vector<at::IValue>& values();
|
| 87 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 88 |
+
|
| 89 |
+
private:
|
| 90 |
+
std::vector<at::IValue> values_;
|
| 91 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 92 |
+
const MessageType type_;
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
class TORCH_API ScriptRRefFetchRet final : public RRefFetchRet {
|
| 96 |
+
public:
|
| 97 |
+
explicit ScriptRRefFetchRet(std::vector<at::IValue> values)
|
| 98 |
+
: RRefFetchRet(std::move(values), MessageType::SCRIPT_RREF_FETCH_RET) {}
|
| 99 |
+
|
| 100 |
+
static std::unique_ptr<ScriptRRefFetchRet> fromMessage(
|
| 101 |
+
const Message& message);
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
class TORCH_API PythonRRefFetchRet final : public RRefFetchRet {
|
| 105 |
+
public:
|
| 106 |
+
explicit PythonRRefFetchRet(std::vector<at::IValue> values)
|
| 107 |
+
: RRefFetchRet(std::move(values), MessageType::PYTHON_RREF_FETCH_RET) {}
|
| 108 |
+
|
| 109 |
+
static std::unique_ptr<PythonRRefFetchRet> fromMessage(
|
| 110 |
+
const Message& message);
|
| 111 |
+
};
|
| 112 |
+
|
| 113 |
+
// UserRRef (regardless it's the creator or not) uses this message to notify
|
| 114 |
+
// OwnerRRef on delete.
|
| 115 |
+
class TORCH_API RRefUserDelete final : public ForkMessageBase {
|
| 116 |
+
public:
|
| 117 |
+
RRefUserDelete(const RRefId& rrefId, const ForkId& forkId)
|
| 118 |
+
: ForkMessageBase(rrefId, forkId, MessageType::RREF_USER_DELETE) {}
|
| 119 |
+
|
| 120 |
+
static std::unique_ptr<RRefUserDelete> fromMessage(const Message& message);
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
class TORCH_API RemoteRet final : public ForkMessageBase {
|
| 124 |
+
public:
|
| 125 |
+
RemoteRet(const RRefId& rrefId, const ForkId& forkId)
|
| 126 |
+
: ForkMessageBase(rrefId, forkId, MessageType::REMOTE_RET) {}
|
| 127 |
+
|
| 128 |
+
static std::unique_ptr<RemoteRet> fromMessage(const Message& message);
|
| 129 |
+
};
|
| 130 |
+
|
| 131 |
+
// A child RRef uses this message to notify its parent that the child has been
|
| 132 |
+
// confirmed by the owner.
|
| 133 |
+
class TORCH_API RRefChildAccept final : public RpcCommandBase {
|
| 134 |
+
public:
|
| 135 |
+
explicit RRefChildAccept(const ForkId& forkId) : forkId_(forkId) {}
|
| 136 |
+
|
| 137 |
+
const ForkId& forkId() const;
|
| 138 |
+
|
| 139 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 140 |
+
static std::unique_ptr<RRefChildAccept> fromMessage(const Message& message);
|
| 141 |
+
|
| 142 |
+
private:
|
| 143 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 144 |
+
const ForkId forkId_;
|
| 145 |
+
};
|
| 146 |
+
|
| 147 |
+
// A child RRef uses this message to send a fork request to the owner.
|
| 148 |
+
class TORCH_API RRefForkRequest final : public ForkMessageBase {
|
| 149 |
+
public:
|
| 150 |
+
RRefForkRequest(const RRefId& rrefId, const ForkId& forkId)
|
| 151 |
+
: ForkMessageBase(rrefId, forkId, MessageType::RREF_FORK_REQUEST) {}
|
| 152 |
+
|
| 153 |
+
static std::unique_ptr<RRefForkRequest> fromMessage(const Message& message);
|
| 154 |
+
};
|
| 155 |
+
|
| 156 |
+
class TORCH_API RRefAck final : public RpcCommandBase {
|
| 157 |
+
public:
|
| 158 |
+
RRefAck() = default;
|
| 159 |
+
|
| 160 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 161 |
+
static std::unique_ptr<RRefAck> fromMessage(const Message& message);
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
} // namespace torch::distributed::rpc
|
| 165 |
+
|
| 166 |
+
#else
|
| 167 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 168 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_call.h
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 6 |
+
#include <torch/csrc/jit/runtime/operator.h>
|
| 7 |
+
#include <optional>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch::distributed::rpc {
|
| 11 |
+
|
| 12 |
+
using torch::jit::Operator;
|
| 13 |
+
|
| 14 |
+
// A ScriptCall instance represents an invocation of a builtin operator for a
|
| 15 |
+
// TorchScript function. If it is a builtin operator, it
|
| 16 |
+
// contains a shared ptr to the `Operator` and a list of arguments.
|
| 17 |
+
// If it is a TorchScript function, it contains a non empty qualifiedName string
|
| 18 |
+
// to the TorchScript function schema name and a list of arguments.
|
| 19 |
+
class TORCH_API ScriptCall : public RpcCommandBase {
|
| 20 |
+
public:
|
| 21 |
+
// Constructor for builtin operator call.
|
| 22 |
+
ScriptCall(std::shared_ptr<Operator> op, std::vector<at::IValue>&& stack);
|
| 23 |
+
// Constructor for TorchScript function call.
|
| 24 |
+
ScriptCall(
|
| 25 |
+
const c10::QualifiedName& qualifiedName,
|
| 26 |
+
std::vector<at::IValue>&& stack,
|
| 27 |
+
const bool isAsyncExecution = false);
|
| 28 |
+
|
| 29 |
+
bool hasOp() const;
|
| 30 |
+
std::shared_ptr<Operator> op() const;
|
| 31 |
+
bool hasQualifiedName() const;
|
| 32 |
+
const c10::QualifiedName& qualifiedName() const;
|
| 33 |
+
// return the argument stack of this builtin operator
|
| 34 |
+
const std::vector<at::IValue>& stack() const;
|
| 35 |
+
std::vector<at::IValue>& stackRef();
|
| 36 |
+
inline bool isAsyncExecution() const {
|
| 37 |
+
return isAsyncExecution_;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 41 |
+
static std::unique_ptr<ScriptCall> fromMessage(const Message& message);
|
| 42 |
+
|
| 43 |
+
~ScriptCall() override = default;
|
| 44 |
+
|
| 45 |
+
protected:
|
| 46 |
+
virtual void toIValues(std::vector<at::IValue>& ivalues) const;
|
| 47 |
+
static std::unique_ptr<ScriptCall> fromIValues(
|
| 48 |
+
std::vector<at::IValue>& ivalues);
|
| 49 |
+
|
| 50 |
+
private:
|
| 51 |
+
// Given an operator symbol and a string schema, return the matched operator.
|
| 52 |
+
static std::shared_ptr<Operator> matchOperator(const std::string& str_schema);
|
| 53 |
+
|
| 54 |
+
static const std::string BUILTIN_OP_NAMESPACE_;
|
| 55 |
+
static const std::string ATEN_PREFIX_;
|
| 56 |
+
|
| 57 |
+
// This field has value if this ScriptCall represents invocation of a builtin
|
| 58 |
+
// operator.
|
| 59 |
+
std::optional<std::shared_ptr<Operator>> op_;
|
| 60 |
+
// This field has non empty string if this ScriptCall represents invocation of
|
| 61 |
+
// an annotated torchscript function defined by users.
|
| 62 |
+
std::optional<const c10::QualifiedName> qualifiedName_;
|
| 63 |
+
std::vector<at::IValue> stack_;
|
| 64 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 65 |
+
const bool isAsyncExecution_;
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
} // namespace torch::distributed::rpc
|
| 69 |
+
|
| 70 |
+
#else
|
| 71 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 72 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_remote_call.h
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/script_call.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/types.h>
|
| 6 |
+
#include <torch/csrc/jit/runtime/operator.h>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
|
| 11 |
+
using torch::jit::Operator;
|
| 12 |
+
|
| 13 |
+
// A ScriptRemoteCall instance represents an invocation of `dist.remote` on a
|
| 14 |
+
// builtin operator. Currently, it does not support using RRef as arguments yet.
|
| 15 |
+
// Besides the operator and a vector of arguments, ScriptRemoteCall also
|
| 16 |
+
// contains the RRefId and the ForkId of the return value RRef.
|
| 17 |
+
class TORCH_API ScriptRemoteCall final : public ScriptCall {
|
| 18 |
+
public:
|
| 19 |
+
// Constructor for builtin operator call.
|
| 20 |
+
ScriptRemoteCall(
|
| 21 |
+
std::shared_ptr<Operator> op,
|
| 22 |
+
std::vector<at::IValue>&& stack,
|
| 23 |
+
const RRefId& retRRefId,
|
| 24 |
+
const ForkId& retForkId);
|
| 25 |
+
|
| 26 |
+
// Constructor for TorchScript function call.
|
| 27 |
+
ScriptRemoteCall(
|
| 28 |
+
const c10::QualifiedName& qualifiedName,
|
| 29 |
+
std::vector<at::IValue>&& stack,
|
| 30 |
+
const RRefId& retRRefId,
|
| 31 |
+
const ForkId& retForkId,
|
| 32 |
+
const bool isAsyncExecution);
|
| 33 |
+
|
| 34 |
+
inline const RRefId& retRRefId() const {
|
| 35 |
+
return retRRefId_;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
inline const ForkId& retForkId() const {
|
| 39 |
+
return retForkId_;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
static std::unique_ptr<ScriptRemoteCall> fromIValues(
|
| 43 |
+
std::vector<at::IValue>& ivalues);
|
| 44 |
+
|
| 45 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 46 |
+
static std::unique_ptr<ScriptRemoteCall> fromMessage(const Message& message);
|
| 47 |
+
|
| 48 |
+
private:
|
| 49 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 50 |
+
const RRefId retRRefId_;
|
| 51 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 52 |
+
const ForkId retForkId_;
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
} // namespace torch::distributed::rpc
|
| 56 |
+
|
| 57 |
+
#else
|
| 58 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 59 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/script_resp.h
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 5 |
+
#include <torch/csrc/distributed/rpc/rpc_command_base.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::distributed::rpc {
|
| 8 |
+
|
| 9 |
+
// Return value of a builtin operator or a TorchScript function.
|
| 10 |
+
class TORCH_API ScriptResp final : public RpcCommandBase {
|
| 11 |
+
public:
|
| 12 |
+
explicit ScriptResp(at::IValue&& values);
|
| 13 |
+
|
| 14 |
+
const at::IValue& value();
|
| 15 |
+
c10::intrusive_ptr<Message> toMessageImpl() && override;
|
| 16 |
+
static std::unique_ptr<ScriptResp> fromMessage(const Message& message);
|
| 17 |
+
|
| 18 |
+
private:
|
| 19 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 20 |
+
const at::IValue value_;
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
} // namespace torch::distributed::rpc
|
| 24 |
+
|
| 25 |
+
#else
|
| 26 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 27 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/tensorpipe_agent.h
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef USE_TENSORPIPE
|
| 5 |
+
|
| 6 |
+
#include <atomic>
|
| 7 |
+
#include <thread>
|
| 8 |
+
|
| 9 |
+
#include <c10/core/thread_pool.h>
|
| 10 |
+
#include <torch/csrc/distributed/c10d/PrefixStore.hpp>
|
| 11 |
+
#include <torch/csrc/distributed/c10d/Store.hpp>
|
| 12 |
+
#include <torch/csrc/distributed/rpc/rpc_agent.h>
|
| 13 |
+
#include <utility>
|
| 14 |
+
|
| 15 |
+
// Forward-declare the TensorPipe classes we need, to avoid including its
|
| 16 |
+
// headers in PyTorch's ones and thus have it become a public dependency.
|
| 17 |
+
|
| 18 |
+
namespace tensorpipe {
|
| 19 |
+
|
| 20 |
+
class Context;
|
| 21 |
+
class Error;
|
| 22 |
+
class Listener;
|
| 23 |
+
class Message;
|
| 24 |
+
class Pipe;
|
| 25 |
+
|
| 26 |
+
namespace transport {
|
| 27 |
+
class Context;
|
| 28 |
+
} // namespace transport
|
| 29 |
+
|
| 30 |
+
namespace channel {
|
| 31 |
+
class Context;
|
| 32 |
+
} // namespace channel
|
| 33 |
+
|
| 34 |
+
} // namespace tensorpipe
|
| 35 |
+
|
| 36 |
+
namespace torch::distributed::rpc {
|
| 37 |
+
|
| 38 |
+
// These priorities instruct TensorPipe on which transport/channel to pick
|
| 39 |
+
// during handshake. Higher priorities will take precedence over lower ones.
|
| 40 |
+
// The transport with lowest priority will be the one used to bootstrap pipes.
|
| 41 |
+
|
| 42 |
+
constexpr int64_t kShmTransportPriority = 200;
|
| 43 |
+
constexpr int64_t kIbvTransportPriority = 100;
|
| 44 |
+
// The UV transport just uses TCP and should work everywhere, thus keep it last.
|
| 45 |
+
constexpr int64_t kUvTransportPriority = 0;
|
| 46 |
+
|
| 47 |
+
constexpr int64_t kCmaChannelPriority = 1200;
|
| 48 |
+
constexpr int64_t kMultiplexedUvChannelPriority = 1100;
|
| 49 |
+
// The basic channel reuses a transport as a channel, and is thus our fallback.
|
| 50 |
+
constexpr int64_t kBasicChannelPriority = 1000;
|
| 51 |
+
|
| 52 |
+
// CPU channel have higher priority than CUDA channels, since the latter might
|
| 53 |
+
// handle CPU-to-CPU transfers, but will always be less efficient than their
|
| 54 |
+
// CPU-only counterparts.
|
| 55 |
+
constexpr int64_t kCudaIpcChannelPriority = 300;
|
| 56 |
+
constexpr int64_t kCudaGdrChannelPriority = 200;
|
| 57 |
+
constexpr int64_t kCudaXthChannelPriority = 400;
|
| 58 |
+
constexpr int64_t kCudaBasicChannelPriority = 0;
|
| 59 |
+
|
| 60 |
+
using steady_clock_time_point =
|
| 61 |
+
std::chrono::time_point<std::chrono::steady_clock>;
|
| 62 |
+
|
| 63 |
+
struct TORCH_API TransportRegistration {
|
| 64 |
+
std::shared_ptr<tensorpipe::transport::Context> transport;
|
| 65 |
+
int64_t priority;
|
| 66 |
+
std::string address;
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
TORCH_DECLARE_REGISTRY(TensorPipeTransportRegistry, TransportRegistration);
|
| 70 |
+
|
| 71 |
+
struct TORCH_API ChannelRegistration {
|
| 72 |
+
std::shared_ptr<tensorpipe::channel::Context> channel;
|
| 73 |
+
int64_t priority;
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
TORCH_DECLARE_REGISTRY(TensorPipeChannelRegistry, ChannelRegistration);
|
| 77 |
+
|
| 78 |
+
struct TORCH_API TensorPipeRpcBackendOptions : public RpcBackendOptions {
|
| 79 |
+
TensorPipeRpcBackendOptions(
|
| 80 |
+
int numWorkerThreads,
|
| 81 |
+
std::optional<std::vector<std::string>> transports,
|
| 82 |
+
std::optional<std::vector<std::string>> channels,
|
| 83 |
+
float rpc_timeout,
|
| 84 |
+
std::string init_method,
|
| 85 |
+
std::unordered_map<std::string, DeviceMap> device_maps = {},
|
| 86 |
+
std::vector<c10::Device> devices = {})
|
| 87 |
+
: RpcBackendOptions(rpc_timeout, std::move(init_method)),
|
| 88 |
+
numWorkerThreads(numWorkerThreads),
|
| 89 |
+
transports(std::move(transports)),
|
| 90 |
+
channels(std::move(channels)),
|
| 91 |
+
deviceMaps(std::move(device_maps)),
|
| 92 |
+
devices(std::move(devices)) {
|
| 93 |
+
TORCH_CHECK(
|
| 94 |
+
numWorkerThreads > 0,
|
| 95 |
+
"num_worker_threads must be positive, got ",
|
| 96 |
+
numWorkerThreads);
|
| 97 |
+
|
| 98 |
+
if (this->transports.has_value()) {
|
| 99 |
+
for (const std::string& transportName : this->transports.value()) {
|
| 100 |
+
TORCH_CHECK(
|
| 101 |
+
TensorPipeTransportRegistry()->Has(transportName),
|
| 102 |
+
"Unknown transport: ",
|
| 103 |
+
transportName);
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
if (this->channels.has_value()) {
|
| 108 |
+
for (const std::string& channelName : this->channels.value()) {
|
| 109 |
+
TORCH_CHECK(
|
| 110 |
+
TensorPipeChannelRegistry()->Has(channelName),
|
| 111 |
+
"Unknown channel: ",
|
| 112 |
+
channelName);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
void setDeviceMap(const std::string& workerName, const DeviceMap& deviceMap) {
|
| 118 |
+
auto iter = deviceMaps.find(workerName);
|
| 119 |
+
if (iter == deviceMaps.end()) {
|
| 120 |
+
deviceMaps[workerName] = deviceMap;
|
| 121 |
+
} else {
|
| 122 |
+
for (auto& entry : deviceMap) {
|
| 123 |
+
// c10::Device has no default constructor, hence map[device] doesn't
|
| 124 |
+
// work In C++-17 we can use insert_or_assign.
|
| 125 |
+
auto entryIter = iter->second.find(entry.first);
|
| 126 |
+
if (entryIter == iter->second.end()) {
|
| 127 |
+
iter->second.emplace(entry.first, entry.second);
|
| 128 |
+
} else {
|
| 129 |
+
entryIter->second = entry.second;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
int numWorkerThreads;
|
| 136 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 137 |
+
const std::optional<std::vector<std::string>> transports;
|
| 138 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 139 |
+
const std::optional<std::vector<std::string>> channels;
|
| 140 |
+
std::unordered_map<std::string, DeviceMap> deviceMaps;
|
| 141 |
+
std::vector<c10::Device> devices;
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
// Struct to track the network source metrics
|
| 145 |
+
struct TORCH_API NetworkSourceInfo {
|
| 146 |
+
worker_id_t srcRank;
|
| 147 |
+
std::vector<uint8_t> srcMachineAddr;
|
| 148 |
+
};
|
| 149 |
+
|
| 150 |
+
// Struct to track aggregated network metrics
|
| 151 |
+
struct TORCH_API AggregatedNetworkData {
|
| 152 |
+
uint64_t numCalls{0};
|
| 153 |
+
uint64_t totalSentBytes{0};
|
| 154 |
+
uint64_t totalRecvBytes{0};
|
| 155 |
+
uint64_t totalErrors{0};
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
// TensorPipeAgent leverages TensorPipe (https://github.com/pytorch/tensorpipe)
|
| 159 |
+
// to transparently move tensors and payloads through the fastest available
|
| 160 |
+
// transport or channel. It acts like a hybrid RPC transport, providing shared
|
| 161 |
+
// memory (linux) and TCP (linux & mac) support. CUDA support is in progress.
|
| 162 |
+
class TORCH_API TensorPipeAgent : public RpcAgent {
|
| 163 |
+
public:
|
| 164 |
+
TensorPipeAgent(
|
| 165 |
+
const c10::intrusive_ptr<::c10d::Store>& store,
|
| 166 |
+
std::string selfName,
|
| 167 |
+
worker_id_t selfId,
|
| 168 |
+
std::optional<int> worldSize,
|
| 169 |
+
TensorPipeRpcBackendOptions opts,
|
| 170 |
+
std::unordered_map<std::string, DeviceMap> reverseDeviceMaps,
|
| 171 |
+
std::vector<c10::Device> devices,
|
| 172 |
+
std::unique_ptr<RequestCallback> cb);
|
| 173 |
+
|
| 174 |
+
TensorPipeAgent(const TensorPipeAgent&) = delete;
|
| 175 |
+
TensorPipeAgent& operator=(const TensorPipeAgent&) = delete;
|
| 176 |
+
|
| 177 |
+
c10::intrusive_ptr<JitFuture> send(
|
| 178 |
+
const WorkerInfo& to,
|
| 179 |
+
c10::intrusive_ptr<Message> message,
|
| 180 |
+
const float rpcTimeoutSeconds = kUnsetRpcTimeout,
|
| 181 |
+
const DeviceMap& deviceMap = {}) override;
|
| 182 |
+
|
| 183 |
+
// join() and sync() would be deprecated -
|
| 184 |
+
// https://github.com/pytorch/pytorch/issues/27647
|
| 185 |
+
void join(bool shutdown = false, float timeout = 0) override;
|
| 186 |
+
void sync() override {}
|
| 187 |
+
void startImpl() override;
|
| 188 |
+
void shutdownImpl() override;
|
| 189 |
+
|
| 190 |
+
~TensorPipeAgent() override;
|
| 191 |
+
|
| 192 |
+
const WorkerInfo& getWorkerInfo(const std::string& workerName) const override;
|
| 193 |
+
const WorkerInfo& getWorkerInfo(worker_id_t workerId) const override;
|
| 194 |
+
std::vector<WorkerInfo> getWorkerInfos() const override;
|
| 195 |
+
void updateGroupMembership(
|
| 196 |
+
const WorkerInfo& workerInfo,
|
| 197 |
+
const std::vector<c10::Device>& devices,
|
| 198 |
+
const std::unordered_map<std::string, DeviceMap>& reverseDeviceMaps,
|
| 199 |
+
bool isJoin);
|
| 200 |
+
|
| 201 |
+
std::unordered_map<std::string, std::string> getMetrics() override;
|
| 202 |
+
|
| 203 |
+
void addGilWaitTime(const std::chrono::microseconds gilWaitTime) override;
|
| 204 |
+
|
| 205 |
+
TensorPipeRpcBackendOptions getBackendOptions() const;
|
| 206 |
+
|
| 207 |
+
const c10::intrusive_ptr<::c10d::Store> getStore() const;
|
| 208 |
+
|
| 209 |
+
DeviceMap getDeviceMap(const WorkerInfo& dest) const override;
|
| 210 |
+
|
| 211 |
+
const std::vector<c10::Device>& getDevices() const override;
|
| 212 |
+
|
| 213 |
+
using NetworkDataDict =
|
| 214 |
+
std::unordered_map<std::string, AggregatedNetworkData>;
|
| 215 |
+
|
| 216 |
+
// Returns metrics tracked by the NetworkDataDict
|
| 217 |
+
NetworkDataDict getNetworkData();
|
| 218 |
+
// Returns NetworkSourceInfo struct
|
| 219 |
+
NetworkSourceInfo getNetworkSourceInfo();
|
| 220 |
+
|
| 221 |
+
static const std::string& guessAddress();
|
| 222 |
+
|
| 223 |
+
// For testing purposes.
|
| 224 |
+
size_t timeoutMapSize();
|
| 225 |
+
size_t numPendingResponses();
|
| 226 |
+
size_t messageIdToTimeoutMapSize();
|
| 227 |
+
|
| 228 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 229 |
+
const bool isStaticGroup_;
|
| 230 |
+
|
| 231 |
+
protected:
|
| 232 |
+
// TensorPipe write function that could be used to write response
|
| 233 |
+
// messages by server, and write request messages by client. This
|
| 234 |
+
// is a protected method since it is overwritten by FaultyTensorPipeAgent
|
| 235 |
+
virtual void pipeWrite(
|
| 236 |
+
const std::shared_ptr<tensorpipe::Pipe>& /*pipe*/,
|
| 237 |
+
const c10::intrusive_ptr<Message>& message,
|
| 238 |
+
std::vector<c10::Device>&& devices,
|
| 239 |
+
std::vector<c10::Stream> streams,
|
| 240 |
+
std::function<void(const tensorpipe::Error&)> /*fn*/) noexcept;
|
| 241 |
+
|
| 242 |
+
private:
|
| 243 |
+
// Removes the given messageId with the given expirationTime from the
|
| 244 |
+
// timeoutMap_.
|
| 245 |
+
void removeFromTimeoutMap(uint64_t messageId);
|
| 246 |
+
|
| 247 |
+
// Populates workerIdToInfo_ and workerNameToInfo_ using addressStore_
|
| 248 |
+
void prepareNames(bool isStaticGroup);
|
| 249 |
+
|
| 250 |
+
// Check the static group attribute with the value set in store
|
| 251 |
+
void checkAndSetStaticGroup(const c10::intrusive_ptr<::c10d::Store>& store);
|
| 252 |
+
|
| 253 |
+
const std::string& findWorkerURL(const WorkerInfo& worker) const;
|
| 254 |
+
|
| 255 |
+
// Only use for Dynamic RPC groups, method to have worker leave group
|
| 256 |
+
void leaveGroup();
|
| 257 |
+
|
| 258 |
+
// TensorPipe read function that could be used to read response messages
|
| 259 |
+
// by client, and read request messages by server.
|
| 260 |
+
void pipeRead(
|
| 261 |
+
const std::shared_ptr<tensorpipe::Pipe>& /*pipe*/,
|
| 262 |
+
std::function<void(
|
| 263 |
+
const tensorpipe::Error&,
|
| 264 |
+
c10::intrusive_ptr<Message>,
|
| 265 |
+
std::vector<c10::Stream>)> /*fn*/) noexcept;
|
| 266 |
+
|
| 267 |
+
// Callback of listener accept()
|
| 268 |
+
void onListenerAccepted(
|
| 269 |
+
const tensorpipe::Error& error,
|
| 270 |
+
std::shared_ptr<tensorpipe::Pipe>& pipe);
|
| 271 |
+
|
| 272 |
+
// Respond to a call from a peer
|
| 273 |
+
void respond(std::shared_ptr<tensorpipe::Pipe>& pipe);
|
| 274 |
+
|
| 275 |
+
void sendCompletedResponseMessage(
|
| 276 |
+
std::shared_ptr<tensorpipe::Pipe>& pipe,
|
| 277 |
+
JitFuture& futureResponseMessage,
|
| 278 |
+
uint64_t messageId,
|
| 279 |
+
std::vector<c10::Stream> stream);
|
| 280 |
+
|
| 281 |
+
// Collects metrics from successful RPC calls
|
| 282 |
+
void trackNetworkData(
|
| 283 |
+
uint64_t requestSize,
|
| 284 |
+
uint64_t responseSize,
|
| 285 |
+
const std::string& destWorkerName);
|
| 286 |
+
|
| 287 |
+
// Collects metrics from failed RPC calls
|
| 288 |
+
void trackNetworkError(
|
| 289 |
+
uint64_t requestSize,
|
| 290 |
+
const std::string& destWorkerName);
|
| 291 |
+
|
| 292 |
+
inline std::vector<c10::Device> getDevicesForRemote(
|
| 293 |
+
const std::string& remoteName,
|
| 294 |
+
const Message& message) const;
|
| 295 |
+
|
| 296 |
+
// When a request+response completes, we need to mark the future message as
|
| 297 |
+
// complete. However, if its timeout has already expired, it already has an
|
| 298 |
+
// error set. There is no atomic "test-and-set" way to mark a future complete
|
| 299 |
+
// only if it isn't yet. It does exist for errors (setErrorIfNeeded) but, even
|
| 300 |
+
// then, it ends up printing a log message, which may worry the user. To solve
|
| 301 |
+
// both issues we use a separate atomic flag to know the status of the future.
|
| 302 |
+
struct AtomicJitFuture {
|
| 303 |
+
explicit AtomicJitFuture(const std::vector<c10::Device>& devices) {
|
| 304 |
+
jitFuture = c10::make_intrusive<at::ivalue::Future>(
|
| 305 |
+
at::AnyClassType::get(), devices);
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
std::atomic_flag isComplete = ATOMIC_FLAG_INIT;
|
| 309 |
+
c10::intrusive_ptr<JitFuture> jitFuture;
|
| 310 |
+
};
|
| 311 |
+
|
| 312 |
+
// Maintains state per client pipe to track pending response messages and
|
| 313 |
+
// error states. pendingResponseMessage_ should be protected by a mutex since
|
| 314 |
+
// it can be raced with user send() call.
|
| 315 |
+
// TODO: To achieve better performance we can have a pipe pool per
|
| 316 |
+
// client that can be configured using RpcBackendOptions.
|
| 317 |
+
struct ClientPipe {
|
| 318 |
+
explicit ClientPipe(std::shared_ptr<tensorpipe::Pipe> pipe)
|
| 319 |
+
: pipe_(std::move(pipe)) {}
|
| 320 |
+
std::shared_ptr<tensorpipe::Pipe> pipe_;
|
| 321 |
+
mutable std::mutex mutex_;
|
| 322 |
+
bool inError_{false};
|
| 323 |
+
// Map from Message Request ID's to corresponding futures.
|
| 324 |
+
std::unordered_map<uint64_t, std::shared_ptr<AtomicJitFuture>>
|
| 325 |
+
pendingResponseMessage_;
|
| 326 |
+
};
|
| 327 |
+
|
| 328 |
+
const c10::intrusive_ptr<::c10d::Store> store_;
|
| 329 |
+
|
| 330 |
+
const TensorPipeRpcBackendOptions opts_;
|
| 331 |
+
// For dynamic RPC, the reverse device maps are updated whenever a new rank
|
| 332 |
+
// joins or leaves the group
|
| 333 |
+
std::unordered_map<std::string, DeviceMap> reverseDeviceMaps_;
|
| 334 |
+
// Local devices used by this agent. If application didn't specify this
|
| 335 |
+
// field, it will be initialized using corresponding local devices in
|
| 336 |
+
// opts_.deviceMaps and reverseDeviceMaps_;
|
| 337 |
+
std::vector<c10::Device> devices_;
|
| 338 |
+
|
| 339 |
+
ThreadPool threadPool_;
|
| 340 |
+
std::shared_ptr<tensorpipe::Context> context_;
|
| 341 |
+
std::shared_ptr<tensorpipe::Listener> listener_;
|
| 342 |
+
|
| 343 |
+
mutable std::mutex connectedPipesMutex_;
|
| 344 |
+
std::unordered_map<worker_id_t, ClientPipe> connectedPipes_;
|
| 345 |
+
|
| 346 |
+
// Maps keyed on name and id for easy WorkerInfo lookup.
|
| 347 |
+
std::unordered_map<worker_id_t, WorkerInfo> workerIdToInfo_;
|
| 348 |
+
std::unordered_map<std::string, WorkerInfo> workerNameToInfo_;
|
| 349 |
+
std::unordered_map<std::string, std::string> workerNameToURL_;
|
| 350 |
+
|
| 351 |
+
::c10d::PrefixStore rankToNameStore_;
|
| 352 |
+
::c10d::PrefixStore nameToAddressStore_;
|
| 353 |
+
// Store keys that will used to count joined processes and active calls during
|
| 354 |
+
// the shutdown process
|
| 355 |
+
::c10d::PrefixStore shutdownStore_;
|
| 356 |
+
int worldSize_ = 0;
|
| 357 |
+
std::atomic<uint64_t> nextMessageID_{0};
|
| 358 |
+
|
| 359 |
+
// Metadata used for tracking of whether certain RPCs have timed out or not.
|
| 360 |
+
struct TimeoutMessageMetadata {
|
| 361 |
+
TimeoutMessageMetadata(
|
| 362 |
+
uint64_t messageId_,
|
| 363 |
+
std::shared_ptr<AtomicJitFuture> responseFuture_,
|
| 364 |
+
std::chrono::milliseconds timeout_)
|
| 365 |
+
: messageId(messageId_),
|
| 366 |
+
responseFuture(std::move(responseFuture_)),
|
| 367 |
+
timeout(timeout_) {}
|
| 368 |
+
uint64_t messageId;
|
| 369 |
+
std::shared_ptr<AtomicJitFuture> responseFuture;
|
| 370 |
+
std::chrono::milliseconds timeout;
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
// Map to store the expiration times for each message.
|
| 374 |
+
std::map<steady_clock_time_point, std::vector<TimeoutMessageMetadata>>
|
| 375 |
+
timeoutMap_;
|
| 376 |
+
|
| 377 |
+
// Map to store the messageId to expiry time.
|
| 378 |
+
std::unordered_map<uint64_t, steady_clock_time_point> messageIdToTimeout_;
|
| 379 |
+
|
| 380 |
+
// Thread that will poll the timeoutMap_ for timed out messages and mark them
|
| 381 |
+
// with an error accordingly
|
| 382 |
+
std::thread timeoutThread_;
|
| 383 |
+
|
| 384 |
+
// Function run by the timeoutThread_ to check for timed out RPCs
|
| 385 |
+
void pollTimeoutRpcs();
|
| 386 |
+
|
| 387 |
+
// Mutex to guard the timeoutMap_
|
| 388 |
+
std::mutex timeoutMapMutex_;
|
| 389 |
+
|
| 390 |
+
// Condition Variable to signal population of the timeoutMap_
|
| 391 |
+
std::condition_variable timeoutThreadCV_;
|
| 392 |
+
|
| 393 |
+
// Returns the expiration time for an RPC by adding the current time to the
|
| 394 |
+
// passed in timeout.
|
| 395 |
+
inline steady_clock_time_point computeRpcMessageExpiryTime(
|
| 396 |
+
std::chrono::milliseconds timeout) const {
|
| 397 |
+
return std::chrono::time_point_cast<std::chrono::milliseconds>(
|
| 398 |
+
std::chrono::steady_clock::now() + timeout);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
// Handle error on an outgoing pipe
|
| 402 |
+
void handleClientError(
|
| 403 |
+
ClientPipe& clientPipe,
|
| 404 |
+
const tensorpipe::Error& error);
|
| 405 |
+
|
| 406 |
+
// This is a generic struct for capturing Time-Series Metrics. It keeps a
|
| 407 |
+
// running sum and count of data points (observations), and can return an
|
| 408 |
+
// average of the data points seen so far. This is currently only used for
|
| 409 |
+
// tracking the GIL Wait Time in RPC Agents, but can be used for other metrics
|
| 410 |
+
// as well.
|
| 411 |
+
struct TimeSeriesMetricsTracker {
|
| 412 |
+
// Running sum of the data points seen so far
|
| 413 |
+
uint64_t currentSum_;
|
| 414 |
+
// Running count of the data points seen so far
|
| 415 |
+
uint64_t currentCount_;
|
| 416 |
+
|
| 417 |
+
explicit TimeSeriesMetricsTracker(
|
| 418 |
+
uint64_t currentSum = 0,
|
| 419 |
+
uint64_t currentCount = 0);
|
| 420 |
+
|
| 421 |
+
// Adds a data point (which is basically one observation for the metric
|
| 422 |
+
// being tracked) to the running sum and count.
|
| 423 |
+
void addData(uint64_t dataPoint);
|
| 424 |
+
// Returns the average of all the data points seen so far.
|
| 425 |
+
float computeAverage() const;
|
| 426 |
+
};
|
| 427 |
+
|
| 428 |
+
// Map of Time-Series metrics tracked by the RPC Agent
|
| 429 |
+
std::unordered_map<std::string, TimeSeriesMetricsTracker> timeSeriesMetrics_;
|
| 430 |
+
// Mutex to guard timeSeriesMetrics_
|
| 431 |
+
std::mutex metricsMutex_;
|
| 432 |
+
|
| 433 |
+
// Custom lock guard used to check if the RPC group is dynamic and lock the
|
| 434 |
+
// mutex if so
|
| 435 |
+
struct GroupMembershipLockGuard {
|
| 436 |
+
GroupMembershipLockGuard(std::mutex& mutex, bool isStaticGroup)
|
| 437 |
+
: ref_(mutex), isStaticGroup_(isStaticGroup) {
|
| 438 |
+
if (isStaticGroup_) {
|
| 439 |
+
ref_.lock();
|
| 440 |
+
}
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
~GroupMembershipLockGuard() {
|
| 444 |
+
if (isStaticGroup_) {
|
| 445 |
+
ref_.unlock();
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
GroupMembershipLockGuard(const GroupMembershipLockGuard&) = delete;
|
| 450 |
+
|
| 451 |
+
private:
|
| 452 |
+
std::mutex& ref_;
|
| 453 |
+
bool isStaticGroup_;
|
| 454 |
+
};
|
| 455 |
+
// Mutex to guard access to group membership data
|
| 456 |
+
// e.g. updates to (workerIdToInfo_, workerNameToInfo_, workerNameToURL_)
|
| 457 |
+
mutable std::mutex groupMembershipMutex_;
|
| 458 |
+
|
| 459 |
+
// Map to Track Network Data
|
| 460 |
+
NetworkDataDict networkData_;
|
| 461 |
+
// Mutex to guard networkData_
|
| 462 |
+
std::mutex networkDataMutex_;
|
| 463 |
+
|
| 464 |
+
// A mutex and a cv to guard access to the call counts and watch for changes.
|
| 465 |
+
std::mutex callCountMutex_;
|
| 466 |
+
std::condition_variable callCountCV_;
|
| 467 |
+
// Running total of un-processed, un-errored RPC calls sent
|
| 468 |
+
int32_t clientActiveCalls_{0};
|
| 469 |
+
// Running total of un-processed RPC requests received
|
| 470 |
+
int32_t serverActiveCalls_{0};
|
| 471 |
+
// Running total of RPC requests that will be completed asynchronously
|
| 472 |
+
int32_t serverActiveAsyncCalls_{0};
|
| 473 |
+
|
| 474 |
+
// Whether a global graceful shutdown has begun, in which case we'll silence
|
| 475 |
+
// error messages due to remote workers closing their pipes.
|
| 476 |
+
std::atomic<bool> shuttingDown_{false};
|
| 477 |
+
|
| 478 |
+
// Helpers to modify the counts while correctly dealing with the mutex and cv.
|
| 479 |
+
void increaseCallCount(int32_t& count);
|
| 480 |
+
void decreaseCallCount(int32_t& count);
|
| 481 |
+
|
| 482 |
+
// Helpers to set the state of the requests.
|
| 483 |
+
void markFutureAsComplete(
|
| 484 |
+
std::shared_ptr<AtomicJitFuture> atomicFuture,
|
| 485 |
+
c10::intrusive_ptr<Message> message,
|
| 486 |
+
std::vector<c10::Stream> streams);
|
| 487 |
+
void markFutureWithError(
|
| 488 |
+
std::shared_ptr<AtomicJitFuture> atomicFuture,
|
| 489 |
+
std::string errorMsg);
|
| 490 |
+
};
|
| 491 |
+
|
| 492 |
+
} // namespace torch::distributed::rpc
|
| 493 |
+
|
| 494 |
+
#endif // USE_TENSORPIPE
|
| 495 |
+
|
| 496 |
+
#else
|
| 497 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 498 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/tensorpipe_utils.h
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef USE_TENSORPIPE
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/distributed/rpc/utils.h>
|
| 7 |
+
|
| 8 |
+
namespace tensorpipe {
|
| 9 |
+
class Message;
|
| 10 |
+
class Allocation;
|
| 11 |
+
class Descriptor;
|
| 12 |
+
} // namespace tensorpipe
|
| 13 |
+
|
| 14 |
+
namespace torch::distributed::rpc {
|
| 15 |
+
|
| 16 |
+
TORCH_API const c10::Stream& getStreamForDevice(
|
| 17 |
+
const std::vector<c10::Stream>& streams,
|
| 18 |
+
const c10::Device& device);
|
| 19 |
+
|
| 20 |
+
// Inspired by c10/core/impl/DeviceGuardImplInterface.h.
|
| 21 |
+
|
| 22 |
+
class TensorpipeDeviceTypeConverter {
|
| 23 |
+
public:
|
| 24 |
+
// Ideally we'd want this to also return a tensorpipe::Message::Tensor object
|
| 25 |
+
// but we cannot forward-declare that class (because it's nested), and we
|
| 26 |
+
// cannot include the TensorPipe headers because it's a private dependency.
|
| 27 |
+
// Thus we bend over backwards and entrust this method with appending that
|
| 28 |
+
// object to the `tensors` field of the tensorpipe::Message object we pass.
|
| 29 |
+
virtual std::optional<std::vector<char>> prepareTensorForSending(
|
| 30 |
+
const c10::Storage& storage,
|
| 31 |
+
const std::vector<c10::Stream>& streams,
|
| 32 |
+
tensorpipe::Message& message) const = 0;
|
| 33 |
+
|
| 34 |
+
// Same as above: this method cannot return a tensorpipe::Allocation::Tensor,
|
| 35 |
+
// thus it appends it to the `tensors` field of the tensorpipe::Allocation.
|
| 36 |
+
virtual at::DataPtr allocateTensorForReceiving(
|
| 37 |
+
c10::DeviceIndex deviceIndex,
|
| 38 |
+
size_t length,
|
| 39 |
+
const std::vector<c10::Stream>& streams,
|
| 40 |
+
tensorpipe::Allocation& allocation) const = 0;
|
| 41 |
+
|
| 42 |
+
virtual ~TensorpipeDeviceTypeConverter() = default;
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
extern TORCH_API std::array<
|
| 46 |
+
std::atomic<const TensorpipeDeviceTypeConverter*>,
|
| 47 |
+
static_cast<size_t>(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)>
|
| 48 |
+
device_type_converter_registry;
|
| 49 |
+
|
| 50 |
+
class TORCH_API TensorpipeDeviceTypeConverterRegistrar {
|
| 51 |
+
public:
|
| 52 |
+
TensorpipeDeviceTypeConverterRegistrar(
|
| 53 |
+
DeviceType /*type*/,
|
| 54 |
+
const TensorpipeDeviceTypeConverter* /*impl*/);
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
#define C10_REGISTER_TENSORPIPE_DEVICE_TYPE_CONVERTER( \
|
| 58 |
+
DevType, TensorpipeDeviceTypeConverter) \
|
| 59 |
+
static ::torch::distributed::rpc::TensorpipeDeviceTypeConverterRegistrar \
|
| 60 |
+
C10_ANONYMOUS_VARIABLE(g_##DeviceType)( \
|
| 61 |
+
::c10::DeviceType::DevType, new TensorpipeDeviceTypeConverter());
|
| 62 |
+
|
| 63 |
+
inline const TensorpipeDeviceTypeConverter* getDeviceTypeConverter(
|
| 64 |
+
DeviceType type) {
|
| 65 |
+
return device_type_converter_registry[static_cast<size_t>(type)].load();
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// A struct that holds pointers that keep alive all the memory that will be
|
| 69 |
+
// accessed by TensorPipe during a write operation.
|
| 70 |
+
struct TensorpipeWriteBuffers {
|
| 71 |
+
// Allocate on heap so pointers stay valid as we move the holder.
|
| 72 |
+
std::unique_ptr<MessageType> type;
|
| 73 |
+
std::unique_ptr<int64_t> id;
|
| 74 |
+
std::vector<char> payload;
|
| 75 |
+
std::vector<char> pickle;
|
| 76 |
+
// This contains the original tensors and the clones of the sparse tensors.
|
| 77 |
+
std::vector<torch::Tensor> tensors;
|
| 78 |
+
// This contains the copies of the data of the tensors that didn't own their
|
| 79 |
+
// memory, e.g., the ones created from torch::from_blob() with no deleter.
|
| 80 |
+
std::vector<std::vector<char>> copiedTensors;
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
// A struct that holds pointers that keep alive all the memory that will be
|
| 84 |
+
// accessed by TensorPipe during a read operation.
|
| 85 |
+
struct TensorpipeReadBuffers {
|
| 86 |
+
// Allocate on heap so pointers stay valid as we move the holder.
|
| 87 |
+
std::unique_ptr<MessageType> type;
|
| 88 |
+
std::unique_ptr<int64_t> id;
|
| 89 |
+
std::vector<char> payload;
|
| 90 |
+
std::vector<char> pickle;
|
| 91 |
+
std::vector<c10::DataPtr> tensors;
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
// Convert an RPC message into a TensorPipe message, plus a holder to all the
|
| 95 |
+
// data that must be kept alive while the write is performed asynchronously.
|
| 96 |
+
TORCH_API std::tuple<tensorpipe::Message, TensorpipeWriteBuffers>
|
| 97 |
+
tensorpipeSerialize(
|
| 98 |
+
const c10::intrusive_ptr<Message>& rpcMessage,
|
| 99 |
+
std::vector<c10::Device> devices,
|
| 100 |
+
const std::vector<c10::Stream>& streams);
|
| 101 |
+
|
| 102 |
+
// Allocate the buffers that will hold the incoming data. They will be managed
|
| 103 |
+
// by the returned holder, which must be kept alive until the asynchronous read
|
| 104 |
+
// has finished. Pointers to these buffers will be stored in the returned
|
| 105 |
+
// tensorpipe::Allocation struct.
|
| 106 |
+
TORCH_API std::pair<tensorpipe::Allocation, TensorpipeReadBuffers>
|
| 107 |
+
tensorpipeAllocate(
|
| 108 |
+
const tensorpipe::Descriptor& tpDescriptor,
|
| 109 |
+
const std::vector<c10::Stream>& streams);
|
| 110 |
+
|
| 111 |
+
// Convert a TensorPipe message back into an RPC message. This requires the data
|
| 112 |
+
// to be available and can thus only be performed once the asynchronous read has
|
| 113 |
+
// completed. The holder can be destroyed once this function returns.
|
| 114 |
+
TORCH_API c10::intrusive_ptr<Message> tensorpipeDeserialize(
|
| 115 |
+
const tensorpipe::Descriptor& tpDescriptor,
|
| 116 |
+
TensorpipeReadBuffers&& holder);
|
| 117 |
+
|
| 118 |
+
} // namespace torch::distributed::rpc
|
| 119 |
+
|
| 120 |
+
#endif // USE_TENSORPIPE
|
| 121 |
+
|
| 122 |
+
#else
|
| 123 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 124 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/testing/faulty_tensorpipe_agent.h
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef USE_TENSORPIPE
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/distributed/rpc/message.h>
|
| 7 |
+
#include <torch/csrc/distributed/rpc/tensorpipe_agent.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::distributed::rpc {
|
| 10 |
+
|
| 11 |
+
struct TORCH_API FaultyTensorPipeRpcBackendOptions
|
| 12 |
+
: public TensorPipeRpcBackendOptions {
|
| 13 |
+
FaultyTensorPipeRpcBackendOptions(
|
| 14 |
+
int num_worker_threads,
|
| 15 |
+
float rpc_timeout,
|
| 16 |
+
std::string init_method,
|
| 17 |
+
std::vector<std::string> messages_to_fail,
|
| 18 |
+
std::unordered_map<std::string, float> messages_to_delay,
|
| 19 |
+
int num_fail_sends = 0)
|
| 20 |
+
: TensorPipeRpcBackendOptions(
|
| 21 |
+
num_worker_threads,
|
| 22 |
+
std::optional<std::vector<std::string>>(),
|
| 23 |
+
std::optional<std::vector<std::string>>(),
|
| 24 |
+
rpc_timeout,
|
| 25 |
+
std::move(init_method)),
|
| 26 |
+
messagesToFail(std::move(messages_to_fail)),
|
| 27 |
+
messagesToDelay(std::move(messages_to_delay)),
|
| 28 |
+
numFailSends(num_fail_sends) {
|
| 29 |
+
TORCH_CHECK(numFailSends >= 0, "numFailSends should be non-negative");
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
std::vector<std::string> messagesToFail;
|
| 33 |
+
std::unordered_map<std::string, float> messagesToDelay;
|
| 34 |
+
int numFailSends;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
class TORCH_API FaultyTensorPipeAgent : public TensorPipeAgent {
|
| 38 |
+
public:
|
| 39 |
+
FaultyTensorPipeAgent(
|
| 40 |
+
const c10::intrusive_ptr<::c10d::Store>& store,
|
| 41 |
+
std::string selfName,
|
| 42 |
+
worker_id_t selfId,
|
| 43 |
+
int worldSize,
|
| 44 |
+
FaultyTensorPipeRpcBackendOptions opts,
|
| 45 |
+
std::unordered_map<std::string, DeviceMap> reverseDeviceMaps,
|
| 46 |
+
std::vector<c10::Device> devices,
|
| 47 |
+
std::unique_ptr<RequestCallback> callback);
|
| 48 |
+
|
| 49 |
+
// Faulty send function for this class.
|
| 50 |
+
c10::intrusive_ptr<JitFuture> send(
|
| 51 |
+
const WorkerInfo& to,
|
| 52 |
+
c10::intrusive_ptr<Message> message,
|
| 53 |
+
const float rpcTimeoutSeconds = torch::distributed::rpc::kUnsetRpcTimeout,
|
| 54 |
+
const DeviceMap& deviceMap = {}) override;
|
| 55 |
+
|
| 56 |
+
// Add delay to writes
|
| 57 |
+
void pipeWrite(
|
| 58 |
+
const std::shared_ptr<tensorpipe::Pipe>& pipe,
|
| 59 |
+
const c10::intrusive_ptr<Message>& rpcMessage,
|
| 60 |
+
std::vector<c10::Device>&& devices,
|
| 61 |
+
std::vector<c10::Stream> streams,
|
| 62 |
+
std::function<void(const tensorpipe::Error&)> fn) noexcept override;
|
| 63 |
+
|
| 64 |
+
protected:
|
| 65 |
+
// This function checks the messageTypesToFail_ to determine whether to use
|
| 66 |
+
// the faulty send or not.
|
| 67 |
+
bool shouldFailMessage(MessageType type) const;
|
| 68 |
+
|
| 69 |
+
private:
|
| 70 |
+
// This function parses the list of strings passed in by the python tests and
|
| 71 |
+
// resolves the Message Types that must use the faulty send.
|
| 72 |
+
std::vector<MessageType> parseMessagesToFailInput(
|
| 73 |
+
const std::vector<std::string>& messagesToFail) const;
|
| 74 |
+
|
| 75 |
+
// Returns amount of time in seconds to delay sending of the given message
|
| 76 |
+
// type.
|
| 77 |
+
float getDelayForMessage(MessageType type) const;
|
| 78 |
+
|
| 79 |
+
// Parse message types that we should inject arbitrary delays for.
|
| 80 |
+
std::unordered_map<MessageType, float, std::hash<int>> parseMessagesToDelay(
|
| 81 |
+
const std::unordered_map<std::string, float>& messageTypesToDelay) const;
|
| 82 |
+
|
| 83 |
+
// Number of sends to intentionally fail before allowing one to succeed.
|
| 84 |
+
const int numFailSends_;
|
| 85 |
+
|
| 86 |
+
// Vector of the MessageTypes that we must use the faulty send for. This is
|
| 87 |
+
// parsed based on a list of strings passed in by the python tests.
|
| 88 |
+
const std::vector<MessageType> messageTypesToFail_;
|
| 89 |
+
|
| 90 |
+
// Mapping of message types to amount we should delay send for in the ::send()
|
| 91 |
+
// function.
|
| 92 |
+
std::unordered_map<MessageType, float, std::hash<int>> messageTypesToDelay_;
|
| 93 |
+
|
| 94 |
+
// Map to track the number of sends we've failed for each RPC.
|
| 95 |
+
std::unordered_map<std::string, int> failMessageCountMap_;
|
| 96 |
+
|
| 97 |
+
// Mutex to guard failMessageCountMap_
|
| 98 |
+
std::mutex failMapMutex_;
|
| 99 |
+
|
| 100 |
+
MessageType messageStringToType(const std::string& messageString) const;
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
} // namespace torch::distributed::rpc
|
| 104 |
+
|
| 105 |
+
#endif // USE_TENSORPIPE
|
| 106 |
+
|
| 107 |
+
#else
|
| 108 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 109 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/testing/testing.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/python_headers.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed::rpc::testing {
|
| 7 |
+
|
| 8 |
+
PyMethodDef* python_functions();
|
| 9 |
+
|
| 10 |
+
} // namespace torch::distributed::rpc::testing
|
| 11 |
+
|
| 12 |
+
#else
|
| 13 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 14 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/torchscript_functions.h
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
#include <torch/csrc/autograd/profiler.h>
|
| 6 |
+
#include <torch/csrc/distributed/autograd/utils.h>
|
| 7 |
+
#include <torch/csrc/distributed/rpc/rref_context.h>
|
| 8 |
+
#include <torch/csrc/distributed/rpc/script_remote_call.h>
|
| 9 |
+
|
| 10 |
+
namespace torch::distributed::rpc {
|
| 11 |
+
|
| 12 |
+
// This function sends an rpc call to run torchscript function, currently the
|
| 13 |
+
// torchscript function could only be a user defined python function with
|
| 14 |
+
// "@torch.jit.script" annotation. The torchscript function could not be
|
| 15 |
+
// a class constructor, class method, instance method or a script module.
|
| 16 |
+
// dst: destination worker name
|
| 17 |
+
// qualifiedName: torchscript function qualified name string like
|
| 18 |
+
// "moduleName::torchscriptFunctionName", e.g,
|
| 19 |
+
// "dist_autograd_test::my_py_add"
|
| 20 |
+
// stack: a bag of IValue args passed to torchscriptFunctionName
|
| 21 |
+
// It returns c10::intrusive_ptr<ivalue::Future>
|
| 22 |
+
c10::intrusive_ptr<c10::ivalue::Future> TORCH_API rpcTorchscript(
|
| 23 |
+
const std::string& dstWorkerName,
|
| 24 |
+
const c10::QualifiedName& qualifiedName,
|
| 25 |
+
const c10::FunctionSchema& functionSchema,
|
| 26 |
+
std::vector<c10::IValue> stack,
|
| 27 |
+
const float rpcTimeoutSeconds = torch::distributed::rpc::kUnsetRpcTimeout,
|
| 28 |
+
const bool isAsyncExecution = false);
|
| 29 |
+
|
| 30 |
+
c10::intrusive_ptr<RRef> TORCH_API remoteTorchscript(
|
| 31 |
+
const std::string& dstWorkerName,
|
| 32 |
+
const c10::QualifiedName& qualifiedName,
|
| 33 |
+
const c10::FunctionSchema& functionSchema,
|
| 34 |
+
std::vector<c10::IValue>& stack,
|
| 35 |
+
const float rpcTimeoutSeconds = torch::distributed::rpc::kUnsetRpcTimeout,
|
| 36 |
+
const bool isAsyncExecution = false);
|
| 37 |
+
|
| 38 |
+
} // namespace torch::distributed::rpc
|
| 39 |
+
|
| 40 |
+
#else
|
| 41 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 42 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/lib/python3.12/site-packages/torch/include/torch/csrc/distributed/rpc/types.h
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::distributed::rpc {
|
| 7 |
+
|
| 8 |
+
using worker_id_t = int16_t;
|
| 9 |
+
using local_id_t = int64_t;
|
| 10 |
+
|
| 11 |
+
bool getAllowJitRRefPickle();
|
| 12 |
+
TORCH_API void enableJitRRefPickle();
|
| 13 |
+
TORCH_API void disableJitRRefPickle();
|
| 14 |
+
|
| 15 |
+
struct TORCH_API JitRRefPickleGuard {
|
| 16 |
+
JitRRefPickleGuard();
|
| 17 |
+
JitRRefPickleGuard(JitRRefPickleGuard&& other) = delete;
|
| 18 |
+
JitRRefPickleGuard(const JitRRefPickleGuard&) = delete;
|
| 19 |
+
JitRRefPickleGuard& operator=(const JitRRefPickleGuard&) = delete;
|
| 20 |
+
JitRRefPickleGuard& operator=(JitRRefPickleGuard&&) = delete;
|
| 21 |
+
~JitRRefPickleGuard();
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
struct TORCH_API GloballyUniqueId final {
|
| 25 |
+
GloballyUniqueId(worker_id_t createdOn, local_id_t localId);
|
| 26 |
+
GloballyUniqueId(const GloballyUniqueId& other) = default;
|
| 27 |
+
GloballyUniqueId& operator=(const GloballyUniqueId& other) = delete;
|
| 28 |
+
GloballyUniqueId(GloballyUniqueId&& other) = default;
|
| 29 |
+
GloballyUniqueId& operator=(GloballyUniqueId&& other) = delete;
|
| 30 |
+
~GloballyUniqueId() = default;
|
| 31 |
+
|
| 32 |
+
bool operator==(const GloballyUniqueId& other) const;
|
| 33 |
+
bool operator!=(const GloballyUniqueId& other) const;
|
| 34 |
+
|
| 35 |
+
at::IValue toIValue() const;
|
| 36 |
+
static GloballyUniqueId fromIValue(const at::IValue& /*ivalue*/);
|
| 37 |
+
|
| 38 |
+
struct Hash {
|
| 39 |
+
size_t operator()(const GloballyUniqueId& key) const {
|
| 40 |
+
return (uint64_t(key.createdOn_) << kLocalIdBits) | key.localId_;
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
static constexpr int kLocalIdBits = 48;
|
| 45 |
+
|
| 46 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 47 |
+
const worker_id_t createdOn_;
|
| 48 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 49 |
+
const local_id_t localId_;
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
TORCH_API std::ostream& operator<<(
|
| 53 |
+
std::ostream& os,
|
| 54 |
+
const GloballyUniqueId& globalId);
|
| 55 |
+
|
| 56 |
+
using RRefId = GloballyUniqueId;
|
| 57 |
+
using ForkId = GloballyUniqueId;
|
| 58 |
+
using ProfilingId = GloballyUniqueId;
|
| 59 |
+
|
| 60 |
+
struct TORCH_API SerializedPyObj final {
|
| 61 |
+
SerializedPyObj(std::string&& payload, std::vector<at::Tensor>&& tensors)
|
| 62 |
+
: payload_(std::move(payload)), tensors_(std::move(tensors)) {}
|
| 63 |
+
|
| 64 |
+
std::vector<at::IValue> toIValues() &&;
|
| 65 |
+
static SerializedPyObj fromIValues(std::vector<at::IValue> value);
|
| 66 |
+
|
| 67 |
+
std::string payload_;
|
| 68 |
+
std::vector<at::Tensor> tensors_;
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
} // namespace torch::distributed::rpc
|
| 72 |
+
|
| 73 |
+
#else
|
| 74 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 75 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|