Skip to content

LD_PRELOAD Script - #8

Draft
EduardDurech wants to merge 1 commit into
fzyzcjy:masterfrom
EduardDurech:LD_PRELOAD_script
Draft

LD_PRELOAD Script#8
EduardDurech wants to merge 1 commit into
fzyzcjy:masterfrom
EduardDurech:LD_PRELOAD_script

Conversation

@EduardDurech

Copy link
Copy Markdown
Contributor
  • Package data contained in torch_memory_saver
  • tms python sets LD_PRELOAD

- Package data contained in torch_memory_saver
- tms python sets `LD_PRELOAD`

@fzyzcjy fzyzcjy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

Comment thread scripts/tms

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what about naming tms to torch_memory_saver

Comment thread scripts/tms
Comment on lines +5 to +9
import torch_memory_saver, importlib.machinery, pathlib, os
package_dir = pathlib.Path(torch_memory_saver.__file__).parent
ext_suffix = importlib.machinery.EXTENSION_SUFFIXES[0]
lib_name = f'torch_memory_saver_cpp{ext_suffix}'
print(str(package_dir / lib_name))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what about we provide such a function in torch_memory_saver python code, and here we only need to do as simple as $(python -c "import torch_memory_saver; torch_memory_saver.that_function()"

Comment thread scripts/tms
")

# Detect if script is being sourced
(return 0 2>/dev/null) && SOURCED=1 || SOURCED=0

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: curious whether this will accurately detect whether it is being sourced, or will there false positive / negative?

return _BinaryInfo(cdll=ctypes.CDLL(env_ld_preload))
paths = env_ld_preload.split(':')
lib_path = next((path for path in paths if 'torch_memory_saver' in path), None)
if lib_path:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: looks like we may have a bug for the else case (or, if the else branch will never occur, we may change if to assert)

assert os.environ[key] == value
os.environ[key] = old_value
logger.debug(f'change_env restore key={key} value={old_value}')
_global_info = _GlobalInfo() No newline at end of file

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, it would be great to keep the old configure_subprocess etc, because it is how SGLang uses it. https://github.com/sgl-project/sglang/blob/8aab7fdb21e86b87a3141951ce498246fd4aae73/python/sglang/srt/entrypoints/engine.py#L522

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look at the approach later and modify PR, not by system for a few days

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take your time!

Comment thread README.md

## Installation

For the library to work properly, it needs to be preloaded using the `LD_PRELOAD` environment variable.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would be great to mention the SGLang approach, which indeed does not require any operations

@fzyzcjy

fzyzcjy commented Apr 15, 2025

Copy link
Copy Markdown
Owner

@EduardDurech Btw, I am curious how/where are you using torch_memory_saver?

@EduardDurech

Copy link
Copy Markdown
Contributor Author

@fzyzcjy right now standalone API

import os, torch
from torch_memory_saver import TorchMemorySaver

memory_saver = TorchMemorySaver()
print(os.system('nvidia-smi'))

with memory_saver.region():
    x = torch.full((1_000_000_000,), 100, dtype=torch.uint8, device='cuda')

print(os.system('nvidia-smi'))

memory_saver.pause()
print(os.system('nvidia-smi'))

memory_saver.resume()
print(os.system('nvidia-smi'))

Testing, will use with veRL-SGLang, did you guys implement LD_PRELOAD there with subprocess?

@fzyzcjy

fzyzcjy commented Apr 15, 2025

Copy link
Copy Markdown
Owner

will use with veRL-SGLang, did you guys implement LD_PRELOAD there with subprocess?

Yes, if you are using memory saver inside SGLang, then the LD_PRELOAD is automatically configured and you do not need to do anything to make it work

@EduardDurech

Copy link
Copy Markdown
Contributor Author

Okay, do you know where in the code it finds the .so? I couldn't find invocation of torch_memory_saver, maybe it would be good to be compatible as standalone and as dependency, but currently the PR moves the .so to packages/torch_memory_saver instead of packages

@fzyzcjy

fzyzcjy commented Apr 15, 2025

Copy link
Copy Markdown
Owner

do you know where in the code it finds the .so?

Just follow the configure_subprocess function you will see

@EduardDurech
EduardDurech marked this pull request as draft May 18, 2025 22:04
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