| 117 | |
| 118 | |
| 119 | class ProxyValue: |
| 120 | # pyre-ignore |
| 121 | def __init__(self, data, proxy: Union[torch.fx.Proxy, torch.fx.Node]): |
| 122 | # pyre-ignore |
| 123 | self.data = data |
| 124 | self.proxy_or_node = proxy |
| 125 | |
| 126 | @property |
| 127 | def node(self) -> torch.fx.Node: |
| 128 | if isinstance(self.proxy_or_node, torch.fx.Node): |
| 129 | return self.proxy_or_node |
| 130 | assert isinstance(self.proxy_or_node, torch.fx.Proxy) |
| 131 | return self.proxy_or_node.node |
| 132 | |
| 133 | @property |
| 134 | def proxy(self) -> torch.fx.Proxy: |
| 135 | if not isinstance(self.proxy_or_node, torch.fx.Proxy): |
| 136 | raise RuntimeError( |
| 137 | f"ProxyValue doesn't have attached Proxy object. Node: {self.proxy_or_node.format_node()}" |
| 138 | ) |
| 139 | return self.proxy_or_node |
| 140 | |
| 141 | def to_tensor(self) -> torch.Tensor: |
| 142 | assert isinstance(self.data, torch.Tensor) |
| 143 | return self.data |
| 144 | |
| 145 | def is_tensor(self) -> bool: |
| 146 | return isinstance(self.data, torch.Tensor) |
| 147 | |
| 148 | # pyre-ignore |
| 149 | def __iter__(self): |
| 150 | yield from self.data |
| 151 | |
| 152 | def __bool__(self) -> bool: |
| 153 | if isinstance(self.data, (torch.SymInt, torch.SymFloat, torch.SymBool)): |
| 154 | raise ExportPassBaseError( |
| 155 | "ProxyValue with symbolic data cannot be used in boolean context." |
| 156 | ) |
| 157 | return bool(self.data) |
| 158 | |
| 159 | def __int__(self): |
| 160 | if isinstance(self.data, torch.SymInt): |
| 161 | raise ExportPassBaseError( |
| 162 | "ProxyValue with SymInt data cannot be converted to int." |
| 163 | ) |
| 164 | return int(self.data) |
| 165 | |
| 166 | def __float__(self): |
| 167 | if isinstance(self.data, torch.SymFloat): |
| 168 | raise ExportPassBaseError( |
| 169 | "ProxyValue with SymFloat data cannot be converted to float." |
| 170 | ) |
| 171 | return float(self.data) |
| 172 | |
| 173 | def __index__(self): |
| 174 | if isinstance(self.data, torch.SymInt): |
| 175 | raise ExportPassBaseError( |
| 176 | "ProxyValue with SymInt data cannot be used in index context." |
no outgoing calls