diff --git a/dipu/torch_dipu/csrc_dipu/base/environ.hpp b/dipu/torch_dipu/csrc_dipu/base/environ.hpp index 6b282e4b2..13f52c021 100644 --- a/dipu/torch_dipu/csrc_dipu/base/environ.hpp +++ b/dipu/torch_dipu/csrc_dipu/base/environ.hpp @@ -86,12 +86,39 @@ T getEnvOrDefault(const char* env_var, U&& default_value, // applyDelayedRegister() is called. DIPU_ENV_VAR(immediateRegisterOp, "DIPU_IMMEDIATE_REGISTER_OP", bool, false); inline const std::string kTorchAllocatorName = "TORCH"; + +// Determine the name of the host memory cache algorithm +// based on the current environment configuration. DIPU_ENV_VAR(hostMemCachingAlgorithm, "DIPU_HOST_MEMCACHING_ALGORITHM", std::string, kTorchAllocatorName); + +// Used to specify the name of the device memory cache algorithm. DIPU_ENV_VAR(deviceMemCachingAlgorithm, "DIPU_DEVICE_MEMCACHING_ALGORITHM", std::string, kTorchAllocatorName); + +// Used to configure and initialize an instance of an object +// "CachingAllocatorConfig". DIPU_ENV_VAR(torchAllocatorConf, "DIPU_TORCH_ALLOCATOR_CONF", std::string, ""); +// maxExtendSize is used to limit the maximum size of an extension +// in the memory allocation in function of "extend()". +DIPU_ENV_VAR(maxExtendSize, "DIPU_MAX_EXTEND_SIZE", std::size_t, 1024); + +// Configure a value to limit the maximum length of the asynchronous resource +// pool to avoid resource leakage and optimize resource management. +inline const std::size_t kDefaultMaxAsyncResourcePoolLength = 96; +DIPU_ENV_VAR(maxAsyncResourcePoolLength, "DIPU_MAX_ASYNC_RESOURCE_POOL_LENGTH", + std::size_t, kDefaultMaxAsyncResourcePoolLength); + +// Control whether to force the use of back-off mode for P2P copy operation +// between Ascend chips. +DIPU_ENV_VAR(forceFallbackP2pCopybetweenascends, + "DIPU_FORCE_FALLBACK_ASCEND_P2P_COPY", bool, false); + +// Configure a numerical value to control the device 's affinity settings +// on the CPU to optimize thread scheduling during concurrent execution. +DIPU_ENV_VAR(affinityCpuAffinit, "DIPU_CPU_AFFINITY", int, 0); + #undef DIPU_ENV_VAR } // namespace dipu::environ diff --git a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBFCachingAllocator.cpp b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBFCachingAllocator.cpp index c9c29d50c..7b58ae227 100644 --- a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBFCachingAllocator.cpp +++ b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUBFCachingAllocator.cpp @@ -7,6 +7,7 @@ #include #include +#include "csrc_dipu/base/environ.hpp" #include "csrc_dipu/utils/env.hpp" #include "DIPUCachingAllocator.h" @@ -15,8 +16,7 @@ namespace dipu { // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -const size_t kMaxExtendSize = get_env_or_default("DIPU_MAX_EXTEND_SIZE", 1024) - << 20U; +const size_t kMaxExtendSize = environ::maxExtendSize() << 20U; class BFCachingAllocatorImpl { public: diff --git a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUCachingAllocator.cpp b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUCachingAllocator.cpp index 64a927e20..d94bb9feb 100644 --- a/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUCachingAllocator.cpp +++ b/dipu/torch_dipu/csrc_dipu/runtime/core/allocator/DIPUCachingAllocator.cpp @@ -25,10 +25,9 @@ namespace dipu { // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) std::mutex DIPURawDeviceAllocator::mutex_; -constexpr size_t kDefaultMaxAsyncResourcePoolLength = 96; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -const size_t kMaxAsyncResourcePoolLength = get_env_or_default( - "DIPU_MAX_ASYNC_RESOURCE_POOL_LENGTH", kDefaultMaxAsyncResourcePoolLength); +const size_t kMaxAsyncResourcePoolLength = + environ::maxAsyncResourcePoolLength(); namespace { diff --git a/dipu/torch_dipu/csrc_dipu/runtime/devproxy/deviceproxy.cpp b/dipu/torch_dipu/csrc_dipu/runtime/devproxy/deviceproxy.cpp index d6bda498e..c3e74c1e3 100644 --- a/dipu/torch_dipu/csrc_dipu/runtime/devproxy/deviceproxy.cpp +++ b/dipu/torch_dipu/csrc_dipu/runtime/devproxy/deviceproxy.cpp @@ -6,6 +6,7 @@ #include +#include "csrc_dipu/base/environ.hpp" #include "csrc_dipu/runtime/core/DIPUEventPool.h" #include "csrc_dipu/runtime/core/allocator/allocator_metrics.h" #include "csrc_dipu/runtime/device/basedef.h" @@ -64,7 +65,8 @@ deviceId_t current_device() { } void setCpuAffinity(const int device) { - static int affinity = get_env_or_default("DIPU_CPU_AFFINITY", 0); + static int affinity = environ::affinityCpuAffinit(); + if (affinity < 0) { return; } diff --git a/dipu/torch_dipu/csrc_dipu/vendor/ascend/deviceimpl.cpp b/dipu/torch_dipu/csrc_dipu/vendor/ascend/deviceimpl.cpp index ad0f58bf4..8deff49ef 100644 --- a/dipu/torch_dipu/csrc_dipu/vendor/ascend/deviceimpl.cpp +++ b/dipu/torch_dipu/csrc_dipu/vendor/ascend/deviceimpl.cpp @@ -10,6 +10,7 @@ #include #include +#include "csrc_dipu/base/environ.hpp" #include "csrc_dipu/runtime/device/basedef.h" #include "csrc_dipu/utils/env.hpp" #include @@ -30,8 +31,7 @@ using AscendDeviceId = int32_t; namespace { -const bool forceFallbackP2PCopy = - get_env_or_default("DIPU_FORCE_FALLBACK_ASCEND_P2P_COPY", false); +const bool forceFallbackP2PCopy = environ::forceFallbackP2pCopybetweenascends(); class NpuP2PInfo { enum class P2pStatus : int8_t {