Skip to content

MAINT Increase ccache hit rate on Azure Pipelines - #22213

Merged
thomasjpfan merged 15 commits into
scikit-learn:mainfrom
ogrisel:optimize-ccache-azure
Jan 17, 2022
Merged

thomasjpfan merged 15 commits into
scikit-learn:mainfrom
ogrisel:optimize-ccache-azure

Conversation

@ogrisel

@ogrisel ogrisel commented Jan 14, 2022 •

Copy link
Copy Markdown
Member

Azure does not seem to update existing cache entries with the same key.

Therefore it seems that the ccache folder contents can get stalled and the hit rate seems to diminish over time.

Base on my understanding of https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops it seems that if a segment of the key is a filename (and hopefully a directory?), the contents of the file (assumed to be relative to $(Pipeline.Workspace)) will be used to check if an updated version of the cache blob needs to be re-uploaded.

By putting the ccache foldername as a cache key segment, I hope that the cache will be updated whenever a compiled binary artifact has been updated.

EDIT: Azure's "Cache" step cannot automatically hash the contents of the ccache folder to be build an automatically invalidated key segment. Therefore I decided to use the ever incremented build nbumber at the last segment of the key and it seems to work as expected at the small expense of potential unnecessary uploads to the cache blob store.

Unused cached entries are automatically collected after a week so we should not waste too much resources.

Azure does not seem to update existing cache with the same name.
Therefore it seems that the ccache folder contents can get stalled
and the hit rate seems to diminish over time.

Base on my understanding of
https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops
it seems that if a segment of the key is a filename (and hopefully a
directory?), the contents of the file (assumed to be relative to
$(Pipeline.Workspace)) will be used to check if an updated version
of the cache blob needs to be re-uploaded.

By putting the ccache foldername as a cache key segment, I hope
that the cache will be updated whenever a compiled binary artifact
has been updated.
@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

Let us wait for this first CI run to complete before pushing a new commit to trigger a second consecutive run and see the impact in terms of build speed and check the logs to see if the update actually happens in the post-job ccache step and also check the hit rate in the call to ccache -s at the end of the install step.

@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

Note that I wrote ccache | "$(Agent.JobName)" instead of "ccache" | "$(Agent.JobName)" so that the ccache segment of the key is interpreted as a folder name instead of a static identifier.

@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

Ok so in the first build we had in the post-job ccache step:

Creating a pipeline cache artifact with the following fingerprint: `ccache|"Linux py37_conda_defaults_openblas"`
Cache item created.
Saved under the following scope: 5;d9c6f05f-7f58-4a17-b143-2a4e7ff015af;refs/pull/22213/merge;scikit-learn/scikit-learn
Saved item.

Not sure what it means...

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is consistent with the Azure Docs on ccache.

It looks like it works well for some of the runs such as py37_conda_forge_openblas_ubuntu_1804 with 48% hits in the cache.

Installs that use pip install -e . such as pylatest_pip_openblas_pandas has 11% hits. I think this is due to pip install -e . always creating a new env where NumPy headers are installed in a new tmp directory every time.

Also osx build does not use the cache. We can work on that in another PR.

@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

Installs that use pip install -e . such as pylatest_pip_openblas_pandas has 11% hits. I think this is due to pip install -e . always creating a new env where NumPy headers are installed in a new tmp directory every time.

Interesting analysis.

But before investigating that let me push a Cython code change to check that the cache gets invalidated in the post job step.

@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

The docker-based build will also need special attention to mount the ccache folder at the right location inside the container.

@ogrisel

ogrisel commented Jan 14, 2022

Copy link
Copy Markdown
Member Author

Hum, after the Cython code change, it seems that the post-job does not want to use the changed ccache folder contents to invalidate the existing cache:

Getting a pipeline cache artifact with one of the following fingerprints:
Fingerprint: `ccache|"Linux py37_conda_defaults_openblas"`
There is a cache hit: `ccache|"Linux py37_conda_defaults_openblas"`
Used scope: 5;d9c6f05f-7f58-4a17-b143-2a4e7ff015af;refs/pull/22213/merge;scikit-learn/scikit-learn
Missed on the following scopes: 5;d9c6f05f-7f58-4a17-b143-2a4e7ff015af;refs/heads/main;scikit-learn/scikit-learn
Cache with fingerprint `ccache|"Linux py37_conda_defaults_openblas"` already exists.
ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session 9a39a1be-d768-4611-99df-3d1569055078

Let me try to add the build number to the cache key.

@ogrisel

ogrisel commented Jan 14, 2022 •

Copy link
Copy Markdown
Member Author

I will also disable the tests to iterate faster. But probably another day as it's late here in Europe. Fill free to push to this PR if you want to experiment in the mean time.

@ogrisel

ogrisel commented Jan 17, 2022 •

Copy link
Copy Markdown
Member Author

Ok the cache hit rate has improved significantly for all the linux and macos builds. This can cut ~5 min of redundant compilation for most jobs.

Installs that use pip install -e . such as pylatest_pip_openblas_pandas has 11% hits. I think this is due to pip install -e . always creating a new env where NumPy headers are installed in a new tmp directory every time.

The hit rate for pylatest_pip_openblas_pandas is indeed only 18% with the latest config (vs 45% to 80% for the others). I am not sure what we can do about it because pylatest_pip_openblas_pandas intentionally tries to build in an temporary isolated environment from the build time dependencies specified in pyproject.toml.

For Windows, it might be either possible to configure ccache or clcache but that seems quite a lot more work to do and I don't have a windows VM handy to try to setup locally.

displayName: ccache
continueOnError: true
- script: >
mkdir -p $CCACHE_DIR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary to bootstrap after an invalidation otherwise one would get a file not found error in the post-job ccache step.

@ogrisel

ogrisel commented Jan 17, 2022

Copy link
Copy Markdown
Member Author

I am not sure what we can do about it because pylatest_pip_openblas_pandas intentionally tries to build in an temporary isolated environment from the build time dependencies specified in pyproject.toml.

Maybe it would be possible to write a CI-specific ccache config file to disable the hashing of the CWD (current working directory) to increased the cache hit rate when building from the pip-generated isolated temp build folder. However I fear this will be a lengthy trial and error process. I think this PR is useful enough to be merged as is without optimizing that last point.

@ogrisel ogrisel changed the title MAINT Try to increase ccache hit rate on Azure Pipelines MAINT Increase ccache hit rate on Azure Pipelines Jan 17, 2022
@thomasjpfan

Copy link
Copy Markdown
Member

Maybe it would be possible to write a CI-specific ccache config file to disable the hashing of the CWD (current working directory) to increased the cache hit rate when building from the pip-generated isolated temp build folder. However I fear this will be a lengthy trial and error process.

I have used CCACHE_BASEDIR in another project to change the "base directory" so paths can be relative. I have not tried it in the context of pip install . tho and it would take some experimenting to get right.

I agree that we have now is already improvement, so let's merge first and we can make more improvements later.

@thomasjpfan
thomasjpfan merged commit 0c65bbf into scikit-learn:main Jan 17, 2022
@ogrisel

ogrisel commented Jan 17, 2022

Copy link
Copy Markdown
Member Author

Note that the pylatest_conda_mkl_no_openmp build is still slow but because of the slow tests: #20427

@ogrisel
ogrisel deleted the optimize-ccache-azure branch January 17, 2022 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants