-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor rotary embedding 3: so it is not on cpu #9307
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -545,11 +545,14 @@ def get_1d_rotary_pos_embed( | |
assert dim % 2 == 0 | ||
|
||
if isinstance(pos, int): | ||
pos = np.arange(pos) | ||
pos = torch.arange(pos) | ||
if isinstance(pos, np.ndarray): | ||
pos = torch.from_numpy(pos) # type: ignore # [S] | ||
|
||
theta = theta * ntk_factor | ||
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2, dtype=freqs_dtype)[: (dim // 2)] / dim)) / linear_factor # [D/2] | ||
t = torch.from_numpy(pos).to(freqs.device) # type: ignore # [S] | ||
freqs = torch.outer(t, freqs) # type: ignore # [S, D/2] | ||
freqs = freqs.to(pos.device) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd expect this to cause a sync as well since by default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohhh let's do |
||
freqs = torch.outer(pos, freqs) # type: ignore # [S, D/2] | ||
if use_real and repeat_interleave_real: | ||
# flux, hunyuan-dit, cogvideox | ||
freqs_cos = freqs.cos().repeat_interleave(2, dim=1).float() # [S, D] | ||
|
@@ -626,7 +629,7 @@ def forward(self, ids: torch.Tensor) -> torch.Tensor: | |
n_axes = ids.shape[-1] | ||
cos_out = [] | ||
sin_out = [] | ||
pos = ids.squeeze().float().cpu().numpy() | ||
pos = ids.squeeze().float() | ||
is_mps = ids.device.type == "mps" | ||
freqs_dtype = torch.float32 if is_mps else torch.float64 | ||
for i in range(n_axes): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be passed a
device
argument to allocate it on the GPU. If this isn't on the GPU, then neither will the following Tensors.