Initial import: grid-bot — grid trading bot for BTC-USDT on Cifra Markets

This commit is contained in:
Kolp
2026-09-24 13:22:23 +07:00
commit 642cc11a9f
18968 changed files with 5683248 additions and 0 deletions
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/channel/context.h>
namespace tensorpipe {
namespace channel {
namespace basic {
std::shared_ptr<Context> create();
} // namespace basic
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/channel/context.h>
namespace tensorpipe {
namespace channel {
namespace cma {
std::shared_ptr<Context> create();
} // namespace cma
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,113 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <tensorpipe/common/buffer.h>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
namespace channel {
enum class Endpoint : bool { kConnect, kListen };
class Channel;
// Abstract base class for channel context classes.
//
// Instances of these classes are expected to be registered with a
// context. All registered instances are assumed to be eligible
// channels for all pairs.
//
class Context {
public:
// Return whether the context is able to operate correctly.
//
// Some channel types may be unable to perform as intended under some
// circumstances (e.g., specialized hardware unavailable, lack of
// permissions). They can report it through this method in order for
// the core context to avoid registering them in the first place.
//
virtual bool isViable() const = 0;
// Return the number of control connections needed to create an instance of
// this channel.
//
// Most channels require only one, but some require more (cuda_basic), and
// some might require none.
//
virtual size_t numConnectionsNeeded() const = 0;
// Return a map from supported devices to strings describing the device from
// the channel's perspective.
//
// Two processes with a channel context of the same type can leverage this
// channel to make two devices communicate if one side's device descriptor is
// "accepted" by the other one, using the canCommunicateWithRemote method
// below. That method must be symmetric, and unless overridden defaults to
// string comparison.
//
virtual const std::unordered_map<Device, std::string>& deviceDescriptors()
const = 0;
// Compare local and remote device descriptors for compatibility.
//
// Determine whether a channel can be opened between a local device and
// a remote one that has the given device descriptor. This function
// needs to be symmetric: if we called this method on the remote
// context with the local descriptor we should get the same answer.
// Unless overridden it defaults to string comparison.
//
virtual bool canCommunicateWithRemote(
const std::string& localDeviceDescriptor,
const std::string& remoteDeviceDescriptor) const = 0;
// Return newly created channel using the specified connections.
//
// It is up to the channel to either use these connections for further
// initialization, or use them directly. Either way, the returned
// channel should be immediately usable. If the channel isn't fully
// initialized yet, take care to queue these operations to execute
// as soon as initialization has completed.
//
virtual std::shared_ptr<Channel> createChannel(
std::vector<std::shared_ptr<transport::Connection>>,
Endpoint) = 0;
// Tell the context what its identifier is.
//
// This is only supposed to be called from the high-level context. It will
// only used for logging and debugging purposes.
virtual void setId(std::string id) = 0;
// Put the channel context in a terminal state, in turn closing all of its
// channels, and release its resources. This may be done asynchronously, in
// background.
virtual void close() = 0;
// Wait for all resources to be released and all background activity to stop.
virtual void join() = 0;
virtual ~Context() = default;
private:
std::string name_;
};
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,45 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tensorpipe/common/error.h>
namespace tensorpipe {
namespace channel {
class ContextClosedError final : public BaseError {
public:
ContextClosedError() {}
std::string what() const override;
};
class ChannelClosedError final : public BaseError {
public:
ChannelClosedError() {}
std::string what() const override;
};
class ContextNotViableError final : public BaseError {
public:
ContextNotViableError() {}
std::string what() const override;
};
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,32 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <vector>
#include <tensorpipe/channel/context.h>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
namespace channel {
namespace mpt {
std::shared_ptr<Context> create(
std::vector<std::shared_ptr<transport::Context>> contexts,
std::vector<std::shared_ptr<transport::Listener>> listeners);
} // namespace mpt
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/channel/context.h>
namespace tensorpipe {
namespace channel {
namespace xth {
std::shared_ptr<Context> create();
} // namespace xth
} // namespace channel
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,140 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <tensorpipe/common/cpu_buffer.h>
#include <tensorpipe/common/device.h>
namespace tensorpipe {
class Buffer {
class AbstractBufferWrapper {
public:
virtual Device device() const = 0;
virtual void copyConstructInto(void* ptr) const = 0;
virtual void moveConstructInto(void* ptr) = 0;
virtual ~AbstractBufferWrapper() = default;
};
template <typename TBuffer>
class BufferWrapper : public AbstractBufferWrapper {
static_assert(
std::is_trivially_copyable<TBuffer>::value,
"wrapping non-trivially copyable class");
public:
TBuffer buffer;
explicit BufferWrapper(TBuffer buffer) : buffer(std::move(buffer)) {}
Device device() const override {
return buffer.getDevice();
}
void copyConstructInto(void* ptr) const override {
new (ptr) BufferWrapper(*this);
}
void moveConstructInto(void* ptr) override {
new (ptr) BufferWrapper(std::move(*this));
}
};
public:
template <typename TBuffer>
/* implicit */ Buffer(TBuffer b) {
static_assert(
sizeof(BufferWrapper<TBuffer>) <= kStructSize, "kStructSize too small");
static_assert(
alignof(BufferWrapper<TBuffer>) <= kStructAlign,
"kStructAlign too small");
new (&raw_) BufferWrapper<TBuffer>(std::move(b));
}
Buffer() : Buffer(CpuBuffer{}) {}
Buffer(const Buffer& other) {
other.ptr()->copyConstructInto(&raw_);
}
Buffer& operator=(const Buffer& other) {
if (this != &other) {
ptr()->~AbstractBufferWrapper();
other.ptr()->copyConstructInto(&raw_);
}
return *this;
}
Buffer(Buffer&& other) noexcept {
other.ptr()->moveConstructInto(&raw_);
}
Buffer& operator=(Buffer&& other) {
if (this != &other) {
ptr()->~AbstractBufferWrapper();
other.ptr()->moveConstructInto(&raw_);
}
return *this;
}
~Buffer() {
ptr()->~AbstractBufferWrapper();
}
template <typename TBuffer>
TBuffer& unwrap() {
BufferWrapper<TBuffer>* wrapperPtr =
dynamic_cast<BufferWrapper<TBuffer>*>(ptr());
if (wrapperPtr == nullptr) {
throw std::runtime_error("Invalid unwrapping of tensorpipe::Buffer");
}
return wrapperPtr->buffer;
}
template <typename TBuffer>
const TBuffer& unwrap() const {
const BufferWrapper<TBuffer>* wrapperPtr =
dynamic_cast<const BufferWrapper<TBuffer>*>(ptr());
if (wrapperPtr == nullptr) {
throw std::runtime_error("Invalid unwrapping of tensorpipe::Buffer");
}
return wrapperPtr->buffer;
}
Device device() const {
return ptr()->device();
}
private:
static constexpr int kStructSize = 32;
static constexpr int kStructAlign = 8;
std::aligned_storage<kStructSize, kStructAlign>::type raw_{};
const AbstractBufferWrapper* ptr() const {
// FIXME: Once we go C++17, use std::launder on the returned pointer.
return reinterpret_cast<const AbstractBufferWrapper*>(&raw_);
}
AbstractBufferWrapper* ptr() {
// FIXME: Once we go C++17, use std::launder on the returned pointer.
return reinterpret_cast<AbstractBufferWrapper*>(&raw_);
}
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <tensorpipe/common/device.h>
namespace tensorpipe {
struct CpuBuffer {
void* ptr{nullptr};
Device getDevice() const {
return Device{kCpuDeviceType, 0};
}
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,69 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <sstream>
#include <stdexcept>
#include <string>
namespace tensorpipe {
const std::string kCpuDeviceType{"cpu"};
const std::string kCudaDeviceType{"cuda"};
struct Device {
std::string type;
int index;
// This pointless constructor is needed to work around a bug in GCC 5.5 (and
// possibly other versions). It appears to be needed in the nop types that
// are used inside nop::Optional.
Device() {}
Device(std::string type, int index) : type(std::move(type)), index(index) {}
std::string toString() const {
std::stringstream ss;
ss << type << ":" << index;
return ss.str();
}
bool operator==(const Device& other) const {
return type == other.type && index == other.index;
}
};
} // namespace tensorpipe
namespace std {
template <>
struct hash<::tensorpipe::Device> {
size_t operator()(const ::tensorpipe::Device& device) const noexcept {
return std::hash<std::string>{}(device.toString());
}
};
template <>
struct hash<std::pair<::tensorpipe::Device, ::tensorpipe::Device>> {
size_t operator()(const std::pair<::tensorpipe::Device, ::tensorpipe::Device>&
p) const noexcept {
size_t h1 = std::hash<::tensorpipe::Device>{}(p.first);
size_t h2 = std::hash<::tensorpipe::Device>{}(p.second);
// Shifting one hash to avoid collisions between (a, b) and (b, a).
return h1 ^ (h2 << 1);
}
};
} // namespace std
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,132 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <string>
namespace tensorpipe {
// Base class for actual errors.
class BaseError {
public:
virtual ~BaseError() = default;
// Returns an explanatory string.
// Like `std::exception` but returns a `std::string`.
virtual std::string what() const = 0;
};
// Wrapper class for errors.
//
// Background: we wish to not use exceptions yet need an error
// representation that can propagate across function and thread
// boundaries. This representation must be copyable (so we can store
// and return it at a later point in time) and retain downstream type
// information. This implies a heap allocation because it's the
// easiest way to deal with variable size objects (barring a union of
// all downstream error classes and a lot of custom code). Instead of
// passing a shared_ptr around directly, we use this wrapper class to
// keep implementation details hidden from calling code.
//
class Error final {
public:
// Constant instance that indicates success.
static const Error kSuccess;
// Default constructor for error that is not an error.
Error() {}
Error(std::shared_ptr<BaseError> error, std::string file, int line)
: error_(std::move(error)), file_(std::move(file)), line_(line) {}
~Error() = default;
// Converting to boolean means checking if there is an error. This
// means we don't need to use an `std::optional` and allows for a
// snippet like the following:
//
// if (error) {
// // Deal with it.
// }
//
operator bool() const {
return static_cast<bool>(error_);
}
template <typename T>
std::shared_ptr<T> castToType() const {
return std::dynamic_pointer_cast<T>(error_);
}
template <typename T>
bool isOfType() const {
return castToType<T>() != nullptr;
}
// Like `std::exception` but returns a `std::string`.
std::string what() const;
private:
std::shared_ptr<BaseError> error_;
std::string file_;
int line_;
};
class SystemError final : public BaseError {
public:
explicit SystemError(const char* syscall, int error)
: syscall_(syscall), error_(error) {}
std::string what() const override;
int errorCode() const;
private:
const char* syscall_;
const int error_;
};
class ShortReadError final : public BaseError {
public:
ShortReadError(ssize_t expected, ssize_t actual)
: expected_(expected), actual_(actual) {}
std::string what() const override;
private:
const ssize_t expected_;
const ssize_t actual_;
};
class ShortWriteError final : public BaseError {
public:
ShortWriteError(ssize_t expected, ssize_t actual)
: expected_(expected), actual_(actual) {}
std::string what() const override;
private:
const ssize_t expected_;
const ssize_t actual_;
};
class EOFError final : public BaseError {
public:
EOFError() {}
std::string what() const override;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,16 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <optional>
namespace tensorpipe {
using std::optional;
using std::nullopt;
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,19 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#define TENSORPIPE_HAS_SHM_TRANSPORT 1
#define TENSORPIPE_HAS_IBV_TRANSPORT 1
#define TENSORPIPE_HAS_CMA_CHANNEL 1
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,101 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <tensorpipe/transport/context.h>
#include <tensorpipe/channel/context.h>
namespace tensorpipe {
class ContextImpl;
class Listener;
class Pipe;
class ContextOptions {
public:
// The name should be a semantically meaningful description of this context.
// It will only be used for logging and debugging purposes, to identify the
// endpoints of a pipe.
ContextOptions&& name(std::string name) && {
name_ = std::move(name);
return std::move(*this);
}
private:
std::string name_;
friend ContextImpl;
};
class PipeOptions {
public:
// The name should be a semantically meaningful description of the context
// that the pipe is connecting to. It will only be used for logging and
// debugging purposes, to identify the endpoints of a pipe.
PipeOptions&& remoteName(std::string remoteName) && {
remoteName_ = std::move(remoteName);
return std::move(*this);
}
private:
std::string remoteName_;
friend ContextImpl;
};
class Context final {
public:
explicit Context(ContextOptions opts = ContextOptions());
void registerTransport(
int64_t priority,
std::string transport,
std::shared_ptr<transport::Context> context);
void registerChannel(
int64_t priority,
std::string channel,
std::shared_ptr<channel::Context> context);
std::shared_ptr<Listener> listen(const std::vector<std::string>& urls);
std::shared_ptr<Pipe> connect(
const std::string& url,
PipeOptions opts = PipeOptions());
// Put the context in a terminal state, in turn closing all of its pipes and
// listeners, and release its resources. This may be done asynchronously, in
// background.
void close();
// Wait for all resources to be released and all background activity to stop.
void join();
~Context();
private:
// The implementation is managed by a shared_ptr because each child object
// will also hold a shared_ptr to it. However, its lifetime is tied to the one
// of this public object since when the latter is destroyed the implementation
// is closed and joined.
const std::shared_ptr<ContextImpl> impl_;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,53 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tensorpipe/common/error.h>
namespace tensorpipe {
class LogicError final : public BaseError {
public:
explicit LogicError(std::string reason) : reason_(std::move(reason)) {}
std::string what() const override;
private:
const std::string reason_;
};
class ContextClosedError final : public BaseError {
public:
explicit ContextClosedError() {}
std::string what() const override;
};
class ListenerClosedError final : public BaseError {
public:
explicit ListenerClosedError() {}
std::string what() const override;
};
class PipeClosedError final : public BaseError {
public:
explicit PipeClosedError() {}
std::string what() const override;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,101 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <tensorpipe/common/error.h>
namespace tensorpipe {
class ContextImpl;
class ListenerImpl;
class Pipe;
// The listener.
//
// Listeners are used to produce pipes. Depending on the type of the
// context, listeners may use a variety of addresses to listen on. For
// example, for TCP/IP sockets they listen on an IPv4 or IPv6 address,
// for Unix domain sockets they listen on a path, etcetera.
//
// A pipe can only be accepted from this listener after it has been
// fully established. This means that both its connection and all its
// side channels have been established.
//
class Listener final {
// Use the passkey idiom to allow make_shared to call what should be a private
// constructor. See https://abseil.io/tips/134 for more information.
struct ConstructorToken {};
public:
Listener(
ConstructorToken token,
std::shared_ptr<ContextImpl> context,
std::string id,
const std::vector<std::string>& urls);
//
// Entry points for user code
//
using accept_callback_fn =
std::function<void(const Error&, std::shared_ptr<Pipe>)>;
void accept(accept_callback_fn fn);
// Returns map with the materialized address of listeners by transport.
//
// If you don't bind a transport listener to a specific port or address, it
// may generate its address automatically. Then, in order to connect to the
// listener, the user must use a separate mechanism to communicate the
// materialized address to whoever wants to connect.
//
const std::map<std::string, std::string>& addresses() const;
// Returns materialized address for specific transport.
//
// See `addresses()` for more information.
//
const std::string& address(const std::string& transport) const;
// Returns URL with materialized address for specific transport.
//
// See `addresses()` for more information.
//
std::string url(const std::string& transport) const;
// Put the listener in a terminal state, aborting its pending operations and
// rejecting future ones, and release its resrouces. This may be carried out
// asynchronously, in background. Since the pipes may occasionally use the
// listener to open new connections, closing a listener may trigger errors
// in the pipes.
void close();
~Listener();
private:
// Using a shared_ptr allows us to detach the lifetime of the implementation
// from the public object's one and perform the destruction asynchronously.
const std::shared_ptr<ListenerImpl> impl_;
// Allow context to access constructor token.
friend ContextImpl;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,109 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <cstddef>
#include <string>
#include <vector>
#include <tensorpipe/common/buffer.h>
#include <tensorpipe/common/optional.h>
namespace tensorpipe {
// Messages consist of a primary buffer and zero or more separate
// buffers. The primary buffer is always a host-side memory region that
// contains a serialized version of the message we're dealing with. This
// serialized message, in turn, may have references to the separate
// buffers that accompany the primary buffer. These separate buffers may
// point to any type of memory, host-side or device-side.
//
class Message final {
public:
std::string metadata;
struct Payload {
void* data{nullptr};
size_t length{0};
// Users may include arbitrary metadata in the following fields.
// This may contain allocation hints for the receiver, for example.
std::string metadata;
};
// Holds the payloads that are transferred over the primary connection.
std::vector<Payload> payloads;
struct Tensor {
tensorpipe::Buffer buffer;
size_t length{0};
// Users may optionally specify the target device, on which the receiver
// should allocate memory for this tensor. If left unset, the receiver will
// choose one at their convenience.
optional<Device> targetDevice;
// Users may include arbitrary metadata in the following field.
// This may contain allocation hints for the receiver, for example.
std::string metadata;
};
// Holds the tensors that are offered to the side channels.
std::vector<Tensor> tensors;
};
// Descriptors consist of metadata required by the receiver to allocate memory
// for an incoming message.
class Descriptor final {
public:
std::string metadata;
struct Payload {
size_t length{0};
std::string metadata;
};
std::vector<Payload> payloads;
struct Tensor {
size_t length{0};
// This is the sender-side device from which this tensor is being sent.
Device sourceDevice;
// The sender may optionally specify a target device, in which case the
// receiver must allocate memory for this tensor on the specified device.
optional<Device> targetDevice;
std::string metadata;
};
std::vector<Tensor> tensors;
};
// Allocations consist of actual memory allocations provided by the receiver for
// an incoming message. They must match the length and target devices specified
// in the corresponding Descriptor.
class Allocation final {
public:
struct Payload {
void* data{nullptr};
};
std::vector<Payload> payloads;
struct Tensor {
tensorpipe::Buffer buffer;
};
std::vector<Tensor> tensors;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,103 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <tensorpipe/common/error.h>
#include <tensorpipe/core/message.h>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
class ContextImpl;
class ListenerImpl;
class PipeImpl;
// The pipe.
//
// Pipes represent a set of connections between a pair of processes.
// Unlike POSIX pipes, they are message oriented instead of byte
// oriented. Messages that are sent through the pipe may use whatever
// channels are at their disposal to make it happen. If the pair of
// processes happen to be colocated on the same machine, they may
// leverage a region of shared memory to communicate the primary
// buffer of a message. Secondary buffers may use shared memory as
// well, if they're located in CPU memory, or use a CUDA device to
// device copy if they're located in NVIDIA GPU memory. If the pair is
// located across the world, they may simply use a set of TCP
// connections to communicate.
//
class Pipe final {
// Use the passkey idiom to allow make_shared to call what should be a private
// constructor. See https://abseil.io/tips/134 for more information.
struct ConstructorToken {};
public:
//
// Initialization
//
Pipe(
ConstructorToken token,
std::shared_ptr<ContextImpl> context,
std::string id,
std::string remoteName,
const std::string& url);
Pipe(ConstructorToken token, std::shared_ptr<PipeImpl> impl);
//
// Entry points for user code
//
using read_descriptor_callback_fn =
std::function<void(const Error&, Descriptor)>;
void readDescriptor(read_descriptor_callback_fn fn);
using read_callback_fn = std::function<void(const Error&)>;
void read(Allocation allocation, read_callback_fn fn);
using write_callback_fn = std::function<void(const Error&)>;
void write(Message message, write_callback_fn fn);
// Retrieve the user-defined name that was given to the constructor of the
// context on the remote side, if any (if not, this will be the empty string).
// This is intended to help in logging and debugging only.
const std::string& getRemoteName();
// Put the pipe in a terminal state, aborting its pending operations and
// rejecting future ones, and release its resrouces. This may be carried out
// asynchronously, in background.
void close();
~Pipe();
private:
// Using a shared_ptr allows us to detach the lifetime of the implementation
// from the public object's one and perform the destruction asynchronously.
const std::shared_ptr<PipeImpl> impl_;
// Allow context to access constructor token.
friend ContextImpl;
// Allow listener to access constructor token.
friend ListenerImpl;
};
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,60 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <tensorpipe/config.h>
// High-level API
#include <tensorpipe/core/context.h>
#include <tensorpipe/core/error.h>
#include <tensorpipe/core/listener.h>
#include <tensorpipe/core/message.h>
#include <tensorpipe/core/pipe.h>
#include <tensorpipe/common/buffer.h>
#include <tensorpipe/common/cpu_buffer.h>
// Transports
#include <tensorpipe/transport/context.h>
#include <tensorpipe/transport/error.h>
#include <tensorpipe/transport/uv/error.h>
#include <tensorpipe/transport/uv/factory.h>
#include <tensorpipe/transport/uv/utility.h>
#if TENSORPIPE_HAS_SHM_TRANSPORT
#include <tensorpipe/transport/shm/factory.h>
#endif // TENSORPIPE_HAS_SHM_TRANSPORT
#if TENSORPIPE_HAS_IBV_TRANSPORT
#include <tensorpipe/transport/ibv/error.h>
#include <tensorpipe/transport/ibv/factory.h>
#include <tensorpipe/transport/ibv/utility.h>
#endif // TENSORPIPE_HAS_IBV_TRANSPORT
// Channels
#include <tensorpipe/channel/context.h>
#include <tensorpipe/channel/error.h>
#include <tensorpipe/channel/basic/factory.h>
#include <tensorpipe/channel/mpt/factory.h>
#include <tensorpipe/channel/xth/factory.h>
#if TENSORPIPE_HAS_CMA_CHANNEL
#include <tensorpipe/channel/cma/factory.h>
#endif // TENSORPIPE_HAS_CMA_CHANNEL
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,83 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <string>
namespace tensorpipe {
namespace transport {
class Connection;
class Listener;
class Context {
public:
virtual std::shared_ptr<Connection> connect(std::string addr) = 0;
virtual std::shared_ptr<Listener> listen(std::string addr) = 0;
// Return whether the context is able to operate correctly.
//
// Some transport types may be unable to perform as intended under
// some circumstances (e.g., specialized hardware unavailable, lack
// of permissions). They can report it through this method in order
// for the core context to avoid registering them in the first place.
//
virtual bool isViable() const = 0;
// Return string to describe the domain for this context.
//
// Two processes with a context of the same type can connect to each
// other if one side's domain descriptor is "accepted" by the other
// one, using the canCommunicateWithRemote method below. That method
// must be symmetric, and unless overridden defaults to string
// comparison.
//
// For example, for a transport that leverages TCP/IP, this may be
// as simple as the address family (assuming we can route between
// any two processes). For a transport that leverages shared memory,
// this descriptor must uniquely identify the machine, such that
// only co-located processes generate the same domain descriptor.
//
virtual const std::string& domainDescriptor() const = 0;
// Compare local and remote domain descriptor for compatibility.
//
// Determine whether a connection can be opened between this context
// and a remote one that has the given domain descriptor. This
// function needs to be symmetric: if we called this method on the
// remote context with the local descriptor we should get the same
// answer. Unless overridden it defaults to string comparison.
//
virtual bool canCommunicateWithRemote(
const std::string& remoteDomainDescriptor) const {
return domainDescriptor() == remoteDomainDescriptor;
}
// Tell the context what its identifier is.
//
// This is only supposed to be called from the high-level context or from
// channel contexts. It will only used for logging and debugging purposes.
virtual void setId(std::string id) = 0;
virtual void close() = 0;
virtual void join() = 0;
virtual ~Context() = default;
};
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,52 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tensorpipe/common/error.h>
namespace tensorpipe {
namespace transport {
class ContextClosedError final : public BaseError {
public:
ContextClosedError() {}
std::string what() const override;
};
class ListenerClosedError final : public BaseError {
public:
ListenerClosedError() {}
std::string what() const override;
};
class ConnectionClosedError final : public BaseError {
public:
ConnectionClosedError() {}
std::string what() const override;
};
class ContextNotViableError final : public BaseError {
public:
ContextNotViableError() {}
std::string what() const override;
};
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,53 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tensorpipe/transport/error.h>
namespace tensorpipe {
namespace transport {
namespace ibv {
class IbvError final : public BaseError {
public:
explicit IbvError(std::string error) : error_(error) {}
std::string what() const override;
private:
std::string error_;
};
class GetaddrinfoError final : public BaseError {
public:
explicit GetaddrinfoError(int error) : error_(error) {}
std::string what() const override;
private:
int error_;
};
class NoAddrFoundError final : public BaseError {
public:
NoAddrFoundError() {}
std::string what() const override;
};
} // namespace ibv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
namespace transport {
namespace ibv {
std::shared_ptr<Context> create();
} // namespace ibv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,31 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tuple>
#include <tensorpipe/common/error.h>
namespace tensorpipe {
namespace transport {
namespace ibv {
std::tuple<Error, std::string> lookupAddrForIface(std::string iface);
std::tuple<Error, std::string> lookupAddrForHostname();
} // namespace ibv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
namespace transport {
namespace shm {
std::shared_ptr<Context> create();
} // namespace shm
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,43 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tensorpipe/transport/error.h>
namespace tensorpipe {
namespace transport {
namespace uv {
class UVError final : public BaseError {
public:
explicit UVError(int error) : error_(error) {}
std::string what() const override;
private:
int error_;
};
class NoAddrFoundError final : public BaseError {
public:
NoAddrFoundError() {}
std::string what() const override;
};
} // namespace uv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,28 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <tensorpipe/transport/context.h>
namespace tensorpipe {
namespace transport {
namespace uv {
std::shared_ptr<Context> create();
} // namespace uv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
@@ -0,0 +1,41 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <tuple>
#include <sys/socket.h>
#include <tensorpipe/common/error.h>
#include <tensorpipe/common/optional.h>
namespace tensorpipe {
namespace transport {
namespace uv {
std::tuple<Error, std::string> lookupAddrForIface(std::string iface);
std::tuple<Error, std::string> lookupAddrForHostname();
// Try to replicate the same logic used by NCCL to find a node's own address.
// Roughly, it returns the "first" usable address it can find, and prioritizes
// the interfaces with an `ib` prefix and de-prioritizes those with a `docker`
// or `lo` prefix. It can optionally only return only IPv4 or IPv4 addresses.
std::tuple<Error, std::string> lookupAddrLikeNccl(
optional<sa_family_t> familyFilter = nullopt);
} // namespace uv
} // namespace transport
} // namespace tensorpipe
#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)