Skip to content

fix: Fix memory allocation of ndarray - #1704

Merged
abetlen merged 8 commits into
abetlen:mainfrom
xu-song:patch-3
Sep 19, 2024
Merged

abetlen merged 8 commits into
abetlen:mainfrom
xu-song:patch-3

Conversation

@xu-song

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

Copy link
Copy Markdown
Contributor

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

abetlen commented Sep 19, 2024

Copy link
Copy Markdown
Owner

@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
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