Skip to content

Commit

Permalink
Remove the heacks to handle dynamically sized tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
newhouseb committed Apr 23, 2023
1 parent 6ff18df commit a49e51b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addMatrix, causalMask, copy, gelu, getSlice, layerNorm, linear, mapInPlace, merge, multiplyMatrix, softmax, split, tensor, transposeMatrix, unsqueeze } from "./math";
import { Var, addMatrix, causalMask, copy, gelu, getSlice, layerNorm, linear, mapInPlace, merge, multiplyMatrix, softmax, split, tensor, transposeMatrix, unsqueeze } from "./math";
import * as fs from 'fs';
import { inflate } from 'zlib';
import { decode } from '@msgpack/msgpack';
Expand Down Expand Up @@ -125,10 +125,7 @@ async function main() {

const gpt = await loadSmallGPT();

const inputs = tensor([gpt.SequenceLength, gpt.EmbeddingDimensions])

// Fake truncate things
inputs.shape = [tokens.length as any, gpt.EmbeddingDimensions]
const inputs = tensor([Var(tokens.length, 'Sequence Length'), gpt.EmbeddingDimensions])

// Map each token into an embedding + position vector
tokens.map((token, i) => {
Expand Down Expand Up @@ -183,10 +180,10 @@ async function main() {
console.log("Splitting out k, q, and v tensors");

// Next split out each of the heads
const kHeads = split(k, gpt.EmbeddingDimensions / gpt.AttentionHeads as 64);
const qHeads = split(q, gpt.EmbeddingDimensions / gpt.AttentionHeads as 64);
const vHeads = split(v, gpt.EmbeddingDimensions / gpt.AttentionHeads as 64);
const aHeads = [] as Tensor<readonly [typeof gpt.SequenceLength, 64]>[];
const kHeads = split(k, Var(gpt.EmbeddingDimensions / gpt.AttentionHeads, 'Head Width'));
const qHeads = split(q, Var(gpt.EmbeddingDimensions / gpt.AttentionHeads, 'Head Width'));
const vHeads = split(v, Var(gpt.EmbeddingDimensions / gpt.AttentionHeads, 'Head Width'));
const aHeads = [] as Tensor<readonly [Var<'Sequence Length'>, Var<'Head Width'>]>[];

console.log("Performing self-attention");

Expand Down
4 changes: 2 additions & 2 deletions math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Used to define a dynamically sized (at runtime) dimension
type Var<N extends string> = number & { label: N };
const Var = <L extends string>(d: number, label: L) => { return d as Var<L> };
export type Var<N extends string> = number & { label: N };
export const Var = <L extends string>(d: number, label: L) => { return d as Var<L> };

// Used to ensure that there's not ambiguity that worms its way through the type system via Union
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
Expand Down

0 comments on commit a49e51b

Please sign in to comment.