data_manager.py 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
import collections
C
chenjian 已提交
16 17
import random
import threading
18 19

DEFAULT_PLUGIN_MAXSIZE = {
走神的阿圆's avatar
走神的阿圆 已提交
20
    "scalar": 1000,
21
    "image": 10,
22
    "histogram": 100,
走神的阿圆's avatar
走神的阿圆 已提交
23
    "embeddings": 50000000,
走神的阿圆's avatar
走神的阿圆 已提交
24
    "audio": 10,
走神的阿圆's avatar
走神的阿圆 已提交
25
    "pr_curve": 300,
P
Peter Pan 已提交
26
    "roc_curve": 300,
走神的阿圆's avatar
走神的阿圆 已提交
27
    "meta_data": 100,
走神的阿圆's avatar
走神的阿圆 已提交
28 29
    "text": 10,
    "hyper_parameters": 10000
30 31 32
}


C
chenjian 已提交
33 34 35 36
def add_sub_tag(tag, sub_tag):
    return tag.replace('%', '_') + '_' + sub_tag


37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class Reservoir(object):
    """A map-to-arrays dict, with deterministic Reservoir Sampling.

    Store each reservoir bucket by key, and each bucket is a list sampling
    with reservoir algorithm.
    """

    def __init__(self, max_size, seed=0):
        """Creates a new reservoir.

        Args:
            max_size: The number of values to keep in the reservoir for each tag,
                if max_size is zero, all values will be kept in bucket.
            seed: The seed to initialize a random.Random().
            num_item_index: The index of data to add.

        Raises:
          ValueError: If max_size is not a nonnegative integer.
        """
        if max_size < 0 or max_size != round(max_size):
            raise ValueError("Max_size must be nonnegative integer.")
        self._max_size = max_size
C
chenjian 已提交
59 60
        self._buckets = collections.defaultdict(lambda: _ReservoirBucket(
            max_size=self._max_size, random_instance=random.Random(seed)))
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        self._mutex = threading.Lock()

    @property
    def keys(self):
        """Return all keys in self._buckets.

        Returns:
            All keys in reservoir buckets.
        :return:
        """
        with self._mutex:
            return list(self._buckets.keys())

    def _exist_in_keys(self, key):
        """Determine if key exists.

        Args:
            key: Key to determine if exists.

        Returns:
            True if key exists in buckets.keys, otherwise False.
        """
        return True if key in self._buckets.keys() else False

85 86
    def exist_in_keys(self, run, tag):
        """Determine if run_tag exists.
87 88 89 90

        For usage habits of VisualDL, actually call self._exist_in_keys()

        Args:
91
            run: Identity of one tablet.
92 93 94
            tag: Identity of one record in tablet.

        Returns:
95
            True if run_tag exists in buckets.keys, otherwise False.
96
        """
97
        key = run + "/" + tag
98 99 100 101 102 103 104 105
        return self._exist_in_keys(key)

    def _get_num_items_index(self, key):
        keys = self.keys
        if key not in keys:
            raise KeyError("Key %s not in buckets.keys()" % key)
        return self._buckets[key].num_items_index

106 107
    def get_num_items_index(self, run, tag):
        key = run + "/" + tag
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        return self._get_num_items_index(key)

    def _get_items(self, key):
        """Get items with tag "key"

        Args:
            key: Key to finding bucket in reservoir buckets.

        Returns:
            One bucket in reservoir buckets by key.
        """
        keys = self.keys
        with self._mutex:
            if key not in keys:
                raise KeyError("Key %s not in buckets.keys()" % key)
            return self._buckets[key].items

125 126
    def get_items(self, run, tag):
        """Get items with tag 'run_tag'
127

128 129 130
        For usage habits of VisualDL, actually call self._get_items()

        Args:
131
            run: Identity of one tablet.
132 133 134
            tag: Identity of one record in tablet.

        Returns:
135
            One bucket in reservoir buckets by run and tag.
136
        """
137
        key = run + "/" + tag
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        return self._get_items(key)

    def _add_item(self, key, item):
        """Add a new item to reservoir buckets with given tag as key.

        If bucket with key has not yet reached full size, each item will be
        added.

        If bucket with key is full, each item will be added with same
        probability.

        Add new item to buckets will always valid because self._buckets is a
        collection.defaultdict.

        Args:
            key: Tag of one bucket to add new item.
            item: New item to add to bucket.
        """
        with self._mutex:
            self._buckets[key].add_item(item)

走神的阿圆's avatar
走神的阿圆 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    def _add_scalar_item(self, key, item):
        """Add a new scalar item to reservoir buckets with given tag as key.

        If bucket with key has not yet reached full size, each item will be
        added.

        If bucket with key is full, each item will be added with same
        probability.

        Add new item to buckets will always valid because self._buckets is a
        collection.defaultdict.

        Args:
            key: Tag of one bucket to add new item.
            item: New item to add to bucket.
        """
        with self._mutex:
            self._buckets[key].add_scalar_item(item)

C
chenjian 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    def _add_scalars_item(self, key, item):
        """Add a new scalar item to reservoir buckets with given tag as key.

        If bucket with key has not yet reached full size, each item will be
        added.

        If bucket with key is full, each item will be added with same
        probability.

        Add new item to buckets will always valid because self._buckets is a
        collection.defaultdict.

        Args:
            key: Tag of one bucket to add new item.
            item: New item to add to bucket.
        """
        with self._mutex:
            self._buckets[key].add_scalars_item(item)

197
    def add_item(self, run, tag, item):
198 199 200 201 202
        """Add a new item to reservoir buckets with given tag as key.

        For usage habits of VisualDL, actually call self._add_items()

        Args:
203
            run: Identity of one tablet.
204 205 206
            tag: Identity of one record in tablet.
            item: New item to add to bucket.
        """
207
        key = run + "/" + tag
208 209
        self._add_item(key, item)

走神的阿圆's avatar
走神的阿圆 已提交
210 211 212 213 214 215 216 217 218 219
    def add_scalar_item(self, run, tag, item):
        """Add a new scalar item to reservoir buckets with given tag as key.

        For usage habits of VisualDL, actually call self._add_items()

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.
            item: New item to add to bucket.
        """
C
chenjian 已提交
220 221 222 223 224 225 226 227
        if item.WhichOneof("one_value") == "value":
            key = run + "/" + tag
            self._add_scalar_item(key, item)
        elif item.WhichOneof("one_value") == "tag_value":
            key = run + "/" + add_sub_tag(tag, item.tag_value.tag) + "/" + tag
            self._add_scalars_item(key, item)
        else:
            raise ValueError("Not scalar type:" + item.WhichOneof("one_value"))
走神的阿圆's avatar
走神的阿圆 已提交
228

229 230 231 232
    def _cut_tail(self, key):
        with self._mutex:
            self._buckets[key].cut_tail()

233
    def cut_tail(self, run, tag):
234
        """Pop the last item in reservoir buckets.
235

236 237 238 239
        Sometimes the tail of the retrieved data is abnormal 0. This
        method is used to handle this problem.

        Args:
240
            run: Identity of one tablet.
241 242
            tag: Identity of one record in tablet.
        """
243
        key = run + "/" + tag
244 245
        self._cut_tail(key)

246 247 248 249

class _ReservoirBucket(object):
    """Data manager for sampling data, use reservoir sampling.
    """
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    def __init__(self, max_size, random_instance=None):
        """Create a _ReservoirBucket instance.

        Args:
            max_size: The maximum size of reservoir bucket. If max_size is
                zero, the bucket has unbounded size.
            random_instance: The random number generator. If not specified,
                default to random.Random(0)
            num_item_index: The index of data to add.

        Raises:
            ValueError: If args max_size is not a nonnegative integer.
        """
        if max_size < 0 or max_size != round(max_size):
            raise ValueError("Max_size must be nonnegative integer.")
        self._max_size = max_size
        self._random = random_instance if random_instance is not None else \
            random.Random(0)
        self._items = []
        self._mutex = threading.Lock()
        self._num_items_index = 0

走神的阿圆's avatar
走神的阿圆 已提交
273 274 275
        self.max_scalar = None
        self.min_scalar = None

C
chenjian 已提交
276 277 278
        # improve performance when data is monotonous
        self._last_special = False

279 280 281 282 283 284 285 286 287 288 289 290 291
    def add_item(self, item):
        """ Add an item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

        Args:
            item: The item to add to reservoir bucket.
        """
        with self._mutex:
            if len(self._items) < self._max_size or self._max_size == 0:
                self._items.append(item)
            else:
走神的阿圆's avatar
走神的阿圆 已提交
292
                r = self._random.randint(1, self._num_items_index)
293 294 295
                if r < self._max_size:
                    self._items.pop(r)
                    self._items.append(item)
296 297
                else:
                    self._items[-1] = item
298 299
            self._num_items_index += 1

走神的阿圆's avatar
走神的阿圆 已提交
300 301 302 303 304 305
    def add_scalar_item(self, item):
        """ Add an scalar item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

C
chenjian 已提交
306 307
        use _last_special mark to improve performance when data is monotonous

走神的阿圆's avatar
走神的阿圆 已提交
308 309 310 311
        Args:
            item: The item to add to reservoir bucket.
        """
        with self._mutex:
C
chenjian 已提交
312
            # save max and min value
走神的阿圆's avatar
走神的阿圆 已提交
313 314 315 316 317 318
            if not self.max_scalar or self.max_scalar.value < item.value:
                self.max_scalar = item
            if not self.min_scalar or self.min_scalar.value > item.value:
                self.min_scalar = item

            if len(self._items) < self._max_size or self._max_size == 0:
C
chenjian 已提交
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
                # capacity is valid, append directly
                self._items.append(item)
            else:
                if self._last_special:
                    if self._items[-1].id == self.min_scalar.id or self._items[
                            -1].id == self.max_scalar.id:
                        # data is not monotonous, set special to False
                        self._last_special = False
                    else:
                        # data is monotonous, drop last item by reservoir algorithm
                        r = self._random.randint(1, self._num_items_index)
                        if r >= self._max_size:
                            self._items.pop(-1)
                            self._items.append(item)
                            self._num_items_index += 1
                            return
                if item.id == self.min_scalar.id or item.id == self.max_scalar.id:
                    # this item is max or min, should be reserved
                    r = self._random.randint(1, self._max_size - 1)
                    self._last_special = True
                else:
                    # drop by reservoir algorithm
                    r = self._random.randint(1, self._num_items_index)
                    self._last_special = False
                if r < self._max_size:
                    if self._items[r].id == self.min_scalar.id or self._items[
                            r].id == self.max_scalar.id:
                        # reserve max and min point
                        if r - 1 > 0:
                            r = r - 1
                        elif r + 1 < self._max_size:
                            r = r + 1
                    self._items.pop(r)
                    self._items.append(item)

            self._num_items_index += 1

    def add_scalars_item(self, item):
        """ Add an scalar item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

        Args:
            item: The item to add to reservoir bucket.
        """
        with self._mutex:
            # save max and min value
            if not self.max_scalar or self.max_scalar.tag_value.value < item.tag_value.value:
                self.max_scalar = item
            if not self.min_scalar or self.min_scalar.tag_value.value > item.tag_value.value:
                self.min_scalar = item

            if len(self._items) < self._max_size or self._max_size == 0:
                # capacity is valid, append directly
走神的阿圆's avatar
走神的阿圆 已提交
374 375
                self._items.append(item)
            else:
C
chenjian 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388
                if self._last_special:
                    if self._items[-1].id == self.min_scalar.id or self._items[
                            -1].id == self.max_scalar.id:
                        # data is not monotic, set special to False
                        self._last_special = False
                    else:
                        # data is monotic, drop last item by reservoir algorithm
                        r = self._random.randint(1, self._num_items_index)
                        if r >= self._max_size:
                            self._items.pop(-1)
                            self._items.append(item)
                            self._num_items_index += 1
                            return
走神的阿圆's avatar
走神的阿圆 已提交
389
                if item.id == self.min_scalar.id or item.id == self.max_scalar.id:
C
chenjian 已提交
390
                    # this item is max or min, should be reserved
走神的阿圆's avatar
走神的阿圆 已提交
391
                    r = self._random.randint(1, self._max_size - 1)
C
chenjian 已提交
392
                    self._last_special = True
走神的阿圆's avatar
走神的阿圆 已提交
393
                else:
C
chenjian 已提交
394
                    # drop by reservoir algorithm
走神的阿圆's avatar
走神的阿圆 已提交
395
                    r = self._random.randint(1, self._num_items_index)
C
chenjian 已提交
396
                    self._last_special = False
走神的阿圆's avatar
走神的阿圆 已提交
397
                if r < self._max_size:
C
chenjian 已提交
398 399 400
                    if self._items[r].id == self.min_scalar.id or self._items[
                            r].id == self.max_scalar.id:
                        # reserve max and min point
走神的阿圆's avatar
走神的阿圆 已提交
401 402 403 404 405 406
                        if r - 1 > 0:
                            r = r - 1
                        elif r + 1 < self._max_size:
                            r = r + 1
                    self._items.pop(r)
                    self._items.append(item)
407 408
                else:
                    self._items[-1] = item
走神的阿圆's avatar
走神的阿圆 已提交
409 410 411

            self._num_items_index += 1

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    @property
    def items(self):
        """Get self._items

        Returns:
            All items.
        """
        with self._mutex:
            return self._items

    @property
    def num_items_index(self):
        with self._mutex:
            return self._num_items_index

427 428 429 430 431 432 433 434 435 436
    def cut_tail(self):
        """Pop the last item in reservoir buckets.

        Sometimes the tail of the retrieved data is abnormal 0. This
        method is used to handle this problem.
        """
        with self._mutex:
            self._items.pop()
            self._num_items_index -= 1

437 438 439 440

class DataManager(object):
    """Data manager for all plugin.
    """
441

442 443 444 445 446 447 448
    def __init__(self):
        """Create a data manager for all plugin.

        All kinds of plugin has own reservoir, stored in a dict with plugin
        name as key.

        """
449 450 451 452 453 454 455 456 457 458
        self._reservoirs = {
            "scalar":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["scalar"]),
            "histogram":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["histogram"]),
            "image":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["image"]),
            "embeddings":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["embeddings"]),
            "audio":
走神的阿圆's avatar
走神的阿圆 已提交
459 460
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["audio"]),
            "pr_curve":
走神的阿圆's avatar
走神的阿圆 已提交
461
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["pr_curve"]),
P
Peter Pan 已提交
462 463
            "roc_curve":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["roc_curve"]),
走神的阿圆's avatar
走神的阿圆 已提交
464
            "meta_data":
走神的阿圆's avatar
走神的阿圆 已提交
465 466
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["meta_data"]),
            "text":
走神的阿圆's avatar
走神的阿圆 已提交
467 468 469
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["text"]),
            "hyper_parameters":
            Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE["hyper_parameters"])
470
        }
471 472
        self._mutex = threading.Lock()

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    def add_reservoir(self, plugin):
        """Add reservoir to reservoirs.

        Every reservoir is attached to one plugin.

        Args:
            plugin: Key to get one reservoir bucket for one specified plugin.
        """
        with self._mutex:
            if plugin not in self._reservoirs.keys():
                self._reservoirs.update({
                    plugin:
                    Reservoir(max_size=DEFAULT_PLUGIN_MAXSIZE[plugin])
                })

488 489 490 491 492 493 494 495 496 497 498 499 500 501
    def get_reservoir(self, plugin):
        """Get reservoir by plugin as key.

        Args:
            plugin: Key to get one reservoir bucket for one specified plugin.

        Returns:
            Reservoir bucket for plugin.
        """
        with self._mutex:
            if plugin not in self._reservoirs.keys():
                raise KeyError("Key %s not in reservoirs." % plugin)
            return self._reservoirs[plugin]

502
    def add_item(self, plugin, run, tag, item):
503 504
        """Add item to one plugin reservoir bucket.

505
        Use 'run', 'tag' for usage habits of VisualDL.
506 507 508

        Args:
            plugin: Key to get one reservoir bucket.
509
            run: Each tablet has different 'run'.
510 511 512 513
            tag: Tag will be used to generate paths of tablets.
            item: The item to add to reservoir bucket.
        """
        with self._mutex:
C
chenjian 已提交
514 515
            if 'scalar' == plugin or 'scalars' == plugin:  # We adapt scalars data to be saved in scalar reservoir.
                self._reservoirs['scalar'].add_scalar_item(run, tag, item)
走神的阿圆's avatar
走神的阿圆 已提交
516 517
            else:
                self._reservoirs[plugin].add_item(run, tag, item)
518 519 520 521 522 523 524 525 526 527 528 529

    def get_keys(self):
        """Get all plugin buckets name.

        Returns:
            All plugin keys.
        """
        with self._mutex:
            return self._reservoirs.keys()


default_data_manager = DataManager()