helper function to spherically interpolate two arrays v1 v2
(t, v0, v1, DOT_THRESHOLD=0.9995)
| 20 | |
| 21 | |
| 22 | def slerp(t, v0, v1, DOT_THRESHOLD=0.9995): |
| 23 | """helper function to spherically interpolate two arrays v1 v2""" |
| 24 | |
| 25 | if not isinstance(v0, np.ndarray): |
| 26 | inputs_are_torch = True |
| 27 | input_device = v0.device |
| 28 | v0 = v0.cpu().numpy() |
| 29 | v1 = v1.cpu().numpy() |
| 30 | |
| 31 | dot = np.sum(v0 * v1 / (np.linalg.norm(v0) * np.linalg.norm(v1))) |
| 32 | if np.abs(dot) > DOT_THRESHOLD: |
| 33 | v2 = (1 - t) * v0 + t * v1 |
| 34 | else: |
| 35 | theta_0 = np.arccos(dot) |
| 36 | sin_theta_0 = np.sin(theta_0) |
| 37 | theta_t = theta_0 * t |
| 38 | sin_theta_t = np.sin(theta_t) |
| 39 | s0 = np.sin(theta_0 - theta_t) / sin_theta_0 |
| 40 | s1 = sin_theta_t / sin_theta_0 |
| 41 | v2 = s0 * v0 + s1 * v1 |
| 42 | |
| 43 | if inputs_are_torch: |
| 44 | v2 = torch.from_numpy(v2).to(input_device) |
| 45 | |
| 46 | return v2 |
| 47 | |
| 48 | |
| 49 | class StableDiffusionWalkPipeline(DiffusionPipeline, StableDiffusionMixin): |