Check whether a model uses 4-bit quantization by inspecting its config.json. Detects both standard GPTQ (`quant_method: "gptq"`) and affine 4-bit (`mode: "affine"`, `bits: 4`) used by some quantized models.
(config_path: &Path)
| 31 | /// Detects both standard GPTQ (`quant_method: "gptq"`) and affine 4-bit |
| 32 | /// (`mode: "affine"`, `bits: 4`) used by some quantized models. |
| 33 | pub fn is_gptq_quantized(config_path: &Path) -> bool { |
| 34 | let Ok(data) = std::fs::read_to_string(config_path) else { |
| 35 | return false; |
| 36 | }; |
| 37 | let Ok(json) = serde_json::from_str::<serde_json::Value>(&data) else { |
| 38 | return false; |
| 39 | }; |
| 40 | // Check top-level and nested text_config for quantization_config |
| 41 | for root in [&json, json.get("text_config").unwrap_or(&json)] { |
| 42 | if let Some(qc) = root.get("quantization_config") { |
| 43 | // Standard GPTQ: quant_method == "gptq" |
| 44 | let is_gptq = qc.get("quant_method") |
| 45 | .and_then(|qm| qm.as_str()) |
| 46 | .map(|s| s == "gptq") |
| 47 | .unwrap_or(false); |
| 48 | if is_gptq { |
| 49 | return true; |
| 50 | } |
| 51 | // Affine 4-bit: mode == "affine" && bits == 4 |
| 52 | let is_affine_4bit = qc.get("mode") |
| 53 | .and_then(|m| m.as_str()) |
| 54 | .map(|s| s == "affine") |
| 55 | .unwrap_or(false) |
| 56 | && qc.get("bits") |
| 57 | .and_then(|b| b.as_u64()) |
| 58 | .map(|b| b == 4) |
| 59 | .unwrap_or(false); |
| 60 | if is_affine_4bit { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | false |
| 66 | } |
| 67 | |
| 68 | /// Read the GPTQ group_size from config.json (defaults to 128). |
| 69 | pub fn gptq_group_size(config_path: &Path) -> usize { |
no test coverage detected