Skip to content

Instantly share code, notes, and snippets.

View wolfecameron's full-sized avatar

Cameron R. Wolfe wolfecameron

View GitHub Profile
@wolfecameron
wolfecameron / masked_self_attention.py
Last active July 12, 2024 15:42
Basic PyTorch implementation of masked self-attention with a single attention head.
"""
Source: https://github.com/karpathy/nanoGPT/blob/master/model.py
"""
import math
import torch
from torch import nn
import torch.nn.functional as F
class MaskedSelfAttention(nn.Module):
@wolfecameron
wolfecameron / gpt.py
Created March 2, 2024 22:02
Implementation of a GPT-style decoder only transformer.
"""
Source: https://github.com/karpathy/nanoGPT/blob/master/model.py
"""
import torch
from torch import nn
import torch.nn.functional as F
class GPT(nn.Module):
@wolfecameron
wolfecameron / decoder_only_block.py
Created March 2, 2024 21:43
Implementation of a decoder-only transformer block.
"""
Source: https://github.com/karpathy/nanoGPT/blob/master/model.py
"""
from torch import nn
class Block(nn.Module):
def __init__(
self,
d,
@wolfecameron
wolfecameron / transformer_ffnn.py
Created March 2, 2024 21:37
Feed-forward layer of a transformer.
"""
Source: https://github.com/karpathy/nanoGPT/blob/master/model.py
"""
from torch import nn
class FFNN(nn.Module):
def __init__(
self,
@wolfecameron
wolfecameron / exploding_activations.py
Created March 2, 2024 21:23
Exploding activations from repeated matrix multiplications.
import torch
# experiment settings
d = 5
nlayers = 100
normalize = False # set True to use normalization
# create vector with random entries between [-1, 1]
input_vector = (torch.rand(d) - 0.5) * 2.0
@wolfecameron
wolfecameron / causal_self_attention.py
Last active December 23, 2024 10:43
Implementation of causal self-attention in PyTorch
"""
Source: https://github.com/karpathy/nanoGPT/blob/master/model.py
"""
import math
import torch
from torch import nn
import torch.nn.functional as F
class CausalSelfAttention(nn.Module):
@wolfecameron
wolfecameron / cartier_session2_links
Last active October 12, 2023 04:01
Links from LLM presentation for Cartier (week 2).
@wolfecameron
wolfecameron / cartier_session1_links.txt
Created September 27, 2023 13:58
Links from LLM presentation for Cartier (week 1).
@wolfecameron
wolfecameron / llm_preso_links_2.txt
Created August 10, 2023 13:42
LLM Presentation Links (EY Week #2)