Skip to content
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

[MAXTEXT] use split and concat directly instead of reshape and transpose and slice and concat #335

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions MaxText/layers/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,14 @@ def __call__(
* (self.max_timescale / self.min_timescale) ** fraction
)
position = position[:, :, jnp.newaxis, jnp.newaxis]
timescale = timescale[jnp.newaxis, jnp.newaxis, jnp.newaxis, :]
sinusoid_inp = position / timescale
sin = jnp.sin(sinusoid_inp)
cos = jnp.cos(sinusoid_inp)
reshape_tensor = inputs.astype(jnp.float32).reshape(
*inputs.shape[:-1], 2, -1
)
reshape_tensor = jax.numpy.swapaxes(reshape_tensor, -1, -2)
first_half = reshape_tensor[..., 0]
second_half = reshape_tensor[..., 1]
first_half, second_half = jnp.split(inputs, 2, axis=-1)
first_part = first_half * cos - second_half * sin
second_part = second_half * cos + first_half * sin
if self.cast_as_fprop_dtype:
first_part = first_part.astype(self.fprop_dtype)
second_part = second_part.astype(self.fprop_dtype)
x_out = jnp.stack((first_part, second_part), axis=-1).reshape(
*first_part.shape[:-1], -1
)
x_out = jnp.concatenate((first_part, second_part), axis=-1)
return x_out
Loading