-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
533 lines (510 loc) · 19.4 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use async_stream::try_stream;
use futures_core::Stream;
use rustboard_server::logdir::LogdirLoader;
use std::borrow::Borrow;
use std::collections::HashSet;
use std::default::Default;
use std::hash::Hash;
use std::path::PathBuf;
use std::pin::Pin;
use std::thread;
use std::time::Duration;
use tonic::{transport::Server, Request, Response, Status};
use rustboard_server::blob_key::BlobKey;
use rustboard_server::commit::Commit;
use rustboard_server::proto::tensorboard::data;
use data::tensor_board_data_provider_server as tbdps;
use tbdps::{TensorBoardDataProvider, TensorBoardDataProviderServer};
const BLOB_BATCH_SIZE: usize = 1024 * 1024 * 8;
const RELOAD_INTERVAL: Duration = Duration::from_secs(5);
struct DataProvider<'a> {
head: &'a Commit,
}
#[tonic::async_trait]
// TODO(@wchargin): Figure out if we can make this broader than 'static.
impl TensorBoardDataProvider for DataProvider<'static> {
async fn list_runs(
&self,
_request: Request<data::ListRunsRequest>,
) -> Result<Response<data::ListRunsResponse>, Status> {
let mut res = data::ListRunsResponse::default();
let start_times = self.head.start_times.read();
for (run_name, start_time) in &*start_times {
res.runs.push(data::Run {
id: run_name.clone(),
name: run_name.clone(),
start_time: *start_time,
});
}
Ok(Response::new(res))
}
async fn list_scalars(
&self,
req: Request<data::ListScalarsRequest>,
) -> Result<Response<data::ListScalarsResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ListScalarsResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.scalars.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::list_scalars_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res = data::list_scalars_response::TagEntry::default();
tag_res.tag_name = tag.clone();
tag_res.time_series = Some(data::ScalarTimeSeries {
max_step: ts.values.last().map(|(step, _)| *step).unwrap_or(0),
max_wall_time: ts
.values
.iter()
.map(|(_, (wt, _))| *wt)
.max_by(|x, y| x.partial_cmp(y).unwrap())
.unwrap_or(-f64::INFINITY),
summary_metadata: Some(ts.metadata.clone()),
});
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
async fn read_scalars(
&self,
req: Request<data::ReadScalarsRequest>,
) -> Result<Response<data::ReadScalarsResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ReadScalarsResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.scalars.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::read_scalars_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res: data::read_scalars_response::TagEntry = Default::default();
tag_res.tag_name = tag.clone();
let mut data: data::ScalarData = Default::default();
for (step, (wall_time, maybe_value)) in &ts.values {
match maybe_value {
Ok(value) => {
data.step.push(*step);
data.wall_time.push(*wall_time);
data.value.push(value.0);
}
Err(_) => {
eprintln!("dropping corrupt datum at step {}", *step);
}
};
}
tag_res.data = Some(data);
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
async fn list_tensors(
&self,
req: Request<data::ListTensorsRequest>,
) -> Result<Response<data::ListTensorsResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ListTensorsResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.tensors.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::list_tensors_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res = data::list_tensors_response::TagEntry::default();
tag_res.tag_name = tag.clone();
tag_res.time_series = Some(data::TensorTimeSeries {
max_step: ts.values.last().map(|(step, _)| *step).unwrap_or(0),
max_wall_time: ts
.values
.iter()
.map(|(_, (wt, _))| *wt)
.max_by(|x, y| x.partial_cmp(y).unwrap())
.unwrap_or(-f64::INFINITY),
summary_metadata: Some(ts.metadata.clone()),
});
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
async fn read_tensors(
&self,
req: Request<data::ReadTensorsRequest>,
) -> Result<Response<data::ReadTensorsResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ReadTensorsResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.tensors.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::read_tensors_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res: data::read_tensors_response::TagEntry = Default::default();
tag_res.tag_name = tag.clone();
let mut data: data::TensorData = Default::default();
for (step, (wall_time, maybe_value)) in &ts.values {
match maybe_value {
Ok(value) => {
data.step.push(*step);
data.wall_time.push(*wall_time);
data.value.push(value.0.clone());
}
Err(_) => {
eprintln!("dropping corrupt datum at step {}", *step);
}
};
}
tag_res.data = Some(data);
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
async fn list_blob_sequences(
&self,
req: Request<data::ListBlobSequencesRequest>,
) -> Result<Response<data::ListBlobSequencesResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ListBlobSequencesResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.blob_sequences.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::list_blob_sequences_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res = data::list_blob_sequences_response::TagEntry::default();
tag_res.tag_name = tag.clone();
tag_res.time_series = Some(data::BlobSequenceTimeSeries {
max_step: ts.values.last().map(|(step, _)| *step).unwrap_or(0),
max_wall_time: ts
.values
.iter()
.map(|(_, (wt, _))| *wt)
.max_by(|x, y| x.partial_cmp(y).unwrap())
.unwrap_or(-f64::INFINITY),
max_length: ts
.values
.iter()
.filter_map(|(_, (_, blobs))| {
blobs.as_ref().ok().map(|bsv| bsv.0.len() as i64)
})
.max()
.unwrap_or(-1),
summary_metadata: Some(ts.metadata.clone()),
});
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
async fn read_blob_sequences(
&self,
req: Request<data::ReadBlobSequencesRequest>,
) -> Result<Response<data::ReadBlobSequencesResponse>, Status> {
let mut req = req.into_inner();
let mut res: data::ReadBlobSequencesResponse = Default::default();
let (run_filter, tag_filter) = parse_rtf(req.run_tag_filter.take());
let want_plugin = req
.plugin_filter
.map(|pf| pf.plugin_name)
.unwrap_or_default();
let store = self.head.blob_sequences.read();
for (run, tag_map) in &*store {
if !run_filter.want(run) {
continue;
}
let tags = tag_map.read();
let mut run_res = data::read_blob_sequences_response::RunEntry::default();
for (tag, ts) in &*tags {
if !tag_filter.want(tag) {
continue;
}
if ts
.metadata
.plugin_data
.as_ref()
.map(|pd| pd.plugin_name.as_str())
!= Some(want_plugin.as_str())
{
continue;
}
let mut tag_res: data::read_blob_sequences_response::TagEntry = Default::default();
tag_res.tag_name = tag.clone();
let mut data: data::BlobSequenceData = Default::default();
for (step, (wall_time, maybe_value)) in &ts.values {
match maybe_value {
Ok(value) => {
data.step.push(*step);
data.wall_time.push(*wall_time);
let experiment_id = req.experiment_id.as_ref();
let sample_count = value.0.len();
use std::borrow::Cow;
let blob_refs = (0..sample_count)
.map(|index| {
let bk = BlobKey {
experiment_id: Cow::Borrowed(experiment_id),
run: Cow::Borrowed(run.as_ref()),
tag: Cow::Borrowed(tag.as_ref()),
step: *step,
index,
};
data::BlobReference {
blob_key: bk.to_string(),
url: String::new(),
}
})
.collect();
data.values.push(data::BlobReferenceSequence { blob_refs });
}
Err(_) => {
eprintln!("dropping corrupt datum at step {}", *step);
}
};
}
tag_res.data = Some(data);
run_res.tags.push(tag_res);
}
if !run_res.tags.is_empty() {
run_res.run_name = run.clone();
res.runs.push(run_res);
}
}
Ok(Response::new(res))
}
type ReadBlobStream =
Pin<Box<dyn Stream<Item = Result<data::ReadBlobResponse, Status>> + Send + Sync + 'static>>;
async fn read_blob(
&self,
req: Request<data::ReadBlobRequest>,
) -> Result<Response<Self::ReadBlobStream>, Status> {
let req = req.into_inner();
let bk: BlobKey = match req.blob_key.parse() {
Err(e) => {
return Err(Status::invalid_argument(format!(
"failed to parse blob key: {:?}",
e,
)))
}
Ok(bk) => bk,
};
let store = self.head.blob_sequences.read();
let tag_map = store
.get(bk.run.as_ref())
.ok_or_else(|| Status::not_found(format!("no such run: {:?}", bk.run)))?
.read();
let ts = tag_map.get(bk.tag.as_ref()).ok_or_else(|| {
Status::not_found(format!("run {:?} has no such tag: {:?}", bk.run, bk.tag))
})?;
let datum = ts
.values
.iter()
.find_map(
|(step, (_, value))| {
if *step == bk.step {
Some(value)
} else {
None
}
},
)
.ok_or_else(|| {
Status::not_found(format!(
"run {:?}, tag {:?} has no step {}; may have been evicted",
bk.run, bk.tag, bk.step
))
})?
.as_ref()
.map_err(|_| {
Status::data_loss(format!(
"blob sequence for run {:?}, tag {:?}, step {} has invalid data",
bk.run, bk.tag, bk.step
))
})?;
let blob = datum.0.get(bk.index).ok_or_else(|| {
Status::not_found(format!(
"blob sequence at run {:?}, tag {:?}, step {:?} has no index {} (length: {})",
bk.run,
bk.tag,
bk.step,
bk.index,
datum.0.len()
))
})?;
let blob = blob.clone();
drop(tag_map);
drop(store);
let stream = try_stream! {
for chunk in blob.chunks(BLOB_BATCH_SIZE) {
yield data::ReadBlobResponse {data: chunk.to_vec()};
}
};
Ok(Response::new(Box::pin(stream) as Self::ReadBlobStream))
}
}
fn parse_rtf(rtf: Option<data::RunTagFilter>) -> (StringFilter, StringFilter) {
let rtf = rtf.unwrap_or_default();
let run_filter = match rtf.runs {
None => StringFilter::All,
Some(data::RunFilter { runs }) => StringFilter::Just(runs.into_iter().collect()),
};
let tag_filter = match rtf.tags {
None => StringFilter::All,
Some(data::TagFilter { tags }) => StringFilter::Just(tags.into_iter().collect()),
};
(run_filter, tag_filter)
}
enum StringFilter {
All,
Just(HashSet<String>),
}
impl StringFilter {
pub fn want<Q: ?Sized>(&self, value: &Q) -> bool
where
String: Borrow<Q>,
Q: Hash + Eq,
{
match self {
StringFilter::All => true,
StringFilter::Just(some) => some.contains(value),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let logdir = std::env::args_os()
.nth(1)
.expect("give logdir as first argument");
// leak commit because gRPC server requires static lifetime
let commit: &'static Commit = Box::leak(Box::new(Commit::new()));
thread::Builder::new()
.name("Reloader".into())
.spawn(move || {
let mut loader = LogdirLoader::new(commit, PathBuf::from(logdir));
loop {
loader.reload();
thread::sleep(RELOAD_INTERVAL);
}
})?;
let addr = "[::1]:6206".parse().unwrap();
let dp = DataProvider { head: commit };
let svc = TensorBoardDataProviderServer::new(dp);
Server::builder().add_service(svc).serve(addr).await?;
Ok(())
}