-
Notifications
You must be signed in to change notification settings - Fork 23
Refactor/cleanup model loaders #58
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
mikepapadim
merged 7 commits into
beehive-lab:main
from
mikepapadim:refactor/cleanup_model_loaders
Nov 6, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ba9104
Refactor: remove AOT.java and update model loaders to enhance modular…
mikepapadim 8b06ffb
Support Q8_0 models for Qwen2 and Deepseek
mairooni fd0b6c6
Support Q8_0 for Qwen3
mairooni ea06ee5
[WIP] Support Q8_0 for Phi3 - testing pending
mairooni 320850c
Refactor: remove AOT.java and update model loaders to enhance modular…
mikepapadim fb9939e
Refine model loader refactoring and converge with Q8 support
orionpapadakis eb13ea7
Move Qwen2Q8_0TornadoVMLayerPlanner to tornadovm package and weights …
orionpapadakis 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
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...nference/weights/tornado/FP16Weights.java → ...nce/weights/tornado/fp16/FP16Weights.java
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
2 changes: 1 addition & 1 deletion
2
.../weights/tornado/LlamaTornadoWeights.java → ...hts/tornado/fp16/LlamaTornadoWeights.java
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
2 changes: 1 addition & 1 deletion
2
...e/weights/tornado/Phi3TornadoWeights.java → ...ghts/tornado/fp16/Phi3TornadoWeights.java
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
2 changes: 1 addition & 1 deletion
2
.../weights/tornado/Qwen2TornadoWeights.java → ...hts/tornado/fp16/Qwen2TornadoWeights.java
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
2 changes: 1 addition & 1 deletion
2
.../weights/tornado/Qwen3TornadoWeights.java → ...hts/tornado/fp16/Qwen3TornadoWeights.java
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
2 changes: 1 addition & 1 deletion
2
...ights/tornado/Phi3TornadoWeightsQ8_0.java → .../tornado/q8_0/Phi3TornadoWeightsQ8_0.java
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
3 changes: 2 additions & 1 deletion
3
...nference/weights/tornado/Q8_0Weights.java → ...nce/weights/tornado/q8_0/Q8_0Weights.java
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
2 changes: 1 addition & 1 deletion
2
...ghts/tornado/Qwen2TornadoWeightsQ8_0.java → ...tornado/q8_0/Qwen2TornadoWeightsQ8_0.java
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
2 changes: 1 addition & 1 deletion
2
...ghts/tornado/Qwen3Q8_0TornadoWeights.java → ...tornado/q8_0/Qwen3Q8_0TornadoWeights.java
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
172 changes: 172 additions & 0 deletions
172
src/main/java/org/beehive/gpullama3/model/loader/AbstractModelLoader.java
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,172 @@ | ||
| package org.beehive.gpullama3.model.loader; | ||
|
|
||
| import org.beehive.gpullama3.core.model.GGUF; | ||
| import org.beehive.gpullama3.core.model.tensor.GGMLTensorEntry; | ||
| import org.beehive.gpullama3.core.types.Pair; | ||
| import org.beehive.gpullama3.inference.weights.Weights; | ||
| import org.beehive.gpullama3.model.Configuration; | ||
| import org.beehive.gpullama3.model.Model; | ||
| import org.beehive.gpullama3.tokenizer.impl.Tokenizer; | ||
| import org.beehive.gpullama3.tokenizer.vocabulary.Vocabulary; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.channels.FileChannel; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Abstract base class for model loaders using Template Method pattern. Provides common loading flow with extension points for model-specific logic. | ||
| * | ||
| * @param <M> | ||
| * The specific Model type to load | ||
| * @param <C> | ||
| * The specific Configuration type for the model | ||
| */ | ||
| public abstract class AbstractModelLoader<M extends Model, C extends Configuration> { | ||
|
|
||
| protected final FileChannel fileChannel; | ||
| protected final GGUF gguf; | ||
| protected final int contextLength; | ||
| protected final boolean loadWeights; | ||
| protected final boolean useTornadovm; | ||
|
|
||
| protected Vocabulary vocabulary; | ||
|
|
||
| protected AbstractModelLoader(FileChannel fileChannel, GGUF gguf, int contextLength, boolean loadWeights, boolean useTornadovm) { | ||
| this.fileChannel = fileChannel; | ||
| this.gguf = gguf; | ||
| this.contextLength = contextLength; | ||
| this.loadWeights = loadWeights; | ||
| this.useTornadovm = useTornadovm; | ||
| } | ||
|
|
||
| /** | ||
| * Template method that defines the model loading workflow. Subclasses should not override this method. | ||
| * | ||
| * @return The loaded model instance | ||
| */ | ||
| public final M loadModel() { | ||
| try { | ||
| Map<String, Object> metadata = gguf.getMetadata(); | ||
|
|
||
| // Step 1: Load vocabulary | ||
| this.vocabulary = loadVocabulary(metadata); | ||
|
|
||
| // Step 2: Create tokenizer | ||
| Tokenizer tokenizer = createTokenizer(metadata, vocabulary); | ||
|
|
||
| // Step 3: Create configuration | ||
| C config = createConfiguration(metadata); | ||
|
|
||
| // Step 4: Load weights (if requested) | ||
| Weights weights = null; | ||
| if (loadWeights) { | ||
| Map<String, GGMLTensorEntry> tensorEntries = GGUF.loadTensors(fileChannel, gguf.getTensorDataOffset(), gguf.getTensorInfos()); | ||
| weights = loadWeights(tensorEntries, config); | ||
| } | ||
|
|
||
| // Step 5: Create and return model instance | ||
| return createModel(config, tokenizer, weights); | ||
|
|
||
| } catch (IOException e) { | ||
| throw new ModelLoadException("Failed to load model", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Load the vocabulary from GGUF metadata. Model-specific implementations should override this method. | ||
| * | ||
| * @param metadata | ||
| * The GGUF metadata map | ||
| * @return The loaded Vocabulary | ||
| */ | ||
| protected abstract Vocabulary loadVocabulary(Map<String, Object> metadata); | ||
|
|
||
| /** | ||
| * Create a tokenizer instance for this model. | ||
| * | ||
| * @param metadata | ||
| * The GGUF metadata map | ||
| * @param vocabulary | ||
| * The loaded vocabulary | ||
| * @return The tokenizer instance | ||
| */ | ||
| protected abstract Tokenizer createTokenizer(Map<String, Object> metadata, Vocabulary vocabulary); | ||
|
|
||
| /** | ||
| * Create a configuration instance from GGUF metadata. | ||
| * | ||
| * @param metadata | ||
| * The GGUF metadata map | ||
| * @return The configuration instance | ||
| */ | ||
| protected abstract C createConfiguration(Map<String, Object> metadata); | ||
|
|
||
| /** | ||
| * Load model weights from tensor entries. Default implementation handles common weight loading logic. | ||
| * | ||
| * @param tensorEntries | ||
| * Map of tensor names to tensor entries | ||
| * @param config | ||
| * The model configuration | ||
| * @return The loaded weights | ||
| */ | ||
| public Weights loadWeights(Map<String, GGMLTensorEntry> tensorEntries, C config) { | ||
| // Precompute RoPE frequencies | ||
| Pair<float[], float[]> ropeFreqs = precomputeRopeFrequencies(config); | ||
|
|
||
| // Get token embeddings and output weights | ||
| GGMLTensorEntry tokenEmbeddings = getTokenEmbeddings(tensorEntries); | ||
| GGMLTensorEntry outputWeight = getOutputWeight(tensorEntries, tokenEmbeddings); | ||
|
|
||
| // Delegate to specific implementation | ||
| if (useTornadovm) { | ||
| return createTornadoVMWeights(tensorEntries, config, ropeFreqs, tokenEmbeddings, outputWeight); | ||
| } else { | ||
| return createStandardWeights(tensorEntries, config, ropeFreqs, tokenEmbeddings, outputWeight); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create the final model instance. | ||
| * | ||
| * @param config | ||
| * The model configuration | ||
| * @param tokenizer | ||
| * The tokenizer | ||
| * @param weights | ||
| * The loaded weights | ||
| * @return The model instance | ||
| */ | ||
| protected abstract M createModel(C config, Tokenizer tokenizer, Weights weights); | ||
|
|
||
| /** | ||
| * Precompute RoPE frequencies for this model. Default implementation can be overridden for custom RoPE configurations. | ||
| */ | ||
| protected abstract Pair<float[], float[]> precomputeRopeFrequencies(C config); | ||
|
|
||
| /** | ||
| * Get token embeddings tensor entry. Default implementation can be overridden for different tensor naming. | ||
| */ | ||
| protected GGMLTensorEntry getTokenEmbeddings(Map<String, GGMLTensorEntry> tensorEntries) { | ||
| return tensorEntries.get("token_embd.weight"); | ||
| } | ||
|
|
||
| /** | ||
| * Get output weight tensor entry. Default implementation falls back to token embeddings if output.weight not found. | ||
| */ | ||
| protected GGMLTensorEntry getOutputWeight(Map<String, GGMLTensorEntry> tensorEntries, GGMLTensorEntry tokenEmbeddings) { | ||
| return tensorEntries.getOrDefault("output.weight", tokenEmbeddings); | ||
| } | ||
|
|
||
| /** | ||
| * Create standard (CPU) weights. | ||
| */ | ||
| protected abstract Weights createStandardWeights(Map<String, GGMLTensorEntry> tensorEntries, C config, Pair<float[], float[]> ropeFreqs, GGMLTensorEntry tokenEmbeddings, | ||
| GGMLTensorEntry outputWeight); | ||
|
|
||
| /** | ||
| * Create TornadoVM (GPU) weights. | ||
| */ | ||
| protected abstract Weights createTornadoVMWeights(Map<String, GGMLTensorEntry> tensorEntries, C config, Pair<float[], float[]> ropeFreqs, GGMLTensorEntry tokenEmbeddings, | ||
| GGMLTensorEntry outputWeight); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.