MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / CreateVariable

Function CreateVariable

tensorflow/c/eager/c_api_test.cc:1426–1469  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1424BENCHMARK(BM_ExecuteFunction)->Arg(0)->Arg(1);
1425
1426TFE_TensorHandle* CreateVariable(TFE_Context* ctx, float value,
1427 TF_Status* status) {
1428 // Create the variable handle.
1429 TFE_Op* op = TFE_NewOp(ctx, "VarHandleOp", status);
1430 if (TF_GetCode(status) != TF_OK) return nullptr;
1431 TFE_OpSetAttrType(op, "dtype", TF_FLOAT);
1432 TFE_OpSetAttrShape(op, "shape", {}, 0, status);
1433 TFE_OpSetAttrString(op, "container", "", 0);
1434 TFE_OpSetAttrString(op, "shared_name", "", 0);
1435 if (TF_GetCode(status) != TF_OK) return nullptr;
1436 TFE_TensorHandle* var_handle = nullptr;
1437 int num_retvals = 1;
1438 TFE_Execute(op, &var_handle, &num_retvals, status);
1439 TFE_DeleteOp(op);
1440 if (TF_GetCode(status) != TF_OK) return nullptr;
1441 CHECK_EQ(1, num_retvals);
1442
1443 // Assign 'value' to it.
1444 op = TFE_NewOp(ctx, "AssignVariableOp", status);
1445 if (TF_GetCode(status) != TF_OK) return nullptr;
1446 TFE_OpSetAttrType(op, "dtype", TF_FLOAT);
1447 TFE_OpAddInput(op, var_handle, status);
1448
1449 // Convert 'value' to a TF_Tensor then a TFE_TensorHandle.
1450 std::unique_ptr<TF_Tensor, decltype(&TF_DeleteTensor)> t(
1451 TF_AllocateTensor(TF_FLOAT, nullptr, 0, sizeof(value)), TF_DeleteTensor);
1452 memcpy(TF_TensorData(t.get()), &value, TF_TensorByteSize(t.get()));
1453
1454 std::unique_ptr<TFE_TensorHandle, decltype(&TFE_DeleteTensorHandle)>
1455 value_handle(TFE_NewTensorHandle(t.get(), status),
1456 TFE_DeleteTensorHandle);
1457 if (TF_GetCode(status) != TF_OK) return nullptr;
1458
1459 TFE_OpAddInput(op, value_handle.get(), status);
1460 if (TF_GetCode(status) != TF_OK) return nullptr;
1461
1462 num_retvals = 0;
1463 TFE_Execute(op, nullptr, &num_retvals, status);
1464 TFE_DeleteOp(op);
1465 if (TF_GetCode(status) != TF_OK) return nullptr;
1466 CHECK_EQ(0, num_retvals);
1467
1468 return var_handle;
1469}
1470
1471TEST(CAPI, Variables) {
1472 // Variables use resource handles, so this is really a test for resource

Callers 2

TESTFunction · 0.85
BM_ReadVariableFunction · 0.85

Calls 13

TFE_NewOpFunction · 0.85
TF_GetCodeFunction · 0.85
TFE_OpSetAttrTypeFunction · 0.85
TFE_OpSetAttrShapeFunction · 0.85
TFE_OpSetAttrStringFunction · 0.85
TFE_ExecuteFunction · 0.85
TFE_DeleteOpFunction · 0.85
TFE_OpAddInputFunction · 0.85
TF_AllocateTensorFunction · 0.85
TF_TensorDataFunction · 0.85
TF_TensorByteSizeFunction · 0.85
TFE_NewTensorHandleFunction · 0.85

Tested by

no test coverage detected