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

Use std::get instead of cpp17 tuple unpack #108

Closed
wants to merge 7 commits into from
Closed
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
10 changes: 7 additions & 3 deletions include/cppflow/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
#include <memory>
#include <stdexcept>
#include <utility>

#include <tensorflow/c/c_api.h>
#include <tensorflow/c/eager/c_api.h>


namespace cppflow {

inline bool status_check(TF_Status* status) {
if (TF_GetCode(status) != TF_OK) {
throw std::runtime_error(TF_Message(status));
if (TF_GetCode(status) != TF_OK)
{
// @MarkJGx: This used to be a throw, but UE4 doesn't do exceptions.
const char* ErrorChar = TF_Message(status);
UE_LOG(LogTemp, Fatal, TEXT("CppFlow StatusCheck failed: %s"), *FString(std::string(ErrorChar).c_str()));
return false;
}
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion include/cppflow/datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ namespace cppflow {
return TF_UINT64;

// decode with `c++filt --type $output` for gcc
throw std::runtime_error{"Could not deduce type! type_name: " + std::string(typeid(T).name())};

UE_LOG(LogTemp, Fatal, TEXT("Could not deduce type! type_name: %s"), *FString(typeid(T).name()));
return TF_UINT64;
}

/**
Expand Down
30 changes: 22 additions & 8 deletions include/cppflow/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ namespace cppflow {

// Operation does not exist
if (!out_op.oper)
throw std::runtime_error("No operation named \"" + operation + "\" exists");

{
UE_LOG(LogTemp, Fatal, TEXT("No operation named \"%s\" exists"), *FString(operation.c_str()));
return {};
}
// DIMENSIONS

// Get number of dimensions
Expand Down Expand Up @@ -124,28 +126,40 @@ namespace cppflow {
for (int i=0; i<inputs.size(); i++) {

// Operations
const auto[op_name, op_idx] = parse_name(std::get<0>(inputs[i]));
// const auto[op_name, op_idx] = parse_name(std::get<0>(inputs[i]));
const auto parsed_name = parse_name(std::get<0>(inputs[i]));
const auto op_name = std::get<0>(parsed_name);
const auto op_idx = std::get<1>(parsed_name);

inp_ops[i].oper = TF_GraphOperationByName(this->graph.get(), op_name.c_str());
inp_ops[i].index = op_idx;

if (!inp_ops[i].oper)
throw std::runtime_error("No operation named \"" + op_name + "\" exists");

{
UE_LOG(LogTemp, Fatal, TEXT("No operation named \"%s\" exists"), *FString(op_name.c_str()));
return {};
}

// Values
inp_val[i] = std::get<1>(inputs[i]).get_tensor().get();
}

std::vector<TF_Output> out_ops(outputs.size());
auto out_val = std::make_unique<TF_Tensor*[]>(outputs.size());
for (int i=0; i<outputs.size(); i++) {
// const auto[op_name, op_idx] = parse_name(outputs[i]);
const auto parsed_name = parse_name(outputs[i]);
const auto op_name = std::get<0>(parsed_name);
const auto op_idx = std::get<1>(parsed_name);

const auto[op_name, op_idx] = parse_name(outputs[i]);
out_ops[i].oper = TF_GraphOperationByName(this->graph.get(), op_name.c_str());
out_ops[i].index = op_idx;

if (!out_ops[i].oper)
throw std::runtime_error("No operation named \"" + op_name + "\" exists");

{
UE_LOG(LogTemp, Fatal, TEXT("No operation named \"%s\" exists"), *FString(op_name.c_str()));
return {};
}
}

TF_SessionRun(this->session.get(), NULL,
Expand Down
2 changes: 1 addition & 1 deletion include/cppflow/ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace cppflow {


inline std::string to_string(const tensor &t) {
auto res_tensor = string_format({t.shape(), t}, "(tensor: shape=%s, data=\n%s)");
auto res_tensor = string_format({t.shape(), t, to_string(t.dtype())}, "(tensor: shape=%s, data=\n%s, dtype=%s)");
auto res_tensor_h = res_tensor.get_tensor();

#ifdef TENSORFLOW_C_TF_TSTRING_H_
Expand Down
35 changes: 33 additions & 2 deletions include/cppflow/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace cppflow {
*/
template<typename T>
tensor(const std::vector<T>& values, const std::vector<int64_t>& shape);


/**
* Creates a flat tensor with the given values
Expand All @@ -51,6 +52,26 @@ namespace cppflow {
template<typename T>
tensor(const T& value);

/**
* Creates a tensor with from the given buffer and size.
* @tparam T A type that can be convertible into a tensor
* @param data value buffer.
* @param size the amount of elements in the buffer.
*/
template<typename T>
explicit tensor(const T* data, const uint32_t& size);

/**
* Creates a tensor with from the given buffer and size.
* @tparam T A type that can be convertible into a tensor
* @param data value buffer.
* @param size the amount of elements in the buffer.
* @param dimensions
*/
template<typename T>
explicit tensor(const T* data, const uint32_t& size, const std::vector<int64_t>& dimensions);


/**
* @return Shape of the tensor
*/
Expand Down Expand Up @@ -140,6 +161,15 @@ namespace cppflow {
tensor::tensor(const std::vector<T>& values, const std::vector<int64_t>& shape) :
tensor(deduce_tf_type<T>(), values.data(), values.size() * sizeof(T), shape) {}

template<typename T>
tensor::tensor(const T* data, const uint32_t& size) :
tensor(deduce_tf_type<T>(), data, size * sizeof(T), {size}) {}

template<typename T>
tensor::tensor(const T* data, const uint32_t& size, const std::vector<int64_t>& dimensions) :
tensor(deduce_tf_type<T>(), data, size * sizeof(T), dimensions) {}


template<typename T>
tensor::tensor(const std::initializer_list<T>& values) :
tensor(std::vector<T>(values), {(int64_t) values.size()}) {}
Expand Down Expand Up @@ -220,8 +250,9 @@ namespace cppflow {
if (this->dtype() != deduce_tf_type<T>()) {
auto type1 = cppflow::to_string(deduce_tf_type<T>());
auto type2 = cppflow::to_string(this->dtype());
auto error = "Datatype in function get_data (" + type1 + ") does not match tensor datatype (" + type2 + ")";
throw std::runtime_error(error);
std::string error = "Datatype in function get_data (" + type1 + ") does not match tensor datatype (" + type2 + ")";
UE_LOG(LogTemp, Fatal, *FString(error.c_str()));
return {};
}


Expand Down