SafeOpHandle ShapeOp(SafeContextHandle ctx, SafeTensorHandleHandle a) { using var status = TF_NewStatus(); var op = TFE_NewOp(ctx, "Shape", status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, a, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a)); return(op); }
/// <summary> /// Execute the operation defined by <paramref name="op"/> and return handles to computed /// tensors in <paramref name="retvals"/>. /// </summary> /// <remarks> /// Upon successful return, the first <paramref name="num_retvals"/> slots in <paramref name="retvals"/> will /// contain handle instances which the caller is responsible for disposing once they are no longer in use. /// </remarks> /// <param name="op"></param> /// <param name="retvals"></param> /// <param name="num_retvals"></param> /// <param name="status"></param> public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals, out int num_retvals, SafeStatusHandle status) { unsafe { num_retvals = retvals?.Length ?? 0; var rawReturns = stackalloc IntPtr[num_retvals]; TFE_Execute(op, rawReturns, ref num_retvals, status); for (var i = 0; i < num_retvals; i++) { // A handle is created for every return, even if rawReturns[i] is null. The resulting handle will be // non-null but invalid, which is the same behavior P/Invoke gives for non-array SafeHandle return // values. retvals[i] = new SafeTensorHandleHandle(rawReturns[i]); } } }
public unsafe EagerTensorV2(NDArray nd, string device_name = "") { if (nd.typecode == NPTypeCode.String) { throw new NotImplementedException("Support for NDArray of type string not implemented yet"); } var arraySlice = nd.Unsafe.Storage.Shape.IsContiguous ? nd.GetData() : nd.CloneData(); _handle = c_api.TF_NewTensor(nd.dtype.as_dtype(), nd.shape.Select(i => (long)i).ToArray(), nd.ndim, new IntPtr(arraySlice.Address), nd.size * nd.dtypesize, deallocator: (IntPtr dataPtr, long len, IntPtr args) => { }, IntPtr.Zero); EagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.status.Handle); }
public EagerTensorV2(IntPtr handle) { EagerTensorHandle = c_api.TFE_EagerTensorHandle(handle); _handle = c_api.TFE_TensorHandleResolve(EagerTensorHandle, tf.status.Handle); }
public static extern SafeTensorHandleHandle TFE_TensorHandleCopyToDevice(SafeTensorHandleHandle h, SafeContextHandle ctx, string device_name, SafeStatusHandle status);
public static extern int TFE_TensorHandleDim(SafeTensorHandleHandle h, int dim, SafeStatusHandle status);
public static extern IntPtr TFE_TensorHandleBackingDeviceName(SafeTensorHandleHandle h, SafeStatusHandle status);
public static extern IntPtr TFE_TensorHandleResolve(SafeTensorHandleHandle h, SafeStatusHandle status);
public static extern int TFE_TensorHandleNumDims(SafeTensorHandleHandle h, SafeStatusHandle status);
public static extern void TFE_OpAddInput(SafeOpHandle op, SafeTensorHandleHandle h, SafeStatusHandle status);
public static extern TF_DataType TFE_TensorHandleDataType(SafeTensorHandleHandle h);
protected void TFE_OpAddInput(SafeOpHandle op, SafeTensorHandleHandle h, SafeStatusHandle status) => c_api.TFE_OpAddInput(op, h, status);
unsafe SafeTensorHandleHandle CreateVariable(SafeContextHandle ctx, float value, SafeStatusHandle status) { var var_handle = new SafeTensorHandleHandle[1]; int num_retvals; using (var op = TFE_NewOp(ctx, "VarHandleOp", status)) { if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } TFE_OpSetAttrType(op, "dtype", TF_FLOAT); TFE_OpSetAttrShape(op, "shape", new long[0], 0, status); TFE_OpSetAttrString(op, "container", "", 0); TFE_OpSetAttrString(op, "shared_name", "", 0); if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } TFE_Execute(op, var_handle, out num_retvals, status); if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } CHECK_EQ(1, num_retvals); } // Assign 'value' to it. using (var op = TFE_NewOp(ctx, "AssignVariableOp", status)) { if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } TFE_OpSetAttrType(op, "dtype", TF_FLOAT); TFE_OpAddInput(op, var_handle[0], status); // Convert 'value' to a TF_Tensor then a TFE_TensorHandle. var t = c_api.TF_AllocateTensor(TF_DataType.TF_FLOAT, new long[0], 0, sizeof(float)); tf.memcpy(TF_TensorData(t).ToPointer(), &value, TF_TensorByteSize(t)); var value_handle = c_api.TFE_NewTensorHandle(t, status); if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } TFE_OpAddInput(op, value_handle, status); if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } c_api.TFE_Execute(op, null, out num_retvals, status); if (TF_GetCode(status) != TF_OK) { return(new SafeTensorHandleHandle(IntPtr.Zero)); } CHECK_EQ(0, num_retvals); } return(var_handle[0]); }
protected int TFE_TensorHandleNumDims(SafeTensorHandleHandle h, SafeStatusHandle status) => c_api.TFE_TensorHandleNumDims(h, status);
protected TF_DataType TFE_TensorHandleDataType(SafeTensorHandleHandle h) => c_api.TFE_TensorHandleDataType(h);
protected SafeTensorHandleHandle TFE_TensorHandleCopyToDevice(SafeTensorHandleHandle h, SafeContextHandle ctx, string device_name, SafeStatusHandle status) => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
protected string TFE_TensorHandleBackingDeviceName(SafeTensorHandleHandle h, SafeStatusHandle status) => c_api.StringPiece(c_api.TFE_TensorHandleBackingDeviceName(h, status));
protected IntPtr TFE_TensorHandleResolve(SafeTensorHandleHandle h, SafeStatusHandle status) => c_api.TFE_TensorHandleResolve(h, status);