Questions tagged [libtorch]

libtorch is the PyTorch's C++ frontend providing a high-level interaction for the machine learning research and production use.

The PyTorch C++ frontend provides a high level, pure C++ modeling interface for neural network and general machine learning research and production use cases, largely following the Python API in design and provided functionality. The C++ frontend includes the following:

  • An interface for defining machine learning models through a hierarchical module system (like torch.nn.Module);

  • A “standard library” of pre-existing modules for the most common modeling purposes (e.g. convolutions, RNNs, batch normalization etc.);

  • An optimization API, including implementations of popular optimizers such as SGD, Adam, RMSprop, and others;

  • A means of representing datasets and data pipelines, including functionality to load data in parallel over many CPU cores;

  • A serialization format for storing and loading checkpoints of a training session (like torch.utils.data.DataLoader);

  • Automatic parallelization of models onto multiple GPUs (like torch.nn.parallel.DataParallel);

  • Support code to easily bind C++ models into Python using pybind11;

  • Entry points to the TorchScript JIT compiler;

  • Helpful utilities to facilitate interfacing with the Aten and Autograd APIs.

290 questions
42
votes
2 answers

Using torch.nn.DataParallel with a custom CUDA extension

To my understanding, the built-in PyTorch operations all automatically handle batches through implicit vectorization, allowing parallelism across multiple GPUs. However, when writing a custom operation in CUDA as per the Documentation, the LLTM…
Jack H
  • 2,440
  • 4
  • 40
  • 63
11
votes
2 answers

Equivalence of slicing tensor in Pytorch/ATen C++

In Python given a 2-D tensor, we can use tensor[:,:2] to slice the a 2x2 matrix of the first two elements in the top left of the matrix, e.g. : x = torch.tensor([[-1.4673, 0.9980, -2.1427, -1.1798, -0.0646, -0.2635, -2.8930, -0.2563, …
alvas
  • 115,346
  • 109
  • 446
  • 738
9
votes
2 answers

What's the best way of checking whether a torch::Tensor is empty?

I'm currently using the has_storage() method to check whether a tensor is empty or not, but I wonder if there is anything better other than this! and whether there are any implications involved in using this other than the fact that an initialized…
Hossein
  • 24,202
  • 35
  • 119
  • 224
9
votes
1 answer

Torch C++: Getting the value of a int tensor by using *.data()

In the C++ version of Libtorch, I found that I can get the value of a float tensor by *tensor_name[0].data(), in which instead of 0 I can use any other valid index. But, when I have defined an int tensor by adding option at::kInt into the…
Afshin Oroojlooy
  • 1,326
  • 3
  • 21
  • 43
8
votes
2 answers

Can't link a project using cmake with OpenCV and LibTorch

I asked a similar question about linking a project with OpenCV a few days ago. I got that working, but now I've hit a very weird problem using CMake and adding LibTorch to the project. If I only use OpenCV in the project, everything compiles,…
Tim K
  • 109
  • 1
  • 3
7
votes
1 answer

What is the LibTorch equivalent to PyTorch's torch.no_grad?

When testing a network in PyTorch one can use with torch.no_grad():. What is the Libtorch (C++) equivalent? Thanks!
MD98
  • 344
  • 2
  • 9
7
votes
1 answer

In Torch C++ API, How to write to the internal data of a tensor fastly?

I am using torch C++ frontend and want to have a tensor with specified value in it. To achieve this one may allocate memory and set value by hand, then use torch::from_blob to build a tensor on the memory block, but it seems not clean enough for…
LIU Qingyuan
  • 524
  • 5
  • 18
7
votes
3 answers

Create a torch::Tensor from C/C++ array without using "from_blob(...)..."

Using the C++ libtorch frontend for Pytorch I want to create a torch::Tensor from a C++ double[] array. Comming from a legacy C/C++ API. I could not find a simple documentation about the subject not in docs nor in the forums. Something…
imbr
  • 6,226
  • 4
  • 53
  • 65
7
votes
2 answers

Errors while using Libtorch + OpenCV + QT Creator

I have the following configuration in the .pro file TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt CONFIG += thread SOURCES += main.cpp INCLUDEPATH += /usr/local/include/opencv4 LIBS += -L/usr/local/lib/ LIBS +=…
Saptarshi
  • 603
  • 2
  • 7
  • 15
6
votes
0 answers

How to access module attributes such as the stride of convolutions from jit::script::module

I am currently writing a C++ program which needs to do some analyses of the structure of a CNN model in torchScript format. I am using the C++ torch library the way it is shown on torch.org, loading in the model like so: #include…
Mercury
  • 594
  • 1
  • 12
  • 28
6
votes
2 answers

How is it possible to convert a std::vector> to a torch::Tensor?

I have a std::vector> where I want to conver it into a torch::Tensor in libtorch. However it seems, the torch::tensor(), or torch::from_blob(), can't be used for this purpose! I tried to use c10::ArrayRef and then use that for…
Hossein
  • 24,202
  • 35
  • 119
  • 224
6
votes
1 answer

libtorch (PyTorch C++) weird class syntax

In the official PyTorch C++ examples on GitHub Here you can witness a strange definition of a class: class CustomDataset : public torch::data::datasets::Dataset {...} My understanding is that this defines a class CustomDataset which…
JacKeown
  • 2,780
  • 7
  • 26
  • 34
6
votes
1 answer

cmake - linking static library pytorch cannot find its internal functions during build

I'm trying to build a program using cmake. For several reasons, the program must be built using static libraries rather than dynamic libraries, and I need to use PyTorch so this is what I've done: Downloaded and installed PyTorch static library…
WannabeArchitect
  • 1,058
  • 2
  • 11
  • 22
5
votes
2 answers

How to build libtorch on mac arm?

i downloaded libtorch and make these files on macbook pro ARM: example-app/ build/ libtorch/ CMakeLists.txt example-app.cpp then i used these commands for build torch: cmake…
Sajjad Aemmi
  • 2,120
  • 3
  • 13
  • 25
5
votes
1 answer

How to convert a list of tensors into a torch::Tensor?

I'm trying to convert the following Python code into its equivalent libtorch: tfm = np.float32([[A[0, 0], A[1, 0], A[2, 0]], [A[0, 1], A[1, 1], A[2, 1]] ]) In Pytorch we could simply use torch.stack or simply use…
Hossein
  • 24,202
  • 35
  • 119
  • 224
1
2 3
19 20