11 #include <fmt/format.h>
15 #include <unordered_map>
22 const int64_t primary_size = GetPrimarySize();
23 for (
auto& kv : *
this) {
24 if (kv.second.GetLength() != primary_size) {
31 void TensorMap::AssertPrimaryKeyInMapOrEmpty()
const {
32 if (
size() != 0 &&
count(primary_key_) == 0) {
38 void TensorMap::AssertNoReservedKeys()
const {
39 const std::unordered_set<std::string>& reserved_keys =
GetReservedKeys();
40 for (
const auto& kv : *
this) {
41 if (reserved_keys.count(kv.first)) {
50 const int64_t primary_size = GetPrimarySize();
52 ss <<
fmt::format(
"Primary Tensor \"{}\" has size {}, however: \n",
53 primary_key_, primary_size);
54 for (
auto& kv : *
this) {
55 if (kv.first != primary_key_ &&
56 kv.second.GetLength() != primary_size) {
57 ss <<
fmt::format(
" > Tensor \"{}\" has size {}.\n",
58 kv.first, kv.second.GetLength());
66 for (
const auto& kv : *
this) {
67 if (!kv.second.IsContiguous()) {
76 for (
const auto& kv : *
this) {
78 tensor_map_contiguous[kv.first] = kv.second.
Contiguous();
80 return tensor_map_contiguous;
84 const static std::unordered_set<std::string> reserved_keys = {
129 "is_size_synchronized",
130 "assert_size_synchronized",
132 return reserved_keys;
139 return fmt::format(
"TensorMap(primary_key=\"{}\") with no attribute",
143 size_t max_key_len = 0;
144 bool has_primary_key =
false;
145 std::vector<std::string> keys;
146 keys.reserve(
size());
147 for (
const auto& kv : *
this) {
148 const std::string key = kv.first;
150 max_key_len =
std::max(max_key_len, key.size());
151 if (key == primary_key) {
152 has_primary_key =
true;
155 std::sort(keys.begin(), keys.end());
158 " - {{:<{}}}: shape={{}}, dtype={{}}, device={{}}", max_key_len);
160 std::stringstream ss;
161 ss <<
fmt::format(
"TensorMap(primary_key=\"{}\") with {} attribute{}:",
162 primary_key,
size(),
size() > 1 ?
"s" :
"")
164 for (
const std::string& key : keys) {
169 if (key == primary_key) {
175 const std::string example_key = has_primary_key ? primary_key : keys[0];
176 ss <<
fmt::format(
" (Use . to access attributes, e.g., tensor_map.{})",
filament::Texture::InternalFormat format
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
std::string ToString() const
std::string ToString() const
Device GetDevice() const override
SizeVector GetShape() const
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
std::string ToString() const
Print the TensorMap to string.
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
static std::unordered_set< std::string > GetReservedKeys()
Get reserved keys for the map. A map cannot contain any of these keys.
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
bool IsContiguous() const
TensorMap Contiguous() const
QTextStream & endl(QTextStream &stream)
Generic file read and write utility for python interface.