"sgl-kernel/git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "ac2a723bb3ec83d9f7eadb09f7c07e6d7a4f5042"
lib.rs 2.84 KB
Newer Older
1
2
use pyo3::prelude::*;
pub mod router;
3
pub mod server;
4
pub mod tree;
5

6
7
8
9
10
#[pyclass(eq)]
#[derive(Clone, PartialEq)]
pub enum PolicyType {
    Random,
    RoundRobin,
11
    CacheAware,
12
13
}

14
15
16
17
18
#[pyclass]
struct Router {
    host: String,
    port: u16,
    worker_urls: Vec<String>,
19
    policy: PolicyType,
20
    cache_threshold: f32,
21
22
    balance_abs_threshold: usize,
    balance_rel_threshold: f32,
23
24
    eviction_interval_secs: u64,
    max_tree_size: usize,
25
    max_payload_size: usize,
26
    verbose: bool,
27
28
29
30
31
}

#[pymethods]
impl Router {
    #[new]
32
33
34
35
36
    #[pyo3(signature = (
        worker_urls,
        policy = PolicyType::RoundRobin,
        host = String::from("127.0.0.1"),
        port = 3001,
37
        cache_threshold = 0.50,
38
39
        balance_abs_threshold = 32,
        balance_rel_threshold = 1.0001,
40
        eviction_interval_secs = 60,
41
        max_tree_size = 2usize.pow(24),
42
        max_payload_size = 4 * 1024 * 1024,
43
        verbose = false
44
45
46
47
48
49
    ))]
    fn new(
        worker_urls: Vec<String>,
        policy: PolicyType,
        host: String,
        port: u16,
50
        cache_threshold: f32,
51
52
        balance_abs_threshold: usize,
        balance_rel_threshold: f32,
53
54
        eviction_interval_secs: u64,
        max_tree_size: usize,
55
        max_payload_size: usize,
56
        verbose: bool,
57
58
    ) -> PyResult<Self> {
        Ok(Router {
59
60
61
            host,
            port,
            worker_urls,
62
            policy,
63
            cache_threshold,
64
65
            balance_abs_threshold,
            balance_rel_threshold,
66
67
            eviction_interval_secs,
            max_tree_size,
68
            max_payload_size,
69
            verbose,
70
        })
71
72
73
    }

    fn start(&self) -> PyResult<()> {
74
75
76
        let policy_config = match &self.policy {
            PolicyType::Random => router::PolicyConfig::RandomConfig,
            PolicyType::RoundRobin => router::PolicyConfig::RoundRobinConfig,
77
78
            PolicyType::CacheAware => router::PolicyConfig::CacheAwareConfig {
                cache_threshold: self.cache_threshold,
79
80
                balance_abs_threshold: self.balance_abs_threshold,
                balance_rel_threshold: self.balance_rel_threshold,
81
82
                eviction_interval_secs: self.eviction_interval_secs,
                max_tree_size: self.max_tree_size,
83
84
            },
        };
85
86

        actix_web::rt::System::new().block_on(async move {
87
88
89
90
91
92
            server::startup(server::ServerConfig {
                host: self.host.clone(),
                port: self.port,
                worker_urls: self.worker_urls.clone(),
                policy_config,
                verbose: self.verbose,
93
                max_payload_size: self.max_payload_size,
94
95
96
            })
            .await
            .unwrap();
97
98
99
100
101
102
103
        });

        Ok(())
    }
}

#[pymodule]
104
fn sglang_router_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
105
    m.add_class::<PolicyType>()?;
106
107
    m.add_class::<Router>()?;
    Ok(())
108
}