Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal change #4741

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

* Added a helper function to get a vector of strings for the elements of a
tensor in order to aid in formatting.

# Release 0.80.0

## Breaking Changes
Expand Down
22 changes: 22 additions & 0 deletions tensorflow_federated/cc/core/impl/aggregation/core/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
#include <cmath>
#include <cstddef>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h"
#include "tensorflow_federated/cc/core/impl/aggregation/core/agg_vector.h"
Expand Down Expand Up @@ -108,6 +111,13 @@ class Tensor final {
return AggVector<T>(data_.get());
}

// Returns the elements of the tensor as a vector of strings. This can be
// called on tensors of any type. The elements of the tensor are formatted as
// strings using absl::StrCat.
std::vector<std::string> AsStringVector() const {
DTYPE_CASES(dtype_, T, return TensorValuesToStringVector<T>());
}

// Provides access to the (numerical) tensor data as an integral scalar.
// Values are automatically casted and rounded.
template <typename T, typename std::enable_if<
Expand Down Expand Up @@ -174,6 +184,18 @@ class Tensor final {
return reinterpret_cast<const T*>(data_->data());
}

template <typename T>
std::vector<std::string> TensorValuesToStringVector() const {
std::vector<std::string> vec(num_elements());
if (num_elements() > 0) {
AggVector<T> agg_vector = AsAggVector<T>();
for (auto [i, v] : agg_vector) {
vec[i] = absl::StrCat(v);
}
}
return vec;
}

// Tensor data type.
DataType dtype_;
// Tensor shape.
Expand Down