DEV Community

Cover image for Local LLM हर बार अलग score क्यों देता है, और क्या temperature 0 से यह ठीक हो जाता है?

Local LLM हर बार अलग score क्यों देता है, और क्या temperature 0 से यह ठीक हो जाता है?

किसी language model को एक CV दीजिए और 1 से 10 तक score माँगिए, तो आपको एक नंबर और पूरे भरोसे वाला कारण मिल जाता है। दोबारा पूछिए, तो नंबर बदल सकता है। आम सलाह होती है "temperature को 0 कर दो"। यह सही है, लेकिन दो सवाल फिर भी बचते हैं: default settings में score असल में कितना हिलता है, और temperature 0 क्या ठीक नहीं करता? दोनों को laptop पर कुछ ही मिनटों में मापा जा सकता है, क्योंकि score सिर्फ एक token है और model उसकी probability सीधे दे देता है।

नीचे का setup 8 GB वाले M1 Mac पर mlx-lm 0.32.2 के साथ Qwen2.5-3B-Instruct (4-bit, MLX) इस्तेमाल करता है। Prompts वही हैं जो osFoundry desktop app के Models → Playground में CV-scoring demo में थे: junior data analyst role के लिए छह लाइनों का एक CV, और उसके बाद "Score this CV from 1 to 10 for a junior data analyst role. Start with the score, then one reason." यह app local models को llama.cpp के llama-server से चलाता है, इसलिए sampler settings वहीं से आती हैं।

जब आप खुद कुछ नहीं चुनते, तब मिलने वाले defaults

अगर किसी request में temperature नहीं है, तो llama-server अपने defaults से sampling करता है। App के साथ आने वाला build (version 10358) इन्हें --help में दिखाता है:

--temp, --temperature N    temperature (default: 0.80)
--top-k N                  top-k sampling (default: 40, 0 = disabled)
--top-p N                  top-p sampling (default: 0.95, 1.0 = disabled)
--min-p N                  min-p sampling (default: 0.05, 0.0 = disabled)
-s, --seed SEED            RNG seed (default: -1, use random seed for -1)
Enter fullscreen mode Exit fullscreen mode

ये values हर उस request पर लागू होती हैं जो अपनी values खुद set नहीं करती। App जब llama-server शुरू करता है, तो --temp तभी जोड़ता है जब server के लिए कोई temperature configure किया गया हो (local_inference.rs)। इसलिए बिना temperature वाली request 0.8 पर और random seed के साथ sample होती है, और ज़्यादातर llama.cpp front ends भी out of the box यही करते हैं।

Score को अगला token बनाना

Free-form जवाब "Score: 7", "7/10" या "I would rate this CV a 7" से शुरू हो सकता है, जिससे score कहाँ है यह पकड़ना मुश्किल हो जाता है। इसका हल है जवाब की शुरुआत पहले से भर देना (pre-fill), ताकि model जो अगला token बनाए वही digit हो:

import mlx.core as mx
from mlx_lm import load, generate
from mlx_lm.sample_utils import make_sampler

model, tok = load("mlx-community/Qwen2.5-3B-Instruct-4bit")
DIGITS = {d: tok.encode(str(d))[0] for d in range(1, 10)}

def ids_for(cv, question, prefill="Score: "):
    msgs = [{"role": "user", "content": cv + "\n\n" + question}]
    text = tok.apply_chat_template(msgs, add_generation_prompt=True, tokenize=False)
    return tok.encode(text + prefill)

def digit_probs(ids, T=1.0):
    logits = model(mx.array([ids]))[0, -1].astype(mx.float32)
    p = mx.softmax(logits / T)
    return {d: float(p[t]) for d, t in DIGITS.items()}
Enter fullscreen mode Exit fullscreen mode

Qwen का tokenizer numbers को single digits में तोड़ता है, इसलिए "7" एक token है और "10" token "1" से शुरू होगा। 173 tokens के prompt पर एक forward pass से score का पूरा distribution मिल जाता है।

Score एक नंबर नहीं, एक distribution है

English CV के लिए, temperature 1 पर (model की raw probabilities):

Score Probability
7 74.2%
6 23.3%
8 1.3%
5 1.2%

Model यह "नहीं सोचता" कि CV 7 का है। वह सोचता है कि यह शायद 7 है, और 6 होने का चांस लगभग चार में एक है। Temperature softmax से पहले logits को divide करता है, जिससे यह distribution ज़्यादा नुकीला या ज़्यादा सपाट हो जाता है:

Temperature P(7) P(6) P(8)
1.0 74.2% 23.3% 1.3%
0.8 80.2% 18.9% 0.5%
0.5 91.0% 9.0% 0.0%
0.2 99.7% 0.3% 0.0%

llama-server सिर्फ temperature लागू नहीं करता। उसकी default chain पहले top-k 40, फिर top-p 0.95, फिर min-p 0.05 चलाती है, और temperature सबसे आखिर में। इन logits पर top-p ही 7 और 6 के बाद सब कुछ काट देता है (दोनों मिलकर 95% से ज़्यादा हैं), इसलिए default sampler ठीक दो scores में से चुनता है:

def llamacpp_default(logits, temp=0.8, top_k=40, top_p=0.95, min_p=0.05):
    order = mx.argsort(-logits)[:top_k]
    kept = logits[order]
    p = mx.softmax(kept)
    n = int(mx.sum(mx.cumsum(p) < top_p).item()) + 1
    idx = [i for i in range(n) if p[i].item() >= min_p * p[0].item()]
    q = mx.softmax(kept[mx.array(idx)] / temp)
    return {tok.decode([int(order[i])]): float(q[j]) for j, i in enumerate(idx)}
# -> {'7': 0.8093, '6': 0.1907}
Enter fullscreen mode Exit fullscreen mode

फिर मैंने असल में sampling की: उसी prompt से 200 generations, mlx-lm के sampler को उन्हीं चार values पर set करके (make_sampler(temp=0.8, top_p=0.95, min_p=0.05, top_k=40))।

7 6
Chain से अनुमान 80.9% 19.1%
असल में, 200 runs 162 (81%) 38 (19%)

यानी default settings में लगभग हर पाँच में से एक run इस CV को 7 की जगह 6 देता है। अगर आप दो candidates को एक-एक बार score करें और एक को 6 और दूसरे को 7 मिले, तो इस बात का अच्छा-खासा चांस है कि यह फर्क सिर्फ draw का है।

एक और सपाट case: वही CV Japanese में

वही CV और सवाल Japanese में (222 tokens, pre-fill 「点数:」) देने पर model कहीं कम तय था:

Score T=1 पर probability Defaults पर असल में, 200 runs
8 39.8% 108
9 23.8% 44
7 19.4% 40
10 ("1" से शुरू: 4.1%) 8

यहाँ सबसे संभावित score सिर्फ लगभग आधी बार जीतता है, और 200 runs में चार अलग-अलग scores आए। Score कितना डगमगाता है, यह model की कोई fixed property नहीं है। यह इस पर निर्भर करता है कि इस खास input के लिए top candidates कितने करीब हैं, और यह probabilities देखकर ही पता चलता है।

Temperature 0 क्या ठीक करता है

Temperature 0 (greedy decoding: हमेशा सबसे संभावित token) के साथ मैंने हर भाषा के लिए पूरा 40-token जवाब 20 बार generate किया। हर भाषा में सिर्फ 1 अलग text मिला, character by character एक जैसा। English CV के लिए हर बार "7" और उसके बाद वही कारण, और Japanese के लिए 「8点」।

Fixed seed sampling में भी यही करता है: हर run से पहले mx.random.seed(42) reset करके 10 runs में 1 ही अलग text मिला। यह तब काम का है जब आपको variety चाहिए लेकिन किसी खास run को दोबारा reproduce करना हो। लेकिन यह fairness का tool नहीं है। Seed के साथ भी अलग CV को draws का अलग sequence मिलता है, इसलिए दो candidates फिर भी अलग-अलग किस्मत से score होते हैं।

इन सब पर एक शर्त लागू है: यहाँ हर run एक idle model पर एक single request था। जो servers कई requests को एक batch में चलाते हैं, उनमें runs के बीच बहुत छोटे floating-point फर्क आ सकते हैं, जो तभी मायने रखते हैं जब दो tokens लगभग बराबर हों। इसे यहाँ नहीं मापा गया।

Temperature 0 क्या ठीक नहीं करता

Greedy decoding output को input का function बना देता है, लेकिन stable function नहीं। मैंने चार ऐसे बदलाव किए जिनसे CV का score नहीं बदलना चाहिए, और हर बार greedy score और probabilities नोट कीं:

बदलाव (English) Greedy score P(top) दूसरे नंबर पर
कोई नहीं 7 74.2% 6, 23.3% पर
Skills वाली line ऊपर की 7 69.6% 6, 28.1% पर
नाम बदला (Mia Chen → James Miller) 7 68.5% 6, 28.5% पर
एक extra blank line 7 71.0% 6, 26.1% पर
सवाल दूसरे शब्दों में 6 67.5% 5, 19.3% पर

दूसरे शब्दों वाला सवाल था "Rate this CV for a junior data analyst role on a 1 to 10 scale. Give the score first, then one reason." Task वही, CV वही, और greedy score 7 से गिरकर 6 हो गया। Japanese में पाँचों versions temperature 0 पर 8 ही रहे, लेकिन top probability कभी 44% से ऊपर नहीं गई, और बदलाव के हिसाब से दूसरा नंबर 9 और 7 के बीच बदलता रहा।

तो temperature 0 runs के बीच की randomness हटाता है, prompt के प्रति sensitivity नहीं। CVs की तुलना के लिए इसका मतलब है:

  • Temperature 0 इस्तेमाल करें और जिन चीज़ों की तुलना कर रहे हैं उन सब के लिए बिल्कुल वही सवाल, character by character। सवाल बदला, तो सब कुछ दोबारा score करें।
  • सिर्फ score नहीं, probability भी रखें। llama-server का /completion endpoint n_probs पास करने पर token probabilities लौटाता है, और ऊपर वाली pre-fill trick के साथ पहला token ही score है। 74% वाला 7 और 40% वाला 8 एक जैसे जवाब नहीं हैं।
  • कम confidence वाले एक point के फर्क को tie मानें, और किसी भी नंबर पर भरोसा करने से पहले model का दिया कारण पढ़ें।

App में यही करना

osFoundry desktop app में यही experiment Models → Playground में एक ही local model वाले तीन panels से हो जाता है। पहले defaults के साथ prompt एक बार चलाकर फैलाव देखें, फिर settings row खोलकर Temp को 0 करें।

एक ही model और एक ही CV वाले तीन Playground panels, default settings पर 6, 8 और 7 score देते हुए (उदाहरण)

Temperature और top-p की glossary entries इन दोनों settings को और विस्तार से समझाती हैं। App osFoundry download page से free में मिलता है।

Top comments (0)