pytorch / examples

A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc.
https://pytorch.org/examples
BSD 3-Clause "New" or "Revised" License
22.28k stars 9.53k forks source link

torch::Tensor can't be use with std::tuple or std::vector? #759

Open williamlzw opened 4 years ago

williamlzw commented 4 years ago

include <torch/torch.h>

include

include

include

include

auto ReadRsv(const std::string path) { HANDLE filea= CreateFileA((LPCSTR)path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN,NULL); int cout; int length; ReadFile(filea, &cout,4,NULL,NULL); std::vector<std::tuple<torch::Tensor, torch::Tensor>> rsv; byte dataa = new byte[784]; byte datab = new byte[1]; DWORD hasread; for (int i = 0; i<cout; ++i) { ReadFile(filea, &length, 4, &hasread, NULL); ReadFile(filea, &dataa, 784, &hasread, NULL); torch::Tensor line = torch::from_blob(&dataa, { 784 },torch::kByte); ReadFile(filea, &datab, 1, &hasread, NULL); torch::Tensor label = torch::from_blob(&datab, { 1 }, torch::kByte); rsv.push_back(std::make_tuple(line,label)); //wrong? } delete []dataa; delete []datab; CloseHandle(filea); return rsv; }


win10 x64;libtorch 1.5 release x64;

download rsv file: https://share.weiyun.com/5DYsiDe

when i=0,it run success,but when i=1,it run wrong. 0x00007FFD618EF7E4 (torch_cpu.dll) (in consoleapplication1.exe) throws an exception: 0xC0000005: an access conflict occurs while writing to location 0x0000000000000000. Remove this sentence and it will run successfully ->rsv.push_back(std::make_tuple(line,label));

williamlzw commented 4 years ago

Problem solved

auto ReadRsv(const std::string path) { HANDLE filea= CreateFileA((LPCSTR)path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN,NULL); int cout; int length; ReadFile(filea, &cout,4,NULL,NULL); std::vector<std::tuple<torch::Tensor, torch::Tensor>> rsv; byte dataa = new byte[784]; byte datab = new byte[1]; DWORD hasread; for (int i = 0; i<cout; ++i) {
ReadFile(filea, &length, 4, &hasread, NULL); ReadFile(filea, &dataa[0], 784, &hasread, NULL); torch::Tensor line = torch::from_blob(&dataa, { 784 },torch::kByte); ReadFile(filea, &datab[0], 1, &hasread, NULL); torch::Tensor label = torch::from_blob(&datab, { 1 }, torch::kByte); rsv.push_back(std::make_tuple(line,label));
} delete []dataa; delete []datab; CloseHandle(filea); return rsv; }