sdk.h 6.6 KB
Newer Older
S
superjom 已提交
1 2
#ifndef VISUALDL_LOGIC_SDK_H
#define VISUALDL_LOGIC_SDK_H
S
superjom 已提交
3

4
#include "visualdl/logic/histogram.h"
S
superjom 已提交
5 6
#include "visualdl/storage/storage.h"
#include "visualdl/storage/tablet.h"
S
superjom 已提交
7
#include "visualdl/utils/string.h"
8

S
superjom 已提交
9
namespace visualdl {
S
superjom 已提交
10

S
superjom 已提交
11 12
const static std::string kDefaultMode{"default"};

13
class LogWriter {
S
superjom 已提交
14
public:
S
superjom 已提交
15
  LogWriter(const std::string& dir, int sync_cycle) {
S
superjom 已提交
16
    storage_.SetDir(dir);
S
superjom 已提交
17 18
    storage_.meta.cycle = sync_cycle;
  }
S
superjom 已提交
19

20
  LogWriter(const LogWriter& other) {
S
superjom 已提交
21
    mode_ = other.mode_;
S
superjom 已提交
22
    storage_ = other.storage_;
S
superjom 已提交
23 24
  }

S
superjom 已提交
25 26 27 28
  void SetMode(const std::string& mode) {
    mode_ = mode;
    storage_.AddMode(mode);
  }
S
superjom 已提交
29

30 31
  LogWriter AsMode(const std::string& mode) {
    LogWriter writer = *this;
S
superjom 已提交
32
    storage_.AddMode(mode);
S
superjom 已提交
33 34
    writer.mode_ = mode;
    return writer;
S
superjom 已提交
35 36
  }

S
superjom 已提交
37 38 39 40
  Tablet AddTablet(const std::string& tag) {
    // TODO(ChunweiYan) add string check here.
    auto tmp = mode_ + "/" + tag;
    string::TagEncode(tmp);
S
superjom 已提交
41 42
    auto res = storage_.AddTablet(tmp);
    res.SetCaptions(std::vector<std::string>({mode_}));
S
superjom 已提交
43
    res.SetTag(mode_, tag);
S
superjom 已提交
44
    return res;
S
superjom 已提交
45 46 47 48 49 50
  }

  Storage& storage() { return storage_; }

private:
  Storage storage_;
S
superjom 已提交
51
  std::string mode_{kDefaultMode};
S
superjom 已提交
52 53
};

54
class LogReader {
S
superjom 已提交
55
public:
56
  LogReader(const std::string& dir) : reader_(dir) {}
S
superjom 已提交
57

S
superjom 已提交
58 59
  void SetMode(const std::string& mode) { mode_ = mode; }

60
  LogReader AsMode(const std::string& mode) {
S
superjom 已提交
61 62 63 64
    auto tmp = *this;
    tmp.mode_ = mode;
    return tmp;
  }
S
superjom 已提交
65

S
superjom 已提交
66 67
  const std::string& mode() { return mode_; }

S
superjom 已提交
68 69 70 71 72 73
  TabletReader tablet(const std::string& tag) {
    auto tmp = mode_ + "/" + tag;
    string::TagEncode(tmp);
    return reader_.tablet(tmp);
  }

S
superjom 已提交
74 75 76 77
  std::vector<std::string> all_tags() {
    auto tags = reader_.all_tags();
    auto it =
        std::remove_if(tags.begin(), tags.end(), [&](const std::string& tag) {
S
superjom 已提交
78
          return !TagMatchMode(tag, mode_);
S
superjom 已提交
79 80 81 82 83 84 85 86
        });
    tags.erase(it + 1);
    return tags;
  }

  std::vector<std::string> tags(const std::string& component) {
    auto type = Tablet::type(component);
    auto tags = reader_.tags(type);
87 88
    CHECK(!tags.empty()) << "component " << component
                         << " has no taged records";
S
superjom 已提交
89 90
    std::vector<std::string> res;
    for (const auto& tag : tags) {
S
superjom 已提交
91 92
      if (TagMatchMode(tag, mode_)) {
        res.push_back(GenReadableTag(mode_, tag));
S
superjom 已提交
93 94 95 96 97 98 99
      }
    }
    return res;
  }

  StorageReader& storage() { return reader_; }

S
superjom 已提交
100 101
  static std::string GenReadableTag(const std::string& mode,
                                    const std::string& tag) {
S
superjom 已提交
102 103
    auto tmp = tag;
    string::TagDecode(tmp);
S
superjom 已提交
104 105 106 107 108 109
    return tmp.substr(mode.size() + 1);  // including `/`
  }

  static bool TagMatchMode(const std::string& tag, const std::string& mode) {
    if (tag.size() <= mode.size()) return false;
    return tag.substr(0, mode.size()) == mode;
S
superjom 已提交
110 111
  }

S
superjom 已提交
112
protected:
S
superjom 已提交
113 114
private:
  StorageReader reader_;
S
superjom 已提交
115
  std::string mode_{kDefaultMode};
S
superjom 已提交
116 117
};

S
superjom 已提交
118 119 120 121 122 123
namespace components {

/*
 * Read and write support for Scalar component.
 */
template <typename T>
S
superjom 已提交
124
struct Scalar {
S
superjom 已提交
125 126 127 128
  Scalar(Tablet tablet) : tablet_(tablet) {
    tablet_.SetType(Tablet::Type::kScalar);
  }

S
superjom 已提交
129
  void SetCaption(const std::string cap) {
S
superjom 已提交
130
    tablet_.SetCaptions(std::vector<std::string>({cap}));
S
superjom 已提交
131
  }
S
superjom 已提交
132

S
superjom 已提交
133 134 135
  void AddRecord(int id, T value) {
    auto record = tablet_.AddRecord();
    record.SetId(id);
136
    auto entry = record.AddData();
137

S
superjom 已提交
138 139
    time_t time = std::time(nullptr);
    record.SetTimeStamp(time);
S
superjom 已提交
140 141
    entry.Set(value);
  }
S
superjom 已提交
142

S
superjom 已提交
143
private:
S
superjom 已提交
144
  Tablet tablet_;
S
superjom 已提交
145 146
};

S
superjom 已提交
147 148 149 150 151
template <typename T>
struct ScalarReader {
  ScalarReader(TabletReader&& reader) : reader_(reader) {}

  std::vector<T> records() const;
S
superjom 已提交
152 153
  std::vector<T> ids() const;
  std::vector<T> timestamps() const;
S
superjom 已提交
154
  std::string caption() const;
S
superjom 已提交
155
  size_t total_records() { return reader_.total_records(); }
S
superjom 已提交
156 157
  size_t size() const;

S
superjom 已提交
158
private:
S
superjom 已提交
159 160 161
  TabletReader reader_;
};

S
superjom 已提交
162 163 164 165 166
/*
 * Image component writer.
 */
struct Image {
  using value_t = float;
S
superjom 已提交
167
  using shape_t = int64_t;
S
superjom 已提交
168

S
superjom 已提交
169 170 171 172 173 174 175 176 177
  /*
   * step_cycle: store every `step_cycle` as a record.
   * num_samples: how many samples to take in a step.
   */
  Image(Tablet tablet, int num_samples, int step_cycle)
      : writer_(tablet), num_samples_(num_samples), step_cycle_(step_cycle) {
    CHECK_GT(step_cycle, 0);
    CHECK_GT(num_samples, 0);

S
superjom 已提交
178
    writer_.SetType(Tablet::Type::kImage);
S
superjom 已提交
179
    // make image's tag as the default caption.
S
superjom 已提交
180
    writer_.SetNumSamples(num_samples);
S
superjom 已提交
181
    SetCaption(tablet.reader().tag());
S
superjom 已提交
182
  }
S
superjom 已提交
183 184 185
  void SetCaption(const std::string& c) {
    writer_.SetCaptions(std::vector<std::string>({c}));
  }
S
superjom 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198
  /*
   * Start a sample period.
   */
  void StartSampling();
  /*
   * Will this sample will be taken.
   */
  int IsSampleTaken();
  /*
   * End a sample period.
   */
  void FinishSampling();

S
superjom 已提交
199 200 201
  /*
   * Just store a tensor with nothing to do with image format.
   */
S
superjom 已提交
202
  void SetSample(int index,
S
superjom 已提交
203
                 const std::vector<shape_t>& shape,
S
superjom 已提交
204 205
                 const std::vector<value_t>& data);

S
superjom 已提交
206 207 208
protected:
  bool ToSampleThisStep() { return step_id_ % step_cycle_ == 0; }

S
superjom 已提交
209 210 211 212 213
private:
  Tablet writer_;
  Record step_;
  int num_records_{0};
  int num_samples_{0};
S
superjom 已提交
214 215
  int step_id_{0};
  int step_cycle_;
S
superjom 已提交
216 217
};

S
superjom 已提交
218 219 220 221 222 223 224
/*
 * Image reader.
 */
struct ImageReader {
  using value_t = typename Image::value_t;
  using shape_t = typename Image::shape_t;

S
superjom 已提交
225 226
  struct ImageRecord {
    int step_id;
S
superjom 已提交
227
    std::vector<int> data;
S
superjom 已提交
228 229 230
    std::vector<shape_t> shape;
  };

S
superjom 已提交
231 232
  ImageReader(const std::string& mode, TabletReader tablet)
      : reader_(tablet), mode_{mode} {}
S
superjom 已提交
233

S
superjom 已提交
234
  std::string caption();
S
superjom 已提交
235 236 237 238

  // number of steps.
  int num_records() { return reader_.total_records(); }

S
superjom 已提交
239 240 241 242
  int num_samples() { return reader_.num_samples(); }

  int64_t timestamp(int step) { return reader_.record(step).timestamp(); }

S
superjom 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  /*
   * offset: offset of a step.
   * index: index of a sample.
   */
  ImageRecord record(int offset, int index);

  /*
   * offset: offset of a step.
   * index: index of a sample.
   */
  std::vector<value_t> data(int offset, int index);

  /*
   * offset: offset of a step.
   * index: index of a sample.
   */
  std::vector<shape_t> shape(int offset, int index);
S
superjom 已提交
260

S
superjom 已提交
261
  int stepid(int offset, int index);
S
superjom 已提交
262 263 264

private:
  TabletReader reader_;
S
superjom 已提交
265
  std::string mode_;
S
superjom 已提交
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
template <typename T>
struct Histogram {
  Histogram(Tablet tablet, int num_buckets)
      : writer_(tablet), num_buckets_(num_buckets) {
    writer_.SetType(Tablet::Type::kHistogram);
  }

  void AddRecord(int step, const std::vector<T>& data);

private:
  int num_buckets_;
  Tablet writer_;
};

template <typename T>
struct HistogramReader {
  HistogramReader(TabletReader tablet) : reader_(tablet) {}

  size_t num_records() { return reader_.total_records(); }

  HistogramRecord<T> record(int i);

private:
  TabletReader reader_;
};

S
superjom 已提交
294
}  // namespace components
S
superjom 已提交
295
}  // namespace visualdl
S
superjom 已提交
296

S
superjom 已提交
297
#endif