Skip to content

Fast TensorAccessor #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ce679e2
fast copy tensor accessor
haytham2597 Oct 25, 2024
958a187
rollback sln
haytham2597 Oct 25, 2024
abe9990
Merge branch 'main' into fast_tensor_accesor
NiklasGustafsson Oct 25, 2024
0b20f13
Numel
haytham2597 Oct 25, 2024
7df8e46
Merge branch 'fast_tensor_accesor' of https://github.com/haytham2597/…
haytham2597 Oct 25, 2024
d6865a6
original TorchSharp.sln
haytham2597 Oct 31, 2024
d2857bf
Fast Accesor to non contiguous tensor
haytham2597 Nov 2, 2024
f5e43d7
Test of TensorAccessor Contiguous and Non-Contiguous
haytham2597 Nov 2, 2024
6235075
Procedural NDArray
haytham2597 Nov 2, 2024
1ab3891
Fix TestJit_3 Threshold
haytham2597 Nov 2, 2024
19effd7
Improve tensor accessor
haytham2597 Nov 3, 2024
9ff7866
Simplify
haytham2597 Nov 3, 2024
9e6ba01
fix index and add more test
haytham2597 Nov 9, 2024
46fe73a
Merge branch 'main' into fast_tensor_accesor
NiklasGustafsson Nov 12, 2024
d24c709
Merge branch 'main' into fast_tensor_accesor
NiklasGustafsson Dec 2, 2024
63aa144
Merge branch 'main' into fast_tensor_accesor
NiklasGustafsson Dec 10, 2024
c29592b
Merge branch 'main' of https://github.com/dotnet/TorchSharp into fast…
haytham2597 Apr 16, 2025
c031c61
release note
haytham2597 Apr 16, 2025
0fe2f96
Update RELEASENOTES.md
haytham2597 Apr 16, 2025
ac06718
Merge branch 'main' into fast_tensor_accesor
haytham2597 Apr 17, 2025
1ea4266
removed unused
haytham2597 Apr 18, 2025
5d50ebe
Merge branch 'fast_tensor_accesor' of https://github.com/haytham2597/…
haytham2597 Apr 18, 2025
e55f01c
Merge branch 'main' into fast_tensor_accesor
alinpahontu2912 Jul 2, 2025
d0e5db8
Merge branch 'main' into fast_tensor_accesor
haytham2597 Aug 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,5 @@ packages/
/.idea
/test/TorchSharpTest/exportsd.py
.vscode/settings.json
/TestClear
TestClear/
14 changes: 7 additions & 7 deletions TorchSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TorchSharp", "TorchSharp",
pkg\TorchSharp\TorchSharp.symbols.nupkgproj = pkg\TorchSharp\TorchSharp.symbols.nupkgproj
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibTorchSharp", "bin\obj\x64.Debug\Native\LibTorchSharp\LibTorchSharp.vcxproj", "{2B359162-062E-3C52-91D3-027A8542A58C}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibTorchSharp", "bin\obj\x64.Debug\Native\LibTorchSharp\LibTorchSharp.vcxproj", "{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibTorchSharp", "bin\obj\x64.Release\Native\LibTorchSharp\LibTorchSharp.vcxproj", "{E4C0DBEE-0815-311B-9065-137BB50BD793}"
EndProject
Expand Down Expand Up @@ -66,9 +66,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
azure-pipelines.yml = azure-pipelines.yml
build\BranchInfo.props = build\BranchInfo.props
DEVGUIDE.md = DEVGUIDE.md
global.json = global.json
README.md = README.md
RELEASENOTES.md = RELEASENOTES.md
global.json = global.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TorchVision", "src\TorchVision\TorchVision.csproj", "{DCF01EE5-6431-4115-85E0-1FC4C3DE86A2}"
Expand Down Expand Up @@ -107,10 +107,10 @@ Global
{42B45168-476D-4BFA-87B8-81A34E6295CD}.Release|Any CPU.Build.0 = Release|Any CPU
{42B45168-476D-4BFA-87B8-81A34E6295CD}.Release|x64.ActiveCfg = Release|Any CPU
{42B45168-476D-4BFA-87B8-81A34E6295CD}.Release|x64.Build.0 = Release|Any CPU
{2B359162-062E-3C52-91D3-027A8542A58C}.Debug|Any CPU.ActiveCfg = Debug|x64
{2B359162-062E-3C52-91D3-027A8542A58C}.Debug|x64.ActiveCfg = Debug|x64
{2B359162-062E-3C52-91D3-027A8542A58C}.Release|Any CPU.ActiveCfg = Release|x64
{2B359162-062E-3C52-91D3-027A8542A58C}.Release|x64.ActiveCfg = Release|x64
{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06}.Debug|Any CPU.ActiveCfg = Debug|x64
{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06}.Debug|x64.ActiveCfg = Debug|x64
{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06}.Release|Any CPU.ActiveCfg = Release|x64
{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06}.Release|x64.ActiveCfg = Release|x64
{E4C0DBEE-0815-311B-9065-137BB50BD793}.Debug|Any CPU.ActiveCfg = Debug|x64
{E4C0DBEE-0815-311B-9065-137BB50BD793}.Debug|x64.ActiveCfg = Debug|x64
{E4C0DBEE-0815-311B-9065-137BB50BD793}.Release|Any CPU.ActiveCfg = Release|x64
Expand Down Expand Up @@ -181,7 +181,7 @@ Global
{6C323B05-9028-4B09-911C-3C03AE058BEE} = {AED9C836-31E3-4F3F-8ABC-929555D3F3C4}
{42B45168-476D-4BFA-87B8-81A34E6295CD} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
{567456AD-B026-4CB6-B98D-4FC930C90223} = {D3D38B03-B557-484D-8348-8BADEE4DF592}
{2B359162-062E-3C52-91D3-027A8542A58C} = {CF2C1A9E-3A8A-4329-8A6E-7880C15AAC3D}
{265C2E6F-04E6-37A8-B504-E3DD4A3FEE06} = {CF2C1A9E-3A8A-4329-8A6E-7880C15AAC3D}
{E4C0DBEE-0815-311B-9065-137BB50BD793} = {4DB9E84D-324C-408F-87A6-246E86205540}
{CF2C1A9E-3A8A-4329-8A6E-7880C15AAC3D} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
{D8C60CD8-8429-45F2-A755-47B6CD10FDF8} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
Expand Down
46 changes: 46 additions & 0 deletions src/TorchSharp/Utils/TensorAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using static TorchSharp.PInvoke.NativeMethods;

namespace TorchSharp.Utils
Expand Down Expand Up @@ -47,6 +48,16 @@ public T[] ToArray()
if (_tensor.ndim < 2)
return (T[])ToNDArray();

if (_tensor.is_contiguous()) {
//This is very fast. And work VERY WELL
var shps = _tensor.shape;
long TempCount = 1;
for (int i = 0; i < shps.Length; i++)
TempCount *= shps[i]; //Theorically the numel is simple as product of each element shape
unsafe {
return new Span<T>(_tensor_data_ptr.ToPointer(), Convert.ToInt32(TempCount)).ToArray();
}
}
var result = new T[Count];
CopyTo(result);
return result;
Expand Down Expand Up @@ -231,8 +242,38 @@ private void validate(long index)
if (index >= Count) throw new IndexOutOfRangeException();
}

private void CopyContiguous(T[] array, int index=0, int count=0)
{
if (!_tensor.is_contiguous())
throw new Exception("The tensor is not contiguous");
var shps = _tensor.shape;
long TempCount = 1;
for (int i = 0; i < shps.Length; i++)
TempCount *= shps[i]; //Theorically the numel is simple as product of each element shape
if (count > TempCount || count == 0)
count = (int)TempCount;
if (array is byte[] ba)
Marshal.Copy(_tensor_data_ptr, ba, index, count);
if (array is short[] sa)
Marshal.Copy(_tensor_data_ptr, sa, index, count);
if(array is char[] ca)
Marshal.Copy(_tensor_data_ptr, ca, index, count);
if (array is long[] la)
Marshal.Copy(_tensor_data_ptr, la, index, count);
if (array is float[] fa)
Marshal.Copy(_tensor_data_ptr, fa, index, count);
if (array is int[] ia)
Marshal.Copy(_tensor_data_ptr, ia, index, count);
if (array is double[] da)
Marshal.Copy(_tensor_data_ptr, da, index, count);
}
public void CopyTo(T[] array, int arrayIndex = 0, long tensorIndex = 0)
{
if (_tensor.is_contiguous()) {
CopyContiguous(array, arrayIndex, array.Length);
return;
}

int idx = arrayIndex;
foreach (int offset in GetSubsequentIndices(tensorIndex)) {
if (idx >= array.Length) break;
Expand All @@ -243,6 +284,11 @@ public void CopyTo(T[] array, int arrayIndex = 0, long tensorIndex = 0)

public void CopyTo(Span<T> array, int arrayIndex = 0, long tensorIndex = 0)
{
if (_tensor.is_contiguous()) {
ToArray().CopyTo(array);
return;
}

int idx = arrayIndex;
foreach (int offset in GetSubsequentIndices(tensorIndex)) {
if (idx >= array.Length) break;
Expand Down