Views
No views yet
prepare_dataset.py builds a local dataset of grouped embeddings from a base Qwen3 with a custom layer 0 that performs token grouping.train_custom_qwen3.py fine-tunes a customized Qwen3 that adds a small MLP adapter for grouped inputs, while freezing all weights except layer 0.inference_qwen3_merged.py runs end-to-end inference by first grouping with the base model, then generating with the trained model that understands grouped inputs. Includes perf metrics and estimated attention-memory savings.grouped_hidden_states.grouped_hidden_states, and serializes them together with target responses.grouped_inputs as inputs_embeds, then generation proceeds with past-key-values.torch, transformers, datasets, tqdm, psutil. These are imported directly in the scripts.pip install torch transformers datasets tqdm psutilprepare_dataset.py - dataset builder using custom layer 0 grouping.train_custom_qwen3.py - trainer for grouped-input Qwen3 with an MLP adapter, freezing all but layer 0.inference_qwen3_merged.py - two-stage inference runner with metrics.python prepare_dataset.pyDatasetProcessor:model_name="Qwen/Qwen3-0.6B"dataset_name="Magpie-Align/Magpie-Qwen2.5-Pro-1M-v0.1"output_dir="./processed_dataset"batch_size=1, max_samples=None, save_frequency=1000
Edit these in the constructor if you need to change them.grouped_hidden_states, and buffers results.output_dir:processed_dataset.pkl - list of samples with inputs_embeds (grouped), response, and metadata.python train_custom_qwen3.py --mode trainmodel_name="Qwen/Qwen3-0.6B"dataset_path="./processed_qwen3_dataset/processed_dataset.pkl"output_dir="./grouped_qwen3_checkpoint"batch_size=4, learning_rate=5e-4, num_epochs=3, warmup_steps=100grouped_inputs via inputs_embeds with is_prefill=True.past_key_values.1python inference_qwen3_merged.py \
2 --checkpoint ./grouped_qwen3_checkpoint/epoch_2_best \
3 --grouping_model Qwen/Qwen3-0.6B \
4 --instruction "Explain attention like I am in 9th grade" \
5 --max_length 256 \
6 --temperature 0.7 \
7 --device cuda--checkpoint, --grouping_model, --instruction, --max_length, --temperature, --no_sample for greedy, and --device for cuda or cpu.load_trained_model and generate_with_grouped_input in the training script if you prefer a programmatic flow.model_name - base HF model for grouping, default Qwen/Qwen3-0.6B.dataset_name - source HF dataset split, default Magpie-Align... Qwen2.5-Pro-1M.output_dir - where pickles and metadata go.max_samples - optional cap for quick tests.dataset_path - path to processed_dataset.pkl.output_dir - where checkpoints are written.batch_size, learning_rate, num_epochs, warmup_steps - training hyperparams.requires_grad settings in _freeze_layers.--checkpoint - path to trained checkpoint folder.--grouping_model - HF model name used for grouping.--instruction - user prompt, any language.--max_length, --temperature, --no_sample, --device.CustomQwen3Attention.is_initialized is reset before each prefill.pytorch_model.bin or model.safetensors in the checkpoint directory.N to G groups, attention memory scales roughly with G^2 vs N^2. The script prints an estimated savings based on that relation.@misc{Kolomeitsev2025ContextMerging,
title = {Context Merging: from Tokens to Entities and Concepts},
author = {Konstantin Kolomeitsev},
year = {2025}
}