aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/c/c_api_test.cc
diff options
context:
space:
mode:
authorGravatar Asim Shankar <ashankar@google.com>2016-11-15 00:15:48 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-11-15 00:24:28 -0800
commit1f0c5119a0230c5160d45496175b9256f097e144 (patch)
tree107fd56ab6f05ce743d1867893c4dd75bd7a98f4 /tensorflow/c/c_api_test.cc
parent1791aeef881915d75c1d077ae77a205e1bc2d51c (diff)
C API: Do not take ownership of the TF_Tensors
Prior to this change, TF_*Run, TF_SetAttrTensor and TF_SetAttrTensorList took ownership of the TF_Tensor*s of the feeds. This can make performance client languages bothersome when the same Tensor is repeatedly fed into multiple session executions as the memory for the feed tensor would need to be re-allocated and filled in every time. With this change, these functions no longer take ownership of the TF_Tensor*. The changes to the Go API implementation reflect the claimed benefits. (Another step towards #10) Change: 139169388
Diffstat (limited to 'tensorflow/c/c_api_test.cc')
-rw-r--r--tensorflow/c/c_api_test.cc15
1 files changed, 11 insertions, 4 deletions
diff --git a/tensorflow/c/c_api_test.cc b/tensorflow/c/c_api_test.cc
index ca1324033d..ad0e6a7da8 100644
--- a/tensorflow/c/c_api_test.cc
+++ b/tensorflow/c/c_api_test.cc
@@ -45,6 +45,9 @@ TF_Tensor* TF_Tensor_EncodeStrings(const Tensor& src);
namespace {
+typedef std::unique_ptr<TF_Tensor, decltype(&TF_DeleteTensor)>
+ unique_tensor_ptr;
+
TEST(CAPI, Version) { EXPECT_NE("", string(TF_Version())); }
TEST(CAPI, Status) {
@@ -259,8 +262,9 @@ TF_Operation* Placeholder(TF_Graph* graph, TF_Status* s) {
}
TF_Operation* ScalarConst(int32 v, TF_Graph* graph, TF_Status* s) {
+ unique_tensor_ptr tensor(Int32Tensor(v), TF_DeleteTensor);
TF_OperationDescription* desc = TF_NewOperation(graph, "Const", "scalar");
- TF_SetAttrTensor(desc, "value", Int32Tensor(v), s);
+ TF_SetAttrTensor(desc, "value", tensor.get(), s);
if (TF_GetCode(s) != TF_OK) return nullptr;
TF_SetAttrType(desc, "dtype", TF_INT32);
return TF_FinishOperation(desc, s);
@@ -752,8 +756,7 @@ class CSession {
inputs_.size(), outputs_ptr, output_values_ptr,
outputs_.size(), targets_ptr, targets_.size(), nullptr, s);
- // TF_SessionRun() takes ownership of the tensors in input_values_.
- input_values_.clear();
+ DeleteInputValues();
}
void CloseAndDelete(TF_Status* s) {
@@ -1261,7 +1264,8 @@ TEST_F(CApiAttributesTest, Tensor) {
const size_t ndims = TF_ARRAYSIZE(dims);
auto desc = init("tensor");
- TF_SetAttrTensor(desc, "v", Int8Tensor(dims, ndims, tensor), s_);
+ unique_tensor_ptr v(Int8Tensor(dims, ndims, tensor), TF_DeleteTensor);
+ TF_SetAttrTensor(desc, "v", v.get(), s_);
ASSERT_EQ(TF_OK, TF_GetCode(s_)) << TF_Message(s_);
auto oper = TF_FinishOperation(desc, s_);
@@ -1296,6 +1300,9 @@ TEST_F(CApiAttributesTest, TensorList) {
Int8Tensor(dims1, ndims1, tensor1), Int8Tensor(dims2, ndims2, tensor2),
};
TF_SetAttrTensorList(desc, "v", tmp, TF_ARRAYSIZE(tmp), s_);
+ for (int i = 0; i < TF_ARRAYSIZE(tmp); ++i) {
+ TF_DeleteTensor(tmp[i]);
+ }
ASSERT_EQ(TF_OK, TF_GetCode(s_)) << TF_Message(s_);
auto oper = TF_FinishOperation(desc, s_);
ASSERT_EQ(TF_OK, TF_GetCode(s_)) << TF_Message(s_);