|
| 1 | +module Distributed |
| 2 | + |
| 3 | +using ..Reactant: Reactant |
| 4 | + |
| 5 | +const initialized = Ref(false) |
| 6 | + |
| 7 | +function initialize(; |
| 8 | + coordinator_address::Union{Nothing,String}=nothing, |
| 9 | + num_processes::Union{Nothing,Integer}=nothing, |
| 10 | + process_id::Union{Nothing,Integer}=nothing, |
| 11 | + local_device_ids::Union{Nothing,Vector{Int}}=nothing, |
| 12 | + initialization_timeout_in_seconds::Integer=300, |
| 13 | + kwargs..., |
| 14 | +) |
| 15 | + @assert !initialized[] "`Distributed.initialize` has already been called" |
| 16 | + |
| 17 | + (coordinator_address, num_processes, process_id, local_device_ids) = auto_detect_unset_distributed_params(; |
| 18 | + coordinator_address, |
| 19 | + num_processes, |
| 20 | + process_id, |
| 21 | + local_device_ids, |
| 22 | + initialization_timeout_in_seconds, |
| 23 | + ) |
| 24 | + |
| 25 | + @debug "Detected Reactant distributed params" coordinator_address num_processes process_id local_device_ids |
| 26 | + |
| 27 | + Reactant.XLA.update_global_state!(; |
| 28 | + coordinator_address, num_processes, process_id, local_device_ids, kwargs... |
| 29 | + ) |
| 30 | + |
| 31 | + @debug "New Global State" Reactant.XLA.global_state |
| 32 | + |
| 33 | + initialized[] = true |
| 34 | + return nothing |
| 35 | +end |
| 36 | + |
| 37 | +abstract type AbstractClusterEnvDetector end |
| 38 | + |
| 39 | +abstract type AbstractOMPIClusterEnvDetector <: AbstractClusterEnvDetector end |
| 40 | + |
| 41 | +struct OpenMPIORTEEnvDetector <: AbstractOMPIClusterEnvDetector end |
| 42 | +struct OpenMPIPMIXEnvDetector <: AbstractOMPIClusterEnvDetector end |
| 43 | + |
| 44 | +struct MPIEnvDetector <: AbstractClusterEnvDetector end |
| 45 | + |
| 46 | +# Based on https://github.com/jax-ml/jax/blob/b0117366686ab084d38ad2657d9a2ae3a581ca7e/jax/_src/clusters/cluster.py |
| 47 | + |
| 48 | +is_env_present(::AbstractClusterEnvDetector) = false |
| 49 | + |
| 50 | +function get_coordinator_address end |
| 51 | +function get_process_count end |
| 52 | +function get_process_id end |
| 53 | +function get_local_process_id end |
| 54 | + |
| 55 | +function auto_detect_unset_distributed_params(; |
| 56 | + detector_list=[OpenMPIORTEEnvDetector(), OpenMPIPMIXEnvDetector(), MPIEnvDetector()], |
| 57 | + coordinator_address::Union{Nothing,String}=nothing, |
| 58 | + num_processes::Union{Nothing,Integer}=nothing, |
| 59 | + process_id::Union{Nothing,Integer}=nothing, |
| 60 | + local_device_ids::Union{Nothing,Vector{Int}}=nothing, |
| 61 | + initialization_timeout_in_seconds::Integer=300, |
| 62 | +) |
| 63 | + if all( |
| 64 | + Base.Fix2(!==, nothing), |
| 65 | + (coordinator_address, num_processes, process_id, local_device_ids), |
| 66 | + ) |
| 67 | + return coordinator_address, num_processes, process_id, local_device_ids |
| 68 | + end |
| 69 | + |
| 70 | + idx = findfirst(is_env_present, detector_list) |
| 71 | + if idx === nothing |
| 72 | + error("Couldn't find a functional cluster environment detector. Attempted to use: \ |
| 73 | + $(detector_list)") |
| 74 | + end |
| 75 | + |
| 76 | + detector = detector_list[idx] |
| 77 | + |
| 78 | + @debug "Detected cluster environment" detector |
| 79 | + |
| 80 | + if coordinator_address === nothing |
| 81 | + coordinator_address = get_coordinator_address( |
| 82 | + detector, initialization_timeout_in_seconds |
| 83 | + ) |
| 84 | + end |
| 85 | + |
| 86 | + if num_processes === nothing |
| 87 | + num_processes = get_process_count(detector) |
| 88 | + end |
| 89 | + |
| 90 | + if process_id === nothing |
| 91 | + process_id = get_process_id(detector) |
| 92 | + end |
| 93 | + |
| 94 | + if local_device_ids === nothing |
| 95 | + local_device_ids = [get_local_process_id(detector)] |
| 96 | + end |
| 97 | + |
| 98 | + return coordinator_address, num_processes, process_id, local_device_ids |
| 99 | +end |
| 100 | + |
| 101 | +# OpenMPIORTEEnvDetector & OpenMPIPMIXEnvDetector |
| 102 | +# Based on https://github.com/jax-ml/jax/blob/b0117366686ab084d38ad2657d9a2ae3a581ca7e/jax/_src/clusters/ompi_cluster.py and adapted for latest OpenMPI versions |
| 103 | +const _ORTE_URI = "OMPI_MCA_orte_hnp_uri" |
| 104 | +const _PMIX_SERVER_URI = ( |
| 105 | + "PMIX_SERVER_URI2", |
| 106 | + "PMIX_SERVER_URI3", |
| 107 | + "PMIX_SERVER_URI4", |
| 108 | + "PMIX_SERVER_URI41", |
| 109 | + "PMIX_SERVER_URI21", |
| 110 | +) |
| 111 | +const _OMPI_PROCESS_COUNT = "OMPI_COMM_WORLD_SIZE" |
| 112 | +const _OMPI_PROCESS_ID = "OMPI_COMM_WORLD_RANK" |
| 113 | +const _OMPI_LOCAL_PROCESS_ID = "OMPI_COMM_WORLD_LOCAL_RANK" |
| 114 | + |
| 115 | +is_env_present(::OpenMPIORTEEnvDetector) = haskey(ENV, _ORTE_URI) |
| 116 | +is_env_present(::OpenMPIPMIXEnvDetector) = any(Base.Fix1(haskey, ENV), _PMIX_SERVER_URI) |
| 117 | + |
| 118 | +function get_coordinator_address(::OpenMPIORTEEnvDetector, ::Integer) |
| 119 | + orte_uri = ENV[_ORTE_URI] |
| 120 | + |
| 121 | + job_id = parse(Int, split(orte_uri, '.'; limit=2)[1]) |
| 122 | + port = job_id % 2^12 + (65535 - 2^12 + 1) |
| 123 | + |
| 124 | + launcher_ip_match = match(r"tcp://(.+?)[,:]|tcp6://\[(.+?)[,\]]", orte_uri) |
| 125 | + |
| 126 | + @assert launcher_ip_match !== nothing "Could not parse coordinator IP address from \ |
| 127 | + Open MPI environment." |
| 128 | + |
| 129 | + launcher_ip = launcher_ip_match.captures[findfirst( |
| 130 | + !isnothing, launcher_ip_match.captures |
| 131 | + )] |
| 132 | + return "$(launcher_ip):$(port)" |
| 133 | +end |
| 134 | + |
| 135 | +function get_coordinator_address(::OpenMPIPMIXEnvDetector, ::Integer) |
| 136 | + varname = findfirst(Base.Fix1(haskey, ENV), _PMIX_SERVER_URI) |
| 137 | + pmix_uri = ENV[_PMIX_SERVER_URI[varname]] |
| 138 | + |
| 139 | + job_id = parse(Int, split(split(pmix_uri, '-'; limit=3)[3], "@"; limit=2)[1]) |
| 140 | + port = job_id % 2^12 + (65535 - 2^12 + 1) |
| 141 | + |
| 142 | + launcher_ip_match = match(r"tcp4://(.+?):|tcp6://\[(.+?)\]", pmix_uri) |
| 143 | + |
| 144 | + @assert launcher_ip_match !== nothing "Could not parse coordinator IP address from \ |
| 145 | + Open MPI environment." |
| 146 | + |
| 147 | + launcher_ip = launcher_ip_match.captures[findfirst( |
| 148 | + !isnothing, launcher_ip_match.captures |
| 149 | + )] |
| 150 | + |
| 151 | + return "$(launcher_ip):$(port)" |
| 152 | +end |
| 153 | + |
| 154 | +get_process_count(::AbstractOMPIClusterEnvDetector) = parse(Int, ENV[_OMPI_PROCESS_COUNT]) |
| 155 | + |
| 156 | +get_process_id(::AbstractOMPIClusterEnvDetector) = parse(Int, ENV[_OMPI_PROCESS_ID]) |
| 157 | + |
| 158 | +function get_local_process_id(::AbstractOMPIClusterEnvDetector) |
| 159 | + return parse(Int, ENV[_OMPI_LOCAL_PROCESS_ID]) |
| 160 | +end |
| 161 | + |
| 162 | +end |
0 commit comments