sharded_client.rs 3.17 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
use tonic::transport::Uri;
7
use tracing::instrument;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
8

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
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
23
        let uris = master_client.service_discovery().await.unwrap();
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
    /// Clear the past generations cache
42
    #[instrument(skip(self))]
43
    pub async fn clear_cache(&mut self, batch_id: Option<u64>) -> Result<()> {
44
45
46
        let futures: Vec<_> = self
            .clients
            .iter_mut()
47
            .map(|client| client.clear_cache(batch_id))
48
49
50
51
            .collect();
        join_all(futures).await.into_iter().collect()
    }

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

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