Git Product home page Git Product logo

Comments (6)

qindazhu avatar qindazhu commented on August 21, 2024

RE 1): Is the build system supposed to work with any installation of PyTorch? e.g. if users build PyTorch from source, or install using Conda
RE 2): Why do we need to replace CudaContext? I thought we can just create a new class PyTorchContext which inherits from Context, then we can operate PyTorch tensor (no matter GPU or CPU, exposed with DLPack) with current interfaces (e.g. Array1) and algorithms. Correct me if I'm wrong.

from k2.

csukuangfj avatar csukuangfj commented on August 21, 2024

It takes several hours to build PyTorch from source. Another problem to build from source is that there is a good chance the PyTorch version K2 uses is different from the one Python uses.

The current CudaContext is a naive implementation. What I want to do is to change GetCudaContext so that
it returns a pytorch context.

I have no experience with conda. But I guess it will work.

from k2.

danpovey avatar danpovey commented on August 21, 2024

from k2.

csukuangfj avatar csukuangfj commented on August 21, 2024

To the best of my knowledge, there are two approaches to build against PyTorch:

(1) Build PyTorch from source. I think only PyTorch developers use this method. The building process can take several hours.
It's very likely that the source version is different from the one installed with pip.

(2) Build against LibTorch. https://pytorch.org/ provides links to download libtorch-shared-with-deps-xxx.zip, which
contains a set of header files and shared/static libraries. These files are also contained in the PyTorch package installed via pip install torch. pip install torch is chosen so that we use the same PyTorch version with Python.

cd build/bin
readelf -d cu_array_test

prints

 0x0000000000000001 (NEEDED)             Shared library: [libfsa.so]
 0x0000000000000001 (NEEDED)             Shared library: [libgtest_maind.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cpu.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cuda.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc10_cuda.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc10.so]
 0x0000000000000001 (NEEDED)             Shared library: [libnvToolsExt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libcudart.so.10.0]
 0x0000000000000001 (NEEDED)             Shared library: [libgtestd.so]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib:/xxx/cuda/lib64:/xxx/cuda/lib64/stubs]

Part of the linking commands for cu_array_test is given below:

/usr/bin/g++ xxx/array_test.cu.o -o ../../bin/cu_array_test  
-Wl,-rpath,/xxx/k2/build/lib:/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib
/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib/libtorch.so 
-Wl,--no-as-needed,/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so 
-Wl,--no-as-needed,/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so 
-Wl,--as-needed /xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib/libc10_cuda.so 
/xxx/pyenv/versions/3.7.8/lib/python3.7/site-packages/torch/lib/libc10.so

We can see that k2 uses the same PyTorch libraries from the torch package.


Note that there are no restrictions on the PyTorch version. Users can choose which PyTorch version to
use by pip install torch==x.x.x and K2 will pick the corresponding version correctly.

from k2.

csukuangfj avatar csukuangfj commented on August 21, 2024

how does your pull request relate to this, in terms of PyTorch versions and compatibility?

k2/cmake/torch.cmake

Lines 3 to 7 in f784a2c

execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c "import os; import torch; print(os.path.dirname(torch.__file__))"
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE TORCH_DIR
)

K2 uses the PyTorch installation information from the pip package, so it's guaranteed that k2 uses whichever version Python
is currently using.

target_link_libraries(context PUBLIC ${TORCH_LIBRARIES})

from k2.

danpovey avatar danpovey commented on August 21, 2024

from k2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.