This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Conceptual pseudocode explaining how sliding-window attention works | |
| using loops. This is NOT meant to be efficient or runnable in a real | |
| model. It simply illustrates what PyTorch operations like: | |
| unfold | |
| unsqueeze | |
| squeeze | |
| batched matrix multiplication |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a | |
| abandon | |
| ability | |
| able | |
| abortion | |
| about | |
| above | |
| abroad | |
| absence | |
| absolute |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Counter: | |
| def __init__(self): | |
| self.count = 0 | |
| def get_value(self): | |
| return self.count | |
| def increment(self): | |
| self.count += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Counter: | |
| def __init__(self): | |
| self.count = 0 | |
| def get_value(self): | |
| return self.count | |
| def increment(self): | |
| self.count += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Counter: | |
| def __init__(self): | |
| self.count = 0 | |
| def get_value(self): | |
| return self.count | |
| def increment(self): | |
| self.count += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import random | |
| from math import ceil | |
| import numpy as np | |
| import torch | |
| import torch.nn.functional as F | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from tqdm.auto import tqdm | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| from typing import Optional, Tuple, List, Any | |
| class TreeNode: | |
| """Represents a node in the parse tree.""" | |
| def __init__(self, rule_name: str, content: Any = None, children: List['TreeNode'] = None): | |
| self.rule_name = rule_name | |
| self.content = content # For terminal nodes | |
| self.children = children or [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from copy import copy | |
| from collections import defaultdict, Counter | |
| from functools import cache | |
| from glob import glob | |
| import pandas as pd | |
| import json | |
| from tqdm import tqdm | |
| @cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import torch.nn as nn | |
| from collections import defaultdict | |
| # --- Hook Implementation --- | |
| # Dictionary to store attention weights during the forward pass | |
| # Structure: {layer_index: attention_weights_tensor} | |
| # The tensor shape will be (batch_size, num_heads, seq_len, seq_len) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib.request | |
| from collections import defaultdict | |
| raw_bytes = urllib.request.urlopen( | |
| 'http://www.sls.hawaii.edu/bley-vroman/brown.txt') | |
| brown_corpus = raw_bytes.read().decode('utf8').replace('\r\n', '\n') | |
| B = brown_corpus[:250] | |
| def find_pairs(text): | |
| pairs = defaultdict(int) |
NewerOlder