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,43 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/core/CachingHostAllocator.h>
#include <ATen/xpu/XPUEvent.h>
#include <c10/core/Allocator.h>
#include <c10/util/Deprecated.h>
#include <c10/xpu/XPUStream.h>
namespace at::xpu {
C10_DEPRECATED_MESSAGE(
"at::xpu::getCachingHostAllocator() is deprecated. Please use at::getHostAllocator(at::kXPU) instead.")
inline TORCH_XPU_API at::HostAllocator* getCachingHostAllocator() {
return at::getHostAllocator(at::kXPU);
}
C10_DEPRECATED_MESSAGE(
"at::xpu::CachingHostAllocator_recordEvent(...) is deprecated. Please use at::getHostAllocator(at::kXPU)->record_event(...) instead.")
inline TORCH_XPU_API bool CachingHostAllocator_recordEvent(
void* ptr,
void* ctx,
c10::xpu::XPUStream stream) {
return getHostAllocator(at::kXPU)->record_event(ptr, ctx, stream.unwrap());
}
C10_DEPRECATED_MESSAGE(
"at::xpu::CachingHostAllocator_emptyCache() is deprecated. Please use at::getHostAllocator(at::kXPU)->empty_cache() instead.")
inline TORCH_XPU_API void CachingHostAllocator_emptyCache() {
getHostAllocator(at::kXPU)->empty_cache();
}
C10_DEPRECATED_MESSAGE(
"at::xpu::HostAlloc(...) is deprecated. Please use at::getHostAllocator(at::kXPU)->allocate(...) instead.")
inline TORCH_XPU_API at::DataPtr HostAlloc(size_t size) {
return getHostAllocator(at::kXPU)->allocate(size);
}
} // namespace at::xpu
#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,47 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/core/TensorBase.h>
namespace at::detail {
TORCH_XPU_API TensorBase empty_xpu(
IntArrayRef size,
ScalarType dtype,
std::optional<Device> device_opt,
std::optional<c10::MemoryFormat> memory_format_opt);
TORCH_XPU_API TensorBase empty_xpu(
IntArrayRef size,
std::optional<ScalarType> dtype_opt,
std::optional<Layout> layout_opt,
std::optional<Device> device_opt,
std::optional<bool> pin_memory_opt,
std::optional<c10::MemoryFormat> memory_format_opt);
TORCH_XPU_API TensorBase
empty_xpu(IntArrayRef size, const TensorOptions& options);
TORCH_XPU_API TensorBase empty_strided_xpu(
IntArrayRef size,
IntArrayRef stride,
ScalarType dtype,
std::optional<Device> device_opt);
TORCH_XPU_API TensorBase empty_strided_xpu(
IntArrayRef size,
IntArrayRef stride,
std::optional<ScalarType> dtype_opt,
std::optional<Layout> layout_opt,
std::optional<Device> device_opt,
std::optional<bool> pin_memory_opt);
TORCH_XPU_API TensorBase empty_strided_xpu(
IntArrayRef size,
IntArrayRef stride,
const TensorOptions& options);
} // namespace at::detail
#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,29 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <c10/xpu/PeerToPeerAccess.h>
#include <ATen/Context.h>
namespace at::xpu {
namespace detail {
// Initialize the peer-to-peer access cache for XPU devices.
inline void init_p2p_access_cache(c10::DeviceIndex num_devices) {
c10::xpu::detail::init_p2p_access_cache(num_devices);
}
} // namespace detail
// Query if peer-to-peer access is available between two devices.
// This wrapper ensures XPU lazy initialization before forwarding to c10.
inline bool get_p2p_access(
c10::DeviceIndex dev,
c10::DeviceIndex dev_to_access) {
at::globalContext().lazyInitDevice(c10::DeviceType::XPU);
return c10::xpu::get_p2p_access(dev, dev_to_access);
}
} // namespace at::xpu
#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,50 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
namespace at {
struct PhiloxXpuState {
PhiloxXpuState() = default;
PhiloxXpuState(uint64_t seed, uint64_t offset) {
seed_.val = seed;
offset_.val = offset;
}
// for graph capture
PhiloxXpuState(
int64_t* seed,
int64_t* offset_extragraph,
uint32_t offset_intragraph) {
seed_.ptr = seed;
offset_.ptr = offset_extragraph;
offset_intragraph_ = offset_intragraph;
captured_ = true;
}
union Payload {
uint64_t val;
int64_t* ptr;
};
Payload seed_{};
Payload offset_{};
uint32_t offset_intragraph_ = 0;
bool captured_ = false;
};
namespace xpu::philox {
inline std::tuple<uint64_t, uint64_t> unpack(at::PhiloxXpuState arg) {
if (arg.captured_) {
return std::make_tuple(
static_cast<uint64_t>(*arg.seed_.ptr),
static_cast<uint64_t>(*(arg.offset_.ptr) + arg.offset_intragraph_));
} else {
return std::make_tuple(arg.seed_.val, arg.offset_.val);
}
}
} // namespace xpu::philox
} // namespace at
#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 <ATen/xpu/CachingHostAllocator.h>
#include <c10/core/Allocator.h>
namespace at::xpu {
inline TORCH_XPU_API at::HostAllocator* getPinnedMemoryAllocator() {
return at::getHostAllocator(at::kXPU);
}
} // namespace at::xpu
#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,27 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/Context.h>
#include <c10/xpu/XPUFunctions.h>
#include <c10/xpu/XPUStream.h>
namespace at::xpu {
// XPU is available if we compiled with XPU.
inline bool is_available() {
return c10::xpu::device_count() > 0;
}
TORCH_XPU_API DeviceProp* getCurrentDeviceProperties();
TORCH_XPU_API DeviceProp* getDeviceProperties(DeviceIndex device);
TORCH_XPU_API int32_t getGlobalIdxFromDevice(DeviceIndex device);
TORCH_XPU_API bool canDeviceAccessPeer(DeviceIndex device, DeviceIndex peer);
} // namespace at::xpu
#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,18 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/Context.h>
#include <c10/xpu/XPUFunctions.h>
namespace at::xpu {
inline Device getDeviceFromPtr(void* ptr) {
auto device = c10::xpu::get_device_idx_from_pointer(ptr);
return {c10::DeviceType::XPU, device};
}
} // namespace at::xpu
#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,8 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/xpu/XPUContext.h>
#include <c10/xpu/XPUEvent.h>
#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,88 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/core/Generator.h>
#include <ATen/core/TensorBase.h>
#include <ATen/xpu/PhiloxXpuState.h>
#include <unordered_set>
namespace at {
namespace xpu {
struct XPUGraphImpl;
}
struct XPUGeneratorState : public c10::intrusive_ptr_target {
uint64_t seed_;
uint64_t philox_offset_per_thread_;
uint32_t offset_intragraph_;
bool capturing_{};
std::unordered_set<xpu::XPUGraphImpl*> registered_graphs_;
at::TensorBase seed_extragraph_{};
at::TensorBase offset_extragraph_{};
XPUGeneratorState(
uint64_t seed = default_rng_seed_val,
uint64_t philox_offset_per_thread = 0,
uint32_t offset_intragraph = 0)
: seed_(seed),
philox_offset_per_thread_(philox_offset_per_thread),
offset_intragraph_(offset_intragraph) {}
void increase(uint64_t increment);
void register_graph(xpu::XPUGraphImpl* graph);
void unregister_graph(xpu::XPUGraphImpl* graph);
void capture_prologue();
uint64_t capture_epilogue();
void replay_prologue(uint64_t wholegraph_increment);
c10::intrusive_ptr<XPUGeneratorState> clone();
};
struct TORCH_XPU_API XPUGeneratorImpl : public GeneratorImpl {
// Constructors
XPUGeneratorImpl(DeviceIndex device_index = -1);
XPUGeneratorImpl(
DeviceIndex device_index,
c10::intrusive_ptr<XPUGeneratorState> state_);
~XPUGeneratorImpl() override = default;
// XPUGeneratorImpl methods
std::shared_ptr<XPUGeneratorImpl> clone() const;
void set_current_seed(uint64_t seed) override;
void set_offset(uint64_t offset) override;
uint64_t get_offset() const override;
uint64_t current_seed() const override;
uint64_t seed() override;
void set_state(const c10::TensorImpl& new_state) override;
c10::intrusive_ptr<c10::TensorImpl> get_state() const override;
void graphsafe_set_state(
const c10::intrusive_ptr<GeneratorImpl>& state) override;
c10::intrusive_ptr<c10::GeneratorImpl> graphsafe_get_state() const override;
void set_philox_offset_per_thread(uint64_t offset);
uint64_t philox_offset_per_thread() const;
void register_graph(xpu::XPUGraphImpl* graph);
void unregister_graph(xpu::XPUGraphImpl* graph);
PhiloxXpuState philox_xpu_state(uint64_t increment);
std::pair<uint64_t, uint64_t> philox_engine_inputs(uint64_t increment);
static c10::DeviceType device_type();
private:
XPUGeneratorImpl* clone_impl() const override;
c10::intrusive_ptr<XPUGeneratorState> state_;
};
namespace xpu::detail {
TORCH_XPU_API const Generator& getDefaultXPUGenerator(DeviceIndex device = -1);
TORCH_XPU_API Generator createXPUGenerator(DeviceIndex device = -1);
} // namespace xpu::detail
} // namespace at
#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,123 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/Tensor.h>
#include <ATen/core/GraphImplInterface.h>
#include <ATen/xpu/XPUGeneratorImpl.h>
#include <c10/core/Device.h>
#include <c10/util/flat_hash_map.h>
#include <c10/xpu/XPUCachingAllocator.h>
#include <c10/xpu/XPUGraphsC10Utils.h>
#include <c10/xpu/XPUStream.h>
namespace at::xpu {
TORCH_XPU_API MempoolId_t graph_pool_handle();
using xpuGraph_t = sycl::ext::oneapi::experimental::command_graph<
sycl::ext::oneapi::experimental::graph_state::modifiable>;
using xpuGraphExec_t = sycl::ext::oneapi::experimental::command_graph<
sycl::ext::oneapi::experimental::graph_state::executable>;
struct TORCH_XPU_API XPUGraphImpl : public at::GraphImplInterface {
XPUGraphImpl(const GraphImplArgs& args = {});
~XPUGraphImpl() override;
C10_DISABLE_COPY_AND_ASSIGN(XPUGraphImpl);
void register_generator_state(
c10::intrusive_ptr<at::XPUGeneratorState> state);
void register_generator_state(const at::Generator& generator);
void capture_begin(
MempoolId_t pool = {0, 0},
GraphCaptureMode capture_mode = GraphCaptureMode::Default) override;
void capture_end() override;
void instantiate() override;
void replay() override;
void reset() override;
MempoolId_t pool() const override;
void enable_debug_mode() override;
void debug_dump(const std::string& debug_path) override;
xpuGraph_t* raw_xpu_graph();
xpuGraphExec_t* raw_xpu_graph_exec();
protected:
std::unique_ptr<xpuGraph_t> graph_;
std::unique_ptr<xpuGraphExec_t> graph_exec_;
bool has_graph_ = false;
bool capture_ended_ = false;
bool has_graph_exec_ = false;
MempoolId_t mempool_id_;
at::xpu::XPUStream capture_stream_;
// GeneratorState and whole graph offset increments mapping
ska::flat_hash_map<c10::intrusive_ptr<at::XPUGeneratorState>, uint64_t>
captured_generator_states_;
static constexpr c10::DeviceIndex UNDEFINED_DEVICE = -1;
c10::DeviceIndex capture_dev_{UNDEFINED_DEVICE};
bool keep_graph_;
};
struct TORCH_XPU_API XPUGraph {
XPUGraph(bool keep_graph = false) {
GraphImplArgs args;
args.keep_graph = keep_graph;
impl_ = std::make_unique<XPUGraphImpl>(args);
}
~XPUGraph() = default;
C10_DISABLE_COPY_AND_ASSIGN(XPUGraph);
XPUGraph(XPUGraph&& other) = delete;
XPUGraph& operator=(XPUGraph&& other) = delete;
void register_generator_state(
c10::intrusive_ptr<at::XPUGeneratorState> state) {
impl_->register_generator_state(state);
}
void register_generator_state(const at::Generator& generator) {
impl_->register_generator_state(generator);
}
void capture_begin(MempoolId_t pool = {0, 0}) {
impl_->capture_begin(pool);
}
void capture_end() {
impl_->capture_end();
}
void instantiate() {
impl_->instantiate();
}
void replay() {
impl_->replay();
}
void reset() {
impl_->reset();
}
MempoolId_t pool() const {
return impl_->pool();
}
void enable_debug_mode() {
impl_->enable_debug_mode();
}
void debug_dump(const std::string& debug_path) {
impl_->debug_dump(debug_path);
}
xpuGraph_t* raw_xpu_graph() {
return impl_->raw_xpu_graph();
}
xpuGraphExec_t* raw_xpu_graph_exec() {
return impl_->raw_xpu_graph_exec();
}
private:
std::unique_ptr<XPUGraphImpl> impl_;
};
} // namespace at::xpu
#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,27 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <c10/xpu/XPUGraphsC10Utils.h>
namespace at::xpu {
inline CaptureStatus currentStreamCaptureStatus() {
return c10::xpu::currentStreamCaptureStatusMayInitCtx();
}
inline void assertNotCapturing(const std::string& attempt) {
auto status = currentStreamCaptureStatus();
TORCH_CHECK(
status == CaptureStatus::Executing,
attempt,
" during XPU graph capture. If you need this call to be captured, "
"please file an issue. "
"Current xpuStreamCaptureStatus: ",
status);
}
} // namespace at::xpu
#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,100 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#include <c10/core/Scalar.h>
#include <c10/core/ScalarType.h>
#include <c10/util/Exception.h>
#include <c10/util/SmallVector.h>
#include <c10/util/typeid.h>
#include <cstdint>
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/Dispatch.h>
#include <ATen/ExpandUtils.h>
#include <ATen/OpMathType.h>
#include <ATen/TensorUtils.h>
#include <ATen/core/NamedTensor.h>
#include <ATen/core/Tensor.h>
#include <ATen/native/Resize.h>
#include <c10/util/MaybeOwned.h>
#include <ATen/BlasBackend.h>
#include <ATen/ceil_div.h>
#ifdef USE_MSLK
#include <mslk/gemm/gemm_torch.h>
#endif
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_addmm_activation_native.h>
#include <ATen/ops/_efficientzerotensor.h>
#include <ATen/ops/_scaled_mm_native.h>
#include <ATen/ops/_unsafe_view_native.h>
#include <ATen/ops/abs.h>
#include <ATen/ops/addmm_native.h>
#include <ATen/ops/addmv_native.h>
#include <ATen/ops/baddbmm_native.h>
#include <ATen/ops/bmm_native.h>
#include <ATen/ops/copy_native.h>
#include <ATen/ops/dot_native.h>
#include <ATen/ops/empty.h>
#include <ATen/ops/empty_strided.h>
#include <ATen/ops/gelu.h>
#include <ATen/ops/max.h>
#include <ATen/ops/mm_native.h>
#include <ATen/ops/mul.h>
#include <ATen/ops/ones.h>
#include <ATen/ops/relu.h>
#include <ATen/ops/scalar_tensor_native.h>
#include <ATen/ops/vdot_native.h>
#endif
using at::blas::ScalingType;
namespace at::native::onednn::scaled {
/**
* Track concrete implementations available
*/
enum class ScaledGemmImplementation {
NONE = 0,
TENSORWISE_TENSORWISE = 1,
ROWWISE_ROWWISE = 2,
};
/**
* Convert passed int (enum) from python back into a
* strictly-typed enum
*/
template <class EnumType, class ArrayType>
std::vector<EnumType> convert_int_to_enum(ArrayType& v) {
std::vector<EnumType> converted;
converted.reserve(v.size());
for (auto vi : v) {
converted.push_back(static_cast<EnumType>(vi));
}
return converted;
}
bool check_tensorwise_recipe(
c10::ScalarType,
std::vector<ScalingType>&,
ArrayRef<Tensor>&,
c10::ScalarType,
std::vector<ScalingType>&,
ArrayRef<Tensor>&);
bool check_rowwise_recipe(
c10::ScalarType,
std::vector<ScalingType>&,
ArrayRef<Tensor>&,
c10::ScalarType,
std::vector<ScalingType>&,
ArrayRef<Tensor>&);
} // namespace at::native::onednn::scaled
#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,26 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/xpu/XPUContext.h>
namespace at::xpu {
// Check if every tensor in a list of tensors matches the current device.
inline bool check_device(ArrayRef<Tensor> ts) {
if (ts.empty()) {
return true;
}
Device curDevice = Device(kXPU, current_device());
for (const Tensor& t : ts) {
if (t.device() != curDevice) {
return false;
}
}
return true;
}
} // namespace at::xpu
#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 <ATen/detail/XPUHooksInterface.h>
namespace at::xpu {
// Forward-declares at::xpu::LevelZero
struct LevelZero;
namespace detail {
extern LevelZero lazyLevelZero;
} // namespace detail
} // namespace at::xpu
#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,39 @@
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
#pragma once
#include <ATen/detail/XPUHooksInterface.h>
namespace at::xpu::detail {
// The real implementation of XPUHooksInterface
struct XPUHooks : public at::XPUHooksInterface {
XPUHooks(at::XPUHooksArgs) {}
void init() const override;
bool hasXPU() const override;
std::string showConfig() const override;
int32_t getGlobalIdxFromDevice(const at::Device& device) const override;
const Generator& getDefaultGenerator(
DeviceIndex device_index = -1) const override;
Generator getNewGenerator(DeviceIndex device_index = -1) const override;
Device getDeviceFromPtr(void* data) const override;
c10::DeviceIndex getNumGPUs() const override;
DeviceIndex current_device() const override;
void deviceSynchronize(DeviceIndex device_index) const override;
Allocator* getPinnedMemoryAllocator() const override;
bool isBuilt() const override {
return true;
}
bool isAvailable() const override;
bool isPinnedPtr(const void* data) const override;
bool hasPrimaryContext(DeviceIndex device_index) const override;
DeviceIndex deviceCount() const override;
DeviceIndex getCurrentDevice() const override;
const at::xpu::LevelZero& level_zero() const override;
};
} // namespace at::xpu::detail
#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)