|
| 1 | +""" |
| 2 | +Compute configuration for JAX, NumPy, and Dask. |
| 3 | +
|
| 4 | +This module handles GPU/CPU configuration and thread limiting for computational backends. |
| 5 | +
|
| 6 | +IMPORTANT NOTES: |
| 7 | +1. NumPy thread limits: Set early in main() via environment variables BEFORE NumPy import. |
| 8 | + The _configure_thread_limits() function here is called later but only affects subsequently |
| 9 | + loaded modules (like Dask), not NumPy which is already initialized. |
| 10 | +
|
| 11 | +2. JAX configuration: Must be called AFTER mellon import, as mellon configures JAX on import. |
| 12 | + The _configure_jax() function can override mellon's settings. |
| 13 | +
|
| 14 | +3. Dask configuration: Can be set at any time via dask.config. |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import logging |
| 19 | + |
| 20 | +logger = logging.getLogger("kompot.cli") |
| 21 | + |
| 22 | + |
| 23 | +def configure_compute(use_gpu: bool = False, n_threads: int = None): |
| 24 | + """ |
| 25 | + Configure computational backends (JAX, NumPy, Dask) for thread control and GPU usage. |
| 26 | +
|
| 27 | + This function must be called AFTER importing mellon, as mellon configures JAX |
| 28 | + to use CPU on import. This function can override that configuration. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + use_gpu : bool, default=False |
| 33 | + If True, configure JAX to use GPU. If False, force CPU usage. |
| 34 | + n_threads : int, optional |
| 35 | + Number of threads to use. If specified, limits threads for: |
| 36 | + - JAX (XLA) |
| 37 | + - NumPy (OpenBLAS/MKL) |
| 38 | + - Dask |
| 39 | +
|
| 40 | + Notes |
| 41 | + ----- |
| 42 | + Thread limiting affects: |
| 43 | + - JAX: Set via XLA_FLAGS environment variable |
| 44 | + - NumPy: Set via OMP_NUM_THREADS, MKL_NUM_THREADS, OPENBLAS_NUM_THREADS |
| 45 | + - Dask: Set via dask.config |
| 46 | +
|
| 47 | + Examples |
| 48 | + -------- |
| 49 | + >>> # CPU-only with 4 threads |
| 50 | + >>> configure_compute(use_gpu=False, n_threads=4) |
| 51 | +
|
| 52 | + >>> # GPU with thread limiting |
| 53 | + >>> configure_compute(use_gpu=True, n_threads=8) |
| 54 | + """ |
| 55 | + logger.info("=" * 60) |
| 56 | + logger.info("Configuring computational backends") |
| 57 | + logger.info("=" * 60) |
| 58 | + |
| 59 | + # Configure thread limits BEFORE JAX initialization |
| 60 | + if n_threads is not None: |
| 61 | + logger.info(f"Setting thread limit: {n_threads} threads") |
| 62 | + _configure_thread_limits(n_threads) |
| 63 | + else: |
| 64 | + logger.info("No thread limit specified (using system defaults)") |
| 65 | + |
| 66 | + # Configure JAX (must be done AFTER mellon import) |
| 67 | + _configure_jax(use_gpu, n_threads) |
| 68 | + |
| 69 | + # Configure Dask if available |
| 70 | + try: |
| 71 | + _configure_dask(n_threads) |
| 72 | + except ImportError: |
| 73 | + logger.debug("Dask not available, skipping dask configuration") |
| 74 | + |
| 75 | + logger.info("=" * 60) |
| 76 | + |
| 77 | + |
| 78 | +def _configure_thread_limits(n_threads: int): |
| 79 | + """ |
| 80 | + Set environment variables to limit threads for NumPy and related libraries. |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + n_threads : int |
| 85 | + Number of threads to use |
| 86 | + """ |
| 87 | + n_threads_str = str(n_threads) |
| 88 | + |
| 89 | + # OpenMP (used by NumPy, SciPy, etc.) |
| 90 | + os.environ['OMP_NUM_THREADS'] = n_threads_str |
| 91 | + logger.debug(f" Set OMP_NUM_THREADS={n_threads_str}") |
| 92 | + |
| 93 | + # Intel MKL (if NumPy is built with MKL) |
| 94 | + os.environ['MKL_NUM_THREADS'] = n_threads_str |
| 95 | + logger.debug(f" Set MKL_NUM_THREADS={n_threads_str}") |
| 96 | + |
| 97 | + # OpenBLAS (if NumPy is built with OpenBLAS) |
| 98 | + os.environ['OPENBLAS_NUM_THREADS'] = n_threads_str |
| 99 | + logger.debug(f" Set OPENBLAS_NUM_THREADS={n_threads_str}") |
| 100 | + |
| 101 | + # BLAS (general) |
| 102 | + os.environ['BLAS_NUM_THREADS'] = n_threads_str |
| 103 | + logger.debug(f" Set BLAS_NUM_THREADS={n_threads_str}") |
| 104 | + |
| 105 | + logger.info(f" NumPy/BLAS thread limit: {n_threads} threads") |
| 106 | + |
| 107 | + |
| 108 | +def _configure_jax(use_gpu: bool, n_threads: int = None): |
| 109 | + """ |
| 110 | + Configure JAX for GPU/CPU usage and thread limiting. |
| 111 | +
|
| 112 | + Must be called AFTER mellon import, as mellon sets JAX to CPU mode on import. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + use_gpu : bool |
| 117 | + Whether to use GPU |
| 118 | + n_threads : int, optional |
| 119 | + Number of threads for CPU execution |
| 120 | + """ |
| 121 | + import jax |
| 122 | + |
| 123 | + if use_gpu: |
| 124 | + # Check if GPU is available |
| 125 | + try: |
| 126 | + devices = jax.devices('gpu') |
| 127 | + if len(devices) > 0: |
| 128 | + logger.info(f" JAX: GPU mode enabled") |
| 129 | + logger.info(f" Available GPU devices: {len(devices)}") |
| 130 | + for i, device in enumerate(devices): |
| 131 | + logger.info(f" Device {i}: {device}") |
| 132 | + |
| 133 | + # Set default device to GPU |
| 134 | + # Note: mellon may have set it to CPU, we override here |
| 135 | + jax.config.update('jax_platform_name', 'gpu') |
| 136 | + else: |
| 137 | + logger.warning(" JAX: GPU requested but no GPU devices found, falling back to CPU") |
| 138 | + jax.config.update('jax_platform_name', 'cpu') |
| 139 | + use_gpu = False |
| 140 | + except RuntimeError as e: |
| 141 | + logger.warning(f" JAX: GPU not available ({e}), using CPU") |
| 142 | + jax.config.update('jax_platform_name', 'cpu') |
| 143 | + use_gpu = False |
| 144 | + else: |
| 145 | + logger.info(" JAX: CPU mode (GPU disabled)") |
| 146 | + jax.config.update('jax_platform_name', 'cpu') |
| 147 | + |
| 148 | + # Configure thread limits for JAX/XLA |
| 149 | + if not use_gpu and n_threads is not None: |
| 150 | + # Set intra-op parallelism for CPU |
| 151 | + xla_flags = os.environ.get('XLA_FLAGS', '') |
| 152 | + |
| 153 | + # Add thread limit to XLA_FLAGS |
| 154 | + thread_flag = f'--xla_cpu_multi_thread_eigen=true intra_op_parallelism_threads={n_threads}' |
| 155 | + |
| 156 | + if 'intra_op_parallelism_threads' not in xla_flags: |
| 157 | + if xla_flags: |
| 158 | + xla_flags = f'{xla_flags} {thread_flag}' |
| 159 | + else: |
| 160 | + xla_flags = thread_flag |
| 161 | + |
| 162 | + os.environ['XLA_FLAGS'] = xla_flags |
| 163 | + logger.info(f" JAX/XLA thread limit: {n_threads} threads") |
| 164 | + logger.debug(f" XLA_FLAGS={xla_flags}") |
| 165 | + else: |
| 166 | + logger.debug(" XLA thread limit already configured") |
| 167 | + |
| 168 | + |
| 169 | +def _configure_dask(n_threads: int = None): |
| 170 | + """ |
| 171 | + Configure Dask thread limits. |
| 172 | +
|
| 173 | + Parameters |
| 174 | + ---------- |
| 175 | + n_threads : int, optional |
| 176 | + Number of threads for Dask |
| 177 | + """ |
| 178 | + try: |
| 179 | + import dask |
| 180 | + import dask.config |
| 181 | + |
| 182 | + if n_threads is not None: |
| 183 | + # Configure Dask to use specified number of threads |
| 184 | + dask.config.set(scheduler='threads', num_workers=n_threads) |
| 185 | + logger.info(f" Dask: thread limit set to {n_threads} threads") |
| 186 | + logger.debug(f" Dask scheduler: threads, num_workers={n_threads}") |
| 187 | + else: |
| 188 | + logger.debug(" Dask: using default configuration") |
| 189 | + |
| 190 | + except ImportError: |
| 191 | + # Dask not installed, skip |
| 192 | + pass |
| 193 | + |
| 194 | + |
| 195 | +def get_device_info(): |
| 196 | + """ |
| 197 | + Get information about available compute devices. |
| 198 | +
|
| 199 | + Returns |
| 200 | + ------- |
| 201 | + dict |
| 202 | + Dictionary with device information including: |
| 203 | + - gpu_available: bool |
| 204 | + - gpu_devices: list of device descriptions |
| 205 | + - cpu_count: int (logical cores) |
| 206 | + - jax_platform: str (current JAX platform) |
| 207 | + """ |
| 208 | + info = { |
| 209 | + 'gpu_available': False, |
| 210 | + 'gpu_devices': [], |
| 211 | + 'cpu_count': os.cpu_count(), |
| 212 | + 'jax_platform': None |
| 213 | + } |
| 214 | + |
| 215 | + try: |
| 216 | + import jax |
| 217 | + |
| 218 | + # Check current JAX platform |
| 219 | + try: |
| 220 | + current_backend = jax.devices()[0].platform |
| 221 | + info['jax_platform'] = current_backend |
| 222 | + except Exception: |
| 223 | + info['jax_platform'] = 'unknown' |
| 224 | + |
| 225 | + # Check for GPU devices |
| 226 | + try: |
| 227 | + gpu_devices = jax.devices('gpu') |
| 228 | + if len(gpu_devices) > 0: |
| 229 | + info['gpu_available'] = True |
| 230 | + info['gpu_devices'] = [str(d) for d in gpu_devices] |
| 231 | + except RuntimeError: |
| 232 | + pass |
| 233 | + |
| 234 | + except ImportError: |
| 235 | + pass |
| 236 | + |
| 237 | + return info |
| 238 | + |
| 239 | + |
| 240 | +def log_compute_environment(): |
| 241 | + """Log information about the current compute environment.""" |
| 242 | + info = get_device_info() |
| 243 | + |
| 244 | + logger.info("Compute Environment:") |
| 245 | + logger.info(f" CPU cores: {info['cpu_count']}") |
| 246 | + logger.info(f" JAX platform: {info['jax_platform']}") |
| 247 | + logger.info(f" GPU available: {info['gpu_available']}") |
| 248 | + if info['gpu_available']: |
| 249 | + logger.info(f" GPU devices: {len(info['gpu_devices'])}") |
| 250 | + for i, device in enumerate(info['gpu_devices']): |
| 251 | + logger.info(f" {i}: {device}") |
0 commit comments