FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add c_api.StringPiece to avoid crash for unmanaged memory. · MSavameri/TensorFlow.NET@4fa14fe · GitHub

Repository navigation

Commit 4fa14fe

Browse files
committed
add c_api.StringPiece to avoid crash for unmanaged memory.
1 parent 740ca28 commit 4fa14fe

10 files changed

Lines changed: 38 additions & 42 deletions

File tree

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
34
using System.Text;
45

56
namespace Tensorflow
@@ -14,16 +15,16 @@ public class Operation
1415

1516
private Status status = new Status();
1617

17-
public string name { get; }
18-
public string optype { get; }
19-
public string device { get; }
20-
public int NumOutputs { get; }
21-
public TF_DataType OutputType { get; }
22-
public int OutputListLength { get; }
23-
public int NumInputs { get; }
24-
public int NumConsumers { get; }
25-
public int NumControlInputs { get; }
26-
public int NumControlOutputs { get; }
18+
public string name => c_api.StringPiece(c_api.TF_OperationName(_handle));
19+
public string optype => c_api.StringPiece(c_api.TF_OperationOpType(_handle));
20+
public string device => c_api.StringPiece(c_api.TF_OperationDevice(_handle));
21+
public int NumOutputs => c_api.TF_OperationNumOutputs(_handle);
22+
public TF_DataType OutputType => c_api.TF_OperationOutputType(new TF_Output(_handle, 0));
23+
public int OutputListLength => c_api.TF_OperationOutputListLength(_handle, "output", status);
24+
public int NumInputs => c_api.TF_OperationNumInputs(_handle);
25+
public int NumConsumers => c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, 0));
26+
public int NumControlInputs => c_api.TF_OperationNumControlInputs(_handle);
27+
public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle);
2728

2829
private Tensor[] _outputs;
2930
public Tensor[] outputs => _outputs;
@@ -35,17 +36,6 @@ public Operation(IntPtr handle)
3536
return;
3637

3738
_handle = handle;
38-
39-
name = c_api.TF_OperationName(_handle);
40-
optype = c_api.TF_OperationOpType(_handle);
41-
device = "";// c_api.TF_OperationDevice(_handle);
42-
NumOutputs = c_api.TF_OperationNumOutputs(_handle);
43-
OutputType = c_api.TF_OperationOutputType(new TF_Output(_handle, 0));
44-
OutputListLength = c_api.TF_OperationOutputListLength(_handle, "output", status);
45-
NumInputs = c_api.TF_OperationNumInputs(_handle);
46-
NumConsumers = c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, 0));
47-
NumControlInputs = c_api.TF_OperationNumControlInputs(_handle);
48-
NumControlOutputs = c_api.TF_OperationNumControlOutputs(_handle);
4939
}
5040

5141
public Operation(Graph g, string opType, string oper_name)
@@ -62,8 +52,8 @@ public Operation(NodeDef node_def, Graph g, List<Tensor> inputs = null, TF_DataT
6252
Graph = g;
6353

6454
_id_value = Graph._next_id();
55+
6556
_handle = ops._create_c_op(g, node_def, inputs);
66-
NumOutputs = c_api.TF_OperationNumOutputs(_handle);
6757

6858
_outputs = new Tensor[NumOutputs];
6959
for (int i = 0; i < NumOutputs; i++)

‎src/TensorFlowNET.Core/Operations/c_api.ops.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static partial class c_api
3838
public static extern IntPtr TF_NewOperation(IntPtr graph, string opType, string oper_name);
3939

4040
[DllImport(TensorFlowLibName)]
41-
public static extern string TF_OperationDevice(IntPtr oper);
41+
public static extern IntPtr TF_OperationDevice(IntPtr oper);
4242

4343
/// <summary>
4444
/// Sets `output_attr_value` to the binary-serialized AttrValue proto
@@ -50,13 +50,13 @@ public static partial class c_api
5050
public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, IntPtr status);
5151

5252
[DllImport(TensorFlowLibName)]
53-
public static extern string TF_OperationName(IntPtr oper);
53+
public static extern IntPtr TF_OperationName(IntPtr oper);
5454

5555
[DllImport(TensorFlowLibName)]
5656
public static extern int TF_OperationNumInputs(IntPtr oper);
5757

5858
[DllImport(TensorFlowLibName)]
59-
public static extern string TF_OperationOpType(IntPtr oper);
59+
public static extern IntPtr TF_OperationOpType(IntPtr oper);
6060

6161
/// <summary>
6262
/// Get the number of control inputs to an operation.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten
3030
// Add inputs
3131
if(inputs != null && inputs.Count > 0)
3232
{
33-
/*foreach (var op_input in inputs)
33+
foreach (var op_input in inputs)
3434
{
3535
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
36-
}*/
36+
}
3737

38-
c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
38+
//c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
3939
}
4040

4141
var status = new Status();

‎src/TensorFlowNET.Core/Status/Status.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Status
1515
/// <summary>
1616
/// Error message
1717
/// </summary>
18-
public string Message => c_api.TF_Message(_handle);
18+
public string Message => c_api.StringPiece(c_api.TF_Message(_handle));
1919

2020
/// <summary>
2121
/// Error code

‎src/TensorFlowNET.Core/Status/c_api.status.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public static partial class c_api
1212
/// </summary>
1313
/// <param name="s"></param>
1414
[DllImport(TensorFlowLibName)]
15-
public static unsafe extern void TF_DeleteStatus(IntPtr s);
15+
public static extern void TF_DeleteStatus(IntPtr s);
1616

1717
/// <summary>
1818
/// Return the code record in *s.
1919
/// </summary>
2020
/// <param name="s"></param>
2121
/// <returns></returns>
2222
[DllImport(TensorFlowLibName)]
23-
public static extern unsafe TF_Code TF_GetCode(IntPtr s);
23+
public static extern TF_Code TF_GetCode(IntPtr s);
2424

2525
/// <summary>
2626
/// Return a pointer to the (null-terminated) error message in *s.
@@ -30,7 +30,7 @@ public static partial class c_api
3030
/// <param name="s"></param>
3131
/// <returns></returns>
3232
[DllImport(TensorFlowLibName)]
33-
public static extern unsafe string TF_Message(IntPtr s);
33+
public static extern IntPtr TF_Message(IntPtr s);
3434

3535
/// <summary>
3636
/// Return a new status object.

‎src/TensorFlowNET.Core/c_api.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ namespace Tensorflow
1212
/// The API leans towards simplicity and uniformity instead of convenience
1313
/// since most usage will be by language specific wrappers.
1414
///
15-
/// The params type mapping between .net and c_api
15+
/// The params type mapping between c_api and .NET
1616
/// TF_XX** => ref IntPtr (TF_Operation** op) => (ref IntPtr op)
1717
/// TF_XX* => IntPtr (TF_Graph* graph) => (IntPtr graph)
1818
/// struct => struct (TF_Output output) => (TF_Output output)
19+
/// struct* => struct (TF_Output* output) => (TF_Output[] output)
1920
/// const char* => string
2021
/// int32_t => int
2122
/// int64_t* => long[]
2223
/// size_t* => unlong[]
2324
/// void* => IntPtr
25+
/// string => IntPtr c_api.StringPiece(IntPtr)
2426
/// </summary>
2527
public static partial class c_api
2628
{
2729
public const string TensorFlowLibName = "tensorflow";
2830

31+
public static string StringPiece(IntPtr handle)
32+
{
33+
return handle == IntPtr.Zero ? String.Empty : Marshal.PtrToStringAnsi(handle);
34+
}
35+
2936
public delegate void Deallocator(IntPtr data, IntPtr size, ref bool deallocator);
3037

3138
[DllImport(TensorFlowLibName)]

‎src/TensorFlowNET.Core/tf.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void enable_eager_execution()
3737
context.default_execution_mode = Context.EAGER_MODE;
3838
}
3939

40-
public static string VERSION => Marshal.PtrToStringAnsi(c_api.TF_Version());
40+
public static string VERSION => c_api.StringPiece(c_api.TF_Version());
4141

4242
public static Graph get_default_graph()
4343
{

‎test/TensorFlowNET.UnitTest/GraphTest.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,14 @@ public void c_api_Graph()
3939
// Test not found errors in TF_Operation*() query functions.
4040
Assert.AreEqual(-1, c_api.TF_OperationOutputListLength(feed, "bogus", s));
4141
Assert.AreEqual(TF_Code.TF_INVALID_ARGUMENT, s.Code);
42-
//Assert.IsFalse(c_test_util.GetAttrValue(feed, "missing", ref attr_value, s));
43-
//Assert.AreEqual("Operation '' has no attr named 'missing'.", s.Message);
42+
Assert.IsFalse(c_test_util.GetAttrValue(feed, "missing", ref attr_value, s));
43+
Assert.AreEqual("Operation 'feed' has no attr named 'missing'.", s.Message);
4444

4545
// Make a constant oper with the scalar "3".
4646
var three = c_test_util.ScalarConst(3, graph, s);
4747

4848
// Add oper.
4949
var add = c_test_util.Add(feed, three, graph, s);
50-
51-
NodeDef node_def = null;
52-
c_test_util.GetNodeDef(feed, ref node_def);
5350
}
5451
}
5552
}

‎test/TensorFlowNET.UnitTest/OperationsTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void addInPlaceholder()
4343
public void addInConstant()
4444
{
4545
var a = tf.constant(4.0f);
46-
var b = tf.placeholder(tf.float32);
46+
var b = tf.constant(5.0f);
4747
var c = tf.add(a, b);
4848

4949
using (var sess = tf.Session())

‎test/TensorFlowNET.UnitTest/c_test_util.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ public static void AddOpHelper(Operation l, Operation r, Graph graph, Status s,
2323
{
2424
var desc = c_api.TF_NewOperation(graph, "AddN", name);
2525

26-
c_api.TF_AddInputList(desc, new TF_Output[]
26+
var inputs = new TF_Output[]
2727
{
2828
new TF_Output(l, 0),
2929
new TF_Output(r, 0),
30-
}, 2);
30+
};
31+
32+
c_api.TF_AddInputList(desc, inputs, inputs.Length);
3133

3234
op = c_api.TF_FinishOperation(desc, s);
3335
s.Check();

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL