Download bbflow_base/scripts/check_directional_condition_equivariance.py from hanghang214/data_transfer: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/scripts/check_directional_condition_equivariance.py
- Command line
-
hf download hf://datasets/hanghang214/data_transfer/bbflow_base/scripts/check_directional_condition_equivariance.py
-
curl -L -o check_directional_condition_equivariance.py https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/scripts/check_directional_condition_equivariance.py
2.49 kB
| #!/usr/bin/env python | |
| """Known-answer checks for directional node/edge conditions.""" | |
| from pathlib import Path | |
| import sys | |
| import torch | |
| REPO_ROOT = Path(__file__).resolve().parents[1] | |
| if str(REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(REPO_ROOT)) | |
| from bbflow.models.latent import LatentEncoder | |
| class _Cfg: | |
| dim = 16 | |
| num_modes = 2 | |
| motion_hidden_dim = 16 | |
| motion_dist_bins = 4 | |
| motion_dist_width = 4.0 | |
| motion_message_passing_layers = 1 | |
| template_residual_scale = 0.1 | |
| template_remove_rotation = True | |
| def main(): | |
| torch.manual_seed(7) | |
| encoder = LatentEncoder(_Cfg()).eval() | |
| trans = torch.tensor([[[0.0, 0.0, 0.0], [1.2, 0.1, 0.0], [0.3, 1.1, 0.2], [1.0, 1.2, 0.5], [0.2, 0.4, 1.3]]]) | |
| rotmats = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, trans.shape[1], 1, 1) | |
| seq = torch.zeros(1, trans.shape[1], 21) | |
| seq[0, torch.arange(trans.shape[1]), torch.arange(trans.shape[1]) % 21] = 1.0 | |
| mask = torch.ones(1, trans.shape[1]) | |
| rotation = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) | |
| with torch.no_grad(): | |
| condition, _ = encoder.forward_prior(trans, rotmats, seq, mask) | |
| trans_rot = torch.einsum("ij,bnj->bni", rotation, trans) | |
| rotmats_rot = torch.einsum("ij,bnjk->bnik", rotation, rotmats) | |
| condition_rot, _ = encoder.forward_prior(trans_rot, rotmats_rot, seq, mask) | |
| disp_local_diff = (condition_rot["disp_node_cond"].float() - condition["disp_node_cond"].float()).abs().max() | |
| disp_edge_diff = (condition_rot["disp_edge_cond"].float() - condition["disp_edge_cond"].float()).abs().max() | |
| expected_u = torch.einsum("ij,bnkj->bnki", rotation, condition["u_global"].float()) | |
| u_global_diff = (condition_rot["u_global"].float() - expected_u).abs().max() | |
| print("disp_node_cond_shape =", tuple(condition["disp_node_cond"].shape)) | |
| print("disp_edge_cond_shape =", tuple(condition["disp_edge_cond"].shape)) | |
| print("disp_local_equivariance_max_abs_diff =", float(disp_local_diff.item())) | |
| print("disp_edge_invariance_max_abs_diff =", float(disp_edge_diff.item())) | |
| print("u_global_equivariance_max_abs_diff =", float(u_global_diff.item())) | |
| assert tuple(condition["disp_node_cond"].shape) == (1, trans.shape[1], 3) | |
| assert tuple(condition["disp_edge_cond"].shape) == (1, trans.shape[1], trans.shape[1], 1) | |
| assert disp_local_diff < 1e-4 | |
| assert disp_edge_diff < 1e-4 | |
| assert u_global_diff < 1e-4 | |
| if __name__ == "__main__": | |
| main() |