torchchat icon indicating copy to clipboard operation
torchchat copied to clipboard

Comparing tokens/second with llama-cpp on mac m1

Open agunapal opened this issue 1 year ago • 7 comments

I was trying to compare TorchChat's generate tokens/second with llama-cpp's generate on my mac.

I am using GGML FP16 with llama-cpp and I believe the default in TorchChat is FP16?

TorchChat : 1.85 tokens/sec llama-cpp: 11.27 tokens/sec

Please correct me if I am not doing an apples-apples comparison!

TorchChat

python torchchat.py generate llama3 --prompt "write me a story about a boy and his bear" --seed 42 --max-new-tokens 50
Using device=mps 
Loading model...
Time to load model: 37.44 seconds
-----------------------------------------------------------
write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a boy named Max. Max was a curious and adventurous boy who loved exploring the woods and discovering new things. One day, while wandering through the forest, Max stumbled upon
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 1: 26.99 sec total, 50 tokens, 1.85 tokens/sec, 539.72 ms/token
Bandwidth achieved: 29.76 GB/s
*** This first iteration will include cold start effects for dynamic import, hardware caches. ***

========================================

Average tokens/sec: 1.85
Memory used: 0.00 GB

llama-cpp

./main -m models/model/models--meta-llama--Meta-Llama-3-8B-Instruct/snapshots/e1945c40cd546c78e41f1151f4db032b271faeaa/ggml-model-f16.gguf -p "write me a story about a boy and his bear" -n 50
Log start
main: build = 3078 (bde7cd3c)
main: built with Apple clang version 15.0.0 (clang-1500.3.9.4) for arm64-apple-darwin23.4.0
main: seed  = 1717464687
llama_model_loader: loaded meta data with 22 key-value pairs and 291 tensors from models/model/models--meta-llama--Meta-Llama-3-8B-Instruct/snapshots/e1945c40cd546c78e41f1151f4db032b271faeaa/ggml-model-f16.gguf (version GGUF V3 (latest))
llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
llama_model_loader: - kv   0:                       general.architecture str              = llama
llama_model_loader: - kv   1:                               general.name str              = e1945c40cd546c78e41f1151f4db032b271faeaa
llama_model_loader: - kv   2:                          llama.block_count u32              = 32
llama_model_loader: - kv   3:                       llama.context_length u32              = 8192
llama_model_loader: - kv   4:                     llama.embedding_length u32              = 4096
llama_model_loader: - kv   5:                  llama.feed_forward_length u32              = 14336
llama_model_loader: - kv   6:                 llama.attention.head_count u32              = 32
llama_model_loader: - kv   7:              llama.attention.head_count_kv u32              = 8
llama_model_loader: - kv   8:                       llama.rope.freq_base f32              = 500000.000000
llama_model_loader: - kv   9:     llama.attention.layer_norm_rms_epsilon f32              = 0.000010
llama_model_loader: - kv  10:                          general.file_type u32              = 1
llama_model_loader: - kv  11:                           llama.vocab_size u32              = 128256
llama_model_loader: - kv  12:                 llama.rope.dimension_count u32              = 128
llama_model_loader: - kv  13:                       tokenizer.ggml.model str              = gpt2
llama_model_loader: - kv  14:                         tokenizer.ggml.pre str              = llama-bpe
llama_model_loader: - kv  15:                      tokenizer.ggml.tokens arr[str,128256]  = ["!", "\"", "#", "$", "%", "&", "'", ...
llama_model_loader: - kv  16:                  tokenizer.ggml.token_type arr[i32,128256]  = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
llama_model_loader: - kv  17:                      tokenizer.ggml.merges arr[str,280147]  = ["Ġ Ġ", "Ġ ĠĠĠ", "ĠĠ ĠĠ", "...
llama_model_loader: - kv  18:                tokenizer.ggml.bos_token_id u32              = 128000
llama_model_loader: - kv  19:                tokenizer.ggml.eos_token_id u32              = 128009
llama_model_loader: - kv  20:                    tokenizer.chat_template str              = {% set loop_messages = messages %}{% ...
llama_model_loader: - kv  21:               general.quantization_version u32              = 2
llama_model_loader: - type  f32:   65 tensors
llama_model_loader: - type  f16:  226 tensors
llm_load_vocab: special tokens cache size = 256
llm_load_vocab: token to piece cache size = 0.8000 MB
llm_load_print_meta: format           = GGUF V3 (latest)
llm_load_print_meta: arch             = llama
llm_load_print_meta: vocab type       = BPE
llm_load_print_meta: n_vocab          = 128256
llm_load_print_meta: n_merges         = 280147
llm_load_print_meta: n_ctx_train      = 8192
llm_load_print_meta: n_embd           = 4096
llm_load_print_meta: n_head           = 32
llm_load_print_meta: n_head_kv        = 8
llm_load_print_meta: n_layer          = 32
llm_load_print_meta: n_rot            = 128
llm_load_print_meta: n_embd_head_k    = 128
llm_load_print_meta: n_embd_head_v    = 128
llm_load_print_meta: n_gqa            = 4
llm_load_print_meta: n_embd_k_gqa     = 1024
llm_load_print_meta: n_embd_v_gqa     = 1024
llm_load_print_meta: f_norm_eps       = 0.0e+00
llm_load_print_meta: f_norm_rms_eps   = 1.0e-05
llm_load_print_meta: f_clamp_kqv      = 0.0e+00
llm_load_print_meta: f_max_alibi_bias = 0.0e+00
llm_load_print_meta: f_logit_scale    = 0.0e+00
llm_load_print_meta: n_ff             = 14336
llm_load_print_meta: n_expert         = 0
llm_load_print_meta: n_expert_used    = 0
llm_load_print_meta: causal attn      = 1
llm_load_print_meta: pooling type     = 0
llm_load_print_meta: rope type        = 0
llm_load_print_meta: rope scaling     = linear
llm_load_print_meta: freq_base_train  = 500000.0
llm_load_print_meta: freq_scale_train = 1
llm_load_print_meta: n_yarn_orig_ctx  = 8192
llm_load_print_meta: rope_finetuned   = unknown
llm_load_print_meta: ssm_d_conv       = 0
llm_load_print_meta: ssm_d_inner      = 0
llm_load_print_meta: ssm_d_state      = 0
llm_load_print_meta: ssm_dt_rank      = 0
llm_load_print_meta: model type       = 8B
llm_load_print_meta: model ftype      = F16
llm_load_print_meta: model params     = 8.03 B
llm_load_print_meta: model size       = 14.96 GiB (16.00 BPW) 
llm_load_print_meta: general.name     = e1945c40cd546c78e41f1151f4db032b271faeaa
llm_load_print_meta: BOS token        = 128000 '<|begin_of_text|>'
llm_load_print_meta: EOS token        = 128009 '<|eot_id|>'
llm_load_print_meta: LF token         = 128 'Ä'
llm_load_print_meta: EOT token        = 128009 '<|eot_id|>'
llm_load_tensors: ggml ctx size =    0.30 MiB
ggml_backend_metal_log_allocated_size: allocated buffer, size = 14315.03 MiB, (14315.09 / 21845.34)
llm_load_tensors: offloading 32 repeating layers to GPU
llm_load_tensors: offloading non-repeating layers to GPU
llm_load_tensors: offloaded 33/33 layers to GPU
llm_load_tensors:      Metal buffer size = 14315.02 MiB
llm_load_tensors:        CPU buffer size =  1002.00 MiB
.........................................................................................
llama_new_context_with_model: n_ctx      = 512
llama_new_context_with_model: n_batch    = 512
llama_new_context_with_model: n_ubatch   = 512
llama_new_context_with_model: flash_attn = 0
llama_new_context_with_model: freq_base  = 500000.0
llama_new_context_with_model: freq_scale = 1
ggml_metal_init: allocating
ggml_metal_init: found device: Apple M1 Pro
ggml_metal_init: picking default device: Apple M1 Pro
ggml_metal_init: default.metallib not found, loading from source
ggml_metal_init: GGML_METAL_PATH_RESOURCES = nil
ggml_metal_init: loading '/Users/agunapal/Documents/experiments/llama.cpp/ggml-metal.metal'
ggml_metal_init: GPU name:   Apple M1 Pro
ggml_metal_init: GPU family: MTLGPUFamilyApple7  (1007)
ggml_metal_init: GPU family: MTLGPUFamilyCommon3 (3003)
ggml_metal_init: GPU family: MTLGPUFamilyMetal3  (5001)
ggml_metal_init: simdgroup reduction support   = true
ggml_metal_init: simdgroup matrix mul. support = true
ggml_metal_init: hasUnifiedMemory              = true
ggml_metal_init: recommendedMaxWorkingSetSize  = 22906.50 MB
llama_kv_cache_init:      Metal KV buffer size =    64.00 MiB
llama_new_context_with_model: KV self size  =   64.00 MiB, K (f16):   32.00 MiB, V (f16):   32.00 MiB
llama_new_context_with_model:        CPU  output buffer size =     0.49 MiB
llama_new_context_with_model:      Metal compute buffer size =   258.50 MiB
llama_new_context_with_model:        CPU compute buffer size =     9.01 MiB
llama_new_context_with_model: graph nodes  = 1030
llama_new_context_with_model: graph splits = 2

system_info: n_threads = 8 / 10 | AVX = 0 | AVX_VNNI = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | AVX512_BF16 = 0 | FMA = 0 | NEON = 1 | SVE = 0 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | SSSE3 = 0 | VSX = 0 | MATMUL_INT8 = 0 | LLAMAFILE = 1 | 
sampling: 
	repeat_last_n = 64, repeat_penalty = 1.000, frequency_penalty = 0.000, presence_penalty = 0.000
	top_k = 40, tfs_z = 1.000, top_p = 0.950, min_p = 0.050, typical_p = 1.000, temp = 0.800
	mirostat = 0, mirostat_lr = 0.100, mirostat_ent = 5.000
sampling order: 
CFG -> Penalties -> top_k -> tfs_z -> typical_p -> top_p -> min_p -> temperature 
generate: n_ctx = 512, n_batch = 2048, n_predict = 50, n_keep = 0


write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a young boy named Max. Max loved the forest and spent most of his days exploring its secrets, discovering hidden streams and secret meadows. But Max wasn’t alone in his adventures –
llama_print_timings:        load time =    4308.18 ms
llama_print_timings:      sample time =       2.69 ms /    50 runs   (    0.05 ms per token, 18615.04 tokens per second)
llama_print_timings: prompt eval time =     158.47 ms /    11 tokens (   14.41 ms per token,    69.41 tokens per second)
llama_print_timings:        eval time =    4346.29 ms /    49 runs   (   88.70 ms per token,    11.27 tokens per second)
llama_print_timings:       total time =    4524.97 ms /    60 tokens
ggml_metal_free: deallocating
Log end


agunapal avatar Jun 04 '24 01:06 agunapal

@agunapal can you post your time to first token result as well?

larryliu0820 avatar Jun 10 '24 17:06 larryliu0820

  1. IIRC, llama.cpp is hiding metal initialization cost behind that startup wall of text.
  2. The PyTorch eager MPS backend has a significant overhead on the first run, which you can see by passing --num-samples > 1:
$ python torchchat.py generate llama3 --prompt "write me a story about a boy and his bear" --seed 42 --max-new-tokens 50 --num-samples 3
Using device=mps
Loading model...
Time to load model: 28.65 seconds
-----------------------------------------------------------
write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a boy named Max. Max was a curious and adventurous boy who loved exploring the woods and discovering new things. One day, while wandering through the forest, Max stumbled upon a big
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 1: 28.39 sec total, time to first token 19.70, 50 tokens, 1.76 tokens/sec, 567.89 ms/token
Bandwidth achieved: 28.28 GB/s
*** This first iteration will include cold start effects for dynamic import, hardware caches. ***
write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a young boy named Max. Max was a curious and adventurous boy who loved exploring the woods and playing with the animals that lived there. Among his favorite friends were a group of bears
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 2: 5.64 sec total, time to first token 0.18, 50 tokens, 8.87 tokens/sec, 112.74 ms/token
Bandwidth achieved: 142.46 GB/s
write me a story about a boy and his bear
Once upon a time, in a small village nestled in the heart of a dense forest, there lived a young boy named Kaito. Kaito was a curious and adventurous boy who loved exploring the forest and all its secrets. One
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 3: 5.68 sec total, time to first token 0.21, 50 tokens, 8.80 tokens/sec, 113.60 ms/token
Bandwidth achieved: 141.37 GB/s

========================================

Average tokens/sec: 6.48
Memory used: 0.00 GB

(generated on my M1 Pro)

swolchok avatar Jun 10 '24 18:06 swolchok

Hi @swolchok You are right. I suggest showing --num_samples > 1 in the README. Otherwise the first experience for a user would be that torchchat is "slow"

Also, now I see time to first token value as 0.20. Is this accurate?

Loading model...
Time to load model: 54.12 seconds
-----------------------------------------------------------
write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a boy named Max. Max was a curious and adventurous boy who loved exploring the woods and discovering new things. One day, while wandering through the forest, Max stumbled upon a big
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 1: 36.65 sec total, time to first token 29.71, 50 tokens, 1.36 tokens/sec, 733.10 ms/token
Bandwidth achieved: 21.91 GB/s
*** This first iteration will include cold start effects for dynamic import, hardware caches. ***
write me a story about a boy and his bear
Once upon a time, in a dense forest, there lived a young boy named Max. Max was a curious and adventurous boy who loved exploring the woods and playing with the animals that lived there. Among his favorite friends were a group of bears
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 2: 5.63 sec total, time to first token 0.20, 50 tokens, 8.88 tokens/sec, 112.65 ms/token
Bandwidth achieved: 142.56 GB/s
write me a story about a boy and his bear
Once upon a time, in a small village nestled in the heart of a dense forest, there lived a young boy named Kaito. Kaito was a curious and adventurous boy who loved exploring the forest and all its secrets. One
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------

agunapal avatar Jun 10 '24 19:06 agunapal

The way that I measure time to first token is by starting and ending the timer around prefill() function in generate.py. This should be accurate.

larryliu0820 avatar Jun 10 '24 19:06 larryliu0820

Hi @larryliu0820 It doesn't seem like llama-cpp is showing this by default. Will have to add the logic to check this. Will update when I can try this out

agunapal avatar Jun 10 '24 19:06 agunapal

$ python torchchat.py generate llama3 --prompt "write me a story about a boy and his bear" --seed 42 --max-new-tokens 50 --num-samples 3

For some reason llama2 does not have the same problem, see

% python3 torchchat.py generate llama2 --prompt "write me a story about a boy and his bear" --seed 42 --max-new-tokens 50  --num-samples 3 
Using device=mps 
Loading model...
Time to load model: 43.31 seconds
-----------------------------------------------------------
write me a story about a boy and his bear

Write a story about a boy and his bear. The boy and his bear were best friends and did everything together. One day, the boy and his bear went on a camping trip in the woods. As they were setting
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 1: 5.41 sec total, 9.24 tokens/sec
Bandwidth achieved: 124.56 GB/s
*** This first iteration will include cold start effects for dynamic import, hardware caches. ***
write me a story about a boy and his bear

Title: The Adventures of Benny and Barnaby

Once upon a time, there was a young boy named Benny who lived in a small village nestled in the rolling hills of the countryside
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 2: 5.04 sec total, 9.92 tokens/sec
Bandwidth achieved: 133.75 GB/s
write me a story about a boy and his bear

Once upon a time, in a small village surrounded by dense forests, there lived a young boy named Max. Max was a curious and adventurous boy, always eager to explore the world around him. He spent most
[Max Sequence Length Reached. Ending Conversation.]
---------------------------------------------------
Time for inference 3: 4.98 sec total, 10.03 tokens/sec
Bandwidth achieved: 135.18 GB/s

========================================

Average tokens/sec: 9.73
Memory used: 0.00 GB

(Also on corporate M1 Pro)

malfet avatar Jun 10 '24 19:06 malfet

From the previous chat, llama3 in torchchat is still slower than that in llama-cpp on mac m1, right? torchchat: 8.88 tokens/sec llama-cpp: 11.27 tokens/sec

yanbing-j avatar Aug 08 '24 08:08 yanbing-j

From the previous chat, llama3 in torchchat is still slower than that in llama-cpp on mac m1, right? torchchat: 8.88 tokens/sec llama-cpp: 11.27 tokens/sec

It is but we will be landing some changes that will push it past llama.cpp very shortly.

byjlw avatar Aug 30 '24 16:08 byjlw