chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from polygraphy.exception import *
|
||||
from polygraphy.common.struct import *
|
||||
@@ -0,0 +1,192 @@
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# 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 copy
|
||||
from collections import OrderedDict
|
||||
|
||||
from polygraphy import util
|
||||
from polygraphy.logger import G_LOGGER
|
||||
|
||||
#
|
||||
# NOTE: These classes intentionally don't inherit from the built-in collections (dict, list, etc.)
|
||||
# because doing so prevents us from providing custom JSON serialization methods, since the default
|
||||
# encoder implementation can handle most of the built-in collections and therefore doesn't dispatch
|
||||
# to custom implementations.
|
||||
#
|
||||
|
||||
|
||||
def TypedDict(key_type_func, value_type_func):
|
||||
"""
|
||||
Returns a class (not an instance) that will provide a dictionary-like
|
||||
interface with runtime type checks.
|
||||
|
||||
Note: The types are provided lazily via a callable to avoid unnecessary dependencies
|
||||
on types from external packages at import-time.
|
||||
|
||||
Args:
|
||||
key_type_func (Callable() -> type):
|
||||
A callable that returns the expected key type.
|
||||
value_type_func (Callable() -> type):
|
||||
A callable that returns the expected value type.
|
||||
"""
|
||||
|
||||
class Interface:
|
||||
def __init__(self, dct=None):
|
||||
self.dct = OrderedDict(util.default(dct, {}))
|
||||
self.key_type = key_type_func()
|
||||
self.value_type = value_type_func()
|
||||
|
||||
def _check_types(self, key, val):
|
||||
if not isinstance(key, self.key_type):
|
||||
G_LOGGER.critical(
|
||||
f"Unsupported key type in {self}. Key: {repr(key)} is type `{type(key).__name__}` but {type(self).__name__} expects type `{self.key_type.__name__}`"
|
||||
)
|
||||
if not isinstance(val, self.value_type):
|
||||
G_LOGGER.critical(
|
||||
f"Unsupported value type in {self}. Value: {repr(val)} for key: {repr(key)} is type `{type(val).__name__}` but {type(self).__name__} expects type `{self.value_type.__name__}`"
|
||||
)
|
||||
|
||||
def keys(self):
|
||||
return self.dct.keys()
|
||||
|
||||
def values(self):
|
||||
return self.dct.values()
|
||||
|
||||
def items(self):
|
||||
return self.dct.items()
|
||||
|
||||
def update(self, other):
|
||||
for key, val in other.items():
|
||||
self._check_types(key, val)
|
||||
return self.dct.update(other)
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self.dct
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.dct[key]
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
self._check_types(key, val)
|
||||
self.dct[key] = val
|
||||
|
||||
def __str__(self):
|
||||
return str(self.dct)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.dct)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.dct)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.dct == other.dct
|
||||
|
||||
def __iter__(self):
|
||||
return self.dct.__iter__()
|
||||
|
||||
def __copy__(self):
|
||||
new_dict = type(self)()
|
||||
new_dict.__dict__.update(self.__dict__)
|
||||
new_dict.dct = copy.copy(self.dct)
|
||||
return new_dict
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
new_dict = type(self)()
|
||||
new_dict.__dict__.update(self.__dict__)
|
||||
new_dict.dct = copy.deepcopy(self.dct)
|
||||
return new_dict
|
||||
|
||||
return Interface
|
||||
|
||||
|
||||
def TypedList(elem_type_func):
|
||||
"""
|
||||
Returns a class (not an instance) that will provide a list-like
|
||||
interface with runtime type checks.
|
||||
|
||||
Note: The types are provided lazily via a callable to avoid unnecessary dependencies
|
||||
on types from external packages at import-time.
|
||||
|
||||
Args:
|
||||
elem_type_func (Callable() -> type):
|
||||
A callable that returns the expected list-element type.
|
||||
"""
|
||||
|
||||
class Interface:
|
||||
def __init__(self, lst=None):
|
||||
self.lst = util.default(lst, [])
|
||||
self.elem_type = elem_type_func()
|
||||
|
||||
def _check_type(self, elem):
|
||||
if not isinstance(elem, self.elem_type):
|
||||
G_LOGGER.critical(
|
||||
f"Unsupported element type type in {type(self).__name__}. Element: {repr(elem)} is type: {type(elem).__name__} but type: {self.elem_type.__name__} was expected"
|
||||
)
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self.lst
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.lst[index]
|
||||
|
||||
def __setitem__(self, index, elem):
|
||||
self._check_type(elem)
|
||||
self.lst[index] = elem
|
||||
|
||||
def __str__(self):
|
||||
return str(self.lst)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.lst)
|
||||
|
||||
def append(self, elem):
|
||||
self._check_type(elem)
|
||||
return self.lst.append(elem)
|
||||
|
||||
def extend(self, elems):
|
||||
for elem in elems:
|
||||
self._check_type(elem)
|
||||
return self.lst.extend(elems)
|
||||
|
||||
def __iadd__(self, elems):
|
||||
for elem in elems:
|
||||
self._check_type(elem)
|
||||
self.lst += elems
|
||||
|
||||
def __len__(self):
|
||||
return len(self.lst)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.lst == other.lst
|
||||
|
||||
def __iter__(self):
|
||||
return self.lst.__iter__()
|
||||
|
||||
def __copy__(self):
|
||||
new_list = type(self)()
|
||||
new_list.__dict__.update(self.__dict__)
|
||||
new_list.lst = copy.copy(self.lst)
|
||||
return new_list
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
new_list = type(self)()
|
||||
new_list.__dict__.update(self.__dict__)
|
||||
new_list.lst = copy.deepcopy(self.lst)
|
||||
return new_list
|
||||
|
||||
return Interface
|
||||
@@ -0,0 +1,203 @@
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
from polygraphy import mod, util
|
||||
from polygraphy.common.interface import TypedDict
|
||||
from polygraphy.datatype import DataType
|
||||
from polygraphy.json import Decoder, Encoder, add_json_methods
|
||||
|
||||
|
||||
class BoundedShape(list):
|
||||
"""
|
||||
Represents a shape with min/max bounds.
|
||||
"""
|
||||
|
||||
def __init__(self, shape, min=None, max=None):
|
||||
super().__init__(shape)
|
||||
self.min = min
|
||||
self.max = max
|
||||
|
||||
def __repr__(self):
|
||||
return f"BoundedShape({list(self)}, min={self.min}, max={self.max})"
|
||||
|
||||
|
||||
class MetadataTuple:
|
||||
def __init__(self, dtype, shape, docstring):
|
||||
self.dtype = dtype
|
||||
self.shape = shape
|
||||
self.docstring = docstring
|
||||
|
||||
@property
|
||||
def dtype(self):
|
||||
return self._dtype
|
||||
|
||||
@dtype.setter
|
||||
def dtype(self, new):
|
||||
self._dtype = DataType.from_dtype(new) if new is not None else None
|
||||
|
||||
def __iter__(self):
|
||||
yield from [self.dtype, self.shape]
|
||||
|
||||
def __repr__(self):
|
||||
return f"MetadataTuple({self.dtype}, {self.shape}, {self.docstring})"
|
||||
|
||||
def __str__(self):
|
||||
ret = ""
|
||||
meta_items = []
|
||||
if self.dtype is not None:
|
||||
meta_items.append(f"dtype={self.dtype}")
|
||||
if self.shape is not None:
|
||||
meta_items.append(f"shape={tuple(self.shape)}")
|
||||
if self.docstring:
|
||||
meta_items.append(self.docstring)
|
||||
if meta_items:
|
||||
ret += "[" + ", ".join(meta_items) + "]"
|
||||
return ret
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.shape == other.shape and self.dtype == other.dtype
|
||||
|
||||
|
||||
@mod.export()
|
||||
class TensorMetadata(TypedDict(lambda: str, lambda: MetadataTuple)):
|
||||
"""
|
||||
An OrderedDict[str, MetadataTuple] that maps input names to their data types and shapes.
|
||||
|
||||
Shapes may include negative values, ``None``, or strings to indicate dynamic dimensions.
|
||||
|
||||
Example:
|
||||
::
|
||||
|
||||
shape = tensor_meta["input0"].shape
|
||||
dtype = tensor_meta["input0"].dtype
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def from_feed_dict(feed_dict):
|
||||
"""
|
||||
Constructs a new TensorMetadata using information from the provided feed_dict.
|
||||
|
||||
Args:
|
||||
feed_dict (OrderedDict[str, Union[numpy.ndarray, torch.tensor]]):
|
||||
A mapping of input tensor names to corresponding input arrays.
|
||||
|
||||
Returns:
|
||||
TensorMetadata
|
||||
"""
|
||||
meta = TensorMetadata()
|
||||
for name, arr in feed_dict.items():
|
||||
meta.add(name, util.array.dtype(arr), util.array.shape(arr))
|
||||
return meta
|
||||
|
||||
def add(self, name, dtype, shape, min_shape=None, max_shape=None, docstring=None):
|
||||
"""
|
||||
Convenience function for adding entries.
|
||||
|
||||
Args:
|
||||
name (str): The name of the input.
|
||||
dtype (Any):
|
||||
The data type of the input.
|
||||
This can be any type that can be converted to a Polygraphy DataType.
|
||||
shape (Sequence[Union[int, str]]]):
|
||||
The shape of the input. Dynamic dimensions may
|
||||
be indicated by negative values, ``None``, or a string.
|
||||
|
||||
min_shape (Sequence[int]):
|
||||
The minimum valid shape for the input.
|
||||
If provided, this shape should not include any dynamic dimensions.
|
||||
max_shape (Sequence[int]):
|
||||
The maximum valid shape for the input.
|
||||
If provided, this shape should not include any dynamic dimensions.
|
||||
docstring (str):
|
||||
Any additional information associated with a tensor.
|
||||
|
||||
Returns:
|
||||
The newly added entry.
|
||||
"""
|
||||
self[name] = MetadataTuple(
|
||||
dtype,
|
||||
(
|
||||
BoundedShape(shape, min=min_shape, max=max_shape)
|
||||
if shape is not None
|
||||
else None
|
||||
),
|
||||
docstring,
|
||||
)
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
ret = "TensorMetadata()"
|
||||
for name, metadata_tuple in self.items():
|
||||
(dtype, shape) = metadata_tuple
|
||||
ret += util.make_repr(
|
||||
".add",
|
||||
name,
|
||||
dtype,
|
||||
list(shape),
|
||||
min_shape=shape.min,
|
||||
max_shape=shape.max,
|
||||
docstring=metadata_tuple.docstring,
|
||||
)[0]
|
||||
return ret
|
||||
|
||||
def __str__(self):
|
||||
sep = ",\n "
|
||||
elems = [f"{name} {meta_tuple}".strip() for name, meta_tuple in self.items()]
|
||||
return "{" + sep.join(elems) + "}"
|
||||
|
||||
|
||||
@mod.export()
|
||||
@add_json_methods("formatted array")
|
||||
class FormattedArray:
|
||||
"""
|
||||
[EXPERIMENTAL, UNTESTED] This API is experimental and untested and may be significantly
|
||||
modified in future releases. Use with caution!
|
||||
|
||||
Representes an array whose semantic shape differs from its physical size in memory.
|
||||
|
||||
For example, consider an ``NCHW`` tensor of shape ``(1, 3, 28, 28)``. If we use a vectorized format
|
||||
like ``N(C/4)HW4``, then the physical size of the array would be ``(1, 1, 28, 28 * 4)`` since
|
||||
the channel dimension would be padded to a multiple of 4. However, we still need a way to keep
|
||||
track of the semantic shape for things like shape inference.
|
||||
|
||||
This class provides a mechanism to specify the shape of an array independently of
|
||||
the underlying array.
|
||||
"""
|
||||
|
||||
def __init__(self, array, shape):
|
||||
"""
|
||||
Args:
|
||||
array (Union[np.ndarray, polygraphy.cuda.DeviceView]):
|
||||
The array. In most cases, this will be a raw byte-array.
|
||||
shape (Sequence[int]):
|
||||
The semantic shape of the data.
|
||||
"""
|
||||
self.array = array
|
||||
self.shape = shape
|
||||
|
||||
|
||||
@Encoder.register(FormattedArray)
|
||||
def encode(farray):
|
||||
return {
|
||||
"array": farray.array,
|
||||
"shape": farray.shape,
|
||||
}
|
||||
|
||||
|
||||
@Decoder.register(FormattedArray)
|
||||
def decode(dct):
|
||||
return FormattedArray(dct["array"], dct["shape"])
|
||||
Reference in New Issue
Block a user