Skip to content

Commit 7e6d81d

Browse files
Add auto_scale_fc0_kernel option to GLN MoE model.
1 parent de9efc5 commit 7e6d81d

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/eir/models/input/array/models_locally_connected.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,17 @@ class LCLInformedMoEModelConfig(LCLModelConfig):
301301
and return zeros of shape (batch_size, 1) during forward.
302302
Useful for retaining first kernel outputs to be passed e.g. exclusively
303303
via TB.
304+
305+
:param auto_scale_fc0_kernel:
306+
When True, the fc_0 kernel size is automatically scaled per expert
307+
based on each expert's SNP count. Overrides the global
308+
kernel_width / first_kernel_expansion for the first layer only.
309+
Resulting kernel sizes are always divisible by 3.
304310
"""
305311

306312
stub_experts: bool = False
307313
expert_output_dim: int | None = None
314+
auto_scale_fc0_kernel: bool = False
308315

309316

310317
class ExpertBranch(nn.Module):
@@ -379,12 +386,18 @@ def __init__(
379386
)
380387
self._expert_indices[name] = getattr(self, f"_expert_idx_{name}")
381388

389+
n_snps = len(snp_indices)
382390
expert_in_features = (
383-
len(snp_indices) * data_dimensions.channels * data_dimensions.height
391+
n_snps * data_dimensions.channels * data_dimensions.height
384392
)
385393

394+
if model_config.auto_scale_fc0_kernel:
395+
cur_fc_0_kernel = _get_auto_scaled_fc0_kernel(n_snps=n_snps)
396+
else:
397+
cur_fc_0_kernel = fc_0_kernel_size
398+
386399
expert_fc_0_kernel = _clamp_kernel_for_min_chunks(
387-
kernel_size=fc_0_kernel_size,
400+
kernel_size=cur_fc_0_kernel,
388401
in_features=expert_in_features,
389402
min_chunks=4,
390403
min_kernel=4,
@@ -807,6 +820,21 @@ def _clamp_kernel_for_min_chunks(
807820
return max(clamped, min_kernel)
808821

809822

823+
def _get_auto_scaled_fc0_kernel(n_snps: int) -> int:
824+
if n_snps < 1_000:
825+
return 3
826+
elif n_snps < 10_000:
827+
return 6
828+
elif n_snps < 100_000:
829+
return 12
830+
elif n_snps < 500_000:
831+
return 24
832+
elif n_snps < 2_000_000:
833+
return 48
834+
else:
835+
return 96
836+
837+
810838
def _do_add_attention(
811839
in_features: int, embedding_dim: int, attention_inclusion_cutoff: int | None
812840
) -> bool:

0 commit comments

Comments
 (0)