forked from migueldeicaza/TensorFlowSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpGenerator.cs
More file actions
658 lines (574 loc) · 16.9 KB
/
OpGenerator.cs
File metadata and controls
658 lines (574 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//
// This is the driver for the operation generator, this takes data that
// is provided by the Tensorflow runtime to produce strongly-typed and
// high level methods on the TFGraph class.
//
// The result is generated into a partial class that is lined with the
// main TensorFlowSharp library
//
// Authors:
// Miguel de Icaza
//
// Copyright 2017, the year of downfall, Microsoft Inc
//
#pragma warning disable RECS0063 // Warns when a culture-aware 'StartsWith' call is used by default.
using System;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using tensorflow;
class ApiDefMap : IDisposable
{
public class Status : IDisposable
{
[DllImport ("libtensorflow")]
static extern unsafe IntPtr TF_NewStatus ();
[DllImport ("libtensorflow")]
internal static extern unsafe void TF_DeleteStatus (IntPtr status);
[DllImport ("libtensorflow")]
static extern unsafe int TF_GetCode (IntPtr s);
IntPtr handle;
public Status ()
{
handle = TF_NewStatus ();
}
void IDisposable.Dispose ()
{
TF_DeleteStatus (handle);
handle = IntPtr.Zero;
}
public bool Ok => TF_GetCode (handle) == 0;
public bool Error => TF_GetCode (handle) != 0;
public static implicit operator IntPtr (Status s)
{
return s.handle;
}
}
[DllImport ("libtensorflow")]
unsafe extern static IntPtr TF_NewApiDefMap (IntPtr buffer, IntPtr status);
[DllImport ("libtensorflow")]
static extern void TF_DeleteApiDefMap (IntPtr handle);
[DllImport ("libtensorflow")]
static extern void TF_ApiDefMapPut (IntPtr handle, string text, IntPtr textLen, IntPtr status);
[DllImport ("libtensorflow")]
unsafe static extern OpGenerator.LLBuffer *TF_ApiDefMapGet (IntPtr handle, string name, IntPtr nameLen, IntPtr status);
IntPtr handle;
unsafe public ApiDefMap (OpGenerator.LLBuffer* buffer)
{
using (var status = new Status ()) {
handle = TF_NewApiDefMap ((IntPtr)buffer, status);
if (status.Error)
throw new ArgumentException ("Failure to call TF_NewApiDefMap");
}
}
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
~ApiDefMap ()
{
Dispose (false);
}
void Dispose (bool disposing)
{
if (disposing) {
if (handle != IntPtr.Zero) {
TF_DeleteApiDefMap (handle);
handle = IntPtr.Zero;
}
}
}
public unsafe ApiDef Get (string name)
{
using (var status = new Status ()) {
var ptr = TF_ApiDefMapGet (handle, name, (IntPtr)name.Length, status);
if (status.Error)
return null;
var ret = new byte [(int)ptr->length];
Marshal.Copy (ptr->data, ret, 0, (int)ptr->length);
var str = new MemoryStream (ret);
return Serializer.Deserialize<ApiDef> (str);
}
}
public unsafe bool Put (string text)
{
using (var status = new Status ()) {
TF_ApiDefMapPut (handle, text, (IntPtr)text.Length, status);
if (status.Error)
return false;
return true;
}
}
}
class OpGenerator
{
[DllImport ("libtensorflow")]
static extern unsafe IntPtr TF_Version ();
public static string GetVersion ()
{
return Marshal.PtrToStringAnsi (TF_Version ());
}
//
// Maps a TensorFlow type to a C# type
//
string CSharpType (string tfType)
{
bool list = false;
string cstype;
if (tfType.StartsWith ("list(")) {
list = true;
tfType = tfType.Substring (5, tfType.Length - 6);
}
switch (tfType) {
case "int":
cstype = "long"; break;
case "float":
cstype = "float"; break;
case "bool":
cstype = "bool"; break;
case "type":
cstype = "TFDataType"; break;
case "shape":
cstype = "TFShape"; break;
case "tensor":
cstype = "TFTensor"; break;
case "string":
cstype = "string"; break;
default:
Console.WriteLine ("Unknown data TensorFlow type: {0}", tfType);
return null;
}
return cstype + (list ? "[]" : "");
}
bool IsReferenceType (string tfType)
{
if (tfType.StartsWith ("list("))
return true;
if (tfType == "tensor" || tfType == "string" || tfType == "shape")
return true;
return false;
}
// Maps a parameter name to a C# acceptable name, to avoid clashes with
// language keywords
string ParamMap (string paramName)
{
switch (paramName) {
case "out":
return "output";
case "params":
return "parameters";
case "ref":
return "reference";
case "event":
return "evnt";
}
return paramName;
}
// Determines if the specified ArgDef represents a TensorFlow list
bool IsListArg (OpDef.ArgDef arg)
{
return arg.type_list_attr != "" || arg.number_attr != "";
}
//
// These values are the result of calling SetupArguments
//
Dictionary<string, bool> inferred_input_args;
List<OpDef.AttrDef> required_attrs, optional_attrs;
bool have_return_value;
void SetupArguments (OpDef def)
{
// Attributes related to the InputArg's type are inferred automatically
// and are not exposed to the client.
var inferred_input_args = new Dictionary<string, bool> ();
required_attrs = new List<OpDef.AttrDef> ();
optional_attrs = new List<OpDef.AttrDef> ();
foreach (var argdef in def.input_arg) {
if (argdef.type_attr != "")
inferred_input_args [argdef.type_attr] = true;
else if (argdef.type_list_attr != "")
inferred_input_args [argdef.type_list_attr] = true;
if (argdef.number_attr != "")
inferred_input_args [argdef.number_attr] = true;
}
foreach (var attr in def.attr) {
if (inferred_input_args.ContainsKey (attr.name))
continue;
if (attr.default_value == null)
required_attrs.Add (attr);
else
optional_attrs.Add (attr);
}
have_return_value = def.output_arg.Count > 0;
}
// Generates arguments:
// * Input arguments (TFOutput or TFOutput [])
// * All required attributes
// * variadic optional arguments
string FillArguments (OpDef def)
{
var sb = new StringBuilder ();
string comma = "";
foreach (var inarg in def.input_arg) {
string type = "TFOutput" + (IsListArg (inarg) ? "[]" : "");
sb.AppendFormat ($"{comma}{type} {ParamMap (inarg.name)}");
comma = ", ";
}
foreach (var attr in required_attrs) {
sb.AppendFormat ($"{comma}{CSharpType (attr.type)} {ParamMap (attr.name)}");
comma = ", ";
}
#if false
if (!return_is_tfoutput) {
foreach (var arg in def.output_arg) {
string type = "TFOutput" + (IsListArg (arg) ? "[]" : "");
sb.AppendFormat ($"{comma}ref {type} {ParamMap (arg.name)}");
comma = ", ";
}
}
#endif
int n = 0;
foreach (var attr in optional_attrs) {
bool reftype = IsReferenceType (attr.type);
var cstype = CSharpType (attr.type);
var cstypesuffix = reftype ? "" : "?";
sb.AppendFormat ($"{comma}{cstype}{cstypesuffix} {attr.name} = null");
comma = ", ";
}
if (sb.Length != 0)
sb.Append (", ");
return sb.ToString ();
}
void Comment (string text)
{
if (text == null || text == "")
return;
var lines = text.Split ('\n');
var open = true;
string Quote (string input)
{
var p = input.IndexOf ('`');
if (p == -1)
return input;
var res = new StringBuilder ();
foreach (var c in input) {
if (c == '`') {
res.Append (open ? "<c>" : "</c>");
open = !open;
} else
res.Append (c);
}
return res.ToString ();
}
bool blockOpen = true;
foreach (var line in lines) {
if (line.IndexOf ("in image height coordinates.") != -1) {
Console.WriteLine ("Hello");
}
var line2 = line.Trim ().Replace ("<", "<").Replace (">", ">").Replace ("&", "&");
if (line2.StartsWith ("```")){
p ("/// " + (blockOpen ? "<code>" : "</code>"));
blockOpen = !blockOpen;
if (line2 == "```python" || line2 == "```c++" || line2 == "```")
continue;
// Handle some broken comments in the api specs, they sometimes missuse the
line2 = line2.Substring (3);
if (line2.EndsWith ("```")){
var line3 = line2.Substring (0, line2.Length - 3);
p ($"/// {Quote (line3)}");
p ("/// " + (blockOpen ? "<code>" : "</code>"));
blockOpen = !blockOpen;
continue;
}
}
p ($"/// {Quote (line2)}");
}
}
// Produces the C# inline documentation
void GenDocs (OpDef oper)
{
var api = apimap.Get (oper.name);
p ("/// <summary>");
Comment (api.Summary);
p ("/// </summary>");
foreach (var input in api.InArgs) {
p ($"/// <param name=\"{ParamMap (input.Name)}\">");
Comment (input.Description);
p ($"/// </param>");
}
#if DOCS
if (!return_is_tfoutput) {
foreach (var attr in oper.output_arg) {
if (String.IsNullOrEmpty (attr.description))
continue;
p ($"/// <param name=\"{ParamMap (attr.name)}\">");
Comment (attr.description);
p ($"/// </param>");
}
}
#endif
p ("/// <param name=\"operName\">");
p ($"/// If specified, the created operation in the graph will be this one, otherwise it will be named '{oper.name}'.");
p ("/// </param>");
foreach (var attr in optional_attrs) {
p ($"/// <param name=\"{ParamMap (attr.name)}\">");
Comment ("Optional argument");
Comment (api.Attrs.Where (x=>x.Name == attr.name).FirstOrDefault ().Description);
p ($"/// </param>");
}
foreach (var attr in required_attrs) {
p ($"/// <param name=\"{ParamMap (attr.name)}\">");
Comment (api.Attrs.Where (x=>x.Name == attr.name).FirstOrDefault ().Description);
p ($"/// </param>");
}
p ($"/// <returns>");
if (have_return_value) {
if (oper.output_arg.Count == 1) {
Comment (api.OutArgs.First ().Description);
Comment ("The TFOperation can be fetched from the resulting TFOutput, by fethching the Operation property from the result.");
} else {
Comment ("Returns a tuple with multiple values, as follows:");
foreach (var arg in oper.output_arg) {
var oapi = api.OutArgs.Where (x => x.Name == arg.name).FirstOrDefault ();
Comment (ParamMap (arg.name) + ": " + oapi.Description);
}
Comment ("The TFOperation can be fetched from any of the TFOutputs returned in the tuple values, by fethching the Operation property.");
}
} else {
Comment ("Returns the description of the operation");
}
p ($"/// </returns>");
if (!String.IsNullOrEmpty (api.Description)) {
p ("/// <remarks>");
Comment (api.Description);
p ("/// </remarks>");
}
}
void SetAttribute (string type, string attrName, string csAttrName)
{
if (type == "shape") {
p ($"desc.SetAttrShape (\"{attrName}\", {csAttrName});");
return;
}
if (type.StartsWith ("list(shape")) {
p ($"desc.SetAttrShape (\"{attrName}\", {csAttrName});");
return;
}
var cstype = CSharpType (type);
switch (cstype) {
case "long":
case "long[]":
case "string":
case "string[]":
case "float":
case "float[]":
case "bool":
case "bool[]":
p ($"desc.SetAttr (\"{attrName}\", {csAttrName});");
break;
case "TFDataType":
case "TFDataType[]":
p ($"desc.SetAttrType (\"{attrName}\", {csAttrName});");
break;
// This should pass the cstatus, but requires the
// function to take a TFStatus as well, so need to weave that
// in the parameters
case "TFTensor":
case "TFTensor[]":
p ($"desc.SetAttr (\"{attrName}\", {csAttrName} /* cstatus */);");
break;
default:
throw new Exception ("Unexpected type: " + cstype);
}
}
/// <summary>
/// Generate the specified oper.
/// </summary>
/// <param name="oper">Oper.</param>
void Generate (OpDef oper)
{
SetupArguments (oper);
GenDocs (oper);
var name = oper.name;
string retType;
if (have_return_value) {
if (oper.output_arg.Count > 1) {
var rb = new StringBuilder ("(");
foreach (var arg in oper.output_arg) {
rb.AppendFormat ("TFOutput{0} {1}, ", IsListArg (arg) ? "[]" : "", ParamMap (arg.name));
}
rb.Remove (rb.Length - 2, 2);
rb.Append (")");
retType = rb.ToString ();
} else
retType = "TFOutput" + (IsListArg (oper.output_arg.First ()) ? "[]" : "");
} else
retType = "TFOperation";
p ($"public {retType} {name} ({FillArguments(oper)}string operName = null)");
pi ("{");
bool needStatus = required_attrs.Concat (optional_attrs).Any (attr => attr.type.Contains ("TFTensor"));
p ($"var desc = new TFOperationDesc (this, \"{oper.name}\", MakeName (\"{oper.name}\", operName));");
foreach (var arg in oper.input_arg) {
if (IsListArg (arg))
p ($"desc.AddInputs ({ParamMap (arg.name)});");
else
p ($"desc.AddInput ({ParamMap (arg.name)});");
}
pi ("foreach ( TFOperation control in CurrentDependencies )");
p ("desc.AddControlInput (control);");
pd ("");
// If we have attributes
if (required_attrs.Count > 0 || optional_attrs.Count > 0) {
foreach (var attr in required_attrs) {
SetAttribute (attr.type, attr.name, ParamMap (attr.name));
}
foreach (var attr in optional_attrs) {
var reftype = IsReferenceType (attr.type);
var csattr = ParamMap (attr.name);
if (reftype)
pi ($"if ({csattr} != null)");
else
pi ($"if ({csattr}.HasValue)");
SetAttribute (attr.type, attr.name, csattr + (reftype ? "" : ".Value"));
pd ("");
}
}
p ("var op = desc.FinishOperation ();");
if (oper.output_arg.Count () > 0)
p ("int _idx = 0;");
if (oper.output_arg.Any (x => IsListArg (x)))
p ("int _n = 0;");
foreach (var arg in oper.output_arg) {
if (IsListArg (arg)) {
var outputs = new StringBuilder ();
p ($"_n = op.OutputListLength (\"{ParamMap (arg.name)}\");");
p ($"var {ParamMap (arg.name)} = new TFOutput [_n];");
pi ("for (int i = 0; i < _n; i++)");
p ($"{ParamMap (arg.name)} [i] = new TFOutput (op, _idx++);");
pd ("");
} else {
p ($"var {ParamMap (arg.name)} = new TFOutput (op, _idx++);");
}
}
if (have_return_value) {
if (oper.output_arg.Count == 1) {
p ($"return {ParamMap (oper.output_arg.First ().name)};");
} else {
;
p ("return (" + oper.output_arg.Select (x => ParamMap (x.name)).Aggregate ((i, j) => (i + ", " + j)) + ");");
}
} else {
p ("return op;");
}
pd ("}\n");
}
[StructLayout (LayoutKind.Sequential)]
internal struct LLBuffer
{
internal IntPtr data;
internal IntPtr length;
internal IntPtr data_deallocator;
}
[DllImport ("libtensorflow")]
unsafe extern static LLBuffer *TF_GetAllOpList ();
ApiDefMap apimap;
MemoryStream GetOpsList ()
{
unsafe
{
LLBuffer* ptr = TF_GetAllOpList ();
apimap = new ApiDefMap (ptr);
var ret = new byte [(int)ptr->length];
Marshal.Copy (ptr->data, ret, 0, (int)ptr->length);
return new MemoryStream (ret);
}
}
// Incorporates out-of-band data into the API definitions that we pulled out of GetAllOpList
void UpdateApis (string [] dirs)
{
foreach (var dir in dirs) {
foreach (var f in Directory.GetFiles (dir)) {
var s = File.ReadAllText (f);
apimap.Put (s);
}
}
}
void Run (string [] dirs)
{
output = File.CreateText ("../../../TensorFlowSharp/Operations.g.cs");
var operations = Serializer.Deserialize<List<OpDef>> (GetOpsList ());
UpdateApis (dirs);
p ("using System;\n");
pi ("namespace TensorFlow {");
pi ("public partial class TFGraph {");
foreach (var oper in (from o in operations orderby o.name select o)){
// Skip internal operations
if (oper.name.StartsWith ("_"))
continue;
// Ignore functions where we lack a C# type mapping
if (oper.attr.Any (attr => CSharpType (attr.type) == null)) {
var attr = oper.attr.First (a => CSharpType (a.type) == null);
Console.WriteLine ($"SkipTYPE: {oper.name} due to attribute ({attr.type} {attr.name}) lacking a mapping to C#");
continue;
}
#if false
// Ignore reference types as well (per go's binding)
if (oper.input_arg.Any (ia => ia.is_ref)) {
var pars = String.Join (", ", oper.input_arg.Where (x => x.is_ref).Select (x => $"{x.type} {x.name}"));
Console.WriteLine ($"SkipInREF: {oper.name} parameters with is_ref: {pars}");
continue;
}
// Ignore reference types as well (per go's binding)
if (oper.output_arg.Any (ia => ia.is_ref)) {
var pars = String.Join (", ", oper.output_arg.Where (x => x.is_ref).Select (x => $"{x.type} {x.name}"));
var all = String.Join (", ", oper.input_arg.Select (x => $"{x.type} {x.name}"));
Console.WriteLine ($"SkipOutREF: {oper.name} parameters with is_ref: {pars} all: {all}");
continue;
}
#endif
var def = apimap.Get (oper.name);
// Undocumented operation, perhaps we should not surface
if (def.Summary == "")
continue;
Generate (oper);
}
pd ("}");
pd ("}");
output.Close ();
}
// The output file
StreamWriter output;
int indent = 0;
// Convenience methods to generate output
void pi (string fmt, params object [] args)
{
p (fmt, args);
indent++;
}
void pd (string fmt, params object [] args)
{
indent--;
p (fmt, args);
}
void p (string fmt, params object [] args)
{
for (int i = 0; i < indent; i++)
output.Write ("\t");
if (args.Length == 0)
output.WriteLine (fmt);
else
output.WriteLine (fmt, args);
}
public static void Main (string [] args)
{
Console.WriteLine ("Getting code for {0}", GetVersion ());
if (Marshal.SizeOf (typeof (IntPtr)) != 8)
throw new Exception ("Need to run in 64");
if (args.Length == 0)
args = new string [] { "/cvs/tensorflow/tensorflow/core/api_def/base_api" };
new OpGenerator ().Run (args);
}
}