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
use super::*;

/// A [Event] initializer.
#[derive(Debug, Clone, PartialEq)]
pub struct EventInit {
    /// The wall clock time in microseconds.
    ///
    /// If the field is set to `None`, it sets to current system time when the event is built.
    pub wall_time: Option<f64>,
    /// The global step.
    pub step: i64,
}

impl EventInit {
    /// Create a initializer with global step and wall time.
    pub fn new(step: i64, wall_time: f64) -> Self {
        Self {
            wall_time: Some(wall_time),
            step,
        }
    }

    /// Create a initializer with global step and without wall time.
    pub fn with_step(step: i64) -> Self {
        Self {
            wall_time: None,
            step,
        }
    }

    /// Build an empty event.
    pub fn build_empty(self) -> Event {
        let (wall_time, step) = self.to_parts();
        Event {
            wall_time,
            step,
            what: None,
        }
    }

    /// Build an event with summary.
    pub fn build_with_summary(self, summary: Summary) -> Event {
        let (wall_time, step) = self.to_parts();
        Event {
            wall_time,
            step,
            what: Some(What::Summary(summary)),
        }
    }

    fn to_parts(self) -> (f64, i64) {
        let Self {
            wall_time: wall_time_opt,
            step,
        } = self;
        let wall_time = wall_time_opt.unwrap_or_else(|| Self::get_wall_time());
        (wall_time, step)
    }

    fn get_wall_time() -> f64 {
        SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_nanos() as f64
            / 1.0e9
    }
}

impl From<i64> for EventInit {
    fn from(step: i64) -> Self {
        Self::with_step(step)
    }
}

impl From<(i64, f64)> for EventInit {
    fn from((step, wall_time): (i64, f64)) -> Self {
        Self::new(step, wall_time)
    }
}

impl From<(i64, SystemTime)> for EventInit {
    fn from((step, time): (i64, SystemTime)) -> Self {
        Self::new(
            step,
            time.duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_nanos() as f64
                / 1.0e9,
        )
    }
}

/// A [Summary] initializer.
#[derive(Debug, Clone, PartialEq)]
pub struct SummaryInit<T>
where
    T: ToString,
{
    /// The tag of the summary.
    pub tag: T,
}

impl<T> SummaryInit<T>
where
    T: ToString,
{
    /// Create an initializer with a tag.
    pub fn new(tag: T) -> Self {
        Self { tag }
    }

    /// Build a scalar summary.
    pub fn build_scalar(self, value: f32) -> Result<Summary, Error> {
        let Self { tag } = self;

        let summary = Summary {
            value: vec![Value {
                node_name: "".into(),
                tag: tag.to_string(),
                metadata: None,
                value: Some(ValueEnum::SimpleValue(value)),
            }],
        };
        Ok(summary)
    }

    /// Build a histogram summary.
    pub fn build_histogram<H, E>(self, histogram: H) -> Result<Summary, Error>
    where
        H: TryInto<HistogramProto, Error = E>,
        Error: From<E>,
    {
        let Self { tag } = self;

        let summary = Summary {
            value: vec![Value {
                node_name: "".into(),
                tag: tag.to_string(),
                metadata: None,
                value: Some(ValueEnum::Histo(histogram.try_into()?)),
            }],
        };
        Ok(summary)
    }

    /// Build a tensor summary.
    pub fn build_tensor<S, E>(self, tensor: S) -> Result<Summary, Error>
    where
        S: TryInto<TensorProto, Error = E>,
        Error: From<E>,
    {
        let Self { tag } = self;

        let summary = Summary {
            value: vec![Value {
                node_name: "".into(),
                tag: tag.to_string(),
                metadata: None,
                value: Some(ValueEnum::Tensor(tensor.try_into()?)),
            }],
        };
        Ok(summary)
    }

    /// Build an image summary.
    pub fn build_image<M, E>(self, image: M) -> Result<Summary, Error>
    where
        M: TryInto<Image, Error = E>,
        Error: From<E>,
    {
        let Self { tag } = self;

        let summary = Summary {
            value: vec![Value {
                node_name: "".into(),
                tag: tag.to_string(),
                metadata: None,
                value: Some(ValueEnum::Image(image.try_into()?)),
            }],
        };
        Ok(summary)
    }

    /// Build a summary with multiple images.
    pub fn build_image_list<V, E>(self, images: V) -> Result<Summary, Error>
    where
        V: TryInfoImageList<Error = E>,
        Error: From<E>,
    {
        let Self { tag } = self;

        let image_protos = images.try_into_image_list()?;

        let values = match image_protos.len() {
            1 => {
                let image_proto = image_protos.into_iter().next().unwrap();
                let values = vec![Value {
                    node_name: "".into(),
                    tag: format!("{}/image", tag.to_string()),
                    metadata: None,
                    value: Some(ValueEnum::Image(image_proto)),
                }];
                values
            }
            _ => {
                let values = image_protos
                    .into_iter()
                    .enumerate()
                    .map(|(index, image_proto)| Value {
                        node_name: "".into(),
                        tag: format!("{}/image/{}", tag.to_string(), index),
                        metadata: None,
                        value: Some(ValueEnum::Image(image_proto)),
                    })
                    .collect::<Vec<_>>();
                values
            }
        };

        let summary = Summary { value: values };
        Ok(summary)
    }

    /// Build an audio summary.
    pub fn build_audio<A, E>(self, audio: A) -> Result<Summary, Error>
    where
        A: TryInto<Audio, Error = E>,
        Error: From<E>,
    {
        let Self { tag } = self;

        let summary = Summary {
            value: vec![Value {
                node_name: "".into(),
                tag: tag.to_string(),
                metadata: None,
                value: Some(ValueEnum::Audio(audio.try_into()?)),
            }],
        };
        Ok(summary)
    }
}