Skip to content

Commit 68d5378

Browse files
zcbenzeejd
authored andcommitted
Add "config" module (backport of ml-explore#4647 to v0.32.3)
Cherry-picked from 0569a18 (zcbenz, open upstream PR ml-explore#4647). Conflicts resolved by keeping v0.32.3 code and converting its remaining env::get_var / env:: helpers to config::get, matching the PR on main. Also converts test_fast_gated_delta.py os.environ mutations to mx.config.update: config::get caches the first env read, so later os.environ writes in the same process are ignored (also present in the upstream PR head).
1 parent 64ea011 commit 68d5378

31 files changed

Lines changed: 393 additions & 116 deletions

‎docs/src/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ are the CPU and GPU.
6666

6767
python/array
6868
python/data_types
69+
python/config
6970
python/devices_and_streams
7071
python/export
7172
python/ops

‎docs/src/python/config.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Config
2+
======
3+
4+
.. currentmodule:: mlx.core.config
5+
6+
.. autosummary::
7+
:toctree: _autosummary
8+
9+
update
10+
get
11+
scoped_update

‎docs/src/usage/environment_variables.rst‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Environment Variables
44
=====================
55

66
MLX uses environment variables to configure compilation, numerical precision,
7-
backend behavior, and distributed execution. Set them before starting the
8-
process. Many variables are read when the corresponding subsystem is first
9-
initialized and changing them later may have no effect.
7+
backend behavior, and distributed execution. They must be set before invoking
8+
any MLX API, and can not be changed later.
9+
10+
For dynamically changing the configurations in the middle of program, use the
11+
:func:`mlx.core.config.update` API.
1012

1113
Boolean variables use ``0`` to disable and a nonzero integer to enable unless
1214
otherwise noted.

‎mlx/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ target_sources(
22
mlx
33
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/array.cpp
44
${CMAKE_CURRENT_SOURCE_DIR}/compile.cpp
5+
${CMAKE_CURRENT_SOURCE_DIR}/config.cpp
56
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
67
${CMAKE_CURRENT_SOURCE_DIR}/dtype.cpp
78
${CMAKE_CURRENT_SOURCE_DIR}/dtype_utils.cpp

‎mlx/backend/cuda/device.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace mlx::core::cu {
1515
namespace {
1616

1717
bool use_cuda_graphs() {
18-
static bool use_graphs = env::get_var("MLX_USE_CUDA_GRAPHS", true);
18+
static bool use_graphs = config::get("MLX_USE_CUDA_GRAPHS", true);
1919
return use_graphs;
2020
}
2121

‎mlx/backend/cuda/lru_cache.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class LRUCache {
3333

3434
// Initialize with capacity read from |env_name|.
3535
LRUCache(const char* env_name, int default_capacity)
36-
: LRUCache(env::get_var(env_name, default_capacity)) {
37-
if (env::get_var("MLX_ENABLE_CACHE_THRASHING_CHECK", 1)) {
36+
: LRUCache(config::get(env_name, default_capacity)) {
37+
if (config::get("MLX_ENABLE_CACHE_THRASHING_CHECK", 1)) {
3838
env_name_ = env_name;
3939
}
4040
}

‎mlx/backend/cuda/scaled_dot_product_attention.cpp‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,7 @@ bool supports_sdpa_cudnn(
319319
bool has_arr_mask,
320320
bool do_causal,
321321
Stream s) {
322-
static bool enabled = env::get_var("MLX_CUDA_USE_CUDNN_SDPA", 1);
323-
if (!enabled) {
324-
return false;
325-
}
322+
bool enabled = config::get("MLX_CUDA_USE_CUDNN_SDPA", 1);
326323

327324
// cuDNN SDPA requires Ampere and later.
328325
if (cu::device(s.device).compute_capability_major() < 8) {

‎mlx/backend/metal/conv.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ inline int max_unfold_rows(metal::Device& d, size_t row_bytes, int total_rows) {
3535
size_t max_buffer = d.mtl_device()->maxBufferLength();
3636
size_t max_rows = row_bytes == 0 ? total_rows : max_buffer / row_bytes;
3737
// Force a smaller tile (tests, or to bound the unfold buffer further).
38-
if (int forced = env::get_var("MLX_CONV_UNFOLD_TILE_ROWS", 0); forced > 0) {
38+
if (int forced = config::get("MLX_CONV_UNFOLD_TILE_ROWS", 0); forced > 0) {
3939
max_rows = std::min(max_rows, static_cast<size_t>(forced));
4040
}
4141
if (max_rows == 0) {
@@ -72,7 +72,7 @@ inline int winograd_batch_step(
7272

7373
// Limit of maximum memory can be used for the step.
7474
size_t working_set = d.mtl_device()->recommendedMaxWorkingSetSize();
75-
if (int env_ws = env::get_var("MLX_CONV_WINOGRAD_WORKING_SET", 0);
75+
if (int env_ws = config::get("MLX_CONV_WINOGRAD_WORKING_SET", 0);
7676
env_ws > 0) {
7777
working_set = env_ws;
7878
}
@@ -96,8 +96,7 @@ inline int winograd_batch_step(
9696
(conv_params.C + conv_params.O) * itemsize;
9797
auto max_n = static_cast<int64_t>(budget / bytes_per_n);
9898
int safe_n = static_cast<int>(std::min<int64_t>(max_n, total_n));
99-
if (int forced = env::get_var("MLX_CONV_WINOGRAD_TILE_BATCH", 0);
100-
forced > 0) {
99+
if (int forced = config::get("MLX_CONV_WINOGRAD_TILE_BATCH", 0); forced > 0) {
101100
return std::min(forced, safe_n);
102101
}
103102

‎mlx/backend/metal/device.cpp‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ namespace {
3434

3535
constexpr const char* default_mtllib_path = METAL_PATH;
3636

37+
const std::string& metal_gpu_arch() {
38+
static std::string gpu_arch_ = []() {
39+
if (const char* str = std::getenv("MLX_METAL_GPU_ARCH")) {
40+
return str;
41+
} else {
42+
return "";
43+
}
44+
}();
45+
return gpu_arch_;
46+
}
47+
3748
void set_compile_options(
3849
MTL::CompileOptions* mtl_options,
3950
const CompileOptions& compile_options) {
@@ -509,7 +520,9 @@ void CommandEncoder::wait_event(Event event, uint64_t value) {
509520
}
510521

511522
bool CommandEncoder::needs_commit() const {
512-
auto [max_ops, max_mb] = device_.get_max_ops_mb_per_buffer();
523+
auto [max_ops, max_mb] = device_.get_default_max_ops_mb_per_buffer();
524+
max_ops = env::max_ops_per_buffer(max_ops);
525+
max_mb = env::max_mb_per_buffer(max_mb);
513526
return (buffer_ops_ > max_ops) || ((buffer_sizes_ >> 20) > max_mb);
514527
}
515528

@@ -586,7 +599,7 @@ MTL::ComputeCommandEncoder* CommandEncoder::get_command_encoder() {
586599
Device::Device() : device_(load_device()), residency_sets_(device_.get()) {
587600
auto pool = new_scoped_memory_pool();
588601
default_library_ = NS::TransferPtr(load_default_library(device_.get()));
589-
arch_ = env::metal_gpu_arch();
602+
arch_ = metal_gpu_arch();
590603
if (arch_.empty()) {
591604
arch_ = std::string(device_->architecture()->name()->utf8String());
592605
}
@@ -622,8 +635,6 @@ Device::Device() : device_(load_device()), residency_sets_(device_.get()) {
622635
max_mb_per_buffer_ = 40;
623636
break;
624637
}
625-
max_ops_per_buffer_ = env::max_ops_per_buffer(max_ops_per_buffer_);
626-
max_mb_per_buffer_ = env::max_mb_per_buffer(max_mb_per_buffer_);
627638
}
628639

629640
Device::~Device() = default;

‎mlx/backend/metal/device.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class MLX_API Device {
161161
int get_architecture_gen() const {
162162
return arch_gen_;
163163
}
164-
std::tuple<int, int> get_max_ops_mb_per_buffer() const {
164+
std::tuple<int, int> get_default_max_ops_mb_per_buffer() const {
165165
return std::make_tuple(max_ops_per_buffer_, max_mb_per_buffer_);
166166
}
167167

0 commit comments

Comments
 (0)