-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path_metadata.py
More file actions
45 lines (32 loc) · 1.51 KB
/
_metadata.py
File metadata and controls
45 lines (32 loc) · 1.51 KB
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
# Copyright (c) ONNX Project Contributors
# SPDX-License-Identifier: Apache-2.0
"""Class for storing metadata about the IR objects."""
from __future__ import annotations
import collections
from collections.abc import Mapping
from typing import Any
class MetadataStore(collections.UserDict):
"""Class for storing metadata about the IR objects.
Metadata is stored as key-value pairs. The keys are strings and the values
can be any Python object.
The metadata store also supports marking keys as invalid. This is useful
when a pass wants to mark a key that needs to be recomputed.
"""
def __init__(self, data: Mapping[str, Any] | None = None, /) -> None:
super().__init__(data)
self._invalid_keys: set[str] = set()
def __setitem__(self, key: str, item: Any) -> None:
self.data[key] = item
self._invalid_keys.discard(key)
def invalidate(self, key: str) -> None:
self._invalid_keys.add(key)
def is_valid(self, key: str) -> bool:
"""Returns whether the value is valid.
Note that default values (None) are not necessarily invalid. For example,
a shape that is unknown (None) may be still valid if shape inference has
determined that the shape is unknown.
Whether a value is valid is solely determined by the user that sets the value.
"""
return key not in self._invalid_keys
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.data!r}, invalid_keys={self._invalid_keys!r})"