-
Notifications
You must be signed in to change notification settings - Fork 589
DSV4: Add mhc_split_axis_contraction to contract the mHC rate and embed axes separately #4912
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
base: main
Are you sure you want to change the base?
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -75,6 +75,25 @@ def sinkhorn(t, iters=20): | |||||
| return t.astype(initial_dtype) | ||||||
|
|
||||||
|
|
||||||
| class _SplitAxesRMSNorm(nnx.Module): | ||||||
| """RMS normalization across the mHC rate and embedding axes.""" | ||||||
|
|
||||||
| def __init__(self, rate: int, dim: int, dtype, weight_dtype, epsilon: float, rngs: nnx.Rngs): | ||||||
| self.dtype = dtype | ||||||
| self.epsilon = epsilon | ||||||
| self.scale = nnx.Param( | ||||||
| jax.nn.initializers.ones(rngs.params(), (rate, dim), weight_dtype), | ||||||
| out_sharding=(None, "activation_embed"), | ||||||
| ) | ||||||
|
|
||||||
| def __call__(self, x: Array) -> Array: | ||||||
| x = jnp.asarray(x, jnp.float32) | ||||||
| mean2 = jnp.mean(jax.lax.square(x), axis=(-2, -1), keepdims=True) | ||||||
| y = jnp.asarray(x * jax.lax.rsqrt(mean2 + self.epsilon), self.dtype) | ||||||
| scale = jnp.asarray(self.scale.get_value(), self.dtype) | ||||||
|
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. Using
Suggested change
|
||||||
| return y * scale | ||||||
|
|
||||||
|
|
||||||
| class ManifoldConstrainedHyperConnections(nnx.Module): | ||||||
| """Implements Manifold-Constrained Hyper-Connections (mHC). | ||||||
|
|
||||||
|
|
@@ -107,15 +126,24 @@ def __init__( | |||||
| if getattr(self.config, "use_mhc_pallas_kernel", False) and not self.config.enable_mhc_lite: | ||||||
| raise ValueError("use_mhc_pallas_kernel=True requires enable_mhc_lite=True.") | ||||||
|
|
||||||
| # Norm layer | ||||||
| self.mhc_norm = RMSNorm( | ||||||
| num_features=self.k * self.dim, | ||||||
| dtype=self.config.dtype, | ||||||
| weight_dtype=self.weight_dtype, | ||||||
| kernel_axes=("norm",), | ||||||
| epsilon=self.config.normalization_layer_epsilon, | ||||||
| rngs=self.rngs, | ||||||
| ) | ||||||
| if self.config.mhc_split_axis_contraction: | ||||||
| self.mhc_norm = _SplitAxesRMSNorm( | ||||||
| rate=self.k, | ||||||
| dim=self.dim, | ||||||
| dtype=self.config.dtype, | ||||||
| weight_dtype=self.weight_dtype, | ||||||
| epsilon=self.config.normalization_layer_epsilon, | ||||||
| rngs=self.rngs, | ||||||
| ) | ||||||
| else: | ||||||
| self.mhc_norm = RMSNorm( | ||||||
| num_features=self.k * self.dim, | ||||||
| dtype=self.config.dtype, | ||||||
| weight_dtype=self.weight_dtype, | ||||||
| kernel_axes=("norm",), | ||||||
| epsilon=self.config.normalization_layer_epsilon, | ||||||
| rngs=self.rngs, | ||||||
| ) | ||||||
|
|
||||||
| # Scalars | ||||||
| self.res_alpha_scale = nnx.Param( | ||||||
|
|
@@ -142,15 +170,23 @@ def __init__( | |||||
| res_beta_shape = (self.k, self.k) | ||||||
| res_beta_sharding = (None, None) | ||||||
|
|
||||||
| # Weight matrices | ||||||
| scale_init = nd_dense_init(1.0, "fan_in", "normal") | ||||||
| in_axis = 0 | ||||||
| out_axis = 1 | ||||||
| weight_sharding_axis_name = ("activation_embed", None) | ||||||
| if self.config.mhc_split_axis_contraction: | ||||||
| in_axis = (0, 1) | ||||||
| out_axis = 2 | ||||||
| weight_sharding_axis_name = (None, "activation_embed", None) | ||||||
| res_alpha_shape = (self.k, self.dim, res_out_dim) | ||||||
| alpha_shape = (self.k, self.dim, self.k) | ||||||
| else: | ||||||
| in_axis = 0 | ||||||
| out_axis = 1 | ||||||
| weight_sharding_axis_name = ("activation_embed", None) | ||||||
| res_alpha_shape = (self.k * self.dim, res_out_dim) | ||||||
| alpha_shape = (self.k * self.dim, self.k) | ||||||
| self.res_alpha = nnx.Param( | ||||||
| scale_init( | ||||||
| self.rngs.params(), | ||||||
| (self.k * self.dim, res_out_dim), | ||||||
| res_alpha_shape, | ||||||
| self.weight_dtype, | ||||||
| in_axis=in_axis, | ||||||
| out_axis=out_axis, | ||||||
|
|
@@ -160,7 +196,7 @@ def __init__( | |||||
| self.pre_alpha = nnx.Param( | ||||||
| scale_init( | ||||||
| self.rngs.params(), | ||||||
| (self.k * self.dim, self.k), | ||||||
| alpha_shape, | ||||||
| self.weight_dtype, | ||||||
| in_axis=in_axis, | ||||||
| out_axis=out_axis, | ||||||
|
|
@@ -170,7 +206,7 @@ def __init__( | |||||
| self.post_alpha = nnx.Param( | ||||||
| scale_init( | ||||||
| self.rngs.params(), | ||||||
| (self.k * self.dim, self.k), | ||||||
| alpha_shape, | ||||||
| self.weight_dtype, | ||||||
| in_axis=in_axis, | ||||||
| out_axis=out_axis, | ||||||
|
|
@@ -286,8 +322,11 @@ def __call__( | |||||
| ) | ||||||
| else: | ||||||
| with jax.named_scope("mhc_norm"): | ||||||
| # 1. Flatten the tensor, and RMS normalization | ||||||
| norm_x = self.mhc_norm(jnp.reshape(x, (b, s, k * d))) | ||||||
| if self.config.mhc_split_axis_contraction: | ||||||
| norm_x = self.mhc_norm(x) | ||||||
| else: | ||||||
| # 1. Flatten the tensor, and RMS normalization | ||||||
| norm_x = self.mhc_norm(jnp.reshape(x, (b, s, k * d))) | ||||||
|
|
||||||
| # Fused Projections | ||||||
| pre_alpha = jnp.asarray(self.pre_alpha[...], self.dtype) | ||||||
|
|
@@ -297,7 +336,11 @@ def __call__( | |||||
| alpha_concat = jnp.concatenate([pre_alpha, post_alpha, res_alpha], axis=-1) | ||||||
|
|
||||||
| # MatMul on normalized input | ||||||
| h_concat = jnp.einsum("bsm,mn -> bsn", norm_x, alpha_concat, precision=self.matmul_precision) | ||||||
| if self.config.mhc_split_axis_contraction: | ||||||
| # Keeping the TP-sharded embed axis separate avoids gathering it before contraction. | ||||||
| h_concat = jnp.einsum("bskd,kdn -> bsn", norm_x, alpha_concat, precision=self.matmul_precision) | ||||||
| else: | ||||||
| h_concat = jnp.einsum("bsm,mn -> bsn", norm_x, alpha_concat, precision=self.matmul_precision) | ||||||
| h_pre = h_concat[..., : self.k] | ||||||
| h_post = h_concat[..., self.k : 2 * self.k] | ||||||
| h_res = h_concat[..., 2 * self.k :] | ||||||
|
|
||||||
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.
Accessing
t.shapedirectly on the elements ofinput_tensorsbefore converting them to numpy arrays can raise anAttributeErrorif any element is a list or other non-array-like object. It is safer and more robust to convert to a numpy array first and then access.shapeon the converted array.