-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathhealth.rs
179 lines (168 loc) · 5.75 KB
/
health.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use lorax_client::{
input_chunk, Batch, InputChunk, NextTokenChooserParameters, Request, ShardInfo, ShardedClient,
StoppingCriteriaParameters, TokenizedInputs,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
// Note: Request ids and batch ids cannot collide.
#[allow(dead_code)]
const LIVENESS_ID: u64 = u64::MAX;
const BATCH_ID: u64 = u64::MAX;
#[derive(Clone, Debug)]
pub(crate) struct Health {
client: ShardedClient,
inference_health: Arc<AtomicBool>,
shard_info: ShardInfo,
}
impl Health {
pub(crate) fn new(
client: ShardedClient,
inference_health: Arc<AtomicBool>,
shard_info: ShardInfo,
) -> Self {
Self {
#[allow(dead_code)]
client,
#[allow(dead_code)]
inference_health,
shard_info,
}
}
pub(crate) fn shard_info(&self) -> &ShardInfo {
&self.shard_info
}
pub(crate) async fn check_generation(&mut self) -> bool {
let generation_liveness_request = Request {
id: LIVENESS_ID,
inputs: "liveness".to_string(),
tokenized_inputs: Some(TokenizedInputs {
ids: vec![75],
input_chunks: vec![InputChunk {
chunk: Some(input_chunk::Chunk::Text("liveness".to_string())),
}],
}),
truncate: 10,
prefill_logprobs: false,
parameters: Some(NextTokenChooserParameters {
temperature: 1.0,
top_k: 0,
top_p: 1.0,
typical_p: 1.0,
do_sample: false,
seed: 0,
repetition_penalty: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
watermark: false,
adapter_id: "".to_string(),
schema: None,
return_k_alternatives: 0,
}),
stopping_parameters: Some(StoppingCriteriaParameters {
max_new_tokens: 1,
stop_sequences: vec![],
ignore_eos_token: false,
}),
adapter_index: 0,
// Block 0 is reserved for health checks
blocks: vec![0],
slots: (0..self.shard_info.block_size).collect(),
cache_len: 0,
chunk_len: None,
};
let batch = Batch {
id: BATCH_ID,
requests: vec![generation_liveness_request],
size: 1,
max_tokens: 2,
max_blocks: 1,
};
// Skips the queue
self.client.prefill(batch, None).await.is_ok()
}
pub(crate) async fn check_classification(&mut self) -> bool {
let classify_request = Request {
id: LIVENESS_ID,
inputs: "liveness".to_string(),
tokenized_inputs: Some(TokenizedInputs {
ids: vec![75],
input_chunks: vec![InputChunk {
chunk: Some(input_chunk::Chunk::Text("liveness".to_string())),
}],
}),
truncate: 10,
prefill_logprobs: false,
parameters: None,
stopping_parameters: None,
adapter_index: 0,
blocks: vec![0],
slots: (0..self.shard_info.block_size).collect(),
cache_len: 0,
chunk_len: None,
};
let batch = Batch {
id: BATCH_ID,
requests: vec![classify_request],
size: 1,
max_tokens: 2,
max_blocks: 1,
};
self.client.classify(batch).await.is_ok()
}
pub(crate) async fn check_embeddings(&mut self) -> bool {
let embed_request = Request {
id: LIVENESS_ID,
inputs: "liveness".to_string(),
tokenized_inputs: Some(TokenizedInputs {
ids: vec![75],
input_chunks: vec![InputChunk {
chunk: Some(input_chunk::Chunk::Text("liveness".to_string())),
}],
}),
truncate: 10,
prefill_logprobs: false,
parameters: None,
stopping_parameters: None,
adapter_index: 0,
blocks: vec![0],
slots: (0..self.shard_info.block_size).collect(),
cache_len: 0,
chunk_len: None,
};
let batch = Batch {
id: BATCH_ID,
requests: vec![embed_request],
size: 1,
max_tokens: 2,
max_blocks: 1,
};
self.client.embed(batch).await.is_ok()
}
#[allow(dead_code)]
pub(crate) async fn check(&mut self) -> bool {
if self.inference_health.load(Ordering::SeqCst) {
// Generation is healthy, we only check that the shards are answering gRPC calls
self.client.health().await.is_ok()
} else {
// Generation is unhealthy or have not sent any generation request yet
let mut value = false;
// Dummy batch of 1 token and 1 generated token
if self.shard_info().supports_generation {
value = self.check_generation().await;
// Update generation health
self.inference_health.store(value, Ordering::SeqCst);
}
if self.shard_info().supports_classification {
value = self.check_classification().await;
// Update generation health
self.inference_health.store(value, Ordering::SeqCst);
}
if self.shard_info().supports_embeddings {
value = self.check_embeddings().await;
// Update generation health
self.inference_health.store(value, Ordering::SeqCst);
}
value
}
}
}