https://mooseframework.inl.gov
Functions
LibtorchUtils Namespace Reference

Functions

template<typename DataType >
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. More...
 
template<typename DataType >
torch::Tensor vectorToTensorCopy (const std::vector< DataType > &vector, c10::IntArrayRef sizes)
 Utility function that creates an owning tensor copy of a standard vector. More...
 
template<typename DataType >
torch::Tensor vectorToTensorView (std::vector< DataType > &vector, c10::IntArrayRef sizes)
 Utility function that creates a non-owning tensor view of a standard vector. More...
 
void moveToLibtorchDevice (torch::Tensor &tensor, const torch::DeviceType device_type)
 Move a tensor to the configured libtorch device. More...
 
torch::Tensor toCPUContiguous (const torch::Tensor &tensor)
 Return a detached contiguous CPU copy of a tensor. More...
 
template<typename DataType >
void tensorToVector (torch::Tensor &tensor, std::vector< DataType > &vector)
 Utility function that converts a torch::Tensor to a standard vector. More...
 
template torch::Tensor vectorToTensorCopy< Real > (const std::vector< Real > &vector, c10::IntArrayRef sizes)
 
template void vectorToTensor< Real > (const std::vector< Real > &vector, torch::Tensor &tensor, const bool detach)
 
template torch::Tensor vectorToTensorView< Real > (std::vector< Real > &vector, c10::IntArrayRef sizes)
 
template void tensorToVector< Real > (torch::Tensor &tensor, std::vector< Real > &vector)
 

Function Documentation

◆ moveToLibtorchDevice()

void LibtorchUtils::moveToLibtorchDevice ( torch::Tensor tensor,
const torch::DeviceType  device_type 
)

Move a tensor to the configured libtorch device.

Parameters
tensorThe tensor to move
device_typeThe target torch device type

Definition at line 100 of file LibtorchUtils.C.

101 {
102  tensor = tensor.to(device_type);
103 }

◆ tensorToVector()

template<typename DataType >
void LibtorchUtils::tensorToVector ( torch::Tensor tensor,
std::vector< DataType > &  vector 
)

Utility function that converts a torch::Tensor to a standard vector.

Template Parameters
DataTypeThe type of data (float,double, etc.) which the vector is filled with
Parameters
tensorThe tensor which needs to be converted
vectorThe output vector

Definition at line 114 of file LibtorchUtils.C.

115 {
116  try
117  {
118  tensor.data_ptr<DataType>();
119  }
120  catch (const c10::Error & e)
121  {
122  mooseError(
123  "Cannot cast tensor values to", MooseUtils::prettyCppType<DataType>(), "!\n", e.msg());
124  }
125 
126  const auto & sizes = tensor.sizes();
127 
128  long int max_size = 0;
129  for (const auto & dim_size : sizes)
130  // We do this comparison because XCode complains if we use std::max
131  max_size = dim_size > max_size ? dim_size : max_size;
132 
133  mooseAssert(max_size == tensor.numel(), "The given tensor should be one-dimensional!");
134  vector = {tensor.data_ptr<DataType>(), tensor.data_ptr<DataType>() + tensor.numel()};
135 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311

◆ tensorToVector< Real >()

template void LibtorchUtils::tensorToVector< Real > ( torch::Tensor tensor,
std::vector< Real > &  vector 
)

◆ toCPUContiguous()

torch::Tensor LibtorchUtils::toCPUContiguous ( const torch::Tensor tensor)

Return a detached contiguous CPU copy of a tensor.

This is for call sites that read tensor storage through CPU accessors or data_ptr(). Moving a tensor to CPU does not guarantee that logical tensor order is backed by a dense linear memory layout; contiguous() makes that invariant explicit.

Parameters
tensorThe tensor to copy to CPU

Definition at line 106 of file LibtorchUtils.C.

Referenced by dataStore().

107 {
108  // CPU accessors can handle strides, but data_ptr()-based reads require dense logical order.
109  return tensor.detach().to(tensor.options().device(at::kCPU)).contiguous();
110 }

◆ vectorToTensor()

template<typename DataType >
void LibtorchUtils::vectorToTensor ( const std::vector< DataType > &  vector,
torch::Tensor tensor,
const bool  detach = false 
)

Utility function that converts a standard vector to a torch::Tensor.

Template Parameters
DataTypeThe type of data (float,double, etc.) which the vector is filled with
Parameters
vectorThe vector that needs to be converted
tensorThe output tensor
detachIf the gradient information needs to be detached during the conversion

Definition at line 71 of file LibtorchUtils.C.

Referenced by LibtorchNeuralNetControl::prepareInputTensor().

72 {
73  tensor = vectorToTensorCopy(vector, {long(vector.size()), 1});
74 
75  if (detach)
76  tensor = tensor.detach();
77 }
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

◆ vectorToTensor< Real >()

template void LibtorchUtils::vectorToTensor< Real > ( const std::vector< Real > &  vector,
torch::Tensor tensor,
const bool  detach 
)

◆ vectorToTensorCopy()

template<typename DataType >
torch::Tensor LibtorchUtils::vectorToTensorCopy ( const std::vector< DataType > &  vector,
c10::IntArrayRef  sizes 
)

Utility function that creates an owning tensor copy of a standard vector.

Template Parameters
DataTypeThe vector element type
Parameters
vectorThe vector that needs to be copied
sizesThe desired tensor shape

Definition at line 52 of file LibtorchUtils.C.

Referenced by vectorToTensor().

53 {
54  checkTensorShape(vector, sizes);
55 
56  const auto options = tensorOptions<DataType>();
57  auto tensor = torch::empty(sizes, options);
58 
59  if (!vector.empty())
60  std::copy(vector.begin(), vector.end(), tensor.template data_ptr<DataType>());
61 
62  return tensor;
63 }

◆ vectorToTensorCopy< Real >()

template torch::Tensor LibtorchUtils::vectorToTensorCopy< Real > ( const std::vector< Real > &  vector,
c10::IntArrayRef  sizes 
)

◆ vectorToTensorView()

template<typename DataType >
torch::Tensor LibtorchUtils::vectorToTensorView ( std::vector< DataType > &  vector,
c10::IntArrayRef  sizes 
)

Utility function that creates a non-owning tensor view of a standard vector.

The returned tensor shares the mutable storage of the provided vector, so the vector must outlive the tensor and may be modified through the tensor.

Template Parameters
DataTypeThe vector element type
Parameters
vectorThe vector that needs to be wrapped
sizesThe desired tensor shape

Definition at line 85 of file LibtorchUtils.C.

86 {
87  checkTensorShape(vector, sizes);
88 
89  const auto options = tensorOptions<DataType>();
90  if (vector.empty())
91  return torch::empty(sizes, options);
92 
93  return torch::from_blob(vector.data(), sizes, options);
94 }

◆ vectorToTensorView< Real >()

template torch::Tensor LibtorchUtils::vectorToTensorView< Real > ( std::vector< Real > &  vector,
c10::IntArrayRef  sizes 
)