1

enter image description here

I'm trying to load a traced PyTorch model in C++ using LibTorch but encountering a std::bad_alloc error. I've set up a minimal example to demonstrate the issue.

The Problem: When running my C++ application that loads a PyTorch model, it crashes with:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

cpp code :

#include <iostream>
#include <memory>
#include <torch/script.h>

int main()
{
    // Load the traced model
    torch::jit::script::Module module;

    // Load the saved model - update path to your .pt file
    module = torch::jit::load("net.pt");


    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.18)
project(torch_example)

include(FetchContent)

set(LIBTORCH_CPU_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-2.5.1%2Bcpu.zip")

FetchContent_Declare(
    Torch
    URL ${LIBTORCH_CPU_URL }
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
FetchContent_MakeAvailable(Torch)

add_executable(torch_example main.cpp)
target_link_libraries(torch_example PRIVATE "${TORCH_LIBRARIES}")
target_include_directories(torch_example PRIVATE "${torch_SOURCE_DIR}/include")
set_target_properties(torch_example PROPERTIES CXX_STANDARD 23)
7
  • That means that you do not have enough free memory available for the model. Commented Nov 11, 2024 at 10:04
  • that doesnt make any sense i have 13gb ram left Commented Nov 11, 2024 at 10:08
  • 1
    that doesnt make any sense i have 13gb ram left -- 1) It makes a lot of sense if the application is 32-bit. You can have 100gb or ram, a 32-bit application can only access 4Gb or less. 2) A std::bad_alloc can be thrown if the argument being given to new[] is negative. See this. Commented Nov 11, 2024 at 10:40
  • Can you load your traced your serialized net in a python code and run a random input through it to check that it was properly serialized and is not broken or corrupted in any way ? Also, please link when you crosspost : discuss.pytorch.org/t/… Commented Nov 11, 2024 at 12:01
  • @trialNerror so i made a seperate c++ project and it works. but not on the my other big project i checked compiler configuration it all looks good Commented Nov 11, 2024 at 12:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.