10 #include <type_traits>
18 namespace eigen_converter {
27 template <
typename func_t>
31 func(indexer.GetInputPtr(0, i), i);
36 static Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
38 static_assert(std::is_same<T, double>::value ||
39 std::is_same<T, float>::value ||
40 std::is_same<T, int>::value,
41 "Only supports double, float and int (MatrixXd, MatrixXf and "
46 if (dim.
size() != 2) {
48 " [TensorToEigenMatrix]: Number of dimensions supported = 2, "
55 T *data_ptr = tensor_cpu_contiguous.
GetDataPtr<T>();
58 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
59 eigen_matrix(data_ptr, dim[0], dim[1]);
61 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
62 eigen_matrix_copy(eigen_matrix);
63 return eigen_matrix_copy;
66 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
68 return TensorToEigenMatrix<double>(tensor);
71 Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
73 return TensorToEigenMatrix<float>(tensor);
76 Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
78 return TensorToEigenMatrix<int>(tensor);
81 template <
typename T,
int N>
87 (std::is_same<T, double>::value || std::is_same<T, int>::value) &&
89 "Only supports double and int (VectorNd and VectorNi) with N>0.");
91 if (std::is_same<T, double>::value) {
93 }
else if (std::is_same<T, int>::value) {
96 if (dtype.
ByteSize() * N !=
sizeof(Eigen::Matrix<T, N, 1>)) {
98 dtype.
ByteSize() * N,
sizeof(Eigen::Matrix<T, N, 1>));
104 std::vector<Eigen::Matrix<T, N, 1>> eigen_vector(tensor.
GetLength());
112 template <
typename T,
int N>
114 const std::vector<Eigen::Matrix<T, N, 1>> &values,
120 (std::is_same<T, double>::value || std::is_same<T, int>::value) &&
122 "Only supports double and int (VectorNd and VectorNi) with N>0.");
124 int64_t num_values =
static_cast<int64_t
>(values.size());
136 *
static_cast<scalar_t *
>(ptr) =
static_cast<scalar_t
>(
137 values[workload_idx / N](workload_idx % N));
142 return tensor_cpu.
To(device);
147 return TensorToEigenVectorNxVector<double, 2>(tensor);
152 return TensorToEigenVectorNxVector<double, 3>(tensor);
157 return TensorToEigenVectorNxVector<int, 3>(tensor);
162 return TensorToEigenVectorNxVector<int, 2>(tensor);
166 const std::vector<Eigen::Vector3d> &values,
173 const std::vector<Eigen::Vector2d> &values,
180 const std::vector<Eigen::Vector3i> &values,
186 const std::vector<Eigen::Vector2i> &values,
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
#define AssertTensorShape(tensor,...)
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Tensor Contiguous() const
int64_t GetLength() const
int64_t NumElements() const
Device GetDevice() const override
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
SizeVector GetShape() const
Tensor To(Dtype dtype, bool copy=false) const
static Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > TensorToEigenMatrix(const core::Tensor &tensor)
static core::Tensor EigenVectorNxVectorToTensor(const std::vector< Eigen::Matrix< T, N, 1 >> &values, core::Dtype dtype, const core::Device &device)
std::vector< Eigen::Vector2i > TensorToEigenVector2iVector(const core::Tensor &tensor)
Converts a tensor of shape (N, 2) to std::vector<Eigen::Vector2i>. An exception will be thrown if the...
core::Tensor EigenVector2iVectorToTensor(const std::vector< Eigen::Vector2i > &values, core::Dtype dtype, const core::Device &device)
Converts a vector of Eigen::Vector2i to a (N, 2) tensor. This function also takes care of dtype conve...
static std::vector< Eigen::Matrix< T, N, 1 > > TensorToEigenVectorNxVector(const core::Tensor &tensor)
Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > TensorToEigenMatrixXi(const core::Tensor &tensor)
Converts a 2D tensor to Eigen::MatrixXi of same shape. Regardless of the tensor dtype,...
std::vector< Eigen::Vector2d > TensorToEigenVector2dVector(const core::Tensor &tensor)
Converts a tensor of shape (N, 2) to std::vector<Eigen::Vector2d>. An exception will be thrown if the...
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > TensorToEigenMatrixXf(const core::Tensor &tensor)
Converts a 2D tensor to Eigen::MatrixXf of same shape. Regardless of the tensor dtype,...
std::vector< Eigen::Vector3d > TensorToEigenVector3dVector(const core::Tensor &tensor)
Converts a tensor of shape (N, 3) to std::vector<Eigen::Vector3d>. An exception will be thrown if the...
core::Tensor EigenVector3dVectorToTensor(const std::vector< Eigen::Vector3d > &values, core::Dtype dtype, const core::Device &device)
Converts a vector of Eigen::Vector3d to a (N, 3) tensor. This function also takes care of dtype conve...
std::vector< Eigen::Vector3i > TensorToEigenVector3iVector(const core::Tensor &tensor)
Converts a tensor of shape (N, 3) to std::vector<Eigen::Vector3i>. An exception will be thrown if the...
static void LaunchIndexFillKernel(const Indexer &indexer, const func_t &func)
core::Tensor EigenVector2dVectorToTensor(const std::vector< Eigen::Vector2d > &values, core::Dtype dtype, const core::Device &device)
Converts a vector of Eigen::Vector2d to a (N, 2) tensor. This function also takes care of dtype conve...
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > TensorToEigenMatrixXd(const core::Tensor &tensor)
Converts a 2D tensor to Eigen::MatrixXd of same shape. Regardless of the tensor dtype,...
core::Tensor EigenVector3iVectorToTensor(const std::vector< Eigen::Vector3i > &values, core::Dtype dtype, const core::Device &device)
Converts a vector of Eigen::Vector3i to a (N, 3) tensor. This function also takes care of dtype conve...
void ParallelFor(const Device &device, int64_t n, const func_t &func)
constexpr nullopt_t nullopt
Generic file read and write utility for python interface.