File size: 12,861 Bytes
a0098d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Quantization Routes
Core quantization API endpoints
"""

from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
from pydantic import BaseModel
from typing import Optional, Dict, Any, List
import torch
import asyncio
import json

from backend.core.quantizer import (
    QuantizationConfig, QuantizationMethod, QuantizationMode,
    INT8Quantizer, INT4Quantizer, NF4Quantizer, get_quantizer
)
from backend.core.model_loader import model_loader
from backend.core.visualization import visualizer

router = APIRouter()


class QuantizeWeightsRequest(BaseModel):
    """Request to quantize custom weights"""
    in_features: int = 64
    out_features: int = 128
    bits: int = 8  # 4 or 8
    method: str = "int8"  # int8, int4, nf4
    mode: str = "symmetric"  # symmetric, asymmetric
    group_size: Optional[int] = None
    weight_pattern: str = "random"  # random, eye, ones, alternating, gradient
    dtype: str = "float32"


class QuantizeLayerRequest(BaseModel):
    """Request to quantize a specific layer from loaded model"""
    layer_name: str
    bits: int = 8
    method: str = "int8"
    mode: str = "symmetric"
    group_size: Optional[int] = None


class QuantizeModelRequest(BaseModel):
    """Request to quantize entire model"""
    bits: int = 8
    method: str = "int8"
    mode: str = "symmetric"
    group_size: Optional[int] = None
    layers_to_skip: List[str] = []
    layers_to_include: Optional[List[str]] = None  # None = all quantizable


def _generate_weights(pattern: str, out_features: int, in_features: int, 
                      dtype: torch.dtype) -> torch.Tensor:
    """Generate weights based on pattern"""
    if pattern == "random":
        return torch.randn((out_features, in_features), dtype=dtype)
    elif pattern == "eye":
        weights = torch.zeros((out_features, in_features), dtype=dtype)
        min_dim = min(out_features, in_features)
        weights[:min_dim, :min_dim] = torch.eye(min_dim, dtype=dtype)
        return weights
    elif pattern == "ones":
        return torch.ones((out_features, in_features), dtype=dtype)
    elif pattern == "alternating":
        weights = torch.ones((out_features, in_features), dtype=dtype)
        for i in range(out_features):
            for j in range(in_features):
                if (i + j) % 2 == 1:
                    weights[i, j] = -1.0
        return weights
    elif pattern == "gradient":
        x = torch.linspace(-1, 1, in_features)
        y = torch.linspace(-1, 1, out_features)
        xx, yy = torch.meshgrid(x, y, indexing='ij')
        return (xx + yy).t().to(dtype)
    else:
        return torch.randn((out_features, in_features), dtype=dtype)


def _get_quantizer_from_config(request) -> tuple:
    """Get quantizer and config from request parameters"""
    method_map = {
        "int8": QuantizationMethod.INT8,
        "int4": QuantizationMethod.INT4,
        "nf4": QuantizationMethod.NF4
    }
    mode_map = {
        "symmetric": QuantizationMode.SYMMETRIC,
        "asymmetric": QuantizationMode.ASYMMETRIC
    }
    
    config = QuantizationConfig(
        bits=request.bits,
        method=method_map.get(request.method, QuantizationMethod.INT8),
        mode=mode_map.get(request.mode, QuantizationMode.SYMMETRIC),
        group_size=request.group_size
    )
    
    quantizer = get_quantizer(config)
    return quantizer, config


@router.post("/weights")
async def quantize_custom_weights(request: QuantizeWeightsRequest) -> Dict[str, Any]:
    """
    Quantize custom generated weights.
    This endpoint works without loading a real model.
    """
    # Map dtype
    dtype_map = {
        "float32": torch.float32,
        "float16": torch.float16,
        "bfloat16": torch.bfloat16
    }
    dtype = dtype_map.get(request.dtype, torch.float32)
    
    # Generate weights
    weights = _generate_weights(
        request.weight_pattern, 
        request.out_features, 
        request.in_features, 
        dtype
    )
    
    # Get quantizer
    quantizer, config = _get_quantizer_from_config(request)
    
    # Quantize
    result = quantizer.quantize(weights)
    
    # Dequantize for visualization
    dequantized = quantizer.dequantize(result)
    
    # Generate visualizations
    original_heatmap = visualizer.to_dict(
        visualizer.weight_heatmap(weights, "Original Weights")
    )
    quantized_heatmap = visualizer.to_dict(
        visualizer.weight_heatmap(result.quantized_weights.float(), f"Quantized Weights ({request.bits}-bit)")
    )
    dequantized_heatmap = visualizer.to_dict(
        visualizer.weight_heatmap(dequantized, "Dequantized Weights")
    )
    error_heatmap = visualizer.to_dict(
        visualizer.weight_heatmap((weights - dequantized).abs(), "Quantization Error")
    )
    original_hist = visualizer.to_dict(
        visualizer.weight_histogram(weights, "Original Distribution")
    )
    quantized_hist = visualizer.to_dict(
        visualizer.weight_histogram(result.quantized_weights.float(), "Quantized Distribution")
    )
    scales_hist = visualizer.to_dict(
        visualizer.scales_histogram(result.scales)
    )
    
    return {
        "success": True,
        "config": config.to_dict(),
        "stats": {
            "original_shape": list(weights.shape),
            "quantized_shape": list(result.quantized_weights.shape),
            "scales_shape": list(result.scales.shape),
            "max_error": result.max_error,
            "mean_error": result.mean_error,
            "memory_savings_percent": result.memory_savings_percent,
            "original_dtype": str(weights.dtype),
            "quantized_dtype": str(result.quantized_weights.dtype)
        },
        "visualizations": {
            "original_heatmap": original_heatmap,
            "quantized_heatmap": quantized_heatmap,
            "dequantized_heatmap": dequantized_heatmap,
            "error_heatmap": error_heatmap,
            "original_histogram": original_hist,
            "quantized_histogram": quantized_hist,
            "scales_histogram": scales_hist
        }
    }


@router.post("/layer")
async def quantize_layer(request: QuantizeLayerRequest) -> Dict[str, Any]:
    """
    Quantize a specific layer from the loaded model.
    Requires a model to be loaded first.
    """
    if model_loader is None or model_loader.get_model() is None:
        raise HTTPException(
            status_code=400, 
            detail="No model loaded. Load a model first or use /quantize/weights for custom weights."
        )
    
    # Get layer weights
    weights = model_loader.get_layer_weights(request.layer_name)
    if weights is None:
        raise HTTPException(status_code=404, detail=f"Layer not found: {request.layer_name}")
    
    # Ensure 2D
    original_shape = weights.shape
    if len(weights.shape) == 1:
        weights = weights.unsqueeze(0)
    elif len(weights.shape) > 2:
        weights = weights.reshape(weights.shape[0], -1)
    
    # Get quantizer
    quantizer, config = _get_quantizer_from_config(request)
    
    # Quantize
    result = quantizer.quantize(weights)
    dequantized = quantizer.dequantize(result)
    
    # Generate Visualizations
    original_hist = visualizer.to_dict(visualizer.weight_histogram(weights, "Original Distribution"))
    quantized_hist = visualizer.to_dict(visualizer.weight_histogram(result.quantized_weights.float(), "Quantized Distribution"))
    scales_hist = visualizer.to_dict(visualizer.scales_histogram(result.scales))

    return {
        "success": True,
        "layer_name": request.layer_name,
        "config": config.to_dict(),
        "stats": {
            "original_shape": list(original_shape),
            "quantized_shape": list(result.quantized_weights.shape),
            "scales_shape": list(result.scales.shape),
            "max_error": result.max_error,
            "mean_error": result.mean_error,
            "memory_savings_percent": result.memory_savings_percent,
            "original_dtype": str(weights.dtype),
            "quantized_dtype": str(result.quantized_weights.dtype)
        },
        "visualizations": {
            "original_heatmap": visualizer.to_dict(
                visualizer.weight_heatmap(weights, f"Original: {request.layer_name}")
            ),
            "quantized_heatmap": visualizer.to_dict(
                visualizer.weight_heatmap(result.quantized_weights.float(), f"Quantized ({request.bits}-bit)")
            ),
            "dequantized_heatmap": visualizer.to_dict(
                visualizer.weight_heatmap(dequantized, "Dequantized Weights")
            ),
            "error_heatmap": visualizer.to_dict(
                visualizer.weight_heatmap((weights - dequantized).abs(), "Error")
            ),
            "original_histogram": original_hist,
            "quantized_histogram": quantized_hist,
            "scales_histogram": scales_hist
        }
    }


@router.post("/model")
async def quantize_model(request: QuantizeModelRequest) -> Dict[str, Any]:
    """
    Quantize all quantizable layers in the loaded model.
    Returns summary statistics for all layers.
    """
    if model_loader is None or model_loader.get_model() is None:
        raise HTTPException(
            status_code=400,
            detail="No model loaded. This feature requires a loaded model."
        )
    
    model_info = model_loader.get_model_info()
    if model_info is None:
        raise HTTPException(status_code=500, detail="Failed to get model info")
    
    # Determine layers to quantize
    if request.layers_to_include:
        layers_to_quantize = request.layers_to_include
    else:
        layers_to_quantize = model_info.quantizable_layers
    
    # Remove skipped layers
    layers_to_quantize = [l for l in layers_to_quantize if l not in request.layers_to_skip]
    
    # Get quantizer
    quantizer, config = _get_quantizer_from_config(request)
    
    # Quantize each layer
    results = []
    total_memory_saved = 0
    total_original_size = 0
    
    for layer_name in layers_to_quantize:
        weights = model_loader.get_layer_weights(layer_name)
        if weights is None:
            continue
        
        # Handle non-2D weights
        original_shape = weights.shape
        if len(weights.shape) == 1:
            weights = weights.unsqueeze(0)
        elif len(weights.shape) > 2:
            weights = weights.reshape(weights.shape[0], -1)
        
        try:
            result = quantizer.quantize(weights)
            
            original_bytes = weights.numel() * weights.element_size()
            total_original_size += original_bytes
            total_memory_saved += original_bytes * (result.memory_savings_percent / 100)
            
            results.append({
                "layer": layer_name,
                "shape": list(original_shape),
                "max_error": result.max_error,
                "mean_error": result.mean_error,
                "memory_savings_percent": result.memory_savings_percent
            })
        except Exception as e:
            results.append({
                "layer": layer_name,
                "error": str(e)
            })
    
    return {
        "success": True,
        "config": config.to_dict(),
        "summary": {
            "layers_quantized": len([r for r in results if "error" not in r]),
            "layers_failed": len([r for r in results if "error" in r]),
            "total_memory_saved_mb": total_memory_saved / (1024 * 1024),
            "average_memory_savings_percent": (total_memory_saved / total_original_size * 100) if total_original_size > 0 else 0
        },
        "layers": results
    }


# WebSocket for real-time progress
@router.websocket("/stream")
async def quantization_stream(websocket: WebSocket):
    """WebSocket endpoint for streaming quantization progress"""
    await websocket.accept()
    
    try:
        while True:
            # Receive quantization request
            data = await websocket.receive_text()
            request_data = json.loads(data)
            
            # Process and send updates
            await websocket.send_json({
                "type": "progress",
                "progress": 0,
                "message": "Starting quantization..."
            })
            
            # Simulate progress (in real implementation, this would be actual quantization)
            for i in range(0, 101, 10):
                await asyncio.sleep(0.1)
                await websocket.send_json({
                    "type": "progress", 
                    "progress": i,
                    "message": f"Processing... {i}%"
                })
            
            await websocket.send_json({
                "type": "complete",
                "message": "Quantization complete"
            })
            
    except WebSocketDisconnect:
        pass