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
| 64 | |
| 65 | @mod.export() |
| 66 | class TensorMetadata(TypedDict(lambda: str, lambda: MetadataTuple)): |
| 67 | """ |
| 68 | An OrderedDict[str, MetadataTuple] that maps input names to their data types and shapes. |
| 69 | |
| 70 | Shapes may include negative values, ``None``, or strings to indicate dynamic dimensions. |
| 71 | |
| 72 | Example: |
| 73 | :: |
| 74 | |
| 75 | shape = tensor_meta["input0"].shape |
| 76 | dtype = tensor_meta["input0"].dtype |
| 77 | """ |
| 78 | |
| 79 | @staticmethod |
| 80 | def from_feed_dict(feed_dict): |
| 81 | """ |
| 82 | Constructs a new TensorMetadata using information from the provided feed_dict. |
| 83 | |
| 84 | Args: |
| 85 | feed_dict (OrderedDict[str, numpy.ndarray]): |
| 86 | A mapping of input tensor names to corresponding input NumPy arrays. |
| 87 | |
| 88 | Returns: |
| 89 | TensorMetadata |
| 90 | """ |
| 91 | meta = TensorMetadata() |
| 92 | for name, arr in feed_dict.items(): |
| 93 | meta.add(name, arr.dtype, arr.shape) |
| 94 | return meta |
| 95 | |
| 96 | def add(self, name, dtype, shape, min_shape=None, max_shape=None): |
| 97 | """ |
| 98 | Convenience function for adding entries. |
| 99 | |
| 100 | Args: |
| 101 | name (str): The name of the input. |
| 102 | dtype (numpy.dtype): The data type of the input. |
| 103 | shape (Sequence[Union[int, str]]]): |
| 104 | The shape of the input. Dynamic dimensions may |
| 105 | be indicated by negative values, ``None``, or a string. |
| 106 | |
| 107 | min_shape (Sequence[int]): |
| 108 | The minimum valid shape for the input. |
| 109 | If provided, this shape should not include any dynamic dimensions. |
| 110 | max_shape (Sequence[int]): |
| 111 | The maximum valid shape for the input. |
| 112 | If provided, this shape should not include any dynamic dimensions. |
| 113 | |
| 114 | Returns: |
| 115 | The newly added entry. |
| 116 | """ |
| 117 | self[name] = MetadataTuple( |
| 118 | dtype, BoundedShape(shape, min=min_shape, max=max_shape) if shape is not None else None |
| 119 | ) |
| 120 | return self |
| 121 | |
| 122 | def __repr__(self): |
| 123 | ret = "TensorMetadata()" |