candle भारी Python ML रनटाइम्स का शुद्ध Rust विकल्प है
PyTorch inference सेवा को candle पर ले जाना: tensor API साथ-साथ, weights को safetensors में export करना, CPU, CUDA और WASM backends, और candle में अब भी क्या कमी है।

क्या candle production में models चलाने के लिए PyTorch की जगह ले सकता है?
inference के लिए, अक्सर हाँ। candle Hugging Face का minimalist Rust ML framework है, जिसे serverless inference को व्यावहारिक बनाने और production से Python हटाने के लिए बनाया गया है। यह safetensors, npz, ggml और PyTorch weight files load करता है, CPU, CUDA पर या WASM के ज़रिए browser में चलता है, और LLaMA, Whisper, Stable Diffusion, YOLO आदि के examples देता है।
Related reading
candle: Mind क्रेट्स के नीचे Hugging Face का टेंसर रनटाइमcandle, Hugging Face का न्यूनतम Rust ML फ्रेमवर्क है। Remade with Rust इसका फोर्क रखता है ताकि argus, Diana और FFAI क्रेट्स एक ही टेंसर रनटाइम साझा करें और प्रोडक्शन में Python न रहे।
एक सामान्य Python inference सेवा interpreter, PyTorch, native wheels का ढेर और एक web framework के साथ आती है, फिर requests को parallel में serve करने के लिए global interpreter lock से जूझती है। Rust के लिए Hugging Face का minimalist ML framework candle, उस काम के लिए भारी Python ML runtimes का शुद्ध Rust विकल्प है: एक tensor library और model collection जो एक ही binary में compile होती है, CPU, CUDA पर या browser में चलती है, और आपके पास पहले से मौजूद weight files पढ़ती है। Remade with Rust ने candle नहीं बनाया; यह एक fork रखता है ताकि उसके अपने crates एक runtime pin कर सकें। यह पेज एक inference workload को इधर ले जाने के बारे में है। candle फ़ील्ड नोट बताती है कि fork क्यों मौजूद है।
inference के समय Python runtime की लागत
आकार और cold start
candle के maintainers अपना मुख्य लक्ष्य serverless inference को संभव बनाना बताते हैं। PyTorch जैसे पूरे frameworks बड़े होते हैं, जिससे नए instances शुरू होने में धीमे रहते हैं, और edge device या छोटा container हर gigabyte की कीमत चुकाता है। candle binary सिर्फ़ वही operations और model code ले जाती है जो वह इस्तेमाल करती है, इसलिए deployments छोटे होते हैं और CPU पर जल्दी शुरू होते हैं।
Concurrency और overhead
Python hot path पर हर call में overhead जोड़ता है, और global interpreter lock सीमित करता है कि एक process parallel में कितना कर सकता है, इसीलिए Python सेवाएँ अक्सर कई worker processes चलाती हैं, जिनमें हर एक weights की अपनी copy रखता है। एक Rust server एक loaded model को threads के बीच साझा कर सकता है।
device पर supply chain
Python environment में हर native wheel एक और build है जिस पर भरोसा करना और जिसे patch करना पड़ता है। किसी के घर के hardware पर चलने वाले models के लिए कम moving parts ही मक़सद है। candle का core और CPU backend Rust हैं, और यह safetensors पढ़ता है, ऐसा format जो pickled checkpoints के विपरीत tensors को code चलाए बिना load करने के लिए बनाया गया है।
candle क्या देता है, और इसकी सीमाएँ
Backends और formats
CPU backend optimised है, x86 पर optional MKL और macOS पर Accelerate के साथ। CUDA backend GPUs पर चलता है, कई GPUs के लिए NCCL और optional cuDNN के साथ। WASM support models को browser में चलाता है। Models safetensors, npz, ggml या PyTorch files से load होते हैं, और quantization llama.cpp के quantized types इस्तेमाल करती है। Workspace कई हिस्सों में बँटा है: tensors और devices के लिए candle-core, models बनाने के लिए candle-nn, transformer utilities के लिए candle-transformers, ONNX evaluation के लिए candle-onnx और flash attention v2 के लिए candle-flash-attn।
वे models जिन्हें आपको port नहीं करना पड़ता
Examples में language models (LLaMA v1 से v3, Mistral, Mixtral, Gemma, Phi, Qwen, Falcon, StarCoder, RWKV, Mamba), quantized LLMs, Stable Diffusion 1.5, 2.1 और XL, speech के लिए Whisper, speech synthesis के लिए Parler-TTS और MetaVoice, image to text के लिए BLIP और TrOCR, YOLO v3 और v8, Segment Anything, DINOv2 और कई vision backbones शामिल हैं। अगर आपका model उस सूची में है, तो migration ज़्यादातर weights load करना और मौजूदा implementation को call करना है।
PyTorch से कहाँ कम पड़ता है
candle training support करता है, लेकिन उसके लिए इसका ecosystem PyTorch से कहीं छोटा है; candle-ext जैसे projects खास तौर पर उन PyTorch functions को देने के लिए मौजूद हैं जो candle में नहीं हैं। Examples में न होने वाले model को Rust में layer दर layer फिर से implement करना पड़ता है, जो नई architecture के लिए असली काम है। GPU acceleration Rust के बजाय native CUDA libraries पर निर्भर है। अपेक्षा रखें कि research और training के लिए Python रखेंगे, और inference को आगे बढ़ाएँगे।
inference को एक-एक कदम में ले जाना
weights को export करें
अपने trained model का state dict Python से safetensors में save करें। इससे candle को ऐसी file मिलती है जिसे वह memory-map कर सकता है और candle::safetensors::load("model.safetensors", &device) से load कर सकता है, और production से pickle loading हट जाती है। जिन LLMs को आप quantize करना चाहते हैं, उनके लिए llama.cpp format की GGUF file भी quantized examples के साथ काम करती है।
forward pass को translate करें
अगर architecture पहले से candle-transformers में है, तो उसे इस्तेमाल करें। वरना candle-nn layers से forward pass को फिर से लिखें, cheatsheet mapping के साथ: torch.zeros((2, 2)) से Tensor::zeros((2, 2), DType::F32, &Device::Cpu)?, tensor.i(...) से indexing, view से reshape, a + b से &a + &b, और .to(dtype=torch.float16) से to_dtype(DType::F16)। Tokenization आम तौर पर Rust tokenizers crate पर चला जाता है।
speed से पहले numerics जाँचें
एक ही inputs को Python model और Rust model दोनों में चलाएँ और output tensors को अपने dtype के लिए उपयुक्त tolerance के भीतर तुलना करें। पहले CPU पर करें, फिर cuda feature enable करें और Device::new_cuda(0)? से device बदलें। उसके बाद ही realistic concurrency में Python सेवा के मुकाबले latency और memory नापें।
deployment target चुनें
Servers के लिए CPU या CUDA backend के साथ release binary build करें। Browser या local-first app के लिए wasm32-unknown-unknown target जोड़ें और candle-wasm-examples pattern अपनाएँ। Remade stack के on-device Mind crates पहले से इस runtime पर हैं: detection के लिए Diana, speech के लिए Mercury, और FFAI media toolkit।
संबंधित links
Remade का अपना fork candle repo landing पर pin किया गया है, और जिन results को persist रहना है वे SpaceDB में जा सकते हैं। जो inference आपके अपने hardware पर रहता है उसे MATA.NETWORK पर रखा जा सकता है और Disco Party के ज़रिए साझा किया जा सकता है। अन्य model crates Mind band guide पर हैं।
FAQ
candle को कौन बनाए रखता है?
Hugging Face candle को upstream विकसित करता है। Remade with Rust github.com/Remade-With-Rust/candle पर एक fork रखता है ताकि उसके Mind crates एक साझा tensor runtime pin कर सकें; इसने framework नहीं बनाया।
candle API PyTorch के कितना करीब है?
सामान्य operations के लिए करीब। torch.zeros((2, 2)) अब Tensor::zeros((2, 2), DType::F32, &Device::Cpu)? बनता है, tensor[:, :4] अब tensor.i((.., ..4))?, view अब reshape, और a.matmul(b) अब a.matmul(&b)?। ज़्यादातर calls Result लौटाते हैं, इसलिए errors ? से संभाले जाते हैं।
क्या candle llama.cpp जैसे quantized LLMs चला सकता है?
हाँ। candle llama.cpp के quantized types support करता है और इसके examples में quantized LLaMA, Mistral, Mixtral, Zephyr, OpenChat और Qwen3 MoE models शामिल हैं, जिन्हें cargo run --example quantized --release जैसे commands से चलाया जाता है।
क्या candle web browser में काम करता है?
हाँ। यह wasm32-unknown-unknown पर compile होता है, और project Whisper, LLaMA2, T5, Phi, Segment Anything, YOLO और BLIP के लिए in-browser demos host करता है। llama2-c example trunk से build होता है और locally serve करता है।
क्या candle में सब कुछ शुद्ध Rust है?
Core और CPU backend Rust हैं। Optional accelerators नहीं हैं: x86 पर MKL, macOS पर Accelerate, custom kernels और cuDNN के साथ CUDA, और multi-GPU के लिए NCCL। अगर native libraries के बिना dependency tree आपके लिए मायने रखता है तो उन features को off रखें।