Files
fblissjr-ComfyUI-WanSeamles…/utils/optimization.py
T
2025-03-16 10:12:43 -05:00

84 lines
2.3 KiB
Python

# WanSeamlessFlow/utils/optimization.py
import numpy as np
import torch
from typing import List, Tuple
def calc_embedding_distance(embed1, embed2):
"""
Calculate distance between two embeddings.
Args:
embed1: First embedding tensor
embed2: Second embedding tensor
Returns:
Distance measure (lower means more similar)
"""
# Move to CPU for numpy compatibility
e1 = embed1.cpu().numpy()
e2 = embed2.cpu().numpy()
# Calculate L2 distance
return np.sum((e1 - e2)**2)
def optimize_embedding_order(embeddings: List[torch.Tensor]) -> List[int]:
"""
Optimize the order of embeddings to minimize semantic distance between consecutive prompts.
Args:
embeddings: List of embedding tensors
Returns:
Optimized ordering indices
"""
if len(embeddings) <= 1:
return list(range(len(embeddings)))
# Calculate embedding centroids
means = [torch.mean(embed, dim=0).cpu().numpy() for embed in embeddings]
# Nearest neighbor ordering
order = [0] # Start with first prompt
remaining = set(range(1, len(means)))
while remaining:
curr, best_dist = order[-1], float('inf')
best_next = None
for i in remaining:
# Calculate distance between current and candidate
dist = np.sum((means[curr] - means[i])**2)
if dist < best_dist:
best_dist, best_next = dist, i
order.append(best_next)
remaining.remove(best_next)
return order
def compute_transition_cost_matrix(embeddings: List[torch.Tensor]) -> np.ndarray:
"""
Compute full transition cost matrix between all embeddings.
Args:
embeddings: List of embedding tensors
Returns:
Matrix where cost_matrix[i][j] is transition cost from i to j
"""
n = len(embeddings)
cost_matrix = np.zeros((n, n), dtype=np.float32)
# Calculate centroids for efficiency
means = [torch.mean(embed, dim=0).cpu().numpy() for embed in embeddings]
# Compute all pairwise distances
for i in range(n):
for j in range(n):
if i != j:
cost_matrix[i, j] = np.sum((means[i] - means[j])**2)
return cost_matrix