Repository: pytorch
Issue: Issues compiling `torch` with `mkl`
Labels: module: build,triaged,module: mkl,module: intel

I am trying to debug some build/compilation issues of `pytorch` using `mkl` as the `lapack`/`blas` provider.
See https://github.com/NixOS/nixpkgs/issues/269271 for downstream issue.

Attempting to build `pytorch` with `mkl` produces the following errors:
```
CMake Error at caffe2/cmake_install.cmake:115 (file):
  file RPATH_CHANGE could not write new RPATH:

    $ORIGIN:/lib:/lib/intel64:/lib/intel64_win:/lib/win-x64

  to the file:

    /build/source/torch/lib/libtorch_cuda_linalg.so

  The current RUNPATH is:

    /nix/store/3l73glzx595bnya3kfk6z6x2d8v5wlhs-python3.12-torch-2.3.1-lib/lib:/nix/store/3ah4c8n0bprsl5bw1hlm78pvjgmk1zqm-mkl-2023.1.0.46342/lib:/nix/store/23j56hv7plgkgmhj8l2aj4mgjk32529h-cuda_cudart-12.2.140-lib/lib:/nix/store/8071nd4ikv569wfqwrqlxkrnrcksdnmy-cuda_nvtx-12.2.140-lib/lib:/nix/store/rnyc2acy5c45pi905ic9cb2iybn35crz-libcublas-12.2.5.6-lib/lib:/nix/store/y3khb4k396kn3pzlj4i5vbjksmlqp04g-libcusolver-11.5.2.141-lib/lib:/nix/store/8s3nkibydv4iqyj5sp1bq0j4ww9xn7mq-libcusparse-12.1.2.141-lib/lib:/nix/store/0wydilnf1c9vznywsvxqnaing4wraaxp-glibc-2.39-52/lib:/nix/store/kgmfgzb90h658xg0i7mxh9wgyx0nrqac-gcc-13.3.0-lib/lib

  which does not contain:

    /lib:/lib/intel64:/lib/intel64_win:/lib/win-x64:/build/source/build/lib:

  as was expected.
```

I was able to narrow the cause of this error down to these lines in `pytorch/cmake/public/mkl.cmake`:
https://github.com/pytorch/pytorch/blob/47ed5f57b09a280cdc32b5c0f39b811749e341aa/cmake/public/mkl.cmake#L19-L23

The problem is that the `MKL_ROOT` variable is not set, so this line ends up adding `/lib`, `/lib/intel64`, `/lib/intel64_win` and `/lib/win-x64` to `INTERFACE_LINK_DIRECTORIES` for `caffe2::mkl` (on a somewhat related note, I am not sure if this line makes sense even if `MKL_ROOT` **was** set to the correct value since the `intel64`, `intel64_win` and `win-x64` subdirectories don't seem to be present in most distributions of MKL).

I am honestly not sure, why this line expects `MKL_ROOT` to be set. The `find_package(MKL QUIET)` call earlier in the file ends up calling [`cmake/Modules/FindMKL.cmake`](https://github.com/pytorch/pytorch/blob/0d4cedaa47c7ee22042eb24e87eb3cfe95502404/cmake/Modules/FindMKL.cmake) provided by `pytorch`, which doesn't mention `MKL_ROOT` anywhere. The only place, where I could find mentions of `MKL_ROOT` is in the `lib/cmake/mkl/MKLConfig.cmake` file provided by `mkl`, but this file doesn't seem to be called at any point during the build.

---

I am not sure if this `set_property` "hack" is even needed at this point in time. Here is an abbreviated blame history of `MKL_ROOT` usage in `cmake/public/mkl.cmake`:

1) Originally introduced in commit https://github.com/pytorch/pytorch/commit/7b0d577c226fae78f377b26feab4122c4203ad59 (pytorch/pytorch#89359),
    mentions issue pytorch/pytorch#73008

    https://github.com/pytorch/pytorch/blob/7b0d577c226fae78f377b26feab4122c4203ad59/cmake/public/mkl.cmake#L13-L17

2) Replaced with a loop over `MKL_LIBRARIES` in commit https://github.com/pytorch/pytorch/commit/dc2b7aa95554188155a4e2e087412f06f2f3b642 (pytorch/pytorch#94924)

    https://github.com/pytorch/pytorch/blob/dc2b7aa95554188155a4e2e087412f06f2f3b642/cmake/public/mkl.cmake#L10-L19

3) For loop removed, `MKL_ROOT` added back in commit https://github.com/pytorch/pytorch/commit/3226ad21cfdea4a35f13bafd746c3b84a60e5643 (this was a revert of the previous PR)

    https://github.com/pytorch/pytorch/blob/3226ad21cfdea4a35f13bafd746c3b84a60e5643/cmake/public/mkl.cmake#L13-L17

4) `intel64`, `intel64_win` and `win-x64` subdirectories added in commit https://github.com/pytorch/pytorch/commit/b5d3d58497c4926c34f6ae6880fb2c03268439c2 (pytorch/pytorch#105525)

    https://github.com/pytorch/pytorch/blob/b5d3d58497c4926c34f6ae6880fb2c03268439c2/cmake/public/mkl.cmake#L13-L17

5) The loop over `MKL_LIBRARIES` from step 2 was reintroduced in commit https://github.com/pytorch/pytorch/commit/0cc2f06aec16fe9f41c9c1602c2b8b318cc3ce6c (pytorch/pytorch#104224), however this time the `MKL_ROOT` line was not removed for some reason.

    https://github.com/pytorch/pytorch/blob/0cc2f06aec16fe9f41c9c1602c2b8b318cc3ce6c/cmake/public/mkl.cmake#L9-L22

So it's possible that this line is no longer needed after pytorch/pytorch#104224. I was able to successfully build torch with MKL on NixOS after removing the `set_property` call.

cc @malfet @seemethere @frank-wei @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10