Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimised embedding #59

Merged
merged 1 commit into from
May 21, 2024
Merged

optimised embedding #59

merged 1 commit into from
May 21, 2024

Conversation

bclarkson-code
Copy link
Owner

The embedding layer has been optimsed and is now ~8.5x faster!
I ran this benchmark:

import numpy as np

from tricycle.layers import Embedding, EmbeddingV2
from tricycle.tensor import to_tensor

N_LOOPS = 100
DEVICE = 1


def test_embedding_original():
    np.random.seed(0)
    x = np.random.randint(0, 64, size=64)
    x = to_tensor(x, is_vector=True, requires_grad=False, dtype=int)
    x.to_gpu(DEVICE)

    layer = Embedding(from_size=64, to_size=1024)
    layer.to_gpu(DEVICE)

    for _ in range(N_LOOPS):
        out = layer(x)
        out.backward()


def test_embedding_new():
    np.random.seed(0)
    x = np.random.randint(0, 64, size=64)
    x = to_tensor(x, is_vector=True, requires_grad=False, dtype=int)
    x.to_gpu(DEVICE)

    layer = EmbeddingV2(from_size=64, to_size=1024)
    layer.to_gpu(DEVICE)

    for _ in range(N_LOOPS):
        out = layer(x)
        out.backward()


__benchmarks__ = [(test_embedding_original, test_embedding_new, "original")]

And richbench gave this:


┏━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Benchmark ┃ Min     ┃ Max     ┃ Mean    ┃ Min (+)         ┃ Max (+)         ┃ Mean (+)        ┃
┡━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│  original │ 0.677   │ 0.744   │ 0.692   │ 0.080 (8.5x)    │ 0.081 (9.2x)    │ 0.080 (8.6x)    │
└───────────┴─────────┴─────────┴─────────┴─────────────────┴─────────────────┴─────────────────┘

The main changes were simply removing python lists and replacing them with proper vectorised operations.

@bclarkson-code bclarkson-code linked an issue May 21, 2024 that may be closed by this pull request
@bclarkson-code bclarkson-code merged commit cdc124c into main May 21, 2024
1 check failed
@bclarkson-code bclarkson-code deleted the 58-optimise-embedding branch May 21, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimise embedding
1 participant