-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathphc_torch_utils.patch
129 lines (129 loc) · 3.79 KB
/
phc_torch_utils.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
31d30
< from isaacgym.torch_utils import *
38c37,102
< def project_to_norm(x, norm=5, z_type = "sphere"):
---
> # The following functions are copied from Isaac Lab to avoid dependency on it.
> # ---- Start of functions from Isaac Lab -----
> @torch.jit.script
> def quat_from_euler_xyz(roll: torch.Tensor, pitch: torch.Tensor, yaw: torch.Tensor) -> torch.Tensor:
> """Convert rotations given as Euler angles in radians to Quaternions.
>
> Note:
> The euler angles are assumed in XYZ convention.
>
> Args:
> roll: Rotation around x-axis (in radians). Shape is (N,).
> pitch: Rotation around y-axis (in radians). Shape is (N,).
> yaw: Rotation around z-axis (in radians). Shape is (N,).
>
> Returns:
> The quaternion in (w, x, y, z). Shape is (N, 4).
> """
> cy = torch.cos(yaw * 0.5)
> sy = torch.sin(yaw * 0.5)
> cr = torch.cos(roll * 0.5)
> sr = torch.sin(roll * 0.5)
> cp = torch.cos(pitch * 0.5)
> sp = torch.sin(pitch * 0.5)
> # compute quaternion
> qw = cy * cr * cp + sy * sr * sp
> qx = cy * sr * cp - sy * cr * sp
> qy = cy * cr * sp + sy * sr * cp
> qz = sy * cr * cp - cy * sr * sp
>
> return torch.stack([qw, qx, qy, qz], dim=-1)
>
>
> @torch.jit.script
> def normalize(x: torch.Tensor, eps: float = 1e-9) -> torch.Tensor:
> """Normalizes a given input tensor to unit length.
>
> Args:
> x: Input tensor of shape (N, dims).
> eps: A small value to avoid division by zero. Defaults to 1e-9.
>
> Returns:
> Normalized tensor of shape (N, dims).
> """
> return x / x.norm(p=2, dim=-1).clamp(min=eps, max=None).unsqueeze(-1)
>
>
> @torch.jit.script
> def quat_from_angle_axis(angle: torch.Tensor, axis: torch.Tensor) -> torch.Tensor:
> """Convert rotations given as angle-axis to quaternions.
>
> Args:
> angle: The angle turned anti-clockwise in radians around the vector's direction. Shape is (N,).
> axis: The axis of rotation. Shape is (N, 3).
>
> Returns:
> The quaternion in (w, x, y, z). Shape is (N, 4).
> """
> theta = (angle / 2).unsqueeze(-1)
> xyz = normalize(axis) * theta.sin()
> w = theta.cos()
> return normalize(torch.cat([w, xyz], dim=-1))
>
> # ---- End of functions from Isaac Lab -----
>
>
> def project_to_norm(x, norm=5, z_type="sphere"):
44a109
>
52,54c117
< c = q_vec * \
< torch.bmm(q_vec.view(shape[0], 1, 3), v.view(
< shape[0], 3, 1)).squeeze(-1) * 2.0
---
> c = q_vec * torch.bmm(q_vec.view(shape[0], 1, 3), v.view(shape[0], 3, 1)).squeeze(-1) * 2.0
56a120,125
>
> @torch.jit.script
> def normalize_angle(x):
> return torch.atan2(torch.sin(x), torch.cos(x))
>
>
62c131
< # ZL: could have issues.
---
> # ZL: could have issues.
144c213,214
< q = quat_from_euler_xyz(roll, pitch, yaw)
---
> q = quat_from_euler_xyz(roll, pitch, yaw) # IsaacLab return wxyz
> q = q.roll(-1, dims=-1)
172c242,243
< q = quat_from_angle_axis(angle, axis)
---
> q = quat_from_angle_axis(angle, axis) # IsaacLab return wxyz
> q = q.roll(-1, dims=-1)
226c297,298
< heading_q = quat_from_angle_axis(heading, axis)
---
> heading_q = quat_from_angle_axis(heading, axis) # IsaacLab return wxyz
> heading_q = heading_q.roll(-1, dims=-1)
240c312,313
< heading_q = quat_from_angle_axis(-heading, axis)
---
> heading_q = quat_from_angle_axis(-heading, axis) # IsaacLab return wxyz
> heading_q = heading_q.roll(-1, dims=-1)
242a316
>
244c318
< if act_name == 'relu':
---
> if act_name == "relu":
246c320
< elif act_name == 'tanh':
---
> elif act_name == "tanh":
248c322
< elif act_name == 'sigmoid':
---
> elif act_name == "sigmoid":
261c335
< return nn.Identity
\ No newline at end of file
---
> return nn.Identity