-
Notifications
You must be signed in to change notification settings - Fork 23
Add Q4_0 quantization support for all models in TornadoVM path #67
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | ||||||||
| package org.beehive.gpullama3.tensor.tornado; | ||||||||
|
|
||||||||
| import org.beehive.gpullama3.tensor.GGMLTensorEntry; | ||||||||
| import org.beehive.gpullama3.tensor.GGMLType; | ||||||||
| import org.beehive.gpullama3.tensor.standard.FloatTensor; | ||||||||
| import uk.ac.manchester.tornado.api.types.HalfFloat; | ||||||||
| import uk.ac.manchester.tornado.api.types.arrays.HalfFloatArray; | ||||||||
| import uk.ac.manchester.tornado.api.types.arrays.ByteArray; | ||||||||
|
|
||||||||
| import java.lang.foreign.MemorySegment; | ||||||||
| import java.lang.foreign.ValueLayout; | ||||||||
| import java.nio.ByteOrder; | ||||||||
|
|
||||||||
| public class Q4_0TornadoTensor extends TornadoTensor { | ||||||||
|
|
||||||||
| private final HalfFloatArray scales; // One per 32-element block | ||||||||
| private final ByteArray quants; // Packed 4-bit quantized values (2 per byte) | ||||||||
| private MemorySegment segment; | ||||||||
|
|
||||||||
| public Q4_0TornadoTensor(int size, HalfFloatArray scales, ByteArray quants, MemorySegment segment) { | ||||||||
| super(size); | ||||||||
| this.scales = scales; | ||||||||
| this.quants = quants; | ||||||||
| this.segment = segment; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns the scale factors for GPU kernels. | ||||||||
| * | ||||||||
| * @return HalfFloatArray containing fp16 scale factors | ||||||||
| */ | ||||||||
| public HalfFloatArray getScales() { | ||||||||
| return scales; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns the quantized values for GPU kernels. | ||||||||
| * | ||||||||
| * @return ByteArray containing packed 4-bit quantized values | ||||||||
| */ | ||||||||
|
||||||||
| */ | |
| */ | |
| @Override |
Copilot
AI
Nov 13, 2025
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.
Potential overflow in int multiplication before it is converted to long by use in an assignment context.
| long blockOffset = block * GGMLType.Q4_0.getTypeSize(); // 18 bytes per block | |
| long blockOffset = ((long) block) * GGMLType.Q4_0.getTypeSize(); // 18 bytes per block |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.beehive.gpullama3.tornadovm.layerplanner.model.q4_0; | ||
|
|
||
| import org.beehive.gpullama3.inference.state.LlamaState; | ||
| import org.beehive.gpullama3.inference.weights.tornado.LlamaTornadoWeights; | ||
| import org.beehive.gpullama3.model.Model; | ||
| import org.beehive.gpullama3.model.llama.LlamaConfiguration; | ||
| import org.beehive.gpullama3.tornadovm.layerplanner.quantization.Q4_0LayerPlanner; | ||
| import org.beehive.gpullama3.tornadovm.layers.Activation; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.LlamaQ4_0FFNLayers; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.LogitsQ4_0Layer; | ||
|
|
||
| public class LlamaQ4_0LayerPlanner extends Q4_0LayerPlanner<LlamaState, LlamaConfiguration, LlamaTornadoWeights> { | ||
|
|
||
| public LlamaQ4_0LayerPlanner(LlamaState state, Model model) { | ||
| super(state, model); | ||
| validateQuantizationType(); | ||
| setupTornadoForwardPlan(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void initializeLayerComponents() { | ||
| this.activationLayer = new Activation("activationUpdate", this.state, this.weights, this.config); | ||
| this.ffnLayers = new LlamaQ4_0FFNLayers("llamaFFN", this.state, this.weights, this.config, this.schedulerType); | ||
| this.logitsLayer = new LogitsQ4_0Layer("llamaLogits", this.state, this.weights, this.config, ffnLayers.getLastTaskGraphID(), this.schedulerType); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.beehive.gpullama3.tornadovm.layerplanner.model.q4_0; | ||
|
|
||
| import org.beehive.gpullama3.inference.state.Phi3State; | ||
| import org.beehive.gpullama3.inference.weights.tornado.Phi3TornadoWeights; | ||
| import org.beehive.gpullama3.model.Model; | ||
| import org.beehive.gpullama3.model.phi3.Phi3Configuration; | ||
| import org.beehive.gpullama3.tornadovm.layerplanner.quantization.Q4_0LayerPlanner; | ||
| import org.beehive.gpullama3.tornadovm.layers.Activation; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.Phi3Q4_0FFNLayers; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.LogitsQ4_0Layer; | ||
|
|
||
| /** | ||
| * Phi3Q4_0LayerPlanner: Phi3 model with Q4_0-quantized weights. | ||
| * | ||
| * Follows the same pattern as Qwen3Q4_0LayerPlanner but with: | ||
| * - Phi3-specific FFN layers (combined QKV + gate/up FFN) | ||
| * - Phi3TornadoWeights (4-bit integer quantization) | ||
| * - Phi3Configuration | ||
| * - 4x memory compression vs FP16, 2x vs Q8_0 | ||
| * | ||
| * Inherits from Q4_0LayerPlanner<Phi3State, Phi3Configuration, Phi3TornadoWeights> | ||
| */ | ||
| public class Phi3Q4_0LayerPlanner extends Q4_0LayerPlanner<Phi3State, Phi3Configuration, Phi3TornadoWeights> { | ||
|
|
||
| public Phi3Q4_0LayerPlanner(Phi3State state, Model model) { | ||
| super(state, model); | ||
| validateQuantizationType(); | ||
| setupTornadoForwardPlan(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void initializeLayerComponents() { | ||
| this.activationLayer = new Activation("activationUpdate", this.state, this.weights, this.config); | ||
| this.ffnLayers = new Phi3Q4_0FFNLayers("phi3FFN", this.state, this.weights, this.config, this.schedulerType); | ||
| this.logitsLayer = new LogitsQ4_0Layer("phi3Logits", this.state, this.weights, this.config, ffnLayers.getLastTaskGraphID(), this.schedulerType); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.beehive.gpullama3.tornadovm.layerplanner.model.q4_0; | ||
|
|
||
| import org.beehive.gpullama3.inference.state.Qwen2State; | ||
| import org.beehive.gpullama3.inference.weights.tornado.Qwen2TornadoWeights; | ||
| import org.beehive.gpullama3.model.Model; | ||
| import org.beehive.gpullama3.model.qwen2.Qwen2Configuration; | ||
| import org.beehive.gpullama3.tornadovm.layerplanner.quantization.Q4_0LayerPlanner; | ||
| import org.beehive.gpullama3.tornadovm.layers.Activation; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.Qwen2Q4_0FFNLayers; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.LogitsQ4_0Layer; | ||
|
|
||
| public class Qwen2Q4_0LayerPlanner extends Q4_0LayerPlanner<Qwen2State, Qwen2Configuration, Qwen2TornadoWeights> { | ||
|
|
||
| public Qwen2Q4_0LayerPlanner(Qwen2State state, Model model) { | ||
| super(state, model); | ||
| validateQuantizationType(); | ||
| setupTornadoForwardPlan(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void initializeLayerComponents() { | ||
| this.activationLayer = new Activation("activationUpdate", this.state, this.weights, this.config); | ||
| this.ffnLayers = new Qwen2Q4_0FFNLayers("qwen2FFN", this.state, this.weights, this.config, this.schedulerType); | ||
| this.logitsLayer = new LogitsQ4_0Layer("qwen2Logits", this.state, this.weights, this.config, ffnLayers.getLastTaskGraphID(), this.schedulerType); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.beehive.gpullama3.tornadovm.layerplanner.model.q4_0; | ||
|
|
||
| import org.beehive.gpullama3.inference.state.Qwen3State; | ||
| import org.beehive.gpullama3.inference.weights.tornado.Qwen3TornadoWeights; | ||
| import org.beehive.gpullama3.model.Model; | ||
| import org.beehive.gpullama3.model.qwen3.Qwen3Configuration; | ||
| import org.beehive.gpullama3.tornadovm.layerplanner.quantization.Q4_0LayerPlanner; | ||
| import org.beehive.gpullama3.tornadovm.layers.Activation; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.Qwen3Q4_0FFNLayers; | ||
| import org.beehive.gpullama3.tornadovm.layers.type.q4_0.LogitsQ4_0Layer; | ||
|
|
||
| /** | ||
| * Qwen3Q4_0LayerPlanner: Qwen3 model with Q4_0-quantized weights. | ||
| * | ||
| * Follows the same pattern as LlamaQ4_0LayerPlanner but with: | ||
| * - Qwen3-specific FFN layers (supports GQA) | ||
| * - Qwen3TornadoWeights (4-bit integer quantization) | ||
| * - Qwen3Configuration | ||
| * - 4x memory compression vs FP16, 2x vs Q8_0 | ||
| * | ||
| * Inherits from Q4_0LayerPlanner<Qwen3State, Qwen3Configuration, Qwen3TornadoWeights> | ||
| */ | ||
| public class Qwen3Q4_0LayerPlanner extends Q4_0LayerPlanner<Qwen3State, Qwen3Configuration, Qwen3TornadoWeights> { | ||
|
|
||
| public Qwen3Q4_0LayerPlanner(Qwen3State state, Model model) { | ||
| super(state, model); | ||
| validateQuantizationType(); | ||
| setupTornadoForwardPlan(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void initializeLayerComponents() { | ||
| this.activationLayer = new Activation("activationUpdate", this.state, this.weights, this.config); | ||
| this.ffnLayers = new Qwen3Q4_0FFNLayers("qwen3FFN", this.state, this.weights, this.config, this.schedulerType); | ||
| this.logitsLayer = new LogitsQ4_0Layer("qwen3Logits", this.state, this.weights, this.config, ffnLayers.getLastTaskGraphID(),this.schedulerType); | ||
| } | ||
| } |
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.
This method overrides TornadoTensor.getScales; it is advisable to add an Override annotation.