Skip to content

Commit 533f8fd

Browse files
committed
Tensor.Creation.cs: perf-ops
1 parent 4be99ec commit 533f8fd

1 file changed

Lines changed: 41 additions & 41 deletions

File tree

src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using NumSharp;
1818
using System;
19+
using System.Diagnostics.CodeAnalysis;
1920
using System.Linq;
2021
using System.Numerics;
2122
using System.Runtime.CompilerServices;
@@ -463,7 +464,7 @@ public unsafe Tensor(Complex value, TF_DataType? dType = null)
463464
*v = value;
464465
_handle = TF_NewTensor(dType ?? dtypes.as_dtype(typeof(Complex)), dims:new long[0], num_dims: 0, data: (IntPtr)v, len: (UIntPtr)sizeof(Complex), deallocator: _hGlobalDeallocator, ref _deallocatorArgs);
465466
IsMemoryOwner=true;
466-
}
467+
}
467468
#endif
468469

469470
/// <summary>
@@ -532,11 +533,10 @@ public unsafe Tensor(NDArray nd, TF_DataType? tensorDType = null)
532533

533534
private unsafe IntPtr CreateTensorFromNDArray(NDArray nd, TF_DataType? given_dtype)
534535
{
535-
if (nd.dtype.Name == "String")
536-
throw new NotImplementedException("Support for NDArray of type string not implemented yet");
536+
if (nd.dtype.Name == "String")
537+
throw new NotImplementedException("Support for NDArray of type string not implemented yet");
537538
IArraySlice arraySlice;
538-
var shape = nd.Unsafe.Storage.Shape;
539-
if (shape.IsSliced || shape.IsBroadcasted)
539+
if (nd.Unsafe.Storage.Shape.IsContiguous == false)
540540
{
541541
// the memory is NOT contiguous, so we have to copy the view into a contiguous memory block.
542542
arraySlice = nd.CloneData();
@@ -553,40 +553,40 @@ private unsafe IntPtr CreateTensorFromNDArray(NDArray nd, TF_DataType? given_dty
553553
var handle = TF_NewTensor(dtype, dims: nd.shape.Select(i=>(long)i).ToArray(), num_dims: nd.ndim, data: ptr, len: (UIntPtr)num_bytes, deallocator: _nothingDeallocator, ref _deallocatorArgs);
554554
IsMemoryOwner = false;
555555
return handle;
556-
}
557-
558-
public unsafe Tensor(byte[][] buffer, long[] shape)
559-
{
560-
int size = 0;
561-
foreach (var b in buffer)
562-
{
563-
size += (int)TF_StringEncodedSize((UIntPtr)b.Length);
564-
}
565-
int totalSize = size + buffer.Length * 8;
566-
ulong offset = 0;
567-
IntPtr handle = TF_AllocateTensor(TF_DataType.TF_STRING, shape, shape.Length, (UIntPtr)totalSize);
568-
569-
// Clear offset table
570-
IntPtr pOffset = TF_TensorData(handle);
571-
IntPtr dst = pOffset + buffer.Length * 8;
572-
IntPtr dstLimit = pOffset + totalSize;
573-
for (int i = 0; i < buffer.Length; i++)
574-
{
575-
Marshal.WriteInt64(pOffset, (long)offset);
576-
using (var status = new Status())
577-
{
578-
fixed (byte* src = &buffer[i][0])
579-
{
580-
var written = TF_StringEncode(src, (UIntPtr)buffer[i].Length, (sbyte*)dst, (UIntPtr)(dstLimit.ToInt64() - dst.ToInt64()), status);
581-
status.Check(true);
582-
pOffset += 8;
583-
dst += (int)written;
584-
offset += written;
585-
}
586-
}
587-
}
588-
589-
_handle = handle;
556+
}
557+
558+
public unsafe Tensor(byte[][] buffer, long[] shape)
559+
{
560+
int size = 0;
561+
foreach (var b in buffer)
562+
{
563+
size += (int)TF_StringEncodedSize((UIntPtr)b.Length);
564+
}
565+
int totalSize = size + buffer.Length * 8;
566+
ulong offset = 0;
567+
IntPtr handle = TF_AllocateTensor(TF_DataType.TF_STRING, shape, shape.Length, (UIntPtr)totalSize);
568+
569+
// Clear offset table
570+
IntPtr pOffset = TF_TensorData(handle);
571+
IntPtr dst = pOffset + buffer.Length * 8;
572+
IntPtr dstLimit = pOffset + totalSize;
573+
for (int i = 0; i < buffer.Length; i++)
574+
{
575+
Marshal.WriteInt64(pOffset, (long)offset);
576+
using (var status = new Status())
577+
{
578+
fixed (byte* src = &buffer[i][0])
579+
{
580+
var written = TF_StringEncode(src, (UIntPtr)buffer[i].Length, (sbyte*)dst, (UIntPtr)(dstLimit.ToInt64() - dst.ToInt64()), status);
581+
status.Check(true);
582+
pOffset += 8;
583+
dst += (int)written;
584+
offset += written;
585+
}
586+
}
587+
}
588+
589+
_handle = handle;
590590
}
591591

592592
public Tensor(Operation op, int value_index, TF_DataType dtype)
@@ -611,11 +611,11 @@ public Tensor(Operation op, int value_index, TF_DataType dtype)
611611
/// specified dimensions.
612612
/// </remarks>
613613
[MethodImpl(MethodImplOptions.AggressiveInlining)]
614+
[SuppressMessage("ReSharper", "LocalVariableHidesMember")]
614615
protected unsafe IntPtr CreateTensorWithoutCopying(TF_DataType dt, long[] shape, Array data, int element_size)
615616
{
616-
if (dt == TF_DataType.TF_STRING && data is byte[])
617+
if (dt == TF_DataType.TF_STRING && data is byte[] buffer)
617618
{
618-
var buffer = (byte[])data;
619619
var size = c_api.TF_StringEncodedSize((UIntPtr)buffer.Length);
620620
var handle = TF_AllocateTensor(TF_DataType.TF_STRING, IntPtr.Zero, 0, (UIntPtr)((ulong)size + 8));
621621

0 commit comments

Comments
 (0)