10 #ifdef MOOSE_LIBTORCH_ENABLED 24 template <
typename DataType>
28 if constexpr (std::is_same<DataType, double>::value)
29 return torch::TensorOptions().dtype(at::kDouble);
30 else if constexpr (std::is_same<DataType, float>::value)
31 return torch::TensorOptions().dtype(at::kFloat);
33 static_assert(Moose::always_false<DataType>,
34 "Tensor conversion is not implemented for the given data type!");
37 template <
typename DataType>
39 checkTensorShape(
const std::vector<DataType> & vector, c10::IntArrayRef sizes)
41 const auto expected_numel =
42 std::accumulate(sizes.begin(), sizes.end(), int64_t{1}, std::multiplies<int64_t>());
44 if (expected_numel != cast_int<int64_t>(vector.size()))
45 mooseError(
"The requested tensor shape is incompatible with the vector size.");
50 template <
typename DataType>
54 checkTensorShape(vector, sizes);
56 const auto options = tensorOptions<DataType>();
57 auto tensor = torch::empty(sizes, options);
60 std::copy(vector.begin(), vector.end(), tensor.template data_ptr<DataType>());
67 c10::IntArrayRef sizes);
69 template <
typename DataType>
71 vectorToTensor(
const std::vector<DataType> & vector, torch::Tensor & tensor,
const bool detach)
76 tensor = tensor.detach();
81 vectorToTensor<Real>(
const std::vector<Real> & vector, torch::Tensor & tensor,
const bool detach);
83 template <
typename DataType>
87 checkTensorShape(vector, sizes);
89 const auto options = tensorOptions<DataType>();
91 return torch::empty(sizes, options);
93 return torch::from_blob(vector.data(), sizes, options);
102 tensor = tensor.to(device_type);
109 return tensor.detach().to(tensor.options().device(at::kCPU)).contiguous();
112 template <
typename DataType>
118 tensor.data_ptr<DataType>();
120 catch (
const c10::Error & e)
123 "Cannot cast tensor values to", MooseUtils::prettyCppType<DataType>(),
"!\n", e.msg());
126 const auto & sizes = tensor.sizes();
128 long int max_size = 0;
129 for (
const auto & dim_size : sizes)
131 max_size = dim_size > max_size ? dim_size : max_size;
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()};
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
torch::Tensor vectorToTensorView(std::vector< DataType > &vector, c10::IntArrayRef sizes)
Utility function that creates a non-owning tensor view of a standard vector.
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 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.
template void vectorToTensor< Real >(const std::vector< Real > &vector, torch::Tensor &tensor, const bool detach)
template torch::Tensor vectorToTensorCopy< Real >(const std::vector< Real > &vector, c10::IntArrayRef sizes)
template torch::Tensor vectorToTensorView< Real >(std::vector< Real > &vector, c10::IntArrayRef sizes)
template void tensorToVector< Real >(torch::Tensor &tensor, std::vector< Real > &vector)
void moveToLibtorchDevice(torch::Tensor &tensor, const torch::DeviceType device_type)
Move a tensor to the configured libtorch device.