Cannot build THE C++ FRONTEND example. no known conversion from 'const c10::optional<unsigned long>' to 'size_t'


(Beomsoo Kim) #1

Hello, I’ve have been studying pytorch source code and I’m exited to finally touch the c++ part! I built the libtorch.dylib from the pytorch source code and I also succeeded to install and build a basic example https://pytorch.org/cppdocs/installing.html.

Then I tried another example from https://pytorch.org/cppdocs/frontend.html#end-to-end-example. I modified the example file a little by adding a main function but I failed to build it. Here is the result of make command after cmake command. Please ignore some *'s. They came from copy and pasting.

[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
/Users/derek/playground/libtorch_practice/example-app.cpp:29:16:** **error:** **no member named 'data_loader' in namespace 'torch::data'**
torch::data::data_loader(torch::data::datasets::MNIST("./data"));
~~~~~~~~~~~~~^

/Users/derek/playground/libtorch_practice/example-app.cpp:42:31:** **error:** **use of undeclared identifier 'model'**
auto prediction = model.forward(batch.data);
                    ^

In file included from /Users/derek/playground/libtorch_practice/example-app.cpp:1:
In file included from /anaconda3/envs/torch_fork/lib/python3.7/site-packages/torch/lib/include/torch/csrc/api/include/torch/torch.h:3:
In file included from /anaconda3/envs/torch_fork/lib/python3.7/site-packages/torch/lib/include/torch/csrc/api/include/torch/all.h:8:

/anaconda3/envs/torch_fork/lib/python3.7/site-packages/torch/lib/include/torch/csrc/api/include/torch/serialize.h:39:11:** **error:** **invalid operands to binary expression
    ('serialize::OutputArchive' and 'const Net')
archive << value;
~~~~~~~ ^  ~~~~~
.....

I guessed the source code from the website maybe old or wrong… So I found other code from https://github.com/goldsborough/examples/blob/cpp/cpp/mnist/mnist.cpp

I tried it and failed again. Here is the error message from make command

[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app2.cpp.o

/Users/derek/playground/libtorch_practice/example-app2.cpp:154:5:** **error:** **no matching function for call to 'train'**
    train(
    ^~~~~

/Users/derek/playground/libtorch_practice/example-app2.cpp:52:6:** **note:** candidate function not viable: no known conversion from 'const c10::optional&lt;unsigned long&gt;' to 'size_t' (aka 'unsigned long') for 7th
    argument

void train(
       ^

/Users/derek/playground/libtorch_practice/example-app2.cpp:156:5:** **error:** **no matching function for call to 'test'
    test(model, device, *test_loader, dataset_size);
    ^~~~

/Users/derek/playground/libtorch_practice/example-app2.cpp:79:6:** **note:** candidate function not viable: no known conversion from 'const c10::optional&lt;unsigned long&gt;' to 'size_t' (aka 'unsigned long') for 4th
    argument
void test(
      ^

2 errors generated.

make[2]: *** [CMakeFiles/example-app.dir/example-app2.cpp.o] Error 1
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
make: *** [all] Error 2

So I have been tackling this problem for hours… but I still find it hard to solve. const c10::optional<unsigned long> seems like a kind of a wrapper for unsigned long , but I couldn’t find good resource that helps me understand the exact mechanism. I will be grateful if anybody give me a piece of advice!

(CMake log)

-- The C compiler identification is AppleClang 10.0.0.10001145
-- The CXX compiler identification is AppleClang 10.0.0.10001145
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Found torch: /anaconda3/envs/torch_fork/lib/python3.7/site-packages/torch/lib/libtorch.dylib  
torch;caffe2_library
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/derek/playground/libtorch_practice/build

(clang++ / clang / pytorchversion)

Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

>>> torch.__version__
'1.0.0a0+2efec05'

@goldsborough


(Shigeki Karita) #2

It is already solved in this forum.

see



(Beomsoo Kim) #3

@ShigekiKarita ah thanks! Maybe I will move this problem to that thread.


(Peter Goldsborough) #4

Thanks for reporting. The syntax error in the mnist example is now fixed. I’ll update frontend.html, which is slightly outdated code.