|
| 1 | +package org.beehive.gpullama3.tornadovm.layerplanner.model.q8_0; |
| 2 | + |
| 3 | +import org.beehive.gpullama3.auxiliary.Tuple2; |
| 4 | +import org.beehive.gpullama3.inference.state.Phi3State; |
| 5 | +import org.beehive.gpullama3.inference.weights.tornado.Q8_0Weights.Phi3TornadoWeightsQ8_0; |
| 6 | +import org.beehive.gpullama3.model.Model; |
| 7 | +import org.beehive.gpullama3.model.phi3.Phi3Configuration; |
| 8 | +import org.beehive.gpullama3.tornadovm.layerplanner.quantization.Q8_0LayerPlanner; |
| 9 | +import org.beehive.gpullama3.tornadovm.layers.Activation; |
| 10 | +import org.beehive.gpullama3.tornadovm.layers.type.q8_0.LogitsQ8_0Layer; |
| 11 | +import org.beehive.gpullama3.tornadovm.layers.type.q8_0.Phi3Q8_0FFNLayers; |
| 12 | +import uk.ac.manchester.tornado.api.GridScheduler; |
| 13 | +import uk.ac.manchester.tornado.api.ImmutableTaskGraph; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * Phi3Q8_0LayerPlanner: Phi3 model with Q8_0-quantized weights. |
| 20 | + * |
| 21 | + * Follows the same pattern as Qwen3Q8_0LayerPlanner but with: |
| 22 | + * - Phi3-specific FFN layers (combined QKV + gate/up FFN) |
| 23 | + * - Phi3TornadoWeightsQ8_0 (8-bit integer quantization) |
| 24 | + * - Phi3Configuration |
| 25 | + * - 2x memory compression vs FP16 |
| 26 | + * |
| 27 | + * Inherits from Q8_0LayerPlanner<Phi3State, Phi3Configuration, Phi3TornadoWeightsQ8_0> |
| 28 | + */ |
| 29 | +public class Phi3Q8_0LayerPlanner extends Q8_0LayerPlanner<Phi3State, Phi3Configuration, Phi3TornadoWeightsQ8_0> { |
| 30 | + |
| 31 | + private Activation activationLayer; |
| 32 | + private Phi3Q8_0FFNLayers ffnLayers; |
| 33 | + private LogitsQ8_0Layer logitsLayer; |
| 34 | + |
| 35 | + // Cache |
| 36 | + private List<ImmutableTaskGraph> cachedTaskGraphs; |
| 37 | + private GridScheduler cachedScheduler; |
| 38 | + |
| 39 | + public Phi3Q8_0LayerPlanner(Phi3State state, Model model) { |
| 40 | + super(state, model); |
| 41 | + validateQuantizationType(); |
| 42 | + setupTornadoForwardPlan(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void initializeLayerComponents() { |
| 47 | + this.activationLayer = new Activation("activationUpdate", this.state, this.weights, this.config); |
| 48 | + |
| 49 | + this.ffnLayers = new Phi3Q8_0FFNLayers("phi3FFN", this.state, this.weights, this.config); |
| 50 | + |
| 51 | + this.logitsLayer = new LogitsQ8_0Layer("phi3Logits", this.state, this.weights, this.config, |
| 52 | + ffnLayers.getLastTaskGraphID()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Tuple2<List<ImmutableTaskGraph>, GridScheduler> setupTornadoForwardPlanLayered() { |
| 57 | + if (this.cachedTaskGraphs != null && this.cachedScheduler != null) { |
| 58 | + return new Tuple2<>(this.cachedTaskGraphs, this.cachedScheduler); |
| 59 | + } |
| 60 | + |
| 61 | + List<ImmutableTaskGraph> allTaskGraphs = new ArrayList<>(); |
| 62 | + GridScheduler masterScheduler = new GridScheduler(); |
| 63 | + |
| 64 | + // 1. Activation layer |
| 65 | + allTaskGraphs.add(activationLayer.getImmutableTaskGraph()); |
| 66 | + activationLayer.updateGridScheduler(masterScheduler); |
| 67 | + |
| 68 | + // 2. FFN layers (N transformer layers with Q8_0 quantization) |
| 69 | + allTaskGraphs.addAll(ffnLayers.getFfnLayerTaskGraphs()); |
| 70 | + ffnLayers.updateGridScheduler(masterScheduler); |
| 71 | + |
| 72 | + // 3. Logits layer |
| 73 | + allTaskGraphs.add(logitsLayer.getTaskGraph().snapshot()); |
| 74 | + logitsLayer.updateGridScheduler(masterScheduler); |
| 75 | + |
| 76 | + // Cache |
| 77 | + this.cachedTaskGraphs = allTaskGraphs; |
| 78 | + this.cachedScheduler = masterScheduler; |
| 79 | + |
| 80 | + return new Tuple2<>(allTaskGraphs, masterScheduler); |
| 81 | + } |
| 82 | + |
| 83 | + public void setupTornadoForwardPlan() { |
| 84 | + List<ImmutableTaskGraph> allTaskGraphs = new ArrayList<>(); |
| 85 | + GridScheduler masterScheduler = new GridScheduler(); |
| 86 | + |
| 87 | + // 1. Activation layer |
| 88 | + allTaskGraphs.add(activationLayer.getImmutableTaskGraph()); |
| 89 | + activationLayer.updateGridScheduler(masterScheduler); |
| 90 | + |
| 91 | + // 2. FFN layers (N transformer layers with Q8_0 quantization) |
| 92 | + allTaskGraphs.addAll(ffnLayers.getFfnLayerTaskGraphs()); |
| 93 | + ffnLayers.updateGridScheduler(masterScheduler); |
| 94 | + |
| 95 | + // 3. Logits layer |
| 96 | + allTaskGraphs.add(logitsLayer.getTaskGraph().snapshot()); |
| 97 | + logitsLayer.updateGridScheduler(masterScheduler); |
| 98 | + |
| 99 | + // Cache |
| 100 | + this.cachedTaskGraphs = allTaskGraphs; |
| 101 | + this.cachedScheduler = masterScheduler; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Tuple2<List<ImmutableTaskGraph>, GridScheduler> setupTornadoForwardPlanLayeredNonNvidia() { |
| 106 | + // For now, same as NVIDIA version |
| 107 | + // Hardware strategy will optimize scheduler |
| 108 | + return setupTornadoForwardPlanLayered(); |
| 109 | + } |
| 110 | + |
| 111 | + public List<ImmutableTaskGraph> getCachedTaskGraphs() { |
| 112 | + return this.cachedTaskGraphs; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public GridScheduler getCachedGridScheduler() { |
| 117 | + return this.cachedScheduler; |
| 118 | + } |
| 119 | + |
| 120 | + public void clearCache() { |
| 121 | + this.cachedTaskGraphs = null; |
| 122 | + this.cachedScheduler = null; |
| 123 | + } |
| 124 | +} |
0 commit comments