1616/**
1717 * Abstract base class for model loaders using Template Method pattern. Provides common loading flow with extension points for model-specific logic.
1818 *
19- * @param <M>
20- * The specific Model type to load
21- * @param <C>
22- * The specific Configuration type for the model
19+ * @param <M> The specific Model type to load
20+ * @param <C> The specific Configuration type for the model
2321 */
2422public abstract class AbstractModelLoader <M extends Model , C extends Configuration > {
2523
2624 protected final FileChannel fileChannel ;
2725 protected final GGUF gguf ;
2826 protected final int contextLength ;
29- protected final boolean loadWeights ;
3027 protected final boolean useTornadovm ;
3128
3229 protected Vocabulary vocabulary ;
3330
34- protected AbstractModelLoader (FileChannel fileChannel , GGUF gguf , int contextLength , boolean loadWeights , boolean useTornadovm ) {
31+ protected AbstractModelLoader (FileChannel fileChannel , GGUF gguf , int contextLength , boolean useTornadovm ) {
3532 this .fileChannel = fileChannel ;
3633 this .gguf = gguf ;
3734 this .contextLength = contextLength ;
38- this .loadWeights = loadWeights ;
3935 this .useTornadovm = useTornadovm ;
4036 }
4137
@@ -57,13 +53,17 @@ public final M loadModel() {
5753 // Step 3: Create configuration
5854 C config = createConfiguration (metadata );
5955
60- // Step 4: Load weights (if requested)
61- Weights weights = null ;
62- if (loadWeights ) {
63- Map <String , GGMLTensorEntry > tensorEntries = GGUF .loadTensors (fileChannel , gguf .getTensorDataOffset (), gguf .getTensorInfos ());
64- weights = loadWeights (tensorEntries , config );
56+ // Step 4: Load tensor entries
57+ Map <String , GGMLTensorEntry > tensorEntries ;
58+ if (useTornadovm ) {
59+ tensorEntries = GGUF .loadTensorsTornado (fileChannel , gguf .getTensorDataOffset (), gguf .getTensorInfos ());
60+ } else {
61+ tensorEntries = GGUF .loadTensorsStandard (fileChannel , gguf .getTensorDataOffset (), gguf .getTensorInfos ());
6562 }
6663
64+ // Step 4: Load weights
65+ Weights weights = loadWeights (tensorEntries , config );
66+
6767 // Step 5: Create and return model instance
6868 return createModel (config , tokenizer , weights );
6969
@@ -75,39 +75,33 @@ public final M loadModel() {
7575 /**
7676 * Load the vocabulary from GGUF metadata. Model-specific implementations should override this method.
7777 *
78- * @param metadata
79- * The GGUF metadata map
78+ * @param metadata The GGUF metadata map
8079 * @return The loaded Vocabulary
8180 */
8281 protected abstract Vocabulary loadVocabulary (Map <String , Object > metadata );
8382
8483 /**
8584 * Create a tokenizer instance for this model.
8685 *
87- * @param metadata
88- * The GGUF metadata map
89- * @param vocabulary
90- * The loaded vocabulary
86+ * @param metadata The GGUF metadata map
87+ * @param vocabulary The loaded vocabulary
9188 * @return The tokenizer instance
9289 */
9390 protected abstract Tokenizer createTokenizer (Map <String , Object > metadata , Vocabulary vocabulary );
9491
9592 /**
9693 * Create a configuration instance from GGUF metadata.
9794 *
98- * @param metadata
99- * The GGUF metadata map
95+ * @param metadata The GGUF metadata map
10096 * @return The configuration instance
10197 */
10298 protected abstract C createConfiguration (Map <String , Object > metadata );
10399
104100 /**
105101 * Load model weights from tensor entries. Default implementation handles common weight loading logic.
106102 *
107- * @param tensorEntries
108- * Map of tensor names to tensor entries
109- * @param config
110- * The model configuration
103+ * @param tensorEntries Map of tensor names to tensor entries
104+ * @param config The model configuration
111105 * @return The loaded weights
112106 */
113107 public Weights loadWeights (Map <String , GGMLTensorEntry > tensorEntries , C config ) {
@@ -129,12 +123,9 @@ public Weights loadWeights(Map<String, GGMLTensorEntry> tensorEntries, C config)
129123 /**
130124 * Create the final model instance.
131125 *
132- * @param config
133- * The model configuration
134- * @param tokenizer
135- * The tokenizer
136- * @param weights
137- * The loaded weights
126+ * @param config The model configuration
127+ * @param tokenizer The tokenizer
128+ * @param weights The loaded weights
138129 * @return The model instance
139130 */
140131 protected abstract M createModel (C config , Tokenizer tokenizer , Weights weights );
@@ -161,12 +152,10 @@ protected GGMLTensorEntry getOutputWeight(Map<String, GGMLTensorEntry> tensorEnt
161152 /**
162153 * Create standard (CPU) weights.
163154 */
164- protected abstract Weights createStandardWeights (Map <String , GGMLTensorEntry > tensorEntries , C config , Pair <float [], float []> ropeFreqs , GGMLTensorEntry tokenEmbeddings ,
165- GGMLTensorEntry outputWeight );
155+ protected abstract Weights createStandardWeights (Map <String , GGMLTensorEntry > tensorEntries , C config , Pair <float [], float []> ropeFreqs , GGMLTensorEntry tokenEmbeddings , GGMLTensorEntry outputWeight );
166156
167157 /**
168158 * Create TornadoVM (GPU) weights.
169159 */
170- protected abstract Weights createTornadoVMWeights (Map <String , GGMLTensorEntry > tensorEntries , C config , Pair <float [], float []> ropeFreqs , GGMLTensorEntry tokenEmbeddings ,
171- GGMLTensorEntry outputWeight );
160+ protected abstract Weights createTornadoVMWeights (Map <String , GGMLTensorEntry > tensorEntries , C config , Pair <float [], float []> ropeFreqs , GGMLTensorEntry tokenEmbeddings , GGMLTensorEntry outputWeight );
172161}
0 commit comments