↰ Return to documentation for file (morpheus/_lib/include/morpheus/utilities/tensor_util.hpp
)
#pragma once
#include "morpheus/objects/tensor_object.hpp"
#include <algorithm>// IWYU pragma: keep
#include <iosfwd>// for ostream
#include <string>// for string
#include <vector>// for vector
// <algorithm> is needed for copy, min_element & transform
namespace morpheus {
struct TensorUtils
{
using shape_type_t = std::vector<TensorIndex>;
static void write_shape_to_stream(const shape_type_t& shape, std::ostream& os);
static std::string shape_to_string(const shape_type_t& shape);
static void set_contiguous_stride(const std::vector<TensorIndex>& shape, std::vector<TensorIndex>& stride);
static bool has_contiguous_stride(const std::vector<TensorIndex>& shape, const shape_type_t& stride);
static bool validate_shape_and_stride(const std::vector<TensorIndex>& shape,
const std::vector<TensorIndex>& stride);
template <typename IndexT, typename SrcIndexT = IndexT>
static std::vector<IndexT> get_element_stride(const std::vector<SrcIndexT>& stride)
{
std::vector<IndexT> tensor_stride(stride.size());
auto min_stride = std::min_element(stride.cbegin(), stride.cend());
auto min_stride_val = *min_stride;
std::transform(stride.cbegin(), stride.cend(), tensor_stride.begin(), [min_stride_val](const SrcIndexT s) {
return s / min_stride_val;
});
return tensor_stride;
}
};
// end of group
} // namespace morpheus