Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/hub/index.ts
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));
}
})();
72 changes: 72 additions & 0 deletions packages/hub/src/lib/hardware-requirements.ts
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;
Copy link
Member

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 in tasks.ModelData so it's clearer the data is coming from normalized parsing of HF model repos

Copy link
Member

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 or gguf modules rather than here

Copy link
Member Author

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 4096

Setting 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)

Copy link
Member

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"?

Copy link
Member

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)

}) {
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);
}
Loading