https://mooseframework.inl.gov
LibtorchUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #ifdef MOOSE_LIBTORCH_ENABLED
11 
12 #pragma once
13 
14 #include <torch/torch.h>
15 #include "MooseUtils.h"
16 
17 namespace LibtorchUtils
18 {
19 
27 template <typename DataType>
28 void vectorToTensor(const std::vector<DataType> & vector,
29  torch::Tensor & tensor,
30  const bool detach = false);
31 
38 template <typename DataType>
39 torch::Tensor vectorToTensorCopy(const std::vector<DataType> & vector, c10::IntArrayRef sizes);
40 
49 template <typename DataType>
50 torch::Tensor vectorToTensorView(std::vector<DataType> & vector, c10::IntArrayRef sizes);
51 
57 void moveToLibtorchDevice(torch::Tensor & tensor, const torch::DeviceType device_type);
58 
68 torch::Tensor toCPUContiguous(const torch::Tensor & tensor);
69 
76 template <typename DataType>
77 void tensorToVector(torch::Tensor & tensor, std::vector<DataType> & vector);
78 
79 } // LibtorchUtils namespace
80 
81 #endif
torch::Tensor vectorToTensorView(std::vector< DataType > &vector, c10::IntArrayRef sizes)
Utility function that creates a non-owning tensor view of a standard vector.
Definition: LibtorchUtils.C:85
torch::Tensor vectorToTensorCopy(const std::vector< DataType > &vector, c10::IntArrayRef sizes)
Utility function that creates an owning tensor copy of a standard vector.
Definition: LibtorchUtils.C:52
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)
Utility function that converts a torch::Tensor to a standard vector.
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
Return a detached contiguous CPU copy of a tensor.
void vectorToTensor(const std::vector< DataType > &vector, torch::Tensor &tensor, const bool detach=false)
Utility function that converts a standard vector to a torch::Tensor.
Definition: LibtorchUtils.C:71
void moveToLibtorchDevice(torch::Tensor &tensor, const torch::DeviceType device_type)
Move a tensor to the configured libtorch device.