Skip to content

Commit 88d0e96

Browse files
committed
TensorHandleDevices
1 parent 692ba77 commit 88d0e96

15 files changed

Lines changed: 214 additions & 33 deletions

File tree

‎TensorFlow.NET.sln‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "src\Ten
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras.UnitTest", "test\Tensorflow.Keras.UnitTest\Tensorflow.Keras.UnitTest.csproj", "{EB92DD90-6346-41FB-B967-2B33A860AD98}"
1515
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Hub", "src\TensorFlowNET.Hub\Tensorflow.Hub.csproj", "{95B077C1-E21B-486F-8BDD-1C902FE687AB}"
17-
EndProject
1816
Global
1917
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2018
Debug|Any CPU = Debug|Any CPU
@@ -107,22 +105,6 @@ Global
107105
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|Any CPU.Build.0 = Release|Any CPU
108106
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.ActiveCfg = Release|Any CPU
109107
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.Build.0 = Release|Any CPU
110-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
112-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug|x64.ActiveCfg = Debug|Any CPU
113-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug|x64.Build.0 = Debug|Any CPU
114-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
115-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
116-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug-Minimal|x64.ActiveCfg = Debug|Any CPU
117-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Debug-Minimal|x64.Build.0 = Debug|Any CPU
118-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
119-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Publish|Any CPU.Build.0 = Debug|Any CPU
120-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Publish|x64.ActiveCfg = Debug|Any CPU
121-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Publish|x64.Build.0 = Debug|Any CPU
122-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
123-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Release|Any CPU.Build.0 = Release|Any CPU
124-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Release|x64.ActiveCfg = Release|Any CPU
125-
{95B077C1-E21B-486F-8BDD-1C902FE687AB}.Release|x64.Build.0 = Release|Any CPU
126108
EndGlobalSection
127109
GlobalSection(SolutionProperties) = preSolution
128110
HideSolutionNode = FALSE

‎src/TensorFlowNET.Core/APIs/tf.math.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public Tensor sub<Tx, Ty>(Tx a, Ty b, string name = null)
261261

262262

263263
public Tensor divide(Tensor a, Tensor b)
264-
=> gen_math_ops.real_div(a, b);
264+
=> a / b;
265265

266266
public Tensor sqrt(Tensor a, string name = null)
267267
=> gen_math_ops.sqrt(a, name);

‎src/TensorFlowNET.Core/Binding.Util.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public static void print(object obj)
7777
Console.WriteLine(_tostring(obj));
7878
}
7979

80+
public static void print(string format, params object[] objects)
81+
{
82+
if (!format.Contains("{}"))
83+
Console.WriteLine(format, string.Join(" ", objects.Select(x => x.ToString())));
84+
85+
foreach(var obj in objects)
86+
{
87+
88+
}
89+
90+
Console.WriteLine(format);
91+
}
92+
8093
public static int len(object a)
8194
{
8295
switch (a)

‎src/TensorFlowNET.Core/Device/c_api.device.cs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,35 @@ public partial class c_api
3737
[DllImport(TensorFlowLibName)]
3838
public static extern int TF_DeviceListCount(IntPtr list);
3939

40+
/// <summary>
41+
/// Retrieves the type of the device at the given index.
42+
/// </summary>
43+
/// <param name="list">TF_DeviceList*</param>
44+
/// <param name="index">int</param>
45+
/// <param name="status">TF_Status*</param>
46+
/// <returns></returns>
47+
[DllImport(TensorFlowLibName)]
48+
public static extern IntPtr TF_DeviceListType(IntPtr list, int index, IntPtr status);
49+
4050
/// <summary>
4151
/// Deallocates the device list.
4252
/// </summary>
4353
/// <param name="list">TF_DeviceList*</param>
4454
[DllImport(TensorFlowLibName)]
4555
public static extern void TF_DeleteDeviceList(IntPtr list);
4656

57+
/// <summary>
58+
/// Create a new TFE_TensorHandle with the same contents as 'h' but placed
59+
/// in the memory of the device name 'device_name'.
60+
/// </summary>
61+
/// <param name="h">TFE_TensorHandle*</param>
62+
/// <param name="ctx">TFE_Context*</param>
63+
/// <param name="device_name">char*</param>
64+
/// <param name="status">TF_Status*</param>
65+
/// <returns>TFE_TensorHandle*</returns>
66+
[DllImport(TensorFlowLibName)]
67+
public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status);
68+
4769
/// <summary>
4870
/// Retrieves the full name of the device (e.g. /job:worker/replica:0/...)
4971
/// The return value will be a pointer to a null terminated string. The caller
@@ -54,6 +76,6 @@ public partial class c_api
5476
/// <param name="index"></param>
5577
/// <param name="status">TF_Status*</param>
5678
[DllImport(TensorFlowLibName)]
57-
public static extern string TF_DeviceListName(IntPtr list, int index, IntPtr status);
79+
public static extern IntPtr TF_DeviceListName(IntPtr list, int index, IntPtr status);
5880
}
5981
}

‎src/TensorFlowNET.Core/Eager/c_api.eager.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ public partial class c_api
159159
[DllImport(TensorFlowLibName)]
160160
public static extern int TFE_TensorHandleNumDims(IntPtr h, IntPtr status);
161161

162+
/// <summary>
163+
/// Returns the device of the operation that produced `h`. If `h` was produced by
164+
/// a copy, returns the destination device of the copy. Note that the returned
165+
/// device name is not always the device holding the tensor handle's memory. If
166+
/// you want the latter, use TFE_TensorHandleBackingDeviceName. This function
167+
/// will block till the operation that produces `h` has completed.
168+
/// </summary>
169+
/// <param name="h">TFE_TensorHandle*</param>
170+
/// <param name="status">TF_Status*</param>
171+
/// <returns></returns>
172+
[DllImport(TensorFlowLibName)]
173+
public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, IntPtr status);
174+
175+
/// <summary>
176+
/// Returns the name of the device in whose memory `h` resides.
177+
/// </summary>
178+
/// <param name="h">TFE_TensorHandle*</param>
179+
/// <param name="status">TF_Status*</param>
180+
/// <returns></returns>
181+
[DllImport(TensorFlowLibName)]
182+
public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status);
183+
162184
/// <summary>
163185
///
164186
/// </summary>

‎src/TensorFlowNET.Core/Eager/pywrap_tfe_src.cs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static EagerTensor TFE_Py_FastPathExecute(Context ctx,
1414
string device_name,
1515
string opName,
1616
string name,
17-
params Tensor[] inputs)
17+
params object[] inputs)
1818
{
1919
IntPtr op = IntPtr.Zero;
2020
var attr_list_sizes = new Dictionary<string, int>();
@@ -42,7 +42,14 @@ public static EagerTensor TFE_Py_FastPathExecute(Context ctx,
4242
else
4343
{
4444
// The item is a single item.
45-
AddInputToOp(inputs[i], true, input_arg, op, status);
45+
switch (inputs[i])
46+
{
47+
case Tensor inputTensor:
48+
AddInputToOp(inputTensor, true, input_arg, op, status);
49+
break;
50+
default:
51+
throw new NotImplementedException("");
52+
}
4653
}
4754
}
4855

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,14 @@ public static Tensor asin(Tensor x, string name = null)
140140
return _op.outputs[0];
141141
}
142142

143-
public static EagerTensor add(Tensor x, Tensor y, string name = null)
143+
public static Tensor add<Tx, Ty>(Tx x, Ty y, string name = null)
144144
{
145145
if (tf.context.executing_eagerly())
146146
{
147-
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "Add", name, new[] { x, y });
147+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "Add", name, x, y);
148148
return _result;
149149
}
150150

151-
return null;
152-
}
153-
154-
public static Tensor add<Tx, Ty>(Tx x, Ty y, string name = null)
155-
{
156151
var _op = _op_def_lib._apply_op_helper("Add", name, args: new { x, y });
157152

158153
return _op.output;
@@ -469,6 +464,12 @@ public static Tensor log(Tensor x, string name = null)
469464

470465
public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate= false, string name= null)
471466
{
467+
if (tf.context.executing_eagerly())
468+
{
469+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "Cast", name, x, "DstT", DstT, "Truncate", Truncate);
470+
return _result;
471+
}
472+
472473
var _op = _op_def_lib._apply_op_helper("Cast", name, args: new { x, DstT, Truncate });
473474

474475
return _op.outputs[0];
@@ -490,6 +491,12 @@ public static Tensor sqrt(Tensor x, string name = null)
490491

491492
public static Tensor sub<Tx, Ty>(Tx x, Ty y, string name = null)
492493
{
494+
if (tf.context.executing_eagerly())
495+
{
496+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "Sub", name, x, y);
497+
return _result;
498+
}
499+
493500
var _op = _op_def_lib._apply_op_helper("Sub", name, args: new { x, y });
494501

495502
return _op.outputs[0];
@@ -535,6 +542,12 @@ public static Tensor atan2(Tensor y, Tensor x, string name = null)
535542

536543
public static Tensor mul<Tx, Ty>(Tx x, Ty y, string name = null)
537544
{
545+
if (tf.context.executing_eagerly())
546+
{
547+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "Mul", name, x, y);
548+
return _result;
549+
}
550+
538551
var _op = _op_def_lib._apply_op_helper("Mul", name, args: new { x, y });
539552

540553
return _op.outputs[0];
@@ -549,6 +562,12 @@ public static Tensor mul_no_nan<Tx, Ty>(Tx x, Ty y, string name = null)
549562

550563
public static Tensor real_div(Tensor x, Tensor y, string name = null)
551564
{
565+
if (tf.context.executing_eagerly())
566+
{
567+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "RealDiv", name, x, y);
568+
return _result;
569+
}
570+
552571
var _op = _op_def_lib._apply_op_helper("RealDiv", name, args: new { x, y });
553572

554573
return _op.outputs[0];
@@ -570,6 +589,12 @@ public static Tensor floor_mod(Tensor x, Tensor y, string name = null)
570589

571590
public static Tensor floor_div(Tensor x, Tensor y, string name = null)
572591
{
592+
if (tf.context.executing_eagerly())
593+
{
594+
var _result = pywrap_tfe_src.TFE_Py_FastPathExecute(tf.context, "", "FloorDiv", name, x, y);
595+
return _result;
596+
}
597+
573598
var _op = _op_def_lib._apply_op_helper("FloorDiv", name, args: new { x, y });
574599

575600
return _op.outputs[0];

‎src/TensorFlowNET.Core/Operations/math_ops.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,22 @@ public static Tensor _truediv_python3(Tensor x, Tensor y, string name = null)
712712
var x_dtype = x.dtype.as_base_dtype();
713713
var y_dtype = y.dtype.as_base_dtype();
714714

715+
if (x_dtype != y_dtype)
716+
throw new TypeError($"x and y must have the same dtype, got {x_dtype} != {y_dtype}");
717+
718+
var dtype = x_dtype switch
719+
{
720+
TF_DataType.TF_UINT8 => TF_DataType.TF_FLOAT,
721+
TF_DataType.TF_INT8 => TF_DataType.TF_FLOAT,
722+
TF_DataType.TF_INT16 => TF_DataType.TF_FLOAT,
723+
TF_DataType.TF_UINT16 => TF_DataType.TF_FLOAT,
724+
TF_DataType.TF_INT32 => TF_DataType.TF_DOUBLE,
725+
TF_DataType.TF_INT64 => TF_DataType.TF_DOUBLE,
726+
_ => x_dtype
727+
};
728+
x = cast(x, dtype);
729+
y = cast(y, dtype);
730+
715731
return gen_math_ops.real_div(x, y, name: name);
716732
});
717733
}

‎src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public partial class Tensor
130130
public static Tensor operator *(double lhs, Tensor rhs) => BinaryOpWrapper("mul", lhs, rhs);
131131
public static Tensor operator *(Tensor lhs, Complex rhs) => BinaryOpWrapper("mul", lhs, rhs);
132132
public static Tensor operator *(Complex lhs, Tensor rhs) => BinaryOpWrapper("mul", lhs, rhs);
133-
public static Tensor operator /(Tensor lhs, Tensor rhs) => BinaryOpWrapper("div", lhs, rhs);
133+
public static Tensor operator /(Tensor lhs, Tensor rhs) => BinaryOpWrapper("truediv", lhs, rhs);
134134
public static Tensor operator /(Tensor lhs, NDArray rhs) => BinaryOpWrapper("div", lhs, rhs);
135135
public static Tensor operator /(NDArray lhs, Tensor rhs) => BinaryOpWrapper("div", lhs, rhs);
136136
public static Tensor operator /(Tensor lhs, sbyte rhs) => BinaryOpWrapper("div", lhs, rhs);

‎src/TensorFlowNET.Core/ops.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public static Graph _get_graph_from_inputs(Tensor[] op_input_list, Graph graph =
9797
/// <returns></returns>
9898
public static Tensor convert_to_tensor(object value, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, TF_DataType preferred_dtype = TF_DataType.DtInvalid)
9999
{
100+
if (value is Tensor tensor)
101+
return tensor;
102+
100103
return convert_to_tensor_v2(value, dtype, preferred_dtype, name);
101104
}
102105

0 commit comments

Comments
 (0)