Skip to content

Instantly share code, notes, and snippets.

@qgallouedec
qgallouedec / prefix_tree_deduplication.py
Last active October 7, 2026 23:01
Prefix-tree deduplication
from dataclasses import dataclass
import torch
from fla.ops.gated_delta_rule import chunk_gated_delta_rule
from kernels import get_kernel
from transformers import AttentionInterface
from transformers.activations import ACT2FN
from transformers.masking_utils import AttentionMaskInterface
from transformers.models.qwen3_5 import modeling_qwen3_5
@qgallouedec
qgallouedec / zero_sync_longcot_repro.py
Created August 27, 2026 16:32
Zero-sync GRPO on one GPU: KV pool vs training memory split (long-CoT)
"""Zero-sync GRPO on one GPU: transformers continuous batching generates from the SAME weights the
optimizer updates in place (no server, no weight sync). Minimal example for tinkering with the
KV-pool / training memory split in the long-CoT regime.
python zero_sync_longcot_repro.py --max-new 2048 --kv-percent 0.25 --inflight 32
python zero_sync_longcot_repro.py --max-new 4096 --kv-percent 0.4 --inflight 64
Both flow on an 80 GB H100 (completions 600-1400 tokens, sub-second to a few seconds per step).
The loss computes lm_head + logprob per token chunk under checkpoint, so the full-vocab logits
never materialize: the naive per-row fp32 version OOMs in the backward at these lengths, which
def compute_flops_per_token(config: PretrainedConfig, seq_len: int) -> int:
"""
Estimate training FLOPs per token for a causal language model (forward +
backward).
Supports dense and MoE architectures. Backward is assumed to cost 2* the forward
pass, so total
training FLOPs = 3 * forward FLOPs. The attention-score term uses the non-causal
convention (every
token attends to the full `seq_len`, matching PaLM / Megatron / nanoGPT); pass
import logging
import random
import pandas as pd
from accelerate.utils import is_wandb_available
from transformers import Trainer, TrainerCallback
from transformers.integrations import is_comet_available, is_mlflow_available
from trl.trainer.utils import log_table_to_comet_experiment
if is_wandb_available():
# coding=utf-8
# Copyright 2025 The HuggingFace Inc. team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@qgallouedec
qgallouedec / train.py
Last active September 13, 2025 23:17
from datasets import load_dataset
from trl import SFTTrainer
dataset = load_dataset("trl-lib/Capybara", split="train")
trainer = SFTTrainer(
model="Qwen/Qwen2.5-0.5B",
train_dataset=dataset,
)
trainer.train()
train.push_to_hub("Qwen2.5-0.5B-SFT")
import pandas as pd
from datetime import datetime, timedelta
from datasets import load_dataset
# Helper function to filter data based on time range
def filter_date_range(df, date_col, start_date, end_date):
return df[(df[date_col] >= start_date) & (df[date_col] < end_date)]
# Get the current time
from transformers import pipeline
from string import Template
template = Template(
"""Please act as an impartial judge and evaluate the quality of the responses provided by
two AI assistants to the user question displayed below. Your evaluation should consider
factors such as the helpfulness and relevance. Ensure that the order in which the responses
were presented does not influence your decision. Answer just by [[A]] if assistant A is better,
[[B]] if assistant B is better, and [[C]] for a tie.
import urllib.request
from datetime import datetime
import wandb
import yaml
from yaml.loader import SafeLoader
atari_ids = [
# "AdventureNoFrameskip-v4",
# "AirRaidNoFrameskip-v4",
@qgallouedec
qgallouedec / MyBeautifulGraph0.py
Last active November 28, 2021 17:09
MyBeautifulGraph0
from manim import * # I'll skip this line for the following
class MyBeautifulGraph(Scene):
def construct(self):
axes = Axes()
self.add(axes)