Extract scalar value from a lifted tensor constant node. Lifted constants are created during torch.export and contain small constant tensors (like epsilon values). The actual value is stored in node.meta["val"]. Args: node: A node that may be a lifted tensor constant
(node: Node)
| 73 | |
| 74 | |
| 75 | def extract_lifted_tensor_constant(node: Node) -> Optional[float]: |
| 76 | """ |
| 77 | Extract scalar value from a lifted tensor constant node. |
| 78 | |
| 79 | Lifted constants are created during torch.export and contain small |
| 80 | constant tensors (like epsilon values). The actual value is stored |
| 81 | in node.meta["val"]. |
| 82 | |
| 83 | Args: |
| 84 | node: A node that may be a lifted tensor constant |
| 85 | |
| 86 | Returns: |
| 87 | The scalar float value, or None if not a lifted constant or not scalar |
| 88 | """ |
| 89 | if not isinstance(node, Node): |
| 90 | return None |
| 91 | if "lifted_tensor_constant" not in node.name: |
| 92 | return None |
| 93 | val = node.meta.get("val") |
| 94 | if val is None: |
| 95 | return None |
| 96 | if not hasattr(val, "item"): |
| 97 | return None |
| 98 | try: |
| 99 | return float(val.item()) |
| 100 | except (RuntimeError, ValueError): |
| 101 | return None |
| 102 | |
| 103 | |
| 104 | @dataclass |