Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Tensorstore af-dist implementation #161

Open
jeromekelleher opened this issue Aug 28, 2024 · 50 comments
Open

Python Tensorstore af-dist implementation #161

jeromekelleher opened this issue Aug 28, 2024 · 50 comments

Comments

@jeromekelleher
Copy link
Contributor

jeromekelleher commented Aug 28, 2024

The C++ af-dist implementation added in #157 (and notes in #160) is surprisingly slow. In some informal tests it looks like it's about 3X slower than Python Zarr, just retrieving the chunks. There's no good reason for this, so I suspect we must be using Tensorstore badly.

To make it easier to experiment with Tensorstore configuration settings, I think it would be very helpful to have a Python implementation of the afdist operation using the Python tensorstore API. The Python API is properly documented, and the (unbelievably slow!) C++ compile times don't have to be endured.

CPU time should be dominated by decompression, so we really should expect to have performance parity with the Python Zarr approach.

@Will-Tyler - fancy taking this one on?

@tomwhite
Copy link
Contributor

This may be a useful tool for finding good Tensorstore configuration settings: https://github.com/royerlab/czpeedy (via cubed-dev/cubed#513)

@jeromekelleher
Copy link
Contributor Author

Looks like that's just for write performance, but definitely worth having a look at 👍

@Will-Tyler
Copy link
Contributor

I am certainly happy to investigate this issue! Before I start implementing afdist with TensorStore's Python API, I wonder if you built the project in Release mode (i.e. cmake -DCMAKE_BUILD_TYPE=Release ..). The performance I observed when working in Debug mode matches the performance that you are describing. However, I just checked out the latest version and built it in Release mode and found the Release-mode performance to be similar to the Python Zarr implementation:

time ./software/ts-afdist/build/ts_afdist scaling/data/chr21_10_4.zarr/call_genotype
# PROB_DIST, genotype probability distribution, assumes HWE
PROB_DIST       0       0.1     44835153
PROB_DIST       0.1     0.2     46629516
PROB_DIST       0.2     0.3     61330193
PROB_DIST       0.3     0.4     81254580
PROB_DIST       0.4     0.5     188462062
PROB_DIST       0.5     0.6     18017770
PROB_DIST       0.6     0.7     20193848
PROB_DIST       0.7     0.8     18465643
PROB_DIST       0.8     0.9     19033680
PROB_DIST       0.9     1       71487987
./software/ts-afdist/build/ts_afdist   30.68s user 1.21s system 96% cpu 32.941 total
python src/collect_data.py whole-matrix-compute scaling/data/chr21_10_4.ts -t zarr
scaling/data/chr21_10_4.ts n=10000, m=863998
   num_samples  num_sites  tool  user_time  sys_time  wall_time storage
0        10000     863998  zarr  31.880481  1.641813  30.567949     hdd

If you are already building in Release mode, can you describe your informal testing? I would like to replicate the problem to debug it.

References


P.S. I found --parallel helps make the build faster.

@jeromekelleher
Copy link
Contributor Author

Thanks for the tip on Release mode @Will-Tyler, you're right I'd forgotten that.

This is what I get when I run the benchmarks (added in #162):

   num_samples  num_sites    tool  user_time  sys_time    wall_time storage
1           10     116230  ts_cpp       0.27      0.04     0.344683     hdd
0           10     116230    zarr       0.27      0.01     0.288311     hdd
3          100     204714  ts_cpp       0.66      0.04     0.725902     hdd
2          100     204714    zarr       0.52      0.01     0.541813     hdd
5         1000     403989  ts_cpp       2.10      0.07     2.196294     hdd
4         1000     403989    zarr       1.83      0.08     1.920323     hdd
7        10000     863998  ts_cpp      44.12      0.39    44.371482     hdd
6        10000     863998    zarr      30.59      0.13    31.309266     hdd
9       100000    2365367  ts_cpp    1238.77      6.34  1239.721479     hdd
8       100000    2365367    zarr     882.03     18.54  1271.120435     hdd

If we focus on the user_time column, we can see there's a substantial difference between tensorstore and Zarr python there. (The 100k samples result is confusing when you look at wall_time because the disk cache was warm for ts_cpp, but not for zarr.)

Looking at top for ts_cpp, I can see that tensorstore is running a bunch of background threads to do the fetching and decoding. Perhaps this is contributing to the overhead? It shouldn't be that much, though.

@jeromekelleher
Copy link
Contributor Author

Basically, I don't mind if we poke at this in C++ or Python - I'd just find it easier to understand what Tensorstore is doing if we go through the Python API.

@jeromekelleher
Copy link
Contributor Author

Cutting out the bits where we process the chunks,

Here's the perf report for ts_py:

  29.63%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so           [.] blosc_internal_bshuf_shuffle_bit_eightelem_scal                                                           
  15.71%  python3         [kernel.kallsyms]                                     [k] syscall_enter_from_user_mode                                                                              
  10.02%  ts_pool_worker  libc-2.31.so                                          [.] __memmove_sse2_unaligned_erms                                                                             
   4.49%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so           [.] blosc_internal_bshuf_trans_byte_bitrow_sse2                                                               
   4.01%  python3         [kernel.kallsyms]                                     [k] __x64_sys_futex                                                                                           
   3.85%  python3         [kernel.kallsyms]                                     [k] __schedule                                                                                                
   3.58%  python3         libc-2.31.so                                          [.] __sched_yield                                                                                             
   3.19%  ts_pool_worker  libc-2.31.so                                          [.] __memset_erms                                                                                             
   2.56%  python3         [kernel.kallsyms]                                     [k] _raw_spin_unlock_irqrestore                                                                               
   2.21%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so           [.] ZSTD_decompressBlock_internal.part.0                                                                      
   1.15%  python3         python3.9                                             [.] _PyEval_EvalFrameDefault                                                                                  
   1.00%  python3         [kernel.kallsyms]                                     [k] do_sched_yield                                                                                            
   0.96%  ts_pool_worker  [kernel.kallsyms]                                     [k] clear_page_erms                                                                                           
   0.85%  python3         [kernel.kallsyms]                                     [k] do_syscall_64                                                                                             
   0.59%  python3         [kernel.kallsyms]                                     [k] get_timespec64                                 

vs Zarr python (which is much quicker):

  37.77%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] blosc_internal_bshuf_shuffle_bit_eightelem_scal                                                                      
   8.30%  python3  [kernel.kallsyms]                                 [k] __schedule                                                                                                           
   7.66%  python3  libc-2.31.so                                      [.] __sched_yield                                                                                                        
   7.65%  python3  [kernel.kallsyms]                                 [k] syscall_enter_from_user_mode                                                                                         
   7.05%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] blosc_internal_bshuf_trans_byte_bitrow_sse2                                                                          
   3.38%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] ZSTD_decompressBlock_internal                                                                                        
   2.63%  python3  python3.9                                         [.] _PyEval_EvalFrameDefault                                                                                             
   2.09%  python3  [kernel.kallsyms]                                 [k] do_sched_yield                                          

@jeromekelleher
Copy link
Contributor Author

jeromekelleher commented Sep 2, 2024

Hmm - so looks like the threaded async approach tensorstore is using is adding substantial overhead here. I'll dig a bit more.

@jeromekelleher
Copy link
Contributor Author

Tweaking the tensorstore context options a bit doesn't seem to help much. With just loading the chunks into memory we get:

   num_samples  num_sites   tool  user_time  sys_time  wall_time storage
0        10000     863998  ts_py      39.24      5.78   27.97086     hdd
   num_samples  num_sites  tool  user_time  sys_time  wall_time storage
0        10000     863998  zarr      13.82      0.12  13.947528     hdd

which is a huge disparity, given tensorstore should be doing very little here.

@jeromekelleher
Copy link
Contributor Author

Hmmm, looking at strace output seems to confirm that tensorstore really is reading the chunk files once, although may be doing it with async APIs rather than sync file system calls.

@laramiel
Copy link

laramiel commented Sep 3, 2024

What is your test harness for this? What's the zarr spec? I can try to see if there are any C++ inefficiencies to remove here.

Is it just https://github.com/sgkit-dev/vcf-zarr-publication/pull/157/files#diff-236bc7438efdb12c94dbd13ba6e252219e51b1b7594cc63a8ef8ce7906aab453

@jeromekelleher
Copy link
Contributor Author

Hi @laramiel 👋 Any help would be much appreciated!

We have two different implementations, the C++ one that we started out with, and a Python one which we made to make playing around with it a bit easier.

The easiest way to run this and compare the implementations is using the dataset we have created for benchmarking, and the benchmarking script. This should get you up and running:

  • Clone this repo, and cd to scaling.
  • Run make data/chr21_10_1.ts data/chr21_10_4.zarr. This should work to get you the file you want with a minimum of extra stuff 🤞 The files are pretty big so it'll take a while.
  • From the root directory, run python3 src/collect_data.py whole-matrix-compute -t zarr -t ts_py scaling/data/chr21_10_4.ts. This will output the stats comparing the Zarr Python and Tensorstore Python implementations.

For simplicity, you may want to factor out the actual processing of the chunks. You can do this by replacing the function count_genotypes_chunk with a no-op. These are the numbers I get on my (busy) machine when the two implementations are just iterating chunk-by-chunk and not doing any processing:

$ python3 src/collect_data.py whole-matrix-compute -t zarr -t ts_py scaling/data/chr21_10_4.ts  
   num_samples  num_sites   tool  user_time  sys_time  wall_time storage
1        10000     863998  ts_py      45.11      9.73  34.475312     hdd
0        10000     863998   zarr      17.88      0.12  18.026047     hdd

The overall CPU time cost is over 2X here for Tensorstore, which seems unnecessary?

The benchmarking setup is a bit convoluted sorry, it's something that's evolved over time. Please do let me know if you hit any problems, I'd be more than happy to help! Thanks again for taking a look.

@benjeffery
Copy link
Contributor

benjeffery commented Sep 4, 2024

@jeromekelleher I'm not seeing the c++ tensorstore implementation slowness on my desktop:

   num_samples  num_sites    tool  user_time  sys_time  wall_time storage
1        10000     863998  ts_cpp      15.73      0.11  15.932257     hdd
2        10000     863998   ts_py      60.19      8.88  69.118334     hdd
0        10000     863998    zarr      19.83      0.05  19.896405     hdd

Will run the larger dataset overnight.

@laramiel
Copy link

laramiel commented Sep 4, 2024

Both of those have a very similar structure where they issue a read and then wait via .Wait() or .result() after the read.
It might be faster if they were converted to async methods, though you might need a semaphore to limit memory use.

@jeromekelleher
Copy link
Contributor Author

@jeromekelleher I'm not seeing the c++ tensorstore implementation slowness on my desktop:

That's interesting - @Will-Tyler noted something similar. I'll rerun and see what we get.

Still not obvious why the Python version is using so much more CPU though. I see the argument for sync vs async, but I would have thought that this should only affect the overall wall-time rather than actual CPU cycles used (user time)?

@benjeffery
Copy link
Contributor

benjeffery commented Sep 5, 2024

ts_py was spending a lot of it's time copying data around in numpy calls. Here's where I have got it to (will PR):

   num_samples  num_sites    tool  user_time  sys_time  wall_time storage
1        10000     863998  ts_cpp      15.93      0.07  16.093759     hdd
2        10000     863998   ts_py      20.65      0.13  20.871725     hdd
0        10000     863998    zarr      20.18      0.04  20.228303     hdd

@jeromekelleher
Copy link
Contributor Author

That's super - exactly what we'd hope to see. And excellent that TensorStore c++ is outperforming the Python code!

@jeromekelleher
Copy link
Contributor Author

Here's what I'm getting currently:

11        10000     863998  ts_cpp      44.01      0.44  44.289501     hdd                     
10        10000     863998   ts_py      75.32     13.66  54.940146     hdd                     
9         10000     863998    zarr      30.62      0.17  30.832301     hdd     

It must be something to do with the setup on my server I guess?

@jeromekelleher
Copy link
Contributor Author

Are you working off an SSD or spinning platters here @benjeffery?

@benjeffery
Copy link
Contributor

Are you working off an SSD or spinning platters here @benjeffery?

I've tested both and seen little difference. I've also just realised that you merged #164 yesterday afternoon - my previous python numbers were with code before that. 🤦

Here are the numbers with that included:

   num_samples  num_sites    tool  user_time  sys_time  wall_time storage
1        10000     863998  ts_cpp      16.01      0.09  16.189232     hdd
2        10000     863998   ts_py      23.96      0.09  24.145617     hdd
0        10000     863998    zarr      20.43      0.04  20.498896     hdd

@jeromekelleher
Copy link
Contributor Author

Good to know. So, must be something specific to my setup so - the machine and OS are quite old, so could be some important compile time optimisations are missing or something.

Did you do anything funky with the Tensorstore build?

@benjeffery
Copy link
Contributor

Did you do anything funky with the Tensorstore build?

Nope nothing at all - just installed cmake and ran the build as it is configured in the repo.

@benjeffery
Copy link
Contributor

I'm running

cmake version 3.30.2
NASM version 2.15.05
gcc version 11.4.0

@jeromekelleher
Copy link
Contributor Author

jeromekelleher commented Sep 5, 2024

Well this really is bizarre. After upgrading to the latest version of the Tensorstore Python module (which I'm not compiling, using the distributed binarys) I get this:

   num_samples  num_sites    tool  user_time  sys_time  wall_time storage
2        10000     863998  ts_cpp      58.77      0.72  59.302641     hdd
1        10000     863998   ts_py      89.10      0.50  89.613749     hdd
0        10000     863998    zarr      31.43      0.09  31.563379     hdd

So, the Python tensorstore version here is nearly a full minute slower than the Zarr version!

Digging in with perf I get this for the Zarr python version:
zarr-full

and this for Tensorstore Python version:
ts_py_all

So in both cases, the majority of the time is spent in Blosc, as you'd expect, but one takes 3X the time of the other. The processor here is old (Xeon E5-2680) and that probably explains the disparity with @benjeffery's results.

I can think of two possibilities:

  • Zarr Python is somehow doing a better job of passing aligned memory to blosc.
  • The official blosc package is compiled differently to the one vendored in with tensorstore, and that works better on this CPU.

@benjeffery - could you run these through perf record also, so we can see what's being called under the hood on your machine?

It would be nice if we could just say "oh well it doesn't work on old machines 🤷 " but this is the hardware I have available for doing these benchmarks, and they need a lot of storage and CPU time. I'm not keen on moving them somewhere else, so if we want to include Tensorstore we'll have to figure out a way of solving the issue.

@laramiel
Copy link

laramiel commented Sep 5, 2024

So it's possible that we're missing the default -msse2 flag when building blosc, which is a default option when building with CMake. I'll try and update those and see if it helps.

Edit: Nah, -msse2 should be the default with all x86_64 arch.

Edit: I noticed that you set the concurrency limits pretty low. Was there a reason for this?

            "data_copy_concurrency": {"limit": 1},
            "file_io_concurrency": {"limit": 1},

https://google.github.io/tensorstore/kvstore/file/index.html

@jbms
Copy link

jbms commented Sep 6, 2024

I haven't had a chance to look at your benchmarks and results in detail, but one thing to note is that the blosc codec in numcodecs (https://github.com/zarr-developers/numcodecs/blob/02d3ceec395481f3d84eb8f2f770e66aca6361b0/numcodecs/blosc.pyx#L478) used by zarr-python uses multi-threaded blosc under certain conditions that may apply in your benchmark. Tensorstore never uses multi-threaded blosc but instead can decode multiple chunks in parallel; in the case of your benchmark there is only one chunk at a time, though.

@jeromekelleher
Copy link
Contributor Author

Thanks for the input @laramiel and @jbms!

@laramiel

Edit: I noticed that you set the concurrency limits pretty low. Was there a reason for this?

That was just experimenting trying to rule out the overhead of having background threads. As we're just synchronously working chunk-by-chunk, there's nothing to gain from doing things in threads, and I was trying this to see if it made any difference. It doesn't.

@jbms

zarr-python uses multi-threaded blosc under certain conditions

This should all be factored out by measuring user time - true, having threads in blosc could affect the wall time, but it shouldn't make any difference to the overall compute cost. Wall-time and User-time + Sys-time are basically the same here, so I don't think threads are having any effect.

@benjeffery
Copy link
Contributor

Here are my perf reports:
Zarr:
zarr
Ts:
ts

__memmove_sse2_unaligned_erms (only seen under tensorstore) is a larger fraction on the Xeon, that's expected as ERMS was only introduced with Ivy Bridge and your Xeon is Sandy Bridge. That doesn't explain all the time though, but it's interesting that zarr doesn't need to use that nearly as much.

@jeromekelleher
Copy link
Contributor Author

jeromekelleher commented Sep 6, 2024

Ah yes, interestingly you have __memmove_avx_unaligned_erms vs I have __memmove_sse2_unaligned_erms on the old Xeon under Tensorstore.

My guess here is that Zarr is somehow handing memory to Blosc that meets the stricter alignment requirements for SSE/AVX than Tensorstore, and so we don't see this ```__memmove_XXX_unaligned_erms`` call in Zarr. The newer generation of CPUs is better able to handle stuff that's not aligned.

It would be amazing if the penalty for not having aligned memory under SSE2 was this big, but I think it's the best guess right now?

I see there are some calls to __memset_xxx under Zarr, so perhaps this is a memset vs memmove thing?

@jeromekelleher
Copy link
Contributor Author

Interesting... When I run perf collecting the full call graph, it seems to be telling me that the memmove is happening when Tensorstore copies data out (in CopyReadChunk). I think there might be some room for improvement there @laramiel @jbms?

call-graph

@benjeffery
Copy link
Contributor

benjeffery commented Sep 6, 2024

What I don't quite understand is how dissimilar your perf report cycle count ratio is to your timing ratio.
31s vs 89s compared with 161,859,771,726 vs 189,617,929,134?
The sample count looks consistent, so tensorstore must be waiting more?
Here's the result of doing

perf sched record -a --
perf sched latency

which shows there isn't much switching between python and the ts_pool_worker:
sched

Also shows the time is equally shared, would be interested to see this for the xeon.

@jeromekelleher
Copy link
Contributor Author

That is puzzling all right - 3X penalty just for unaligned memory seems huge.

I'm not able to run the sched events in perf right now and have to context switch I'm afraid!

@jeromekelleher
Copy link
Contributor Author

Anecdotally, I noticed the same problem when running on some Azure VMs last week. I ran the Python Tensorstore version and it was way slower than Zarr python. I didn't have time to characterise properly I'm afraid, but it does indicate that this is more than just a problem with crusty old hardware and likely something that people will encounter in the real world.

@jbms
Copy link

jbms commented Sep 18, 2024

I am able to reproduce this locally per your instructions:

python3 src/collect_data.py whole-matrix-compute -t zarr -t ts_py scaling/data/chr21_10_4.ts                                                                         
scaling/data/chr21_10_4.ts n=10000, m=863998                                                                                                                                                                        
   num_samples  num_sites  tool  user_time  sys_time  wall_time storage                                                                                                                                             
0        10000     863998  zarr      10.43      0.09  10.524919     hdd                                                                                                                                             
   num_samples  num_sites   tool  user_time  sys_time  wall_time storage                                                                                                                                            
1        10000     863998  ts_py      14.75      0.17  14.962039     hdd                                                                                                                                            
0        10000     863998   zarr      10.43      0.09  10.524919     hdd                    

Running both the tensorstore and zarr-python benchmarks with google-perftools separately, the additional time with tensorstore seems to be as follows:

(a) blosc_decompress: 11.12 seconds (tensorstore) / 8.15 seconds (zarr-python)
This accounts for most of the difference (in blosc_internal_bshuf_shuffle_bit_eightelem_scal specifically). More investigation will be needed to determine the source of the discrepancy here. The two likely explanations that come to mind are:

  1. Difference in generated code quality due to different compiler versions / flags used. (Note that because the element size is 1, the avx2 and sse2 implementations actually don't apply which may mean that compiler optimizations have a larger impact.)
  2. Cache effects due to additional memset and memcpy done by tensorstore. Because blosc is decoding a 20MB chunk this seems rather unlikely but it is hard to rule it out entirely without further testing.

(b) An additional ~1 second is spent by tensorstore on an unnecessary memset of the output buffer used by blosc. This is pure overhead and we should be able to fix this relatively easily.

(c) An additional ~1 second is spent by tensorstore on a memcpy to the destination array from the chunk cache. Currently tensorstore always decodes first to the chunk cache, but we do have some work in progress to bypass the chunk cache in cases like this where the chunk cache has a total_bytes_limit of 0 and the read covers the entire chunk.

The additional memset and memcpy have a particularly strong impact on this benchmark because of the very high compression ratio --- 100 to 1.

We will definitely look into these 3 causes, and try to get to the bottom of the blosc shuffle performance discrepancy, and remove the unnecessary memset and memcpy.

I'll note that this benchmark seems to be obviously very useful for identifying sources of overhead like these, but because of the very high compression ratio (meaning the I/O is negligible) and the fact that it involves just reading a single complete chunk at a time, it essentially just tests chunk decoding and not any of the indexing or I/O functionality of the zarr implementation. If the benchmark matches the actual use case then that is the right thing to test, though.

@jeromekelleher
Copy link
Contributor Author

Fantastic, thanks for looking into this @jbms! I would love to include the Tensorstore benchmarks in the paper because cross language support and the potential to integrate with existing C++ based tools is a key part of our story. If you can help us with the Tensorstore overhead here we'd be very happy to include you (and anyone else involved) as authors on this paper.

I think the benchmark is a useful one, in that it follows a very common access pattern for the data, and implements the core allele classification operation that will be used a great deal, e.g., in dataset quality control. Certainly the compression ratios are realistic, and what we observe for real data.

copybara-service bot pushed a commit to google/tensorstore that referenced this issue Oct 31, 2024
This eliminates the unnecessary memset (but not the unnecessary
copy) described here:
sgkit-dev/vcf-zarr-publication#161 (comment)

PiperOrigin-RevId: 691924460
Change-Id: I2cfe01659a6088278228763b7a36953125cf2380
@jeromekelleher
Copy link
Contributor Author

Just noting here that I've upgraded from Python tensorstore 0.1.64 to 0.1.68 and it hasn't made much difference unfortunately. The changes may have shaved a few seconds off, but we're still looking at ~80 seconds using Tensorstore Python vs ~30 seconds with Zarr python.

@jeromekelleher
Copy link
Contributor Author

Running some more profiling, here's what we get from perf record -e cpu-clock on Zarr python:

Samples: 176K of event 'cpu-clock', Event count (approx.): 44140500000                                                                                                                                                                        
Overhead  Command  Shared Object                                     Symbol                                                                                                                                                                   
  22.61%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] blosc_internal_bshuf_shuffle_bit_eightelem_scal                                                                                                                     ◆
   8.44%  python3  libopenblasp-r0-01191904.3.27.so                  [.] blas_thread_server                                                                                                                                                  ▒
   8.10%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc22111                                                                                                                                                  ▒
   6.83%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc2211e                                                                                                                                                  ▒
   4.25%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] blosc_internal_bshuf_trans_byte_bitrow_sse2                                                                                                                         ▒
   3.90%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc2211b                                                                                                                                                  ▒
   3.71%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc22105                                                                                                                                                  ▒
   3.70%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc220f9                                                                                                                                                  ▒
   3.67%  python3  [JIT] tid 693348                                  [.] 0x00007f97bdc2210f                                                                                                                                                  ▒
   2.50%  python3  [kernel.kallsyms]                                 [k] __schedule                                                                                                                                                          ▒
   2.40%  python3  [kernel.kallsyms]                                 [k] syscall_enter_from_user_mode                                                                                                                                        ▒
   2.27%  python3  libc-2.31.so                                      [.] __sched_yield                                                                                                                                                       ▒
   2.08%  python3  blosc.cpython-39-x86_64-linux-gnu.so              [.] ZSTD_decompressBlock_internal                                   

Drilling in then on blosc_internal_bshuf_shuffle_bit_eightelem_scal we get

Samples: 176K of event 'cpu-clock', 4000 Hz, Event count (approx.): 44140500000                                                                                                                                                               
blosc_internal_bshuf_shuffle_bit_eightelem_scal  /home/jk/.local/lib/python3.9/site-packages/numcodecs/blosc.cpython-39-x86_64-linux-gnu.so [Percent: local period]                                                                           
  5.12 │       xor    %rax,%rdx                                                                                                                                                                                                              ▒
  0.11 │       mov    %rdx,%rax                                                                                                                                                                                                              ▒
  1.15 │       shr    $0xe,%rax                                                                                                                                                                                                              ▒
  0.07 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  4.92 │       and    %rbp,%rax                                                                                                                                                                                                              ▒
  0.14 │       xor    %rax,%rdx                                                                                                                                                                                                              ▒
  1.38 │       shl    $0xe,%rax                                                                                                                                                                                                              ▒
  0.16 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  4.88 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.10 │       shr    $0x1c,%rdx                                                                                                                                                                                                             ▒
  1.82 │       xor    %rax,%rdx                                                                                                                                                                                                              ▒
  1.96 │       and    $0xf0f0f0f0,%edx                                                                                                                                                                                                       ▒
  5.22 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  4.24 │       shl    $0x1c,%rdx                                                                                                                                                                                                             ▒
  1.28 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  5.25 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.61 │       mov    %al,(%rcx)                                                                                                                                                                                                             ▒
  5.15 │       shr    $0x8,%rdx                                                                                                                                                                                                              ▒
  0.16 │       mov    %dl,(%rcx,%r9,1)                                                                                                                                                                                                       ▒
  5.91 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.06 │       shr    $0x10,%rdx                                                                                                                                                                                                             ▒
  0.20 │       mov    %dl,(%rcx,%rbx,1)                                                                                                                                                                                                      ▒
  5.72 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.05 │       shr    $0x18,%rdx                                                                                                                                                                                                             ▒
  0.17 │       mov    %dl,(%rcx,%r10,1)                                                                                                                                                                                                      ▒
  6.05 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.06 │       shr    $0x20,%rdx                                                                                                                                                                                                             ▒
  0.17 │       mov    %dl,(%rcx,%r11,1)                                                                                                                                                                                                      ▒
  5.86 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.07 │       shr    $0x28,%rdx                                                                                                                                                                                                             ▒
  0.22 │       mov    %dl,(%rcx,%r15,1)                                                                                                                                                                                                      ▒
  5.94 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  0.05 │       shr    $0x38,%rax                                                                                                                                                                                                             ▒
  0.20 │       shr    $0x30,%rdx                                                                                                                                                                                                             ▒
  5.45 │       mov    %dl,(%rcx,%r14,1)                                                                                                                                                                                                      ▒
  1.33 │       mov    %al,(%rcx,%r13,1)                                                                                                                                                                                                      ▒
  5.03 │       add    %rdi,%rcx                                                                                                                                                                                                              ▒
  1.11 │       lea    (%r8,%rcx,1),%rax                                                                                                                                                                                                      ▒
  0.06 │       cmp    %rax,-0x28(%rsp)                                                                                                                                                                                                       ▒
       │     ↑ ja     c0                                                                                                                                                                                                                     ▒
       │       addq   $0x8,-0x20(%rsp)                                                                                                                                                                                                       ▒
  0.00 │       mov    -0x20(%rsp),%rax                                                                                                                                                                                                       ▒
       │       cmp    %rax,%rdi                                                                                                                                                                                                              ▒
       │     ↑ ja     98                                                                                                                                                                                                                     ▒
  0.01 │17f:   mov    -0x28(%rsp),%rax                                                                                                                                                                                                       ▒
       │184:   pop    %rbx                                                                                                                                                                                                                   ▒
  0.01 │       pop    %rbp                                                                                                                                                                                                                   ▒
       │       pop    %r12                                                                                                                                                                                                                   ▒
       │       pop    %r13                                                                                                                                                                                                                   ▒
       │       pop    %r14                                                                                                                                                                                                                   ◆
       │       pop    %r15                                                                                                                                                                                                                   ▒
       │     ← retq                                                                                                                                                                                                                          ▒
       │18f:   mov    $0xffffffffffffffb0,%rax                                                                                                                                                                                               ▒
       │     ↑ jmp    184                                                              

@jeromekelleher
Copy link
Contributor Author

Here's the same for tensorstore Python (latest version)

Samples: 366K of event 'cpu-clock', Event count (approx.): 91703500000                                                                                                                                                                        
Overhead  Command         Shared Object                                        Symbol                                                                                                                                                         
  30.86%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so          [.] blosc_internal_bshuf_shuffle_bit_eightelem_scal                                                                                                            
   8.28%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b60111                                                                                                                                         
   7.52%  ts_pool_worker  libc-2.31.so                                         [.] __memmove_sse2_unaligned_erms                                                                                                                              
   6.97%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b6011e                                                                                                                                         
   4.72%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so          [.] blosc_internal_bshuf_trans_byte_bitrow_sse2                                                                                                                
   4.07%  python3         libopenblasp-r0-01191904.3.27.so                     [.] blas_thread_server                                                                                                                                         
   4.02%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b6011b                                                                                                                                         
   3.76%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b600f9                                                                                                                                         
   3.74%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b60105                                                                                                                                         
   3.72%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b6010f                                                                                                                                         
   2.28%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so          [.] ZSTD_decompressBlock_internal.part.0                                                                                                                       
   1.97%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b600f4                                                                                                                                         
   1.89%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b60101                                                                                                                                         
   1.84%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b6010b                                                                                                                                         
   1.62%  python3         [JIT] tid 693725                                     [.] 0x00007f2a19b60119                                                                                                                                         
   1.12%  python3         [kernel.kallsyms]                                    [k] __schedule                                                                                                                                                 
   1.09%  python3         [kernel.kallsyms]                                    [k] syscall_enter_from_user_mode                                                                                                                               
   1.03%  python3         libc-2.31.so                                         [.] __sched_yield                                                                                                                                              
   1.03%  ts_pool_worker  libc-2.31.so                                         [.] __memset_erms                                                                                                                                              
   0.74%  python3         python3.9                                            [.] _PyEval_EvalFrameDefault                                                                                                                                   
   0.36%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so          [.] HUF_decompress4X1_usingDTable_internal                                                                                                                     
   0.30%  python3         [kernel.kallsyms]                                    [k] do_sched_yield                                                                                                                                             
   0.25%  python3         _tskit.cpython-39-x86_64-linux-gnu.so                [.] tsk_table_collection_check_integrity                                                                                                                       
   0.21%  python3         [kernel.kallsyms]                                    [k] clear_page_erms                             

Drilling in to blosc_internal_bshuf_shuffle_bit_eightelem_scal again (and selecting the hottest instruction):

Samples: 366K of event 'cpu-clock', 4000 Hz, Event count (approx.): 91703500000                                                                                                                                                               
blosc_internal_bshuf_shuffle_bit_eightelem_scal  /home/jk/.local/lib/python3.9/site-packages/tensorstore/_tensorstore.cpython-39-x86_64-linux-gnu.so [Percent: local period]                                                                  
Percent│       mov    %r14,%r8                                                                                                                                                                                                               ▒
       │       mov    %r15,%r11                                                                                                                                                                                                              ▒
       │       shr    $0x3,%rdi                                                                                                                                                                                                              ▒
       │       sub    %rdi,%r8                                                                                                                                                                                                               ▒
  0.00 │       sub    %rdi,%r11                                                                                                                                                                                                              ▒
       │       add    -0x30(%rbp),%r8                                                                                                                                                                                                        ▒
       │       cmp    %r15,%r9                                                                                                                                                                                                               ▒
       │     ↓ jbe    eb                                                                                                                                                                                                                     ▒
       │       nop                                                                                                                                                                                                                           ▒
  0.06 │ 70:   mov    (%r8,%rdi,1),%rdx                                                                                                                                                                                                      ▒
  0.18 │       mov    %rdx,%rcx                                                                                                                                                                                                              ▒
  4.72 │       shr    $0x7,%rcx                                                                                                                                                                                                              ▒
  0.01 │       xor    %rdx,%rcx                                                                                                                                                                                                              ▒
  0.09 │       and    %r13,%rcx                                                                                                                                                                                                              ▒
  0.65 │       xor    %rcx,%rdx                                                                                                                                                                                                              ▒
  4.77 │       shl    $0x7,%rcx                                                                                                                                                                                                              ▒
  0.22 │       xor    %rcx,%rdx                                                                                                                                                                                                              ▒
  1.12 │       mov    %rdx,%rcx                                                                                                                                                                                                              ▒
  0.06 │       mov    %rdx,%rax                                                                                                                                                                                                              ▒
  3.89 │       shr    $0xe,%rcx                                                                                                                                                                                                              ▒
  1.06 │       xor    %rdx,%rcx                                                                                                                                                                                                              ▒
  1.91 │       and    %r12,%rcx                                                                                                                                                                                                              ▒
  3.21 │       xor    %rcx,%rax                                                                                                                                                                                                              ▒
  5.06 │       shl    $0xe,%rcx                                                                                                                                                                                                              ▒
  0.41 │       xor    %rcx,%rax                                                                                                                                                                                                              ▒
  3.70 │       lea    (%rbx,%rdi,1),%rcx                                                                                                                                                                                                     ▒
  0.00 │       mov    %rax,%rdx                                                                                                                                                                                                              ▒
  1.28 │       shr    $0x1c,%rdx                                                                                                                                                                                                             ▒
  3.66 │       xor    %rax,%rdx                                                                                                                                                                                                              ▒
  4.15 │       and    $0xf0f0f0f0,%edx                                                                                                                                                                                                       ▒
  4.40 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  5.34 │       shl    $0x1c,%rdx                                                                                                                                                                                                             ▒
  0.86 │       xor    %rdx,%rax                                                                                                                                                                                                              ▒
  4.76 │       mov    $0x8,%edx                                                                                                                                                                                                              ▒
  0.00 │       nop                                                                                                                                                                                                                           ▒
  1.30 │ d0:   mov    %al,(%rcx)                                                                                                                                                                                                             ▒
 38.16 │       shr    $0x8,%rax                                                                                                                                                                                                              ▒
  0.02 │       add    %rsi,%rcx                                                                                                                                                                                                              ▒
  0.02 │       sub    $0x1,%rdx                                                                                                                                                                                                              ▒
       │     ↑ jne    d0                                                                                                                                                                                                                     ▒
  0.16 │       add    %r10,%rdi                                                                                                                                                                                                              ▒
  4.75 │       lea    (%r11,%rdi,1),%rax                                                                                                                                                                                                     ▒
  0.00 │       cmp    %rax,%r9                                                                                                                                                                                                               ▒
       │     ↑ ja     70                                                                                                                                                                                                                     ◆
       │ eb:   add    $0x8,%r14                                                                                                                                                                                                              ▒
  0.00 │       cmp    %r14,%r10                                                                                                                                                                                                              ▒
       │     ↑ ja     50                                                                                                                                                                                                                     ▒
       │ f8:   mov    %r9,%rax                                                                                                                                                                                                               ▒
       │ fb:   pop    %rbx                                                                                                                                                                                                                   ▒
  0.00 │       pop    %r12                                                                                                                                                                                                                   ▒
       │       pop    %r13                                                                                                                                                                                                                   ▒
       │       pop    %r14                                                                                                                                                                                                                   ▒
       │       pop    %r15                                                                                                                                                                                                                   ▒
       │       pop    %rbp                                                                                                                                                                                                                   ▒
       │     ← retq                                                                                                                                                                                                                          ▒
       │106:   mov    $0xffffffffffffffb0,%rax                                                                                                                                                                                               ▒
       │     ↑ jmp    fb                                                                                 

@jeromekelleher
Copy link
Contributor Author

I'm out of my depth here with the assembly level stuff, but it looks like the big difference is the shr $0x8,%rax instruction (which is weird, as it's a bit shift??).

Does this give you any insights as to what's going on @jbms?

@laramiel
Copy link

laramiel commented Nov 19, 2024

That tensorstore loop is not unrolled; the python loop is. https://godbolt.org/z/T1bh36bcK
We should check our build optimization settings as that looks like -O1 vs. python using -O2 at least.

Edit: it appears that GCC doesn't unroll this loop at -O2, but clang does. GCC unrolls the loop at -O3 or when passed -funroll-loops or when a #pragma GCC unroll 8 is added

@jbms
Copy link

jbms commented Nov 19, 2024

Thanks for producing those profiles, and nice work analyzing that @laramiel

@jeromekelleher
Copy link
Contributor Author

FWIW, it's not clear to me where numcodecs gets its compiler flags set, but my best guess is that is follows the Python values, which might look like

>>> import sysconfig
>>> sysconfig.get_config_vars('CFLAGS')
['-Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g       -fstack-protector-strong -Wformat -Werror=format-security  -g -fwrapv -O2   ']

I think It's built on the manylinux2014 Docker image, which is based on GCC 10.

That would predict that this loop is not unrolled according to @laramiel's analysis above though.

copybara-service bot pushed a commit to google/tensorstore that referenced this issue Nov 20, 2024
See sgkit-dev/vcf-zarr-publication#161

The default bazel -c opt build mode is -O2, vs. the default cmake Release
optimization mode of -O3.  It turns out that gcc doesn't unroll the underlying
blosc loop at -O3, so this sets the level to -O3 when building wheels.

PiperOrigin-RevId: 698176524
Change-Id: Iea9c47e1cb96d40169d2b28af93cf53b33e6991c
@laramiel
Copy link

I pushed v0.1.69 which should now be built with -O3. PTAL.

@jbms
Copy link

jbms commented Nov 27, 2024

Thanks @laramiel. With tensorstore 0.1.69 I am seeing about 11.54 seconds compared to 10.54 with zarr-python; tensorstore 0.1.68 is about 2 seconds slower. Per the previous profiling results, the remaining 1 second discrepancy is presumably due to the extra memcpy that we have not yet eliminated but plan to work on.

@laramiel
Copy link

You could try it with https://google.github.io/tensorstore/kvstore/file/index.html#json-Context.file_io_memmap and see if that helps.

@jeromekelleher
Copy link
Contributor Author

I've tried it out on my benchmarks @laramiel and unfortunately it's made no noticeable difference:

Samples: 367K of event 'cpu-clock', Event count (approx.): 91791750000                                                                                                                                                                        
Overhead  Command         Shared Object                                     Symbol                                                                                                                                                            
  27.26%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so       [.] blosc_internal_bshuf_shuffle_bit_eightelem_scal                                                                                                               
   8.56%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb5111                                                                                                                                            
   8.23%  ts_pool_worker  libc-2.31.so                                      [.] __memmove_sse2_unaligned_erms                                                                                                                                 
   7.15%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb511e                                                                                                                                            
   4.83%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so       [.] blosc_internal_bshuf_trans_byte_bitrow_sse2                                                                                                                   
   4.14%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb511b                                                                                                                                            
   4.07%  python3         libopenblasp-r0-01191904.3.27.so                  [.] blas_thread_server                                                                                                                                            
   3.99%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb5105                                                                                                                                            
   3.96%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb50f9                                                                                                                                            
   3.91%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb510f                                                                                                                                            
   2.45%  ts_pool_worker  _tensorstore.cpython-39-x86_64-linux-gnu.so       [.] ZSTD_decompressSequences_default.constprop.0                                                                                                                  
   2.05%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb50f4                                                                                                                                            
   1.91%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb510b                                                                                                                                            
   1.89%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb5101                                                                                                                                            
   1.63%  python3         [JIT] tid 935979                                  [.] 0x00007f2e57eb5119                            

Annotating blosc function:

 Samples: 367K of event 'cpu-clock', 4000 Hz, Event count (approx.): 91791750000                                                                                                                                                               
blosc_internal_bshuf_shuffle_bit_eightelem_scal  /home/jk/.local/lib/python3.9/site-packages/tensorstore/_tensorstore.cpython-39-x86_64-linux-gnu.so [Percent: local period]                                                                  
  0.92 │       shl    $0xe,%rax                                                                                                                                                                                                               
  0.37 │       xor    %rdx,%rax                                                                                                                                                                                                               
  5.36 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.34 │       shr    $0x1c,%rdx                                                                                                                                                                                                              
  1.87 │       xor    %rax,%rdx                                                                                                                                                                                                               
  2.99 │       and    $0xf0f0f0f0,%edx                                                                                                                                                                                                        
  5.44 │       xor    %rdx,%rax                                                                                                                                                                                                               
  4.75 │       shl    $0x1c,%rdx                                                                                                                                                                                                              
  0.81 │       xor    %rdx,%rax                                                                                                                                                                                                               
  4.92 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.77 │       mov    %al,(%rcx)                                                                                                                                                                                                              
  5.28 │       shr    $0x8,%rdx                                                                                                                                                                                                               
  0.06 │       mov    %dl,(%rcx,%r9,1)                                                                                                                                                                                                        
  5.53 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.17 │       shr    $0x10,%rdx                                                                                                                                                                                                              
  0.06 │       mov    %dl,(%rcx,%rbx,1)                                                                                                                                                                                                       
  5.57 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.15 │       shr    $0x18,%rdx                                                                                                                                                                                                              
  0.06 │       mov    %dl,(%rcx,%r10,1)                                                                                                                                                                                                       
  5.60 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.17 │       shr    $0x20,%rdx                                                                                                                                                                                                              
  0.06 │       mov    %dl,(%rcx,%r11,1)                                                                                                                                                                                                       
  5.79 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.15 │       shr    $0x28,%rdx                                                                                                                                                                                                              
  0.05 │       mov    %dl,(%rcx,%r15,1)                                                                                                                                                                                                       
  5.68 │       mov    %rax,%rdx                                                                                                                                                                                                               
  0.15 │       shr    $0x38,%rax                                                                                                                                                                                                              
  0.05 │       shr    $0x30,%rdx                                                                                                                                                                                                              
  5.29 │       mov    %dl,(%rcx,%r14,1)                                                                                                                                                                                                       
  0.72 │       mov    -0x38(%rbp),%rdx                                                                                                                                                                                                        
  0.26 │       mov    %al,(%rcx,%rdx,1)                                                                                                                                                                                                       
  6.55 │       add    %rdi,%rcx                                                                                                                                                                                                               
  0.17 │       lea    (%r8,%rcx,1),%rax                                                                                                                                                                                                       
  0.07 │       cmp    %rax,-0x30(%rbp)                                                                                                                                                                                                        
       │     ↑ ja     c0                                                                                                                                                                                                                      
  0.00 │       addq   $0x8,-0x40(%rbp)                                                                                                                                                                                                        
  0.00 │       mov    -0x40(%rbp),%rax                                                                                                                                                                                                        
       │       cmp    %rax,%rdi                                                                                                                                                                                                               
       │     ↑ ja     98                                                                                                                                                                                                                      
       │17f:   mov    -0x30(%rbp),%rax                                                                                                                                                                                                        
       │183:   pop    %rbx                                                                                                                                                                                                                    
       │       pop    %r12                                                                                                                                                                                                                    
  0.00 │       pop    %r13                                                                                                                                                                                                                    
       │       pop    %r14                                                                                                                                                                                                                    
       │       pop    %r15                                                                                                                                                                                                                    
       │       pop    %rbp                                                                                                                                                                                                                    
       │     ← retq                                                                                                                                                                                                                           
       │18e:   mov    $0xffffffffffffffb0,%rax                                                                                                                                                                                                
       │     ↑ jmp    183                                                                                                                      

Version 0.1.69 installed from pip:

$ python3 -m pip show tensorstore
Name: tensorstore
Version: 0.1.69

@laramiel
Copy link

That assembly is unrolled and is substantially the same as the python version; there's a little difference between some instruction order, but nothing stands out to me there.

@jbms
Copy link

jbms commented Nov 27, 2024

One other possibility that occurs to me --- @jeromekelleher are you running the benchmark on a machine with NUMA?

@jeromekelleher
Copy link
Contributor Author

I am indeed @jbms, a dual socket Xeon. I'll paste in the numa details later.

@jbms
Copy link

jbms commented Nov 27, 2024

The NUMA-related issue I suspected would be that the output array from blosc decompression happens to be on a different NUMA node from the CPU executing the decompression. In zarr-python everything is single-threaded so it is unlikely to happen, while tensorstore uses multiple threads making it more plausible. However, looking at the code in tensorstore, I see that the output array gets allocated in the same thread that does the decompression, which I think makes a NUMA-related issue less likely.

In any case, can you try running the benchmark pinned to a single numa node in order to rule out this possibility?

@jeromekelleher
Copy link
Contributor Author

I've tried pinning to numa node 0 and 1 (with numactl -m) and oddly node 0 seems to run a bit slower than node 1 (~82 seconds vs 76 seconds), but I think it's probably something to do with the cooling on one of the CPUs being a bit better than the other (I've observed frequency limiting messages from the kernel at various times). But it's nowhere near big enough to explain the difference to the Zarr python version (~30 seconds).

Previous experience with trying to tune numa performance on this box would suggest a max of 5-10% perf difference between optimal numa usage vs not, so I doubt that's what's going on here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants