Repository: cupy/cupy

Issue:
Using `-Xfatbin=-compress-all` for kernel compilation
Not sure how relevant this is for how CuPy compiles things, but something we have been exploring in RAPIDS that may be of interest is building with `-Xfatbin=-compress-all`. In some cases this can make for quite dramatic reductions in compiled kernel sizes. For an example please see PR ( https://github.com/rapidsai/cudf/pull/7583 ). Again not sure if this is relevant for how CuPy is building things, but just wanted to mention it in case it is.


Discussion:
Thanks for sharing this! I tried this locally (my environment uses `export CUPY_NVCC_GENERATE_CODE=arch=compute_60,code=sm_60;arch=compute_70,code=sm_70;arch=compute_80,code=sm_80`) and measured `du -csh cupy cupyx cupy_backends`:

* with compression: 95MB
* without compression: 102MB

In CuPy it seems the effect is limited because `nvcc` is only used for a few modules (cub/thrust/random).

Moreover, after compressing files into a wheel (ie ZIP), the **compressed** one was 262 KB larger.
@kmaehashi Should we reopen this issue? I see that while this flag might not help reduce the wheel size, it helps reduce the total size taken by CuPy after decompression/installation: https://github.com/cupy/cupy/pull/6941#issuecomment-1204005112.
There should be some (maybe one time at the first CUDA execution in CuPy?) overhead when compression is enabled, so I'm wondering it's worth doing only for saving 17MB. I guess this also increases the build time?
It's another 15-25M for the `cub` module and I didn't check other modules. I don't see the build time increased, and the runtime overhead for decompression is negligible based on an internal discussion (though likely outdated). For reference, all RAPIDS binaries are built with this option.

Though, to be fair, it does not always help. For example, with [this feature](https://github.com/cupy/cupy/issues/6699#issuecomment-1120348863) I see `cub` inflates to 190M+, and the compression only brings it down to 170M+, not a significant change. 
OK, let's try it and see how it would work :)

Resolving patch:
diff --git a/install/cupy_builder/_compiler.py b/install/cupy_builder/_compiler.py
--- a/install/cupy_builder/_compiler.py
+++ b/install/cupy_builder/_compiler.py
@@ -215,7 +215,7 @@ def _compile_unix_nvcc(self, obj: str, src: str, ext: Extension) -> None:
 
         cuda_version = self._context.features['cuda'].get_version()
         postargs = _nvcc_gencode_options(cuda_version) + [
-            '-O2', '--compiler-options="-fPIC"']
+            '-Xfatbin=-compress-all', '-O2', '--compiler-options="-fPIC"']
         if cuda_version >= 11020:
             postargs += ['--std=c++14']
             num_threads = int(os.environ.get('CUPY_NUM_NVCC_THREADS', '2'))
@@ -255,7 +255,8 @@ def compile(self, obj: str, src: str, ext: Extension) -> None:
         compiler_so = build.get_nvcc_path()
         cc_args = self._get_preprocess_options(ext) + ['-c']
         cuda_version = self._context.features['cuda'].get_version()
-        postargs = _nvcc_gencode_options(cuda_version) + ['-O2']
+        postargs = _nvcc_gencode_options(cuda_version) + [
+            '-Xfatbin=-compress-all', '-O2']
         if cuda_version >= 11020:
             # MSVC 14.0 (2015) is deprecated for CUDA 11.2 but we need it
             # to build CuPy because some Python versions were built using it.


Test patch: