-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Streamline in-memory span eviction thresholds #161
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
Merged
Yuge Zhang (ultmaster)
merged 7 commits into
main
from
codex/add-span-eviction-for-inmemorylightningstore
Oct 16, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d41212
Streamline in-memory span eviction thresholds
ultmaster 1f89907
Refine span memory accounting and typing
ultmaster 3acb8ff
Fix psutil import
ultmaster f0ee52c
fix pyright issues
ultmaster 3ed5602
add docs
ultmaster 1686a07
add logs
ultmaster 2bb2851
Merge branch 'main' of github.com:microsoft/agent-lightning into code…
ultmaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,13 +6,30 @@ | |||||
| import functools | ||||||
| import hashlib | ||||||
| import logging | ||||||
| import sys | ||||||
| import threading | ||||||
| import time | ||||||
| import uuid | ||||||
| from collections import deque | ||||||
| from typing import Any, Callable, Counter, Dict, List, Literal, Optional, Sequence, TypeVar, cast | ||||||
| from collections.abc import Iterable | ||||||
| from collections.abc import Mapping as MappingABC | ||||||
| from typing import ( | ||||||
| Any, | ||||||
| Callable, | ||||||
| Counter, | ||||||
| Dict, | ||||||
| List, | ||||||
| Literal, | ||||||
| Mapping, | ||||||
| Optional, | ||||||
| Sequence, | ||||||
| Set, | ||||||
| TypeVar, | ||||||
| cast, | ||||||
| ) | ||||||
|
|
||||||
| from opentelemetry.sdk.trace import ReadableSpan | ||||||
| from pydantic import BaseModel | ||||||
|
|
||||||
| from agentlightning.types import ( | ||||||
| Attempt, | ||||||
|
|
@@ -35,6 +52,21 @@ | |||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
||||||
| def estimate_model_size(obj: Any) -> int: | ||||||
| """Rough recursive size estimate for Pydantic BaseModel instances.""" | ||||||
|
|
||||||
| if isinstance(obj, BaseModel): | ||||||
| values = cast(Iterable[Any], obj.__dict__.values()) | ||||||
| return sum(estimate_model_size(value) for value in values) + sys.getsizeof(cast(object, obj)) | ||||||
| if isinstance(obj, MappingABC): | ||||||
| mapping = cast(Mapping[Any, Any], obj) | ||||||
| return sum(estimate_model_size(value) for value in mapping.values()) + sys.getsizeof(cast(object, obj)) | ||||||
| if isinstance(obj, (list, tuple, set)): | ||||||
| iterable = cast(Iterable[Any], obj) | ||||||
| return sum(estimate_model_size(value) for value in iterable) + sys.getsizeof(cast(object, obj)) | ||||||
| return sys.getsizeof(cast(object, obj)) | ||||||
|
|
||||||
|
|
||||||
| def _healthcheck_wrapper(func: T_callable) -> T_callable: | ||||||
| """ | ||||||
| Decorator to run the watchdog healthcheck **before** executing the decorated method. | ||||||
|
|
@@ -82,16 +114,43 @@ def _generate_attempt_id() -> str: | |||||
| return "at-" + short_id | ||||||
|
|
||||||
|
|
||||||
| def _detect_total_memory_bytes() -> int: | ||||||
| """Best-effort detection of the total available system memory in bytes.""" | ||||||
|
|
||||||
| try: | ||||||
| import psutil | ||||||
|
|
||||||
| return int(psutil.virtual_memory().total) | ||||||
| except ImportError: | ||||||
| # Fallback to 8GB if memory cannot be detected. | ||||||
| logger.error("psutil is not installed. Falling back to 8GB of memory in total.") | ||||||
| return 8 * 1024**3 | ||||||
|
|
||||||
|
|
||||||
| class InMemoryLightningStore(LightningStore): | ||||||
| """ | ||||||
| In-memory implementation of LightningStore using Python data structures. | ||||||
| Thread-safe and async-compatible but data is not persistent. | ||||||
|
|
||||||
| The methods in this class should generally not call each other, | ||||||
| especially those that are locked. | ||||||
|
|
||||||
| Args: | ||||||
| eviction_memory_threshold: The threshold for evicting spans in bytes. | ||||||
| By default, it's 70% of the total VRAM available. | ||||||
|
||||||
| By default, it's 70% of the total VRAM available. | |
| By default, it's 70% of the total RAM available. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mapping size estimation is incomplete. It should include the size of dictionary keys as well as values.