Extending torchscript with custom c++ operators

Hi, all, I tried to extend C++ ops for pytorch follow this tutorial https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html. But I got this error, "op.cpp:17:50: error: ‘class at::Tensor’ has no member named ‘ptr’ ".
what I do as follow:

  1. download pytorch C++ form https://download.pytorch.org/libtorch/cu90/libtorch-shared-with-deps-latest.zip
  2. add a cmake to find the lib and include directory as this file:
include(FindPackageHandleStandardArgs)
set(TORCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libtorch/include)

find_library(TORCH_LIBRARY
       NAMES torch
       PATHS ${CMAKE_CURRENT_SOURCE_DIR}/libtorch/lib NO_DEFAULT_PATH)

find_package_handle_standard_args(torch TORCH_INCLUDE_DIR TORCH_LIBRARY)
  1. in CMakeList, I add like these:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(warp_perspective)

add_compile_options(-std=c++11)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

include_directories(
        "${TORCH_INCLUDE_DIR}"
)

# Define our library target
add_library(warp_perspective SHARED op.cpp)
# Enable C++11
target_compile_features(warp_perspective PRIVATE cxx_range_for)
# Link against LibTorch
target_link_libraries(warp_perspective "${TORCH_LIBRARIES}")
# Link against OpenCV
target_link_libraries(warp_perspective opencv_core opencv_imgproc)
  1. then the compile error show
op.cpp:17:50: error: ‘class at::Tensor’ has no member named ‘ptr’

I think I found a bug for this docmenthttps://pytorch.org/tutorials/advanced/torch_script_custom_ops.html, that in the code

torch::Tensor output = torch::from_blob(output.ptr<float>(), /*sizes=*/{8, 8});

it should be

torch::Tensor output = torch::from_blob(output_mat.ptr<float>(), /*sizes=*/{8, 8});
1 Like

Good catch! This should be fixed definitely.