Instructions to use MircoFox-AI/MeLlamFox with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MircoFox-AI/MeLlamFox with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("MircoFox-AI/MeLlamFox", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create src/lib.rs
Browse files- src/lib.rs +72 -0
src/lib.rs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use pyo3::prelude::*;
|
| 2 |
+
use pyo3::exceptions::PyRuntimeError;
|
| 3 |
+
use candle_core::{Tensor, Device};
|
| 4 |
+
use std::collections::HashMap;
|
| 5 |
+
use std::fs::File;
|
| 6 |
+
use std::path::Path;
|
| 7 |
+
|
| 8 |
+
/// Helper function to perform raw tensor manipulation/quantization
|
| 9 |
+
fn perform_quantization(input_path: &str, output_path: &str, precision: &str) -> Result<(), String> {
|
| 10 |
+
let device = Device::Cpu; // Can switch to Device::new_cuda(0) for GPU
|
| 11 |
+
|
| 12 |
+
// 1. Read raw Llama 3.2 safetensors from the input directory
|
| 13 |
+
let content = std::fs::read(input_path)
|
| 14 |
+
.map_err(|e| format!("Failed to read input safetensor file: {}", e))?;
|
| 15 |
+
|
| 16 |
+
let safetensor = safetensors::SafeTensors::deserialize(&content)
|
| 17 |
+
.map_err(|e| format!("Invalid Safetensor format: {}", e))?;
|
| 18 |
+
|
| 19 |
+
let mut new_tensors = HashMap::new();
|
| 20 |
+
|
| 21 |
+
// 2. Iterate through layers (Weights, Attention blocks, Tensors)
|
| 22 |
+
for (name, tensor_view) in safetensor.tensors() {
|
| 23 |
+
let candle_tensor = candle_core::safetensors::load_buffer(tensor_view, &device)
|
| 24 |
+
.map_err(|e| format!("Candle failed to parse tensor {}: {}", name, e))?;
|
| 25 |
+
|
| 26 |
+
// 3. Low-level optimization based on UI input
|
| 27 |
+
let optimized_tensor = match precision {
|
| 28 |
+
"F16" => candle_tensor.to_dtype(candle_core::DType::F16)
|
| 29 |
+
.map_err(|e| e.to_string())?,
|
| 30 |
+
"BF16" => candle_tensor.to_dtype(candle_core::DType::BF16)
|
| 31 |
+
.map_err(|e| e.to_string())?,
|
| 32 |
+
_ => candle_tensor // Keep original if layout matches
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
new_tensors.insert(name, optimized_tensor);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// 4. Safely serialize and stream the weights back to disk
|
| 39 |
+
let output_file = Path::new(output_path);
|
| 40 |
+
candle_core::safetensors::save(&new_tensors, output_file)
|
| 41 |
+
.map_err(|e| format!("Failed to write optimized Llama 3.2 model: {}", e))?;
|
| 42 |
+
|
| 43 |
+
Ok(())
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/// This is the actual function exposed directly to Python
|
| 47 |
+
#[pyfunction]
|
| 48 |
+
fn compress_safetensors(repo_id: String, precision: String) -> PyResult<bool> {
|
| 49 |
+
// Fake directory path routing for local example layout
|
| 50 |
+
let input_file = format!("./models/{}/model.safetensors", repo_id);
|
| 51 |
+
let output_file = format!("./models/{}/model_{}.safetensors", repo_id, precision.to_lowercase());
|
| 52 |
+
|
| 53 |
+
println!("[Rust Engine] Quantizing Llama 3.2 weights for: {}", repo_id);
|
| 54 |
+
|
| 55 |
+
match perform_quantization(&input_file, &output_file, &precision) {
|
| 56 |
+
Ok(_) => {
|
| 57 |
+
println!("[Rust Engine] Success. Optimized file written to {}", output_file);
|
| 58 |
+
Ok(true)
|
| 59 |
+
},
|
| 60 |
+
Err(err_msg) => {
|
| 61 |
+
// Converts native Rust system errors into explicit Python exceptions
|
| 62 |
+
Err(PyRuntimeError::new_err(err_msg))
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/// Defines the Python library entry point structures
|
| 68 |
+
#[pymodule]
|
| 69 |
+
fn llama32_rust_core(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
| 70 |
+
m.add_function(wrap_pyfunction!(compress_safetensors, m)?)?;
|
| 71 |
+
Ok(())
|
| 72 |
+
}
|