-
Notifications
You must be signed in to change notification settings - Fork 61
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
Accelerate DirectLiNGAM by parallelising causal ordering on GPUs with CUDA #128
Conversation
Very interesting adaption, looking forward to it. |
Hi @Viktour19 , thanks for your contribution! First of all, I could not install culingam in my Windows environment with
I attempted a manual installation using the procedure shown here, but it failed. With various modifications I was able to install. Environment variable CUDA_HOMEThe environment variable CUDA_HOME is specified as a path in Windows. Inclusion of nvToolsExt.hChanged the header inclusion method as shown in the official site. Compile error for M_PIAdded nvToolsExt.lib is missingAdd the following path to library_dirs in CUDAExtension. And, change to nvToolsExt64_1.lib instead of nvToolsExt.lib. |
Hi @Viktour19, import numpy as np
import pandas as pd
import graphviz
import lingam
from lingam.utils import make_dot
print([np.__version__, pd.__version__, graphviz.__version__, lingam.__version__])
np.random.seed(0) ['1.25.2', '2.2.0', '0.20', '1.8.3'] Test Datax2 = np.random.uniform(size=100000)
x0 = 3.0*x2 + np.random.uniform(size=100000)
x1 = 1.0*x0 + 6.0*x2 + np.random.uniform(size=100000)
X = pd.DataFrame(np.array([x0, x1, x2]).T ,columns=['x0', 'x1', 'x2'])
make_dot([[0.0, 0.0, 3.0], [1.0, 0.0, 6.0], [0.0, 0.0, 0.0]]) CPU%%time
model = lingam.DirectLiNGAM()
model = model.fit(X) CPU times: total: 156 ms print('causal ordering:', model.causal_order_)
make_dot(model.adjacency_matrix_) causal ordering: [2, 0, 1] GPU%%time
model = lingam.DirectLiNGAM(measure='pwling_fast')
model = model.fit(X) CPU times: total: 141 ms print('causal ordering:', model.causal_order_)
make_dot(model.adjacency_matrix_) causal ordering: [0, 1, 2] |
The output of the
The culingam installed by pip install is v0.0.7, but in the github repository it is v0.0.6. |
I installed culingam v0.07 on Linux with pip and ran DirectLiNGAM_fast.py, but got an AssertionError on |
I tried to run it with only culingam v0.0.7. Execution Result
Collecting culingam import numpy as np
import subprocess
# [[ 0. 0. 0. 2.99982982 0. 0. ]
# [ 2.99997222 0. 2.00008518 0. 0. 0. ]
# [ 0. 0. 0. 5.99981965 0. 0. ]
# [ 0. 0. 0. 0. 0. 0. ]
# [ 7.99857006 0. -0.99911522 0. 0. 0. ]
# [ 3.99974733 0. 0. 0. 0. 0. ]]
# [3, 0, 2, 5, 4, 1]
def get_cuda_version():
try:
nvcc_version = subprocess.check_output(["nvcc", "--version"]).decode('utf-8')
print("CUDA Version found:\n", nvcc_version)
return True
except Exception as e:
print("CUDA not found or nvcc not in PATH:", e)
return False
def main():
np.random.seed(42)
size = 100000
x3 = np.random.uniform(size=size)
x0 = 3.0*x3 + np.random.uniform(size=size)
x2 = 6.0*x3 + np.random.uniform(size=size)
x1 = 3.0*x0 + 2.0*x2 + np.random.uniform(size=size)
x5 = 4.0*x0 + np.random.uniform(size=size)
x4 = 8.0*x0 - 1.0*x2 + np.random.uniform(size=size)
X = np.array([x0, x1, x2, x3, x4, x5]).T
dlm = DirectLiNGAM(12)
dlm.fit(X, disable_tqdm=False)
np.set_printoptions(precision=3, suppress=True)
print(dlm._adjacency_matrix)
print(dlm.causal_order_) # Check for cuda availability before importing CUDA-dependent packages
if get_cuda_version():
try:
from culingam.directlingam import DirectLiNGAM
main()
except ImportError as e:
print("Failed to import CUDA-dependent package:", e)
else:
print("CUDA is not available. Please ensure CUDA is installed and correctly configured.") CUDA Version found: 100%|██████████| 6/6 [00:00<00:00, 17.03it/s] |
Thanks for your patience! it seems I needed to allow for a broader range of CUDA gpu compute capability. E.g the P100 on Kaggle is |
@Viktour19 It would be great if you could support |
@Viktour19 |
Excellent! |
I temporarily reverted because I found that the CI test did not pass and the docs build did not pass in an environment without culingam installed. The error is due to the following code (direct_lingam.py): from lingam_cuda import causal_order as causal_order_gpu To avoid the error in the above code, we can install culingam. |
Changed the import location and reverted again. e64892b |
This PR includes the implementation drastically speed-up (up to 32x on consumer GPU) DirectLiNGAM and its variants e.g VarLiNGAM.
The details are to allow for an optional dependency: https://github.com/Viktour19/culingam which implements custom CUDA kernels for the pairwise likelihood ratio causal ordering method.
The implementation has been tested locally on an NVIDIA RTX 6000 on a Linux machine - but tests on other setups are needed.