mod.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. pub mod commands;
  2. pub mod ops;
  3. pub mod serve;
  4. pub mod session;
  5. use anyhow::Result;
  6. use crate::cli::TmuxCommands;
  7. use crate::config::{Config, NcclConfig, Node, Recipe};
  8. /// Manages tmux sessions for model inference across a DGX Spark cluster.
  9. #[derive(Clone)]
  10. pub struct TmuxManager;
  11. impl TmuxManager {
  12. /// Generates a namespaced session name for a recipe and node rank.
  13. /// Format: `{recipe_name}-{engine}-r{rank}`
  14. pub fn session_name(&self, recipe: &Recipe, rank: u32) -> String {
  15. let name = recipe.name.replace(' ', "_");
  16. format!("{}-{}-r{}", name, recipe.engine, rank)
  17. }
  18. /// Builds the list of `export` commands for NCCL environment variables.
  19. pub fn build_nccl_setup(&self, nccl: &NcclConfig) -> Vec<String> {
  20. vec![
  21. format!("export NCCL_IB_HCA=\"{}\"", nccl.ib_hca),
  22. format!("export NCCL_SOCKET_IFNAME=\"{}\"", nccl.socket_ifname),
  23. format!("export NCCL_IB_GID_INDEX={}", nccl.ib_gid_index),
  24. format!("export NCCL_IB_DISABLE={}", nccl.ib_disable),
  25. ]
  26. }
  27. /// Builds engine-specific host IP environment variable.
  28. pub fn build_host_ip_export(&self, engine: &str, node: &Node) -> Vec<String> {
  29. match engine {
  30. "vllm" => vec![format!("export VLLM_HOST_IP={}", node.ib_ip)],
  31. "sglang" => vec![format!("export SGLANG_WORKER_IP={}", node.ib_ip)],
  32. _ => vec![
  33. format!("export VLLM_HOST_IP={}", node.ib_ip),
  34. format!("export SGLANG_WORKER_IP={}", node.ib_ip),
  35. ],
  36. }
  37. }
  38. }
  39. /// Detailed health status of a tmux session.
  40. #[derive(Debug, Clone)]
  41. pub struct SessionHealth {
  42. pub session_exists: bool,
  43. pub process_alive: bool,
  44. pub gpu_ok: bool,
  45. pub api_ok: bool,
  46. }
  47. impl SessionHealth {
  48. pub fn is_healthy(&self) -> bool {
  49. self.session_exists && self.process_alive && self.gpu_ok && self.api_ok
  50. }
  51. pub fn is_degraded(&self) -> bool {
  52. self.session_exists && !self.process_alive
  53. }
  54. }
  55. /// Top-level handler for all `cargo run tmux <subcommand>` commands.
  56. pub async fn handle_tmux(config: &Config, cmd: TmuxCommands, dry_run: bool) -> Result<()> {
  57. match cmd {
  58. TmuxCommands::Serve {
  59. recipe,
  60. cluster,
  61. skip_before_serve,
  62. } => {
  63. serve::tmux_serve(config, &recipe, &cluster, skip_before_serve, dry_run).await?;
  64. }
  65. TmuxCommands::Stop { recipe, cluster } => {
  66. ops::tmux_stop(config, &recipe, &cluster, dry_run).await?;
  67. }
  68. TmuxCommands::Status { recipe, cluster } => {
  69. ops::tmux_status(config, &recipe, &cluster, dry_run).await?;
  70. }
  71. TmuxCommands::Logs {
  72. recipe,
  73. follow,
  74. cluster,
  75. } => {
  76. ops::tmux_logs(config, &recipe, &cluster, follow, dry_run).await?;
  77. }
  78. TmuxCommands::Attach { recipe, cluster } => {
  79. ops::tmux_attach(config, &recipe, &cluster, dry_run).await?;
  80. }
  81. }
  82. Ok(())
  83. }