https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
18{
19
27template <typename DataType>
28void vectorToTensor(const std::vector<DataType> & vector,
29 torch::Tensor & tensor,
30 const bool detach = false);
31
38template <typename DataType>
39torch::Tensor vectorToTensorCopy(const std::vector<DataType> & vector, c10::IntArrayRef sizes);
40
49template <typename DataType>
50torch::Tensor vectorToTensorView(std::vector<DataType> & vector, c10::IntArrayRef sizes);
51
57void moveToLibtorchDevice(torch::Tensor & tensor, const torch::DeviceType device_type);
58
68torch::Tensor toCPUContiguous(const torch::Tensor & tensor);
69
76template <typename DataType>
77void tensorToVector(torch::Tensor & tensor, std::vector<DataType> & vector);
78
79} // LibtorchUtils namespace
80
81#endif
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
Return a detached contiguous CPU copy of a tensor.
void moveToLibtorchDevice(torch::Tensor &tensor, const torch::DeviceType device_type)
Move a tensor to the configured libtorch device.
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.
torch::Tensor vectorToTensorCopy(const std::vector< DataType > &vector, c10::IntArrayRef sizes)
Utility function that creates an owning tensor copy of a standard vector.
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)
Utility function that converts a torch::Tensor to a standard vector.
torch::Tensor vectorToTensorView(std::vector< DataType > &vector, c10::IntArrayRef sizes)
Utility function that creates a non-owning tensor view of a standard vector.