### Install FlashAttention 3 for Hopper GPUs Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Installs FlashAttention version 3, specifically for Hopper GPUs. This involves cloning the repository, navigating to the 'hopper' directory, and running the setup script. ```bash git clone git@github.com:Dao-AILab/flash-attention.git cd flash-attention/hopper python setup.py install ``` -------------------------------- ### Install Python Dependencies Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Installs all necessary Python packages listed in the 'requirements.txt' file. This command ensures that all project dependencies are met. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install CUDA 12.6 Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Installs CUDA 12.6 using a provided URL and sets the CUDA_HOME environment variable. This is a prerequisite for installing PyTorch with CUDA support and FlashAttention for specific GPU architectures. ```bash # Install CUDA 12.6 CUDA_URL=https://developer.download.nvidia.com/compute/cuda/12.6.3/local_installers/cuda_12.6.3_560.35.05_linux.run wget -q --show-progress --progress=bar:force:noscroll -O cuda_installer.run $CUDA_URL sudo sh cuda_installer.run --silent --toolkit --override export CUDA_HOME=/usr/local/cuda-12.6 ``` -------------------------------- ### Install PyTorch with CUDA 12.6 Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Installs PyTorch, Torchvision, and Torchaudio with support for CUDA 12.6. This command specifies the index URL for PyTorch wheels compatible with the installed CUDA version. ```bash # Install PyTorch with CUDA 12.6 PYTORCH_INDEX_URL=https://download.pytorch.org/whl/cu126 pip3 install torch torchvision torchaudio --index-url $PYTORCH_INDEX_URL ``` -------------------------------- ### Hydra Pretraining Configuration (YAML) Source: https://context7.com/dandarm/hrm-ts/llms.txt Example Hydra configuration file (cfg_pretrain.yaml) for pretraining. Defines defaults, data paths, training parameters, and learning rate schedules. ```yaml # config/cfg_pretrain.yaml defaults: - arch: hrm_v1 - _self_ data_path: data/arc-aug-1000 global_batch_size: 768 epochs: 100000 eval_interval: 10000 lr: 1e-4 lr_min_ratio: 1.0 lr_warmup_steps: 2000 beta1: 0.9 beta2: 0.95 weight_decay: 0.1 puzzle_emb_weight_decay: 0.1 puzzle_emb_lr: 1e-2 ``` -------------------------------- ### Install FlashAttention 2 for Ampere or earlier GPUs Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Installs FlashAttention version 2 using pip. This is the recommended version for GPUs older than Hopper architecture, such as Ampere. ```bash pip install flash-attn ``` -------------------------------- ### Define Dataset and Checkpoint Paths Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Specifies the file paths for the dataset and model checkpoints. These constants are crucial for loading training data and pre-trained models. The dataset path can be switched between ARC-1 and ARC-2 configurations. ```python DATASET_PATH = "data/arc-aug-1000" # ARC-1 # DATASET_PATH = "data/arc-2-aug-1000" # ARC-2 CHECKPOINT_PATH = "checkpoints/Arc-aug-1000 ACT-torch/HierarchicalReasoningModel_ACTV1 amphibian-turaco/step_414456" ``` -------------------------------- ### Visualize Input and Ground Truth Data Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb This snippet visualizes input and ground truth heatmaps using matplotlib. It iterates through trials, displaying each with a corresponding title and disabling axes. Dependencies include matplotlib and ARC_COLOR_MAP. ```python if visualize: # Show input and ground truth axes[fig_id, 0].imshow(global_hmap[input_hash], cmap=ARC_COLOR_MAP) axes[fig_id, 0].set_title(f"{name}\nInput") axes[fig_id, 0].axis('off') axes[fig_id, 1].imshow(global_hmap[label_hash], cmap=ARC_COLOR_MAP) axes[fig_id, 1].set_title(f"{name}\nAnswer") axes[fig_id, 1].axis('off') trial_id = 2 for h, stats in p_map[:2]: ans = global_hmap[h] axes[fig_id, trial_id].imshow(ans, cmap=ARC_COLOR_MAP) axes[fig_id, trial_id].set_title(f"{name}\nTrial {trial_id}") axes[fig_id, trial_id].axis('off') trial_id += 1 fig_id += 1 ``` -------------------------------- ### Import Libraries for Data Handling and Model Training Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Imports essential Python libraries for file operations, data serialization, pattern matching, hashing, plotting, deep learning (PyTorch), numerical computation (NumPy), and just-in-time compilation (Numba). These are foundational for data processing and model development. ```python import os import json from glob import glob import hashlib import matplotlib.pyplot as plt import matplotlib.colors as mcolors import torch import torch.nn.functional as F import numpy as np from numba import njit from dataset.common import inverse_dihedral_transform ``` -------------------------------- ### Lorenz Attractor Time Series Forecasting (PyTorch) Source: https://context7.com/dandarm/hrm-ts/llms.txt Complete example for chaotic time series forecasting using the Lorenz system and HRM-TS. Includes data generation, model building, training, and autoregressive inference. ```python import numpy as np import torch from torch.utils.data import DataLoader from dataset import TimeSeriesWindows from models.ts_hierarchical_core import TimeSeriesHRMCore from models.ts_hrm_adapter import TimeSeriesHRM, ts_train_step def lorenz_series(T=1000, dt=0.01, sigma=10.0, rho=28.0, beta=8/3): """Generate Lorenz attractor time series.""" xyz = np.zeros((T, 3), dtype=np.float32) xyz[0] = [1.0, 1.0, 1.0] for t in range(1, T): x, y, z = xyz[t-1] xyz[t] = xyz[t-1] + dt * np.array([ sigma * (y - x), x * (rho - z) - y, x * y - beta * z ], dtype=np.float32) return xyz # Generate data device = torch.device("cuda" if torch.cuda.is_available() else "cpu") series = lorenz_series(T=1000) dataset = TimeSeriesWindows(series, series, T_in=20, T_out=1) loader = DataLoader(dataset, batch_size=32, shuffle=True) # Build model core = TimeSeriesHRMCore(d_model=64, num_heads=4, H_layers=2, L_layers=2) model = TimeSeriesHRM(core, d_in=3, d_model=64, d_out=3).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) # Train for epoch in range(50): for batch in loader: batch = {k: v.to(device) for k, v in batch.items()} optimizer.zero_grad() loss = ts_train_step(model, batch) loss.backward() optimizer.step() if (epoch + 1) % 10 == 0: print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}") # Autoregressive inference model.eval() mu = torch.tensor(dataset.stats["mu"], device=device) sd = torch.tensor(dataset.stats["sd"], device=device) x_win = ((torch.tensor(series[:20], device=device) - mu) / sd).unsqueeze(0) predictions = [] with torch.no_grad(): for _ in range(980): # Predict remaining timesteps y_hat, _ = model(x_win, T_out=1) predictions.append(y_hat[:, -1, :].cpu().numpy()) y_norm = (y_hat[:, -1:, :] - mu) / sd x_win = torch.cat([x_win[:, 1:, :], y_norm], dim=1) predictions = np.concatenate(predictions, axis=0) print(f"Predictions shape: {predictions.shape}") # [980, 3] ``` -------------------------------- ### Execute Test Function Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb This snippet demonstrates the execution of a test function, likely for evaluating the model's performance. It calls the 'test' function with the 'visualize' parameter set to False, indicating that no visualization should be generated during this test run. ```python test(visualize=False) ``` -------------------------------- ### Grid Hashing Utility Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Generates a hash for a given numpy grid. It converts the grid to bytes and includes its shape in the hash to ensure uniqueness for grids with the same content but different dimensions. Input is a numpy array. ```python def grid_hash(grid: np.ndarray): return hash((grid.tobytes(), grid.shape)) ``` -------------------------------- ### Load Identifiers and Predictions Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Loads puzzle identifiers and predictions from specified dataset and checkpoint paths. It handles JSON and PyTorch file formats, filters out padding, and concatenates predictions. Dependencies include os, json, glob, torch, and numpy. ```python def load_identifiers_and_preds(dataset_path: str, checkpoint_path: str): # Load puzzle identifiers with open(os.path.join(dataset_path, "identifiers.json"), "r") as f: identifier_map = json.load(f) # Load preds all_preds = {} for filename in glob(f"{checkpoint_path}_all_preds.*"): preds = torch.load(filename) for k, v in preds.items(): all_preds.setdefault(k, []) all_preds[k].append(v) del preds all_preds = {k: torch.cat(v, dim=0) for k, v in all_preds.items()} # Remove paddings mask = all_preds["puzzle_identifiers"] != PAD_PUZZLE_IDENTIFIER all_preds = {k: v[mask] for k, v in all_preds.items()} return identifier_map, all_preds ``` -------------------------------- ### Calculate and Print K-Shot Correctness Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb This snippet calculates the total correctness for different k-shot values. It iterates through predefined K values, sums up correct predictions, and prints the percentage of correctness for each k. Dependencies include Ks, num_test_correct, tests, and puzzle_labels. ```python # Total correctness for i in range(len(Ks)): correct[i] += num_test_correct[i] == len(tests) for i, k in enumerate(Ks): print (f"{k}-shot: {correct[i] / len(puzzle_labels) * 100:.2f}%") ``` -------------------------------- ### Inverse Augmentation for Grid Transformations Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Applies the inverse of a specified augmentation transformation to a grid. It parses the transformation ID and permutation from the name, calculates the inverse permutation, and applies the inverse dihedral transform. Input is a string representing the augmentation and a numpy array for the grid. ```python def inverse_aug(name: str, grid: np.ndarray): if "_" not in name: return grid trans_id, perm = name.split("_")[-2:] trans_id = int(trans_id[1:]) # Remove "t" letter inv_perm = np.argsort(list(perm)) return inv_perm[inverse_dihedral_transform(grid, trans_id)] ``` -------------------------------- ### Define Padding Identifier Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Sets a constant value to represent a padded identifier. This is likely used in data preprocessing or model input to denote padding for sequences or grids. ```python PAD_PUZZLE_IDENTIFIER = 0 ``` -------------------------------- ### Test Function for Model Evaluation Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Evaluates the model's performance by loading data, processing predictions, and comparing them against ground truth labels. It handles augmented and non-augmented puzzles, calculates accuracy based on different K values, and optionally visualizes results. Dependencies include matplotlib.pyplot. ```python def test(visualize, Ks=[1, 2, 10, 100, 1000]): identifier_map, all_preds = load_identifiers_and_preds(DATASET_PATH, CHECKPOINT_PATH) global_hmap = {} # Get puzzles and corresponding answers puzzle_labels = {} for identifier, input, label in zip(all_preds["puzzle_identifiers"], all_preds["inputs"], all_preds["labels"]): name = identifier_map[identifier] if "_" not in name: # Not-augmented puzzle_labels.setdefault(name, {}) input = crop(input.numpy()) label = crop(label.numpy()) input_hash = grid_hash(input) label_hash = grid_hash(label) global_hmap[input_hash] = input global_hmap[label_hash] = label assert input_hash not in puzzle_labels[name] puzzle_labels[name][input_hash] = label_hash print ("Number of puzzles", len(puzzle_labels)) # Argmax prediction preds = all_preds["logits"].argmax(-1) # Collate pred_answers = {} for identifier, input, pred, q in zip(all_preds["puzzle_identifiers"], all_preds["inputs"], preds, all_preds["q_halt_logits"].sigmoid()): name = identifier_map[identifier] orig_name = name.split("_")[0] input = input.numpy() input_hash = grid_hash(inverse_aug(name, crop(input))) assert input_hash in puzzle_labels[orig_name] pred = inverse_aug(name, crop(pred.numpy())) pred_hash = grid_hash(pred) global_hmap[pred_hash] = pred pred_answers.setdefault(orig_name, {}) pred_answers[orig_name].setdefault(input_hash, []) pred_answers[orig_name][input_hash].append((pred_hash, q.item())) # test-1 if visualize: num_figs = sum(len(tests) for name, tests in puzzle_labels.items()) fig, axes = plt.subplots(num_figs, 4, figsize=(8, num_figs * 4)) fig_id = 0 correct = [0 for _ in range(len(Ks))] for name, tests in puzzle_labels.items(): num_test_correct = [0 for _ in range(len(Ks))] for input_hash, label_hash in tests.items(): p = pred_answers[name][input_hash] p_map = {} for h, q in p: p_map.setdefault(h, [0, 0]) p_map[h][0] += 1 p_map[h][1] += q for h, stats in p_map.items(): stats[1] /= stats[0] p_map = sorted(p_map.items(), key=lambda kv: kv[1], reverse=True) # 2-vote for i, k in enumerate(Ks): ok = False for h, stats in p_map[:k]: ok |= h == label_hash num_test_correct[i] += ok ``` -------------------------------- ### ARC Color Map Definition Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Defines a ListedColormap for ARC (Abstraction and Reasoning Corpus) visualization. It assigns specific hexadecimal color codes to symbols 0 through 9, used for representing different elements in the visualizations. ```python ARC_COLOR_MAP = mcolors.ListedColormap([ "#000000", # symbol_0: black "#0074D9", # symbol_1: blue "#FF4136", # symbol_2: red "#2ECC40", # symbol_3: green "#FFDC00", # symbol_4: yellow "#AAAAAA", # symbol_5: grey "#F012BE", # symbol_6: fuschia "#FF851B", # symbol_7: orange "#7FDBFF", # symbol_8: teal "#870C25" # symbol_9: brown ]) ``` -------------------------------- ### Crop Grid Function Source: https://github.com/dandarm/hrm-ts/blob/main/arc_eval.ipynb Crops a 30x30 grid to the largest possible rectangle that does not contain any EOS tokens (values < 2 or > 11). It iterates through possible row and column sizes to find the maximum area. Requires the numba njit decorator for optimization. ```python @njit def crop(grid: np.ndarray): # Find maximum-sized rectangle without any EOS token inside. grid = grid.reshape(30, 30) max_area = 0 max_size = (0, 0) nr, nc = grid.shape num_c = nc for num_r in range(1, nr + 1): # Scan for maximum c for c in range(1, num_c + 1): x = grid[num_r - 1, c - 1] if (x < 2) | (x > 11): num_c = c - 1 break area = num_r * num_c if area > max_area: max_area = area max_size = (num_r, num_c) return grid[:max_size[0], :max_size[1]] - 2 ``` -------------------------------- ### Slice 1D Typed Array in JavaScript Source: https://github.com/dandarm/hrm-ts/blob/main/puzzle_visualizer.html The `slice1D` function extracts a portion of a typed array. It takes the array, a start index, and an end index, returning a new `Uint32Array` containing the specified elements. This is useful for segmenting data, such as input or output sequences for processing. ```javascript function slice1D(arr, start, end) { const result = new Uint32Array(end - start); for (let i = start; i < end; i++) { result[i - start] = Number(arr[i]); } return result; } ``` -------------------------------- ### Launch Training with Hydra Configuration (Bash) Source: https://context7.com/dandarm/hrm-ts/llms.txt Commands to launch pretraining using Hydra for configuration management. Shows single GPU, distributed training with torchrun, and overriding architecture parameters. ```bash # Single GPU training python pretrain.py data_path=data/arc-aug-1000 epochs=10000 lr=1e-4 # Distributed training with torchrun torchrun --nproc_per_node=4 pretrain.py \ data_path=data/arc-aug-1000 \ global_batch_size=768 \ epochs=100000 \ lr=1e-4 # Override architecture parameters python pretrain.py \ arch.H_layers=4 \ arch.L_layers=4 \ arch.hidden_size=512 \ arch.num_heads=8 ``` -------------------------------- ### Create Sliding Window Datasets with TimeSeriesWindows Source: https://context7.com/dandarm/hrm-ts/llms.txt The `TimeSeriesWindows` class preprocesses time series data into sliding input/output windows. It handles automatic normalization and can incorporate optional calendar features. The output is suitable for PyTorch's DataLoader. ```python import numpy as np from torch.utils.data import DataLoader from dataset import TimeSeriesWindows # Generate sample time series data np.random.seed(42) T = 1000 data = np.cumsum(np.random.randn(T, 3), axis=0).astype(np.float32) # 3-feature series # Create dataset with 20-step input windows predicting 5 steps ahead dataset = TimeSeriesWindows( X=data, # Input features [N, D_in] Y=data, # Target values [N, D_out] (can differ from X) T_in=20, # Input window length T_out=5, # Prediction horizon stride=1, # Step between consecutive windows stats=None, # Auto-compute normalization stats from X calendar_feats=None # Optional: callable returning temporal features ) # Access normalization statistics print(f"Mean: {dataset.stats['mu'].shape}, Std: {dataset.stats['sd'].shape}") # Mean: (1, 3), Std: (1, 3) # Create DataLoader loader = DataLoader(dataset, batch_size=32, shuffle=True) # Iterate over batches for batch in loader: x_in = batch["x_in"] # [32, 20, 3] - normalized input windows y_out = batch["y_out"] # [32, 5, 3] - target values print(f"Input shape: {x_in.shape}, Target shape: {y_out.shape}") break ``` -------------------------------- ### Run Inference with PyTorch Source: https://context7.com/dandarm/hrm-ts/llms.txt This snippet demonstrates how to run inference using a PyTorch model within an inference mode context. It iterates through an evaluation data loader, moves batches to CUDA, initializes model carry, performs a forward pass, and prints the accuracy metric. ```python with torch.inference_mode(): for batch in eval_loader: batch = {k: v.cuda() for k, v in batch.items()} carry = train_state.model.initial_carry(batch) carry, _, metrics, preds, _ = train_state.model(carry=carry, batch=batch, return_keys=["logits"]) print(f"Accuracy: {metrics['accuracy']:.4f}") ``` -------------------------------- ### Evaluate Model Checkpoint (Bash) Source: https://context7.com/dandarm/hrm-ts/llms.txt Bash commands to evaluate a trained model checkpoint. Demonstrates loading a specific checkpoint and optionally saving outputs like inputs, labels, and logits. ```bash # Evaluate a checkpoint python evaluate.py checkpoint=checkpoints/project/run/step_50000 # With custom output saving python evaluate.py \ checkpoint=checkpoints/project/run/step_50000 \ save_outputs='[inputs,labels,logits]' ``` -------------------------------- ### SwiGLU Feedforward Network (PyTorch) Source: https://context7.com/dandarm/hrm-ts/llms.txt Demonstrates the usage of a SwiGLU feedforward network. It takes input tensor 'x' and produces an output tensor of the same shape. ```python import torch # Assuming SwiGLU is defined elsewhere mlp = SwiGLU(hidden_size=64, expansion=4.0) x = torch.randn(8, 100, 64) mlp_out = mlp(x) print(f"MLP output: {mlp_out.shape}") # [8, 100, 64] ``` -------------------------------- ### Time Series Data Windowing (NumPy/PyTorch) Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Demonstrates the data pipeline for time series, which involves windowing input sequences (T_in) to output sequences (T_out) with normalization and optional calendar features. It supports both NumPy and PyTorch backends, selected via dataset initialization. ```python from dataset import TimeSeriesWindows # Example usage (conceptual) dataset_np = TimeSeriesWindows(backend='numpy', ...) dataset_torch = TimeSeriesWindows(backend='torch', ...) # The output is PyTorch tensors ready for training. ``` -------------------------------- ### Programmatic Model Evaluation (PyTorch) Source: https://context7.com/dandarm/hrm-ts/llms.txt Python code for loading a trained checkpoint and evaluating the model programmatically. Initializes the training state and loads the model weights. ```python # Programmatic evaluation import torch from pretrain import PretrainConfig, init_train_state, create_dataloader # Load checkpoint checkpoint_path = "checkpoints/project/run/step_50000" model_state = torch.load(checkpoint_path, map_location="cuda") # Initialize model and load weights train_state = init_train_state(config, train_metadata, world_size=1) train_state.model.load_state_dict(model_state) train_state.model.eval() ``` -------------------------------- ### Dynamic Attention Backend Selection and Logging Source: https://github.com/dandarm/hrm-ts/blob/main/README.md Implements dynamic selection of attention backends, such as FlashAttention, based on hardware capabilities and configuration. It includes logging functionalities for attention mechanisms via the 'hrm.attn' logger. ```python from models.layers import set_use_flash import logging logger = logging.getLogger('hrm.attn') # Example usage (conceptual) # set_use_flash(True) # Enable flash attention if available # Logs attention mechanism details. ``` -------------------------------- ### RMS Normalization (PyTorch) Source: https://context7.com/dandarm/hrm-ts/llms.txt Illustrates the application of RMS normalization to input tensor 'x'. Requires a variance epsilon for numerical stability. ```python import torch # Assuming rms_norm is defined elsewhere x = torch.randn(8, 100, 64) normalized = rms_norm(x, variance_epsilon=1e-5) ``` -------------------------------- ### Multi-Head Attention Module (PyTorch) Source: https://context7.com/dandarm/hrm-ts/llms.txt Shows the instantiation and usage of a multi-head attention module. It takes hidden states and optionally cosine/sine embeddings for attention calculation. ```python import torch # Assuming Attention and cos, sin are defined elsewhere attn = Attention( hidden_size=64, head_dim=16, num_heads=4, num_key_value_heads=4, causal=False # Non-causal for bidirectional attention ) x = torch.randn(8, 100, 64) attn_out = attn(cos_sin=(cos, sin), hidden_states=x) print(f"Attention output: {attn_out.shape}") # [8, 100, 64] ``` -------------------------------- ### Load ARC Dataset from Local Files Source: https://github.com/dandarm/hrm-ts/blob/main/puzzle_visualizer.html This asynchronous JavaScript function is triggered when the 'Load' button is clicked. It constructs file paths for inputs, labels, puzzle indices, group indices, and puzzle identifiers based on the selected set and subset. It then calls `loadDataset` to parse these files using Npyjs and FileReader, and finally calls `buildGroupList` to display the loaded data. ```javascript document.getElementById("loadBtn").addEventListener("click", async () => { document.getElementById("groupList").innerHTML = ""; document.getElementById("puzzleList").innerHTML = ""; document.getElementById("puzzleView").innerHTML = ""; const setName = document.getElementById("setSelect").value; const subsetName = document.getElementById("subsetSelect").value; try { await loadDataset(setName, subsetName); buildGroupList(); } catch (err) { console.error(err); alert("Error while loading dataset: " + err); } }); async function loadDataset(setName, subsetName) { const prefix = `${setName}/${subsetName}__`; const inputsPath = prefix + "inputs.npy"; const labelsPath = prefix + "labels.npy"; const pIdxPath = prefix + "puzzle_indices.npy"; const gIdxPath = prefix + "group_indices.npy"; const pIdsPath = prefix + "puzzle_identifiers.npy"; const identifi ``` -------------------------------- ### Build Group List UI Source: https://github.com/dandarm/hrm-ts/blob/main/puzzle_visualizer.html Dynamically generates a list of group elements in the UI based on the global `groupIndicesArr`. Each group is displayed as a clickable span, triggering `onSelectGroup` when clicked. ```javascript function buildGroupList() { document.getElementById("groupList").innerHTML = "