On MacOS I compiled lib torch for c++ and then I compiled sample code with built header and library and hit pytorch issue

yat: <p><span class="hashtag-raw">#include</span> “torch/torch.h”</p>
<p>std::string kDataRoot{“/Users/xxx/exp/c++exp/modelcreate/build/data”};</p>
<p>// Define a new Module.<br>
struct Net : torch::nn::Module {<br>
Net() {<br>
// Construct and register two Linear submodules.<br>
fc1 = register_module(“fc1”, torch::nn::Linear(784, 64));<br>
fc2 = register_module(“fc2”, torch::nn::Linear(64, 32));<br>
fc3 = register_module(“fc3”, torch::nn::Linear(32, 10));<br>
}</p>
<p>// Implement the Net’s algorithm.<br>
torch::Tensor forward(torch::Tensor x) {<br>
// Use one of many tensor manipulation functions.<br>
x = torch::relu(fc1-&gt;forward(x.reshape({x.size(0), 784})));<br>
x = torch::dropout(x, /<em>p=</em>/0.5, /<em>train=</em>/is_training());<br>
x = torch::relu(fc2-&gt;forward(x));<br>
x = torch::log_softmax(fc3-&gt;forward(x), /<em>dim=</em>/1);<br>
return x;<br>
}</p>
<p>// Use one of many “standard library” modules.<br>
torch::nn::Linear fc1{nullptr}, fc2{nullptr}, fc3{nullptr};<br>
};</p>
<p>int main() {<br>
// Create a new Net.<br>
auto net = std::make_shared();</p>
<p>// Create a multi-threaded data loader for the MNIST dataset.<br>
auto data_loader = torch::data::make_data_loader(<br>
torch::data::datasets::MNIST(kDataRoot).map(<br>
torch::data::transforms::Stack&lt;&gt;()),<br>
/<em>batch_size=</em>/64);</p>
<p>// Instantiate an SGD optimization algorithm to update our Net’s parameters.<br>
torch::optim::SGD optimizer(net-&gt;parameters(), /<em>lr=</em>/0.01);</p>
<h1><a name="for-size_t-epoch-1-epoch-10-epoch-1" class="anchor" href="#for-size_t-epoch-1-epoch-10-epoch-1"></a>for (size_t epoch = 1; epoch &lt;= 10; ++epoch) {</h1>
<p>size_t batch_index = 0;<br>
// Iterate the data loader to yield batches from the dataset.<br>
for (auto&amp; batch : *data_loader) {<br>
// Reset gradients.<br>
optimizer.zero_grad();<br>
// Execute the model on the input data.<br>
torch::Tensor prediction = net-&gt;forward(batch.data);<br>
// Compute a loss value to judge the prediction of our model.<br>
torch::Tensor loss = torch::nll_loss(prediction, batch.target);<br>
// Compute gradients of the loss w.r.t. the parameters of our model.<br>
loss.backward();<br>
// Update the parameters based on the calculated gradients.<br>
optimizer.step();<br>
// Output the loss and checkpoint every 100 batches.<br>
if (++batch_index % 100 == 0) {<br>
std::cout &lt;&lt; "Epoch: " &lt;&lt; epoch &lt;&lt; " | Batch: " &lt;&lt; batch_index<br>
&lt;&lt; " | Loss: " &lt;&lt; loss.item() &lt;&lt; std::endl;<br>
// Serialize your model periodically as a checkpoint.<br>
torch::save(net, “net.pt”);<br>
}<br>
}<br>
}<br>
}</p>
<p>then I run the code and hit following issue (after downloading mnist dataset):<br>
libc++abi: terminating due to uncaught exception of type c10::Error: stream.read(reinterpret_cast&lt;char*&gt;(&amp;value), sizeof value) INTERNAL ASSERT FAILED at “/Users/xxxx/exp/libtorch/pytorch/torch/csrc/api/src/data/datasets/mnist.cpp”:42, please report a bug to PyTorch.<br>
Exception raised from read_int32 at /Users/xxxx/exp/libtorch/pytorch/torch/csrc/api/src/data/datasets/mnist.cpp:42 (most recent call first):<br>
frame <span class="hashtag-raw">#0:</span> c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 188 (0x101167248 in libc10.dylib)<br>
frame <a href="https://github.com/pytorch/pytorch/issues/1" rel="noopener nofollow ugc">#1</a>: c10::TypeError::~TypeError() + 0 (0x10b645794 in libtorch_cpu.dylib)<br>
frame <a href="https://github.com/pytorch/pytorch/issues/2" rel="noopener nofollow ugc">#2</a>: torch::data::datasets::(anonymous namespace)::expect_int32(std::__1::basic_ifstream&lt;char, std::__1::char_traits&gt;&amp;, unsigned int) + 164 (0x10fa95c04 in libtorch_cpu.dylib)<br>
frame <a href="https://github.com/pytorch/pytorch/issues/3" rel="noopener nofollow ugc">#3</a>: torch::data::datasets::MNIST::MNIST(std::__1::basic_string&lt;char, std::__1::char_traits, std::__1::allocator&gt; const&amp;, torch::data::datasets::MNIST::Mode) + 404 (0x10fa9508c in libtorch_cpu.dylib)<br>
frame <a href="https://github.com/pytorch/pytorch/pull/4" rel="noopener nofollow ugc">#4</a>: main + 72 (0x100b16aac in model_create)<br>
frame <a href="https://github.com/pytorch/pytorch/issues/5" rel="noopener nofollow ugc">#5</a>: start + 2360 (0x1839410e0 in dyld)</p>
<p>Abort trap: 6</p>
<p>Which is obviously indicating to open PyTorch bug. Any clue?</p>
<h3><a name="versions-2" class="anchor" href="#versions-2"></a>Versions</h3>
<p>version is 2.3.0a0</p>

albanD: <p>Hey!</p>
<p>This is quite surprising indeed, it is failing to load the dataset. Are you sure the dataset downloaded properly and you have a valid archive locally?</p>

yat: <p>yes dataset downloaded properly.</p>

yat: <p>It is just to update that I downloaded the mnist dataset using python and it worked. Thanks for looking at this.</p>