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

[BUILD] Add caching to CMake #8373

Merged
merged 4 commits into from
Aug 1, 2021

Conversation

electriclilies
Copy link
Contributor

In this PR I add code to cmake/config.cmake and CMakeLists.txt to cache builds. This makes switching between branches less painful because you don't have to build from scratch every time you switch branches.

@mbrookhart @tkonolige @csullivan Please take a look!

Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

LGTM. One tip: ccache-3.x doesn't support nvcc well, so CUDA kernels may never hit the cache and still need to be re-compiled every time. Using ccache 4.0+ can resolve this issue. Since Ubuntu 20 still uses ccache 3.x, it's hightly possible that users may need to manually install 4.0+.

@mbrookhart
Copy link
Contributor

LGTM. One tip: ccache-3.x doesn't support nvcc well, so CUDA kernels may never hit the cache and still need to be re-compiled every time. Using ccache 4.0+ can resolve this issue.

Can we add that to the documentation on the option?

@mehrdadh
Copy link
Member

@electriclilies this is great! thanks for adding this feature.

@electriclilies
Copy link
Contributor Author

@mbrookhart Added it :)
@mehrdadh This snippet has been circulating for a while, I got it from @csullivan originally :) I figured I should put it in the codebase after it got wiped from my local config.cmake file when I built using docker and then I had to track down the snippet again 🤣

@electriclilies
Copy link
Contributor Author

This PR has failed a few times on the windows build. I'm not sure if this is caused by this PR or not; we should keep an eye on it and perhaps set the default behavior to not cache if it is causing flaky tests.

@junrushao
Copy link
Member

Thanks Lily for the great work! Yeah I am using ccache a lot as well when working on the TVM project, but with a more global flag, like

set(CMAKE_C_COMPILER_LAUNCHER /path/to/ccache)
set(CMAKE_CXX_COMPILER_LAUNCHER /path/to/ccache)
set(CMAKE_CUDA_COMPILER_LAUNCHER /path/to/ccache)

What's the difference between your approach (set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ) and the one above?

@tqchen
Copy link
Member

tqchen commented Jun 30, 2021

Thanks @electriclilies . One thing that might be helpful here is to streamline the option definition. The current proposed option is closer to AUTO mode(enable if found, but disable if not).

Here are how we interpret values to keep things consistent with our previous options:

  • AUTO: automatically detect and if found use it
  • /path/to/ccache: set the ccache to the correct ccache path being specified
  • OFF: disable ccache
  • ON: search and report an error if ccache is not found

We should also look a bit into which way is more standard, since I also see set(CMAKE_C_COMPILER_LAUNCHER /path/to/ccache) being used a bit more frequently.

The use of ccache will likely cause the history cache growing problem in the CI when the node disk space is limited. Because the cost of build is normally smaller compared to testing, we will likely want to disable ccache in the CI setups in the task_config_build_xyz.sh https://github.com/apache/tvm/blob/main/tests/scripts/task_config_build_arm.sh

@tqchen tqchen changed the title Add caching to CMake [Build] Add caching to CMake Jun 30, 2021
@tqchen tqchen changed the title [Build] Add caching to CMake [BUILD] Add caching to CMake Jun 30, 2021
@mehrdadh
Copy link
Member

I agree with @tqchen about the CI. It's better to always build from scratch in CI.

@electriclilies
Copy link
Contributor Author

electriclilies commented Jun 30, 2021

Thanks for the feedback @tqchen @junrushao1994. I'll change the default to AUTO, as suggested, and disable this option in CI.
Regarding which is more common-- I don't know which approach is more commonly used. One advantage to the approach I propose is that the user doesn't have to enter the path to the ccache, so they can take advantage of it without editing the config.cmake file.

@tqchen
Copy link
Member

tqchen commented Jun 30, 2021

Thanks @electriclilies , by standard options, I actually meant we should implement the auto feature(please see the suggested comments) via

  • set(CMAKE_C_COMPILER_LAUNCHER /path/to/ccache) vs set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE /path/to/ccache)

seems they are two ways to do the same thing(see some related issues Tencent/rapidjson#1794 seems to indicate that we should use the other one), but we are not sure

CMakeLists.txt Outdated
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
message(STATUS "Enabling ccache")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
Copy link
Member

Choose a reason for hiding this comment

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

would be grgeat to check if we can use set CXX_COMPILER_LAUNCHER here and get the same goal

CMakeLists.txt Outdated

if(USE_CCACHE)
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
Copy link
Member

Choose a reason for hiding this comment

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

We might want to feed the result of find_program to the variable, in case ccache is not in path but find_program manages to find it

@electriclilies
Copy link
Contributor Author

After some thought, I think that it is actually better to model the options in this case after the options for ROCM runtime, which looks like this:

# Whether enable ROCM runtime
#
# Possible values:
# - ON: enable ROCM with cmake's auto search
# - OFF: disable ROCM
# - /path/to/rocm: use specific path to rocm
set(USE_ROCM OFF)

How does this sound for the possible options?

# Possible values:
# - ON: enable ccache by searching for the path to ccache
# - OFF: disable ccache
# - /path/to/ccache: use specific path to ccache

@tqchen
Copy link
Member

tqchen commented Jul 1, 2021

I agree, in the case of rocm runtime, an error will be raised if rocm is ON and we did not found the runtime. So the current behavior is still closer to AUTO.

# Possible values:
# - AUTO: search for path to ccache, disable if not found.
# - ON: enable ccache by searching for the path to ccache, report an error if not found
# - OFF: disable ccache
# - /path/to/ccache: use specific path to ccache

@electriclilies
Copy link
Contributor Author

electriclilies commented Jul 7, 2021

@tqchen @junrushao1994 I updated the options and changed the method of setting the path to ccache to set(CXX_COMPILER_LAUNCHER /path/to/ccache). I'm not sure if you meant that I need to do that in addition to set(CMAKE_C_COMPILER_LAUNCHER /path/to/ccache).
Let me know what you think

message(STATUS "Setting ccache path to " "${PATH_TO_CCACHE}")
endif()
# Set the flag for ccache
set(CXX_COMPILER_LAUNCHER PATH_TO_CCACHE)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should probably check if these are already set so you don't override user options.

Copy link
Contributor Author

@electriclilies electriclilies Jul 7, 2021

Choose a reason for hiding this comment

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

How do I do that? Also if it is already set, do I just not set it? Or somehow try to concatenate user input with the path to ccache?

Copy link
Member

Choose a reason for hiding this comment

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

In our specific case I think overriding CXX_COMPILER_LAUNCHER should not be a bad expected behavior, so we can leave it as it is for now

Copy link
Member

@tqchen tqchen left a comment

Choose a reason for hiding this comment

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

Thanks @electriclilies ! One last minor comment and we are good to go

CMakeLists.txt Outdated
else()
message(STATUS "Didn't find the path to CCACHE, disabling ccache")
endif(CCACHE_FOUND)
elseif("${USE_CCACHE}" STREQUAL "ON")
Copy link
Member

Choose a reason for hiding this comment

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

change to use

set(IS_TRUE_PATTERN "^[Oo][Nn]$|^[1-9][0-9]*$|^[Tt][Rr][Uu][Ee]$|^[Yy][Ee][Ss]$|^[Yy]$")

Copy link
Member

Choose a reason for hiding this comment

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

see

if(${USE_ETHOSN_HW} MATCHES ${IS_TRUE_PATTERN})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tqchen Done!

@tqchen tqchen added the status: need update need update based on feedbacks label Jul 16, 2021
Fix formatting

Add comment about nvcc

Change default to AUTO

More progress

Add auto as a mode

Disable ccache in CI

add-cache-to-cmake

Fix typo
@electriclilies
Copy link
Contributor Author

@tqchen @junrushao1994 This is passing, could you take a look and make sure I addressed all the comments?

@junrushao
Copy link
Member

I think the PR is in pretty good state. Just one question: Is it intended behavior to disable ccache in our CI? I thought we are using it right now

@electriclilies
Copy link
Contributor Author

@junrushao1994

@tqchen said:

The use of ccache will likely cause the history cache growing problem in the CI when the node disk space is limited. Because the cost of build is normally smaller compared to testing, we will likely want to disable ccache in the CI setups in the task_config_build_xyz.sh https://github.com/apache/tvm/blob/main/tests/scripts/task_config_build_arm.sh

Based on this, it is my understanding that we do want to disable ccache in CI. I'm not sure whether we are using it right now. I think it would be good to double check that this is the intended behavior before it goes in given all the issues we've had with CI in the last week 😎

@electriclilies
Copy link
Contributor Author

electriclilies commented Jul 30, 2021

@tqchen @junrushao1994 Additionally, if I do make this change and we don't update the CI docker images, will ccache just be enabled on CI until the docker images are updated? That might be suboptimal if it does cause the memory usage to go up
cc @mbrookhart

@junrushao
Copy link
Member

Thanks Lily for the explanation! The PR looks good to me :-)

@junrushao junrushao merged commit 9f29e2a into apache:main Aug 1, 2021
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
* ccache

* ccache

Fix formatting

Add comment about nvcc

Change default to AUTO

More progress

Add auto as a mode

Disable ccache in CI

add-cache-to-cmake

Fix typo

* Fix rebase

* flaky test
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
* ccache

* ccache

Fix formatting

Add comment about nvcc

Change default to AUTO

More progress

Add auto as a mode

Disable ccache in CI

add-cache-to-cmake

Fix typo

* Fix rebase

* flaky test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: need update need update based on feedbacks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants