sharded_client.rs 2.92 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};
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
4
use futures::future::join_all;
5
use futures::future::select_all;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
6
7
use tonic::transport::Uri;

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
8
/// Text Generation Inference gRPC multi client
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
9
pub struct ShardedClient {
10
    clients: Vec<Client>,
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
11
12
13
}

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

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
18
19
    /// 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
20
    async fn from_master_client(mut master_client: Client) -> Result<Self> {
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
21
        // Get all uris/unix sockets from the master client
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
22
        let uris = master_client.service_discovery().await.unwrap();
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
23
        let futures = uris.into_iter().map(Client::connect_uds);
Olivier Dehaene's avatar
Olivier Dehaene committed
24
25
        let clients: Result<Vec<Client>> = join_all(futures).await.into_iter().collect();
        Ok(Self::new(clients?))
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
26
27
    }

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

Olivier Dehaene's avatar
Olivier Dehaene committed
34
35
36
    /// 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
37
38
39
        Self::from_master_client(master_client).await
    }

40
41
42
43
44
45
46
47
48
49
    /// Clear the past generations cache
    pub async fn clear_cache(&mut self) -> Result<()> {
        let futures: Vec<_> = self
            .clients
            .iter_mut()
            .map(|client| client.clear_cache())
            .collect();
        join_all(futures).await.into_iter().collect()
    }

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
50
51
    /// Generate one token for each request in the given batch
    ///
52
    /// Returns Generation for each request in batch
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
53
    /// and the next cached batch
54
    pub async fn prefill(&mut self, batch: Batch) -> Result<(Vec<Generation>, Option<Batch>)> {
55
56
57
        let futures: Vec<_> = self
            .clients
            .iter_mut()
58
            .map(|client| Box::pin(client.prefill(batch.clone())))
59
            .collect();
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
60
        // As soon as we receive one response, we can return as all shards will return the same
61
62
        let (result, _, _) = select_all(futures).await;
        result
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
63
64
    }

65
    /// Generate one token for each request in the given cached batches
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
66
    ///
67
    /// Returns Generation for each request in batches
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
68
    /// and the next cached batch
69
    pub async fn decode(
70
        &mut self,
Olivier Dehaene's avatar
Olivier Dehaene committed
71
        batches: Vec<Batch>,
72
    ) -> Result<(Vec<Generation>, Option<Batch>)> {
73
74
75
        let futures: Vec<_> = self
            .clients
            .iter_mut()
76
            .map(|client| Box::pin(client.decode(batches.clone())))
77
            .collect();
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
78
        // As soon as we receive one response, we can return as all shards will return the same
79
80
        let (result, _, _) = select_all(futures).await;
        result
Olivier Dehaene's avatar
Olivier Dehaene committed
81
    }
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
82
}