Skip to content

Commit c011c38

Browse files
committed
Merge branch 'master' into v0.70-tflite
2 parents ac72ec5 + c341b37 commit c011c38

File tree

19 files changed

+1858
-311
lines changed

19 files changed

+1858
-311
lines changed

src/TensorFlowNET.Core/Gradients/array_grad.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ public static Tensor[] _ReshapeGrad(Operation op, Tensor[] grads)
223223
return new Tensor[] { array_ops.reshape(grads[0], array_ops.shape(op.inputs[0])), null };
224224
}
225225

226+
[RegisterGradient("Pack")]
227+
public static Tensor[] _PackGrad(Operation op, Tensor[] grads)
228+
{
229+
var grad = grads[0];
230+
var num = op.get_attr<int>("N");
231+
var axis = op.get_attr<int>("axis");
232+
return array_ops.unstack(grad, num: num, axis: axis);
233+
}
234+
235+
[RegisterGradient("Unpack")]
236+
public static Tensor[] _UnpackGrad(Operation op, Tensor[] grads)
237+
{
238+
var axis = op.get_attr<int>("axis");
239+
return new[] { array_ops.stack(grads, axis: axis) };
240+
}
241+
226242
[RegisterGradient("Pad")]
227243
public static Tensor[] _PadGrad(Operation op, Tensor[] grads)
228244
{
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*****************************************************************************
2+
Copyright 2021 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using System.IO;
19+
using System.IO.MemoryMappedFiles;
20+
using System.Linq;
21+
using Tensorflow;
22+
23+
namespace Tensorflow.IO
24+
{
25+
public class MemmappedFileSystem
26+
{
27+
public const string MEMMAPPED_PACKAGE_DEFAULT_NAME = "memmapped_package://.";
28+
29+
private MemoryMappedFile _mmapFile;
30+
private MemmappedFileSystemDirectory _directory;
31+
32+
public MemmappedFileSystem(string path)
33+
{
34+
using (var stream = File.OpenRead(path))
35+
{
36+
// Read the offset for the directory
37+
var offsetData = new byte[sizeof(ulong)];
38+
stream.Seek(-sizeof(ulong), SeekOrigin.End);
39+
stream.Read(offsetData, 0, sizeof(ulong));
40+
var offset = BitConverter.ToUInt64(offsetData, 0);
41+
42+
var dirLength = stream.Length - (long) offset - sizeof(ulong);
43+
if (dirLength < 0)
44+
{
45+
throw new InvalidDataException("Malformed mmapped filesystem!");
46+
}
47+
48+
var dirData = new byte[dirLength];
49+
50+
stream.Seek((long) offset, SeekOrigin.Begin);
51+
stream.Read(dirData, 0, (int) dirLength);
52+
53+
_directory = MemmappedFileSystemDirectory.Parser.ParseFrom(dirData);
54+
}
55+
56+
_mmapFile = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
57+
}
58+
59+
public Stream OpenMemmapped(string filename)
60+
{
61+
var entry = _directory.Element.FirstOrDefault(x => x.Name == filename);
62+
if (entry == null)
63+
{
64+
throw new FileNotFoundException($"Missing memmaped file entry: {filename}");
65+
}
66+
67+
return _mmapFile.CreateViewStream((long) entry.Offset, (long) entry.Length);
68+
}
69+
}
70+
}

src/TensorFlowNET.Core/Operations/array_ops.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,12 @@ public static Tensor stack(Tensor[] values, int axis = 0, string name = "stack")
494494
return ops.convert_to_tensor(values, name: name);
495495
}
496496

497-
var value_shape = ops.convert_to_tensor(values[0], name: name).shape;
498-
499497
return gen_array_ops.pack(values, axis: axis, name: name);
500498
}
501499

502500
public static Tensor[] unstack(Tensor value, int? num = null, int axis = 0, string name = "unstack")
503501
{
504-
if (num == null)
505-
{
506-
value = ops.convert_to_tensor(value);
507-
var value_shape = value.shape;
508-
num = (int)value_shape.dims[axis];
509-
}
510-
502+
num = num ?? value.shape.as_int_list()[axis];
511503
return gen_array_ops.unpack(value, num: num.Value, axis: axis, name: name);
512504
}
513505

src/TensorFlowNET.Core/Operations/gen_array_ops.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,8 @@ public static (Tensor, Tensor) unique(Tensor x, TF_DataType out_idx = TF_DataTyp
265265
}
266266

267267
public static Tensor[] unpack(Tensor value, int num, int axis = 0, string name = null)
268-
{
269-
var _op = tf.OpDefLib._apply_op_helper("Unpack", name, new { value, num, axis });
270-
return _op.outputs;
271-
}
268+
=> tf.Context.ExecuteOp("Unpack", name, new ExecuteOpArgs(value, num)
269+
.SetAttributes(new { axis }));
272270

273271
public static Tensor where(Tensor condition, string name = null)
274272
{

0 commit comments

Comments
 (0)