57 auto device = torch::Device(torch::kCPU);
58 if (torch::cuda::is_available()) {
59 device = torch::Device(torch::kCUDA);
64 torch::set_num_threads(1);
67 torch::Tensor img_tensor = torch::randn({1, 3, 224, 224});
68 torch::Tensor obstacle_pos = torch::randn({1, 20, 2});
69 torch::Tensor obstacle_pos_step = torch::randn({1, 20, 2});
70 std::vector<torch::jit::IValue> torch_inputs;
71 torch::Tensor torch_default_output_tensor;
73 torch_inputs.push_back(c10::ivalue::Tuple::create(
74 {std::move(img_tensor.to(
device)), std::move(obstacle_pos.to(
device)),
75 std::move(obstacle_pos_step.to(
device))}));
78 WarmUp(torch_inputs, &model_instance_, &torch_default_output_tensor);
83 const std::vector<void*>& input_buffer,
unsigned int input_size,
84 std::vector<void*>* output_buffer,
unsigned int output_size) {
85 ACHECK(input_size == input_buffer.size() && input_size == 3);
86 ACHECK(output_size == output_buffer->size() && output_size == 1);
92 auto device = torch::Device(torch::kCPU);
93 if (torch::cuda::is_available()) {
94 device = torch::Device(torch::kCUDA);
96 torch::Tensor img_tensor =
97 torch::from_blob(input_buffer[0], {1, 3, 224, 224});
98 torch::Tensor obstacle_pos = torch::from_blob(input_buffer[1], {1, 20, 2});
99 torch::Tensor obstacle_pos_step =
100 torch::from_blob(input_buffer[2], {1, 20, 2});
102 std::vector<torch::jit::IValue> torch_inputs;
104 torch_inputs.push_back(c10::ivalue::Tuple::create(
105 {std::move(img_tensor.to(
device)), std::move(obstacle_pos.to(
device)),
106 std::move(obstacle_pos_step.to(
device))}));
108 torch::Tensor torch_output_tensor =
109 model_instance_.forward(torch_inputs).toTensor().to(torch::kCPU);
110 memcpy((*output_buffer)[0], torch_output_tensor.data_ptr<
float>(),
111 1 * 30 * 2 *
sizeof(
float));
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
void WarmUp(const std::vector< torch::jit::IValue > &torch_inputs, torch::jit::script::Module *model, at::Tensor *default_output_ptr)
warm up function to avoid slowly inference of torch model