|
| 1 | +/* Copyright 2025 The xLLM Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + https://github.com/jd-opensource/xllm/blob/main/LICENSE |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "spawn_worker_server.h" |
| 17 | + |
| 18 | +#include <absl/strings/str_split.h> |
| 19 | +#if defined(USE_NPU) |
| 20 | +#include <acl/acl.h> |
| 21 | +#endif |
| 22 | +#include <signal.h> |
| 23 | +#include <sys/prctl.h> |
| 24 | + |
| 25 | +#include <cstdlib> |
| 26 | + |
| 27 | +#include "core/distributed_runtime/worker_server.h" |
| 28 | +#include "core/platform/device.h" |
| 29 | +#include "core/runtime/options.h" |
| 30 | + |
| 31 | +namespace xllm { |
| 32 | + |
| 33 | +bool xllm::SpawnWorkerServer::g_running_ = true; |
| 34 | + |
| 35 | +SpawnWorkerServer::SpawnWorkerServer(const std::string& master_node_addr, |
| 36 | + int local_rank, |
| 37 | + int global_rank, |
| 38 | + int world_size, |
| 39 | + int device_idx, |
| 40 | + int num_decoding_tokens, |
| 41 | + int block_size) { |
| 42 | + // TODO: pass whole xllm::runtime::Options here from main process. |
| 43 | + xllm::runtime::Options runner_options; |
| 44 | + runner_options.block_size(block_size) |
| 45 | + .num_decoding_tokens(num_decoding_tokens) |
| 46 | + .enable_schedule_overlap(false) |
| 47 | + .enable_offline_inference(true) |
| 48 | + .master_node_addr(master_node_addr); |
| 49 | + FLAGS_enable_schedule_overlap = false; |
| 50 | + FLAGS_master_node_addr = master_node_addr; |
| 51 | + FLAGS_block_size = block_size; |
| 52 | + |
| 53 | + std::atomic<bool> done(false); |
| 54 | +#if defined(USE_NPU) |
| 55 | + xllm::Device device("npu:" + std::to_string(device_idx)); |
| 56 | + device.set_device(); |
| 57 | + device.init_device_context(); |
| 58 | + FLAGS_enable_atb_comm_multiprocess = true; |
| 59 | +#endif |
| 60 | + |
| 61 | + ParallelArgs parallel_args(global_rank, world_size, 1, nullptr, 1); |
| 62 | + WorkerServer worker_server(local_rank, |
| 63 | + master_node_addr, |
| 64 | + done, |
| 65 | + parallel_args, |
| 66 | + device, |
| 67 | + runner_options, |
| 68 | + WorkerType::LLM, |
| 69 | + false); |
| 70 | +} |
| 71 | + |
| 72 | +void SpawnWorkerServer::handle_signal(int signum) { g_running_ = false; } |
| 73 | + |
| 74 | +void SpawnWorkerServer::run() { |
| 75 | + signal(SIGINT, SpawnWorkerServer::handle_signal); |
| 76 | + signal(SIGTERM, SpawnWorkerServer::handle_signal); |
| 77 | + |
| 78 | + // main thread waiting here |
| 79 | + while (SpawnWorkerServer::g_running_) { |
| 80 | + sleep(5); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace xllm |
0 commit comments