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

fix: Fix memory allocation of ndarray #1704

Merged
merged 8 commits into from
Sep 19, 2024
Merged

fix: Fix memory allocation of ndarray #1704

merged 8 commits into from
Sep 19, 2024

Conversation

xu-song
Copy link
Contributor

@xu-song xu-song commented Aug 25, 2024

Motivation

self.scores[state.n_tokens :, :] = 0.0

The above part will allocate a large amount of memory.
This commit reduces the memory consumption from more than 10GB to 0GB.

Simle Reproduce

import os
import numpy as np
import psutil

def print_mem():
    mem = psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024
    print(f"process_mem: {mem:.2f} GB")

n_ctx = 32768
n_vocab = 150000
scores = np.ndarray((n_ctx, n_vocab), dtype=np.single)
print_mem()   # 0.03 GB;

scores[:1000, :] = 0.5
print_mem()  # 0.59 GB

# zero-ing out the rest
scores[1000:, :] = 0.0     #  ----- large memory allocation
print_mem()  # 18.34 GB

After this pr

zero-ing out the rest will not allocate extra memory if not necessary.

# zero-ing out the rest
rest = scores[1000:, :]
rest[rest > 0] = 0.0
print_mem()  # 0.59 GB

@abetlen
Copy link
Owner

abetlen commented Sep 19, 2024

@xu-song thanks for the contribution. Just added some tests and bug fixes since the recent sampler api changes.

@abetlen abetlen merged commit 22cedad into abetlen:main Sep 19, 2024
14 checks passed
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.

2 participants