llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Different outputs for differents numbers of threads (same seed)

Open BadisG opened this issue 3 years ago • 2 comments

Hello,

I simply wanted to bring up the point that the output can vary based on the number of threads selected, even if the seed stays constant.

I have an intel core i7 10700K that has 16 threads.

For this example I'm using the 13B model (./models/13B/ggml-model-q4_0.bin)

When I put -t 14 (make -j && ./main -m ./models/13B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -t 14 -n 50 --seed 1678486056), I got this result: duU196l

When I put -t 15 (make -j && ./main -m ./models/13B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -t 15 -n 50 --seed 1678486056), I got this result: 5WIrvd1

I have zero knowledge in machine learning, perhaps this is a normal behavior.

Looking forward for your reactions!

BadisG avatar Mar 13 '23 16:03 BadisG

That isn't surprising, as each thread may be getting its own random seed. Changing the number of threads would then change the random seed initialisation, thus generating different output.

gjmulder avatar Mar 13 '23 16:03 gjmulder

It's not the random seed. The random seed is only one in the main thread.

The problem is more complex - the tensor operations are multi-threaded and based on the number of threads, they process different sizes of the input data. Since these are floating point numbers and they are often accumulated, the finite precision of floating-point operations causes the final results to be slightly different for different number of threads. This effect is amplified by the auto-regressive nature of the transformer.

There are ways to fix this, but a major refactor in ggml is required.

ggerganov avatar Mar 13 '23 17:03 ggerganov

@BadisG I think I have fixed this behaviour on latest master. If you can give it a try and let me know if the results for different number of threads is now the same

ggerganov avatar Mar 23 '23 21:03 ggerganov

@ggerganov I just tried with t = 14, 12 and 5 and I have the same result each time. Looks like it's fixed! Great work I appreciate 😄

BadisG avatar Mar 23 '23 21:03 BadisG