sharded_client.rs 4.14 KB
Newer Older
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
1
/// Multi shard Client
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
2
use crate::Result;
3
use crate::{Batch, Client, Generation, HealthResponse, Request, ShardInfo};
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
4
5
use futures::future::join_all;
use tonic::transport::Uri;
6
use tracing::instrument;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
7

8
#[derive(Debug, Clone)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
9
/// Text Generation Inference gRPC multi client
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
10
pub struct ShardedClient {
11
    clients: Vec<Client>,
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
12
13
14
}

impl ShardedClient {
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
15
    fn new(clients: Vec<Client>) -> Self {
16
        Self { clients }
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
17
18
    }

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
19
20
    /// Create a new ShardedClient from a master client. The master client will communicate with
    /// the other shards and returns all uris/unix sockets with the `service_discovery` gRPC method.
Olivier Dehaene's avatar
Olivier Dehaene committed
21
    async fn from_master_client(mut master_client: Client) -> Result<Self> {
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
22
        // Get all uris/unix sockets from the master client
23
        let uris = master_client.service_discovery().await?;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
24
        let futures = uris.into_iter().map(Client::connect_uds);
Olivier Dehaene's avatar
Olivier Dehaene committed
25
26
        let clients: Result<Vec<Client>> = join_all(futures).await.into_iter().collect();
        Ok(Self::new(clients?))
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
27
28
    }

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
29
    /// Returns a client connected to the given uri
Olivier Dehaene's avatar
Olivier Dehaene committed
30
31
    pub async fn connect(uri: Uri) -> Result<Self> {
        let master_client = Client::connect(uri).await?;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
32
33
34
        Self::from_master_client(master_client).await
    }

Olivier Dehaene's avatar
Olivier Dehaene committed
35
36
37
    /// Returns a client connected to the given unix socket
    pub async fn connect_uds(path: String) -> Result<Self> {
        let master_client = Client::connect_uds(path).await?;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
38
39
40
        Self::from_master_client(master_client).await
    }

41
42
43
44
45
46
47
48
49
50
51
    /// Get the model info
    #[instrument(skip(self))]
    pub async fn info(&mut self) -> Result<ShardInfo> {
        let futures: Vec<_> = self
            .clients
            .iter_mut()
            .map(|client| client.info())
            .collect();
        join_all(futures).await.pop().unwrap()
    }

52
53
54
55
56
57
58
59
60
61
62
    /// GRPC health check
    #[instrument(skip(self))]
    pub async fn health(&mut self) -> Result<HealthResponse> {
        let futures: Vec<_> = self
            .clients
            .iter_mut()
            .map(|client| client.health())
            .collect();
        join_all(futures).await.pop().unwrap()
    }

63
    /// Clear the past generations cache
64
    #[instrument(skip(self))]
65
    pub async fn clear_cache(&mut self, batch_id: Option<u64>) -> Result<()> {
66
67
68
        let futures: Vec<_> = self
            .clients
            .iter_mut()
69
            .map(|client| client.clear_cache(batch_id))
70
71
72
73
            .collect();
        join_all(futures).await.into_iter().collect()
    }

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    /// Filter a cached batch
    #[instrument(skip(self))]
    pub async fn filter_batch(
        &mut self,
        batch_id: u64,
        keep_requests: Vec<Request>,
    ) -> Result<Option<Batch>> {
        let futures: Vec<_> = self
            .clients
            .iter_mut()
            .map(|client| Box::pin(client.filter_batch(batch_id, keep_requests.clone())))
            .collect();
        // all shards return the same message
        join_all(futures).await.pop().unwrap()
    }

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
90
91
    /// Generate one token for each request in the given batch
    ///
92
    /// Returns Generation for each request in batch
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
93
    /// and the next cached batch
94
    #[instrument(skip_all, fields(id = &batch.id, size = &batch.size))]
95
    pub async fn prefill(&mut self, batch: Batch) -> Result<(Vec<Generation>, Option<Batch>)> {
96
97
98
        let futures: Vec<_> = self
            .clients
            .iter_mut()
99
            .map(|client| Box::pin(client.prefill(batch.clone())))
100
            .collect();
101
102
        // all shards return the same message
        join_all(futures).await.pop().unwrap()
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
103
104
    }

105
    /// Generate one token for each request in the given cached batches
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
106
    ///
107
    /// Returns Generation for each request in batches
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
108
    /// and the next cached batch
109
    #[instrument(skip_all, fields(size = batches.iter().map(|batch|{batch.size}).sum::<u32>()))]
110
    pub async fn decode(
111
        &mut self,
Olivier Dehaene's avatar
Olivier Dehaene committed
112
        batches: Vec<Batch>,
113
    ) -> Result<(Vec<Generation>, Option<Batch>)> {
114
115
116
        let futures: Vec<_> = self
            .clients
            .iter_mut()
117
            .map(|client| Box::pin(client.decode(batches.clone())))
118
            .collect();
119
120
        // all shards return the same message
        join_all(futures).await.pop().unwrap()
Olivier Dehaene's avatar
Olivier Dehaene committed
121
    }
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
122
}