sample_id
stringlengths
21
196
text
stringlengths
105
936k
metadata
dict
category
stringclasses
6 values
karpathy/nanochat:dev/repackage_data_reference.py
""" Repackage the FinewebEdu-100B dataset into shards: - each shard is ~100MB in size (after zstd compression) - parquets are written with row group size of 1000 - shuffle the dataset This will be uploaded to HuggingFace for hosting. The big deal is that our DataLoader will be able to stream the data and cache it along the way on disk, decreasing the training latency. NOTE: This file is meant only as reference/documentation of the dataset preparation and it is not used during the project runtime. """ import os import time from datasets import load_dataset import pyarrow.parquet as pq import pyarrow as pa # Source dataset dataset_kwargs = { "path": "HuggingFaceFW/fineweb-edu", "split": "train", "name": "sample-100BT", # ~100B GPT-2 tokens at ~3 chars/token => ~300B chars total } ds = load_dataset(**dataset_kwargs) # Shuffle to scramble the order ds = ds.shuffle(seed=42) ndocs = len(ds) # total number of documents to process print(f"Total number of documents: {ndocs}") # Repackage into parquet files output_dir = "/home/ubuntu/.cache/nanochat/base_data" os.makedirs(output_dir, exist_ok=True) # Write to parquet files chars_per_shard = 250_000_000 row_group_size = 1024 # HF uses 1000 but we use multiple of 2, nicer for distributed data loader later shard_docs = [] shard_index = 0 shard_characters = 0 total_docs_processed = 0 total_time_spent = 0 t0 = time.time() for doc in ds: text = doc['text'] shard_docs.append(text) shard_characters += len(text) collected_enough_chars = shard_characters >= chars_per_shard docs_multiple_of_row_group_size = len(shard_docs) % row_group_size == 0 if collected_enough_chars and docs_multiple_of_row_group_size: # leads to ~100MB of text (compressed) shard_path = os.path.join(output_dir, f"shard_{shard_index:05d}.parquet") shard_table = pa.Table.from_pydict({"text": shard_docs}) pq.write_table( shard_table, shard_path, row_group_size=row_group_size, use_dictionary=False, # this is usually used for categorical data compression="zstd", # Valid values: {β€˜NONE’, β€˜SNAPPY’, β€˜GZIP’, β€˜BROTLI’, β€˜LZ4’, β€˜ZSTD’} compression_level=3, write_statistics=False, # not needed for text ) t1 = time.time() dt = t1 - t0 # for this shard alone t0 = t1 total_docs_processed += len(shard_docs) total_time_spent += dt remaining_docs = ndocs - total_docs_processed avg_time_per_doc = total_time_spent / total_docs_processed remaining_time = remaining_docs * avg_time_per_doc remaining_time_hours = remaining_time / 3600 print(f"Wrote {shard_path}. #documents: {len(shard_docs)} | #characters: {shard_characters} | time: {dt:.2f}s | remaining time: {remaining_time_hours:.2f}h") shard_docs = [] shard_characters = 0 shard_index += 1 # Demonstration of how the data was later uploaded to HuggingFace def upload(): import os from huggingface_hub import HfApi token = os.getenv("HF_TOKEN") api = HfApi(token=token) api.upload_large_folder( folder_path=output_dir, repo_id="karpathy/fineweb-edu-100b-shuffle", repo_type="dataset", ) # upload()
{ "repo_id": "karpathy/nanochat", "file_path": "dev/repackage_data_reference.py", "license": "MIT License", "lines": 83, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
karpathy/nanochat:nanochat/checkpoint_manager.py
""" Utilities for saving and loading model/optim/state checkpoints. """ import os import re import glob import json import logging import torch from nanochat.common import get_base_dir from nanochat.gpt import GPT, GPTConfig from nanochat.tokenizer import get_tokenizer from nanochat.common import setup_default_logging # Set up logging setup_default_logging() logger = logging.getLogger(__name__) def log0(message): if int(os.environ.get('RANK', 0)) == 0: logger.info(message) def _patch_missing_config_keys(model_config_kwargs): """Add default values for new config keys missing in old checkpoints.""" # Old models were trained with full context (no sliding window) if "window_pattern" not in model_config_kwargs: model_config_kwargs["window_pattern"] = "L" log0(f"Patching missing window_pattern in model config to 'L'") def _patch_missing_keys(model_data, model_config): """Add default values for new parameters that may be missing in old checkpoints.""" n_layer = model_config.n_layer # resid_lambdas defaults to 1.0 (identity scaling) if "resid_lambdas" not in model_data: model_data["resid_lambdas"] = torch.ones(n_layer) log0(f"Patching missing resid_lambdas in model data to 1.0") # x0_lambdas defaults to 0.0 (disabled) if "x0_lambdas" not in model_data: model_data["x0_lambdas"] = torch.zeros(n_layer) log0(f"Patching missing x0_lambdas in model data to 0.0") def save_checkpoint(checkpoint_dir, step, model_data, optimizer_data, meta_data, rank=0): if rank == 0: os.makedirs(checkpoint_dir, exist_ok=True) # Save the model state parameters model_path = os.path.join(checkpoint_dir, f"model_{step:06d}.pt") torch.save(model_data, model_path) logger.info(f"Saved model parameters to: {model_path}") # Save the metadata dict as json meta_path = os.path.join(checkpoint_dir, f"meta_{step:06d}.json") with open(meta_path, "w", encoding="utf-8") as f: json.dump(meta_data, f, indent=2) logger.info(f"Saved metadata to: {meta_path}") # Note that optimizer state is sharded across ranks, so each rank must save its own. if optimizer_data is not None: os.makedirs(checkpoint_dir, exist_ok=True) optimizer_path = os.path.join(checkpoint_dir, f"optim_{step:06d}_rank{rank:d}.pt") torch.save(optimizer_data, optimizer_path) logger.info(f"Saved optimizer state to: {optimizer_path}") def load_checkpoint(checkpoint_dir, step, device, load_optimizer=False, rank=0): # Load the model state model_path = os.path.join(checkpoint_dir, f"model_{step:06d}.pt") model_data = torch.load(model_path, map_location=device) # Load the optimizer state if requested optimizer_data = None if load_optimizer: optimizer_path = os.path.join(checkpoint_dir, f"optim_{step:06d}_rank{rank:d}.pt") optimizer_data = torch.load(optimizer_path, map_location=device) # Load the metadata meta_path = os.path.join(checkpoint_dir, f"meta_{step:06d}.json") with open(meta_path, "r", encoding="utf-8") as f: meta_data = json.load(f) return model_data, optimizer_data, meta_data def build_model(checkpoint_dir, step, device, phase): """ A bunch of repetitive code to build a model from a given checkpoint. Returns: - base model - uncompiled, not wrapped in DDP - tokenizer - meta data saved during base model training """ assert phase in ["train", "eval"], f"Invalid phase: {phase}" model_data, optimizer_data, meta_data = load_checkpoint(checkpoint_dir, step, device, load_optimizer=False) if device.type in {"cpu", "mps"}: # Convert bfloat16 tensors to float for CPU inference model_data = { k: v.float() if v.dtype == torch.bfloat16 else v for k, v in model_data.items() } # Hack: fix torch compile issue, which prepends all keys with _orig_mod. model_data = {k.removeprefix("_orig_mod."): v for k, v in model_data.items()} model_config_kwargs = meta_data["model_config"] _patch_missing_config_keys(model_config_kwargs) log0(f"Building model with config: {model_config_kwargs}") model_config = GPTConfig(**model_config_kwargs) _patch_missing_keys(model_data, model_config) with torch.device("meta"): model = GPT(model_config) # Load the model state model.to_empty(device=device) model.init_weights() # note: this is dumb, but we need to init the rotary embeddings. TODO: fix model re-init model.load_state_dict(model_data, strict=True, assign=True) # Put the model in the right training phase / mode if phase == "eval": model.eval() else: model.train() # Load the Tokenizer tokenizer = get_tokenizer() # Sanity check: compatibility between model and tokenizer assert tokenizer.get_vocab_size() == model_config_kwargs["vocab_size"], f"Tokenizer vocab size {tokenizer.get_vocab_size()} does not match model config vocab size {model_config_kwargs['vocab_size']}" return model, tokenizer, meta_data def find_largest_model(checkpoints_dir): # attempt to guess the model tag: take the biggest model available model_tags = [f for f in os.listdir(checkpoints_dir) if os.path.isdir(os.path.join(checkpoints_dir, f))] if not model_tags: raise FileNotFoundError(f"No checkpoints found in {checkpoints_dir}") # 1) normally all model tags are of the form d<number>, try that first: candidates = [] for model_tag in model_tags: match = re.match(r"d(\d+)", model_tag) if match: model_depth = int(match.group(1)) candidates.append((model_depth, model_tag)) if candidates: candidates.sort(key=lambda x: x[0], reverse=True) return candidates[0][1] # 2) if that failed, take the most recently updated model: model_tags.sort(key=lambda x: os.path.getmtime(os.path.join(checkpoints_dir, x)), reverse=True) return model_tags[0] def find_last_step(checkpoint_dir): # Look into checkpoint_dir and find model_<step>.pt with the highest step checkpoint_files = glob.glob(os.path.join(checkpoint_dir, "model_*.pt")) if not checkpoint_files: raise FileNotFoundError(f"No checkpoints found in {checkpoint_dir}") last_step = int(max(os.path.basename(f).split("_")[-1].split(".")[0] for f in checkpoint_files)) return last_step # ----------------------------------------------------------------------------- # convenience functions that take into account nanochat's directory structure def load_model_from_dir(checkpoints_dir, device, phase, model_tag=None, step=None): if model_tag is None: # guess the model tag by defaulting to the largest model model_tag = find_largest_model(checkpoints_dir) log0(f"No model tag provided, guessing model tag: {model_tag}") checkpoint_dir = os.path.join(checkpoints_dir, model_tag) if step is None: # guess the step by defaulting to the last step step = find_last_step(checkpoint_dir) assert step is not None, f"No checkpoints found in {checkpoint_dir}" # build the model log0(f"Loading model from {checkpoint_dir} with step {step}") model, tokenizer, meta_data = build_model(checkpoint_dir, step, device, phase) return model, tokenizer, meta_data def load_model(source, *args, **kwargs): model_dir = { "base": "base_checkpoints", "sft": "chatsft_checkpoints", "rl": "chatrl_checkpoints", }[source] base_dir = get_base_dir() checkpoints_dir = os.path.join(base_dir, model_dir) return load_model_from_dir(checkpoints_dir, *args, **kwargs) def load_optimizer_state(source, device, rank, model_tag=None, step=None): """Load just the optimizer shard for a given rank, without re-loading the model.""" model_dir = { "base": "base_checkpoints", "sft": "chatsft_checkpoints", "rl": "chatrl_checkpoints", }[source] base_dir = get_base_dir() checkpoints_dir = os.path.join(base_dir, model_dir) if model_tag is None: model_tag = find_largest_model(checkpoints_dir) checkpoint_dir = os.path.join(checkpoints_dir, model_tag) if step is None: step = find_last_step(checkpoint_dir) optimizer_path = os.path.join(checkpoint_dir, f"optim_{step:06d}_rank{rank:d}.pt") if not os.path.exists(optimizer_path): log0(f"Optimizer checkpoint not found: {optimizer_path}") return None log0(f"Loading optimizer state from {optimizer_path}") optimizer_data = torch.load(optimizer_path, map_location=device) return optimizer_data
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/checkpoint_manager.py", "license": "MIT License", "lines": 178, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/common.py
""" Common utilities for nanochat. """ import os import re import logging import urllib.request import torch import torch.distributed as dist from filelock import FileLock class ColoredFormatter(logging.Formatter): """Custom formatter that adds colors to log messages.""" # ANSI color codes COLORS = { 'DEBUG': '\033[36m', # Cyan 'INFO': '\033[32m', # Green 'WARNING': '\033[33m', # Yellow 'ERROR': '\033[31m', # Red 'CRITICAL': '\033[35m', # Magenta } RESET = '\033[0m' BOLD = '\033[1m' def format(self, record): # Add color to the level name levelname = record.levelname if levelname in self.COLORS: record.levelname = f"{self.COLORS[levelname]}{self.BOLD}{levelname}{self.RESET}" # Format the message message = super().format(record) # Add color to specific parts of the message if levelname == 'INFO': # Highlight numbers and percentages message = re.sub(r'(\d+\.?\d*\s*(?:GB|MB|%|docs))', rf'{self.BOLD}\1{self.RESET}', message) message = re.sub(r'(Shard \d+)', rf'{self.COLORS["INFO"]}{self.BOLD}\1{self.RESET}', message) return message def setup_default_logging(): handler = logging.StreamHandler() handler.setFormatter(ColoredFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) logging.basicConfig( level=logging.INFO, handlers=[handler] ) setup_default_logging() logger = logging.getLogger(__name__) def get_base_dir(): # co-locate nanochat intermediates with other cached data in ~/.cache (by default) if os.environ.get("NANOCHAT_BASE_DIR"): nanochat_dir = os.environ.get("NANOCHAT_BASE_DIR") else: home_dir = os.path.expanduser("~") cache_dir = os.path.join(home_dir, ".cache") nanochat_dir = os.path.join(cache_dir, "nanochat") os.makedirs(nanochat_dir, exist_ok=True) return nanochat_dir def download_file_with_lock(url, filename, postprocess_fn=None): """ Downloads a file from a URL to a local path in the base directory. Uses a lock file to prevent concurrent downloads among multiple ranks. """ base_dir = get_base_dir() file_path = os.path.join(base_dir, filename) lock_path = file_path + ".lock" if os.path.exists(file_path): return file_path with FileLock(lock_path): # Only a single rank can acquire this lock # All other ranks block until it is released # Recheck after acquiring lock if os.path.exists(file_path): return file_path # Download the content as bytes print(f"Downloading {url}...") with urllib.request.urlopen(url) as response: content = response.read() # bytes # Write to local file with open(file_path, 'wb') as f: f.write(content) print(f"Downloaded to {file_path}") # Run the postprocess function if provided if postprocess_fn is not None: postprocess_fn(file_path) return file_path def print0(s="",**kwargs): ddp_rank = int(os.environ.get('RANK', 0)) if ddp_rank == 0: print(s, **kwargs) def print_banner(): # Cool DOS Rebel font ASCII banner made with https://manytools.org/hacker-tools/ascii-banner/ banner = """ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ """ print0(banner) def is_ddp_requested() -> bool: """ True if launched by torchrun (env present), even before init. Used to decide whether we *should* initialize a PG. """ return all(k in os.environ for k in ("RANK", "LOCAL_RANK", "WORLD_SIZE")) def is_ddp_initialized() -> bool: """ True if torch.distributed is available and the process group is initialized. Used at cleanup to avoid destroying a non-existent PG. """ return dist.is_available() and dist.is_initialized() def get_dist_info(): if is_ddp_requested(): # We rely on torchrun's env to decide if we SHOULD init. # (Initialization itself happens in compute init.) assert all(var in os.environ for var in ['RANK', 'LOCAL_RANK', 'WORLD_SIZE']) ddp_rank = int(os.environ['RANK']) ddp_local_rank = int(os.environ['LOCAL_RANK']) ddp_world_size = int(os.environ['WORLD_SIZE']) return True, ddp_rank, ddp_local_rank, ddp_world_size else: return False, 0, 0, 1 def autodetect_device_type(): # prefer to use CUDA if available, otherwise use MPS, otherwise fallback on CPU if torch.cuda.is_available(): device_type = "cuda" elif torch.backends.mps.is_available(): device_type = "mps" else: device_type = "cpu" print0(f"Autodetected device type: {device_type}") return device_type def compute_init(device_type="cuda"): # cuda|cpu|mps """Basic initialization that we keep doing over and over, so make common.""" assert device_type in ["cuda", "mps", "cpu"], "Invalid device type atm" if device_type == "cuda": assert torch.cuda.is_available(), "Your PyTorch installation is not configured for CUDA but device_type is 'cuda'" if device_type == "mps": assert torch.backends.mps.is_available(), "Your PyTorch installation is not configured for MPS but device_type is 'mps'" # Reproducibility # Note that we set the global seeds here, but most of the code uses explicit rng objects. # The only place where global rng might be used is nn.Module initialization of the model weights. torch.manual_seed(42) if device_type == "cuda": torch.cuda.manual_seed(42) # skipping full reproducibility for now, possibly investigate slowdown later # torch.use_deterministic_algorithms(True) # Precision if device_type == "cuda": torch.set_float32_matmul_precision("high") # uses tf32 instead of fp32 for matmuls, see https://docs.pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html # Distributed setup: Distributed Data Parallel (DDP), optional, and requires CUDA is_ddp_requested, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info() if is_ddp_requested and device_type == "cuda": device = torch.device("cuda", ddp_local_rank) torch.cuda.set_device(device) # make "cuda" default to this device dist.init_process_group(backend="nccl", device_id=device) dist.barrier() else: device = torch.device(device_type) # mps|cpu if ddp_rank == 0: logger.info(f"Distributed world size: {ddp_world_size}") return is_ddp_requested, ddp_rank, ddp_local_rank, ddp_world_size, device def compute_cleanup(): """Companion function to compute_init, to clean things up before script exit""" if is_ddp_initialized(): dist.destroy_process_group() class DummyWandb: """Useful if we wish to not use wandb but have all the same signatures""" def __init__(self): pass def log(self, *args, **kwargs): pass def finish(self): pass # hardcoded BF16 peak flops for various GPUs # inspired by torchtitan: https://github.com/pytorch/torchtitan/blob/main/torchtitan/tools/utils.py # and PR: https://github.com/karpathy/nanochat/pull/147 def get_peak_flops(device_name: str) -> float: name = device_name.lower() # Table order matters: more specific patterns first. _PEAK_FLOPS_TABLE = ( # NVIDIA Blackwell (["gb200"], 2.5e15), (["grace blackwell"], 2.5e15), (["b200"], 2.25e15), (["b100"], 1.8e15), # NVIDIA Hopper (["h200", "nvl"], 836e12), (["h200", "pcie"], 836e12), (["h200"], 989e12), (["h100", "nvl"], 835e12), (["h100", "pcie"], 756e12), (["h100"], 989e12), (["h800", "nvl"], 989e12), (["h800"], 756e12), # NVIDIA Ampere data center (["a100"], 312e12), (["a800"], 312e12), (["a40"], 149.7e12), (["a30"], 165e12), # NVIDIA Ada data center (["l40s"], 362e12), (["l40-s"], 362e12), (["l40 s"], 362e12), (["l4"], 121e12), # AMD CDNA accelerators (["mi355"], 2.5e15), (["mi325"], 1.3074e15), (["mi300x"], 1.3074e15), (["mi300a"], 980.6e12), (["mi250x"], 383e12), (["mi250"], 362.1e12), # Consumer RTX (["5090"], 209.5e12), (["4090"], 165.2e12), (["3090"], 71e12), ) for patterns, flops in _PEAK_FLOPS_TABLE: if all(p in name for p in patterns): return flops if "data center gpu max 1550" in name: # Ponte Vecchio (PVC) - dynamic based on compute units max_comp_units = torch.xpu.get_device_properties("xpu").max_compute_units return 512 * max_comp_units * 1300 * 10**6 # Unknown GPU - return inf so MFU shows as 0% rather than a wrong guess logger.warning(f"Peak flops undefined for: {device_name}, MFU will show as 0%") return float('inf')
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/common.py", "license": "MIT License", "lines": 227, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/core_eval.py
""" Functions for evaluating the CORE metric, as described in the DCLM paper. https://arxiv.org/abs/2406.11794 TODOs: - All tasks ~match except for squad. We get 31% reference is 37%. Figure out why. """ import random from jinja2 import Template import torch import torch.distributed as dist # ----------------------------------------------------------------------------- # Prompt rendering utilities def render_prompts_mc(item, continuation_delimiter, fewshot_examples=None): """Render complete prompts for a multiple choice question""" template_str = """ {%- for example in fewshot_examples -%} {{ example.query }}{{ continuation_delimiter }}{{ example.choices[example.gold] }} {% endfor -%} {{ item.query }}{{ continuation_delimiter }}{{ choice }}""".strip() template = Template(template_str) fewshot_examples = fewshot_examples or [] context = { 'fewshot_examples': fewshot_examples, 'continuation_delimiter': continuation_delimiter, 'item': item } prompts = [template.render(choice=choice, **context) for choice in item['choices']] return prompts def render_prompts_schema(item, continuation_delimiter, fewshot_examples=None): """Render complete prompts for a schema question""" template_str = """ {%- for example in fewshot_examples -%} {{ example.context_options[example.gold] }}{{ continuation_delimiter }}{{ example.continuation }} {% endfor -%} {{ context }}{{ continuation_delimiter }}{{ item.continuation }}""".strip() template = Template(template_str) fewshot_examples = fewshot_examples or [] context = { 'fewshot_examples': fewshot_examples, 'continuation_delimiter': continuation_delimiter, 'item': item } prompts = [template.render(context=context_option, **context) for context_option in item['context_options']] return prompts def render_prompts_lm(item, continuation_delimiter, fewshot_examples=None): """ Render complete prompt for a language modeling task. Notice that we manually trim the context in the template, which in some datasets seems to have trailing whitespace (which we don't want). """ template_str = """ {%- for example in fewshot_examples -%} {{ example.context | trim }}{{ continuation_delimiter }}{{ example.continuation }} {% endfor -%} {{ item.context | trim }}{{ continuation_delimiter }}{% if include_continuation %}{{ item.continuation }}{% endif %}""".strip() template = Template(template_str) fewshot_examples = fewshot_examples or [] context = { 'fewshot_examples': fewshot_examples, 'continuation_delimiter': continuation_delimiter, 'item': item } # Return two prompts: without and with the continuation prompt_without = template.render(include_continuation=False, **context) prompt_with = template.render(include_continuation=True, **context) # Due to the way the data seems to be stored, I think I need to strip in the case of LM here. # Otherwise we may get trailing whitespaces in prompt_without (which get absorbed into the next # token in prompt_with), meaning we don't get a nice and clean prefix in the token space # to detect the final continuation. Tokenizers... prompt_without = prompt_without.strip() return [prompt_without, prompt_with] def find_common_length(token_sequences, direction='left'): """ Find the length of the common prefix or suffix across token sequences - direction: 'left' for prefix, 'right' for suffix """ min_len = min(len(seq) for seq in token_sequences) indices = { 'left': range(min_len), 'right': range(-1, -min_len-1, -1) }[direction] # Find the first position where the token sequences differ for i, idx in enumerate(indices): token = token_sequences[0][idx] if not all(seq[idx] == token for seq in token_sequences): return i return min_len def stack_sequences(tokens, pad_token_id): """Stack up a list of token sequences, pad to longest on the right""" bsz, seq_len = len(tokens), max(len(x) for x in tokens) input_ids = torch.full((bsz, seq_len), pad_token_id, dtype=torch.long) for i, x in enumerate(tokens): input_ids[i, :len(x)] = torch.tensor(x, dtype=torch.long) return input_ids def batch_sequences_mc(tokenizer, prompts): # In multiple choice, contexts are the same but the continuation is different (common prefix) tokens = tokenizer(prompts, prepend=tokenizer.get_bos_token_id()) # figure out the start and end of each continuation answer_start_idx = find_common_length(tokens, direction='left') start_indices = [answer_start_idx] * len(prompts) end_indices = [len(x) for x in tokens] return tokens, start_indices, end_indices def batch_sequences_schema(tokenizer, prompts): # In schema tasks, contexts vary but continuation is the same (common suffix) tokens = tokenizer(prompts, prepend=tokenizer.get_bos_token_id()) # figure out the start and end of each context suffix_length = find_common_length(tokens, direction='right') end_indices = [len(x) for x in tokens] start_indices = [ei - suffix_length for ei in end_indices] return tokens, start_indices, end_indices def batch_sequences_lm(tokenizer, prompts): # In LM tasks, we have two prompts: without and with continuation tokens = tokenizer(prompts, prepend=tokenizer.get_bos_token_id()) tokens_without, tokens_with = tokens start_idx, end_idx = len(tokens_without), len(tokens_with) assert start_idx < end_idx, "prompt without is supposed to be a prefix of prompt with" assert tokens_without == tokens_with[:start_idx], "prompt without is supposed to be a prefix of prompt with" # we only need the with continuation prompt in the LM task, i.e. batch size of 1 return [tokens_with], [start_idx], [end_idx] @torch.no_grad() def forward_model(model, input_ids): """ Take BxT tensor of token ids, return BxT tensor of losses and argmax predictions. The last column of losses is set to nan because we don't have autoregressive targets there. """ batch_size, seq_len = input_ids.size() outputs = model(input_ids) # Roll the tensor to the left by one position to get the (autoregressive) target ids target_ids = torch.roll(input_ids, shifts=-1, dims=1) # Calculate cross entropy at all positions losses = torch.nn.functional.cross_entropy( outputs.view(batch_size * seq_len, -1), target_ids.view(batch_size * seq_len), reduction='none' ).view(batch_size, seq_len) # Set the last column to be nan because there is no autoregressive loss there losses[:, -1] = float('nan') # Get the argmax predictions at each position predictions = outputs.argmax(dim=-1) return losses, predictions @torch.no_grad() def evaluate_example(idx, model, tokenizer, data, device, task_meta): """Evaluate a single example, return True if correct, False otherwise""" item = data[idx] task_type = task_meta['task_type'] num_fewshot = task_meta['num_fewshot'] continuation_delimiter = task_meta['continuation_delimiter'] # Sample few-shot examples (excluding current item) fewshot_examples = [] if num_fewshot > 0: rng = random.Random(1234 + idx) available_indices = [i for i in range(len(data)) if i != idx] fewshot_indices = rng.sample(available_indices, num_fewshot) fewshot_examples = [data[i] for i in fewshot_indices] # Render prompts and batch sequences based on task type if task_type == 'multiple_choice': prompts = render_prompts_mc(item, continuation_delimiter, fewshot_examples) tokens, start_idxs, end_idxs = batch_sequences_mc(tokenizer, prompts) elif task_type == 'schema': prompts = render_prompts_schema(item, continuation_delimiter, fewshot_examples) tokens, start_idxs, end_idxs = batch_sequences_schema(tokenizer, prompts) elif task_type == 'language_modeling': prompts = render_prompts_lm(item, continuation_delimiter, fewshot_examples) tokens, start_idxs, end_idxs = batch_sequences_lm(tokenizer, prompts) else: raise ValueError(f"Unsupported task type: {task_type}") # Some models can't forward sequences beyond a certain length (e.g. GPT-2) # In these cases, we have to truncate sequences to max length and adjust the indices if hasattr(model, 'max_seq_len') and model.max_seq_len is not None: max_tokens = model.max_seq_len new_tokens, new_start_idxs, new_end_idxs = [], [], [] for t, s, e in zip(tokens, start_idxs, end_idxs): if len(t) > max_tokens: num_to_crop = len(t) - max_tokens new_tokens.append(t[-max_tokens:]) # take the last max_tokens tokens new_start_idxs.append(s - num_to_crop) # shift the indices down new_end_idxs.append(e - num_to_crop) assert s - num_to_crop >= 0, "this should never happen right?" assert e - num_to_crop >= 0, "this should never happen right?" else: new_tokens.append(t) # keep unchanged new_start_idxs.append(s) new_end_idxs.append(e) tokens, start_idxs, end_idxs = new_tokens, new_start_idxs, new_end_idxs # Stack up all the sequences into a batch pad_token_id = tokenizer.get_bos_token_id() # use BOS as pad token is ok input_ids = stack_sequences(tokens, pad_token_id) input_ids = input_ids.to(device) # Forward the model, get the autoregressive loss and argmax prediction at each token losses, predictions = forward_model(model, input_ids) # See if the losses/predictions come out correctly if task_type == 'language_modeling': # language modeling task is currently always batch size 1 si = start_idxs[0] ei = end_idxs[0] # predictions[i] predict input_ids[i+1] autoregressively predicted_tokens = predictions[0, si-1:ei-1] actual_tokens = input_ids[0, si:ei] is_correct = torch.all(predicted_tokens == actual_tokens).item() elif task_type in ['multiple_choice', 'schema']: # For MC/schema: find the option with lowest average loss mean_losses = [losses[i, si-1:ei-1].mean().item() for i, (si, ei) in enumerate(zip(start_idxs, end_idxs))] pred_idx = mean_losses.index(min(mean_losses)) is_correct = pred_idx == item['gold'] else: raise ValueError(f"Unsupported task type: {task_type}") return is_correct def evaluate_task(model, tokenizer, data, device, task_meta): """ This function is responsible for evaluating one task across many examples. It also handles dispatch to all processes if the script is run with torchrun. """ rank = dist.get_rank() if dist.is_initialized() else 0 world_size = dist.get_world_size() if dist.is_initialized() else 1 correct = torch.zeros(len(data), dtype=torch.float32, device=device) # stride the examples to each rank for idx in range(rank, len(data), world_size): is_correct = evaluate_example(idx, model, tokenizer, data, device, task_meta) correct[idx] = float(is_correct) # sync results across all the processes if running distributed if world_size > 1: dist.barrier() dist.all_reduce(correct, op=dist.ReduceOp.SUM) # compute the mean mean_correct = correct.mean().item() return mean_correct
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/core_eval.py", "license": "MIT License", "lines": 228, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/dataloader.py
""" Distributed dataloaders for pretraining. BOS-aligned bestfit: - Every row starts with BOS token - Documents packed using best-fit algorithm to minimize cropping - When no document fits remaining space, crops a document to fill exactly - 100% utilization (no padding), ~35% tokens cropped at T=2048 Compared to the original tokenizing_distributed_data_loader: BOS-aligned loses ~35% of tokens to cropping, but ensures that there are fewer "confusing" tokens in the train/val batches as every token can now attend back to the BOS token and sees the full context of the document. Fallback to the original if you have very limited data AND long documents: https://github.com/karpathy/nanochat/blob/3c3a3d7/nanochat/dataloader.py#L78-L117 """ import torch import pyarrow.parquet as pq from nanochat.common import get_dist_info from nanochat.dataset import list_parquet_files def _document_batches(split, resume_state_dict, tokenizer_batch_size): """ Infinite iterator over document batches (list of text strings) from parquet files. Handles DDP sharding and approximate resume. Each yield is (text_batch, (pq_idx, rg_idx, epoch)) where text_batch is a list of document strings, indices track position for resumption, and epoch counts how many times we've cycled through the dataset (starts at 1). """ ddp, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info() parquet_paths = list_parquet_files() assert len(parquet_paths) != 0, "No dataset parquet files found, did you run dataset.py?" parquet_paths = parquet_paths[:-1] if split == "train" else parquet_paths[-1:] resume_pq_idx = resume_state_dict["pq_idx"] if resume_state_dict is not None else 0 resume_rg_idx = resume_state_dict["rg_idx"] if resume_state_dict is not None else None resume_epoch = resume_state_dict.get("epoch", 1) if resume_state_dict is not None else 1 first_pass = True pq_idx = resume_pq_idx epoch = resume_epoch while True: # iterate infinitely (multi-epoch) pq_idx = resume_pq_idx if first_pass else 0 while pq_idx < len(parquet_paths): filepath = parquet_paths[pq_idx] pf = pq.ParquetFile(filepath) # Start from resume point if resuming on same file, otherwise from DDP rank if first_pass and (resume_rg_idx is not None) and (pq_idx == resume_pq_idx): base_idx = resume_rg_idx // ddp_world_size base_idx += 1 # advance by 1 so we don't repeat data after resuming rg_idx = base_idx * ddp_world_size + ddp_rank if rg_idx >= pf.num_row_groups: pq_idx += 1 continue resume_rg_idx = None # only do this once else: rg_idx = ddp_rank while rg_idx < pf.num_row_groups: rg = pf.read_row_group(rg_idx) batch = rg.column('text').to_pylist() for i in range(0, len(batch), tokenizer_batch_size): yield batch[i:i+tokenizer_batch_size], (pq_idx, rg_idx, epoch) rg_idx += ddp_world_size pq_idx += 1 first_pass = False epoch += 1 def tokenizing_distributed_data_loader_with_state_bos_bestfit( tokenizer, B, T, split, tokenizer_threads=4, tokenizer_batch_size=128, device="cuda", resume_state_dict=None, buffer_size=1000 ): """ BOS-aligned dataloader with Best-Fit Cropping. Reduces token waste compared to simple greedy cropping by searching a buffer for documents that fit well, while maintaining 100% utilization (no padding). Algorithm for each row: 1. From buffered docs, pick the LARGEST doc that fits entirely 2. Repeat until no doc fits 3. When nothing fits, crop a doc to fill remaining space exactly Key properties: - Every row starts with BOS - 100% utilization (no padding, every token is trained on) - Approximately 35% of all tokens are discarded due to cropping """ assert split in ["train", "val"], "split must be 'train' or 'val'" row_capacity = T + 1 batches = _document_batches(split, resume_state_dict, tokenizer_batch_size) bos_token = tokenizer.get_bos_token_id() doc_buffer = [] pq_idx, rg_idx, epoch = 0, 0, 1 def refill_buffer(): nonlocal pq_idx, rg_idx, epoch doc_batch, (pq_idx, rg_idx, epoch) = next(batches) token_lists = tokenizer.encode(doc_batch, prepend=bos_token, num_threads=tokenizer_threads) for tokens in token_lists: doc_buffer.append(tokens) # Pre-allocate buffers once: layout is [inputs (B*T) | targets (B*T)] # This gives us contiguous views and a single HtoD transfer use_cuda = device == "cuda" row_buffer = torch.empty((B, row_capacity), dtype=torch.long) # for building rows without creating Python lists cpu_buffer = torch.empty(2 * B * T, dtype=torch.long, pin_memory=use_cuda) # staging area (CPU) gpu_buffer = torch.empty(2 * B * T, dtype=torch.long, device=device) # on-device buffer cpu_inputs = cpu_buffer[:B * T].view(B, T) # a few views into these buffers just for convenience cpu_targets = cpu_buffer[B * T:].view(B, T) inputs = gpu_buffer[:B * T].view(B, T) targets = gpu_buffer[B * T:].view(B, T) while True: for row_idx in range(B): pos = 0 while pos < row_capacity: # Ensure buffer has documents while len(doc_buffer) < buffer_size: refill_buffer() remaining = row_capacity - pos # Find largest doc that fits entirely best_idx = -1 best_len = 0 for i, doc in enumerate(doc_buffer): doc_len = len(doc) if doc_len <= remaining and doc_len > best_len: best_idx = i best_len = doc_len if best_idx >= 0: doc = doc_buffer.pop(best_idx) doc_len = len(doc) row_buffer[row_idx, pos:pos + doc_len] = torch.tensor(doc, dtype=torch.long) pos += doc_len else: # No doc fits - crop shortest in buffer to fill remaining and minimize waste shortest_idx = min(range(len(doc_buffer)), key=lambda i: len(doc_buffer[i])) doc = doc_buffer.pop(shortest_idx) row_buffer[row_idx, pos:pos + remaining] = torch.tensor(doc[:remaining], dtype=torch.long) pos += remaining # Copy to pinned CPU buffer, then single HtoD transfer cpu_inputs.copy_(row_buffer[:, :-1]) cpu_targets.copy_(row_buffer[:, 1:]) state_dict = {"pq_idx": pq_idx, "rg_idx": rg_idx, "epoch": epoch} # Single HtoD copy into persistent GPU buffer and yield gpu_buffer.copy_(cpu_buffer, non_blocking=use_cuda) yield inputs, targets, state_dict def tokenizing_distributed_data_loader_bos_bestfit(*args, **kwargs): """Helper that omits state_dict from yields.""" for inputs, targets, state_dict in tokenizing_distributed_data_loader_with_state_bos_bestfit(*args, **kwargs): yield inputs, targets
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/dataloader.py", "license": "MIT License", "lines": 139, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/dataset.py
""" The base/pretraining dataset is a set of parquet files. This file contains utilities for: - iterating over the parquet files and yielding documents from it - download the files on demand if they are not on disk For details of how the dataset was prepared, see `repackage_data_reference.py`. """ import os import argparse import time import requests import pyarrow.parquet as pq from multiprocessing import Pool from nanochat.common import get_base_dir # ----------------------------------------------------------------------------- # The specifics of the current pretraining dataset # The URL on the internet where the data is hosted and downloaded from on demand BASE_URL = "https://huggingface.co/datasets/karpathy/fineweb-edu-100b-shuffle/resolve/main" MAX_SHARD = 1822 # the last datashard is shard_01822.parquet index_to_filename = lambda index: f"shard_{index:05d}.parquet" # format of the filenames base_dir = get_base_dir() DATA_DIR = os.path.join(base_dir, "base_data") os.makedirs(DATA_DIR, exist_ok=True) # ----------------------------------------------------------------------------- # These functions are useful utilities to other modules, can/should be imported def list_parquet_files(data_dir=None): """ Looks into a data dir and returns full paths to all parquet files. """ data_dir = DATA_DIR if data_dir is None else data_dir parquet_files = sorted([ f for f in os.listdir(data_dir) if f.endswith('.parquet') and not f.endswith('.tmp') ]) parquet_paths = [os.path.join(data_dir, f) for f in parquet_files] return parquet_paths def parquets_iter_batched(split, start=0, step=1): """ Iterate through the dataset, in batches of underlying row_groups for efficiency. - split can be "train" or "val". the last parquet file will be val. - start/step are useful for skipping rows in DDP. e.g. start=rank, step=world_size """ assert split in ["train", "val"], "split must be 'train' or 'val'" parquet_paths = list_parquet_files() parquet_paths = parquet_paths[:-1] if split == "train" else parquet_paths[-1:] for filepath in parquet_paths: pf = pq.ParquetFile(filepath) for rg_idx in range(start, pf.num_row_groups, step): rg = pf.read_row_group(rg_idx) texts = rg.column('text').to_pylist() yield texts # ----------------------------------------------------------------------------- def download_single_file(index): """ Downloads a single file index, with some backoff """ # Construct the local filepath for this file and skip if it already exists filename = index_to_filename(index) filepath = os.path.join(DATA_DIR, filename) if os.path.exists(filepath): print(f"Skipping {filepath} (already exists)") return True # Construct the remote URL for this file url = f"{BASE_URL}/{filename}" print(f"Downloading {filename}...") # Download with retries max_attempts = 5 for attempt in range(1, max_attempts + 1): try: response = requests.get(url, stream=True, timeout=30) response.raise_for_status() # Write to temporary file first temp_path = filepath + f".tmp" with open(temp_path, 'wb') as f: for chunk in response.iter_content(chunk_size=1024 * 1024): # 1MB chunks if chunk: f.write(chunk) # Move temp file to final location os.rename(temp_path, filepath) print(f"Successfully downloaded {filename}") return True except (requests.RequestException, IOError) as e: print(f"Attempt {attempt}/{max_attempts} failed for {filename}: {e}") # Clean up any partial files for path in [filepath + f".tmp", filepath]: if os.path.exists(path): try: os.remove(path) except: pass # Try a few times with exponential backoff: 2^attempt seconds if attempt < max_attempts: wait_time = 2 ** attempt print(f"Waiting {wait_time} seconds before retry...") time.sleep(wait_time) else: print(f"Failed to download {filename} after {max_attempts} attempts") return False return False if __name__ == "__main__": parser = argparse.ArgumentParser(description="Download FineWeb-Edu 100BT dataset shards") parser.add_argument("-n", "--num-files", type=int, default=-1, help="Number of shards to download (default: -1), -1 = disable") parser.add_argument("-w", "--num-workers", type=int, default=4, help="Number of parallel download workers (default: 4)") args = parser.parse_args() num = MAX_SHARD + 1 if args.num_files == -1 else min(args.num_files, MAX_SHARD + 1) ids_to_download = list(range(num)) print(f"Downloading {len(ids_to_download)} shards using {args.num_workers} workers...") print(f"Target directory: {DATA_DIR}") print() with Pool(processes=args.num_workers) as pool: results = pool.map(download_single_file, ids_to_download) # Report results successful = sum(1 for success in results if success) print(f"Done! Downloaded: {successful}/{len(ids_to_download)} shards to {DATA_DIR}")
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/dataset.py", "license": "MIT License", "lines": 110, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/engine.py
""" Engine for efficient inference of our models. Everything works around token sequences: - The user can send token sequences to the engine - The engine returns the next token Notes: - The engine knows nothing about tokenization, it's purely token id sequences. The whole thing is made as efficient as possible. """ import torch import torch.nn.functional as F import signal import warnings from contextlib import contextmanager from collections import deque from nanochat.common import compute_init, autodetect_device_type from nanochat.checkpoint_manager import load_model from contextlib import nullcontext # ----------------------------------------------------------------------------- # Calculator tool helpers @contextmanager def timeout(duration, formula): def timeout_handler(signum, frame): raise Exception(f"'{formula}': timed out after {duration} seconds") signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(duration) yield signal.alarm(0) def eval_with_timeout(formula, max_time=3): try: with timeout(max_time, formula): with warnings.catch_warnings(): warnings.simplefilter("ignore", SyntaxWarning) return eval(formula, {"__builtins__": {}}, {}) except Exception as e: signal.alarm(0) # print(f"Warning: Failed to eval {formula}, exception: {e}") # it's ok ignore wrong calculator usage return None def use_calculator(expr): """ Evaluate a Python expression safely. Supports both math expressions and string operations like .count() """ # Remove commas from numbers expr = expr.replace(",", "") # Check if it's a pure math expression (old behavior) if all([x in "0123456789*+-/.() " for x in expr]): if "**" in expr: # disallow power operator return None return eval_with_timeout(expr) # Check if it's a string operation we support # Allow: strings (single/double quotes), .count(), letters, numbers, spaces, parens allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'\"()._ " if not all([x in allowed_chars for x in expr]): return None # Disallow dangerous patterns dangerous_patterns = ['__', 'import', 'exec', 'eval', 'compile', 'open', 'file', 'input', 'raw_input', 'globals', 'locals', 'vars', 'dir', 'getattr', 'setattr', 'delattr', 'hasattr'] expr_lower = expr.lower() if any(pattern in expr_lower for pattern in dangerous_patterns): return None # Only allow .count() method for now (can expand later) if '.count(' not in expr: return None # Evaluate with timeout return eval_with_timeout(expr) # ----------------------------------------------------------------------------- class KVCache: """ KV Cache designed for Flash Attention 3's flash_attn_with_kvcache API. Key differences from FA2-style cache: - Tensors are (B, T, H, D) not (B, H, T, D) - FA3 updates the cache in-place during flash_attn_with_kvcache - Position tracked per batch element via cache_seqlens tensor """ def __init__(self, batch_size, num_heads, seq_len, head_dim, num_layers, device, dtype): self.batch_size = batch_size self.max_seq_len = seq_len self.n_layers = num_layers self.n_heads = num_heads self.head_dim = head_dim # Pre-allocate cache tensors: (n_layers, B, T, H, D) self.k_cache = torch.zeros(num_layers, batch_size, seq_len, num_heads, head_dim, device=device, dtype=dtype) self.v_cache = torch.zeros(num_layers, batch_size, seq_len, num_heads, head_dim, device=device, dtype=dtype) # Current sequence length per batch element (FA3 needs int32) self.cache_seqlens = torch.zeros(batch_size, dtype=torch.int32, device=device) def reset(self): """Reset cache to empty state.""" self.cache_seqlens.zero_() def get_pos(self): """Get current position (assumes all batch elements at same position).""" return self.cache_seqlens[0].item() def get_layer_cache(self, layer_idx): """Return (k_cache, v_cache) views for a specific layer.""" return self.k_cache[layer_idx], self.v_cache[layer_idx] def advance(self, num_tokens): """Advance the cache position by num_tokens.""" self.cache_seqlens += num_tokens def prefill(self, other): """ Copy cached KV from another cache into this one. Used when we do batch=1 prefill and then want to generate multiple samples in parallel. """ assert self.get_pos() == 0, "Cannot prefill a non-empty KV cache" assert self.n_layers == other.n_layers and self.n_heads == other.n_heads and self.head_dim == other.head_dim assert self.max_seq_len >= other.max_seq_len other_pos = other.get_pos() self.k_cache[:, :, :other_pos, :, :] = other.k_cache[:, :, :other_pos, :, :] self.v_cache[:, :, :other_pos, :, :] = other.v_cache[:, :, :other_pos, :, :] self.cache_seqlens.fill_(other_pos) # ----------------------------------------------------------------------------- @torch.inference_mode() def sample_next_token(logits, rng, temperature=1.0, top_k=None): """Sample a single next token from given logits of shape (B, vocab_size). Returns (B, 1).""" assert temperature >= 0.0, "temperature must be non-negative" if temperature == 0.0: return torch.argmax(logits, dim=-1, keepdim=True) if top_k is not None and top_k > 0: k = min(top_k, logits.size(-1)) vals, idx = torch.topk(logits, k, dim=-1) vals = vals / temperature probs = F.softmax(vals, dim=-1) choice = torch.multinomial(probs, num_samples=1, generator=rng) return idx.gather(1, choice) else: logits = logits / temperature probs = F.softmax(logits, dim=-1) return torch.multinomial(probs, num_samples=1, generator=rng) # ----------------------------------------------------------------------------- class RowState: # Per-row state tracking during generation def __init__(self, current_tokens=None): self.current_tokens = current_tokens or [] # Current token sequence for this row self.forced_tokens = deque() # Queue of tokens to force inject self.in_python_block = False # Whether we are inside a python block self.python_expr_tokens = [] # Tokens of the current python expression self.completed = False # Whether this row has completed generation class Engine: def __init__(self, model, tokenizer): self.model = model self.tokenizer = tokenizer # needed for tool use @torch.inference_mode() def generate(self, tokens, num_samples=1, max_tokens=None, temperature=1.0, top_k=None, seed=42): """Same as generate, but does single prefill and then clones the KV cache.""" assert isinstance(tokens, list) and isinstance(tokens[0], int), "expecting list of ints" device = self.model.get_device() # NOTE: setting the dtype here and in this way is an ugly hack. # Currently the repo assumes that cuda -> bfloat16 and everything else -> float32. # We need to know the dtype here to call __init__ on KVCache and pre-allocate its tensors. # As a quick hack, we're making generate() function inherit and know about this repo-wise assumption. # I think there has to be a bigger refactor to deal with device/dtype tracking across the codebase. # In particular, the KVCache should allocate its tensors lazily dtype = torch.bfloat16 if device.type == "cuda" else torch.float32 rng = torch.Generator(device=device) rng.manual_seed(seed) # Get the special tokens we need to coordinate the tool use state machine get_special = lambda s: self.tokenizer.encode_special(s) python_start = get_special("<|python_start|>") python_end = get_special("<|python_end|>") output_start = get_special("<|output_start|>") output_end = get_special("<|output_end|>") assistant_end = get_special("<|assistant_end|>") # if sampled, ends row bos = self.tokenizer.get_bos_token_id() # if sampled, ends row # 1) Run a batch 1 prefill of the prompt tokens m = self.model.config kv_model_kwargs = {"num_heads": m.n_kv_head, "head_dim": m.n_embd // m.n_head, "num_layers": m.n_layer} kv_cache_prefill = KVCache( batch_size=1, seq_len=len(tokens), device=device, dtype=dtype, **kv_model_kwargs, ) ids = torch.tensor([tokens], dtype=torch.long, device=device) logits = self.model.forward(ids, kv_cache=kv_cache_prefill) logits = logits[:, -1, :].expand(num_samples, -1) # (num_samples, vocab_size) # 2) Replicate the KV cache for each sample/row kv_length_hint = (len(tokens) + max_tokens) if max_tokens is not None else self.model.config.sequence_len kv_cache_decode = KVCache( batch_size=num_samples, seq_len=kv_length_hint, device=device, dtype=dtype, **kv_model_kwargs, ) kv_cache_decode.prefill(kv_cache_prefill) del kv_cache_prefill # no need to keep this memory around # 3) Initialize states for each sample row_states = [RowState(tokens.copy()) for _ in range(num_samples)] # 4) Main generation loop num_generated = 0 while True: # Stop condition: we've reached max tokens if max_tokens is not None and num_generated >= max_tokens: break # Stop condition: all rows are completed if all(state.completed for state in row_states): break # Sample the next token for each row next_ids = sample_next_token(logits, rng, temperature, top_k) # (B, 1) sampled_tokens = next_ids[:, 0].tolist() # Process each row: choose the next token, update state, optional tool use token_column = [] # contains the next token id along each row token_masks = [] # contains the mask (was it sampled (1) or forced (0)?) along each row for i, state in enumerate(row_states): # Select the next token in this row is_forced = len(state.forced_tokens) > 0 # are there tokens waiting to be forced in deque? token_masks.append(0 if is_forced else 1) # mask is 0 if forced, 1 if sampled next_token = state.forced_tokens.popleft() if is_forced else sampled_tokens[i] token_column.append(next_token) # Update the state of this row to include the next token state.current_tokens.append(next_token) # On <|assistant_end|> or <|bos|>, mark the row as completed if next_token == assistant_end or next_token == bos: state.completed = True # Handle tool logic if next_token == python_start: state.in_python_block = True state.python_expr_tokens = [] elif next_token == python_end and state.in_python_block: state.in_python_block = False if state.python_expr_tokens: expr = self.tokenizer.decode(state.python_expr_tokens) result = use_calculator(expr) if result is not None: result_tokens = self.tokenizer.encode(str(result)) state.forced_tokens.append(output_start) state.forced_tokens.extend(result_tokens) state.forced_tokens.append(output_end) state.python_expr_tokens = [] elif state.in_python_block: state.python_expr_tokens.append(next_token) # Yield the token column yield token_column, token_masks num_generated += 1 # Prepare logits for next iteration ids = torch.tensor(token_column, dtype=torch.long, device=device).unsqueeze(1) logits = self.model.forward(ids, kv_cache=kv_cache_decode)[:, -1, :] # (B, vocab_size) def generate_batch(self, tokens, num_samples=1, **kwargs): """ Non-streaming batch generation that just returns the final token sequences. Returns a list of token sequences (list of lists of ints). Terminal tokens (assistant_end, bos) are not included in the results. """ assistant_end = self.tokenizer.encode_special("<|assistant_end|>") bos = self.tokenizer.get_bos_token_id() results = [tokens.copy() for _ in range(num_samples)] masks = [[0] * len(tokens) for _ in range(num_samples)] completed = [False] * num_samples for token_column, token_masks in self.generate(tokens, num_samples, **kwargs): for i, (token, mask) in enumerate(zip(token_column, token_masks)): if not completed[i]: if token == assistant_end or token == bos: completed[i] = True else: results[i].append(token) masks[i].append(mask) # Stop if all rows are completed if all(completed): break return results, masks if __name__ == "__main__": """ Quick inline test to make sure that the naive/slow model.generate function is equivalent to the faster Engine.generate function here. """ import time # init compute device_type = autodetect_device_type() ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=torch.bfloat16) if device_type == "cuda" else nullcontext() # load the model and tokenizer model, tokenizer, meta = load_model("base", device, phase="eval") bos_token_id = tokenizer.get_bos_token_id() # common hyperparameters kwargs = dict(max_tokens=64, temperature=0.0) # set the starting prompt prompt_tokens = tokenizer.encode("The chemical formula of water is", prepend=bos_token_id) # generate the reference sequence using the model.generate() function generated_tokens = [] torch.cuda.synchronize() t0 = time.time() stream = model.generate(prompt_tokens, **kwargs) with autocast_ctx: for token in stream: generated_tokens.append(token) chunk = tokenizer.decode([token]) print(chunk, end="", flush=True) print() torch.cuda.synchronize() t1 = time.time() print(f"Reference time: {t1 - t0:.2f}s") reference_ids = generated_tokens # generate tokens with Engine generated_tokens = [] engine = Engine(model, tokenizer) stream = engine.generate(prompt_tokens, num_samples=1, **kwargs) # note: runs in fp32 torch.cuda.synchronize() t0 = time.time() with autocast_ctx: for token_column, token_masks in stream: token = token_column[0] # only print out the first row generated_tokens.append(token) chunk = tokenizer.decode([token]) print(chunk, end="", flush=True) print() torch.cuda.synchronize() t1 = time.time() print(f"Engine time: {t1 - t0:.2f}s") # compare the two sequences for i in range(len(reference_ids)): if reference_ids[i] != generated_tokens[i]: print(f"Mismatch at {i}: {reference_ids[i]} != {generated_tokens[i]}") break print(f"Match: {reference_ids == generated_tokens}")
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/engine.py", "license": "MIT License", "lines": 316, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/execution.py
""" Sandboxed execution utilities for running Python code that comes out of an LLM. Adapted from OpenAI HumanEval code: https://github.com/openai/human-eval/blob/master/human_eval/execution.py What is covered: - Each execution runs in its own process (can be killed if it hangs or crashes) - Execution is limited by a timeout to stop infinite loops - Memory limits are enforced by default (256MB) - stdout and stderr are captured and returned - Code runs in a temporary directory that is deleted afterwards - Dangerous functions are disabled (examples: os.system, os.kill, shutil.rmtree, subprocess.Popen) What is not covered: - Not a true security sandbox - Network access is not blocked (e.g. sockets could be opened) - Python's dynamic features (e.g. ctypes) could bypass restrictions - No kernel-level isolation (no seccomp, no containers, no virtualization) Overall this sandbox is good for evaluation of generated code and protects against accidental destructive behavior, but it is not safe against malicious adversarial code. """ import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile from dataclasses import dataclass from typing import Optional # ----------------------------------------------------------------------------- @dataclass class ExecutionResult: """Result of executing Python code in a sandbox.""" success: bool stdout: str stderr: str error: Optional[str] = None timeout: bool = False memory_exceeded: bool = False def __repr__(self): parts = [] parts.append(f"ExecutionResult(success={self.success}") if self.timeout: parts.append(", timeout=True") if self.memory_exceeded: parts.append(", memory_exceeded=True") if self.error: parts.append(f", error={self.error!r}") if self.stdout: parts.append(f", stdout={self.stdout!r}") if self.stderr: parts.append(f", stderr={self.stderr!r}") parts.append(")") return "".join(parts) @contextlib.contextmanager def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0) @contextlib.contextmanager def capture_io(): """Capture stdout and stderr, and disable stdin.""" stdout_capture = io.StringIO() stderr_capture = io.StringIO() stdin_block = WriteOnlyStringIO() with contextlib.redirect_stdout(stdout_capture): with contextlib.redirect_stderr(stderr_capture): with redirect_stdin(stdin_block): yield stdout_capture, stderr_capture @contextlib.contextmanager def create_tempdir(): with tempfile.TemporaryDirectory() as dirname: with chdir(dirname): yield dirname class TimeoutException(Exception): pass class WriteOnlyStringIO(io.StringIO): """StringIO that throws an exception when it's read from""" def read(self, *args, **kwargs): raise IOError def readline(self, *args, **kwargs): raise IOError def readlines(self, *args, **kwargs): raise IOError def readable(self, *args, **kwargs): """Returns True if the IO object can be read.""" return False class redirect_stdin(contextlib._RedirectStream): # type: ignore _stream = "stdin" @contextlib.contextmanager def chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield finally: os.chdir(cwd) def reliability_guard(maximum_memory_bytes: Optional[int] = None): """ This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. """ if platform.uname().system != "Darwin": # These resource limit calls seem to fail on macOS (Darwin), skip? import resource resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes)) resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes)) resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes)) faulthandler.disable() import builtins builtins.exit = None builtins.quit = None import os os.environ["OMP_NUM_THREADS"] = "1" os.kill = None os.system = None os.putenv = None os.remove = None os.removedirs = None os.rmdir = None os.fchdir = None os.setuid = None os.fork = None os.forkpty = None os.killpg = None os.rename = None os.renames = None os.truncate = None os.replace = None os.unlink = None os.fchmod = None os.fchown = None os.chmod = None os.chown = None os.chroot = None os.fchdir = None os.lchflags = None os.lchmod = None os.lchown = None os.getcwd = None os.chdir = None import shutil shutil.rmtree = None shutil.move = None shutil.chown = None import subprocess subprocess.Popen = None # type: ignore __builtins__["help"] = None import sys sys.modules["ipdb"] = None sys.modules["joblib"] = None sys.modules["resource"] = None sys.modules["psutil"] = None sys.modules["tkinter"] = None def _unsafe_execute(code: str, timeout: float, maximum_memory_bytes: Optional[int], result_dict): """Execute code in a subprocess with safety guards. Results are written to result_dict.""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir unlink = os.unlink # Disable functionalities that can make destructive changes to the test. reliability_guard(maximum_memory_bytes=maximum_memory_bytes) # Default to failure result_dict.update({ "success": False, "stdout": "", "stderr": "", "timeout": False, "memory_exceeded": False, "error": None, }) try: exec_globals = {} with capture_io() as (stdout_capture, stderr_capture): with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. For more # information on how OpenAI sandboxes its code, see the accompanying paper. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec(code, exec_globals) result_dict.update({ "success": True, "stdout": stdout_capture.getvalue(), "stderr": stderr_capture.getvalue(), }) except TimeoutException: result_dict.update({ "timeout": True, "error": "Execution timed out", }) except MemoryError as e: result_dict.update({ "memory_exceeded": True, "error": f"Memory limit exceeded: {e}", }) except BaseException as e: result_dict.update({ "error": f"{type(e).__name__}: {e}", }) # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir os.unlink = unlink def execute_code( code: str, timeout: float = 5.0, # 5 seconds default maximum_memory_bytes: Optional[int] = 256 * 1024 * 1024, # 256MB default ) -> ExecutionResult: """ Execute Python code in a sandboxed environment. Args: code: Python code to execute as a string timeout: Maximum execution time in seconds (default: 5.0) maximum_memory_bytes: Memory limit in bytes (default: 256MB, None to disable) Returns: ExecutionResult with success status, stdout/stderr, and error information Example: >>> result = execute_code("print('hello world')") >>> result.success True >>> result.stdout 'hello world\\n' """ manager = multiprocessing.Manager() result_dict = manager.dict() p = multiprocessing.Process( target=_unsafe_execute, args=(code, timeout, maximum_memory_bytes, result_dict) ) p.start() p.join(timeout=timeout + 1) if p.is_alive(): p.kill() return ExecutionResult( success=False, stdout="", stderr="", error="Execution timed out (process killed)", timeout=True, memory_exceeded=False, ) if not result_dict: return ExecutionResult( success=False, stdout="", stderr="", error="Execution failed (no result returned)", timeout=True, memory_exceeded=False, ) return ExecutionResult( success=result_dict["success"], stdout=result_dict["stdout"], stderr=result_dict["stderr"], error=result_dict["error"], timeout=result_dict["timeout"], memory_exceeded=result_dict["memory_exceeded"], )
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/execution.py", "license": "MIT License", "lines": 283, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/gpt.py
""" GPT model (rewrite, a lot simpler) Notable features: - rotary embeddings (and no positional embeddings) - QK norm - untied weights for token embedding and lm_head - relu^2 activation in MLP - norm after token embedding - no learnable params in rmsnorm - no bias in linear layers - Group-Query Attention (GQA) support for more efficient inference - Flash Attention 3 integration """ from functools import partial from dataclasses import dataclass import torch import torch.nn as nn import torch.nn.functional as F from nanochat.common import get_dist_info, print0 from nanochat.optim import MuonAdamW, DistMuonAdamW # Our custom Flash Attention module that automatically uses FA3 on Hopper+ and SDPA fallback elsewhere from nanochat.flash_attention import flash_attn @dataclass class GPTConfig: sequence_len: int = 2048 vocab_size: int = 32768 n_layer: int = 12 n_head: int = 6 # number of query heads n_kv_head: int = 6 # number of key/value heads (GQA) n_embd: int = 768 # Sliding window attention pattern string, tiled across layers. Final layer always L. # Characters: L=long (full context), S=short (half context) # Examples: "L"=all full context, "SL"=alternating, "SSL"=two short then one long window_pattern: str = "SSSL" def norm(x): # Purely functional rmsnorm with no learnable params return F.rms_norm(x, (x.size(-1),)) def has_ve(layer_idx, n_layer): """Returns True if GPT layer should have Value Embedding (alternating, last layer always included).""" return layer_idx % 2 == (n_layer - 1) % 2 def apply_rotary_emb(x, cos, sin): assert x.ndim == 4 # multihead attention d = x.shape[3] // 2 x1, x2 = x[..., :d], x[..., d:] # split up last dim into two halves y1 = x1 * cos + x2 * sin # rotate pairs of dims y2 = x1 * (-sin) + x2 * cos return torch.cat([y1, y2], 3) class CausalSelfAttention(nn.Module): def __init__(self, config, layer_idx): super().__init__() self.layer_idx = layer_idx self.n_head = config.n_head self.n_kv_head = config.n_kv_head self.n_embd = config.n_embd self.head_dim = self.n_embd // self.n_head assert self.n_embd % self.n_head == 0 assert self.n_kv_head <= self.n_head and self.n_head % self.n_kv_head == 0 self.c_q = nn.Linear(self.n_embd, self.n_head * self.head_dim, bias=False) self.c_k = nn.Linear(self.n_embd, self.n_kv_head * self.head_dim, bias=False) self.c_v = nn.Linear(self.n_embd, self.n_kv_head * self.head_dim, bias=False) self.c_proj = nn.Linear(self.n_embd, self.n_embd, bias=False) self.ve_gate_channels = 32 self.ve_gate = nn.Linear(self.ve_gate_channels, self.n_kv_head, bias=False) if has_ve(layer_idx, config.n_layer) else None def forward(self, x, ve, cos_sin, window_size, kv_cache): B, T, C = x.size() # Project the input to get queries, keys, and values # Shape: (B, T, H, D) - FA3's native layout, no transpose needed! q = self.c_q(x).view(B, T, self.n_head, self.head_dim) k = self.c_k(x).view(B, T, self.n_kv_head, self.head_dim) v = self.c_v(x).view(B, T, self.n_kv_head, self.head_dim) # Value residual (ResFormer): mix in value embedding with input-dependent gate per head if ve is not None: ve = ve.view(B, T, self.n_kv_head, self.head_dim) gate = 2 * torch.sigmoid(self.ve_gate(x[..., :self.ve_gate_channels])) # (B, T, n_kv_head), range (0, 2) v = v + gate.unsqueeze(-1) * ve # Apply Rotary Embeddings to queries and keys to get relative positional encoding cos, sin = cos_sin q, k = apply_rotary_emb(q, cos, sin), apply_rotary_emb(k, cos, sin) q, k = norm(q), norm(k) # QK norm # Flash Attention (FA3 on Hopper+, PyTorch SDPA fallback elsewhere) # window_size is (left, right) tuple: (N, 0) for causal, (-1, 0) for full context if kv_cache is None: # Training: causal attention with optional sliding window y = flash_attn.flash_attn_func(q, k, v, causal=True, window_size=window_size) else: # Inference: use flash_attn_with_kvcache which handles cache management k_cache, v_cache = kv_cache.get_layer_cache(self.layer_idx) y = flash_attn.flash_attn_with_kvcache( q, k_cache, v_cache, k=k, v=v, cache_seqlens=kv_cache.cache_seqlens, causal=True, window_size=window_size, ) # Advance position after last layer processes if self.layer_idx == kv_cache.n_layers - 1: kv_cache.advance(T) # Re-assemble the heads and project back to residual stream y = y.contiguous().view(B, T, -1) y = self.c_proj(y) return y class MLP(nn.Module): def __init__(self, config): super().__init__() self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd, bias=False) self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd, bias=False) def forward(self, x): x = self.c_fc(x) x = F.relu(x).square() x = self.c_proj(x) return x class Block(nn.Module): def __init__(self, config, layer_idx): super().__init__() self.attn = CausalSelfAttention(config, layer_idx) self.mlp = MLP(config) def forward(self, x, ve, cos_sin, window_size, kv_cache): x = x + self.attn(norm(x), ve, cos_sin, window_size, kv_cache) x = x + self.mlp(norm(x)) return x class GPT(nn.Module): def __init__(self, config, pad_vocab_size_to=64): """ NOTE a major footgun: this __init__ function runs in meta device context (!!) Therefore, any calculations inside here are shapes and dtypes only, no actual data. => We actually initialize all data (parameters, buffers, etc.) in init_weights() instead. """ super().__init__() self.config = config # Compute per-layer window sizes for sliding window attention # window_size is (left, right) tuple: (-1, 0) for full context, (N, 0) for sliding window self.window_sizes = self._compute_window_sizes(config) # Pad vocab for efficiency (DDP, tensor cores). This is just an optimization - outputs are cropped in forward(). # https://huggingface.co/docs/transformers/main_classes/model#transformers.PreTrainedModel.resize_token_embeddings padded_vocab_size = ((config.vocab_size + pad_vocab_size_to - 1) // pad_vocab_size_to) * pad_vocab_size_to if padded_vocab_size != config.vocab_size: print0(f"Padding vocab_size from {config.vocab_size} to {padded_vocab_size} for efficiency") self.transformer = nn.ModuleDict({ "wte": nn.Embedding(padded_vocab_size, config.n_embd), "h": nn.ModuleList([Block(config, layer_idx) for layer_idx in range(config.n_layer)]), }) self.lm_head = nn.Linear(config.n_embd, padded_vocab_size, bias=False) # Per-layer learnable scalars (inspired by modded-nanogpt) # resid_lambdas: scales the residual stream at each layer (init 1.0 = neutral) # x0_lambdas: blends initial embedding back in at each layer (init 0.0 = disabled) # Separate parameters so they can have different optimizer treatment self.resid_lambdas = nn.Parameter(torch.ones(config.n_layer)) # fake init, real init in init_weights() self.x0_lambdas = nn.Parameter(torch.zeros(config.n_layer)) # fake init, real init in init_weights() # Value embeddings (ResFormer-style): alternating layers, last layer always included head_dim = config.n_embd // config.n_head kv_dim = config.n_kv_head * head_dim self.value_embeds = nn.ModuleDict({str(i): nn.Embedding(padded_vocab_size, kv_dim) for i in range(config.n_layer) if has_ve(i, config.n_layer)}) # To support meta device initialization, we init the rotary embeddings here, but it's just "fake" meta tensors only. # As for rotary_seq_len, these rotary embeddings are pretty small/cheap in memory, # so let's just over-compute them by 10X, but assert fail if we ever reach that amount. # In the future we can dynamically grow the cache, for now it's fine. self.rotary_seq_len = config.sequence_len * 10 # 10X over-compute should be enough, TODO make nicer? head_dim = config.n_embd // config.n_head cos, sin = self._precompute_rotary_embeddings(self.rotary_seq_len, head_dim) self.register_buffer("cos", cos, persistent=False) # persistent=False means it's not saved to the checkpoint self.register_buffer("sin", sin, persistent=False) @torch.no_grad() def init_weights(self): """ Initialize the full model in this one function for maximum clarity. wte (embedding): normal, std=1.0 lm_head: normal, std=0.001 for each block: attn.c_q: uniform, std=1/sqrt(n_embd) attn.c_k: uniform, std=1/sqrt(n_embd) attn.c_v: uniform, std=1/sqrt(n_embd) attn.c_proj: zeros mlp.c_fc: uniform, std=1/sqrt(n_embd) mlp.c_proj: zeros """ # Embedding and unembedding torch.nn.init.normal_(self.transformer.wte.weight, mean=0.0, std=1.0) torch.nn.init.normal_(self.lm_head.weight, mean=0.0, std=0.001) # Transformer blocks: uniform init with bound = sqrt(3) * std (same standard deviation as normal) n_embd = self.config.n_embd s = 3**0.5 * n_embd**-0.5 # sqrt(3) multiplier makes sure Uniform achieves the same std as Normal for block in self.transformer.h: torch.nn.init.uniform_(block.attn.c_q.weight, -s, s) # weights use Uniform to avoid outliers torch.nn.init.uniform_(block.attn.c_k.weight, -s, s) torch.nn.init.uniform_(block.attn.c_v.weight, -s, s) torch.nn.init.zeros_(block.attn.c_proj.weight) # projections are zero torch.nn.init.uniform_(block.mlp.c_fc.weight, -s, s) torch.nn.init.zeros_(block.mlp.c_proj.weight) # Per-layer scalars self.resid_lambdas.fill_(1.0) # 1.0 => typical residual connections at init self.x0_lambdas.fill_(0.1) # 0.1 => small initial weight for skip connection to input embedding # Value embeddings (init like c_v: uniform with same std) for ve in self.value_embeds.values(): torch.nn.init.uniform_(ve.weight, -s, s) # Gate weights init to zero so gates start at sigmoid(0) = 0.5, scaled by 2 -> 1.0 (neutral) for block in self.transformer.h: if block.attn.ve_gate is not None: torch.nn.init.zeros_(block.attn.ve_gate.weight) # Rotary embeddings head_dim = self.config.n_embd // self.config.n_head cos, sin = self._precompute_rotary_embeddings(self.rotary_seq_len, head_dim) self.cos, self.sin = cos, sin # Cast embeddings to bf16: optimizer can tolerate it and it saves memory if self.transformer.wte.weight.device.type == "cuda": self.transformer.wte.to(dtype=torch.bfloat16) for ve in self.value_embeds.values(): ve.to(dtype=torch.bfloat16) def _precompute_rotary_embeddings(self, seq_len, head_dim, base=10000, device=None): # TODO: bump base theta more? e.g. 100K is more common more recently # autodetect the device from model embeddings if device is None: device = self.transformer.wte.weight.device # stride the channels channel_range = torch.arange(0, head_dim, 2, dtype=torch.float32, device=device) inv_freq = 1.0 / (base ** (channel_range / head_dim)) # stride the time steps t = torch.arange(seq_len, dtype=torch.float32, device=device) # calculate the rotation frequencies at each (time, channel) pair freqs = torch.outer(t, inv_freq) cos, sin = freqs.cos(), freqs.sin() cos, sin = cos.bfloat16(), sin.bfloat16() # keep them in bfloat16 cos, sin = cos[None, :, None, :], sin[None, :, None, :] # add batch and head dims for later broadcasting return cos, sin def _compute_window_sizes(self, config): """ Compute per-layer window sizes for sliding window attention. Returns list of (left, right) tuples for FA3's window_size parameter: - left: how many tokens before current position to attend to (-1 = unlimited) - right: how many tokens after current position to attend to (0 for causal) Pattern string is tiled across layers. Final layer always gets L (full context). Characters: L=long (full context), S=short (half context) """ pattern = config.window_pattern.upper() assert all(c in "SL" for c in pattern), f"Invalid window_pattern: {pattern}. Use only S and L." # Map characters to window sizes long_window = config.sequence_len short_window = long_window // 2 char_to_window = { "L": (long_window, 0), "S": (short_window, 0), } # Tile pattern across layers window_sizes = [] for layer_idx in range(config.n_layer): char = pattern[layer_idx % len(pattern)] window_sizes.append(char_to_window[char]) # Final layer always gets full context window_sizes[-1] = (long_window, 0) return window_sizes def get_device(self): return self.transformer.wte.weight.device def estimate_flops(self): """ Return the estimated FLOPs per token for the model (forward + backward). Each matmul weight parameter contributes 2 FLOPs (multiply *, accumulate +) in forward, and 2X that in backward => 2+4=6. Cleanest explanation of this: https://medium.com/@dzmitrybahdanau/the-flops-calculus-of-language-model-training-3b19c1f025e4 On top of that, 12 * h * q * effective_seq_len accounts for key @ query matmul flops inside attention. With sliding windows, effective_seq_len varies per layer (capped by window size). Ref: https://arxiv.org/abs/2204.02311 (PaLM paper). This is ~1% off from the exact formulas of Chinchilla paper, the difference is: - Chinchilla counts the embedding layer as flops (? weird, it's just a lookup => we ignore) - Chinchilla counts exp/sum/divide in attention softmax as flops (a little sus and very tiny => we ignore) """ nparams = sum(p.numel() for p in self.parameters()) # Exclude non-matmul params: embeddings and per-layer scalars value_embeds_numel = sum(ve.weight.numel() for ve in self.value_embeds.values()) nparams_exclude = (self.transformer.wte.weight.numel() + value_embeds_numel + self.resid_lambdas.numel() + self.x0_lambdas.numel()) h, q, t = self.config.n_head, self.config.n_embd // self.config.n_head, self.config.sequence_len # Sum attention FLOPs per layer, accounting for sliding window attn_flops = 0 for window_size in self.window_sizes: window = window_size[0] # (left, right) tuple, we use left effective_seq = t if window < 0 else min(window, t) attn_flops += 12 * h * q * effective_seq num_flops_per_token = 6 * (nparams - nparams_exclude) + attn_flops return num_flops_per_token def num_scaling_params(self): """ Return detailed parameter counts for scaling law analysis. Different papers use different conventions: - Kaplan et al. excluded embedding parameters - Chinchilla included all parameters Ref: https://arxiv.org/abs/2203.15556 (Chinchilla paper) Ref: https://arxiv.org/abs/2001.08361 (Kaplan et al. original scaling laws paper) Returns a dict with counts for each parameter group, so downstream analysis can experiment with which combination gives the cleanest scaling laws. """ # Count each group separately (mirrors the grouping in setup_optimizers) wte = sum(p.numel() for p in self.transformer.wte.parameters()) value_embeds = sum(p.numel() for p in self.value_embeds.parameters()) lm_head = sum(p.numel() for p in self.lm_head.parameters()) transformer_matrices = sum(p.numel() for p in self.transformer.h.parameters()) scalars = self.resid_lambdas.numel() + self.x0_lambdas.numel() total = wte + value_embeds + lm_head + transformer_matrices + scalars assert total == sum(p.numel() for p in self.parameters()), "Parameter count mismatch" return { 'wte': wte, 'value_embeds': value_embeds, 'lm_head': lm_head, 'transformer_matrices': transformer_matrices, 'scalars': scalars, 'total': total, } def setup_optimizer(self, unembedding_lr=0.004, embedding_lr=0.2, matrix_lr=0.02, weight_decay=0.0, adam_betas=(0.8, 0.95), scalar_lr=0.5): model_dim = self.config.n_embd ddp, rank, local_rank, world_size = get_dist_info() # Separate out all parameters into groups matrix_params = list(self.transformer.h.parameters()) value_embeds_params = list(self.value_embeds.parameters()) embedding_params = list(self.transformer.wte.parameters()) lm_head_params = list(self.lm_head.parameters()) resid_params = [self.resid_lambdas] x0_params = [self.x0_lambdas] assert len(list(self.parameters())) == len(matrix_params) + len(embedding_params) + len(lm_head_params) + len(value_embeds_params) + len(resid_params) + len(x0_params) # Scale the LR for the AdamW parameters by ∝1/√dmodel (tuned for 768 dim model) dmodel_lr_scale = (model_dim / 768) ** -0.5 print0(f"Scaling the LR for the AdamW parameters ∝1/√({model_dim}/768) = {dmodel_lr_scale:.6f}") # Build param_groups with all required fields explicit param_groups = [ # AdamW groups (embeddings, lm_head, scalars) dict(kind='adamw', params=lm_head_params, lr=unembedding_lr * dmodel_lr_scale, betas=adam_betas, eps=1e-10, weight_decay=0.0), dict(kind='adamw', params=embedding_params, lr=embedding_lr * dmodel_lr_scale, betas=adam_betas, eps=1e-10, weight_decay=0.0), dict(kind='adamw', params=value_embeds_params, lr=embedding_lr * dmodel_lr_scale, betas=adam_betas, eps=1e-10, weight_decay=0.0), dict(kind='adamw', params=resid_params, lr=scalar_lr * 0.01, betas=adam_betas, eps=1e-10, weight_decay=0.0), dict(kind='adamw', params=x0_params, lr=scalar_lr, betas=(0.96, 0.95), eps=1e-10, weight_decay=0.0), # higher beta1 for x0 ] # Muon groups (matrix params, grouped by shape for stacking) for shape in sorted({p.shape for p in matrix_params}): group_params = [p for p in matrix_params if p.shape == shape] param_groups.append(dict( kind='muon', params=group_params, lr=matrix_lr, momentum=0.95, ns_steps=5, beta2=0.95, weight_decay=weight_decay, )) Factory = DistMuonAdamW if ddp else MuonAdamW optimizer = Factory(param_groups) for group in optimizer.param_groups: group["initial_lr"] = group["lr"] return optimizer def forward(self, idx, targets=None, kv_cache=None, loss_reduction='mean'): B, T = idx.size() # Grab the rotary embeddings for the current sequence length (they are of shape (1, seq_len, 1, head_dim/2)) assert T <= self.cos.size(1), f"Sequence length grew beyond the rotary embeddings cache: {T} > {self.cos.size(1)}" assert idx.device == self.cos.device, f"Rotary embeddings and idx are on different devices: {idx.device} != {self.cos.device}" assert self.cos.dtype == torch.bfloat16, "Rotary embeddings must be in bfloat16" # if kv cache exists, we need to offset the rotary embeddings to the current position in the cache T0 = 0 if kv_cache is None else kv_cache.get_pos() cos_sin = self.cos[:, T0:T0+T], self.sin[:, T0:T0+T] # truncate cache to current sequence length # Forward the trunk of the Transformer x = self.transformer.wte(idx) # embed current token x = norm(x) x0 = x # save initial normalized embedding for x0 residual for i, block in enumerate(self.transformer.h): x = self.resid_lambdas[i] * x + self.x0_lambdas[i] * x0 ve = self.value_embeds[str(i)](idx) if str(i) in self.value_embeds else None x = block(x, ve, cos_sin, self.window_sizes[i], kv_cache) x = norm(x) # Forward the lm_head (compute logits) softcap = 15 # smoothly cap the logits to the range [-softcap, softcap] logits = self.lm_head(x) # (B, T, padded_vocab_size) <- very big tensor, large amount of memory logits = logits[..., :self.config.vocab_size] # slice to remove padding logits = logits.float() # switch to fp32 for logit softcap and loss computation logits = softcap * torch.tanh(logits / softcap) # squash the logits if targets is not None: # training: given the targets, compute and return the loss # TODO experiment with chunked cross-entropy? loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1, reduction=loss_reduction) return loss else: # inference: just return the logits directly return logits @torch.inference_mode() def generate(self, tokens, max_tokens, temperature=1.0, top_k=None, seed=42): """ Naive autoregressive streaming inference. To make it super simple, let's assume: - batch size is 1 - ids and the yielded tokens are simple Python lists and ints """ assert isinstance(tokens, list) device = self.get_device() rng = None if temperature > 0: rng = torch.Generator(device=device) rng.manual_seed(seed) ids = torch.tensor([tokens], dtype=torch.long, device=device) # add batch dim for _ in range(max_tokens): logits = self.forward(ids) # (B, T, vocab_size) logits = logits[:, -1, :] # (B, vocab_size) if top_k is not None and top_k > 0: v, _ = torch.topk(logits, min(top_k, logits.size(-1))) logits[logits < v[:, [-1]]] = -float('Inf') if temperature > 0: logits = logits / temperature probs = F.softmax(logits, dim=-1) next_ids = torch.multinomial(probs, num_samples=1, generator=rng) else: next_ids = torch.argmax(logits, dim=-1, keepdim=True) ids = torch.cat((ids, next_ids), dim=1) token = next_ids.item() yield token
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/gpt.py", "license": "MIT License", "lines": 401, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/loss_eval.py
""" A number of functions that help with evaluating a base model. """ import math import torch import torch.distributed as dist @torch.no_grad() def evaluate_bpb(model, batches, steps, token_bytes): """ Instead of the naive 'mean loss', this function returns the bits per byte (bpb), which is a tokenization vocab size-independent metric, meaning you are still comparing apples:apples if you change the vocab size. The way this works is that instead of just calculating the average loss as usual, you calculate the sum loss, and independently also the sum bytes (of all the target tokens), and divide. This normalizes the loss by the number of bytes that the target tokens represent. The added complexity is so that: 1) All "normal" tokens are normalized by the length of the token in bytes 2) No special tokens (e.g. <|bos|>) are included in the metric - they are masked out. 3) No actively masked tokens (using ignore_index of e.g. -1) are included in the metric. In addition to evaluate_loss, we need the token_bytes tensor: It is a 1D tensor of shape (vocab_size,), indicating the number of bytes for each token id, or 0 if the token is to not be counted (e.g. special tokens). """ # record the losses total_nats = torch.tensor(0.0, dtype=torch.float32, device=model.get_device()) total_bytes = torch.tensor(0, dtype=torch.int64, device=model.get_device()) batch_iter = iter(batches) for _ in range(steps): x, y = next(batch_iter) loss2d = model(x, y, loss_reduction='none') # (B, T) loss2d = loss2d.view(-1) # flatten y = y.view(-1) # flatten if (y.int() < 0).any(): # mps does not currently have kernel for < 0 for int64, only int32 # slightly more complex code path if some target tokens are ignore_index (e.g. -1) # any target token < 0 is to be ignored: do NOT index token_bytes with negatives valid = y >= 0 y_safe = torch.where(valid, y, torch.zeros_like(y)) # map valid targets to their byte length; ignored targets contribute 0 bytes num_bytes2d = torch.where( valid, token_bytes[y_safe], torch.zeros_like(y, dtype=token_bytes.dtype) ) total_nats += (loss2d * (num_bytes2d > 0)).sum() total_bytes += num_bytes2d.sum() else: # fast path: no ignored targets, safe to index directly num_bytes2d = token_bytes[y] total_nats += (loss2d * (num_bytes2d > 0)).sum() total_bytes += num_bytes2d.sum() # sum reduce across all ranks world_size = dist.get_world_size() if dist.is_initialized() else 1 if world_size > 1: dist.all_reduce(total_nats, op=dist.ReduceOp.SUM) dist.all_reduce(total_bytes, op=dist.ReduceOp.SUM) # move both to cpu, calculate bpb and return total_nats = total_nats.item() total_bytes = total_bytes.item() if total_bytes == 0: return float('inf') bpb = total_nats / (math.log(2) * total_bytes) return bpb
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/loss_eval.py", "license": "MIT License", "lines": 62, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/report.py
""" Utilities for generating training report cards. More messy code than usual, will fix. """ import os import re import shutil import subprocess import socket import datetime import platform import psutil import torch def run_command(cmd): """Run a shell command and return output, or None if it fails.""" try: result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=5) # Return stdout if we got output (even if some files in xargs failed) if result.stdout.strip(): return result.stdout.strip() if result.returncode == 0: return "" return None except: return None def get_git_info(): """Get current git commit, branch, and dirty status.""" info = {} info['commit'] = run_command("git rev-parse --short HEAD") or "unknown" info['branch'] = run_command("git rev-parse --abbrev-ref HEAD") or "unknown" # Check if repo is dirty (has uncommitted changes) status = run_command("git status --porcelain") info['dirty'] = bool(status) if status is not None else False # Get commit message info['message'] = run_command("git log -1 --pretty=%B") or "" info['message'] = info['message'].split('\n')[0][:80] # First line, truncated return info def get_gpu_info(): """Get GPU information.""" if not torch.cuda.is_available(): return {"available": False} num_devices = torch.cuda.device_count() info = { "available": True, "count": num_devices, "names": [], "memory_gb": [] } for i in range(num_devices): props = torch.cuda.get_device_properties(i) info["names"].append(props.name) info["memory_gb"].append(props.total_memory / (1024**3)) # Get CUDA version info["cuda_version"] = torch.version.cuda or "unknown" return info def get_system_info(): """Get system information.""" info = {} # Basic system info info['hostname'] = socket.gethostname() info['platform'] = platform.system() info['python_version'] = platform.python_version() info['torch_version'] = torch.__version__ # CPU and memory info['cpu_count'] = psutil.cpu_count(logical=False) info['cpu_count_logical'] = psutil.cpu_count(logical=True) info['memory_gb'] = psutil.virtual_memory().total / (1024**3) # User and environment info['user'] = os.environ.get('USER', 'unknown') info['nanochat_base_dir'] = os.environ.get('NANOCHAT_BASE_DIR', 'out') info['working_dir'] = os.getcwd() return info def estimate_cost(gpu_info, runtime_hours=None): """Estimate training cost based on GPU type and runtime.""" # Rough pricing, from Lambda Cloud default_rate = 2.0 gpu_hourly_rates = { "H100": 3.00, "A100": 1.79, "V100": 0.55, } if not gpu_info.get("available"): return None # Try to identify GPU type from name hourly_rate = None gpu_name = gpu_info["names"][0] if gpu_info["names"] else "unknown" for gpu_type, rate in gpu_hourly_rates.items(): if gpu_type in gpu_name: hourly_rate = rate * gpu_info["count"] break if hourly_rate is None: hourly_rate = default_rate * gpu_info["count"] # Default estimate return { "hourly_rate": hourly_rate, "gpu_type": gpu_name, "estimated_total": hourly_rate * runtime_hours if runtime_hours else None } def generate_header(): """Generate the header for a training report.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") git_info = get_git_info() gpu_info = get_gpu_info() sys_info = get_system_info() cost_info = estimate_cost(gpu_info) header = f"""# nanochat training report Generated: {timestamp} ## Environment ### Git Information - Branch: {git_info['branch']} - Commit: {git_info['commit']} {"(dirty)" if git_info['dirty'] else "(clean)"} - Message: {git_info['message']} ### Hardware - Platform: {sys_info['platform']} - CPUs: {sys_info['cpu_count']} cores ({sys_info['cpu_count_logical']} logical) - Memory: {sys_info['memory_gb']:.1f} GB """ if gpu_info.get("available"): gpu_names = ", ".join(set(gpu_info["names"])) total_vram = sum(gpu_info["memory_gb"]) header += f"""- GPUs: {gpu_info['count']}x {gpu_names} - GPU Memory: {total_vram:.1f} GB total - CUDA Version: {gpu_info['cuda_version']} """ else: header += "- GPUs: None available\n" if cost_info and cost_info["hourly_rate"] > 0: header += f"""- Hourly Rate: ${cost_info['hourly_rate']:.2f}/hour\n""" header += f""" ### Software - Python: {sys_info['python_version']} - PyTorch: {sys_info['torch_version']} """ # bloat metrics: count lines/chars in git-tracked source files only extensions = ['py', 'md', 'rs', 'html', 'toml', 'sh'] git_patterns = ' '.join(f"'*.{ext}'" for ext in extensions) files_output = run_command(f"git ls-files -- {git_patterns}") file_list = [f for f in (files_output or '').split('\n') if f] num_files = len(file_list) num_lines = 0 num_chars = 0 if num_files > 0: wc_output = run_command(f"git ls-files -- {git_patterns} | xargs wc -lc 2>/dev/null") if wc_output: total_line = wc_output.strip().split('\n')[-1] parts = total_line.split() if len(parts) >= 2: num_lines = int(parts[0]) num_chars = int(parts[1]) num_tokens = num_chars // 4 # assume approximately 4 chars per token # count dependencies via uv.lock uv_lock_lines = 0 if os.path.exists('uv.lock'): with open('uv.lock', 'r', encoding='utf-8') as f: uv_lock_lines = len(f.readlines()) header += f""" ### Bloat - Characters: {num_chars:,} - Lines: {num_lines:,} - Files: {num_files:,} - Tokens (approx): {num_tokens:,} - Dependencies (uv.lock lines): {uv_lock_lines:,} """ return header # ----------------------------------------------------------------------------- def slugify(text): """Slugify a text string.""" return text.lower().replace(" ", "-") # the expected files and their order EXPECTED_FILES = [ "tokenizer-training.md", "tokenizer-evaluation.md", "base-model-training.md", "base-model-loss.md", "base-model-evaluation.md", "chat-sft.md", "chat-evaluation-sft.md", "chat-rl.md", "chat-evaluation-rl.md", ] # the metrics we're currently interested in chat_metrics = ["ARC-Easy", "ARC-Challenge", "MMLU", "GSM8K", "HumanEval", "ChatCORE"] def extract(section, keys): """simple def to extract a single key from a section""" if not isinstance(keys, list): keys = [keys] # convenience out = {} for line in section.split("\n"): for key in keys: if key in line: out[key] = line.split(":")[1].strip() return out def extract_timestamp(content, prefix): """Extract timestamp from content with given prefix.""" for line in content.split('\n'): if line.startswith(prefix): time_str = line.split(":", 1)[1].strip() try: return datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S") except: pass return None class Report: """Maintains a bunch of logs, generates a final markdown report.""" def __init__(self, report_dir): os.makedirs(report_dir, exist_ok=True) self.report_dir = report_dir def log(self, section, data): """Log a section of data to the report.""" slug = slugify(section) file_name = f"{slug}.md" file_path = os.path.join(self.report_dir, file_name) with open(file_path, "w", encoding="utf-8") as f: f.write(f"## {section}\n") f.write(f"timestamp: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n") for item in data: if not item: # skip falsy values like None or empty dict etc. continue if isinstance(item, str): # directly write the string f.write(item) else: # render a dict for k, v in item.items(): if isinstance(v, float): vstr = f"{v:.4f}" elif isinstance(v, int) and v >= 10000: vstr = f"{v:,.0f}" else: vstr = str(v) f.write(f"- {k}: {vstr}\n") f.write("\n") return file_path def generate(self): """Generate the final report.""" report_dir = self.report_dir report_file = os.path.join(report_dir, "report.md") print(f"Generating report to {report_file}") final_metrics = {} # the most important final metrics we'll add as table at the end start_time = None end_time = None with open(report_file, "w", encoding="utf-8") as out_file: # write the header first header_file = os.path.join(report_dir, "header.md") if os.path.exists(header_file): with open(header_file, "r", encoding="utf-8") as f: header_content = f.read() out_file.write(header_content) start_time = extract_timestamp(header_content, "Run started:") # capture bloat data for summary later (the stuff after Bloat header and until \n\n) bloat_data = re.search(r"### Bloat\n(.*?)\n\n", header_content, re.DOTALL) bloat_data = bloat_data.group(1) if bloat_data else "" else: start_time = None # will cause us to not write the total wall clock time bloat_data = "[bloat data missing]" print(f"Warning: {header_file} does not exist. Did you forget to run `nanochat reset`?") # process all the individual sections for file_name in EXPECTED_FILES: section_file = os.path.join(report_dir, file_name) if not os.path.exists(section_file): print(f"Warning: {section_file} does not exist, skipping") continue with open(section_file, "r", encoding="utf-8") as in_file: section = in_file.read() # Extract timestamp from this section (the last section's timestamp will "stick" as end_time) if "rl" not in file_name: # Skip RL sections for end_time calculation because RL is experimental end_time = extract_timestamp(section, "timestamp:") # extract the most important metrics from the sections if file_name == "base-model-evaluation.md": final_metrics["base"] = extract(section, "CORE") if file_name == "chat-evaluation-sft.md": final_metrics["sft"] = extract(section, chat_metrics) if file_name == "chat-evaluation-rl.md": final_metrics["rl"] = extract(section, "GSM8K") # RL only evals GSM8K # append this section of the report out_file.write(section) out_file.write("\n") # add the final metrics table out_file.write("## Summary\n\n") # Copy over the bloat metrics from the header out_file.write(bloat_data) out_file.write("\n\n") # Collect all unique metric names all_metrics = set() for stage_metrics in final_metrics.values(): all_metrics.update(stage_metrics.keys()) # Custom ordering: CORE first, ChatCORE last, rest in middle all_metrics = sorted(all_metrics, key=lambda x: (x != "CORE", x == "ChatCORE", x)) # Fixed column widths stages = ["base", "sft", "rl"] metric_width = 15 value_width = 8 # Write table header header = f"| {'Metric'.ljust(metric_width)} |" for stage in stages: header += f" {stage.upper().ljust(value_width)} |" out_file.write(header + "\n") # Write separator separator = f"|{'-' * (metric_width + 2)}|" for stage in stages: separator += f"{'-' * (value_width + 2)}|" out_file.write(separator + "\n") # Write table rows for metric in all_metrics: row = f"| {metric.ljust(metric_width)} |" for stage in stages: value = final_metrics.get(stage, {}).get(metric, "-") row += f" {str(value).ljust(value_width)} |" out_file.write(row + "\n") out_file.write("\n") # Calculate and write total wall clock time if start_time and end_time: duration = end_time - start_time total_seconds = int(duration.total_seconds()) hours = total_seconds // 3600 minutes = (total_seconds % 3600) // 60 out_file.write(f"Total wall clock time: {hours}h{minutes}m\n") else: out_file.write("Total wall clock time: unknown\n") # also cp the report.md file to current directory print(f"Copying report.md to current directory for convenience") shutil.copy(report_file, "report.md") return report_file def reset(self): """Reset the report.""" # Remove section files for file_name in EXPECTED_FILES: file_path = os.path.join(self.report_dir, file_name) if os.path.exists(file_path): os.remove(file_path) # Remove report.md if it exists report_file = os.path.join(self.report_dir, "report.md") if os.path.exists(report_file): os.remove(report_file) # Generate and write the header section with start timestamp header_file = os.path.join(self.report_dir, "header.md") header = generate_header() start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(header_file, "w", encoding="utf-8") as f: f.write(header) f.write(f"Run started: {start_time}\n\n---\n\n") print(f"Reset report and wrote header to {header_file}") # ----------------------------------------------------------------------------- # nanochat-specific convenience functions class DummyReport: def log(self, *args, **kwargs): pass def reset(self, *args, **kwargs): pass def get_report(): # just for convenience, only rank 0 logs to report from nanochat.common import get_base_dir, get_dist_info ddp, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info() if ddp_rank == 0: report_dir = os.path.join(get_base_dir(), "report") return Report(report_dir) else: return DummyReport() if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Generate or reset nanochat training reports.") parser.add_argument("command", nargs="?", default="generate", choices=["generate", "reset"], help="Operation to perform (default: generate)") args = parser.parse_args() if args.command == "generate": get_report().generate() elif args.command == "reset": get_report().reset()
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/report.py", "license": "MIT License", "lines": 367, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:nanochat/tokenizer.py
""" BPE Tokenizer in the style of GPT-4. Two implementations are available: 1) HuggingFace Tokenizer that can do both training and inference but is really confusing 2) Our own RustBPE Tokenizer for training and tiktoken for efficient inference """ import os import copy from functools import lru_cache SPECIAL_TOKENS = [ # every document begins with the Beginning of Sequence (BOS) token that delimits documents "<|bos|>", # tokens below are only used during finetuning to render Conversations into token ids "<|user_start|>", # user messages "<|user_end|>", "<|assistant_start|>", # assistant messages "<|assistant_end|>", "<|python_start|>", # assistant invokes python REPL tool "<|python_end|>", "<|output_start|>", # python REPL outputs back to assistant "<|output_end|>", ] # NOTE: this split pattern deviates from GPT-4 in that we use \p{N}{1,2} instead of \p{N}{1,3} # I did this because I didn't want to "waste" too many tokens on numbers for smaller vocab sizes. # I verified that 2 is the sweet spot for vocab size of 32K. 1 is a bit worse, 3 was worse still. SPLIT_PATTERN = r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,2}| ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+""" # ----------------------------------------------------------------------------- # Generic GPT-4-style tokenizer based on HuggingFace Tokenizer from tokenizers import Tokenizer as HFTokenizer from tokenizers import pre_tokenizers, decoders, Regex from tokenizers.models import BPE from tokenizers.trainers import BpeTrainer class HuggingFaceTokenizer: """Light wrapper around HuggingFace Tokenizer for some utilities""" def __init__(self, tokenizer): self.tokenizer = tokenizer @classmethod def from_pretrained(cls, hf_path): # init from a HuggingFace pretrained tokenizer (e.g. "gpt2") tokenizer = HFTokenizer.from_pretrained(hf_path) return cls(tokenizer) @classmethod def from_directory(cls, tokenizer_dir): # init from a local directory on disk (e.g. "out/tokenizer") tokenizer_path = os.path.join(tokenizer_dir, "tokenizer.json") tokenizer = HFTokenizer.from_file(tokenizer_path) return cls(tokenizer) @classmethod def train_from_iterator(cls, text_iterator, vocab_size): # train from an iterator of text # Configure the HuggingFace Tokenizer tokenizer = HFTokenizer(BPE( byte_fallback=True, # needed! unk_token=None, fuse_unk=False, )) # Normalizer: None tokenizer.normalizer = None # Pre-tokenizer: GPT-4 style # the regex pattern used by GPT-4 to split text into groups before BPE # NOTE: The pattern was changed from \p{N}{1,3} to \p{N}{1,2} because I suspect it is harmful to # very small models and smaller vocab sizes, because it is a little bit wasteful in the token space. # (but I haven't validated this! TODO) gpt4_split_regex = Regex(SPLIT_PATTERN) # huggingface demands that you wrap it in Regex!! tokenizer.pre_tokenizer = pre_tokenizers.Sequence([ pre_tokenizers.Split(pattern=gpt4_split_regex, behavior="isolated", invert=False), pre_tokenizers.ByteLevel(add_prefix_space=False, use_regex=False) ]) # Decoder: ByteLevel (it pairs together with the ByteLevel pre-tokenizer) tokenizer.decoder = decoders.ByteLevel() # Post-processor: None tokenizer.post_processor = None # Trainer: BPE trainer = BpeTrainer( vocab_size=vocab_size, show_progress=True, min_frequency=0, # no minimum frequency initial_alphabet=pre_tokenizers.ByteLevel.alphabet(), special_tokens=SPECIAL_TOKENS, ) # Kick off the training tokenizer.train_from_iterator(text_iterator, trainer) return cls(tokenizer) def get_vocab_size(self): return self.tokenizer.get_vocab_size() def get_special_tokens(self): special_tokens_map = self.tokenizer.get_added_tokens_decoder() special_tokens = [w.content for w in special_tokens_map.values()] return special_tokens def id_to_token(self, id): return self.tokenizer.id_to_token(id) def _encode_one(self, text, prepend=None, append=None, num_threads=None): # encode a single string # prepend/append can be either a string of a special token or a token id directly. # num_threads is ignored (only used by the nanochat Tokenizer for parallel encoding) assert isinstance(text, str) ids = [] if prepend is not None: prepend_id = prepend if isinstance(prepend, int) else self.encode_special(prepend) ids.append(prepend_id) ids.extend(self.tokenizer.encode(text, add_special_tokens=False).ids) if append is not None: append_id = append if isinstance(append, int) else self.encode_special(append) ids.append(append_id) return ids def encode_special(self, text): # encode a single special token via exact match return self.tokenizer.token_to_id(text) def get_bos_token_id(self): # Different HuggingFace models use different BOS tokens and there is little consistency # 1) attempt to find a <|bos|> token bos = self.encode_special("<|bos|>") # 2) if that fails, attempt to find a <|endoftext|> token (e.g. GPT-2 models) if bos is None: bos = self.encode_special("<|endoftext|>") # 3) if these fail, it's better to crash than to silently return None assert bos is not None, "Failed to find BOS token in tokenizer" return bos def encode(self, text, *args, **kwargs): if isinstance(text, str): return self._encode_one(text, *args, **kwargs) elif isinstance(text, list): return [self._encode_one(t, *args, **kwargs) for t in text] else: raise ValueError(f"Invalid input type: {type(text)}") def __call__(self, *args, **kwargs): return self.encode(*args, **kwargs) def decode(self, ids): return self.tokenizer.decode(ids, skip_special_tokens=False) def save(self, tokenizer_dir): # save the tokenizer to disk os.makedirs(tokenizer_dir, exist_ok=True) tokenizer_path = os.path.join(tokenizer_dir, "tokenizer.json") self.tokenizer.save(tokenizer_path) print(f"Saved tokenizer to {tokenizer_path}") # ----------------------------------------------------------------------------- # Tokenizer based on rustbpe + tiktoken combo import pickle import rustbpe import tiktoken class RustBPETokenizer: """Light wrapper around tiktoken (for efficient inference) but train with rustbpe""" def __init__(self, enc, bos_token): self.enc = enc self.bos_token_id = self.encode_special(bos_token) @classmethod def train_from_iterator(cls, text_iterator, vocab_size): # 1) train using rustbpe tokenizer = rustbpe.Tokenizer() # the special tokens are inserted later in __init__, we don't train them here vocab_size_no_special = vocab_size - len(SPECIAL_TOKENS) assert vocab_size_no_special >= 256, f"vocab_size_no_special must be at least 256, got {vocab_size_no_special}" tokenizer.train_from_iterator(text_iterator, vocab_size_no_special, pattern=SPLIT_PATTERN) # 2) construct the associated tiktoken encoding for inference pattern = tokenizer.get_pattern() mergeable_ranks_list = tokenizer.get_mergeable_ranks() mergeable_ranks = {bytes(k): v for k, v in mergeable_ranks_list} tokens_offset = len(mergeable_ranks) special_tokens = {name: tokens_offset + i for i, name in enumerate(SPECIAL_TOKENS)} enc = tiktoken.Encoding( name="rustbpe", pat_str=pattern, mergeable_ranks=mergeable_ranks, # dict[bytes, int] (token bytes -> merge priority rank) special_tokens=special_tokens, # dict[str, int] (special token name -> token id) ) return cls(enc, "<|bos|>") @classmethod def from_directory(cls, tokenizer_dir): pickle_path = os.path.join(tokenizer_dir, "tokenizer.pkl") with open(pickle_path, "rb") as f: enc = pickle.load(f) return cls(enc, "<|bos|>") @classmethod def from_pretrained(cls, tiktoken_name): # https://github.com/openai/tiktoken/blob/eedc8563/tiktoken_ext/openai_public.py enc = tiktoken.get_encoding(tiktoken_name) # tiktoken calls the special document delimiter token "<|endoftext|>" # yes this is confusing because this token is almost always PREPENDED to the beginning of the document # it most often is used to signal the start of a new sequence to the LLM during inference etc. # so in nanoChat we always use "<|bos|>" short for "beginning of sequence", but historically it is often called "<|endoftext|>". return cls(enc, "<|endoftext|>") def get_vocab_size(self): return self.enc.n_vocab def get_special_tokens(self): return self.enc.special_tokens_set def id_to_token(self, id): return self.enc.decode([id]) @lru_cache(maxsize=32) def encode_special(self, text): return self.enc.encode_single_token(text) def get_bos_token_id(self): return self.bos_token_id def encode(self, text, prepend=None, append=None, num_threads=8): # text can be either a string or a list of strings if prepend is not None: prepend_id = prepend if isinstance(prepend, int) else self.encode_special(prepend) if append is not None: append_id = append if isinstance(append, int) else self.encode_special(append) if isinstance(text, str): ids = self.enc.encode_ordinary(text) if prepend is not None: ids.insert(0, prepend_id) # TODO: slightly inefficient here? :( hmm if append is not None: ids.append(append_id) elif isinstance(text, list): ids = self.enc.encode_ordinary_batch(text, num_threads=num_threads) if prepend is not None: for ids_row in ids: ids_row.insert(0, prepend_id) # TODO: same if append is not None: for ids_row in ids: ids_row.append(append_id) else: raise ValueError(f"Invalid input type: {type(text)}") return ids def __call__(self, *args, **kwargs): return self.encode(*args, **kwargs) def decode(self, ids): return self.enc.decode(ids) def save(self, tokenizer_dir): # save the encoding object to disk os.makedirs(tokenizer_dir, exist_ok=True) pickle_path = os.path.join(tokenizer_dir, "tokenizer.pkl") with open(pickle_path, "wb") as f: pickle.dump(self.enc, f) print(f"Saved tokenizer encoding to {pickle_path}") def render_conversation(self, conversation, max_tokens=2048): """ Tokenize a single Chat conversation (which we call a "doc" or "document" here). Returns: - ids: list[int] is a list of token ids of this rendered conversation - mask: list[int] of same length, mask = 1 for tokens that the Assistant is expected to train on. """ # ids, masks that we will return and a helper function to help build them up. ids, mask = [], [] def add_tokens(token_ids, mask_val): if isinstance(token_ids, int): token_ids = [token_ids] ids.extend(token_ids) mask.extend([mask_val] * len(token_ids)) # sometimes the first message is a system message... # => just merge it with the second (user) message if conversation["messages"][0]["role"] == "system": # some conversation surgery is necessary here for now... conversation = copy.deepcopy(conversation) # avoid mutating the original messages = conversation["messages"] assert messages[1]["role"] == "user", "System message must be followed by a user message" messages[1]["content"] = messages[0]["content"] + "\n\n" + messages[1]["content"] messages = messages[1:] else: messages = conversation["messages"] assert len(messages) >= 1, f"Conversation has less than 1 message: {messages}" # fetch all the special tokens we need bos = self.get_bos_token_id() user_start, user_end = self.encode_special("<|user_start|>"), self.encode_special("<|user_end|>") assistant_start, assistant_end = self.encode_special("<|assistant_start|>"), self.encode_special("<|assistant_end|>") python_start, python_end = self.encode_special("<|python_start|>"), self.encode_special("<|python_end|>") output_start, output_end = self.encode_special("<|output_start|>"), self.encode_special("<|output_end|>") # now we can tokenize the conversation add_tokens(bos, 0) for i, message in enumerate(messages): # some sanity checking here around assumptions, to prevent footguns must_be_from = "user" if i % 2 == 0 else "assistant" assert message["role"] == must_be_from, f"Message {i} is from {message['role']} but should be from {must_be_from}" # content can be either a simple string or a list of parts (e.g. containing tool calls) content = message["content"] if message["role"] == "user": assert isinstance(content, str), "User messages are simply expected to be strings" value_ids = self.encode(content) add_tokens(user_start, 0) add_tokens(value_ids, 0) add_tokens(user_end, 0) elif message["role"] == "assistant": add_tokens(assistant_start, 0) if isinstance(content, str): # simple string => simply add the tokens value_ids = self.encode(content) add_tokens(value_ids, 1) elif isinstance(content, list): for part in content: value_ids = self.encode(part["text"]) if part["type"] == "text": # string part => simply add the tokens add_tokens(value_ids, 1) elif part["type"] == "python": # python tool call => add the tokens inside <|python_start|> and <|python_end|> add_tokens(python_start, 1) add_tokens(value_ids, 1) add_tokens(python_end, 1) elif part["type"] == "python_output": # python output => add the tokens inside <|output_start|> and <|output_end|> # none of these tokens are supervised because the tokens come from Python at test time add_tokens(output_start, 0) add_tokens(value_ids, 0) add_tokens(output_end, 0) else: raise ValueError(f"Unknown part type: {part['type']}") else: raise ValueError(f"Unknown content type: {type(content)}") add_tokens(assistant_end, 1) # truncate to max_tokens tokens MAX (helps prevent OOMs) ids = ids[:max_tokens] mask = mask[:max_tokens] return ids, mask def visualize_tokenization(self, ids, mask, with_token_id=False): """Small helper function useful in debugging: visualize the tokenization of render_conversation""" RED = '\033[91m' GREEN = '\033[92m' RESET = '\033[0m' GRAY = '\033[90m' tokens = [] for i, (token_id, mask_val) in enumerate(zip(ids, mask)): token_str = self.decode([token_id]) color = GREEN if mask_val == 1 else RED tokens.append(f"{color}{token_str}{RESET}") if with_token_id: tokens.append(f"{GRAY}({token_id}){RESET}") return '|'.join(tokens) def render_for_completion(self, conversation): """ Used during Reinforcement Learning. In that setting, we want to render the conversation priming the Assistant for a completion. Unlike the Chat SFT case, we don't need to return the mask. """ # We have some surgery to do: we need to pop the last message (of the Assistant) conversation = copy.deepcopy(conversation) # avoid mutating the original messages = conversation["messages"] assert messages[-1]["role"] == "assistant", "Last message must be from the Assistant" messages.pop() # remove the last message (of the Assistant) inplace # Now tokenize the conversation ids, mask = self.render_conversation(conversation) # Finally, to prime the Assistant for a completion, append the Assistant start token assistant_start = self.encode_special("<|assistant_start|>") ids.append(assistant_start) return ids # ----------------------------------------------------------------------------- # nanochat-specific convenience functions def get_tokenizer(): from nanochat.common import get_base_dir base_dir = get_base_dir() tokenizer_dir = os.path.join(base_dir, "tokenizer") # return HuggingFaceTokenizer.from_directory(tokenizer_dir) return RustBPETokenizer.from_directory(tokenizer_dir) def get_token_bytes(device="cpu"): import torch from nanochat.common import get_base_dir base_dir = get_base_dir() tokenizer_dir = os.path.join(base_dir, "tokenizer") token_bytes_path = os.path.join(tokenizer_dir, "token_bytes.pt") assert os.path.exists(token_bytes_path), f"Token bytes not found at {token_bytes_path}? It gets written by tok_train.py" with open(token_bytes_path, "rb") as f: token_bytes = torch.load(f, map_location=device) return token_bytes
{ "repo_id": "karpathy/nanochat", "file_path": "nanochat/tokenizer.py", "license": "MIT License", "lines": 353, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/base_eval.py
""" Unified evaluation script for base models. Supports three evaluation modes (comma-separated): --eval core : CORE metric (accuracy on ICL tasks) --eval bpb : Bits per byte on train/val splits --eval sample : Generate samples from the model Default is all three: --eval core,bpb,sample Examples: # Evaluate a HuggingFace model (e.g. GPT-2 124M) using 8 GPUs torchrun --nproc_per_node=8 -m scripts.base_eval --hf-path openai-community/gpt2 # Evaluate a nanochat model (e.g. d24) using 8 GPUs torchrun --nproc_per_node=8 -m scripts.base_eval --model-tag d24 --device-batch-size=16 # Quick/approximate evaluation using a single GPU python -m scripts.base_eval --model-tag d24 --device-batch-size=16 --max-per-task=100 --split-tokens=524288 """ import os import csv import time import json import yaml import shutil import random import zipfile import tempfile import argparse from contextlib import nullcontext import torch from nanochat.common import compute_init, compute_cleanup, print0, get_base_dir, autodetect_device_type, download_file_with_lock from nanochat.tokenizer import HuggingFaceTokenizer, get_token_bytes from nanochat.checkpoint_manager import load_model from nanochat.core_eval import evaluate_task from nanochat.dataloader import tokenizing_distributed_data_loader_bos_bestfit from nanochat.loss_eval import evaluate_bpb from nanochat.engine import Engine # ----------------------------------------------------------------------------- # HuggingFace loading utilities class ModelWrapper: """Lightweight wrapper to give HuggingFace models a nanochat-compatible interface.""" def __init__(self, model, max_seq_len=None): self.model = model self.max_seq_len = max_seq_len def __call__(self, input_ids, targets=None, loss_reduction='mean'): logits = self.model(input_ids).logits if targets is None: return logits loss = torch.nn.functional.cross_entropy( logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1, reduction=loss_reduction ) return loss def get_device(self): return next(self.model.parameters()).device def load_hf_model(hf_path: str, device): """Load a HuggingFace model and tokenizer.""" print0(f"Loading HuggingFace model from: {hf_path}") from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained(hf_path) model.to(device) model.eval() max_seq_len = 1024 if "gpt2" in hf_path else None model = ModelWrapper(model, max_seq_len=max_seq_len) tokenizer = HuggingFaceTokenizer.from_pretrained(hf_path) return model, tokenizer def get_hf_token_bytes(tokenizer, device="cpu"): """Compute token_bytes tensor for a HuggingFace tokenizer.""" vocab_size = tokenizer.tokenizer.get_vocab_size() token_bytes = torch.zeros(vocab_size, dtype=torch.int64, device=device) for token_id in range(vocab_size): token_str = tokenizer.tokenizer.decode([token_id]) token_bytes[token_id] = len(token_str.encode('utf-8')) return token_bytes # ----------------------------------------------------------------------------- # CORE evaluation EVAL_BUNDLE_URL = "https://karpathy-public.s3.us-west-2.amazonaws.com/eval_bundle.zip" def place_eval_bundle(file_path): """Unzip eval_bundle.zip and place it in the base directory.""" base_dir = get_base_dir() eval_bundle_dir = os.path.join(base_dir, "eval_bundle") with tempfile.TemporaryDirectory() as tmpdir: with zipfile.ZipFile(file_path, 'r') as zip_ref: zip_ref.extractall(tmpdir) extracted_bundle_dir = os.path.join(tmpdir, "eval_bundle") shutil.move(extracted_bundle_dir, eval_bundle_dir) print0(f"Placed eval_bundle directory at {eval_bundle_dir}") def evaluate_core(model, tokenizer, device, max_per_task=-1): """ Evaluate a base model on the CORE benchmark. Returns dict with results, centered_results, and core_metric. """ base_dir = get_base_dir() eval_bundle_dir = os.path.join(base_dir, "eval_bundle") # Download the eval bundle if needed if not os.path.exists(eval_bundle_dir): download_file_with_lock(EVAL_BUNDLE_URL, "eval_bundle.zip", postprocess_fn=place_eval_bundle) config_path = os.path.join(eval_bundle_dir, "core.yaml") data_base_path = os.path.join(eval_bundle_dir, "eval_data") eval_meta_data = os.path.join(eval_bundle_dir, "eval_meta_data.csv") with open(config_path, 'r', encoding='utf-8') as f: config = yaml.safe_load(f) tasks = config['icl_tasks'] # Load random baseline values random_baselines = {} with open(eval_meta_data, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: task_name = row['Eval Task'] random_baseline = row['Random baseline'] random_baselines[task_name] = float(random_baseline) # Evaluate each task results = {} centered_results = {} for task in tasks: start_time = time.time() label = task['label'] task_meta = { 'task_type': task['icl_task_type'], 'dataset_uri': task['dataset_uri'], 'num_fewshot': task['num_fewshot'][0], 'continuation_delimiter': task.get('continuation_delimiter', ' ') } print0(f"Evaluating: {label} ({task_meta['num_fewshot']}-shot, type: {task_meta['task_type']})... ", end='') data_path = os.path.join(data_base_path, task_meta['dataset_uri']) with open(data_path, 'r', encoding='utf-8') as f: data = [json.loads(line.strip()) for line in f] # Shuffle for consistent subsampling when using max_per_task shuffle_rng = random.Random(1337) shuffle_rng.shuffle(data) if max_per_task > 0: data = data[:max_per_task] accuracy = evaluate_task(model, tokenizer, data, device, task_meta) results[label] = accuracy random_baseline = random_baselines[label] centered_result = (accuracy - 0.01 * random_baseline) / (1.0 - 0.01 * random_baseline) centered_results[label] = centered_result elapsed = time.time() - start_time print0(f"accuracy: {accuracy:.4f} | centered: {centered_result:.4f} | time: {elapsed:.2f}s") core_metric = sum(centered_results.values()) / len(centered_results) out = { "results": results, "centered_results": centered_results, "core_metric": core_metric } return out # ----------------------------------------------------------------------------- # Main def main(): parser = argparse.ArgumentParser(description="Base model evaluation") parser.add_argument('--eval', type=str, default='core,bpb,sample', help='Comma-separated evaluations to run: core,bpb,sample (default: all)') parser.add_argument('--hf-path', type=str, default=None, help='HuggingFace model path (e.g. openai-community/gpt2-xl)') parser.add_argument('--model-tag', type=str, default=None, help='nanochat model tag to identify the checkpoint directory') parser.add_argument('--step', type=int, default=None, help='Model step to load (default = last)') parser.add_argument('--max-per-task', type=int, default=-1, help='Max examples per CORE task (-1 = all)') parser.add_argument('--device-batch-size', type=int, default=32, help='Per-device batch size for BPB evaluation') parser.add_argument('--split-tokens', type=int, default=40*524288, help='Number of tokens to evaluate per split for BPB') parser.add_argument('--device-type', type=str, default='', help='cuda|cpu|mps (empty = autodetect)') args = parser.parse_args() # Parse evaluation modes eval_modes = set(mode.strip() for mode in args.eval.split(',')) valid_modes = {'core', 'bpb', 'sample'} invalid = eval_modes - valid_modes if invalid: parser.error(f"Invalid eval modes: {invalid}. Valid: {valid_modes}") # Distributed / precision setup device_type = autodetect_device_type() if args.device_type == '' else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=torch.bfloat16) if device_type == "cuda" else nullcontext() # Load model and tokenizer is_hf_model = args.hf_path is not None if is_hf_model: model, tokenizer = load_hf_model(args.hf_path, device) sequence_len = model.max_seq_len or 1024 token_bytes = get_hf_token_bytes(tokenizer, device=device) model_name = args.hf_path model_slug = args.hf_path.replace("/", "-") else: model, tokenizer, meta = load_model("base", device, phase="eval", model_tag=args.model_tag, step=args.step) sequence_len = meta["model_config"]["sequence_len"] token_bytes = get_token_bytes(device=device) model_name = f"base_model (step {meta['step']})" model_slug = f"base_model_{meta['step']:06d}" print0(f"Evaluating model: {model_name}") print0(f"Eval modes: {', '.join(sorted(eval_modes))}") # Results to log core_results = None bpb_results = {} samples = [] unconditioned_samples = [] # --- Sampling --- if 'sample' in eval_modes and not is_hf_model: print0("\n" + "="*80) print0("Model Samples") print0("="*80) if ddp_rank == 0: prompts = [ "The capital of France is", "The chemical symbol of gold is", "If yesterday was Friday, then tomorrow will be", "The opposite of hot is", "The planets of the solar system are:", "My favorite color is", "If 5*x + 3 = 13, then x is", ] engine = Engine(model, tokenizer) print0("\nConditioned samples:") for prompt in prompts: tokens = tokenizer(prompt, prepend="<|bos|>") with autocast_ctx: sample, _ = engine.generate_batch(tokens, num_samples=1, max_tokens=16, temperature=0) sample_str = tokenizer.decode(sample[0]) print0("-" * 80) print0(sample_str) samples.append(sample_str) print0("\nUnconditioned samples:") tokens = tokenizer("", prepend="<|bos|>") with autocast_ctx: uncond, _ = engine.generate_batch(tokens, num_samples=8, max_tokens=128, temperature=1.0) for sample in uncond: sample_str = tokenizer.decode(sample) print0("-" * 80) print0(sample_str) unconditioned_samples.append(sample_str) elif 'sample' in eval_modes and is_hf_model: print0("\nSkipping sampling for HuggingFace models (not supported)") # --- BPB evaluation --- if 'bpb' in eval_modes: print0("\n" + "="*80) print0("BPB Evaluation") print0("="*80) tokens_per_step = args.device_batch_size * sequence_len * ddp_world_size if args.split_tokens % tokens_per_step != 0: # Adjust to nearest multiple args.split_tokens = (args.split_tokens // tokens_per_step) * tokens_per_step print0(f"Adjusted split_tokens to {args.split_tokens} (must be divisible by {tokens_per_step})") steps = args.split_tokens // tokens_per_step for split_name in ["train", "val"]: loader = tokenizing_distributed_data_loader_bos_bestfit(tokenizer, args.device_batch_size, sequence_len, split_name, device=device) with autocast_ctx: bpb = evaluate_bpb(model, loader, steps, token_bytes) bpb_results[split_name] = bpb print0(f"{split_name} bpb: {bpb:.6f}") # --- CORE evaluation --- if 'core' in eval_modes: print0("\n" + "="*80) print0("CORE Evaluation") print0("="*80) with autocast_ctx: core_results = evaluate_core(model, tokenizer, device, max_per_task=args.max_per_task) # Write CSV output if ddp_rank == 0: base_dir = get_base_dir() output_csv_path = os.path.join(base_dir, "base_eval", f"{model_slug}.csv") os.makedirs(os.path.dirname(output_csv_path), exist_ok=True) with open(output_csv_path, 'w', encoding='utf-8', newline='') as f: f.write(f"{'Task':<35}, {'Accuracy':<10}, {'Centered':<10}\n") for label in core_results["results"]: acc = core_results["results"][label] centered = core_results["centered_results"][label] f.write(f"{label:<35}, {acc:<10.6f}, {centered:<10.6f}\n") f.write(f"{'CORE':<35}, {'':<10}, {core_results['core_metric']:<10.6f}\n") print0(f"\nResults written to: {output_csv_path}") print0(f"CORE metric: {core_results['core_metric']:.4f}") # --- Log to report --- from nanochat.report import get_report report_data = [{"model": model_name}] if core_results: report_data[0]["CORE metric"] = core_results["core_metric"] report_data.append(core_results["centered_results"]) if bpb_results: report_data[0]["train bpb"] = bpb_results.get("train") report_data[0]["val bpb"] = bpb_results.get("val") if samples: report_data.append({f"sample {i}": s for i, s in enumerate(samples)}) if unconditioned_samples: report_data.append({f"unconditioned {i}": s for i, s in enumerate(unconditioned_samples)}) get_report().log(section="Base model evaluation", data=report_data) compute_cleanup() if __name__ == "__main__": main()
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/base_eval.py", "license": "MIT License", "lines": 280, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/base_train.py
""" Train model. From root directory of the project, run as: python -m scripts.base_train or distributed as: torchrun --nproc_per_node=8 -m scripts.base_train If you are only on CPU/Macbook, you'll want to train a much much smaller LLM. Example: python -m scripts.base_train --depth=4 --max-seq-len=512 --device-batch-size=1 --eval-tokens=512 --core-metric-every=-1 --total-batch-size=512 --num-iterations=20 """ import os os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True" import gc import json import time import math import argparse from dataclasses import asdict from contextlib import nullcontext, contextmanager import wandb import torch from nanochat.gpt import GPT, GPTConfig from nanochat.dataloader import tokenizing_distributed_data_loader_bos_bestfit, tokenizing_distributed_data_loader_with_state_bos_bestfit from nanochat.common import compute_init, compute_cleanup, print0, DummyWandb, print_banner, get_base_dir, autodetect_device_type, get_peak_flops from nanochat.tokenizer import get_tokenizer, get_token_bytes from nanochat.checkpoint_manager import save_checkpoint, load_checkpoint from nanochat.loss_eval import evaluate_bpb from nanochat.engine import Engine from nanochat.flash_attention import HAS_FA3 from scripts.base_eval import evaluate_core print_banner() # ----------------------------------------------------------------------------- # CLI arguments parser = argparse.ArgumentParser(description="Pretrain base model") # Logging parser.add_argument("--run", type=str, default="dummy", help="wandb run name ('dummy' disables wandb logging)") # Runtime parser.add_argument("--device-type", type=str, default="", help="cuda|cpu|mps (empty = autodetect)") # FP8 training parser.add_argument("--fp8", action="store_true", help="enable FP8 training (requires H100+ GPU and torchao)") parser.add_argument("--fp8-recipe", type=str, default="tensorwise", choices=["rowwise", "tensorwise"], help="FP8 scaling recipe: tensorwise (faster, recommended) or rowwise (more accurate but slower)") # Model architecture parser.add_argument("--depth", type=int, default=20, help="depth of the Transformer model") parser.add_argument("--aspect-ratio", type=int, default=64, help="model_dim = depth * aspect_ratio") parser.add_argument("--head-dim", type=int, default=128, help="target head dimension for attention") parser.add_argument("--max-seq-len", type=int, default=2048, help="max context length") parser.add_argument("--window-pattern", type=str, default="SSSL", help="sliding window pattern tiled across layers: L=full, S=half context (e.g. 'SSL')") # Training horizon (only one used, in order of precedence) parser.add_argument("--num-iterations", type=int, default=-1, help="explicit number of optimization steps (-1 = disable)") parser.add_argument("--target-flops", type=float, default=-1.0, help="calculate num_iterations to reach target_flops (-1 = disable)") parser.add_argument("--target-param-data-ratio", type=float, default=10.5, help="calculate num_iterations to maintain data:param ratio (Chinchilla=20, -1 = disable)") # Optimization parser.add_argument("--device-batch-size", type=int, default=32, help="per-device batch size. good number to reduce to 16,8,4,... if you OOM on VRAM.") parser.add_argument("--total-batch-size", type=int, default=-1, help="total batch size in tokens. decent numbers are e.g. 524288. (-1 = auto-compute optimal)") parser.add_argument("--embedding-lr", type=float, default=0.3, help="learning rate for embedding parameters (Adam)") parser.add_argument("--unembedding-lr", type=float, default=0.004, help="learning rate for unembedding parameters (Adam)") parser.add_argument("--weight-decay", type=float, default=0.2, help="cautious weight decay for the Muon optimizer (for weights)") parser.add_argument("--matrix-lr", type=float, default=0.02, help="learning rate for matrix parameters (Muon)") parser.add_argument("--scalar-lr", type=float, default=0.5, help="learning rate for scalars (resid_lambdas, x0_lambdas)") parser.add_argument("--adam-beta1", type=float, default=0.8, help="Adam beta1 for embedding/unembedding") parser.add_argument("--adam-beta2", type=float, default=0.95, help="Adam beta2 for embedding/unembedding") parser.add_argument("--warmup-ratio", type=float, default=0.0, help="ratio of iterations for LR warmup") parser.add_argument("--warmdown-ratio", type=float, default=0.5, help="ratio of iterations for LR warmdown") parser.add_argument("--final-lr-frac", type=float, default=0.0, help="final LR as fraction of initial LR") parser.add_argument("--resume-from-step", type=int, default=-1, help="resume training from this step (-1 = disable)") # Evaluation parser.add_argument("--eval-every", type=int, default=250, help="evaluate val bpb every N steps (-1 = disable)") parser.add_argument("--eval-tokens", type=int, default=40*524288, help="number of tokens to evaluate val loss on") parser.add_argument("--core-metric-every", type=int, default=2000, help="evaluate CORE metric every N steps (-1 = disable)") parser.add_argument("--core-metric-max-per-task", type=int, default=500, help="examples per task for CORE metric") parser.add_argument("--sample-every", type=int, default=2000, help="sample from model every N steps (-1 = disable)") parser.add_argument("--save-every", type=int, default=-1, help="save checkpoints every N steps (-1 = only at end)") # Output parser.add_argument("--model-tag", type=str, default=None, help="override model tag for checkpoint directory name") args = parser.parse_args() user_config = vars(args).copy() # for logging # ----------------------------------------------------------------------------- # Compute init and wandb logging device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) master_process = ddp_rank == 0 # this process will do logging, checkpointing etc. autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=torch.bfloat16) if device_type == "cuda" else nullcontext() synchronize = torch.cuda.synchronize if device_type == "cuda" else lambda: None get_max_memory = torch.cuda.max_memory_allocated if device_type == "cuda" else lambda: 0 if device_type == "cuda": gpu_device_name = torch.cuda.get_device_name(0) gpu_peak_flops = get_peak_flops(gpu_device_name) print0(f"GPU: {gpu_device_name} | Peak FLOPS (BF16): {gpu_peak_flops:.2e}") else: gpu_peak_flops = float('inf') # MFU not meaningful for CPU/MPS # wandb logging init use_dummy_wandb = args.run == "dummy" or not master_process wandb_run = DummyWandb() if use_dummy_wandb else wandb.init(project="nanochat", name=args.run, config=user_config) # Flash Attention status if HAS_FA3: print0("βœ“ Using Flash Attention 3 (Hopper GPU detected), efficient, new and awesome.") else: print0("!" * 80) print0("WARNING: Flash Attention 3 not available, using PyTorch SDPA fallback") print0("WARNING: Training will be less efficient without FA3") if args.window_pattern != "L": print0(f"WARNING: SDPA has no support for sliding window attention (window_pattern='{args.window_pattern}'). Your GPU utilization will be terrible.") print0("WARNING: Recommend using --window-pattern L for full context attention without alternating sliding window patterns.") print0("!" * 80) # ----------------------------------------------------------------------------- # Tokenizer will be useful for evaluation and also we need the vocab size to init the model tokenizer = get_tokenizer() token_bytes = get_token_bytes(device=device) vocab_size = tokenizer.get_vocab_size() print0(f"Vocab size: {vocab_size:,}") # ----------------------------------------------------------------------------- # Initialize the Model def build_model_meta(depth): """Build a model on meta device for a given depth (shapes/dtypes only, no data).""" # Model dim is nudged up to nearest multiple of head_dim for clean division # (FA3 requires head_dim divisible by 8, and this guarantees head_dim == args.head_dim exactly) base_dim = depth * args.aspect_ratio model_dim = ((base_dim + args.head_dim - 1) // args.head_dim) * args.head_dim num_heads = model_dim // args.head_dim config = GPTConfig( sequence_len=args.max_seq_len, vocab_size=vocab_size, n_layer=depth, n_head=num_heads, n_kv_head=num_heads, n_embd=model_dim, window_pattern=args.window_pattern, ) with torch.device("meta"): model_meta = GPT(config) return model_meta # Build the model, move to device, init the weights model = build_model_meta(args.depth) # 1) Build on meta device (only shapes/dtypes, no data) model_config = model.config model_config_kwargs = asdict(model_config) print0(f"Model config:\n{json.dumps(model_config_kwargs, indent=2)}") model.to_empty(device=device) # 2) All tensors get storage on target device but with uninitialized (garbage) data model.init_weights() # 3) All tensors get initialized # If we are resuming, overwrite the model parameters with those of the checkpoint base_dir = get_base_dir() output_dirname = args.model_tag if args.model_tag else f"d{args.depth}" # e.g. d12 checkpoint_dir = os.path.join(base_dir, "base_checkpoints", output_dirname) resuming = args.resume_from_step != -1 if resuming: print0(f"Resuming optimization from step {args.resume_from_step}") model_data, optimizer_data, meta_data = load_checkpoint(checkpoint_dir, args.resume_from_step, device, load_optimizer=True, rank=ddp_rank) model.load_state_dict(model_data, strict=True, assign=True) del model_data # free up this memory after the copy # ----------------------------------------------------------------------------- # FP8 training initialization and management (this has to be done before torch.compile) # Convert Linear layers to Float8Linear if --fp8 is set if args.fp8: if device_type != "cuda": print0("Warning: FP8 training requires CUDA, ignoring --fp8 flag") else: # our custom fp8 is simpler than torchao, written for exact API compatibility from nanochat.fp8 import Float8LinearConfig, convert_to_float8_training # from torchao.float8 import Float8LinearConfig, convert_to_float8_training import torch.nn as nn # Filter: dims must be divisible by 16 (FP8 hardware requirement) large enough def fp8_module_filter(mod: nn.Module, fqn: str) -> bool: if not isinstance(mod, nn.Linear): return False if mod.in_features % 16 != 0 or mod.out_features % 16 != 0: return False if min(mod.in_features, mod.out_features) < 128: return False return True fp8_config = Float8LinearConfig.from_recipe_name(args.fp8_recipe) num_linear = sum(1 for m in model.modules() if isinstance(m, nn.Linear)) convert_to_float8_training(model, config=fp8_config, module_filter_fn=fp8_module_filter) num_fp8 = sum(1 for m in model.modules() if 'Float8' in type(m).__name__) num_skipped = num_linear - num_fp8 print0(f"βœ“ FP8 training enabled ({args.fp8_recipe} scaling) - converted {num_fp8}/{num_linear} linear layers, skipped {num_skipped} (too small)") # Context manager to temporarily disable FP8 so that model evaluation remains in BF16 @contextmanager def disable_fp8(model): """Temporarily swap Float8Linear modules with nn.Linear for BF16 evaluation. CastConfig is a frozen dataclass, so we can't mutate scaling_type. Instead, we swap out Float8Linear modules entirely and restore them after. """ import torch.nn as nn # Find all Float8Linear modules and their locations fp8_locations = [] # list of (parent_module, attr_name, fp8_module) for name, module in model.named_modules(): if 'Float8' in type(module).__name__: if '.' in name: parent_name, attr_name = name.rsplit('.', 1) parent = model.get_submodule(parent_name) else: parent = model attr_name = name fp8_locations.append((parent, attr_name, module)) if not fp8_locations: yield # No FP8 modules, nothing to do return # Swap Float8Linear -> nn.Linear (shares the same weight tensor, no copy) for parent, attr_name, fp8_module in fp8_locations: linear = nn.Linear( fp8_module.in_features, fp8_module.out_features, bias=fp8_module.bias is not None, device=fp8_module.weight.device, dtype=fp8_module.weight.dtype, ) linear.weight = fp8_module.weight # share, don't copy if fp8_module.bias is not None: linear.bias = fp8_module.bias setattr(parent, attr_name, linear) try: yield finally: # Restore Float8Linear modules for parent, attr_name, fp8_module in fp8_locations: setattr(parent, attr_name, fp8_module) # ----------------------------------------------------------------------------- # Compile the model orig_model = model # original, uncompiled model, for saving raw model state_dict and for inference/evaluation (because the shapes may change shape) model = torch.compile(model, dynamic=False) # the inputs to model will never change shape so dynamic=False is safe # ----------------------------------------------------------------------------- # Scaling laws and muP extrapolations to determine the optimal training horizon, batch size, learning rates, weight decay. # Get the parameter counts of our model param_counts = model.num_scaling_params() print0(f"Parameter counts:") for key, value in param_counts.items(): print0(f"{key:24s}: {value:,}") num_params = param_counts['total'] num_flops_per_token = model.estimate_flops() print0(f"Estimated FLOPs per token: {num_flops_per_token:e}") # 1) Use scaling laws to determine the optimal training horizon in tokens # The compute-optimal models satisfy the Tokens:Params ratio of --target-param-data-ratio (derived experimentally via scaling laws analysis). # We've already initialized the model so we have Params. Optimal Tokens is now simply target-param-data-ratio * Params def get_scaling_params(m): # As for which params to use exactly, transformer matrices + lm_head gives cleanest scaling laws (see dev/LOG.md Jan 27, 2026) params_counts = m.num_scaling_params() scaling_params = params_counts['transformer_matrices'] + params_counts['lm_head'] return scaling_params num_scaling_params = get_scaling_params(model) target_tokens = int(args.target_param_data_ratio * num_scaling_params) # optimal tokens for the model we are about to train # Our reference model is d12, this is where a lot of hyperparameters are tuned and then transfered to higher depths (muP style) d12_ref = build_model_meta(12) # creates the model on meta device D_REF = args.target_param_data_ratio * get_scaling_params(d12_ref) # compute-optimal d12 training horizon in tokens (measured empirically) B_REF = 2**19 # optimal batch size at d12 ~= 524,288 tokens (measured empirically) # 2) Now that we have the token horizon, we can calculate the optimal batch size # We follow the Power Lines paper (Bopt ∝ D^0.383), ref: https://arxiv.org/abs/2505.13738 # The optimal batch size grows as approximately D^0.383, so e.g. if D doubles from d12 to d24, B should grow by 2^0.383 β‰ˆ 1.3x. total_batch_size = args.total_batch_size # user-provided override is possible if total_batch_size == -1: batch_size_ratio = target_tokens / D_REF predicted_batch_size = B_REF * batch_size_ratio ** 0.383 total_batch_size = 2 ** round(math.log2(predicted_batch_size)) # clamp to nearest power of 2 for efficiency print0(f"Auto-computed optimal batch size: {total_batch_size:,} tokens") # 3) Knowing the batch size, we can now calculate a learning rate correction (bigger batch size allows higher learning rates) batch_lr_scale = 1.0 batch_ratio = total_batch_size / B_REF # B/B_ref if batch_ratio != 1.0: # SGD: linear scaling with batch size is standard (not used in nanochat) # AdamW: sqrt scaling is standard: Ξ· ∝ √(B/B_ref) # Muon: we will use the same scaling for Muon as for AdamW: Ξ· ∝ √(B/B_ref) (not studied carefully, assumption!) batch_lr_scale = batch_ratio ** 0.5 # Ξ· ∝ √(B/B_ref) print0(f"Scaling LRs by {batch_lr_scale:.4f} for batch size {total_batch_size:,} (reference: {B_REF:,})") # 4) Knowing the batch size and the token horizon, we can now calculate the appropriate weight decay scaling # We adopt the T_epoch framework from https://arxiv.org/abs/2405.13698 # Central idea of the paper is that T_epoch = B/(η·λ·D) should remain constant. # Above, we used learning rate scaling Ξ· ∝ √(B/B_ref). So it's a matter of ~10 lines of math to derive that to keep T_epoch constant, we need: # Ξ» = Ξ»_ref Β· √(B/B_ref) Β· (D_ref/D) # Note that these papers study AdamW, *not* Muon. We are blindly following AdamW theory for scaling hoping it ~works for Muon too. weight_decay_scaled = args.weight_decay * math.sqrt(total_batch_size / B_REF) * (D_REF / target_tokens) if weight_decay_scaled != args.weight_decay: print0(f"Scaling weight decay from {args.weight_decay:.6f} to {weight_decay_scaled:.6f} for depth {args.depth}") # ----------------------------------------------------------------------------- # Initialize the Optimizer (combined MuonAdamW: Muon for matrix params, AdamW for rest) optimizer = model.setup_optimizer( # AdamW hyperparameters unembedding_lr=args.unembedding_lr * batch_lr_scale, embedding_lr=args.embedding_lr * batch_lr_scale, scalar_lr=args.scalar_lr * batch_lr_scale, adam_betas=(args.adam_beta1, args.adam_beta2), # Muon hyperparameters matrix_lr=args.matrix_lr * batch_lr_scale, weight_decay=weight_decay_scaled, ) if resuming: optimizer.load_state_dict(optimizer_data) del optimizer_data # ----------------------------------------------------------------------------- # Initialize the DataLoaders for train/val dataloader_resume_state_dict = None if not resuming else meta_data["dataloader_state_dict"] train_loader = tokenizing_distributed_data_loader_with_state_bos_bestfit(tokenizer, args.device_batch_size, args.max_seq_len, split="train", device=device, resume_state_dict=dataloader_resume_state_dict) build_val_loader = lambda: tokenizing_distributed_data_loader_bos_bestfit(tokenizer, args.device_batch_size, args.max_seq_len, split="val", device=device) x, y, dataloader_state_dict = next(train_loader) # kick off load of the very first batch of data # ----------------------------------------------------------------------------- # Calculate the number of iterations we will train for and set up the various schedulers # num_iterations: either it is given, or from target flops, or from target data:param ratio (in that order) assert args.num_iterations > 0 or args.target_param_data_ratio > 0 or args.target_flops > 0 if args.num_iterations > 0: # Override num_iterations to a specific value if given num_iterations = args.num_iterations print0(f"Using user-provided number of iterations: {num_iterations:,}") elif args.target_flops > 0: # Calculate the number of iterations from the target flops (used in scaling laws analysis, e.g. runs/scaling_laws.sh) num_iterations = round(args.target_flops / (num_flops_per_token * total_batch_size)) print0(f"Calculated number of iterations from target FLOPs: {num_iterations:,}") elif args.target_param_data_ratio > 0: # Calculate the number of iterations from the target param data ratio (the most common use case) num_iterations = target_tokens // total_batch_size print0(f"Calculated number of iterations from target data:param ratio: {num_iterations:,}") else: raise ValueError("No training horizon specified") total_tokens = total_batch_size * num_iterations # the actual number of tokens we will train for print0(f"Total number of training tokens: {total_tokens:,}") print0(f"Tokens : Scaling params ratio: {total_batch_size * num_iterations / num_scaling_params:.2f}") # e.g. Chinchilla was ~20 print0(f"Total training FLOPs estimate: {num_flops_per_token * total_tokens:e}") # Learning rate schedule (linear warmup, constant, linear warmdown) def get_lr_multiplier(it): warmup_iters = round(args.warmup_ratio * num_iterations) warmdown_iters = round(args.warmdown_ratio * num_iterations) if it < warmup_iters: return (it + 1) / warmup_iters elif it <= num_iterations - warmdown_iters: return 1.0 else: progress = (num_iterations - it) / warmdown_iters return progress * 1.0 + (1 - progress) * args.final_lr_frac # Momentum scheduler for Muon optimizer (warms up to 0.95 over the first 300 steps) def get_muon_momentum(it): frac = min(it / 300, 1) momentum = (1 - frac) * 0.85 + frac * 0.95 return momentum # Weight decay scheduler for Muon optimizer (linearly decays to zero over the course of training) def get_weight_decay(it): return weight_decay_scaled * (1 - it / num_iterations) # ----------------------------------------------------------------------------- # Training loop # Loop state (variables updated by the training loop) if not resuming: step = 0 val_bpb = None # will be set if eval_every > 0 min_val_bpb = float("inf") smooth_train_loss = 0 # EMA of training loss total_training_time = 0 # total wall-clock time of training else: step = meta_data["step"] loop_state = meta_data["loop_state"] val_bpb = meta_data["val_bpb"] min_val_bpb = loop_state["min_val_bpb"] smooth_train_loss = loop_state["smooth_train_loss"] total_training_time = loop_state["total_training_time"] # Figure out the needed gradient accumulation micro-steps to reach the desired total batch size per step tokens_per_fwdbwd = args.device_batch_size * args.max_seq_len # tokens per iteration for a single rank world_tokens_per_fwdbwd = tokens_per_fwdbwd * ddp_world_size # total tokens per iteration for all ranks assert total_batch_size % world_tokens_per_fwdbwd == 0 grad_accum_steps = total_batch_size // world_tokens_per_fwdbwd print0(f"Tokens / micro-batch / rank: {args.device_batch_size} x {args.max_seq_len} = {tokens_per_fwdbwd:,}") print0(f"Tokens / micro-batch: {world_tokens_per_fwdbwd:,}") print0(f"Total batch size {total_batch_size:,} => gradient accumulation steps: {grad_accum_steps}") # Go! while True: last_step = step == num_iterations # loop runs num_iterations+1 times so that we can eval/save at the end flops_so_far = num_flops_per_token * total_batch_size * step # once in a while: evaluate the val bpb (all ranks participate) if args.eval_every > 0 and (last_step or step % args.eval_every == 0): model.eval() val_loader = build_val_loader() eval_steps = args.eval_tokens // (args.device_batch_size * args.max_seq_len * ddp_world_size) with disable_fp8(model), autocast_ctx: val_bpb = evaluate_bpb(model, val_loader, eval_steps, token_bytes) print0(f"Step {step:05d} | Validation bpb: {val_bpb:.6f}") if val_bpb < min_val_bpb: min_val_bpb = val_bpb wandb_run.log({ "step": step, "total_training_flops": flops_so_far, "total_training_time": total_training_time, "val/bpb": val_bpb, }) model.train() # once in a while: estimate the CORE metric (all ranks participate) # use the original uncompiled model because the inputs keep changing shape # disable FP8 for evaluation to use BF16 for more consistent/accurate results results = {} if args.core_metric_every > 0 and (last_step or (step > 0 and step % args.core_metric_every == 0)): model.eval() with disable_fp8(orig_model), autocast_ctx: results = evaluate_core(orig_model, tokenizer, device, max_per_task=args.core_metric_max_per_task) print0(f"Step {step:05d} | CORE metric: {results['core_metric']:.4f}") wandb_run.log({ "step": step, "total_training_flops": flops_so_far, "core_metric": results["core_metric"], "centered_results": results["centered_results"], }) model.train() # once in a while: sample from the model (only on master process) # use the original uncompiled model because the inputs keep changing shape if args.sample_every > 0 and master_process and (last_step or (step > 0 and step % args.sample_every == 0)): model.eval() prompts = [ "The capital of France is", "The chemical symbol of gold is", "If yesterday was Friday, then tomorrow will be", "The opposite of hot is", "The planets of the solar system are:", "My favorite color is", "If 5*x + 3 = 13, then x is", ] engine = Engine(orig_model, tokenizer) # use orig_model to avoid recompilation for prompt in prompts: tokens = tokenizer(prompt, prepend="<|bos|>") with disable_fp8(orig_model), autocast_ctx: sample, _ = engine.generate_batch(tokens, num_samples=1, max_tokens=16, temperature=0) print0(tokenizer.decode(sample[0])) model.train() # save checkpoint: at the end of the run, or every save_every steps, except at the first step or the resume step if last_step or (step > 0 and step != args.resume_from_step and args.save_every > 0 and step % args.save_every == 0): save_checkpoint( checkpoint_dir, step, orig_model.state_dict(), # model parameters optimizer.state_dict(), # optimizer state { # metadata saved as json "step": step, "val_bpb": val_bpb, # loss at last step "model_config": model_config_kwargs, "user_config": user_config, # inputs to the training script "device_batch_size": args.device_batch_size, "max_seq_len": args.max_seq_len, "total_batch_size": total_batch_size, "dataloader_state_dict": dataloader_state_dict, "loop_state": { # all loop state (other than step) so that we can resume training "min_val_bpb": min_val_bpb, "smooth_train_loss": smooth_train_loss, "total_training_time": total_training_time, }, }, rank=ddp_rank, ) # termination conditions (TODO: possibly also add loss explosions etc.) if last_step: break # ------------------------------------------------------------------------- # single training step # evaluate the gradient synchronize() t0 = time.time() for micro_step in range(grad_accum_steps): with autocast_ctx: loss = model(x, y) train_loss = loss.detach() # for logging loss = loss / grad_accum_steps # each .backward() is a grad sum => normalize loss here loss.backward() x, y, dataloader_state_dict = next(train_loader) # prefetch the next batch while the GPU is busy with forward/backward # step the optimizer lrm = get_lr_multiplier(step) muon_momentum = get_muon_momentum(step) muon_weight_decay = get_weight_decay(step) for group in optimizer.param_groups: group["lr"] = group["initial_lr"] * lrm if group['kind'] == 'muon': group["momentum"] = muon_momentum group["weight_decay"] = muon_weight_decay optimizer.step() model.zero_grad(set_to_none=True) train_loss_f = train_loss.item() # .item() is a CPU-GPU sync point synchronize() t1 = time.time() dt = t1 - t0 # ------------------------------------------------------------------------- # logging (CPU action only) ema_beta = 0.9 # EMA decay factor for some smoothing just for nicer logging smooth_train_loss = ema_beta * smooth_train_loss + (1 - ema_beta) * train_loss_f # EMA the training loss debiased_smooth_loss = smooth_train_loss / (1 - ema_beta**(step + 1)) # debias the EMA pct_done = 100 * step / num_iterations tok_per_sec = int(total_batch_size / dt) flops_per_sec = num_flops_per_token * total_batch_size / dt mfu = 100 * flops_per_sec / (gpu_peak_flops * ddp_world_size) if step > 10: total_training_time += dt # only count the time after the first 10 steps # Calculate ETA based on average time per step (excluding first 10 steps) steps_done = step - 10 if steps_done > 0: avg_time_per_step = total_training_time / steps_done remaining_steps = num_iterations - step eta_seconds = remaining_steps * avg_time_per_step eta_str = f" | eta: {eta_seconds/60:.1f}m" else: eta_str = "" epoch = dataloader_state_dict["epoch"] print0(f"step {step:05d}/{num_iterations:05d} ({pct_done:.2f}%) | loss: {debiased_smooth_loss:.6f} | lrm: {lrm:.2f} | dt: {dt * 1000:.2f}ms | tok/sec: {tok_per_sec:,} | bf16_mfu: {mfu:.2f} | epoch: {epoch} | total time: {total_training_time/60:.2f}m{eta_str}") if step % 100 == 0: log_data = { "step": step, "total_training_flops": flops_so_far, "total_training_time": total_training_time, "train/loss": debiased_smooth_loss, "train/lrm": lrm, "train/dt": dt, "train/tok_per_sec": tok_per_sec, "train/mfu": mfu, "train/epoch": epoch, } wandb_run.log(log_data) # state update first_step_of_run = (step == 0) or (resuming and step == args.resume_from_step) step += 1 # The garbage collector is sadly a little bit overactive and for some poorly understood reason, # it spends ~500ms scanning for cycles quite frequently, just to end up cleaning up very few tiny objects each time. # So we manually manage and help it out here if first_step_of_run: gc.collect() # manually collect a lot of garbage from setup gc.freeze() # immediately freeze all currently surviving objects and exclude them from GC gc.disable() # nuclear intervention here: disable GC entirely except: elif step % 5000 == 0: # every 5000 steps... gc.collect() # manually collect, just to be safe for very, very long runs # print a few more stats print0(f"Peak memory usage: {get_max_memory() / 1024 / 1024:.2f}MiB") print0(f"Total training time: {total_training_time/60:.2f}m") if val_bpb is not None: print0(f"Minimum validation bpb: {min_val_bpb:.6f}") # Log to report from nanochat.report import get_report get_report().log(section="Base model training", data=[ user_config, # CLI args { # stats about the training setup "Number of parameters": num_params, "Number of FLOPs per token": f"{num_flops_per_token:e}", "Calculated number of iterations": num_iterations, "Number of training tokens": total_tokens, "Tokens : Scaling params ratio": total_batch_size * num_iterations / num_scaling_params, "DDP world size": ddp_world_size, "warmup_ratio": args.warmup_ratio, "warmdown_ratio": args.warmdown_ratio, "final_lr_frac": args.final_lr_frac, }, { # stats about training outcomes "Minimum validation bpb": min_val_bpb if val_bpb is not None else None, "Final validation bpb": val_bpb, "CORE metric estimate": results.get("core_metric", None), "MFU %": f"{mfu:.2f}%", "Total training flops": f"{flops_so_far:e}", "Total training time": f"{total_training_time/60:.2f}m", "Peak memory usage": f"{get_max_memory() / 1024 / 1024:.2f}MiB", } ]) # cleanup wandb_run.finish() # wandb run finish compute_cleanup()
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/base_train.py", "license": "MIT License", "lines": 541, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/chat_cli.py
""" New and upgraded chat mode because a lot of the code has changed since the last one. Intended to be run single GPU only atm: python -m scripts.chat_cli """ import argparse import torch from nanochat.common import compute_init, autodetect_device_type from contextlib import nullcontext from nanochat.engine import Engine from nanochat.checkpoint_manager import load_model parser = argparse.ArgumentParser(description='Chat with the model') parser.add_argument('-i', '--source', type=str, default="sft", help="Source of the model: sft|rl") parser.add_argument('-g', '--model-tag', type=str, default=None, help='Model tag to load') parser.add_argument('-s', '--step', type=int, default=None, help='Step to load') parser.add_argument('-p', '--prompt', type=str, default='', help='Prompt the model, get a single response back') parser.add_argument('-t', '--temperature', type=float, default=0.6, help='Temperature for generation') parser.add_argument('-k', '--top-k', type=int, default=50, help='Top-k sampling parameter') parser.add_argument('--device-type', type=str, default='', choices=['cuda', 'cpu', 'mps'], help='Device type for evaluation: cuda|cpu|mps. empty => autodetect') parser.add_argument('-d', '--dtype', type=str, default='bfloat16', choices=['float32', 'bfloat16']) args = parser.parse_args() # Init the model and tokenizer device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) ptdtype = torch.float32 if args.dtype == 'float32' else torch.bfloat16 autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) if device_type == "cuda" else nullcontext() model, tokenizer, meta = load_model(args.source, device, phase="eval", model_tag=args.model_tag, step=args.step) # Special tokens for the chat state machine bos = tokenizer.get_bos_token_id() user_start, user_end = tokenizer.encode_special("<|user_start|>"), tokenizer.encode_special("<|user_end|>") assistant_start, assistant_end = tokenizer.encode_special("<|assistant_start|>"), tokenizer.encode_special("<|assistant_end|>") # Create Engine for efficient generation engine = Engine(model, tokenizer) print("\nNanoChat Interactive Mode") print("-" * 50) print("Type 'quit' or 'exit' to end the conversation") print("Type 'clear' to start a new conversation") print("-" * 50) conversation_tokens = [bos] while True: if args.prompt: # Get the prompt from the launch command user_input = args.prompt else: # Get the prompt interactively from the console try: user_input = input("\nUser: ").strip() except (EOFError, KeyboardInterrupt): print("\nGoodbye!") break # Handle special commands if user_input.lower() in ['quit', 'exit']: print("Goodbye!") break if user_input.lower() == 'clear': conversation_tokens = [bos] print("Conversation cleared.") continue if not user_input: continue # Add User message to the conversation conversation_tokens.append(user_start) conversation_tokens.extend(tokenizer.encode(user_input)) conversation_tokens.append(user_end) # Kick off the assistant conversation_tokens.append(assistant_start) generate_kwargs = { "num_samples": 1, "max_tokens": 256, "temperature": args.temperature, "top_k": args.top_k, } response_tokens = [] print("\nAssistant: ", end="", flush=True) with autocast_ctx: for token_column, token_masks in engine.generate(conversation_tokens, **generate_kwargs): token = token_column[0] # pop the batch dimension (num_samples=1) response_tokens.append(token) token_text = tokenizer.decode([token]) print(token_text, end="", flush=True) print() # we have to ensure that the assistant end token is the last token # so even if generation ends due to max tokens, we have to append it to the end if response_tokens[-1] != assistant_end: response_tokens.append(assistant_end) conversation_tokens.extend(response_tokens) # In the prompt mode, we only want a single response and exit if args.prompt: break
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/chat_cli.py", "license": "MIT License", "lines": 89, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/chat_eval.py
""" Evaluate the Chat model. All the generic code lives here, and all the evaluation-specific code lives in nanochat directory and is imported from here. Example runs: python -m scripts.chat_eval -a ARC-Easy torchrun --nproc_per_node=8 -m scripts.chat_eval -- -a ARC-Easy """ import argparse from functools import partial from contextlib import nullcontext import torch import torch.distributed as dist from nanochat.common import compute_init, compute_cleanup, get_dist_info, print0, autodetect_device_type from nanochat.checkpoint_manager import load_model from nanochat.engine import Engine from tasks.humaneval import HumanEval from tasks.mmlu import MMLU from tasks.arc import ARC from tasks.gsm8k import GSM8K from tasks.spellingbee import SpellingBee # ----------------------------------------------------------------------------- # Generative evaluation loop (we go one problem at a time, sample, evaluate) def run_generative_eval(task_object, tokenizer, model, engine, num_samples, max_new_tokens, temperature, top_k, max_problems=None): ddp, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info() device = model.get_device() num_problems = len(task_object) if max_problems is None else min(len(task_object), max_problems) # Run the evaluation num_passed, total = 0, 0 for i in range(ddp_rank, num_problems, ddp_world_size): conversation = task_object[i] # Tokenize the prompt encoded_prompt = tokenizer.render_for_completion(conversation) # Get the completions results, _ = engine.generate_batch( encoded_prompt, num_samples=num_samples, max_tokens=max_new_tokens, temperature=temperature, top_k=top_k, ) # Decode the completions as text prefix_length = len(encoded_prompt) completions = [tokenizer.decode(result_tokens[prefix_length:]) for result_tokens in results] # Evaluate success criteria outcomes = [task_object.evaluate(conversation, completion) for completion in completions] passed = any(outcomes) # Keep stats total += 1 num_passed += int(passed) # Logging (overwrite the same line in the console) print(f"\r\033[KRank {ddp_rank} | {num_passed}/{total} ({100*num_passed/total:.2f}%)", end='', flush=True) # Finish the in-place progress line with a newline before final summary print() # Aggregate results across all ranks if ddp: num_passed_tensor = torch.tensor([num_passed], dtype=torch.long, device=device) total_tensor = torch.tensor([total], dtype=torch.long, device=device) dist.all_reduce(num_passed_tensor, op=dist.ReduceOp.SUM) dist.all_reduce(total_tensor, op=dist.ReduceOp.SUM) num_passed = num_passed_tensor.item() total = total_tensor.item() print0("=" * 50) print0(f"Final: {num_passed}/{total} ({100*num_passed/total:.2f}%)") # Return the accuracy return num_passed/total # ----------------------------------------------------------------------------- # Categorical evaluation loop # A lot easier because we don't have to sample. Therefore, we can actually go # batches at a time and just check the logits for correct answer choices. def run_categorical_eval(task_object, tokenizer, model, batch_size, max_problems=None): ddp, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info() device = model.get_device() bos = tokenizer.get_bos_token_id() # use BOS as pad token is ok, these positions are ignored # We'll process batches of independent problems at a time because there is no sampling needed num_problems = len(task_object) if max_problems is None else min(len(task_object), max_problems) ceil_div = lambda x, y: -(-x // y) num_batches = ceil_div(num_problems, batch_size) # Run the evaluation letter_to_id_cache = {} # many letters will repeat often, let's save the tokenizer some work num_passed, total = 0, 0 for i in range(ddp_rank, num_batches, ddp_world_size): i0, i1 = i * batch_size, min((i + 1) * batch_size, num_problems) # Prepare the batch of problems. They might all be of different length, so we pad/collate them. conversations = [task_object[ii] for ii in range(i0, i1)] prompt_ids = [tokenizer.render_for_completion(conversation) for conversation in conversations] # TODO: remake the way this works max_length = max(len(ids) for ids in prompt_ids) answer_time_positions = [len(ids) - 1 for ids in prompt_ids] # where the last token is (and the predicted answer) padded_prompt_ids = [ids + [bos] * (max_length - len(ids)) for ids in prompt_ids] prompt_ids = torch.tensor(padded_prompt_ids, dtype=torch.long, device=device) # Get the logits for the whole batch of conversations in parallel (efficiency win here) with torch.no_grad(): logits = model(prompt_ids) # (B, T, V) # Focus on the available answer on just the letters corresponding to choices # Note that this helps the evaluation a lot because it specifically narrows the focus to only the available letters # The much harder alternative would be to just generate from the Assistant and check if it responded with the correct # letter (e.g. A, B, C, D), but evaluations typically make the task easier in this way. for idx, conversation in enumerate(conversations): # get the token ids of all the available letters of this problem letters = conversation['letters'] letter_ids = [] for letter in letters: if not letter in letter_to_id_cache: encoded_letter = tokenizer.encode(letter) assert len(encoded_letter) == 1, "Each letter must be a single token" letter_to_id_cache[letter] = encoded_letter[0] letter_ids.append(letter_to_id_cache[letter]) # focus logits just down to the answer position and the available letters of the answer answer_pos = answer_time_positions[idx] focus_logits = logits[idx, answer_pos, letter_ids] # get the argmax letter (the predicted answer) argmax_letter_id = focus_logits.argmax(dim=-1).item() predicted_letter = letters[argmax_letter_id] # evaluate the outcome outcome = task_object.evaluate(conversation, predicted_letter) num_passed += int(outcome) total += 1 # Aggregate results across all ranks if ddp: num_passed_tensor = torch.tensor([num_passed], dtype=torch.long, device=device) total_tensor = torch.tensor([total], dtype=torch.long, device=device) dist.all_reduce(num_passed_tensor, op=dist.ReduceOp.SUM) dist.all_reduce(total_tensor, op=dist.ReduceOp.SUM) num_passed = num_passed_tensor.item() total = total_tensor.item() average = num_passed/total print0(f"Final: {num_passed}/{total} ({100*average:.2f}%)") return average # ----------------------------------------------------------------------------- def run_chat_eval(task_name, model, tokenizer, engine, batch_size=1, num_samples=1, max_new_tokens=512, temperature=0.0, top_k=50, max_problems=None): # Create the evaluation object task_module = { 'HumanEval': HumanEval, 'MMLU': partial(MMLU, subset="all", split="test"), 'ARC-Easy': partial(ARC, subset="ARC-Easy", split="test"), 'ARC-Challenge': partial(ARC, subset="ARC-Challenge", split="test"), 'GSM8K': partial(GSM8K, subset="main", split="test"), 'SpellingBee': partial(SpellingBee, size=256, split="test"), }[task_name] task_object = task_module() # Run the evaluation if task_object.eval_type == 'generative': acc = run_generative_eval(task_object, tokenizer, model, engine, num_samples, max_new_tokens, temperature, top_k, max_problems=max_problems) elif task_object.eval_type == 'categorical': acc = run_categorical_eval(task_object, tokenizer, model, batch_size, max_problems=max_problems) else: raise ValueError(f"Unsupported task evaluation type: {task_object.eval_type}") return acc # ----------------------------------------------------------------------------- if __name__ == "__main__": # Parse command-line arguments parser = argparse.ArgumentParser() parser.add_argument('-i', '--source', type=str, required=True, help="Source of the model: sft|rl") parser.add_argument('-a', '--task-name', type=str, default=None, help="Task name. Default = all tasks. Use | to split multiple tasks.") parser.add_argument('-d', '--dtype', type=str, default='bfloat16', choices=['float32', 'bfloat16']) parser.add_argument('-t', '--temperature', type=float, default=0.0) parser.add_argument('-m', '--max-new-tokens', type=int, default=512) parser.add_argument('-n', '--num-samples', type=int, default=1) parser.add_argument('-k', '--top-k', type=int, default=50) parser.add_argument('-b', '--batch-size', type=int, default=8, help='Batch size for categorical evaluation') parser.add_argument('-g', '--model-tag', type=str, default=None, help='Model tag to load') parser.add_argument('-s', '--step', type=int, default=None, help='Step to load') parser.add_argument('-x', '--max-problems', type=int, default=None, help='Max problems to evaluate') parser.add_argument('--device-type', type=str, default='', choices=['cuda', 'cpu', 'mps'], help='Device type for evaluation: cuda|cpu|mps. empty => autodetect') args = parser.parse_args() device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) ptdtype = torch.float32 if args.dtype == 'float32' else torch.bfloat16 autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) if device_type == "cuda" else nullcontext() model, tokenizer, meta = load_model(args.source, device, phase="eval", model_tag=args.model_tag, step=args.step) engine = Engine(model, tokenizer) # Get the tasks to evaluate on all_tasks = ['ARC-Easy', 'ARC-Challenge', 'MMLU', 'GSM8K', 'HumanEval', 'SpellingBee'] baseline_accuracies = { 'ARC-Easy': 0.25, # multiple choice 1 of 4 => 25% 'ARC-Challenge': 0.25, # multiple choice 1 of 4 => 25% 'MMLU': 0.25, # multiple choice 1 of 4 => 25% 'GSM8K': 0.0, # open-ended => 0% 'HumanEval': 0.0, # open-ended => 0% 'SpellingBee': 0.0, # open-ended => 0% } task_names = all_tasks if args.task_name is None else args.task_name.split('|') # Run all the task evaluations sequentially results = {} for task_name in task_names: with autocast_ctx: acc = run_chat_eval( task_name, model, tokenizer, engine, batch_size=args.batch_size, num_samples=args.num_samples, max_new_tokens=args.max_new_tokens, temperature=args.temperature, top_k=args.top_k, max_problems=args.max_problems, ) results[task_name] = acc print0(f"{task_name} accuracy: {100 * acc:.2f}%") # Log to report from nanochat.report import get_report all_tasks_were_evaluated = all(task_name in results for task_name in all_tasks) # calculate the ChatCORE metric if we can (similar to CORE, it's the mean centered accuracy) # this way, ChatCORE ranges from 0 (at random baseline) to 1 (peak performance) chatcore_metric_dict = {} if all_tasks_were_evaluated: centered_mean = 0 for task_name, acc in results.items(): baseline_acc = baseline_accuracies.get(task_name, 0.0) centered_acc = (acc - baseline_acc) / (1.0 - baseline_acc) centered_mean += centered_acc chatcore_metric = centered_mean / len(results) chatcore_metric_dict = {"ChatCORE metric": chatcore_metric} get_report().log(section="Chat evaluation " + args.source, data=[ vars(args), # CLI args results, chatcore_metric_dict, ]) compute_cleanup()
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/chat_eval.py", "license": "MIT License", "lines": 220, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/chat_rl.py
""" Reinforcement learning on GSM8K via "GRPO". I put GRPO in quotes because we actually end up with something a lot simpler and more similar to just REINFORCE: 1) Delete trust region, so there is no KL regularization to a reference model 2) We are on policy, so there's no need for PPO ratio+clip. 3) We use DAPO style normalization that is token-level, not sequence-level. 4) Instead of z-score normalization (r - mu)/sigma, only use (r - mu) as the advantage. 1 GPU: python -m scripts.chat_rl 8 GPUs: torchrun --standalone --nproc_per_node=8 -m scripts.chat_rl -- --run=default """ import argparse import os import itertools import wandb import torch import torch.distributed as dist from contextlib import nullcontext from nanochat.common import compute_init, compute_cleanup, print0, get_base_dir, DummyWandb, autodetect_device_type from nanochat.checkpoint_manager import save_checkpoint, load_model from nanochat.engine import Engine from tasks.gsm8k import GSM8K # ----------------------------------------------------------------------------- # CLI arguments parser = argparse.ArgumentParser(description="Reinforcement learning on GSM8K") # Logging parser.add_argument("--run", type=str, default="dummy", help="wandb run name ('dummy' disables wandb logging)") # Runtime parser.add_argument("--device-type", type=str, default="", help="cuda|cpu|mps (empty = autodetect)") parser.add_argument("--dtype", type=str, default="bfloat16", help="float32|bfloat16") # Model loading parser.add_argument("--model-tag", type=str, default=None, help="model tag to load from") parser.add_argument("--model-step", type=int, default=None, help="model step to load from") # Training horizon parser.add_argument("--num-epochs", type=int, default=1, help="number of epochs over GSM8K") # Batch sizes / sampling parser.add_argument("--device-batch-size", type=int, default=8, help="max batch size per forward pass") parser.add_argument("--examples-per-step", type=int, default=16, help="total examples per optimization step across all ranks") parser.add_argument("--num-samples", type=int, default=16, help="number of samples per example/question") # Generation parser.add_argument("--max-new-tokens", type=int, default=256, help="max tokens to generate per sample") parser.add_argument("--temperature", type=float, default=1.0, help="sampling temperature") parser.add_argument("--top-k", type=int, default=50, help="top-k sampling (0 = disabled)") # Optimization parser.add_argument("--embedding-lr", type=float, default=0.2, help="learning rate for embedding parameters (Adam)") parser.add_argument("--unembedding-lr", type=float, default=0.004, help="learning rate for unembedding parameters (Adam)") parser.add_argument("--matrix-lr", type=float, default=0.02, help="learning rate for matrix parameters (Muon)") parser.add_argument("--weight-decay", type=float, default=0.0, help="weight decay for embedding/unembedding parameters (Adam)") parser.add_argument("--init-lr-frac", type=float, default=0.05, help="initial LR as fraction of base LR") # Evaluation / checkpointing parser.add_argument("--eval-every", type=int, default=60, help="evaluate pass@k every N steps") parser.add_argument("--eval-examples", type=int, default=400, help="number of examples for pass@k evaluation") parser.add_argument("--save-every", type=int, default=60, help="save checkpoint every N steps") args = parser.parse_args() user_config = vars(args).copy() # ----------------------------------------------------------------------------- # Init compute/precision device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) master_process = ddp_rank == 0 # this process will do logging, checkpointing etc. ptdtype = torch.float32 if args.dtype == 'float32' else torch.bfloat16 autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) if device_type == "cuda" else nullcontext() # wandb logging init use_dummy_wandb = args.run == "dummy" or not master_process wandb_run = DummyWandb() if use_dummy_wandb else wandb.init(project="nanochat-rl", name=args.run, config=user_config) # Init model and tokenizer model, tokenizer, meta = load_model("sft", device, phase="eval", model_tag=args.model_tag, step=args.model_step) engine = Engine(model, tokenizer) # for sampling rollouts # ----------------------------------------------------------------------------- # Rollout / sampling generator loop that yields batches of examples for training train_task = GSM8K(subset="main", split="train") val_task = GSM8K(subset="main", split="test") num_steps = (len(train_task) // args.examples_per_step) * args.num_epochs print0(f"Calculated number of steps: {num_steps}") @torch.no_grad() def get_batch(): assistant_end = tokenizer.encode_special("<|assistant_end|>") # ok to use this token, it's only for padding and isn't used in the loss. rank_indices = range(ddp_rank, len(train_task), ddp_world_size) # each rank is responsible for different examples in the training data for example_idx in itertools.cycle(rank_indices): # First get the full conversation of both user and assistant messages conversation = train_task[example_idx] # Tokenize the conversation, deleting the last Assistant message and priming the Assistant for a completion instead # (i.e. keep the <|assistant_start|>, but delete everything after it) tokens = tokenizer.render_for_completion(conversation) prefix_length = len(tokens) # Generate num_samples samples using batched generation, use loop to avoid OOMs model.eval() # ensure the model is in eval mode generated_token_sequences = [] masks = [] num_sampling_steps = args.num_samples // args.device_batch_size # go sequentially to prevent OOMs for sampling_step in range(num_sampling_steps): seed = hash((step, example_idx, sampling_step)) & 0x7FFFFFFF # positive half of int32 with autocast_ctx: generated_token_sequences_batch, masks_batch = engine.generate_batch( tokens, num_samples=args.device_batch_size, max_tokens=args.max_new_tokens, temperature=args.temperature, top_k=args.top_k, seed=seed, # must make sure to change the seed for each sampling step ) generated_token_sequences.extend(generated_token_sequences_batch) masks.extend(masks_batch) # Calculate the rewards for each sample rewards = [] for sample_tokens in generated_token_sequences: # Get just the generated tokens (after the prompt) generated_tokens = sample_tokens[prefix_length:] # Decode the generated response generated_text = tokenizer.decode(generated_tokens) # Calculate the reward reward = train_task.reward(conversation, generated_text) rewards.append(reward) # Pad the sequences so that their lengths (in time) match max_length = max(len(seq) for seq in generated_token_sequences) padded_generated_token_sequences = [seq + [assistant_end] * (max_length - len(seq)) for seq in generated_token_sequences] padded_masks = [mask + [0] * (max_length - len(mask)) for mask in masks] # Stack up the sequences and masks into PyTorch tensors ids = torch.tensor(padded_generated_token_sequences, dtype=torch.long, device=device) mask_ids = torch.tensor(padded_masks, dtype=torch.long, device=device) # Generate autoregressive inputs and targets to the Transformer inputs = ids[:, :-1] targets = ids[:, 1:].clone() # clone to avoid in-place modification: targets[mask_ids[:, 1:] == 0] = -1 # <-- inplace modification right here. -1 is the ignore index # NOTE also that the Engine returns mask=0 for BOTH the prompt tokens AND the tool use tokens. # So we will (correctly) end up not training on the prompt tokens, or the tool use forced tokens. rewards = torch.tensor(rewards, dtype=torch.float, device=device) # Calculate the advantages by simply subtracting the mean (instead of z-score (x-mu)/sigma) mu = rewards.mean() advantages = rewards - mu # yield inputs/targets as (B, T) of ids and rewards as (B,) of floats yield generated_token_sequences, inputs, targets, rewards, advantages # ----------------------------------------------------------------------------- # Simple evaluation loop for GSM8K pass@k def run_gsm8k_eval(task, tokenizer, engine, max_examples=None, num_samples=1, max_completion_tokens=256, temperature=0.0, top_k=50 ): """ Evaluates GSM8K task and returns a list of records of evaluation outcomes. In a distributed setting, all ranks cooperate but this function will NOT do the reduction across ranks. This is the responsibility of the caller. Because the evaluation can take a while, this function will yield records one by one. """ max_examples = min(max_examples, len(task)) if max_examples is not None else len(task) for idx in range(ddp_rank, max_examples, ddp_world_size): conversation = task[idx] tokens = tokenizer.render_for_completion(conversation) prefix_length = len(tokens) # Generate k samples using batched generation inside the Engine assert num_samples <= args.device_batch_size # usually this is true. we can add a loop if not... generated_token_sequences, masks = engine.generate_batch( tokens, num_samples=num_samples, max_tokens=max_completion_tokens, temperature=temperature, top_k=top_k ) # Check each sample for correctness outcomes = [] for sample_tokens in generated_token_sequences: generated_tokens = sample_tokens[prefix_length:] generated_text = tokenizer.decode(generated_tokens) is_correct = task.evaluate(conversation, generated_text) outcomes.append({ "is_correct": is_correct }) # A bit bloated because I wanted to do more complex logging at one point. record = { "idx": idx, "outcomes": outcomes, } yield record # ----------------------------------------------------------------------------- # Training loop # Init the optimizer optimizer = model.setup_optimizer( unembedding_lr=args.unembedding_lr, embedding_lr=args.embedding_lr, matrix_lr=args.matrix_lr, weight_decay=args.weight_decay, ) # Set the initial learning rate as a fraction of the base learning rate for group in optimizer.param_groups: group["lr"] = group["lr"] * args.init_lr_frac group["initial_lr"] = group["lr"] # Learning rate scheduler: simple rampdown to zero over num_steps def get_lr_multiplier(it): lrm = 1.0 - it / num_steps return lrm # Calculate the number of examples each rank handles to achieve the desired examples_per_step print0(f"Total sequences per step: {args.examples_per_step * args.num_samples}") # total batch size in sequences/step assert args.examples_per_step % ddp_world_size == 0, "Desired examples per step must be divisible by the number of ranks" examples_per_rank = args.examples_per_step // ddp_world_size # per GPU print0(f"Calculated examples per rank: {examples_per_rank}") # Kick off the training loop batch_iterator = get_batch() for step in range(num_steps): # Evaluate the model once in a while and log to wandb if step % args.eval_every == 0: model.eval() passk = torch.zeros(args.device_batch_size, device=device) # pass@k for k=1..device_batch_size with autocast_ctx: records_iter = run_gsm8k_eval(val_task, tokenizer, engine, num_samples=args.device_batch_size, max_examples=args.eval_examples, temperature=1.0) records = list(records_iter) # collect all records for k in range(1, args.device_batch_size + 1): passk[k - 1] = sum(any(o["is_correct"] for o in r["outcomes"][:k]) for r in records) num_records = torch.tensor(len(records), dtype=torch.long, device=device) if ddp: dist.all_reduce(num_records, op=dist.ReduceOp.SUM) dist.all_reduce(passk, op=dist.ReduceOp.SUM) passk = passk / num_records.item() # normalize by the total number of records print_passk = [f"Pass@{k}: {passk[k - 1].item():.4f}" for k in range(1, args.device_batch_size + 1)] print0(f"Step {step} | {', '.join(print_passk)}") log_passk = {f"pass@{k}": passk[k - 1].item() for k in range(1, args.device_batch_size + 1)} wandb_run.log({ "step": step, **log_passk, }) # Forward/Backward on rollouts over multiple examples in the dataset rewards_list = [] sequence_lengths = [] for example_step in range(examples_per_rank): # Get one batch corresponding to one example in the training dataset sequences_all, inputs_all, targets_all, rewards_all, advantages_all = next(batch_iterator) # Evaluate the loss and gradients model.train() # ensure the model is in train mode # We need one more loop because we can never exceed the device_batch_size assert inputs_all.size(0) % args.device_batch_size == 0 num_passes = inputs_all.size(0) // args.device_batch_size for pass_idx in range(num_passes): # Pluck out the batch for this pass b0, b1 = pass_idx * args.device_batch_size, (pass_idx + 1) * args.device_batch_size inputs = inputs_all[b0:b1] targets = targets_all[b0:b1] rewards = rewards_all[b0:b1] advantages = advantages_all[b0:b1] # Calculate log probabilities. Note that the loss calculates NLL = -logp, so we negate with autocast_ctx: logp = -model(inputs, targets, loss_reduction='none').view_as(inputs) # (B, T) # Calculate the PG objective. Note that ignore_index=-1 ensures that invalid tokens have loss 0. pg_obj = (logp * advantages.unsqueeze(-1)).sum() # normalize by the number of valid tokens, number of passes, and examples_per_rank num_valid = (targets >= 0).sum().clamp(min=1) pg_obj = pg_obj / (num_valid * num_passes * examples_per_rank) # Note, there is no need to add PPO ratio+clip because we are on policy # Finally, formulate the loss that we want to minimize (instead of objective we wish to maximize) loss = -pg_obj loss.backward() print0(f"Step {step}/{num_steps} | Example step {example_step} | Pass {pass_idx} | loss: {loss.item():.6f} | Average reward: {rewards.mean().item()}") # For logging rewards_list.append(rewards_all.mean().item()) sequence_lengths.extend(len(seq) for seq in sequences_all) # A bunch of logging for how the rollouts went this step mean_reward = sum(rewards_list) / len(rewards_list) mean_sequence_length = sum(sequence_lengths) / len(sequence_lengths) if ddp: # aggregate across ranks mean_reward_tensor = torch.tensor(mean_reward, dtype=torch.float, device=device) mean_sequence_length_tensor = torch.tensor(mean_sequence_length, dtype=torch.float, device=device) dist.all_reduce(mean_reward_tensor, op=dist.ReduceOp.AVG) dist.all_reduce(mean_sequence_length_tensor, op=dist.ReduceOp.AVG) mean_reward = mean_reward_tensor.item() mean_sequence_length = mean_sequence_length_tensor.item() print0(f"Step {step}/{num_steps} | Average reward: {mean_reward} | Average sequence length: {mean_sequence_length:.2f}") wandb_run.log({ "step": step, "reward": mean_reward, "sequence_length": mean_sequence_length, }) # Update the model parameters lrm = get_lr_multiplier(step) for group in optimizer.param_groups: group["lr"] = group["initial_lr"] * lrm optimizer.step() model.zero_grad(set_to_none=True) wandb_run.log({ "step": step, "lrm": lrm, }) # Master process saves the model once in a while. Skip first step. Save last step. if master_process and ((step > 0 and step % args.save_every == 0) or step == num_steps - 1): base_dir = get_base_dir() depth = model.config.n_layer output_dirname = args.model_tag if args.model_tag else f"d{depth}" # base the model tag on the depth of the base model checkpoint_dir = os.path.join(base_dir, "chatrl_checkpoints", output_dirname) model_config_kwargs = model.config.__dict__ # slightly naughty, abusing the simplicity of GPTConfig, TODO nicer save_checkpoint( checkpoint_dir, step, model.state_dict(), None, # note: we don't bother to save the optimizer state { "model_config": model_config_kwargs, } ) print(f"βœ… Saved model checkpoint to {checkpoint_dir}") # Log to report from nanochat.report import get_report get_report().log(section="Chat RL", data=[ user_config, # CLI args ]) wandb_run.finish() # wandb run finish compute_cleanup()
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/chat_rl.py", "license": "MIT License", "lines": 308, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/chat_sft.py
""" Supervised fine-tuning (SFT) the model. Run as: python -m scripts.chat_sft Or torchrun for training: torchrun --standalone --nproc_per_node=8 -m scripts.chat_sft -- --device-batch-size=16 """ import gc import argparse import os os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True" import time import wandb import torch from contextlib import nullcontext from nanochat.common import compute_init, compute_cleanup, print0, DummyWandb, get_base_dir, autodetect_device_type, get_peak_flops from nanochat.tokenizer import get_token_bytes from nanochat.checkpoint_manager import save_checkpoint, load_model, load_optimizer_state from nanochat.loss_eval import evaluate_bpb import torch.distributed as dist from nanochat.flash_attention import HAS_FA3 from nanochat.engine import Engine from scripts.chat_eval import run_chat_eval from tasks.common import TaskMixture from tasks.gsm8k import GSM8K from tasks.mmlu import MMLU from tasks.smoltalk import SmolTalk from tasks.customjson import CustomJSON from tasks.spellingbee import SimpleSpelling, SpellingBee # ----------------------------------------------------------------------------- # CLI arguments parser = argparse.ArgumentParser(description="Supervised fine-tuning (SFT) the model") # Logging parser.add_argument("--run", type=str, default="dummy", help="wandb run name ('dummy' disables wandb logging)") # Runtime parser.add_argument("--device-type", type=str, default="", help="cuda|cpu|mps (empty = autodetect)") # Model loading parser.add_argument("--model-tag", type=str, default=None, help="model tag to load from") parser.add_argument("--model-step", type=int, default=None, help="model step to load from") parser.add_argument("--load-optimizer", type=int, default=1, help="warm-start optimizer from pretrained checkpoint (0=no, 1=yes)") # Training horizon parser.add_argument("--num-iterations", type=int, default=-1, help="number of optimization steps (-1 = full epoch)") # Batch sizes (default: inherit from pretrained checkpoint) parser.add_argument("--max-seq-len", type=int, default=None, help="max context length (default: inherit from pretrain)") parser.add_argument("--device-batch-size", type=int, default=None, help="per-device batch size (default: inherit from pretrain)") parser.add_argument("--total-batch-size", type=int, default=None, help="total batch size in tokens (default: inherit from pretrain)") # Optimization (default: inherit from pretrained checkpoint) parser.add_argument("--embedding-lr", type=float, default=None, help="learning rate for embedding parameters (Adam) (default: inherit from pretrain)") parser.add_argument("--unembedding-lr", type=float, default=None, help="learning rate for unembedding parameters (Adam) (default: inherit from pretrain)") parser.add_argument("--matrix-lr", type=float, default=None, help="learning rate for matrix parameters (Muon) (default: inherit from pretrain)") parser.add_argument("--init-lr-frac", type=float, default=0.8, help="initial LR as fraction of base LR") parser.add_argument("--warmup-ratio", type=float, default=0.0, help="ratio of iterations for LR warmup") parser.add_argument("--warmdown-ratio", type=float, default=0.5, help="ratio of iterations for LR warmdown") parser.add_argument("--final-lr-frac", type=float, default=0.0, help="final LR as fraction of initial LR") # Evaluation parser.add_argument("--eval-every", type=int, default=200, help="evaluate val bpb every N steps (-1 = disable)") parser.add_argument("--eval-tokens", type=int, default=40*524288, help="number of tokens to evaluate val loss on") parser.add_argument("--chatcore-every", type=int, default=200, help="evaluate ChatCORE metric every N steps (-1 = disable)") parser.add_argument("--chatcore-max-cat", type=int, default=-1, help="max problems per categorical task for ChatCORE") parser.add_argument("--chatcore-max-sample", type=int, default=24, help="max problems per generative task for ChatCORE") # Data mixture parser.add_argument("--mmlu-epochs", type=int, default=3, help="number of epochs of MMLU in training mixture (teaches Multiple Choice)") parser.add_argument("--gsm8k-epochs", type=int, default=4, help="number of epochs of GSM8K in training mixture (teaches Math and Tool Use)") args = parser.parse_args() user_config = vars(args).copy() # ----------------------------------------------------------------------------- # Compute init device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) master_process = ddp_rank == 0 autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=torch.bfloat16) if device_type == "cuda" else nullcontext() synchronize = torch.cuda.synchronize if device_type == "cuda" else lambda: None get_max_memory = torch.cuda.max_memory_allocated if device_type == "cuda" else lambda: 0 if device_type == "cuda": gpu_device_name = torch.cuda.get_device_name(0) gpu_peak_flops = get_peak_flops(gpu_device_name) print0(f"GPU: {gpu_device_name} | Peak FLOPS (BF16): {gpu_peak_flops:.2e}") else: gpu_peak_flops = float('inf') # MFU not meaningful for CPU/MPS # wandb logging init use_dummy_wandb = args.run == "dummy" or not master_process wandb_run = DummyWandb() if use_dummy_wandb else wandb.init(project="nanochat-sft", name=args.run, config=user_config) # Flash Attention status if not HAS_FA3: print0("WARNING: Flash Attention 3 not available, using PyTorch SDPA fallback. Training will be less efficient.") # Load the model and tokenizer model, tokenizer, meta = load_model("base", device, phase="train", model_tag=args.model_tag, step=args.model_step) # Inherit training hyperparameters from pretrained checkpoint (None = inherit, explicit value = override) pretrain_user_config = meta.get("user_config", {}) for name, fallback, source in [ ("max_seq_len", 2048, meta), ("device_batch_size", 32, meta), ("total_batch_size", 524288, meta), ("embedding_lr", 0.3, pretrain_user_config), ("unembedding_lr", 0.004, pretrain_user_config), ("matrix_lr", 0.02, pretrain_user_config), ]: arg_val = getattr(args, name) pretrain_val = source.get(name) if arg_val is None: resolved = pretrain_val if pretrain_val is not None else fallback setattr(args, name, resolved) print0(f"Inherited {name}={resolved} from pretrained checkpoint") elif pretrain_val is not None and arg_val != pretrain_val: print0(f"NOTE: --{name.replace('_', '-')}={arg_val} overrides pretrained value of {pretrain_val}") else: print0(f"Using {name}={arg_val}") orig_model = model model = torch.compile(model, dynamic=False) depth = model.config.n_layer num_flops_per_token = model.estimate_flops() tokens_per_fwdbwd = args.device_batch_size * args.max_seq_len # tokens per iteration for a single rank world_tokens_per_fwdbwd = tokens_per_fwdbwd * ddp_world_size # total tokens per iteration for all ranks assert args.total_batch_size % world_tokens_per_fwdbwd == 0 grad_accum_steps = args.total_batch_size // world_tokens_per_fwdbwd print0(f"Tokens / micro-batch / rank: {args.device_batch_size} x {args.max_seq_len} = {tokens_per_fwdbwd:,}") print0(f"Tokens / micro-batch: {world_tokens_per_fwdbwd:,}") print0(f"Total batch size {args.total_batch_size:,} => gradient accumulation steps: {grad_accum_steps}") token_bytes = get_token_bytes(device=device) # Initialize the Optimizer (combined MuonAdamW: Muon for matrix params, AdamW for rest) # Note that pretraining ramps weight_decay to zero by end of pretraining, so SFT continues with zero optimizer = model.setup_optimizer(unembedding_lr=args.unembedding_lr, embedding_lr=args.embedding_lr, matrix_lr=args.matrix_lr, weight_decay=0.0) # Optionally warm-start optimizer from pretrained checkpoint (momentum buffers etc.) # Note: load_state_dict overwrites param_group metadata (LRs, betas, etc.) with the # pretrained values. Since pretraining warmdown brings LRs to ~0, we must save and # restore our fresh SFT LRs after loading. base_dir = get_base_dir() if args.load_optimizer: optimizer_data = load_optimizer_state("base", device, rank=ddp_rank, model_tag=args.model_tag, step=args.model_step) if optimizer_data is not None: base_lrs = [group["lr"] for group in optimizer.param_groups] optimizer.load_state_dict(optimizer_data) del optimizer_data for group, base_lr in zip(optimizer.param_groups, base_lrs): group["lr"] = base_lr print0("Loaded optimizer state from pretrained checkpoint (momentum buffers only, LRs reset)") else: print0("WARNING: optimizer checkpoint not found, starting with fresh optimizer (slightly worse)") # Override the initial learning rate as a fraction of the base learning rate for group in optimizer.param_groups: group["lr"] = group["lr"] * args.init_lr_frac group["initial_lr"] = group["lr"] # SFT data mixture and DataLoader identity_conversations_filepath = os.path.join(base_dir, "identity_conversations.jsonl") train_tasks = [ SmolTalk(split="train"), # 460K rows of general conversations CustomJSON(filepath=identity_conversations_filepath), # 1000 rows of synthetic identity conversations CustomJSON(filepath=identity_conversations_filepath), # 2 epochs of these *[MMLU(subset="auxiliary_train", split="train") for _ in range(args.mmlu_epochs)], # 100K rows per epoch *[GSM8K(subset="main", split="train") for _ in range(args.gsm8k_epochs)], # 8K rows per epoch SimpleSpelling(size=200000, split="train"), # 200K rows of Simple Spelling (e.g. spell the word 'apple') SpellingBee(size=80000, split="train"), # 80K rows of Spelling Bee (e.g. how many 'r' are in 'strawberry'?) ] train_dataset = TaskMixture(train_tasks) print0(f"Training mixture: {len(train_dataset):,} rows (MMLU x{args.mmlu_epochs}, GSM8K x{args.gsm8k_epochs})") val_dataset = TaskMixture([ SmolTalk(split="test"), # 24K rows in test set MMLU(subset="all", split="test", stop=5200), # 14K rows in test set, use only 5.2K to match the train ratios GSM8K(subset="main", split="test", stop=420), # 1.32K rows in test set, use only 420 to match the train ratios ]) # total: 24K + 14K + 1.32K ~= 39K rows # DataLoader is defined here, it emits inputs, targets : 2D tensors of shape (device_batch_size, max_seq_len) # A big problem is that we don't know the final num_iterations in advance. So we create # these two global variables and update them from within the data generator. last_step = False # we will toggle this to True when we reach the end of the training dataset approx_progress = 0.0 # will go from 0 to 1 over the course of the epoch current_epoch = 1 # track epoch for logging def sft_data_generator_bos_bestfit(split, buffer_size=100): """ BOS-aligned dataloader for SFT with bestfit-pad packing. Each row in the batch starts with BOS (beginning of a conversation). Conversations are packed using best-fit algorithm. When no conversation fits, the row is padded (instead of cropping) to ensure no tokens are ever discarded. Padding positions have targets masked with -1 (ignore_index for cross-entropy). """ global last_step, approx_progress, current_epoch assert split in {"train", "val"}, "split must be 'train' or 'val'" dataset = train_dataset if split == "train" else val_dataset dataset_size = len(dataset) assert dataset_size > 0 row_capacity = args.max_seq_len + 1 # +1 for target at last position bos_token = tokenizer.get_bos_token_id() # Conversation buffer: list of token lists conv_buffer = [] cursor = ddp_rank # Each rank processes different conversations (for fetching) consumed = ddp_rank # Track actual consumption separately from buffering epoch = 1 it = 0 # iteration counter def refill_buffer(): nonlocal cursor, epoch while len(conv_buffer) < buffer_size: conversation = dataset[cursor] ids, _ = tokenizer.render_conversation(conversation) conv_buffer.append(ids) cursor += ddp_world_size if cursor >= dataset_size: cursor = cursor % dataset_size epoch += 1 # Note: last_step is now triggered based on consumption, not fetching while True: rows = [] row_lengths = [] # Track actual content length (excluding padding) for each row for _ in range(args.device_batch_size): row = [] padded = False while len(row) < row_capacity: # Ensure buffer has conversations while len(conv_buffer) < buffer_size: refill_buffer() remaining = row_capacity - len(row) # Find largest conversation that fits entirely best_idx = -1 best_len = 0 for i, conv in enumerate(conv_buffer): conv_len = len(conv) if conv_len <= remaining and conv_len > best_len: best_idx = i best_len = conv_len if best_idx >= 0: # Found a conversation that fits - use it entirely conv = conv_buffer.pop(best_idx) row.extend(conv) consumed += ddp_world_size # Track actual consumption else: # No conversation fits - pad the remainder instead of cropping # This ensures we never discard any tokens content_len = len(row) row.extend([bos_token] * remaining) # Pad with BOS tokens padded = True break # Row is now full (with padding) # Track content length: full row if no padding, otherwise the length before padding if padded: row_lengths.append(content_len) else: row_lengths.append(row_capacity) rows.append(row[:row_capacity]) # Stopping condition to respect num_iterations, if given it += 1 if 0 < args.num_iterations <= it and split == "train": last_step = True # Update progress tracking (based on consumed, not cursor, to account for buffering) if split == "train": current_epoch = epoch if args.num_iterations > 0: approx_progress = it / args.num_iterations else: approx_progress = consumed / dataset_size # Trigger last_step when we've consumed enough (instead of when cursor wraps) if consumed >= dataset_size: last_step = True # Build tensors use_cuda = device_type == "cuda" batch_tensor = torch.tensor(rows, dtype=torch.long, pin_memory=use_cuda) inputs = batch_tensor[:, :-1].to(device=device, dtype=torch.int32, non_blocking=use_cuda) targets = batch_tensor[:, 1:].to(device=device, dtype=torch.int64, non_blocking=use_cuda) # Mask out padding positions in targets (set to -1 = ignore_index) # For each row, positions >= (content_length - 1) in targets should be masked for i, content_len in enumerate(row_lengths): if content_len < row_capacity: targets[i, content_len-1:] = -1 yield inputs, targets train_loader = sft_data_generator_bos_bestfit("train") build_val_loader = lambda: sft_data_generator_bos_bestfit("val") progress = 0 # will go from 0 to 1 over the course of the epoch # Learning rate schedule (linear warmup, constant, linear warmdown) # Same shape as base_train but uses progress (0β†’1) instead of absolute step counts, # because SFT doesn't always know num_iterations in advance (dataset-driven stopping). def get_lr_multiplier(progress): if progress < args.warmup_ratio: return (progress + 1e-8) / args.warmup_ratio elif progress <= 1.0 - args.warmdown_ratio: return 1.0 else: decay = (progress - (1.0 - args.warmdown_ratio)) / args.warmdown_ratio return (1 - decay) * 1.0 + decay * args.final_lr_frac # Momentum scheduler for Muon optimizer def get_muon_momentum(it): frac = min(it / 300, 1) momentum = (1 - frac) * 0.85 + frac * 0.95 return momentum # ----------------------------------------------------------------------------- # Training loop x, y = next(train_loader) # prefetch the very first batch of data min_val_bpb = float("inf") smooth_train_loss = 0 # EMA of training loss ema_beta = 0.9 # EMA decay factor total_training_time = 0 # total wall-clock time of training step = 0 while True: flops_so_far = num_flops_per_token * args.total_batch_size * step # Synchronize last_step across all ranks to avoid hangs in the distributed setting if ddp: last_step_tensor = torch.tensor(last_step, dtype=torch.int32, device=device) dist.all_reduce(last_step_tensor, op=dist.ReduceOp.MAX) last_step = bool(last_step_tensor.item()) # once in a while: evaluate the val bpb (all ranks participate) if last_step or (args.eval_every > 0 and step % args.eval_every == 0): model.eval() val_loader = build_val_loader() eval_steps = args.eval_tokens // (args.device_batch_size * args.max_seq_len * ddp_world_size) with autocast_ctx: val_bpb = evaluate_bpb(model, val_loader, eval_steps, token_bytes) print0(f"Step {step:05d} | Validation bpb: {val_bpb:.4f}") if val_bpb < min_val_bpb: min_val_bpb = val_bpb wandb_run.log({ "step": step, "total_training_flops": flops_so_far, "total_training_time": total_training_time, "val/bpb": val_bpb, }) model.train() # once in a while: estimate the ChatCORE metric (all ranks participate) # use the original uncompiled model because the inputs keep changing shape chatcore_results = {} if args.chatcore_every > 0 and (last_step or (step > 0 and step % args.chatcore_every == 0)): model.eval() engine = Engine(orig_model, tokenizer) all_tasks = ['ARC-Easy', 'ARC-Challenge', 'MMLU', 'GSM8K', 'HumanEval', 'SpellingBee'] categorical_tasks = {'ARC-Easy', 'ARC-Challenge', 'MMLU'} baseline_accuracies = { 'ARC-Easy': 0.25, 'ARC-Challenge': 0.25, 'MMLU': 0.25, 'GSM8K': 0.0, 'HumanEval': 0.0, 'SpellingBee': 0.0, } task_results = {} for task_name in all_tasks: limit = args.chatcore_max_cat if task_name in categorical_tasks else args.chatcore_max_sample max_problems = None if limit < 0 else limit # -1 means no limit with autocast_ctx: acc = run_chat_eval(task_name, orig_model, tokenizer, engine, batch_size=args.device_batch_size, max_problems=max_problems) task_results[task_name] = acc print0(f" {task_name}: {100*acc:.2f}%") # Compute ChatCORE metrics (mean centered accuracy, ranges from 0=random to 1=perfect) def centered_mean(tasks): return sum((task_results[t] - baseline_accuracies[t]) / (1.0 - baseline_accuracies[t]) for t in tasks) / len(tasks) chatcore = centered_mean(all_tasks) chatcore_cat = centered_mean(categorical_tasks) print0(f"Step {step:05d} | ChatCORE: {chatcore:.4f} | ChatCORE_cat: {chatcore_cat:.4f}") wandb_run.log({ "step": step, "total_training_flops": flops_so_far, "chatcore_metric": chatcore, "chatcore_cat": chatcore_cat, **{f"chatcore/{task_name}": acc for task_name, acc in task_results.items()}, }) model.train() # save checkpoint at the end of the run (all ranks participate so each saves its optimizer shard) if last_step: output_dirname = args.model_tag if args.model_tag else f"d{depth}" # e.g. d12 checkpoint_dir = os.path.join(base_dir, "chatsft_checkpoints", output_dirname) save_checkpoint( checkpoint_dir, step, orig_model.state_dict(), optimizer.state_dict(), { "step": step, "val_bpb": val_bpb, # loss at last step "model_config": { "sequence_len": args.max_seq_len, "vocab_size": tokenizer.get_vocab_size(), "n_layer": depth, "n_head": model.config.n_head, "n_kv_head": model.config.n_kv_head, "n_embd": model.config.n_embd, "window_pattern": model.config.window_pattern, }, "user_config": user_config, # inputs to the training script }, rank=ddp_rank, ) if last_step: break # ------------------------------------------------------------------------- # single training step # evaluate the gradient synchronize() t0 = time.time() for micro_step in range(grad_accum_steps): with autocast_ctx: loss = model(x, y) train_loss = loss.detach() # for logging loss = loss / grad_accum_steps # each .backward() is a grad sum => normalize loss here loss.backward() x, y = next(train_loader) # prefetch the next batch while the GPU is busy with forward/backward progress = max(progress, approx_progress) # only increase progress monotonically # step the optimizer lrm = get_lr_multiplier(progress) muon_momentum = get_muon_momentum(step) for group in optimizer.param_groups: group["lr"] = group["initial_lr"] * lrm if group['kind'] == 'muon': group["momentum"] = muon_momentum optimizer.step() model.zero_grad(set_to_none=True) synchronize() t1 = time.time() dt = t1 - t0 # ------------------------------------------------------------------------- # State step += 1 # logging smooth_train_loss = ema_beta * smooth_train_loss + (1 - ema_beta) * train_loss.item() # EMA the training loss debiased_smooth_loss = smooth_train_loss / (1 - ema_beta**(step + 1)) # debias the EMA pct_done = 100 * progress tok_per_sec = int(args.total_batch_size / dt) flops_per_sec = num_flops_per_token * args.total_batch_size / dt mfu = 100 * flops_per_sec / (gpu_peak_flops * ddp_world_size) if step > 10: total_training_time += dt # only count the time after the first 10 steps print0(f"step {step:05d} ({pct_done:.2f}%) | loss: {debiased_smooth_loss:.6f} | lrm: {lrm:.2f} | dt: {dt * 1000:.2f}ms | tok/sec: {tok_per_sec:,} | mfu: {mfu:.2f} | epoch: {current_epoch} | total time: {total_training_time/60:.2f}m") if step % 10 == 0: wandb_run.log({ "step": step, "total_training_flops": flops_so_far, "total_training_time": total_training_time, "train/loss": debiased_smooth_loss, "train/lrm": lrm, "train/dt": dt, "train/tok_per_sec": tok_per_sec, "train/mfu": mfu, "train/epoch": current_epoch, }) # The garbage collector spends ~500ms scanning for cycles quite frequently. # We manually manage it to avoid these pauses during training. if step == 1: gc.collect() # manually collect a lot of garbage from setup gc.freeze() # freeze all currently surviving objects and exclude them from GC gc.disable() # disable GC entirely except: elif step % 5000 == 0: # every 5000 steps... gc.collect() # manually collect, just to be safe for very long runs # print a few more stats print0(f"Peak memory usage: {get_max_memory() / 1024 / 1024:.2f}MiB") print0(f"Total training time: {total_training_time/60:.2f}m") print0(f"Minimum validation bpb: {min_val_bpb:.4f}") # Log to report from nanochat.report import get_report get_report().log(section="SFT", data=[ user_config, # CLI args { # stats about the training setup "Number of iterations": step, "DDP world size": ddp_world_size, }, { # stats about training outcomes "Minimum validation bpb": min_val_bpb, } ]) # cleanup wandb_run.finish() # wandb run finish compute_cleanup()
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/chat_sft.py", "license": "MIT License", "lines": 450, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/chat_web.py
#!/usr/bin/env python3 """ Unified web chat server - serves both UI and API from a single FastAPI instance. Uses data parallelism to distribute requests across multiple GPUs. Each GPU loads a full copy of the model, and incoming requests are distributed to available workers. Launch examples: - single available GPU (default) python -m scripts.chat_web - 4 GPUs python -m scripts.chat_web --num-gpus 4 To chat, open the URL printed in the console. (If on cloud box, make sure to use public IP) Endpoints: GET / - Chat UI POST /chat/completions - Chat API (streaming only) GET /health - Health check with worker pool status GET /stats - Worker pool statistics and GPU utilization Abuse Prevention: - Maximum 500 messages per request - Maximum 8000 characters per message - Maximum 32000 characters total conversation length - Temperature clamped to 0.0-2.0 - Top-k clamped to 0-200 (0 disables top-k filtering, using full vocabulary) - Max tokens clamped to 1-4096 """ import argparse import json import os import torch import asyncio import logging import random from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import StreamingResponse, HTMLResponse, FileResponse from pydantic import BaseModel from typing import List, Optional, AsyncGenerator from dataclasses import dataclass from contextlib import nullcontext from nanochat.common import compute_init, autodetect_device_type from nanochat.checkpoint_manager import load_model from nanochat.engine import Engine # Abuse prevention limits MAX_MESSAGES_PER_REQUEST = 500 MAX_MESSAGE_LENGTH = 8000 MAX_TOTAL_CONVERSATION_LENGTH = 32000 MIN_TEMPERATURE = 0.0 MAX_TEMPERATURE = 2.0 MIN_TOP_K = 0 # 0 disables top-k filtering, using full vocabulary MAX_TOP_K = 200 MIN_MAX_TOKENS = 1 MAX_MAX_TOKENS = 4096 parser = argparse.ArgumentParser(description='NanoChat Web Server') parser.add_argument('-n', '--num-gpus', type=int, default=1, help='Number of GPUs to use (default: 1)') parser.add_argument('-i', '--source', type=str, default="sft", help="Source of the model: sft|rl") parser.add_argument('-t', '--temperature', type=float, default=0.8, help='Default temperature for generation') parser.add_argument('-k', '--top-k', type=int, default=50, help='Default top-k sampling parameter') parser.add_argument('-m', '--max-tokens', type=int, default=512, help='Default max tokens for generation') parser.add_argument('-g', '--model-tag', type=str, default=None, help='Model tag to load') parser.add_argument('-s', '--step', type=int, default=None, help='Step to load') parser.add_argument('-p', '--port', type=int, default=8000, help='Port to run the server on') parser.add_argument('-d', '--dtype', type=str, default='bfloat16', choices=['float32', 'bfloat16']) parser.add_argument('--device-type', type=str, default='', choices=['cuda', 'cpu', 'mps'], help='Device type for evaluation: cuda|cpu|mps. empty => autodetect') parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to bind the server to') args = parser.parse_args() # Configure logging for conversation traffic logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) logger = logging.getLogger(__name__) device_type = autodetect_device_type() if args.device_type == "" else args.device_type ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type) ptdtype = torch.float32 if args.dtype == 'float32' else torch.bfloat16 @dataclass class Worker: """A worker with a model loaded on a specific GPU.""" gpu_id: int device: torch.device engine: Engine tokenizer: object autocast_ctx: torch.amp.autocast class WorkerPool: """Pool of workers, each with a model replica on a different GPU.""" def __init__(self, num_gpus: Optional[int] = None): if num_gpus is None: if device_type == "cuda": num_gpus = torch.cuda.device_count() else: num_gpus = 1 # e.g. cpu|mps self.num_gpus = num_gpus self.workers: List[Worker] = [] self.available_workers: asyncio.Queue = asyncio.Queue() async def initialize(self, source: str, model_tag: Optional[str] = None, step: Optional[int] = None): """Load model on each GPU.""" print(f"Initializing worker pool with {self.num_gpus} GPUs...") if self.num_gpus > 1: assert device_type == "cuda", "Only CUDA supports multiple workers/GPUs. cpu|mps does not." for gpu_id in range(self.num_gpus): if device_type == "cuda": device = torch.device(f"cuda:{gpu_id}") print(f"Loading model on GPU {gpu_id}...") else: device = torch.device(device_type) # e.g. cpu|mps print(f"Loading model on {device_type}...") model, tokenizer, _ = load_model(source, device, phase="eval", model_tag=model_tag, step=step) engine = Engine(model, tokenizer) autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) if device_type == "cuda" else nullcontext() worker = Worker( gpu_id=gpu_id, device=device, engine=engine, tokenizer=tokenizer, autocast_ctx=autocast_ctx ) self.workers.append(worker) await self.available_workers.put(worker) print(f"All {self.num_gpus} workers initialized!") async def acquire_worker(self) -> Worker: """Get an available worker from the pool.""" return await self.available_workers.get() async def release_worker(self, worker: Worker): """Return a worker to the pool.""" await self.available_workers.put(worker) class ChatMessage(BaseModel): role: str content: str class ChatRequest(BaseModel): messages: List[ChatMessage] temperature: Optional[float] = None max_tokens: Optional[int] = None top_k: Optional[int] = None def validate_chat_request(request: ChatRequest): """Validate chat request to prevent abuse.""" # Check number of messages if len(request.messages) == 0: raise HTTPException(status_code=400, detail="At least one message is required") if len(request.messages) > MAX_MESSAGES_PER_REQUEST: raise HTTPException( status_code=400, detail=f"Too many messages. Maximum {MAX_MESSAGES_PER_REQUEST} messages allowed per request" ) # Check individual message lengths and total conversation length total_length = 0 for i, message in enumerate(request.messages): if not message.content: raise HTTPException(status_code=400, detail=f"Message {i} has empty content") msg_length = len(message.content) if msg_length > MAX_MESSAGE_LENGTH: raise HTTPException( status_code=400, detail=f"Message {i} is too long. Maximum {MAX_MESSAGE_LENGTH} characters allowed per message" ) total_length += msg_length if total_length > MAX_TOTAL_CONVERSATION_LENGTH: raise HTTPException( status_code=400, detail=f"Total conversation is too long. Maximum {MAX_TOTAL_CONVERSATION_LENGTH} characters allowed" ) # Validate role values for i, message in enumerate(request.messages): if message.role not in ["user", "assistant"]: raise HTTPException( status_code=400, detail=f"Message {i} has invalid role. Must be 'user', 'assistant', or 'system'" ) # Validate temperature if request.temperature is not None: if not (MIN_TEMPERATURE <= request.temperature <= MAX_TEMPERATURE): raise HTTPException( status_code=400, detail=f"Temperature must be between {MIN_TEMPERATURE} and {MAX_TEMPERATURE}" ) # Validate top_k if request.top_k is not None: if not (MIN_TOP_K <= request.top_k <= MAX_TOP_K): raise HTTPException( status_code=400, detail=f"top_k must be between {MIN_TOP_K} and {MAX_TOP_K}" ) # Validate max_tokens if request.max_tokens is not None: if not (MIN_MAX_TOKENS <= request.max_tokens <= MAX_MAX_TOKENS): raise HTTPException( status_code=400, detail=f"max_tokens must be between {MIN_MAX_TOKENS} and {MAX_MAX_TOKENS}" ) @asynccontextmanager async def lifespan(app: FastAPI): """Load models on all GPUs on startup.""" print("Loading nanochat models across GPUs...") app.state.worker_pool = WorkerPool(num_gpus=args.num_gpus) await app.state.worker_pool.initialize(args.source, model_tag=args.model_tag, step=args.step) print(f"Server ready at http://localhost:{args.port}") yield app = FastAPI(lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") async def root(): """Serve the chat UI.""" ui_html_path = os.path.join("nanochat", "ui.html") with open(ui_html_path, "r", encoding="utf-8") as f: html_content = f.read() # Replace the API_URL to use the same origin html_content = html_content.replace( "const API_URL = `http://${window.location.hostname}:8000`;", "const API_URL = '';" ) return HTMLResponse(content=html_content) @app.get("/logo.svg") async def logo(): """Serve the NanoChat logo for favicon and header.""" logo_path = os.path.join("nanochat", "logo.svg") return FileResponse(logo_path, media_type="image/svg+xml") async def generate_stream( worker: Worker, tokens, temperature=None, max_new_tokens=None, top_k=None ) -> AsyncGenerator[str, None]: """Generate assistant response with streaming.""" temperature = temperature if temperature is not None else args.temperature max_new_tokens = max_new_tokens if max_new_tokens is not None else args.max_tokens top_k = top_k if top_k is not None else args.top_k assistant_end = worker.tokenizer.encode_special("<|assistant_end|>") bos = worker.tokenizer.get_bos_token_id() # Accumulate tokens to properly handle multi-byte UTF-8 characters (like emojis) accumulated_tokens = [] # Track the last complete UTF-8 string (without replacement characters) last_clean_text = "" with worker.autocast_ctx: for token_column, token_masks in worker.engine.generate( tokens, num_samples=1, max_tokens=max_new_tokens, temperature=temperature, top_k=top_k, seed=random.randint(0, 2**31 - 1) ): token = token_column[0] # Stopping criteria if token == assistant_end or token == bos: break # Append the token to sequence accumulated_tokens.append(token) # Decode all accumulated tokens to get proper UTF-8 handling # Note that decode is a quite efficient operation, basically table lookup and string concat current_text = worker.tokenizer.decode(accumulated_tokens) # Only emit text if it doesn't end with a replacement character # This ensures we don't emit incomplete UTF-8 sequences if not current_text.endswith('οΏ½'): # Extract only the new text since last clean decode new_text = current_text[len(last_clean_text):] if new_text: # Only yield if there's new content yield f"data: {json.dumps({'token': new_text, 'gpu': worker.gpu_id}, ensure_ascii=False)}\n\n" last_clean_text = current_text yield f"data: {json.dumps({'done': True})}\n\n" @app.post("/chat/completions") async def chat_completions(request: ChatRequest): """Chat completion endpoint (streaming only) - uses worker pool for multi-GPU.""" # Basic validation to prevent abuse validate_chat_request(request) # Log incoming conversation to console logger.info("="*20) for i, message in enumerate(request.messages): logger.info(f"[{message.role.upper()}]: {message.content}") logger.info("-"*20) # Acquire a worker from the pool (will wait if all are busy) worker_pool = app.state.worker_pool worker = await worker_pool.acquire_worker() try: # Build conversation tokens bos = worker.tokenizer.get_bos_token_id() user_start = worker.tokenizer.encode_special("<|user_start|>") user_end = worker.tokenizer.encode_special("<|user_end|>") assistant_start = worker.tokenizer.encode_special("<|assistant_start|>") assistant_end = worker.tokenizer.encode_special("<|assistant_end|>") conversation_tokens = [bos] for message in request.messages: if message.role == "user": conversation_tokens.append(user_start) conversation_tokens.extend(worker.tokenizer.encode(message.content)) conversation_tokens.append(user_end) elif message.role == "assistant": conversation_tokens.append(assistant_start) conversation_tokens.extend(worker.tokenizer.encode(message.content)) conversation_tokens.append(assistant_end) conversation_tokens.append(assistant_start) # Streaming response with worker release after completion response_tokens = [] async def stream_and_release(): try: async for chunk in generate_stream( worker, conversation_tokens, temperature=request.temperature, max_new_tokens=request.max_tokens, top_k=request.top_k ): # Accumulate response for logging chunk_data = json.loads(chunk.replace("data: ", "").strip()) if "token" in chunk_data: response_tokens.append(chunk_data["token"]) yield chunk finally: # Log the assistant response to console full_response = "".join(response_tokens) logger.info(f"[ASSISTANT] (GPU {worker.gpu_id}): {full_response}") logger.info("="*20) # Release worker back to pool after streaming is done await worker_pool.release_worker(worker) return StreamingResponse( stream_and_release(), media_type="text/event-stream" ) except Exception as e: # Make sure to release worker even on error await worker_pool.release_worker(worker) raise e @app.get("/health") async def health(): """Health check endpoint.""" worker_pool = getattr(app.state, 'worker_pool', None) return { "status": "ok", "ready": worker_pool is not None and len(worker_pool.workers) > 0, "num_gpus": worker_pool.num_gpus if worker_pool else 0, "available_workers": worker_pool.available_workers.qsize() if worker_pool else 0 } @app.get("/stats") async def stats(): """Get worker pool statistics.""" worker_pool = app.state.worker_pool return { "total_workers": len(worker_pool.workers), "available_workers": worker_pool.available_workers.qsize(), "busy_workers": len(worker_pool.workers) - worker_pool.available_workers.qsize(), "workers": [ { "gpu_id": w.gpu_id, "device": str(w.device) } for w in worker_pool.workers ] } if __name__ == "__main__": import uvicorn print(f"Starting NanoChat Web Server") print(f"Temperature: {args.temperature}, Top-k: {args.top_k}, Max tokens: {args.max_tokens}") uvicorn.run(app, host=args.host, port=args.port)
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/chat_web.py", "license": "MIT License", "lines": 357, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:scripts/tok_train.py
""" Train a tokenizer using our own BPE Tokenizer library. In the style of GPT-4 tokenizer. """ import os import time import argparse import torch from nanochat.tokenizer import RustBPETokenizer from nanochat.common import get_base_dir from nanochat.dataset import parquets_iter_batched # ----------------------------------------------------------------------------- # Parse command line arguments parser = argparse.ArgumentParser(description='Train a BPE tokenizer') parser.add_argument('--max-chars', type=int, default=2_000_000_000, help='Maximum characters to train on (default: 10B)') parser.add_argument('--doc-cap', type=int, default=10_000, help='Maximum characters per document (default: 10,000)') parser.add_argument('--vocab-size', type=int, default=32768, help='Vocabulary size (default: 32768 = 2^15)') args = parser.parse_args() print(f"max_chars: {args.max_chars:,}") print(f"doc_cap: {args.doc_cap:,}") print(f"vocab_size: {args.vocab_size:,}") # ----------------------------------------------------------------------------- # Text iterator def text_iterator(): """ 1) Flatten the batches into a single iterator 2) Crop every document to args.doc_cap characters 3) Break when we've seen args.max_chars characters """ nchars = 0 for batch in parquets_iter_batched(split="train"): for doc in batch: doc_text = doc if len(doc_text) > args.doc_cap: doc_text = doc_text[:args.doc_cap] nchars += len(doc_text) yield doc_text if nchars > args.max_chars: return text_iter = text_iterator() # ----------------------------------------------------------------------------- # Train the tokenizer t0 = time.time() tokenizer = RustBPETokenizer.train_from_iterator(text_iter, args.vocab_size) t1 = time.time() train_time = t1 - t0 print(f"Training time: {train_time:.2f}s") # ----------------------------------------------------------------------------- # Save the tokenizer to disk base_dir = get_base_dir() tokenizer_dir = os.path.join(base_dir, "tokenizer") tokenizer.save(tokenizer_dir) # ----------------------------------------------------------------------------- # Quick inline sanity check test_text = """Hello world! This is a test. Numbers: 123, 4567, 89 Contractions: I'm, you're, it's Special chars: @#$%^&*() Unicode: δ½ ε₯½δΈ–η•Œ 🌍""" encoded = tokenizer.encode(test_text) decoded = tokenizer.decode(encoded) assert decoded == test_text # ----------------------------------------------------------------------------- # One more thing: we wish to cache a mapping from token id to number of bytes of that token # for efficient evaluation of bits per byte. Unlike the typical mean loss, this # allows us to report a loss that is invariant to the vocab size of the tokenizer. # The bits per byte on the validation set is then one of the primary metrics we care about. vocab_size = tokenizer.get_vocab_size() special_set = set(tokenizer.get_special_tokens()) token_strings = [tokenizer.decode([token_id]) for token_id in range(vocab_size)] token_bytes = [] for token_id in range(vocab_size): token_str = token_strings[token_id] # the Python string representation of this token if token_str in special_set: token_bytes.append(0) # special characters are not counted else: id_bytes = len(token_str.encode("utf-8")) # number of bytes that make up this token token_bytes.append(id_bytes) token_bytes = torch.tensor(token_bytes, dtype=torch.int32, device='cpu') token_bytes_path = os.path.join(tokenizer_dir, "token_bytes.pt") with open(token_bytes_path, "wb") as f: torch.save(token_bytes, f) print(f"Saved token_bytes to {token_bytes_path}") # Log to report from nanochat.report import get_report token_bytes_nonzero = (token_bytes[token_bytes > 0]).to(dtype=torch.float32) get_report().log(section="Tokenizer training", data=[ vars(args), # argparse command line arguments {"train_time": train_time}, {"num_special_tokens": len(special_set)}, { "token_bytes_min": int(token_bytes_nonzero.min().item()), "token_bytes_max": int(token_bytes_nonzero.max().item()), "token_bytes_mean": token_bytes_nonzero.mean().item(), "token_bytes_std": token_bytes_nonzero.std().item(), } ])
{ "repo_id": "karpathy/nanochat", "file_path": "scripts/tok_train.py", "license": "MIT License", "lines": 97, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
karpathy/nanochat:tasks/arc.py
""" The ARC dataset from Allen AI. https://huggingface.co/datasets/allenai/ai2_arc """ from datasets import load_dataset from tasks.common import Task, render_mc class ARC(Task): def __init__(self, subset, split, **kwargs): super().__init__(**kwargs) assert subset in ["ARC-Easy", "ARC-Challenge"], "ARC subset must be ARC-Easy or ARC-Challenge" assert split in ["train", "validation", "test"], "ARC split must be train|validation|test" self.ds = load_dataset("allenai/ai2_arc", subset, split=split).shuffle(seed=42) @property def eval_type(self): return 'categorical' def num_examples(self): return len(self.ds) def get_example(self, index): row = self.ds[index] question = row["question"] # the question text choices = row["choices"]["text"] # the text of each choice answer_string = row["answerKey"] # e.g. "A", "B", "C", "D" letters = row["choices"]["label"] # e.g. ["A", "B", "C", "D"] assert answer_string in letters, f"ARC answer {answer_string} must be one of {letters}" # sanity check # create and return the Conversation object user_message = render_mc(question, letters, choices) messages = [ {"role": "user", "content": user_message}, {"role": "assistant", "content": answer_string} ] conversation = { "messages": messages, "letters": letters, # useful during evaluation, so we can narrow and clamp the assistant prediction to one of the letters } return conversation def evaluate(self, conversation, assistant_response): # the assert here is not strictly speaking needed, but currently the way we eval, we expect this to be true # I'm going to leave the assert here to prevent footguns, but possibly in the future can remove it. assert assistant_response in conversation['letters'], f"ARC answer {assistant_response} is expected to be one of {conversation['letters']}" assistant_message = conversation['messages'][-1]['content'] # e.g. "A" return assistant_response == assistant_message
{ "repo_id": "karpathy/nanochat", "file_path": "tasks/arc.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
karpathy/nanochat:tasks/common.py
""" Base class for all Tasks. A Task is basically a dataset of conversations, together with some metadata and often also evaluation criteria. Example tasks: MMLU, ARC-Easy, ARC-Challenge, GSM8K, HumanEval, SmolTalk. """ import random class Task: """ Base class of a Task. Allows for lightweight slicing of the underlying dataset. """ def __init__(self, start=0, stop=None, step=1): # allows a lightweight logical view over a dataset assert start >= 0, f"Start must be non-negative, got {start}" assert stop is None or stop >= start, f"Stop should be greater than or equal to start, got {stop} and {start}" assert step >= 1, f"Step must be strictly positive, got {step}" self.start = start self.stop = stop # could be None here self.step = step @property def eval_type(self): # one of 'generative' | 'categorical' raise NotImplementedError def num_examples(self): raise NotImplementedError def get_example(self, index): raise NotImplementedError def __len__(self): start = self.start stop = self.num_examples() if self.stop is None else self.stop step = self.step span = stop - start num = (span + step - 1) // step # ceil_div(span, step) assert num >= 0, f"Negative number of examples???: {num}" # prevent footguns return num def __getitem__(self, index: int): assert isinstance(index, int), f"Index must be an integer, got {type(index)}" physical_index = self.start + index * self.step conversation = self.get_example(physical_index) return conversation def evaluate(self, problem, completion): raise NotImplementedError class TaskMixture(Task): """ For SFT Training it becomes useful to train on a mixture of datasets. Fun trick: if you wish to oversample any task, just pass it in multiple times in the list. """ def __init__(self, tasks, **kwargs): super().__init__(**kwargs) # tasks is a list of Task objects self.tasks = tasks self.lengths = [len(task) for task in self.tasks] self.num_conversations = sum(self.lengths) # Build list of all (task_idx, local_idx) pairs self.index_map = [] for task_idx, task_length in enumerate(self.lengths): for local_idx in range(task_length): self.index_map.append((task_idx, local_idx)) # Deterministically shuffle to mix tasks throughout training rng = random.Random(42) rng.shuffle(self.index_map) # Note: this is not the most elegant or best solution, but it's ok for now def num_examples(self): return self.num_conversations def get_example(self, index): """ Access conversations according to a deterministic shuffle of all examples. This ensures tasks are mixed throughout training, regardless of dataset size. """ assert 0 <= index < self.num_conversations, f"Index {index} out of range for mixture with {self.num_conversations} conversations" task_idx, local_idx = self.index_map[index] return self.tasks[task_idx][local_idx] class TaskSequence(Task): """ For SFT Training sometimes we want to sequentially train on a list of tasks. This is useful for cases that require a training curriculum. """ def __init__(self, tasks, **kwargs): super().__init__(**kwargs) self.tasks = tasks self.lengths = [len(task) for task in self.tasks] self.num_conversations = sum(self.lengths) def num_examples(self): return self.num_conversations def get_example(self, index): assert 0 <= index < self.num_conversations, f"Index {index} out of range for sequence with {self.num_conversations} conversations" for task_idx, task_length in enumerate(self.lengths): if index < task_length: return self.tasks[task_idx][index] index -= task_length def render_mc(question, letters, choices): """ The common multiple choice rendering format we will use. Note two important design decisions: 1) Bigger models don't care as much, but smaller models prefer to have the letter *after* the choice, which results in better binding. 2) There is no whitespace between the delimiter (=) and the letter. This is actually critical because the tokenizer has different token ids for " A" vs. "A". The assistant responses will be just the letter itself, i.e. "A", so it is important that here in the prompt it is the exact same token, i.e. "A" with no whitespace before it. Again, bigger models don't care about this too much, but smaller models do care about some of these details. """ query = f"Multiple Choice question: {question}\n" query += "".join([f"- {choice}={letter}\n" for letter, choice in zip(letters, choices)]) query += "\nRespond only with the letter of the correct answer." return query if __name__ == "__main__": # very lightweight test of slicing from tasks.mmlu import MMLU ds = MMLU(subset="auxiliary_train", split="train") print("Length of MMLU: ", len(ds)) ex = ds[5] print("5th example: ", ex) ds = MMLU(subset="auxiliary_train", split="train", start=5, stop=10) print("Length of sliced MMLU[5:10]: ", len(ds)) print("0th example of sliced MMLU: ", ds[0]) print("They match: ", ex == ds[0])
{ "repo_id": "karpathy/nanochat", "file_path": "tasks/common.py", "license": "MIT License", "lines": 120, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
karpathy/nanochat:tasks/gsm8k.py
""" GSM8K evaluation. https://huggingface.co/datasets/openai/gsm8k Example problem instance: Question: Weng earns $12 an hour for babysitting. Yesterday, she just did 50 minutes of babysitting. How much did she earn? Answer: Weng earns 12/60 = $<<12/60=0.2>>0.2 per minute. Working 50 minutes, she earned 0.2 x 50 = $<<0.2*50=10>>10. #### 10 Notice that GSM8K uses tool calls inside << >> tags. """ import re from datasets import load_dataset from tasks.common import Task GSM_RE = re.compile(r"#### (\-?[0-9\.\,]+)") def extract_answer(completion): """ Extract the numerical answer after #### marker. Follows official code for normalization: https://github.com/openai/grade-school-math/blob/3101c7d5072418e28b9008a6636bde82a006892c/grade_school_math/dataset.py#L28 """ match = GSM_RE.search(completion) if match: match_str = match.group(1).strip() match_str = match_str.replace(",", "") return match_str return None class GSM8K(Task): def __init__(self, subset, split, **kwargs): super().__init__(**kwargs) assert subset in ["main", "socratic"], "GSM8K subset must be main|socratic" assert split in ["train", "test"], "GSM8K split must be train|test" self.ds = load_dataset("openai/gsm8k", subset, split=split).shuffle(seed=42) @property def eval_type(self): return 'generative' def num_examples(self): return len(self.ds) def get_example(self, index): """ Get a single problem from the dataset. """ row = self.ds[index] question = row['question'] # string of the question prompt answer = row['answer'] # string of the full solution and the answer after #### marker # Create and return the Conversation object # This is tricky because GSM8K uses tool calls, which we need to parse here. assistant_message_parts = [] parts = re.split(r'(<<[^>]+>>)', answer) for part in parts: if part.startswith('<<') and part.endswith('>>'): # This is a calculator tool call inner = part[2:-2] # Remove << >> # Split on = to get expression and result if '=' in inner: expr, result = inner.rsplit('=', 1) else: expr, result = inner, "" # Add the tool call as a part assistant_message_parts.append({"type": "python", "text": expr}) # Add the result as a part assistant_message_parts.append({"type": "python_output", "text": result}) else: # Regular text in between tool calls assistant_message_parts.append({"type": "text", "text": part}) # Now put it all together messages = [ {"role": "user", "content": question}, # note: simple string {"role": "assistant", "content": assistant_message_parts}, # note: list of parts (as dicts) ] conversation = { "messages": messages, } return conversation def evaluate(self, conversation, assistant_response): """ Given (conversation, completion), return evaluation outcome (0 = wrong, 1 = correct) Note that: - the conversation has both user AND assistant message (containing the ground truth answer) - the assistant_response is usually the alternative assistant message achieved via sampling TODO: Technically, assistant_response should be a Message (either a string or a list of parts) We can handle this later possibly. For now just assume string. """ assert isinstance(assistant_response, str), "Assuming simple string response for now" # First extract the ground truth answer assistant_message = conversation['messages'][-1] assert assistant_message['role'] == "assistant", "Last message must be from the Assistant" assert isinstance(assistant_message['content'], list), "This is expected to be a list of parts" last_text_part = assistant_message['content'][-1]['text'] # this contains the final answer in GSM8K # Extract both the ground truth answer and the predicted answer ref_num = extract_answer(last_text_part) pred_num = extract_answer(assistant_response) # Compare and return the success as int is_correct = int(pred_num == ref_num) return is_correct def reward(self, conversation, assistant_response): """ Used during RL. To keep things simple, just re-use the evaluation above. Later this could be made more complex (e.g. format matching etc.) """ is_correct = self.evaluate(conversation, assistant_response) is_correct_float = float(is_correct) return is_correct_float
{ "repo_id": "karpathy/nanochat", "file_path": "tasks/gsm8k.py", "license": "MIT License", "lines": 102, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
karpathy/nanochat:tasks/humaneval.py
""" Evaluate the Chat model on HumanEval dataset. Btw this dataset is a misnomer and has nothing to do with humans. It is a coding benchmark. """ import re from datasets import load_dataset from nanochat.execution import execute_code from tasks.common import Task def extract_imports(prompt): """Extract import statements from the beginning of a code block.""" imports = [] for line in prompt.split('\n'): stripped = line.strip() if stripped.startswith('import ') or stripped.startswith('from '): imports.append(stripped) elif stripped and not stripped.startswith('#'): # Stop at first non-import, non-comment line break return '\n'.join(imports) def extract_program(completion): """ Extract Python code from LLM completion. Handles various output formats: - Code wrapped in ```python ... ``` or ``` ... ``` blocks - Plain code without markdown blocks - Extra text before/after code blocks Returns the first code block if found, otherwise returns the whole completion. """ # Try to find markdown code blocks (```python or just ```) # Match ```python\n...\n``` or ```\n...\n``` pattern = r'```(?:python)?\s*\n(.*?)\n```' matches = re.findall(pattern, completion, re.DOTALL) if matches: # Return the first code block found return matches[0].strip() # No code blocks found, return the whole completion return completion.strip() class HumanEval(Task): def __init__(self, **kwargs): super().__init__(**kwargs) self.ds = load_dataset("openai/openai_humaneval", split="test").shuffle(seed=42) @property def eval_type(self): return 'generative' def num_examples(self): return len(self.ds) def get_example(self, index): """ Get a single problem from the dataset. """ row = self.ds[index] prompt = row['prompt'] # prompts in HumanEval are the beginning of the program solution = row['canonical_solution'] # the correct continuation of the program entry_point = row['entry_point'] # the function to check test = row['test'] # the test cases complete_solution = f"{prompt}\n{solution}" messages = [ {"role": "user", "content": prompt}, {"role": "assistant", "content": complete_solution}, ] conversation = { "messages": messages, "entry_point": entry_point, # needed during evaluation "test": test, # needed during evaluation } return conversation def evaluate(self, conversation, completion): """ Given (conversation, completion), return boolean success of the completion. """ # the prompt will contain the imports and the function signature imports = extract_imports(conversation['messages'][0]['content']) # the completion will usually contain the whole function # but not always with the needed imports, so we manually append them completion_code = extract_program(completion) program = ( imports + "\n\n" + completion_code + "\n\n" + conversation['test'] + "\n" + f"check({conversation['entry_point']})" ) result = execute_code(program) success = result.success return success
{ "repo_id": "karpathy/nanochat", "file_path": "tasks/humaneval.py", "license": "MIT License", "lines": 84, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
karpathy/nanochat:tasks/smoltalk.py
""" SmolTalk by HuggingFace. Good "general" conversational dataset. https://huggingface.co/datasets/HuggingFaceTB/smol-smoltalk We use the "smol" version, which is more appropriate for smaller models. """ from datasets import load_dataset from tasks.common import Task class SmolTalk(Task): """ smol-smoltalk dataset. train is 460K rows, test is 24K rows. """ def __init__(self, split, **kwargs): super().__init__(**kwargs) assert split in ["train", "test"], "SmolTalk split must be train|test" self.ds = load_dataset("HuggingFaceTB/smol-smoltalk", split=split).shuffle(seed=42) self.length = len(self.ds) def num_examples(self): return self.length def get_example(self, index): row = self.ds[index] messages = row["messages"] # --------------------------------------------------------------------- # sanity checking asserts here # TODO: we could remove these asserts later, for now just don't want any footguns # there is an optional system message at the beginning assert len(messages) >= 1 first_message = messages[0] if first_message["role"] == "system": rest_messages = messages[1:] # optional system message is OK else: rest_messages = messages assert len(rest_messages) >= 2, "SmolTalk messages must have at least 2 messages" for i, message in enumerate(rest_messages): # user and assistant alternate as user,assistant,user,assistant,... expected_role = "user" if i % 2 == 0 else "assistant" assert message["role"] == expected_role, f"Message {i} has role {message['role']} but should be {expected_role}" assert isinstance(message["content"], str), "Content must be a string" # --------------------------------------------------------------------- # create and return the Conversation object (ok to emit the system message too) conversation = { "messages": messages, } return conversation
{ "repo_id": "karpathy/nanochat", "file_path": "tasks/smoltalk.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/data_structures/veb_tree.py
""" Van Emde Boas Tree (vEB Tree) / van Emde Boas priority queue Reference: https://en.wikipedia.org/wiki/Van_Emde_Boas_tree A van Emde Boas tree is a recursive data structure for storing integers from a fixed universe [0, u - 1], where u is a power of 2. Time complexity: insert / delete / successor / member : O(log log u) min / max : O(1) Space complexity: O(u) """ import math class VEBTree: """ Van Emde Boas tree supporting fast predecessor/successor queries. Attributes: u (int): Universe size (power of 2) min (int | None): Minimum element in the tree max (int | None): Maximum element in the tree summary (VEBTree | None): Summary tree cluster (list[VEBTree] | None): Array of clusters """ def __init__(self, universe_size): """ Initialize a Van Emde Boas tree. Args: universe_size (int): Size of the universe; must be a power of 2 and > 0. Raises: TypeError: If universe_size is not an integer. ValueError: If universe_size <= 0 or not a power of 2. """ if not isinstance(universe_size, int): raise TypeError("universe_size must be an integer.") if not universe_size > 0: raise ValueError("universe_size must be greater than 0.") if not (universe_size & (universe_size - 1)) == 0: raise ValueError("universe_size must be a power of 2.") self.u = universe_size self.min = None self.max = None if universe_size <= 2: self.summary = None self.cluster = None else: self.lower_sqrt = 2 ** (math.floor(math.log2(universe_size) / 2)) self.upper_sqrt = 2 ** (math.ceil(math.log2(universe_size) / 2)) self.summary = VEBTree(self.upper_sqrt) self.cluster = [VEBTree(self.lower_sqrt) for _ in range(self.upper_sqrt)] def _validate_key(self, x): """ Check if x is within the universe range. Args: x (int): Element to validate. Raises: ValueError: If x is not in the range [0, u-1]. """ if not (0 <= x < self.u): raise ValueError(f"Key {x} out of universe range [0, {self.u - 1}]") def high(self, x): """ Return the high part (cluster index) of element x. Args: x (int): Element to split. Returns: int: Cluster index corresponding to x. """ return x // self.lower_sqrt def low(self, x): """ Return the low part (position within cluster) of element x. Args: x (int): Element to split. Returns: int: Position within cluster corresponding to x. """ return x % self.lower_sqrt def index(self, high, low): """ Combine high and low parts to get original element. Args: high (int): Cluster index. low (int): Position within cluster. Returns: int: Original element corresponding to high and low. """ return high * self.lower_sqrt + low def empty_insert(self, x): """ Insert x into an empty vEB tree (sets min and max). Args: x (int): Element to insert. """ self.min = self.max = x def insert(self, x): """ Insert an element into the Van Emde Boas tree. Args: x (int): Element to insert; must be in the universe [0, u-1]. Raises: ValueError: If x is outside the universe. """ self._validate_key(x) if self.min is None: self.empty_insert(x) return if x < self.min: x, self.min = self.min, x if self.u > 2: high = self.high(x) low = self.low(x) if self.cluster[high].min is None: self.summary.insert(high) self.cluster[high].empty_insert(low) else: self.cluster[high].insert(low) if x > self.max: self.max = x def member(self, x): """ Check whether element x exists in the tree. Args: x (int): Element to check. Returns: bool: True if x exists, False otherwise. Raises: ValueError: If x is outside the universe. """ self._validate_key(x) if x == self.min or x == self.max: return True elif self.u == 2: return False else: return self.cluster[self.high(x)].member(self.low(x)) def successor(self, x): """ Return the smallest element greater than x in the tree. Args: x (int): Element to find successor for. Returns: int | None: Successor of x if exists, otherwise None. Raises: ValueError: If x is outside the universe. """ self._validate_key(x) if self.u == 2: if x == 0 and self.max == 1: return 1 return None if self.min is not None and x < self.min: return self.min high = self.high(x) low = self.low(x) max_low = self.cluster[high].max if max_low is not None and low < max_low: offset = self.cluster[high].successor(low) return self.index(high, offset) else: succ_cluster = self.summary.successor(high) if succ_cluster is None: return None offset = self.cluster[succ_cluster].min return self.index(succ_cluster, offset) def delete(self, x): """ Remove element x from the Van Emde Boas tree. Args: x (int): Element to delete. Raises: ValueError: If x is outside the universe. """ self._validate_key(x) if self.min == self.max: self.min = self.max = None return if self.u == 2: if x == 0: self.min = 1 else: self.min = 0 self.max = self.min return if x == self.min: first_cluster = self.summary.min x = self.index(first_cluster, self.cluster[first_cluster].min) self.min = x high = self.high(x) low = self.low(x) self.cluster[high].delete(low) if self.cluster[high].min is None: self.summary.delete(high) if x == self.max: summary_max = self.summary.max if summary_max is None: self.max = self.min else: self.max = self.index(summary_max, self.cluster[summary_max].max) elif x == self.max: self.max = self.index(high, self.cluster[high].max) def minimum(self): """ Get the minimum element in the tree. Returns: int | None: Minimum element, or None if tree is empty. """ return self.min def maximum(self): """ Get the maximum element in the tree. Returns: int | None: Maximum element, or None if tree is empty. """ return self.max
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/veb_tree.py", "license": "MIT License", "lines": 213, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:tests/test_veb_tree.py
import unittest from algorithms.data_structures.veb_tree import VEBTree class TestVEBTree(unittest.TestCase): def setUp(self): self.veb = VEBTree(16) def test_insert_and_member(self): values = [2, 3, 4, 7, 14] for v in values: self.veb.insert(v) for v in values: self.assertTrue(self.veb.member(v)) self.assertFalse(self.veb.member(5)) def test_min_max(self): self.veb.insert(10) self.veb.insert(2) self.veb.insert(15) self.assertEqual(2, self.veb.minimum()) self.assertEqual(15, self.veb.maximum()) def test_successor(self): for v in [2, 4, 8, 12]: self.veb.insert(v) self.assertEqual(4, self.veb.successor(2)) self.assertEqual(8, self.veb.successor(4)) self.assertIsNone(self.veb.successor(12)) def test_delete(self): for v in [1, 3, 5, 7]: self.veb.insert(v) self.veb.delete(3) self.assertFalse(self.veb.member(3)) self.assertEqual(5, self.veb.successor(1)) def test_invalid_universe(self): with self.assertRaises(ValueError): VEBTree(15) # not power of 2 if __name__ == "__main__": unittest.main()
{ "repo_id": "keon/algorithms", "file_path": "tests/test_veb_tree.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keon/algorithms:algorithms/data_structures/sqrt_decomposition.py
""" Square Root (Sqrt) Decomposition Divides an array into blocks of size √n to allow O(√n) range queries and point updates β€” a simple alternative to segment trees for range-aggregate problems. Supports: - **Range sum queries** in O(√n). - **Point updates** in O(1). Reference: https://cp-algorithms.com/data_structures/sqrt_decomposition.html Complexity: Build: O(n) Query: O(√n) Update: O(1) Space: O(n) """ from __future__ import annotations import math class SqrtDecomposition: """Square root decomposition for range sum queries. Attributes: data: The underlying array. block_size: Size of each block (⌈√nβŒ‰). blocks: Precomputed block sums. Examples: >>> sd = SqrtDecomposition([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> sd.query(0, 8) 45 >>> sd.query(2, 5) 18 >>> sd.update(4, 10) >>> sd.query(0, 8) 50 """ def __init__(self, arr: list[int | float]) -> None: """Build the sqrt decomposition from *arr*. Args: arr: Input array of numbers. """ self.data = list(arr) n = len(self.data) self.block_size = max(1, math.isqrt(n)) num_blocks = (n + self.block_size - 1) // self.block_size self.blocks: list[int | float] = [0] * num_blocks for i, val in enumerate(self.data): self.blocks[i // self.block_size] += val def update(self, index: int, value: int | float) -> None: """Set ``data[index]`` to *value* and update the block sum. Args: index: Array index to update. value: New value. Raises: IndexError: If *index* is out of range. Examples: >>> sd = SqrtDecomposition([1, 2, 3]) >>> sd.update(1, 10) >>> sd.query(0, 2) 14 """ if index < 0 or index >= len(self.data): msg = f"index {index} out of range for length {len(self.data)}" raise IndexError(msg) block = index // self.block_size self.blocks[block] += value - self.data[index] self.data[index] = value def query(self, left: int, right: int) -> int | float: """Return the sum of elements from *left* to *right* inclusive. Args: left: Start index (inclusive). right: End index (inclusive). Returns: Sum of ``data[left..right]``. Raises: IndexError: If indices are out of range. Examples: >>> sd = SqrtDecomposition([1, 2, 3, 4, 5]) >>> sd.query(1, 3) 9 """ if left < 0 or right >= len(self.data) or left > right: msg = f"invalid range [{left}, {right}] for length {len(self.data)}" raise IndexError(msg) total: int | float = 0 block_left = left // self.block_size block_right = right // self.block_size if block_left == block_right: # Same block β€” iterate directly for i in range(left, right + 1): total += self.data[i] else: # Partial left block for i in range(left, (block_left + 1) * self.block_size): total += self.data[i] # Full middle blocks for b in range(block_left + 1, block_right): total += self.blocks[b] # Partial right block for i in range(block_right * self.block_size, right + 1): total += self.data[i] return total
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/sqrt_decomposition.py", "license": "MIT License", "lines": 99, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/dijkstra_heapq.py
""" Dijkstra's Shortest-Path Algorithm (Heap-Optimised) Computes single-source shortest paths in a graph with non-negative edge weights using a min-heap (priority queue) for efficient vertex selection. This adjacency-list implementation is faster than the O(VΒ²) matrix version for sparse graphs. Reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm Complexity: Time: O((V + E) log V) using a binary heap Space: O(V + E) """ from __future__ import annotations import heapq def dijkstra( graph: dict[str, dict[str, int | float]], source: str, target: str = "", ) -> tuple[int | float, list[str]]: """Return the shortest distance and path from *source* to *target*. Args: graph: Adjacency-list mapping each vertex to a dict of {neighbour: weight}. source: Starting vertex. target: Destination vertex. When empty, compute shortest distances to all reachable vertices and return the path to the last vertex relaxed (mainly useful when a target is provided). Returns: A ``(distance, path)`` tuple where *distance* is the total shortest-path cost and *path* is the list of vertices from *source* to *target* inclusive. If *target* is unreachable the distance is ``float('inf')`` and the path is empty. Examples: >>> g = { ... "s": {"a": 2, "b": 1}, ... "a": {"s": 3, "b": 4, "c": 8}, ... "b": {"s": 4, "a": 2, "d": 2}, ... "c": {"a": 2, "d": 7, "t": 4}, ... "d": {"b": 1, "c": 11, "t": 5}, ... "t": {"c": 3, "d": 5}, ... } >>> dijkstra(g, "s", "t") (8, ['s', 'b', 'd', 't']) """ dist: dict[str, int | float] = {v: float("inf") for v in graph} dist[source] = 0 prev: dict[str, str | None] = {v: None for v in graph} heap: list[tuple[int | float, str]] = [(0, source)] while heap: d, u = heapq.heappop(heap) if d > dist[u]: continue if u == target: break for v, weight in graph[u].items(): alt = dist[u] + weight if alt < dist.get(v, float("inf")): dist[v] = alt prev[v] = u heapq.heappush(heap, (alt, v)) # Reconstruct path path: list[str] = [] node: str | None = target if target else None if node and dist.get(node, float("inf")) < float("inf"): while node is not None: path.append(node) node = prev.get(node) path.reverse() return (dist.get(target, float("inf")), path) if target else (0, [])
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/dijkstra_heapq.py", "license": "MIT License", "lines": 69, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/goldbach.py
""" Goldbach's Conjecture Every even integer greater than 2 can be expressed as the sum of two primes. This module provides a function to find such a pair of primes and a helper to verify the conjecture over a range. Reference: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture Complexity: Time: O(n * sqrt(n)) per call (sieve could be used for batch queries) Space: O(1) per call """ from __future__ import annotations def _is_prime(n: int) -> bool: """Return ``True`` if *n* is a prime number. Examples: >>> _is_prime(7) True >>> _is_prime(10) False """ if n < 2: return False if n < 4: return True if n % 2 == 0 or n % 3 == 0: return False i = 5 while i * i <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True def goldbach(n: int) -> tuple[int, int]: """Return two primes whose sum equals *n*. Args: n: An even integer greater than 2. Returns: A tuple ``(p, q)`` with ``p <= q`` and ``p + q == n`` where both ``p`` and ``q`` are prime. Raises: ValueError: If *n* is not an even integer greater than 2. Examples: >>> goldbach(4) (2, 2) >>> goldbach(28) (5, 23) >>> p, q = goldbach(100) >>> p + q == 100 and _is_prime(p) and _is_prime(q) True """ if n <= 2 or n % 2 != 0: msg = f"n must be an even integer greater than 2, got {n}" raise ValueError(msg) for i in range(2, n // 2 + 1): if _is_prime(i) and _is_prime(n - i): return (i, n - i) # Should never be reached if Goldbach's conjecture holds msg = f"no prime pair found for {n}" # pragma: no cover raise RuntimeError(msg) # pragma: no cover def verify_goldbach(limit: int) -> bool: """Verify Goldbach's conjecture for all even numbers from 4 to *limit*. Args: limit: Upper bound (inclusive) for verification. Returns: ``True`` if every even number in range can be expressed as a sum of two primes. Examples: >>> verify_goldbach(100) True >>> verify_goldbach(1000) True """ return all(goldbach(n) is not None for n in range(4, limit + 1, 2))
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/goldbach.py", "license": "MIT License", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/tree/binary_tree_views.py
""" Binary Tree Views Compute different "views" of a binary tree β€” the nodes visible when looking at the tree from a particular direction. - **Left view**: first node at each level (leftmost). - **Right view**: last node at each level (rightmost). - **Top view**: nodes visible when looking from above. - **Bottom view**: nodes visible when looking from below (last node at each horizontal distance). Reference: https://en.wikipedia.org/wiki/Binary_tree Complexity (all views): Time: O(n) β€” each node is visited once Space: O(n) β€” queue / dict storage """ from __future__ import annotations from collections import deque from algorithms.common.tree_node import TreeNode def left_view(root: TreeNode | None) -> list[int]: """Return the values visible from the left side of the tree. Args: root: Root of the binary tree. Returns: List of node values, one per level, from the left. Examples: >>> from algorithms.common.tree_node import TreeNode >>> root = TreeNode(1, TreeNode(2, TreeNode(4)), TreeNode(3)) >>> left_view(root) [1, 2, 4] """ if root is None: return [] result: list[int] = [] queue: deque[TreeNode] = deque([root]) while queue: level_size = len(queue) for i in range(level_size): node = queue.popleft() if i == 0: result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) return result def right_view(root: TreeNode | None) -> list[int]: """Return the values visible from the right side of the tree. Args: root: Root of the binary tree. Returns: List of node values, one per level, from the right. Examples: >>> from algorithms.common.tree_node import TreeNode >>> root = TreeNode(1, TreeNode(2, TreeNode(4)), TreeNode(3)) >>> right_view(root) [1, 3, 4] """ if root is None: return [] result: list[int] = [] queue: deque[TreeNode] = deque([root]) while queue: level_size = len(queue) for i in range(level_size): node = queue.popleft() if i == level_size - 1: result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) return result def top_view(root: TreeNode | None) -> list[int]: """Return the values visible when looking at the tree from above. Nodes are ordered by horizontal distance from root (left to right). Args: root: Root of the binary tree. Returns: List of node values visible from the top. Examples: >>> from algorithms.common.tree_node import TreeNode >>> root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), ... TreeNode(3, None, TreeNode(6))) >>> top_view(root) [4, 2, 1, 3, 6] """ if root is None: return [] # Map: horizontal distance -> first node value seen (BFS ensures top-most) hd_map: dict[int, int] = {} queue: deque[tuple[TreeNode, int]] = deque([(root, 0)]) while queue: node, hd = queue.popleft() if hd not in hd_map: hd_map[hd] = node.val if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) return [hd_map[k] for k in sorted(hd_map)] def bottom_view(root: TreeNode | None) -> list[int]: """Return the values visible when looking at the tree from below. Nodes are ordered by horizontal distance from root (left to right). When multiple nodes share the same horizontal distance, the last one encountered in level-order (bottommost) wins. Args: root: Root of the binary tree. Returns: List of node values visible from the bottom. Examples: >>> from algorithms.common.tree_node import TreeNode >>> root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), ... TreeNode(3, None, TreeNode(6))) >>> bottom_view(root) [4, 2, 5, 3, 6] """ if root is None: return [] hd_map: dict[int, int] = {} queue: deque[tuple[TreeNode, int]] = deque([(root, 0)]) while queue: node, hd = queue.popleft() hd_map[hd] = node.val # overwrite β†’ keeps bottommost if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) return [hd_map[k] for k in sorted(hd_map)]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/tree/binary_tree_views.py", "license": "MIT License", "lines": 127, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:tests/test_issue_fixes.py
"""Tests for algorithms added to resolve open GitHub issues.""" from __future__ import annotations import unittest from algorithms.common.tree_node import TreeNode from algorithms.data_structures import SqrtDecomposition from algorithms.graph.dijkstra_heapq import dijkstra from algorithms.math.goldbach import goldbach, verify_goldbach from algorithms.tree.binary_tree_views import ( bottom_view, left_view, right_view, top_view, ) # ── Dijkstra with priority queue (#565) ──────────────────────────────── class TestDijkstraHeapq(unittest.TestCase): """Tests for the heap-based Dijkstra implementation.""" def setUp(self): self.graph = { "s": {"a": 2, "b": 1}, "a": {"s": 3, "b": 4, "c": 8}, "b": {"s": 4, "a": 2, "d": 2}, "c": {"a": 2, "d": 7, "t": 4}, "d": {"b": 1, "c": 11, "t": 5}, "t": {"c": 3, "d": 5}, } def test_shortest_path(self): dist, path = dijkstra(self.graph, "s", "t") self.assertEqual(dist, 8) self.assertEqual(path, ["s", "b", "d", "t"]) def test_same_source_and_target(self): dist, path = dijkstra(self.graph, "s", "s") self.assertEqual(dist, 0) self.assertEqual(path, ["s"]) def test_direct_neighbor(self): dist, path = dijkstra(self.graph, "s", "b") self.assertEqual(dist, 1) self.assertEqual(path, ["s", "b"]) def test_unreachable_target(self): graph = {"a": {"b": 1}, "b": {}, "c": {}} dist, path = dijkstra(graph, "a", "c") self.assertEqual(dist, float("inf")) self.assertEqual(path, []) def test_single_node(self): graph = {"a": {}} dist, path = dijkstra(graph, "a", "a") self.assertEqual(dist, 0) self.assertEqual(path, ["a"]) def test_triangle(self): graph = {"a": {"b": 1, "c": 4}, "b": {"c": 2}, "c": {}} dist, path = dijkstra(graph, "a", "c") self.assertEqual(dist, 3) self.assertEqual(path, ["a", "b", "c"]) # ── Goldbach's conjecture (#908) ──────────────────────────────────────── class TestGoldbach(unittest.TestCase): """Tests for Goldbach's conjecture decomposition.""" def test_small_even(self): self.assertEqual(goldbach(4), (2, 2)) def test_goldbach_28(self): p, q = goldbach(28) self.assertEqual(p + q, 28) self.assertTrue(p <= q) def test_goldbach_100(self): p, q = goldbach(100) self.assertEqual(p + q, 100) def test_goldbach_large(self): p, q = goldbach(1000) self.assertEqual(p + q, 1000) def test_odd_raises(self): with self.assertRaises(ValueError): goldbach(7) def test_two_raises(self): with self.assertRaises(ValueError): goldbach(2) def test_negative_raises(self): with self.assertRaises(ValueError): goldbach(-4) def test_verify_range(self): self.assertTrue(verify_goldbach(200)) # ── Binary tree views (#829) ──────────────────────────────────────────── class TestBinaryTreeViews(unittest.TestCase): """Tests for left/right/top/bottom tree views.""" def setUp(self): """Build a tree: 1 / \\ 2 3 / \\ \\ 4 5 6 """ self.root = TreeNode( 1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, None, TreeNode(6)), ) def test_left_view(self): self.assertEqual(left_view(self.root), [1, 2, 4]) def test_right_view(self): self.assertEqual(right_view(self.root), [1, 3, 6]) def test_top_view(self): self.assertEqual(top_view(self.root), [4, 2, 1, 3, 6]) def test_bottom_view(self): self.assertEqual(bottom_view(self.root), [4, 2, 5, 3, 6]) def test_empty_tree(self): self.assertEqual(left_view(None), []) self.assertEqual(right_view(None), []) self.assertEqual(top_view(None), []) self.assertEqual(bottom_view(None), []) def test_single_node(self): root = TreeNode(42) self.assertEqual(left_view(root), [42]) self.assertEqual(right_view(root), [42]) self.assertEqual(top_view(root), [42]) self.assertEqual(bottom_view(root), [42]) def test_left_skewed(self): """ 1 / 2 / 3 """ root = TreeNode(1, TreeNode(2, TreeNode(3))) self.assertEqual(left_view(root), [1, 2, 3]) self.assertEqual(right_view(root), [1, 2, 3]) def test_right_skewed(self): """ 1 \\ 2 \\ 3 """ root = TreeNode(1, None, TreeNode(2, None, TreeNode(3))) self.assertEqual(left_view(root), [1, 2, 3]) self.assertEqual(right_view(root), [1, 2, 3]) # ── Square root decomposition (#651) ──────────────────────────────────── class TestSqrtDecomposition(unittest.TestCase): """Tests for square root decomposition range queries.""" def test_full_range_sum(self): sd = SqrtDecomposition([1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(sd.query(0, 8), 45) def test_partial_range(self): sd = SqrtDecomposition([1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(sd.query(2, 5), 18) # 3+4+5+6 def test_single_element(self): sd = SqrtDecomposition([10, 20, 30]) self.assertEqual(sd.query(1, 1), 20) def test_update(self): sd = SqrtDecomposition([1, 2, 3, 4, 5]) sd.update(2, 10) self.assertEqual(sd.query(0, 4), 22) # 1+2+10+4+5 def test_update_full_range(self): sd = SqrtDecomposition([1, 2, 3, 4, 5, 6, 7, 8, 9]) sd.update(4, 10) self.assertEqual(sd.query(0, 8), 50) def test_multiple_updates(self): sd = SqrtDecomposition([1, 1, 1, 1, 1]) sd.update(0, 5) sd.update(4, 5) self.assertEqual(sd.query(0, 4), 13) # 5+1+1+1+5 def test_out_of_range_update(self): sd = SqrtDecomposition([1, 2, 3]) with self.assertRaises(IndexError): sd.update(5, 10) def test_invalid_query(self): sd = SqrtDecomposition([1, 2, 3]) with self.assertRaises(IndexError): sd.query(0, 5) def test_small_array(self): sd = SqrtDecomposition([42]) self.assertEqual(sd.query(0, 0), 42) sd.update(0, 100) self.assertEqual(sd.query(0, 0), 100) def test_larger_array(self): arr = list(range(1, 101)) # 1..100 sd = SqrtDecomposition(arr) self.assertEqual(sd.query(0, 99), 5050) self.assertEqual(sd.query(0, 9), 55) # 1+2+...+10 if __name__ == "__main__": unittest.main()
{ "repo_id": "keon/algorithms", "file_path": "tests/test_issue_fixes.py", "license": "MIT License", "lines": 180, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keon/algorithms:algorithms/backtracking/minimax.py
"""Minimax β€” game-tree search with alpha-beta pruning. The minimax algorithm finds the optimal move for a two-player zero-sum game. Alpha-beta pruning reduces the search space by eliminating branches that cannot influence the final decision. Inspired by PR #860 (DD2480-group16). """ from __future__ import annotations import math def minimax( depth: int, is_maximizing: bool, scores: list[int], alpha: float = -math.inf, beta: float = math.inf, ) -> float: """Return the minimax value of a perfect binary game tree. *scores* contains the leaf values (length must be a power of 2). *depth* is the current depth (start with log2(len(scores))). >>> minimax(2, True, [3, 5, 2, 9]) 5 >>> minimax(3, True, [3, 5, 2, 9, 12, 5, 23, 23]) 12 """ if depth == 0: return scores[0] mid = len(scores) // 2 if is_maximizing: value = -math.inf value = max( value, minimax(depth - 1, False, scores[:mid], alpha, beta), ) alpha = max(alpha, value) if alpha < beta: value = max( value, minimax(depth - 1, False, scores[mid:], alpha, beta), ) return value else: value = math.inf value = min( value, minimax(depth - 1, True, scores[:mid], alpha, beta), ) beta = min(beta, value) if alpha < beta: value = min( value, minimax(depth - 1, True, scores[mid:], alpha, beta), ) return value
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/minimax.py", "license": "MIT License", "lines": 52, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/bit_manipulation/gray_code.py
"""Gray code β€” generate n-bit Gray code sequences. A Gray code is an ordering of binary numbers such that successive values differ in exactly one bit. Used in error correction and rotary encoders. Inspired by PR #932 (Simranstha045). """ from __future__ import annotations def gray_code(n: int) -> list[int]: """Return the n-bit Gray code sequence as a list of integers. Uses the reflection (mirror) construction: gray(i) = i ^ (i >> 1) >>> gray_code(2) [0, 1, 3, 2] >>> gray_code(3) [0, 1, 3, 2, 6, 7, 5, 4] """ return [i ^ (i >> 1) for i in range(1 << n)] def gray_to_binary(gray: int) -> int: """Convert a Gray-coded integer back to standard binary.""" mask = gray >> 1 while mask: gray ^= mask mask >>= 1 return gray
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/gray_code.py", "license": "MIT License", "lines": 23, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/kd_tree.py
"""KD-tree β€” a space-partitioning tree for k-dimensional points. Supports efficient nearest-neighbour and range queries. Inspired by PR #915 (gjones1077). """ from __future__ import annotations import math from typing import Any class KDNode: """A single node in a KD-tree.""" __slots__ = ("point", "left", "right", "axis") def __init__( self, point: tuple[float, ...], left: KDNode | None = None, right: KDNode | None = None, axis: int = 0, ) -> None: self.point = point self.left = left self.right = right self.axis = axis class KDTree: """A k-dimensional tree built from a list of points. >>> tree = KDTree([(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2)]) >>> tree.nearest((9, 2)) (8, 1) """ def __init__(self, points: list[tuple[float, ...]]) -> None: self.k = len(points[0]) if points else 0 self.root = self._build(list(points), depth=0) def _build(self, points: list[tuple[float, ...]], depth: int) -> KDNode | None: if not points: return None axis = depth % self.k points.sort(key=lambda p: p[axis]) mid = len(points) // 2 return KDNode( point=points[mid], left=self._build(points[:mid], depth + 1), right=self._build(points[mid + 1 :], depth + 1), axis=axis, ) def nearest(self, target: tuple[float, ...]) -> tuple[float, ...]: """Return the point closest to *target*.""" best: list[Any] = [None, math.inf] self._nearest(self.root, target, best) return best[0] def _nearest( self, node: KDNode | None, target: tuple[float, ...], best: list[Any], ) -> None: if node is None: return dist = _sq_dist(node.point, target) if dist < best[1]: best[0], best[1] = node.point, dist axis = node.axis diff = target[axis] - node.point[axis] close, away = (node.left, node.right) if diff <= 0 else (node.right, node.left) self._nearest(close, target, best) if diff * diff < best[1]: self._nearest(away, target, best) def _sq_dist(a: tuple[float, ...], b: tuple[float, ...]) -> float: return sum((x - y) ** 2 for x, y in zip(a, b, strict=False))
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/kd_tree.py", "license": "MIT License", "lines": 66, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/dynamic_programming/bitmask.py
"""Bitmask dynamic programming β€” Travelling Salesman Problem (TSP). Uses DP with bitmask to find the minimum-cost Hamiltonian cycle in a weighted graph. The state (visited_mask, current_city) encodes which cities have been visited and where we are now. Time: O(2^n * n^2). Space: O(2^n * n). Inspired by PR #855 (AmandaStromdahl). """ from __future__ import annotations import math def tsp(dist: list[list[float]]) -> float: """Return the minimum cost of a Hamiltonian cycle. *dist* is an n x n distance matrix. >>> tsp([[0, 10, 15, 20], ... [10, 0, 35, 25], ... [15, 35, 0, 30], ... [20, 25, 30, 0]]) 80 """ n = len(dist) full_mask = (1 << n) - 1 dp: dict[tuple[int, int], float] = {} def solve(mask: int, pos: int) -> float: if mask == full_mask: return dist[pos][0] key = (mask, pos) if key in dp: return dp[key] ans = math.inf for city in range(n): if not (mask & (1 << city)): ans = min(ans, dist[pos][city] + solve(mask | (1 << city), city)) dp[key] = ans return ans return solve(1, 0)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/bitmask.py", "license": "MIT License", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/count_paths_dp.py
"""Count paths in a grid β€” recursive, memoized, and bottom-up DP. Count the number of unique paths from the top-left to the bottom-right of an m x n grid, moving only right or down. Inspired by PR #857 (c-cret). """ from __future__ import annotations from functools import cache def count_paths_recursive(m: int, n: int) -> int: """Naive recursive solution β€” O(2^(m+n)).""" if m == 1 or n == 1: return 1 return count_paths_recursive(m - 1, n) + count_paths_recursive(m, n - 1) def count_paths_memo(m: int, n: int) -> int: """Top-down DP with memoization β€” O(m*n).""" @cache def helper(i: int, j: int) -> int: if i == 1 or j == 1: return 1 return helper(i - 1, j) + helper(i, j - 1) return helper(m, n) def count_paths_dp(m: int, n: int) -> int: """Bottom-up DP β€” O(m*n) time, O(n) space. >>> count_paths_dp(3, 7) 28 >>> count_paths_dp(3, 3) 6 """ row = [1] * n for _ in range(1, m): for j in range(1, n): row[j] += row[j - 1] return row[n - 1]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/count_paths_dp.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/blossom.py
"""Edmonds' blossom algorithm β€” maximum cardinality matching. Finds a maximum matching in a general (non-bipartite) undirected graph. The algorithm handles odd-length cycles ("blossoms") by contracting them and recursing. Time: O(V^2 * E). Inspired by PR #826 (abhishekiitm). """ from __future__ import annotations from collections import deque def max_matching(n: int, edges: list[tuple[int, int]]) -> list[tuple[int, int]]: """Return a maximum cardinality matching for an undirected graph. *n* is the number of vertices (0..n-1). *edges* is a list of (u, v) edges. Returns a list of matched (u, v) pairs. >>> sorted(max_matching(4, [(0,1),(1,2),(2,3)])) [(0, 1), (2, 3)] """ adj: list[list[int]] = [[] for _ in range(n)] for u, v in edges: adj[u].append(v) adj[v].append(u) match = [-1] * n def find_augmenting_path() -> bool: """Try to find an augmenting path from any free vertex.""" parent = [-1] * n visited = [False] * n for start in range(n): if match[start] != -1: continue # BFS from this free vertex queue: deque[int] = deque([start]) visited[start] = True found = False while queue and not found: u = queue.popleft() for v in adj[u]: if visited[v] or v == match[u]: continue if match[v] == -1 and v != start: # Augmenting path found β€” trace back and flip _augment(parent, match, u, v) return True if match[v] != -1: visited[v] = True visited[match[v]] = True parent[match[v]] = u queue.append(match[v]) return False while find_augmenting_path(): pass result = [] seen = set() for i in range(n): if match[i] != -1 and i not in seen: result.append((i, match[i])) seen.add(i) seen.add(match[i]) return result def _augment(parent: list[int], match: list[int], u: int, v: int) -> None: """Augment the matching along the path ending with edge (u, v).""" while u != -1: prev = match[u] match[u] = v match[v] = u v = prev u = parent[v] if v != -1 else -1
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/blossom.py", "license": "MIT License", "lines": 66, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/math/linear_regression.py
"""Simple linear regression β€” fit a line to (x, y) data. Computes the ordinary least-squares regression line y = mx + b without external libraries. Inspired by PR #871 (MakanFar). """ from __future__ import annotations import math def linear_regression(x: list[float], y: list[float]) -> tuple[float, float]: """Return (slope, intercept) for the best-fit line. >>> m, b = linear_regression([1, 2, 3, 4, 5], [2, 4, 5, 4, 5]) >>> round(m, 4) 0.6 >>> round(b, 4) 2.2 """ n = len(x) if n != len(y) or n < 2: msg = "x and y must have at least 2 equal-length elements" raise ValueError(msg) sum_x = sum(x) sum_y = sum(y) sum_xy = sum(xi * yi for xi, yi in zip(x, y, strict=False)) sum_x2 = sum(xi * xi for xi in x) denom = n * sum_x2 - sum_x * sum_x if denom == 0: msg = "Vertical line β€” slope is undefined" raise ValueError(msg) slope = (n * sum_xy - sum_x * sum_y) / denom intercept = (sum_y - slope * sum_x) / n return slope, intercept def r_squared(x: list[float], y: list[float]) -> float: """Return the R-squared (coefficient of determination) for the fit.""" slope, intercept = linear_regression(x, y) y_mean = sum(y) / len(y) ss_tot = sum((yi - y_mean) ** 2 for yi in y) ss_res = sum( (yi - (slope * xi + intercept)) ** 2 for xi, yi in zip(x, y, strict=False) ) if ss_tot == 0: return 1.0 return 1.0 - ss_res / ss_tot def rmse(x: list[float], y: list[float]) -> float: """Return the root mean squared error for the fit.""" slope, intercept = linear_regression(x, y) n = len(y) return math.sqrt( sum((yi - (slope * xi + intercept)) ** 2 for xi, yi in zip(x, y, strict=False)) / n )
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/linear_regression.py", "license": "MIT License", "lines": 49, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/math/manhattan_distance.py
"""Manhattan distance β€” L1 distance between two points. Also known as taxicab distance or city-block distance, it is the sum of absolute differences of coordinates. Inspired by PR #877 (ChinZhengSheng). """ from __future__ import annotations def manhattan_distance(a: tuple[float, ...], b: tuple[float, ...]) -> float: """Return the Manhattan (L1) distance between points *a* and *b*. Works in any number of dimensions. >>> manhattan_distance((1, 2), (4, 6)) 7 >>> manhattan_distance((0, 0, 0), (1, 2, 3)) 6 """ return sum(abs(x - y) for x, y in zip(a, b, strict=False))
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/manhattan_distance.py", "license": "MIT License", "lines": 15, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/polynomial_division.py
"""Polynomial long division. Divide polynomial *dividend* by *divisor* and return (quotient, remainder). Polynomials are represented as lists of coefficients from highest to lowest degree. E.g. [1, -3, 2] represents x^2 - 3x + 2. Inspired by PR #840 (KTH-Software-Engineering-DD2480). """ from __future__ import annotations def polynomial_division( dividend: list[float], divisor: list[float] ) -> tuple[list[float], list[float]]: """Perform polynomial long division. Returns (quotient_coefficients, remainder_coefficients). >>> polynomial_division([1, -3, 2], [1, -1]) ([1.0, -2.0], [0.0]) >>> polynomial_division([1, 0, -4], [1, -2]) ([1.0, 2.0], [0.0]) """ if not divisor or all(c == 0 for c in divisor): msg = "Cannot divide by zero polynomial" raise ZeroDivisionError(msg) dividend = [float(c) for c in dividend] divisor = [float(c) for c in divisor] remainder = list(dividend) quotient: list[float] = [] divisor_lead = divisor[0] len_diff = len(dividend) - len(divisor) for i in range(len_diff + 1): coeff = remainder[i] / divisor_lead quotient.append(coeff) for j in range(len(divisor)): remainder[i + j] -= coeff * divisor[j] # The remainder is the tail of the array remainder = remainder[len_diff + 1 :] if not remainder: remainder = [0.0] return quotient, remainder
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/polynomial_division.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/searching/exponential_search.py
"""Exponential search β€” locate an element in a sorted array. First finds a range where the target may lie by doubling the index, then performs binary search within that range. Useful when the target is near the beginning of a large or unbounded list. Time: O(log i) where i is the index of the target. Inspired by PR #867 (yuviii99). """ from __future__ import annotations def exponential_search(arr: list[int], target: int) -> int: """Return the index of *target* in sorted *arr*, or -1 if absent.""" if not arr: return -1 if arr[0] == target: return 0 bound = 1 while bound < len(arr) and arr[bound] <= target: bound *= 2 low = bound // 2 high = min(bound, len(arr) - 1) while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/exponential_search.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/searching/sentinel_search.py
"""Sentinel linear search β€” a small optimisation over naive linear search. By placing the target at the end of the array (as a sentinel), we can remove the bounds check from the inner loop, roughly halving comparisons. Time: O(n) β€” same asymptotic complexity but fewer comparisons in practice. Inspired by PR #907 (Abhishek-Pashte). """ from __future__ import annotations def sentinel_search(arr: list[int], target: int) -> int: """Return the index of *target* in *arr*, or -1 if absent. Modifies (and restores) the last element temporarily. """ n = len(arr) if n == 0: return -1 last = arr[-1] arr[-1] = target i = 0 while arr[i] != target: i += 1 arr[-1] = last if i < n - 1 or arr[-1] == target: return i return -1
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/sentinel_search.py", "license": "MIT License", "lines": 23, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/string/alphabet_board_path.py
"""Alphabet board path β€” find moves on a 5x5+1 letter board. Given a board where 'a'-'z' are laid out in rows of 5: a b c d e f g h i j k l m n o p q r s t u v w x y z Return the sequence of moves (U/D/L/R/!) to spell a target word starting from 'a'. Inspired by PR #897 (chnttx). """ from __future__ import annotations def alphabet_board_path(target: str) -> str: """Return move string to spell *target* on the alphabet board.""" moves: list[str] = [] row, col = 0, 0 for ch in target: idx = ord(ch) - ord("a") target_row, target_col = divmod(idx, 5) # Move up/left before down/right to avoid going off-board at 'z' if target_row < row: moves.append("U" * (row - target_row)) if target_col < col: moves.append("L" * (col - target_col)) if target_row > row: moves.append("D" * (target_row - row)) if target_col > col: moves.append("R" * (target_col - col)) moves.append("!") row, col = target_row, target_col return "".join(moves)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/alphabet_board_path.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/manacher.py
"""Manacher's algorithm β€” find the longest palindromic substring in O(n). Manacher's algorithm uses the symmetry of palindromes to avoid redundant comparisons, achieving linear time. It transforms the input to handle both odd- and even-length palindromes uniformly. Inspired by PR #931 (Simranstha045). """ def manacher(s: str) -> str: """Return the longest palindromic substring of *s* in O(n) time.""" # Transform "abc" -> "^#a#b#c#$" so every palindrome has odd length t = "^#" + "#".join(s) + "#$" n = len(t) p = [0] * n # p[i] = radius of palindrome centred at i centre = right = 0 for i in range(1, n - 1): mirror = 2 * centre - i if i < right: p[i] = min(right - i, p[mirror]) while t[i + p[i] + 1] == t[i - p[i] - 1]: p[i] += 1 if i + p[i] > right: centre, right = i, i + p[i] max_len, centre_index = max((v, i) for i, v in enumerate(p)) start = (centre_index - max_len) // 2 return s[start : start + max_len]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/manacher.py", "license": "MIT License", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/string/swap_characters.py
"""Swap characters β€” check if two strings can be made equal by one swap. Given two strings of equal length, determine whether exactly one swap of two characters in the first string can make it equal to the second. Inspired by PR #890 (Thejas-1). """ from __future__ import annotations def can_swap_to_equal(s: str, t: str) -> bool: """Return True if swapping exactly one pair in *s* yields *t*.""" if len(s) != len(t): return False diffs = [(a, b) for a, b in zip(s, t, strict=False) if a != b] return len(diffs) == 2 and diffs[0] == diffs[1][::-1]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/swap_characters.py", "license": "MIT License", "lines": 12, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/z_algorithm.py
"""Z-algorithm β€” linear-time pattern matching via the Z-array. The Z-array for a string S stores at Z[i] the length of the longest substring starting at S[i] that is also a prefix of S. By concatenating pattern + '$' + text, occurrences of the pattern correspond to positions where Z[i] == len(pattern). Inspired by PR #930 (Simranstha045). """ from __future__ import annotations def compute_z_array(s: str) -> list[int]: """Compute the Z-array for string *s* in O(n) time.""" n = len(s) if n == 0: return [] z = [0] * n z[0] = n left = right = 0 for i in range(1, n): if i < right: z[i] = min(right - i, z[i - left]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] > right: left, right = i, i + z[i] return z def z_search(text: str, pattern: str) -> list[int]: """Return all starting indices where *pattern* occurs in *text*.""" if not pattern or not text: return [] concat = pattern + "$" + text z = compute_z_array(concat) m = len(pattern) return [i - m - 1 for i in range(m + 1, len(concat)) if z[i] == m]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/z_algorithm.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:tests/test_community_algorithms.py
"""Tests for community-contributed algorithms. Covers algorithms adopted from open PRs and implemented to match the repo's code standards. """ from __future__ import annotations import unittest from algorithms.backtracking.minimax import minimax from algorithms.bit_manipulation.gray_code import gray_code, gray_to_binary from algorithms.data_structures.kd_tree import KDTree from algorithms.dynamic_programming.bitmask import tsp from algorithms.dynamic_programming.count_paths_dp import ( count_paths_dp, count_paths_memo, count_paths_recursive, ) from algorithms.graph.blossom import max_matching from algorithms.math.linear_regression import linear_regression, r_squared, rmse from algorithms.math.manhattan_distance import manhattan_distance from algorithms.math.polynomial_division import polynomial_division from algorithms.searching.exponential_search import exponential_search from algorithms.searching.sentinel_search import sentinel_search from algorithms.string.alphabet_board_path import alphabet_board_path from algorithms.string.manacher import manacher from algorithms.string.swap_characters import can_swap_to_equal from algorithms.string.z_algorithm import compute_z_array, z_search class TestManacher(unittest.TestCase): def test_odd_palindrome(self): result = manacher("babad") self.assertIn(result, ("bab", "aba")) def test_even_palindrome(self): self.assertEqual(manacher("cbbd"), "bb") def test_single_char(self): self.assertEqual(manacher("a"), "a") def test_full_palindrome(self): self.assertEqual(manacher("racecar"), "racecar") def test_empty(self): self.assertEqual(manacher(""), "") def test_all_same(self): self.assertEqual(manacher("aaaa"), "aaaa") class TestZAlgorithm(unittest.TestCase): def test_z_array_basic(self): z = compute_z_array("aabxaa") self.assertEqual(z[0], 6) self.assertEqual(z[1], 1) self.assertEqual(z[4], 2) def test_z_search_found(self): self.assertEqual(z_search("abxabcabcaby", "abcaby"), [6]) def test_z_search_multiple(self): self.assertEqual(z_search("aaaa", "aa"), [0, 1, 2]) def test_z_search_not_found(self): self.assertEqual(z_search("hello", "xyz"), []) def test_z_search_empty(self): self.assertEqual(z_search("abc", ""), []) self.assertEqual(z_search("", "abc"), []) def test_z_array_empty(self): self.assertEqual(compute_z_array(""), []) class TestGrayCode(unittest.TestCase): def test_2bit(self): self.assertEqual(gray_code(2), [0, 1, 3, 2]) def test_3bit(self): self.assertEqual(gray_code(3), [0, 1, 3, 2, 6, 7, 5, 4]) def test_1bit(self): self.assertEqual(gray_code(1), [0, 1]) def test_0bit(self): self.assertEqual(gray_code(0), [0]) def test_successive_differ_by_one_bit(self): codes = gray_code(4) for i in range(len(codes) - 1): xor = codes[i] ^ codes[i + 1] self.assertTrue(xor & (xor - 1) == 0) # power of 2 def test_gray_to_binary_roundtrip(self): for n in range(16): gray = n ^ (n >> 1) self.assertEqual(gray_to_binary(gray), n) class TestKdTree(unittest.TestCase): def test_nearest_basic(self): points = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2)] tree = KDTree(points) self.assertEqual(tree.nearest((9, 2)), (8, 1)) def test_nearest_exact(self): points = [(1, 1), (2, 2), (3, 3)] tree = KDTree(points) self.assertEqual(tree.nearest((2, 2)), (2, 2)) def test_nearest_3d(self): points = [(0, 0, 0), (1, 1, 1), (2, 2, 2)] tree = KDTree(points) self.assertEqual(tree.nearest((1, 1, 0)), (1, 1, 1)) def test_single_point(self): tree = KDTree([(5, 5)]) self.assertEqual(tree.nearest((0, 0)), (5, 5)) class TestExponentialSearch(unittest.TestCase): def test_found(self): arr = [1, 3, 5, 7, 9, 11, 13, 15] self.assertEqual(exponential_search(arr, 7), 3) def test_first_element(self): self.assertEqual(exponential_search([1, 2, 3], 1), 0) def test_last_element(self): self.assertEqual(exponential_search([1, 2, 3], 3), 2) def test_not_found(self): self.assertEqual(exponential_search([1, 2, 3], 4), -1) def test_empty(self): self.assertEqual(exponential_search([], 1), -1) def test_single_element_found(self): self.assertEqual(exponential_search([42], 42), 0) def test_single_element_not_found(self): self.assertEqual(exponential_search([42], 99), -1) class TestSentinelSearch(unittest.TestCase): def test_found(self): arr = [4, 2, 7, 1, 9] self.assertEqual(sentinel_search(arr, 7), 2) def test_last_element(self): arr = [4, 2, 7, 1, 9] self.assertEqual(sentinel_search(arr, 9), 4) def test_not_found(self): arr = [4, 2, 7, 1, 9] self.assertEqual(sentinel_search(arr, 5), -1) def test_empty(self): self.assertEqual(sentinel_search([], 1), -1) def test_restores_array(self): arr = [1, 2, 3] sentinel_search(arr, 99) self.assertEqual(arr, [1, 2, 3]) class TestManhattanDistance(unittest.TestCase): def test_2d(self): self.assertEqual(manhattan_distance((1, 2), (4, 6)), 7) def test_3d(self): self.assertEqual(manhattan_distance((0, 0, 0), (1, 2, 3)), 6) def test_same_point(self): self.assertEqual(manhattan_distance((3, 4), (3, 4)), 0) def test_negative(self): self.assertEqual(manhattan_distance((-1, -1), (1, 1)), 4) class TestLinearRegression(unittest.TestCase): def test_basic_fit(self): x = [1, 2, 3, 4, 5] y = [2, 4, 5, 4, 5] m, b = linear_regression(x, y) self.assertAlmostEqual(m, 0.6) self.assertAlmostEqual(b, 2.2) def test_perfect_line(self): x = [0, 1, 2, 3] y = [0, 2, 4, 6] m, b = linear_regression(x, y) self.assertAlmostEqual(m, 2.0) self.assertAlmostEqual(b, 0.0) def test_r_squared_perfect(self): x = [0, 1, 2, 3] y = [0, 2, 4, 6] self.assertAlmostEqual(r_squared(x, y), 1.0) def test_rmse_perfect(self): x = [0, 1, 2, 3] y = [0, 2, 4, 6] self.assertAlmostEqual(rmse(x, y), 0.0) def test_rmse_nonperfect(self): x = [1, 2, 3, 4, 5] y = [2, 4, 5, 4, 5] self.assertGreater(rmse(x, y), 0) def test_too_few_points(self): with self.assertRaises(ValueError): linear_regression([1], [2]) class TestPolynomialDivision(unittest.TestCase): def test_basic(self): # (x^2 - 3x + 2) / (x - 1) = (x - 2), remainder 0 q, r = polynomial_division([1, -3, 2], [1, -1]) self.assertEqual(q, [1.0, -2.0]) self.assertEqual(r, [0.0]) def test_with_remainder(self): # (x^2 + 1) / (x - 1) = (x + 1), remainder 2 q, r = polynomial_division([1, 0, 1], [1, -1]) self.assertAlmostEqual(q[0], 1.0) self.assertAlmostEqual(q[1], 1.0) self.assertAlmostEqual(r[0], 2.0) def test_divide_by_zero(self): with self.assertRaises(ZeroDivisionError): polynomial_division([1, 2], [0, 0]) class TestAlphabetBoardPath(unittest.TestCase): def test_leet(self): path = alphabet_board_path("leet") self.assertIn("!", path) # Verify length: must spell 4 chars so exactly 4 '!'s self.assertEqual(path.count("!"), 4) def test_code(self): path = alphabet_board_path("code") self.assertEqual(path.count("!"), 4) def test_z(self): # 'z' is at row 5, col 0 β€” must go down 5 from 'a' path = alphabet_board_path("z") self.assertEqual(path, "DDDDD!") def test_za(self): path = alphabet_board_path("za") self.assertEqual(path, "DDDDD!UUUUU!") class TestSwapCharacters(unittest.TestCase): def test_can_swap(self): self.assertTrue(can_swap_to_equal("ab", "ba")) def test_identical(self): # No differences β€” need exactly 2, so False self.assertFalse(can_swap_to_equal("ab", "ab")) def test_too_many_diffs(self): self.assertFalse(can_swap_to_equal("abc", "xyz")) def test_different_lengths(self): self.assertFalse(can_swap_to_equal("ab", "abc")) def test_one_diff(self): self.assertFalse(can_swap_to_equal("ab", "ac")) class TestMinimax(unittest.TestCase): def test_depth_2(self): # max(min(3,5), min(2,9)) = max(3,2) = 3 self.assertEqual(minimax(2, True, [3, 5, 2, 9]), 3) def test_depth_3(self): self.assertEqual(minimax(3, True, [3, 5, 2, 9, 12, 5, 23, 23]), 12) def test_single_leaf(self): self.assertEqual(minimax(0, True, [42]), 42) def test_minimizing(self): self.assertEqual(minimax(1, False, [3, 5]), 3) class TestTsp(unittest.TestCase): def test_4_cities(self): dist = [ [0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0], ] self.assertEqual(tsp(dist), 80) def test_3_cities(self): dist = [ [0, 1, 2], [1, 0, 3], [2, 3, 0], ] self.assertEqual(tsp(dist), 6) class TestCountPathsDp(unittest.TestCase): def test_3x7(self): self.assertEqual(count_paths_dp(3, 7), 28) self.assertEqual(count_paths_memo(3, 7), 28) self.assertEqual(count_paths_recursive(3, 7), 28) def test_3x3(self): self.assertEqual(count_paths_dp(3, 3), 6) def test_1x1(self): self.assertEqual(count_paths_dp(1, 1), 1) def test_2x2(self): self.assertEqual(count_paths_dp(2, 2), 2) class TestBlossom(unittest.TestCase): def test_path_graph(self): # Path: 0-1-2-3 β†’ max matching has 2 edges matching = max_matching(4, [(0, 1), (1, 2), (2, 3)]) self.assertEqual(len(matching), 2) def test_triangle(self): # Triangle: 0-1-2 β†’ max matching has 1 edge matching = max_matching(3, [(0, 1), (1, 2), (0, 2)]) self.assertEqual(len(matching), 1) def test_empty_graph(self): matching = max_matching(3, []) self.assertEqual(len(matching), 0) def test_complete_4(self): # K4 β†’ max matching has 2 edges edges = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] matching = max_matching(4, edges) self.assertEqual(len(matching), 2) if __name__ == "__main__": unittest.main()
{ "repo_id": "keon/algorithms", "file_path": "tests/test_community_algorithms.py", "license": "MIT License", "lines": 258, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keon/algorithms:algorithms/array/delete_nth.py
""" Delete Nth Occurrence Given a list and a number N, create a new list that contains each element of the original list at most N times, without reordering. Reference: https://www.geeksforgeeks.org/remove-duplicates-from-an-array/ Complexity: delete_nth_naive: Time: O(n^2) due to list.count() Space: O(n) delete_nth: Time: O(n) Space: O(n) """ from __future__ import annotations import collections def delete_nth_naive(array: list[int], n: int) -> list[int]: """Keep at most n copies of each element using naive counting. Args: array: Source list of integers. n: Maximum number of allowed occurrences per element. Returns: New list with each element appearing at most n times. Examples: >>> delete_nth_naive([1, 2, 3, 1, 2, 1, 2, 3], 2) [1, 2, 3, 1, 2, 3] """ result = [] for num in array: if result.count(num) < n: result.append(num) return result def delete_nth(array: list[int], n: int) -> list[int]: """Keep at most n copies of each element using a hash table. Args: array: Source list of integers. n: Maximum number of allowed occurrences per element. Returns: New list with each element appearing at most n times. Examples: >>> delete_nth([1, 2, 3, 1, 2, 1, 2, 3], 2) [1, 2, 3, 1, 2, 3] """ result = [] counts = collections.defaultdict(int) for element in array: if counts[element] < n: result.append(element) counts[element] += 1 return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/delete_nth.py", "license": "MIT License", "lines": 49, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/flatten.py
""" Flatten Arrays Given an array that may contain nested arrays, produce a single flat resultant array. Reference: https://en.wikipedia.org/wiki/Flatten_(higher-order_function) Complexity: Time: O(n) where n is the total number of elements Space: O(n) """ from __future__ import annotations from collections.abc import Generator, Iterable from typing import Any def flatten(input_arr: Iterable[Any], output_arr: list[Any] | None = None) -> list[Any]: """Recursively flatten a nested iterable into a single list. Args: input_arr: A potentially nested iterable to flatten. output_arr: Accumulator list for recursive calls (internal use). Returns: A flat list containing all leaf elements. Examples: >>> flatten([2, 1, [3, [4, 5], 6], 7, [8]]) [2, 1, 3, 4, 5, 6, 7, 8] """ if output_arr is None: output_arr = [] for element in input_arr: if not isinstance(element, str) and isinstance(element, Iterable): flatten(element, output_arr) else: output_arr.append(element) return output_arr def flatten_iter(iterable: Iterable[Any]) -> Generator[Any, None, None]: """Lazily flatten a nested iterable, yielding one element at a time. Args: iterable: A potentially nested iterable to flatten. Returns: A generator producing one-dimensional output. Examples: >>> list(flatten_iter([2, 1, [3, [4, 5], 6], 7, [8]])) [2, 1, 3, 4, 5, 6, 7, 8] """ for element in iterable: if not isinstance(element, str) and isinstance(element, Iterable): yield from flatten_iter(element) else: yield element
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/flatten.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/garage.py
""" Garage Parking Rearrangement There is a parking lot with only one empty spot (represented by 0). Given the initial and final states, find the minimum number of moves to rearrange the lot. Each move swaps a car into the empty spot. Reference: https://en.wikipedia.org/wiki/15_puzzle Complexity: Time: O(n^2) worst case Space: O(n) for storing the sequence """ from __future__ import annotations def garage(initial: list[int], final: list[int]) -> tuple[int, list[list[int]]]: """Find the minimum swaps to rearrange a parking lot from initial to final state. Args: initial: Starting arrangement where 0 represents the empty spot. final: Desired arrangement where 0 represents the empty spot. Returns: A tuple of (number_of_steps, sequence_of_states) showing each intermediate arrangement. Examples: >>> garage([1, 2, 3, 0, 4], [0, 3, 2, 1, 4]) (4, [[0, 2, 3, 1, 4], [2, 0, 3, 1, 4], [2, 3, 0, 1, 4], [0, 3, 2, 1, 4]]) """ current = initial[::] sequence = [] steps = 0 while current != final: zero_pos = current.index(0) if zero_pos != final.index(0): target_car = final[zero_pos] target_pos = current.index(target_car) current[zero_pos], current[target_pos] = ( current[target_pos], current[zero_pos], ) else: for i in range(len(current)): if current[i] != final[i]: current[zero_pos], current[i] = current[i], current[zero_pos] break sequence.append(current[::]) steps += 1 return steps, sequence
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/garage.py", "license": "MIT License", "lines": 43, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/josephus.py
""" Josephus Problem People sit in a circular fashion; every k-th person is eliminated until everyone has been removed. Yield the elimination order. Reference: https://en.wikipedia.org/wiki/Josephus_problem Complexity: Time: O(n^2) due to list.pop at arbitrary index Space: O(1) auxiliary (yields in-place) """ from __future__ import annotations from collections.abc import Generator from typing import Any def josephus(items: list[Any], skip: int) -> Generator[Any, None, None]: """Yield elements eliminated in Josephus-problem order. Args: items: List of participants arranged in a circle. skip: Every *skip*-th person is eliminated each round. Returns: A generator yielding eliminated elements in order. Examples: >>> list(josephus([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)) [3, 6, 9, 4, 8, 5, 2, 7, 1] """ skip = skip - 1 index = 0 remaining = len(items) while remaining > 0: index = (skip + index) % remaining yield items.pop(index) remaining -= 1
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/josephus.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/limit.py
""" Limit Array Values Filter an array to include only elements within a specified minimum and maximum range (inclusive). Reference: https://en.wikipedia.org/wiki/Clipping_(signal_processing) Complexity: Time: O(n) Space: O(n) """ from __future__ import annotations def limit( array: list[int], min_lim: int | None = None, max_lim: int | None = None, ) -> list[int]: """Return elements of array that fall within [min_lim, max_lim]. Args: array: Source list of integers. min_lim: Minimum value (inclusive). Defaults to the array minimum. max_lim: Maximum value (inclusive). Defaults to the array maximum. Returns: A new list containing only values within the specified range. Examples: >>> limit([1, 2, 3, 4, 5], 2, 4) [2, 3, 4] """ if len(array) == 0: return array if min_lim is None: min_lim = min(array) if max_lim is None: max_lim = max(array) return list(filter(lambda x: min_lim <= x <= max_lim, array))
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/limit.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/longest_non_repeat.py
""" Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Multiple algorithm variants are provided. Reference: https://leetcode.com/problems/longest-substring-without-repeating-characters/ Complexity: Time: O(n) for all variants Space: O(min(n, m)) where m is the charset size """ from __future__ import annotations def longest_non_repeat_v1(string: str) -> int: """Find the length of the longest substring without repeating characters. Args: string: Input string to search. Returns: Length of the longest non-repeating substring. Examples: >>> longest_non_repeat_v1("abcabcbb") 3 """ if string is None: return 0 char_index = {} max_length = 0 start = 0 for index in range(len(string)): if string[index] in char_index: start = max(char_index[string[index]], start) char_index[string[index]] = index + 1 max_length = max(max_length, index - start + 1) return max_length def longest_non_repeat_v2(string: str) -> int: """Find the length of the longest substring without repeating characters. Args: string: Input string to search. Returns: Length of the longest non-repeating substring. Examples: >>> longest_non_repeat_v2("abcabcbb") 3 """ if string is None: return 0 start, max_length = 0, 0 used_char = {} for index, char in enumerate(string): if char in used_char and start <= used_char[char]: start = used_char[char] + 1 else: max_length = max(max_length, index - start + 1) used_char[char] = index return max_length def get_longest_non_repeat_v1(string: str) -> tuple[int, str]: """Find the longest substring without repeating characters. Args: string: Input string to search. Returns: A tuple of (length, substring) for the longest non-repeating substring. Examples: >>> get_longest_non_repeat_v1("abcabcbb") (3, 'abc') """ if string is None: return 0, "" substring = "" char_index = {} max_length = 0 start = 0 for index in range(len(string)): if string[index] in char_index: start = max(char_index[string[index]], start) char_index[string[index]] = index + 1 if index - start + 1 > max_length: max_length = index - start + 1 substring = string[start : index + 1] return max_length, substring def get_longest_non_repeat_v2(string: str) -> tuple[int, str]: """Find the longest substring without repeating characters. Args: string: Input string to search. Returns: A tuple of (length, substring) for the longest non-repeating substring. Examples: >>> get_longest_non_repeat_v2("abcabcbb") (3, 'abc') """ if string is None: return 0, "" substring = "" start, max_length = 0, 0 used_char = {} for index, char in enumerate(string): if char in used_char and start <= used_char[char]: start = used_char[char] + 1 else: if index - start + 1 > max_length: max_length = index - start + 1 substring = string[start : index + 1] used_char[char] = index return max_length, substring def get_longest_non_repeat_v3(string: str) -> tuple[int, str]: """Find the longest substring without repeating characters using sliding window. Args: string: Input string to search. Returns: A tuple of (length, substring) for the longest non-repeating substring. Examples: >>> get_longest_non_repeat_v3("abcabcbb") (3, 'abc') """ longest_substring = "" seen = set() start_index = 0 for i in range(len(string)): while string[i] in seen: seen.remove(string[start_index]) start_index += 1 seen.add(string[i]) longest_substring = max(longest_substring, string[start_index : i + 1], key=len) return len(longest_substring), longest_substring
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/longest_non_repeat.py", "license": "MIT License", "lines": 120, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/max_ones_index.py
""" Max Ones Index Find the index of the 0 that, when replaced with 1, produces the longest continuous sequence of 1s in a binary array. Returns -1 if no 0 exists. Reference: https://www.geeksforgeeks.org/find-index-0-replaced-1-get-longest-continuous-sequence-1s-binary-array/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def max_ones_index(array: list[int]) -> int: """Find the index of 0 to replace with 1 for the longest run of 1s. Args: array: Binary array containing only 0s and 1s. Returns: Index of the 0 to flip, or -1 if no 0 exists. Examples: >>> max_ones_index([1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1]) 3 """ length = len(array) max_count = 0 max_index = 0 prev_zero = -1 prev_prev_zero = -1 for current in range(length): if array[current] == 0: if current - prev_prev_zero > max_count: max_count = current - prev_prev_zero max_index = prev_zero prev_prev_zero = prev_zero prev_zero = current if length - prev_prev_zero > max_count: max_index = prev_zero return max_index
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/max_ones_index.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/merge_intervals.py
""" Merge Intervals Given a collection of intervals, merge all overlapping intervals into a consolidated set. Reference: https://en.wikipedia.org/wiki/Interval_(mathematics) Complexity: Time: O(n log n) due to sorting Space: O(n) """ from __future__ import annotations class Interval: """A numeric interval [start, end) with merge and comparison support. Args: start: Lower bound of the interval. end: Upper bound of the interval. """ def __init__(self, start: int = 0, end: int = 0) -> None: self.start = start self.end = end def __repr__(self) -> str: return f"Interval ({self.start}, {self.end})" def __iter__(self): return iter(range(self.start, self.end)) def __getitem__(self, index: int) -> int: if index < 0: return self.end + index return self.start + index def __len__(self) -> int: return self.end - self.start def __contains__(self, item: int) -> bool: return self.start >= item >= self.end def __eq__(self, other: object) -> bool: if not isinstance(other, Interval): return NotImplemented return self.start == other.start and self.end == other.end def as_list(self) -> list[int]: """Return interval as a list of integers. Returns: List of integers in the interval range. """ return list(self) @staticmethod def merge(intervals: list[Interval]) -> list[Interval]: """Merge overlapping intervals into a consolidated list. Args: intervals: List of Interval objects to merge. Returns: List of merged, non-overlapping Interval objects. Examples: >>> Interval.merge([Interval(1, 3), Interval(2, 6)]) [Interval (1, 6)] """ out = [] for interval in sorted(intervals, key=lambda i: i.start): if out and interval.start <= out[-1].end: out[-1].end = max(out[-1].end, interval.end) else: out += (interval,) return out @staticmethod def print_intervals(intervals: list[Interval]) -> str: """Format intervals as a string representation. Args: intervals: List of Interval objects to format. Returns: String representation of all intervals. Examples: >>> Interval.print_intervals([Interval(1, 3)]) 'Interval (1, 3)' """ result = [] for interval in intervals: result.append(repr(interval)) return "".join(result) def merge_intervals(intervals: list[list[int]]) -> list[list[int]] | None: """Merge overlapping intervals represented as nested lists. Args: intervals: List of [start, end] pairs to merge. Returns: List of merged [start, end] pairs, or None if input is None. Examples: >>> merge_intervals([[1, 3], [2, 6], [8, 10]]) [[1, 6], [8, 10]] """ if intervals is None: return None intervals.sort(key=lambda i: i[0]) out = [intervals.pop(0)] for interval in intervals: if out[-1][-1] >= interval[0]: out[-1][-1] = max(out[-1][-1], interval[-1]) else: out.append(interval) return out
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/merge_intervals.py", "license": "MIT License", "lines": 94, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/missing_ranges.py
""" Missing Ranges Find the ranges of numbers that are missing between a given low and high bound, given a sorted array of integers. Reference: https://leetcode.com/problems/missing-ranges/ Complexity: Time: O(n) Space: O(n) for the result list """ from __future__ import annotations def missing_ranges(array: list[int], low: int, high: int) -> list[tuple[int, int]]: """Find gaps between low and high not covered by elements in array. Args: array: Sorted list of integers within [low, high]. low: Lower bound of the expected range (inclusive). high: Upper bound of the expected range (inclusive). Returns: List of (start, end) tuples representing missing ranges. Examples: >>> missing_ranges([3, 5], 1, 10) [(1, 2), (4, 4), (6, 10)] """ result = [] start = low for num in array: if num == start: start += 1 elif num > start: result.append((start, num - 1)) start = num + 1 if start <= high: result.append((start, high)) return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/missing_ranges.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/move_zeros.py
""" Move Zeros Move all zeros in an array to the end while preserving the relative order of the non-zero (and non-integer-zero) elements. Reference: https://leetcode.com/problems/move-zeroes/ Complexity: Time: O(n) Space: O(n) """ from __future__ import annotations from typing import Any def move_zeros(array: list[Any]) -> list[Any]: """Move all integer zeros to the end, preserving order of other elements. Boolean False is not treated as zero. Args: array: Input list with mixed types. Returns: New list with all integer 0s moved to the end. Examples: >>> move_zeros([False, 1, 0, 1, 2, 0, 1, 3, "a"]) [False, 1, 1, 2, 1, 3, 'a', 0, 0] """ result = [] zeros = 0 for element in array: if element == 0 and type(element) is not bool: zeros += 1 else: result.append(element) result.extend([0] * zeros) return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/move_zeros.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/n_sum.py
""" N-Sum Given an array of integers, find all unique n-tuples that sum to a target value. Supports custom sum, comparison, and equality closures for advanced use cases with non-integer elements. Reference: https://leetcode.com/problems/4sum/ Complexity: Time: O(n^(k-1)) where k is the tuple size Space: O(n^(k-1)) for storing results """ from __future__ import annotations from collections.abc import Callable from typing import Any def n_sum( n: int, nums: list[Any], target: Any, **kv: Callable[..., Any], ) -> list[list[Any]]: """Find all unique n-tuples in nums that sum to target. Args: n: Size of each tuple to find. nums: List of elements to search. target: Desired sum for each n-tuple. **kv: Optional closures: sum_closure(a, b) - returns sum of two elements. compare_closure(num, target) - returns -1, 0, or 1. same_closure(a, b) - returns True if elements are equal. Returns: Sorted list of unique n-tuples (as lists) that sum to target. Examples: >>> n_sum(3, [-1, 0, 1, 2, -1, -4], 0) [[-1, -1, 2], [-1, 0, 1]] """ def _sum_closure_default(a: Any, b: Any) -> Any: return a + b def _compare_closure_default(num: Any, target: Any) -> int: if num < target: return -1 elif num > target: return 1 else: return 0 def _same_closure_default(a: Any, b: Any) -> bool: return a == b def _n_sum_inner(n: int, nums: list[Any], target: Any) -> list[list[Any]]: if n == 2: results = _two_sum(nums, target) else: results = [] prev_num = None for index, num in enumerate(nums): if prev_num is not None and same_closure(prev_num, num): continue prev_num = num n_minus1_results = _n_sum_inner( n - 1, nums[index + 1 :], target - num, ) n_minus1_results = _append_elem_to_each_list(num, n_minus1_results) results += n_minus1_results return _union(results) def _two_sum(nums: list[Any], target: Any) -> list[list[Any]]: nums.sort() left = 0 right = len(nums) - 1 results = [] while left < right: current_sum = sum_closure(nums[left], nums[right]) flag = compare_closure(current_sum, target) if flag == -1: left += 1 elif flag == 1: right -= 1 else: results.append(sorted([nums[left], nums[right]])) left += 1 right -= 1 while left < len(nums) and same_closure(nums[left - 1], nums[left]): left += 1 while right >= 0 and same_closure(nums[right], nums[right + 1]): right -= 1 return results def _append_elem_to_each_list( elem: Any, container: list[list[Any]] ) -> list[list[Any]]: results = [] for elems in container: elems.append(elem) results.append(sorted(elems)) return results def _union( duplicate_results: list[list[Any]], ) -> list[list[Any]]: results = [] if len(duplicate_results) != 0: duplicate_results.sort() results.append(duplicate_results[0]) for result in duplicate_results[1:]: if results[-1] != result: results.append(result) return results sum_closure = kv.get("sum_closure", _sum_closure_default) same_closure = kv.get("same_closure", _same_closure_default) compare_closure = kv.get("compare_closure", _compare_closure_default) nums.sort() return _n_sum_inner(n, nums, target)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/n_sum.py", "license": "MIT License", "lines": 108, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/array/plus_one.py
""" Plus One Given a non-negative number represented as an array of digits (big-endian), add one to the number and return the resulting digit array. Reference: https://leetcode.com/problems/plus-one/ Complexity: Time: O(n) Space: O(n) for v1, O(1) auxiliary for v2 and v3 """ from __future__ import annotations def plus_one_v1(digits: list[int]) -> list[int]: """Add one to a big-endian digit array using manual carry propagation. Args: digits: Non-empty list of digits representing a non-negative integer. Returns: New list of digits representing the input number plus one. Examples: >>> plus_one_v1([1, 2, 9]) [1, 3, 0] """ digits[-1] = digits[-1] + 1 result = [] carry = 0 index = len(digits) - 1 while index >= 0 or carry == 1: digit_sum = 0 if index >= 0: digit_sum += digits[index] if carry: digit_sum += 1 result.append(digit_sum % 10) carry = digit_sum // 10 index -= 1 return result[::-1] def plus_one_v2(digits: list[int]) -> list[int]: """Add one to a big-endian digit array by scanning from the right. Args: digits: Non-empty list of digits representing a non-negative integer. Returns: List of digits representing the input number plus one. Examples: >>> plus_one_v2([1, 2, 9]) [1, 3, 0] """ length = len(digits) for index in range(length - 1, -1, -1): if digits[index] < 9: digits[index] += 1 return digits digits[index] = 0 digits.insert(0, 1) return digits def plus_one_v3(num_arr: list[int]) -> list[int]: """Add one to a big-endian digit array using modular arithmetic. Args: num_arr: Non-empty list of digits representing a non-negative integer. Returns: List of digits representing the input number plus one. Examples: >>> plus_one_v3([1, 2, 9]) [1, 3, 0] """ for idx in reversed(list(enumerate(num_arr))): num_arr[idx[0]] = (num_arr[idx[0]] + 1) % 10 if num_arr[idx[0]]: return num_arr return [1] + num_arr
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/plus_one.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/remove_duplicates.py
""" Remove Duplicates Remove duplicate elements from an array while preserving the original order. Handles both hashable and unhashable items. Reference: https://en.wikipedia.org/wiki/Duplicate_code Complexity: Time: O(n) for hashable items / O(n^2) worst case for unhashable items Space: O(n) """ from __future__ import annotations from collections.abc import Hashable from typing import Any def remove_duplicates(array: list[Any]) -> list[Any]: """Remove duplicate elements from an array, preserving order. Uses a set for O(1) lookups on hashable items and falls back to linear search for unhashable items. Args: array: Input list potentially containing duplicates. Returns: New list with duplicates removed, original order preserved. Examples: >>> remove_duplicates([1, 1, 2, 2, 3]) [1, 2, 3] """ seen = set() unique_array = [] for item in array: if isinstance(item, Hashable): if item not in seen: seen.add(item) unique_array.append(item) else: if item not in unique_array: unique_array.append(item) return unique_array
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/remove_duplicates.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/rotate.py
""" Rotate Array Rotate an array of n elements to the right by k steps. Three algorithm variants are provided with different time complexities. Reference: https://leetcode.com/problems/rotate-array/ Complexity: rotate_v1: Time O(n*k), Space O(n) rotate_v2: Time O(n), Space O(n) rotate_v3: Time O(n), Space O(n) """ from __future__ import annotations def rotate_v1(array: list[int], k: int) -> list[int]: """Rotate array to the right by k steps using repeated single shifts. Args: array: List of integers to rotate. k: Number of positions to rotate right. Returns: New rotated list. Examples: >>> rotate_v1([1, 2, 3, 4, 5, 6, 7], 3) [5, 6, 7, 1, 2, 3, 4] """ array = array[:] length = len(array) for _ in range(k): temp = array[length - 1] for position in range(length - 1, 0, -1): array[position] = array[position - 1] array[0] = temp return array def rotate_v2(array: list[int], k: int) -> list[int]: """Rotate array to the right by k steps using three reversals. Args: array: List of integers to rotate. k: Number of positions to rotate right. Returns: New rotated list. Examples: >>> rotate_v2([1, 2, 3, 4, 5, 6, 7], 3) [5, 6, 7, 1, 2, 3, 4] """ array = array[:] def _reverse(arr: list[int], left: int, right: int) -> None: while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 length = len(array) k = k % length _reverse(array, 0, length - k - 1) _reverse(array, length - k, length - 1) _reverse(array, 0, length - 1) return array def rotate_v3(array: list[int] | None, k: int) -> list[int] | None: """Rotate array to the right by k steps using slicing. Args: array: List of integers to rotate, or None. k: Number of positions to rotate right. Returns: New rotated list, or None if input is None. Examples: >>> rotate_v3([1, 2, 3, 4, 5, 6, 7], 3) [5, 6, 7, 1, 2, 3, 4] """ if array is None: return None length = len(array) k = k % length return array[length - k :] + array[: length - k]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/rotate.py", "license": "MIT License", "lines": 69, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/summarize_ranges.py
""" Summarize Ranges Given a sorted integer array without duplicates, return the summary of its ranges as a list of (start, end) tuples. Reference: https://leetcode.com/problems/summary-ranges/ Complexity: Time: O(n) Space: O(n) for the result list """ from __future__ import annotations def summarize_ranges(array: list[int]) -> list[tuple[int, ...]]: """Summarize consecutive runs in a sorted array as (start, end) tuples. Args: array: Sorted list of unique integers. Returns: List of (start, end) tuples for each consecutive range. Examples: >>> summarize_ranges([0, 1, 2, 4, 5, 7]) [(0, 2), (4, 5), (7, 7)] """ result = [] if len(array) == 0: return [] if len(array) == 1: return [(array[0], array[0])] iterator = iter(array) start = end = next(iterator) for num in iterator: if num - end == 1: end = num else: result.append((start, end)) start = end = num result.append((start, end)) return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/summarize_ranges.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/three_sum.py
""" Three Sum Given an array of integers, find all unique triplets that sum to zero using the two-pointer technique. Reference: https://leetcode.com/problems/3sum/ Complexity: Time: O(n^2) Space: O(n) for the result set """ from __future__ import annotations def three_sum(array: list[int]) -> set[tuple[int, int, int]]: """Find all unique triplets in the array that sum to zero. Args: array: List of integers to search. Returns: Set of sorted tuples, each containing three integers summing to zero. Examples: >>> three_sum([-1, 0, 1, 2, -1, -4]) == {(-1, 0, 1), (-1, -1, 2)} True """ result = set() array.sort() for i in range(len(array) - 2): if i > 0 and array[i] == array[i - 1]: continue left, right = i + 1, len(array) - 1 while left < right: current_sum = array[i] + array[left] + array[right] if current_sum > 0: right -= 1 elif current_sum < 0: left += 1 else: result.add((array[i], array[left], array[right])) while left < right and array[left] == array[left + 1]: left += 1 while left < right and array[right] == array[right - 1]: right -= 1 left += 1 right -= 1 return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/three_sum.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/top_1.py
""" Top 1 (Mode) Find the most frequently occurring value(s) in an array. When multiple values share the highest frequency, all are returned. Reference: https://en.wikipedia.org/wiki/Mode_(statistics) Complexity: Time: O(n) Space: O(n) """ from __future__ import annotations from typing import Any def top_1(array: list[Any]) -> list[Any]: """Find the statistical mode(s) of an array. Args: array: Input list of comparable elements. Returns: List of element(s) with the highest frequency. Examples: >>> top_1([1, 1, 2, 2, 3]) [1, 2] """ frequency = {} for element in array: if element in frequency: frequency[element] += 1 else: frequency[element] = 1 max_count = max(frequency.values()) result = [] for element, count in frequency.items(): if count == max_count: result.append(element) return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/top_1.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/trimmean.py
""" Trimmed Mean Compute the mean of an array after discarding a given percentage of the highest and lowest values. Useful for robust averaging in scoring systems. Reference: https://en.wikipedia.org/wiki/Truncated_mean Complexity: Time: O(n log n) due to sorting Space: O(n) for the trimmed copy """ from __future__ import annotations def trimmean(array: list[float], percentage: float) -> float: """Calculate the trimmed mean of an array. Discards the top and bottom halves of the given percentage before computing the arithmetic mean. Args: array: List of numeric values. percentage: Total percentage to trim (split equally between top and bottom). E.g., 20 trims the top 10% and bottom 10%. Returns: The trimmed arithmetic mean. Examples: >>> trimmean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 20) 5.5 """ ratio = percentage / 200 array.sort() trim_count = int(len(array) * ratio) trimmed = array[trim_count : len(array) - trim_count] total = 0 for value in trimmed: total += value return total / len(trimmed)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/trimmean.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/array/two_sum.py
""" Two Sum Given an array of integers and a target sum, return the indices of the two numbers that add up to the target. Reference: https://leetcode.com/problems/two-sum/ Complexity: Time: O(n) Space: O(n) """ from __future__ import annotations def two_sum(array: list[int], target: int) -> tuple[int, int] | None: """Find two indices whose corresponding values sum to target. Args: array: List of integers to search. target: Desired sum of two elements. Returns: Tuple of (index1, index2) if a pair is found, or None otherwise. Examples: >>> two_sum([2, 7, 11, 15], 9) (0, 1) """ seen = {} for index, num in enumerate(array): if num in seen: return seen[num], index else: seen[target - num] = index return None
{ "repo_id": "keon/algorithms", "file_path": "algorithms/array/two_sum.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/add_operators.py
""" Expression Add Operators Given a string of digits and a target value, return all possibilities to insert binary operators (+, -, *) between the digits so they evaluate to the target value. Reference: https://leetcode.com/problems/expression-add-operators/ Complexity: Time: O(4^n) worst Space: O(n) recursion depth """ from __future__ import annotations def add_operators(digits: str, target: int) -> list[str]: """Return all expressions formed by inserting +, -, * that equal target. Args: digits: A string containing only digits 0-9. target: The target integer value. Returns: A list of valid expression strings. Examples: >>> add_operators("123", 6) ['1+2+3', '1*2*3'] """ result: list[str] = [] if not digits: return result _dfs(result, "", digits, target, 0, 0, 0) return result def _dfs( result: list[str], path: str, digits: str, target: int, position: int, evaluated: int, multed: int, ) -> None: """Depth-first search helper that builds expressions recursively.""" if position == len(digits): if target == evaluated: result.append(path) return for i in range(position, len(digits)): if i != position and digits[position] == "0": break current = int(digits[position : i + 1]) if position == 0: _dfs(result, path + str(current), digits, target, i + 1, current, current) else: _dfs( result, path + "+" + str(current), digits, target, i + 1, evaluated + current, current, ) _dfs( result, path + "-" + str(current), digits, target, i + 1, evaluated - current, -current, ) _dfs( result, path + "*" + str(current), digits, target, i + 1, evaluated - multed + multed * current, multed * current, )
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/add_operators.py", "license": "MIT License", "lines": 75, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/backtracking/anagram.py
""" Anagram Checker Given two strings, determine if they are anagrams of each other (i.e. one can be rearranged to form the other). Reference: https://en.wikipedia.org/wiki/Anagram Complexity: Time: O(n) where n is the length of the strings Space: O(1) fixed 26-character alphabet """ from __future__ import annotations def anagram(first: str, second: str) -> bool: """Check whether two strings are anagrams of each other. Args: first: The first string (lowercase letters only). second: The second string (lowercase letters only). Returns: True if the strings are anagrams, False otherwise. Examples: >>> anagram('apple', 'pleap') True >>> anagram('apple', 'cherry') False """ count_first = [0] * 26 count_second = [0] * 26 for char in first: index = ord(char) - ord("a") count_first[index] += 1 for char in second: index = ord(char) - ord("a") count_second[index] += 1 return count_first == count_second
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/anagram.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/array_sum_combinations.py
""" Array Sum Combinations Given three arrays and a target sum, find all three-element combinations (one element from each array) that add up to the target. Reference: https://en.wikipedia.org/wiki/Subset_sum_problem Complexity: Time: O(n^3) brute-force product of three arrays Space: O(k) where k is the number of valid combinations """ from __future__ import annotations import itertools from functools import partial def array_sum_combinations( array_a: list[int], array_b: list[int], array_c: list[int], target: int, ) -> list[list[int]]: """Find all combinations of one element per array that sum to target. Uses backtracking to enumerate valid combinations, allowing duplicates. Args: array_a: First array of integers. array_b: Second array of integers. array_c: Third array of integers. target: The desired sum. Returns: A list of three-element lists that sum to target. Examples: >>> array_sum_combinations([1], [2], [3], 6) [[1, 2, 3]] """ arrays = [array_a, array_b, array_c] def _is_complete(constructed_so_far: list[int]) -> tuple[bool, bool]: total = sum(constructed_so_far) should_stop = total >= target or len(constructed_so_far) >= 3 reached_target = total == target and len(constructed_so_far) == 3 return should_stop, reached_target def _get_candidates(constructed_so_far: list[int]) -> list[int]: return arrays[len(constructed_so_far)] def _backtrack( constructed_so_far: list[int] | None = None, result: list[list[int]] | None = None, ) -> None: if constructed_so_far is None: constructed_so_far = [] if result is None: result = [] should_stop, reached_target = _is_complete(constructed_so_far) if should_stop: if reached_target: result.append(constructed_so_far) return candidates = _get_candidates(constructed_so_far) for candidate in candidates: constructed_so_far.append(candidate) _backtrack(constructed_so_far[:], result) constructed_so_far.pop() result: list[list[int]] = [] _backtrack([], result) return result def unique_array_sum_combinations( array_a: list[int], array_b: list[int], array_c: list[int], target: int, ) -> list[tuple[int, ...]]: """Find unique combinations of one element per array that sum to target. Uses itertools.product and filters by target sum, returning only unique tuples. Args: array_a: First array of integers. array_b: Second array of integers. array_c: Third array of integers. target: The desired sum. Returns: A list of unique tuples that sum to target. Examples: >>> sorted(unique_array_sum_combinations([1, 2], [2, 3], [3, 4], 6)) [(1, 2, 3)] """ def _check_sum(expected: int, *nums: int) -> tuple[bool, tuple[int, ...]]: if sum(nums) == expected: return (True, nums) else: return (False, nums) product = itertools.product(array_a, array_b, array_c) func = partial(_check_sum, target) sums = list(itertools.starmap(func, product)) seen: set[tuple[int, ...]] = set() for is_match, values in sums: if is_match and values not in seen: seen.add(values) return list(seen)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/array_sum_combinations.py", "license": "MIT License", "lines": 93, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/backtracking/combination_sum.py
""" Combination Sum Given a set of candidate numbers (without duplicates) and a target number, find all unique combinations where the candidate numbers sum to the target. The same number may be chosen an unlimited number of times. Reference: https://leetcode.com/problems/combination-sum/ Complexity: Time: O(n^(T/M)) where T is target, M is minimum candidate Space: O(T/M) recursion depth """ from __future__ import annotations def combination_sum(candidates: list[int], target: int) -> list[list[int]]: """Find all unique combinations of candidates that sum to target. Args: candidates: A list of distinct positive integers. target: The target sum. Returns: A list of lists, each containing a valid combination. Examples: >>> combination_sum([2, 3, 6, 7], 7) [[2, 2, 3], [7]] """ result: list[list[int]] = [] candidates.sort() _dfs(candidates, target, 0, [], result) return result def _dfs( nums: list[int], target: int, index: int, path: list[int], result: list[list[int]], ) -> None: """Depth-first search helper for building combinations.""" if target < 0: return if target == 0: result.append(path) return for i in range(index, len(nums)): _dfs(nums, target - nums[i], i, path + [nums[i]], result)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/combination_sum.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/factor_combinations.py
""" Factor Combinations Given an integer n, return all possible combinations of its factors. Factors should be greater than 1 and less than n. Reference: https://leetcode.com/problems/factor-combinations/ Complexity: Time: O(n * log(n)) approximate Space: O(log(n)) recursion depth """ from __future__ import annotations def get_factors(number: int) -> list[list[int]]: """Return all factor combinations of number using iteration. Args: number: A positive integer. Returns: A list of lists, each containing a valid factorization. Examples: >>> get_factors(12) [[2, 6], [2, 2, 3], [3, 4]] """ todo: list[tuple[int, int, list[int]]] = [(number, 2, [])] combinations: list[list[int]] = [] while todo: remaining, divisor, partial = todo.pop() while divisor * divisor <= remaining: if remaining % divisor == 0: combinations.append(partial + [divisor, remaining // divisor]) todo.append((remaining // divisor, divisor, partial + [divisor])) divisor += 1 return combinations def recursive_get_factors(number: int) -> list[list[int]]: """Return all factor combinations of number using recursion. Args: number: A positive integer. Returns: A list of lists, each containing a valid factorization. Examples: >>> recursive_get_factors(12) [[2, 6], [2, 2, 3], [3, 4]] """ def _factor( remaining: int, divisor: int, partial: list[int], combinations: list[list[int]], ) -> list[list[int]]: while divisor * divisor <= remaining: if remaining % divisor == 0: combinations.append(partial + [divisor, remaining // divisor]) _factor( remaining // divisor, divisor, partial + [divisor], combinations ) divisor += 1 return combinations return _factor(number, 2, [], [])
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/factor_combinations.py", "license": "MIT License", "lines": 55, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/find_words.py
""" Word Search II Given a board of characters and a list of words, find all words that can be constructed from adjacent cells (horizontally or vertically). Each cell may only be used once per word. Uses a trie for efficient prefix matching. Reference: https://leetcode.com/problems/word-search-ii/ Complexity: Time: O(M * N * 4^L) where M*N is board size, L is max word length Space: O(W * L) for the trie, where W is number of words """ from __future__ import annotations def find_words(board: list[list[str]], words: list[str]) -> list[str]: """Find all words from the list that exist on the board. Builds a trie from the word list, then uses backtracking to search the board for each word. Args: board: A 2D grid of characters. words: A list of words to search for. Returns: A list of words found on the board. Examples: >>> board = [['o','a','a','n'], ['e','t','a','e']] >>> sorted(find_words(board, ['eat', 'oath'])) ['eat'] """ trie: dict = {} for word in words: current_node = trie for char in word: if char not in current_node: current_node[char] = {} current_node = current_node[char] current_node["#"] = "#" found: set[str] = set() used = [[False] * len(board[0]) for _ in range(len(board))] if board else [] for row in range(len(board)): for col in range(len(board[0])): _backtrack(board, row, col, trie, "", used, found) return list(found) def _backtrack( board: list[list[str]], row: int, col: int, trie: dict, prefix: str, used: list[list[bool]], found: set[str], ) -> None: """Recursively search the board for words matching the trie.""" if "#" in trie: found.add(prefix) if row < 0 or row >= len(board) or col < 0 or col >= len(board[0]): return if not used[row][col] and board[row][col] in trie: used[row][col] = True next_char = board[row][col] _backtrack( board, row + 1, col, trie[next_char], prefix + next_char, used, found ) _backtrack( board, row, col + 1, trie[next_char], prefix + next_char, used, found ) _backtrack( board, row - 1, col, trie[next_char], prefix + next_char, used, found ) _backtrack( board, row, col - 1, trie[next_char], prefix + next_char, used, found ) used[row][col] = False
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/find_words.py", "license": "MIT License", "lines": 69, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/backtracking/generate_abbreviations.py
""" Generalized Abbreviations Given a word, return all possible generalized abbreviations. Each abbreviation replaces contiguous substrings with their lengths. Reference: https://leetcode.com/problems/generalized-abbreviation/ Complexity: Time: O(2^n) where n is the length of the word Space: O(n) recursion depth """ from __future__ import annotations def generate_abbreviations(word: str) -> list[str]: """Generate all possible abbreviations of a word. Args: word: The input word to abbreviate. Returns: A list of all valid abbreviations. Examples: >>> sorted(generate_abbreviations("ab")) ['1b', '2', 'a1', 'ab'] """ result: list[str] = [] _backtrack(result, word, 0, 0, "") return result def _backtrack( result: list[str], word: str, position: int, count: int, current: str, ) -> None: """Recursively build abbreviations by including or skipping characters.""" if position == len(word): if count > 0: current += str(count) result.append(current) return if count > 0: _backtrack(result, word, position + 1, 0, current + str(count) + word[position]) else: _backtrack(result, word, position + 1, 0, current + word[position]) _backtrack(result, word, position + 1, count + 1, current)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/generate_abbreviations.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/generate_parenthesis.py
""" Generate Parentheses Given n pairs of parentheses, generate all combinations of well-formed parentheses. Reference: https://leetcode.com/problems/generate-parentheses/ Complexity: Time: O(4^n / sqrt(n)) β€” the n-th Catalan number Space: O(n) recursion depth """ from __future__ import annotations def generate_parenthesis_v1(count: int) -> list[str]: """Generate all valid parenthesis combinations (right-first variant). Builds combinations by tracking remaining left and right parentheses, trying to add a closing paren before an opening one. Args: count: The number of parenthesis pairs. Returns: A list of all valid parenthesis strings. Examples: >>> generate_parenthesis_v1(2) ['()()', '(())'] """ result: list[str] = [] _add_pair_v1(result, "", count, 0) return result def _add_pair_v1( result: list[str], current: str, left: int, right: int, ) -> None: """Recursive helper for v1: tries closing before opening.""" if left == 0 and right == 0: result.append(current) return if right > 0: _add_pair_v1(result, current + ")", left, right - 1) if left > 0: _add_pair_v1(result, current + "(", left - 1, right + 1) def generate_parenthesis_v2(count: int) -> list[str]: """Generate all valid parenthesis combinations (left-first variant). Builds combinations by tracking remaining left and right parentheses, trying to add an opening paren before a closing one. Args: count: The number of parenthesis pairs. Returns: A list of all valid parenthesis strings. Examples: >>> generate_parenthesis_v2(2) ['(())', '()()'] """ result: list[str] = [] _add_pair_v2(result, "", count, count) return result def _add_pair_v2( result: list[str], current: str, left: int, right: int, ) -> None: """Recursive helper for v2: tries opening before closing.""" if left == 0 and right == 0: result.append(current) if left > 0: _add_pair_v2(result, current + "(", left - 1, right) if right > 0 and left < right: _add_pair_v2(result, current + ")", left, right - 1)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/generate_parenthesis.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/letter_combination.py
""" Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent using a telephone keypad mapping. Reference: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Complexity: Time: O(4^n) where n is the number of digits Space: O(4^n) for the result list """ from __future__ import annotations def letter_combinations(digits: str) -> list[str]: """Return all letter combinations for a digit string. Args: digits: A string of digits (2-9). Returns: A list of all possible letter combinations. Examples: >>> letter_combinations("23") ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'] """ if digits == "": return [] keypad_map = { "2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz", } combinations: list[str] = [""] for digit in digits: expanded: list[str] = [] for existing in combinations: for char in keypad_map[digit]: expanded.append(existing + char) combinations = expanded return combinations
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/letter_combination.py", "license": "MIT License", "lines": 40, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/palindrome_partitioning.py
""" Palindrome Partitioning Given a string, find all ways to partition it into palindromic substrings. There is always at least one way since single characters are palindromes. Reference: https://leetcode.com/problems/palindrome-partitioning/ Complexity: Time: O(n * 2^n) where n is the string length Space: O(n) recursion depth """ from __future__ import annotations from collections.abc import Generator def palindromic_substrings(text: str) -> list[list[str]]: """Return all palindrome partitions of the input string. Args: text: The string to partition. Returns: A list of partitions, each being a list of palindromic substrings. Examples: >>> palindromic_substrings("abc") [['a', 'b', 'c']] """ if not text: return [[]] results: list[list[str]] = [] for i in range(len(text), 0, -1): substring = text[:i] if substring == substring[::-1]: for rest in palindromic_substrings(text[i:]): results.append([substring] + rest) return results def palindromic_substrings_iter(text: str) -> Generator[list[str], None, None]: """Yield all palindrome partitions of the input string via a generator. A slightly more Pythonic approach using a recursive generator. Args: text: The string to partition. Yields: Lists of palindromic substrings forming a valid partition. Examples: >>> list(palindromic_substrings_iter("abc")) [['a', 'b', 'c']] """ if not text: yield [] return for i in range(len(text), 0, -1): substring = text[:i] if substring == substring[::-1]: for rest in palindromic_substrings_iter(text[i:]): yield [substring] + rest
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/palindrome_partitioning.py", "license": "MIT License", "lines": 49, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/pattern_match.py
""" Pattern Matching Given a pattern and a string, determine if the string follows the same pattern. A full match means a bijection between each letter in the pattern and a non-empty substring in the string. Reference: https://leetcode.com/problems/word-pattern-ii/ Complexity: Time: O(n^m) where n is string length, m is pattern length Space: O(m) recursion depth plus mapping storage """ from __future__ import annotations def pattern_match(pattern: str, string: str) -> bool: """Check whether a string matches a given pattern via bijection. Args: pattern: A pattern string of lowercase letters. string: The string to match against (lowercase letters). Returns: True if the string follows the pattern, False otherwise. Examples: >>> pattern_match("abab", "redblueredblue") True >>> pattern_match("aabb", "xyzabcxzyabc") False """ return _backtrack(pattern, string, {}) def _backtrack( pattern: str, string: str, mapping: dict[str, str], ) -> bool: """Recursively attempt to match pattern to string using a mapping.""" if len(pattern) == 0 and len(string) > 0: return False if len(pattern) == 0 and len(string) == 0: return True for end in range(1, len(string) - len(pattern) + 2): if pattern[0] not in mapping and string[:end] not in mapping.values(): mapping[pattern[0]] = string[:end] if _backtrack(pattern[1:], string[end:], mapping): return True del mapping[pattern[0]] elif pattern[0] in mapping and mapping[pattern[0]] == string[:end]: if _backtrack(pattern[1:], string[end:], mapping): return True return False
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/pattern_match.py", "license": "MIT License", "lines": 45, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/permute.py
""" Permutations Given a collection of distinct elements, return all possible permutations. Reference: https://en.wikipedia.org/wiki/Permutation Complexity: Time: O(n * n!) where n is the number of elements Space: O(n * n!) to store all permutations """ from __future__ import annotations from collections.abc import Generator def permute(elements: list | str) -> list: """Return all permutations of the given elements. Args: elements: A list or string of distinct elements. Returns: A list of all permutations (same type as input elements). Examples: >>> permute([1, 2, 3]) [[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]] """ if len(elements) <= 1: return [elements] result = [] for perm in permute(elements[1:]): for i in range(len(elements)): result.append(perm[:i] + elements[0:1] + perm[i:]) return result def permute_iter(elements: list | str) -> Generator: """Yield all permutations of the given elements one at a time. Args: elements: A list or string of distinct elements. Yields: One permutation at a time (same type as input elements). Examples: >>> list(permute_iter([1, 2])) [[1, 2], [2, 1]] """ if len(elements) <= 1: yield elements else: for perm in permute_iter(elements[1:]): for i in range(len(elements)): yield perm[:i] + elements[0:1] + perm[i:] def permute_recursive(nums: list[int]) -> list[list[int]]: """Return all permutations using DFS backtracking. Args: nums: A list of distinct integers. Returns: A list of all permutations. Examples: >>> sorted(permute_recursive([1, 2])) [[1, 2], [2, 1]] """ result: list[list[int]] = [] _dfs(result, nums, []) return result def _dfs( result: list[list[int]], nums: list[int], path: list[int], ) -> None: """DFS helper that builds permutations by choosing remaining elements.""" if not nums: result.append(path) for i in range(len(nums)): _dfs(result, nums[:i] + nums[i + 1 :], path + [nums[i]])
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/permute.py", "license": "MIT License", "lines": 66, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/permute_unique.py
""" Unique Permutations Given a collection of numbers that might contain duplicates, return all possible unique permutations. Reference: https://leetcode.com/problems/permutations-ii/ Complexity: Time: O(n * n!) worst case Space: O(n * n!) to store all unique permutations """ from __future__ import annotations def permute_unique(nums: list[int]) -> list[list[int]]: """Return all unique permutations of a list that may have duplicates. Args: nums: A list of integers, possibly with duplicates. Returns: A list of all unique permutations. Examples: >>> sorted(permute_unique([1, 1, 2])) [[1, 1, 2], [1, 2, 1], [2, 1, 1]] """ permutations: list[list[int]] = [[]] for number in nums: new_permutations: list[list[int]] = [] for existing in permutations: for i in range(len(existing) + 1): new_permutations.append(existing[:i] + [number] + existing[i:]) if i < len(existing) and existing[i] == number: break permutations = new_permutations return permutations
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/permute_unique.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/subsets.py
""" Subsets Given a set of distinct integers, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Reference: https://en.wikipedia.org/wiki/Power_set Complexity: Time: O(2^n) where n is the number of elements Space: O(2^n) to store all subsets """ from __future__ import annotations def subsets(nums: list[int]) -> list[list[int]]: """Return all subsets of the given list using backtracking. Args: nums: A list of distinct integers. Returns: A list of all subsets. Examples: >>> sorted(subsets([1, 2]), key=str) [[], [1], [1, 2], [2]] """ result: list[list[int]] = [] _backtrack(result, nums, [], 0) return result def _backtrack( result: list[list[int]], nums: list[int], stack: list[int], position: int, ) -> None: """Recursive helper that includes or excludes each element.""" if position == len(nums): result.append(list(stack)) else: stack.append(nums[position]) _backtrack(result, nums, stack, position + 1) stack.pop() _backtrack(result, nums, stack, position + 1) def subsets_v2(nums: list[int]) -> list[list[int]]: """Return all subsets of the given list using iteration. Builds subsets iteratively by extending each existing subset with the next element. Args: nums: A list of distinct integers. Returns: A list of all subsets. Examples: >>> sorted(subsets_v2([1, 2]), key=str) [[], [1], [1, 2], [2]] """ result: list[list[int]] = [[]] for number in sorted(nums): result += [item + [number] for item in result] return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/subsets.py", "license": "MIT License", "lines": 53, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/backtracking/subsets_unique.py
""" Unique Subsets Given a collection of integers that might contain duplicates, return all possible unique subsets (the power set without duplicates). Reference: https://leetcode.com/problems/subsets-ii/ Complexity: Time: O(2^n) where n is the number of elements Space: O(2^n) to store all subsets """ from __future__ import annotations def subsets_unique(nums: list[int]) -> list[tuple[int, ...]]: """Return all unique subsets of a list that may have duplicates. Args: nums: A list of integers, possibly with duplicates. Returns: A list of unique tuples representing each subset. Examples: >>> sorted(subsets_unique([1, 2, 2])) [(), (1,), (1, 2), (1, 2, 2), (2,), (2, 2)] """ found: set[tuple[int, ...]] = set() _backtrack(found, nums, [], 0) return list(found) def _backtrack( found: set[tuple[int, ...]], nums: list[int], stack: list[int], position: int, ) -> None: """Recursive helper that includes or excludes each element.""" if position == len(nums): found.add(tuple(stack)) else: stack.append(nums[position]) _backtrack(found, nums, stack, position + 1) stack.pop() _backtrack(found, nums, stack, position + 1)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/backtracking/subsets_unique.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/add_bitwise_operator.py
""" Add Bitwise Operator Add two positive integers without using the '+' operator, using only bitwise operations (AND, XOR, shift). Reference: https://en.wikipedia.org/wiki/Adder_(electronics) Complexity: Time: O(log n) where n is the larger of the two inputs Space: O(1) """ from __future__ import annotations def add_bitwise_operator(first: int, second: int) -> int: """Add two non-negative integers using only bitwise operations. Args: first: First non-negative integer operand. second: Second non-negative integer operand. Returns: The sum of first and second. Examples: >>> add_bitwise_operator(2, 3) 5 >>> add_bitwise_operator(0, 0) 0 """ while second: carry = first & second first = first ^ second second = carry << 1 return first
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/add_bitwise_operator.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/binary_gap.py
""" Binary Gap Given a positive integer N, find and return the longest distance between two consecutive 1-bits in the binary representation of N. If there are not two consecutive 1-bits, return 0. Reference: https://en.wikipedia.org/wiki/Hamming_distance Complexity: Time: O(log n) where n is the input integer Space: O(1) """ from __future__ import annotations def binary_gap(number: int) -> int: """Find the longest distance between consecutive 1-bits in binary. Args: number: A positive integer to examine. Returns: The longest gap between consecutive 1-bits, or 0 if fewer than two 1-bits exist. Examples: >>> binary_gap(22) 2 >>> binary_gap(8) 0 """ last_one_position = None longest_gap = 0 index = 0 while number != 0: if number & 1: if last_one_position is not None: longest_gap = max(longest_gap, index - last_one_position) last_one_position = index index += 1 number >>= 1 return longest_gap
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/binary_gap.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/bit_operation.py
""" Fundamental Bit Operations Basic bit manipulation operations: get, set, clear, and update individual bits at a specific position in an integer. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: Time: O(1) for all operations Space: O(1) """ from __future__ import annotations def get_bit(number: int, position: int) -> int: """Get the bit value at a specific position. Shifts 1 over by *position* bits and ANDs with *number* to isolate the target bit. Args: number: The integer to inspect. position: Zero-based bit index (0 is the least significant bit). Returns: 1 if the bit at *position* is set, 0 otherwise. Examples: >>> get_bit(22, 2) 1 >>> get_bit(22, 3) 0 """ return (number & (1 << position)) != 0 def set_bit(number: int, position: int) -> int: """Set the bit at a specific position to 1. Shifts 1 over by *position* bits and ORs with *number* so that only the bit at *position* is turned on. Args: number: The integer to modify. position: Zero-based bit index to set. Returns: The integer with the bit at *position* set to 1. Examples: >>> set_bit(22, 3) 30 """ return number | (1 << position) def clear_bit(number: int, position: int) -> int: """Clear the bit at a specific position to 0. Creates a mask with all bits set except at *position*, then ANDs with *number*. Args: number: The integer to modify. position: Zero-based bit index to clear. Returns: The integer with the bit at *position* cleared to 0. Examples: >>> clear_bit(22, 2) 18 """ mask = ~(1 << position) return number & mask def update_bit(number: int, position: int, bit: int) -> int: """Update the bit at a specific position to a given value. First clears the bit at *position*, then ORs in the new *bit* value shifted to that position. Args: number: The integer to modify. position: Zero-based bit index to update. bit: The new bit value (0 or 1). Returns: The integer with the bit at *position* set to *bit*. Examples: >>> update_bit(22, 3, 1) 30 >>> update_bit(22, 2, 0) 18 """ mask = ~(1 << position) return (number & mask) | (bit << position)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/bit_operation.py", "license": "MIT License", "lines": 73, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/bytes_int_conversion.py
""" Bytes-Integer Conversion Convert between Python integers and raw byte sequences in both big-endian and little-endian byte orders. Reference: https://en.wikipedia.org/wiki/Endianness Complexity: Time: O(b) where b is the number of bytes in the representation Space: O(b) """ from __future__ import annotations from collections import deque def int_to_bytes_big_endian(number: int) -> bytes: """Convert a non-negative integer to bytes in big-endian order. Args: number: A non-negative integer to convert. Returns: A bytes object with the most significant byte first. Examples: >>> int_to_bytes_big_endian(17) b'\\x11' """ byte_buffer: deque[int] = deque() while number > 0: byte_buffer.appendleft(number & 0xFF) number >>= 8 return bytes(byte_buffer) def int_to_bytes_little_endian(number: int) -> bytes: """Convert a non-negative integer to bytes in little-endian order. Args: number: A non-negative integer to convert. Returns: A bytes object with the least significant byte first. Examples: >>> int_to_bytes_little_endian(17) b'\\x11' """ byte_buffer: list[int] = [] while number > 0: byte_buffer.append(number & 0xFF) number >>= 8 return bytes(byte_buffer) def bytes_big_endian_to_int(byte_string: bytes) -> int: """Convert a big-endian byte sequence to an integer. Args: byte_string: Bytes with the most significant byte first. Returns: The decoded integer value. Examples: >>> bytes_big_endian_to_int(b'\\x11') 17 """ number = 0 for byte in byte_string: number <<= 8 number += byte return number def bytes_little_endian_to_int(byte_string: bytes) -> int: """Convert a little-endian byte sequence to an integer. Args: byte_string: Bytes with the least significant byte first. Returns: The decoded integer value. Examples: >>> bytes_little_endian_to_int(b'\\x11') 17 """ number = 0 exponent = 0 for byte in byte_string: number += byte << exponent exponent += 8 return number
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/bytes_int_conversion.py", "license": "MIT License", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/count_flips_to_convert.py
""" Count Flips to Convert Determine the minimal number of bits you would need to flip to convert integer A to integer B. Uses XOR to find differing bits and Brian Kernighan's algorithm to count them. Reference: https://en.wikipedia.org/wiki/Hamming_distance Complexity: Time: O(k) where k is the number of differing bits Space: O(1) """ from __future__ import annotations def count_flips_to_convert(first: int, second: int) -> int: """Count the number of bit flips needed to convert one integer to another. Args: first: The source integer. second: The target integer. Returns: The number of bits that differ between *first* and *second*. Examples: >>> count_flips_to_convert(29, 15) 2 >>> count_flips_to_convert(34, 34) 0 """ diff = first ^ second count = 0 while diff: diff &= diff - 1 count += 1 return count
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/count_flips_to_convert.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/count_ones.py
""" Count Ones (Hamming Weight) Count the number of 1-bits in the binary representation of an unsigned integer using Brian Kernighan's algorithm. Reference: https://en.wikipedia.org/wiki/Hamming_weight Complexity: Time: O(k) where k is the number of set bits Space: O(1) iterative / O(k) recursive (call stack) """ from __future__ import annotations def count_ones_recur(number: int) -> int: """Count set bits using Brian Kernighan's algorithm (recursive). Args: number: A non-negative integer. Returns: The number of 1-bits in the binary representation. Examples: >>> count_ones_recur(8) 1 >>> count_ones_recur(63) 6 """ if not number: return 0 return 1 + count_ones_recur(number & (number - 1)) def count_ones_iter(number: int) -> int: """Count set bits using Brian Kernighan's algorithm (iterative). Args: number: A non-negative integer. Returns: The number of 1-bits in the binary representation. Examples: >>> count_ones_iter(8) 1 >>> count_ones_iter(63) 6 """ count = 0 while number: number &= number - 1 count += 1 return count
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/count_ones.py", "license": "MIT License", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/find_difference.py
""" Find the Difference Given two strings where the second is generated by shuffling the first and adding one extra letter, find the added letter using XOR. Reference: https://en.wikipedia.org/wiki/Exclusive_or Complexity: Time: O(n) where n is the length of the longer string Space: O(1) """ from __future__ import annotations def find_difference(original: str, shuffled: str) -> str: """Find the single character added to a shuffled copy of a string. Uses XOR on all character code points; paired characters cancel out, leaving only the extra character. Args: original: The original string. shuffled: The shuffled string with one extra character. Returns: The single character that was added. Examples: >>> find_difference("abcd", "abecd") 'e' """ xor_result = 0 for character in original + shuffled: xor_result ^= ord(character) return chr(xor_result)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/find_difference.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/find_missing_number.py
""" Find Missing Number Given a sequence of unique integers in the range [0..n] with one value missing, find and return that missing number. Two approaches are provided: XOR-based and summation-based. Reference: https://en.wikipedia.org/wiki/Exclusive_or Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def find_missing_number(nums: list[int]) -> int: """Find the missing number using XOR. XORs every element with its expected index so that all paired values cancel out, leaving only the missing number. Args: nums: A list of unique integers from 0..n with one missing. Returns: The missing integer. Examples: >>> find_missing_number([4, 1, 3, 0, 6, 5, 2]) 7 >>> find_missing_number([0]) 1 """ missing = 0 for index, number in enumerate(nums): missing ^= number missing ^= index + 1 return missing def find_missing_number2(nums: list[int]) -> int: """Find the missing number using arithmetic summation. Computes the expected sum of 0..n and subtracts the actual sum of the list to isolate the missing value. Args: nums: A list of unique integers from 0..n with one missing. Returns: The missing integer. Examples: >>> find_missing_number2([4, 1, 3, 0, 6, 5, 2]) 7 >>> find_missing_number2([0]) 1 """ total = sum(nums) length = len(nums) expected_total = length * (length + 1) // 2 return expected_total - total
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/find_missing_number.py", "license": "MIT License", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/flip_bit_longest_sequence.py
""" Flip Bit Longest Sequence Given an integer, find the length of the longest sequence of 1-bits you can create by flipping exactly one 0-bit to a 1-bit. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: Time: O(b) where b is the number of bits in the integer Space: O(1) """ from __future__ import annotations def flip_bit_longest_seq(number: int) -> int: """Find the longest 1-bit run achievable by flipping a single 0-bit. Tracks the current run length and the previous run length to determine the best sequence that can be formed by bridging two runs with a single flipped bit. Args: number: A non-negative integer. Returns: The length of the longest sequence of 1-bits after one flip. Examples: >>> flip_bit_longest_seq(1775) 8 >>> flip_bit_longest_seq(0) 1 """ current_length = 0 previous_length = 0 max_length = 0 while number: if number & 1 == 1: current_length += 1 elif number & 1 == 0: previous_length = 0 if number & 2 == 0 else current_length current_length = 0 max_length = max(max_length, previous_length + current_length) number >>= 1 return max_length + 1
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/flip_bit_longest_sequence.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/has_alternative_bit.py
""" Has Alternating Bits Check whether a positive integer has alternating bits, meaning no two adjacent bits share the same value. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: has_alternative_bit: O(number of bits) has_alternative_bit_fast: O(1) """ from __future__ import annotations def has_alternative_bit(number: int) -> bool: """Check for alternating bits by scanning each pair of adjacent bits. Args: number: A positive integer to check. Returns: True if every pair of adjacent bits differs, False otherwise. Examples: >>> has_alternative_bit(5) True >>> has_alternative_bit(7) False """ first_bit = 0 second_bit = 0 while number: first_bit = number & 1 if number >> 1: second_bit = (number >> 1) & 1 if not first_bit ^ second_bit: return False else: return True number >>= 1 return True def has_alternative_bit_fast(number: int) -> bool: """Check for alternating bits using O(1) bitmask arithmetic. Args: number: A positive integer to check. Returns: True if every pair of adjacent bits differs, False otherwise. Examples: >>> has_alternative_bit_fast(5) True >>> has_alternative_bit_fast(7) False """ mask_even_bits = int("aaaaaaaa", 16) # ...10101010 mask_odd_bits = int("55555555", 16) # ...01010101 return mask_even_bits == (number + (number ^ mask_even_bits)) or mask_odd_bits == ( number + (number ^ mask_odd_bits) )
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/has_alternative_bit.py", "license": "MIT License", "lines": 51, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/insert_bit.py
""" Insert Bit Insert one or more bits into an integer at a specific bit position. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def insert_one_bit(number: int, bit: int, position: int) -> int: """Insert a single bit at a specific position in an integer. Splits the number at *position*, shifts the upper part left by one to make room, inserts *bit*, and merges with the lower part. Args: number: The integer to modify. bit: The bit value to insert (0 or 1). position: Zero-based index at which to insert the bit. Returns: The resulting integer after insertion. Examples: >>> insert_one_bit(21, 1, 2) 45 >>> insert_one_bit(21, 0, 2) 41 """ upper = number >> position upper = (upper << 1) | bit upper = upper << position lower = ((1 << position) - 1) & number return lower | upper def insert_mult_bits(number: int, bits: int, length: int, position: int) -> int: """Insert multiple bits at a specific position in an integer. Splits the number at *position*, shifts the upper part left by *length* positions, inserts the *bits* value, and merges with the lower part. Args: number: The integer to modify. bits: The bit pattern to insert. length: The number of bits in the pattern. position: Zero-based index at which to insert. Returns: The resulting integer after insertion. Examples: >>> insert_mult_bits(5, 7, 3, 1) 47 >>> insert_mult_bits(5, 7, 3, 3) 61 """ upper = number >> position upper = (upper << length) | bits upper = upper << position lower = ((1 << position) - 1) & number return lower | upper
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/insert_bit.py", "license": "MIT License", "lines": 53, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/power_of_two.py
""" Power of Two Determine whether a given integer is a power of two using bit manipulation. A power of two has exactly one set bit, so ``n & (n - 1)`` clears that bit and yields zero. Reference: https://en.wikipedia.org/wiki/Power_of_two Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def is_power_of_two(number: int) -> bool: """Check whether an integer is a power of two. Args: number: The integer to test. Returns: True if *number* is a positive power of two, False otherwise. Examples: >>> is_power_of_two(64) True >>> is_power_of_two(91) False >>> is_power_of_two(0) False """ return number > 0 and not number & (number - 1)
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/power_of_two.py", "license": "MIT License", "lines": 26, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/remove_bit.py
""" Remove Bit Remove a single bit at a specific position from an integer, shifting higher bits down to fill the gap. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def remove_bit(number: int, position: int) -> int: """Remove the bit at a specific position from an integer. Splits the number around *position*, shifts the upper part right by one to collapse the gap, and merges with the lower part. Args: number: The integer to modify. position: Zero-based index of the bit to remove. Returns: The resulting integer after removal. Examples: >>> remove_bit(21, 2) 9 >>> remove_bit(21, 4) 5 >>> remove_bit(21, 0) 10 """ upper = number >> (position + 1) upper = upper << position lower = ((1 << position) - 1) & number return upper | lower
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/remove_bit.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/reverse_bits.py
""" Reverse Bits Reverse the bits of a 32-bit unsigned integer. Reference: https://en.wikipedia.org/wiki/Bit_reversal Complexity: Time: O(1) -- always iterates exactly 32 times Space: O(1) """ from __future__ import annotations def reverse_bits(number: int) -> int: """Reverse all 32 bits of an unsigned integer. Args: number: A 32-bit unsigned integer (0 to 2**32 - 1). Returns: The integer formed by reversing the bit order. Examples: >>> reverse_bits(43261596) 964176192 >>> reverse_bits(0) 0 """ result = 0 for _ in range(32): result = (result << 1) + (number & 1) number >>= 1 return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/reverse_bits.py", "license": "MIT License", "lines": 26, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/single_number.py
""" Single Number Given an array of integers where every element appears twice except for one, find the unique element using XOR. Reference: https://en.wikipedia.org/wiki/Exclusive_or Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def single_number(nums: list[int]) -> int: """Find the element that appears only once (all others appear twice). XORs all values together; paired values cancel to zero, leaving the unique value. Args: nums: A list of integers where every element except one appears an even number of times. Returns: The single element, or 0 if all elements are paired. Examples: >>> single_number([1, 0, 2, 1, 2, 3, 3]) 0 >>> single_number([101]) 101 """ result = 0 for number in nums: result ^= number return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/single_number.py", "license": "MIT License", "lines": 29, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/single_number2.py
""" Single Number 2 Given an array of integers where every element appears three times except for one (which appears exactly once), find that unique element using constant space and linear time. Reference: https://en.wikipedia.org/wiki/Exclusive_or Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def single_number2(nums: list[int]) -> int: """Find the element that appears once (all others appear three times). Uses two accumulators (*ones* and *twos*) to track bits that have appeared once and twice respectively; bits appearing a third time are cleared from both accumulators. Args: nums: A list of integers where every element except one appears exactly three times. Returns: The single element that appears only once. Examples: >>> single_number2([4, 2, 3, 2, 1, 1, 4, 2, 4, 1]) 3 """ ones, twos = 0, 0 for index in range(len(nums)): ones = (ones ^ nums[index]) & ~twos twos = (twos ^ nums[index]) & ~ones return ones
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/single_number2.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/single_number3.py
""" Single Number 3 Given an array where exactly two elements appear once and all others appear exactly twice, find those two unique elements in O(n) time and O(1) space. Reference: https://en.wikipedia.org/wiki/Exclusive_or Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def single_number3(nums: list[int]) -> list[int]: """Find the two elements that each appear exactly once. Uses XOR to isolate the combined signature of the two unique values, then partitions all numbers by a distinguishing bit to separate them. Args: nums: A list of integers where exactly two elements appear once and all others appear exactly twice. Returns: A list containing the two unique elements. Examples: >>> sorted(single_number3([1, 2, 1, 3, 2, 5])) [3, 5] """ xor_both = 0 for number in nums: xor_both ^= number rightmost_set_bit = xor_both & (-xor_both) first, second = 0, 0 for number in nums: if number & rightmost_set_bit: first ^= number else: second ^= number return [first, second]
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/single_number3.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/bit_manipulation/subsets.py
""" Subsets via Bit Manipulation Generate all possible subsets of a set of distinct integers using bitmask enumeration. Each integer from 0 to 2^n - 1 represents a unique subset. Reference: https://en.wikipedia.org/wiki/Power_set Complexity: Time: O(n * 2^n) Space: O(n * 2^n) """ from __future__ import annotations def subsets(nums: list[int]) -> set[tuple[int, ...]]: """Return all subsets of the given list as a set of tuples. Uses bitmask enumeration: for each number from 0 to 2^n - 1, the set bits indicate which elements are included in that subset. Args: nums: A list of distinct integers. Returns: A set of tuples, each representing one subset. Examples: >>> sorted(subsets([1, 2, 3])) [(), (1,), (1, 2), (1, 2, 3), (1, 3), (2,), (2, 3), (3,)] """ length = len(nums) total = 1 << length result: set[tuple[int, ...]] = set() for mask in range(total): subset = tuple( number for bit_index, number in enumerate(nums) if mask & 1 << bit_index ) result.add(subset) return result
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/subsets.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation