1
0
Fork 0
mirror of https://github.com/p-e-w/heretic.git synced 2026-12-03 18:42:57 +07:00

[GH-ISSUE #80] A small suggestion about memory allocation #37

Closed
opened 2026-11-01 04:16:52 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @noctrex on GitHub (Dec 10, 2025).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/80

Might I make a suggestion to utilize the max_memory functionality, so that we can use it in the config.toml like this:
max_memory = {0 = "24GB", "cpu" = "64GB"}

in order to set a fixed amount of VRAM to use on a card.

diff:

 src/heretic/config.py | 5 +++++
 src/heretic/model.py  | 9 +++++++++
 2 files changed, 14 insertions(+)

diff --git a/src/heretic/config.py b/src/heretic/config.py
index a69c059..c786349 100644
--- a/src/heretic/config.py
+++ b/src/heretic/config.py
@@ -61,6 +61,11 @@ class Settings(BaseSettings):
         description="Device map to pass to Accelerate when loading the model.",
     )
 
+    max_memory: Dict[str, str] | None = Field(
+        default=None,
+        description="Maximum memory to allocate per device (e.g., {'0': '20GB', 'cpu': '64GB'}).",
+    )
+
     trust_remote_code: bool | None = Field(
         default=None,
         description="Whether to trust remote code when loading the model.",
diff --git a/src/heretic/model.py b/src/heretic/model.py
index cf8185b..11bcb3f 100644
--- a/src/heretic/model.py
+++ b/src/heretic/model.py
@@ -63,10 +63,19 @@ class Model:
             print(f"* Trying dtype [bold]{dtype}[/]... ", end="")
 
             try:
+                max_memory = (
+                    {
+                        int(k) if k.isdigit() else k: v
+                        for k, v in settings.max_memory.items()
+                    }
+                    if settings.max_memory
+                    else None
+                )
                 self.model = AutoModelForCausalLM.from_pretrained(
                     settings.model,
                     dtype=dtype,
                     device_map=settings.device_map,
+                    max_memory=max_memory,
                     trust_remote_code=self.trusted_models.get(settings.model),
                 )
 
Originally created by @noctrex on GitHub (Dec 10, 2025). Original GitHub issue: https://github.com/p-e-w/heretic/issues/80 Might I make a suggestion to utilize the max_memory functionality, so that we can use it in the config.toml like this: `max_memory = {0 = "24GB", "cpu" = "64GB"}` in order to set a fixed amount of VRAM to use on a card. diff: ```` src/heretic/config.py | 5 +++++ src/heretic/model.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/heretic/config.py b/src/heretic/config.py index a69c059..c786349 100644 --- a/src/heretic/config.py +++ b/src/heretic/config.py @@ -61,6 +61,11 @@ class Settings(BaseSettings): description="Device map to pass to Accelerate when loading the model.", ) + max_memory: Dict[str, str] | None = Field( + default=None, + description="Maximum memory to allocate per device (e.g., {'0': '20GB', 'cpu': '64GB'}).", + ) + trust_remote_code: bool | None = Field( default=None, description="Whether to trust remote code when loading the model.", diff --git a/src/heretic/model.py b/src/heretic/model.py index cf8185b..11bcb3f 100644 --- a/src/heretic/model.py +++ b/src/heretic/model.py @@ -63,10 +63,19 @@ class Model: print(f"* Trying dtype [bold]{dtype}[/]... ", end="") try: + max_memory = ( + { + int(k) if k.isdigit() else k: v + for k, v in settings.max_memory.items() + } + if settings.max_memory + else None + ) self.model = AutoModelForCausalLM.from_pretrained( settings.model, dtype=dtype, device_map=settings.device_map, + max_memory=max_memory, trust_remote_code=self.trusted_models.get(settings.model), ) ````
Author
Owner

@p-e-w commented on GitHub (Dec 11, 2025):

This makes sense, and I'd be happy to accept a pull request!

<!-- gh-comment-id:3640164151 --> @p-e-w commented on GitHub (Dec 11, 2025): This makes sense, and I'd be happy to accept a pull request!
Author
Owner

@p-e-w commented on GitHub (Dec 29, 2025):

Implemented in #83.

<!-- gh-comment-id:3696278324 --> @p-e-w commented on GitHub (Dec 29, 2025): Implemented in #83.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
p-e-w/heretic#37
No description provided.