-
Notifications
You must be signed in to change notification settings - Fork 359
wip: add HW requirements calculator #1216
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
Draft
ngxson
wants to merge
1
commit into
huggingface:main
Choose a base branch
from
ngxson:xsn/hw_requirement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 |
---|---|---|
@@ -1 +1,20 @@ | ||
export * from "./src"; | ||
|
||
// TODO: remove this before merging | ||
// Run with: npx ts-node index.ts | ||
import { getHardwareRequirements } from "./src/lib/hardware-requirements"; | ||
(async () => { | ||
const models = [ | ||
"hexgrad/Kokoro-82M", | ||
"microsoft/OmniParser-v2.0", | ||
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", | ||
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B", | ||
"NousResearch/DeepHermes-3-Llama-3-8B-Preview", | ||
"unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit", | ||
]; | ||
|
||
for (const name of models) { | ||
const mem = await getHardwareRequirements({ name }); | ||
console.log('mem', JSON.stringify(mem, null, 2)); | ||
} | ||
})(); |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ListFileEntry, listFiles } from "./list-files"; | ||
|
||
export interface MemoryRequirements { | ||
minimumGigabytes: number; | ||
recommendedGigabytes: number; | ||
}; | ||
|
||
export interface HardwareRequirements { | ||
name: string; | ||
memory: MemoryRequirements; | ||
}; | ||
|
||
export async function getHardwareRequirements(params: { | ||
/** | ||
* The model name in the format of `namespace/repo`. | ||
*/ | ||
name: string; | ||
/** | ||
* The context size in tokens, default to 2048. | ||
*/ | ||
contextSize?: number; | ||
}) { | ||
const files = await getFiles(params.name); | ||
const hasSafetensors = files.some((file) => file.path.endsWith(".safetensors")); | ||
const hasPytorch = files.some((file) => file.path.endsWith(".pth")); | ||
|
||
// Get the total size of the model weight in bytes (we don't care about quantization scheme) | ||
let totalWeightBytes = 0; | ||
if (hasSafetensors) { | ||
totalWeightBytes = sumFileSize(files.filter((file) => file.path.endsWith(".safetensors"))); | ||
} else if (hasPytorch) { | ||
totalWeightBytes = sumFileSize(files.filter((file) => file.path.endsWith(".pth"))); | ||
} | ||
|
||
// Calculate the memory for context window | ||
// TODO: this also scales in function of weight, to be implemented later | ||
const contextWindow = params.contextSize ?? 2048; | ||
const batchSize = 256; // a bit overhead for batching | ||
const contextMemoryBytes = (contextWindow + batchSize) * 0.5 * 1e6; | ||
|
||
// Calculate the memory overhead | ||
const osOverheadBytes = Math.max(512 * 1e6, 0.2 * totalWeightBytes); | ||
|
||
// Calculate the total memory requirements | ||
const totalMemoryGb = (totalWeightBytes + contextMemoryBytes + osOverheadBytes) / 1e9; | ||
|
||
return { | ||
name: params.name, | ||
memory: { | ||
minimumGigabytes: totalMemoryGb, | ||
recommendedGigabytes: totalMemoryGb * 1.1, | ||
}, | ||
} satisfies HardwareRequirements; | ||
} | ||
|
||
async function getFiles(name: string): Promise<ListFileEntry[]> { | ||
const files: ListFileEntry[] = []; | ||
const cursor = listFiles({ | ||
repo: { | ||
name, | ||
type: "model", | ||
}, | ||
}); | ||
for await (const entry of cursor) { | ||
files.push(entry); | ||
} | ||
return files; | ||
}; | ||
|
||
function sumFileSize(files: ListFileEntry[]): number { | ||
return files.reduce((total, file) => total + file.size, 0); | ||
} |
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.
if possible
contextSize
should be taken intasks.ModelData
so it's clearer the data is coming from normalized parsing of HF model reposThere 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.
and tbh i'm wondering if this kinda of method should better live in
tasks
orgguf
modules rather than hereThere 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.
IMO the
contextSize
should be a number that most users gonna use in practice, for example 2048 or 4096Setting it to the max
contextSize
of the model may not be useful, as most users never have enough VRAM to run full context length anyway (especially, 128k context are more and more common nowadays)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.
ok, i see your point. cc @gary149 wdyt?
maybe it's another selector in the UI then with "user-set context size"?
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.
We could set an educated default of 8k tho (claude was 8k only for a long time)