-
Notifications
You must be signed in to change notification settings - Fork 213
concat dataset #1354
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
Merged
Merged
concat dataset #1354
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aedf161
IDataset interface
yueyinqiu dd2e1ed
fix fsharp
yueyinqiu 7d76a4b
Create ConcatDataset.cs
yueyinqiu 3dfec6b
add tests
yueyinqiu 8758a87
test data loader for ConcatDataset
yueyinqiu e093441
Update RELEASENOTES.md
yueyinqiu 94635e9
Update RELEASENOTES.md
yueyinqiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using TorchSharp.Modules; | ||
|
|
||
| namespace TorchSharp | ||
| { | ||
| public static partial class torch | ||
| { | ||
| public static partial class utils | ||
| { | ||
| public static partial class data | ||
| { | ||
| public static ConcatDataset<T> ConcatDataset<T>(IEnumerable<IDataset<T>> datasets) | ||
| { | ||
| return new ConcatDataset<T>(datasets); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| namespace Modules | ||
| { | ||
| public class ConcatDataset<T> : torch.utils.data.Dataset<T> | ||
| { | ||
| private static IEnumerable<long> Cumsum( | ||
| IEnumerable<torch.utils.data.IDataset<T>> datasets) | ||
| { | ||
| var s = 0L; | ||
| foreach (var e in datasets) { | ||
| s += e.Count; | ||
| yield return s; | ||
| } | ||
| } | ||
| private static long bisectRight(long[] a, long x) | ||
| { | ||
| var lo = 0; | ||
| var hi = a.Length; | ||
| while (lo < hi) { | ||
| var mid = (lo + hi) / 2; | ||
| if (x < a[mid]) | ||
| hi = mid; | ||
| else | ||
| lo = mid + 1; | ||
| } | ||
| return lo; | ||
| } | ||
|
|
||
|
|
||
| private readonly torch.utils.data.IDataset<T>[] _datasets; | ||
| public IReadOnlyList<torch.utils.data.IDataset<T>> datasets => _datasets; | ||
|
|
||
| private readonly long[] _cumulativeSizes; | ||
| public IReadOnlyList<long> cumulative_sizes => _cumulativeSizes; | ||
|
|
||
| private readonly bool autoDispose; | ||
|
|
||
| public ConcatDataset( | ||
| IEnumerable<torch.utils.data.IDataset<T>> datasets, | ||
| bool autoDispose = true) | ||
| { | ||
| this._datasets = datasets.ToArray(); | ||
| if (this._datasets.Length is 0) | ||
| throw new ArgumentException( | ||
| "datasets should not be an empty iterable", nameof(datasets)); | ||
|
|
||
| // PyTorch also says 'ConcatDataset does not support IterableDataset'. | ||
| // But it's not our torch.utils.data.IterableDataset in TorchSharp. | ||
| this._cumulativeSizes = Cumsum(datasets).ToArray(); | ||
|
|
||
| this.autoDispose = autoDispose; | ||
| } | ||
|
|
||
| public override long Count => this._cumulativeSizes.Last(); | ||
|
|
||
| public override T GetTensor(long index) | ||
| { | ||
| if (index < 0) { | ||
| if (-index > this.Count) { | ||
| throw new ArgumentException( | ||
| "absolute value of index should not exceed dataset length", | ||
| nameof(index)); | ||
| } | ||
| index = this.Count + index; | ||
| } | ||
|
|
||
| var datasetIdx = bisectRight(this._cumulativeSizes, index); | ||
| long sampleIdx; | ||
| if (datasetIdx == 0) | ||
| sampleIdx = index; | ||
| else | ||
| sampleIdx = index - this._cumulativeSizes[datasetIdx - 1]; | ||
| return this._datasets[datasetIdx][sampleIdx]; | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing && autoDispose) { | ||
| foreach (var dataset in this._datasets) | ||
| dataset.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace TorchSharp | ||
| { | ||
|
|
@@ -13,21 +14,29 @@ public static partial class data | |
| /// <summary> | ||
| /// Map-style data set | ||
| /// </summary> | ||
| public abstract class Dataset : Dataset<Dictionary<string, torch.Tensor>> | ||
| public abstract class Dataset : Dataset<Dictionary<string, Tensor>>, | ||
| IDataset<IReadOnlyDictionary<string, Tensor>> | ||
| { | ||
| // Due to covariation, it should naturally be IDataset<IReadOnlyDictionary<string, Tensor>>. | ||
| // However FSharp.Examples will break down without this. | ||
| IReadOnlyDictionary<string, Tensor> IDataset<IReadOnlyDictionary<string, Tensor>>.this[long index] => this[index]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Iterable-style data sets | ||
| /// </summary> | ||
| public abstract class IterableDataset : Dataset<IList<Tensor>> | ||
| public abstract class IterableDataset : Dataset<IList<Tensor>>, | ||
| IDataset<IEnumerable<Tensor>> | ||
| { | ||
| // Due to covariation, it should naturally be IDataset<IEnumerable<Tensor>>. | ||
| // However FSharp.Examples will break down without this. | ||
| IEnumerable<Tensor> IDataset<IEnumerable<Tensor>>.this[long index] => this[index]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The base nterface for all Datasets. | ||
| /// </summary> | ||
| public abstract class Dataset<T> : IDisposable | ||
| public abstract class Dataset<T> : IDataset<T>, IDisposable | ||
| { | ||
| public void Dispose() | ||
| { | ||
|
|
@@ -40,6 +49,12 @@ public void Dispose() | |
| /// </summary> | ||
| public abstract long Count { get; } | ||
|
|
||
| [IndexerName("DatasetItems")] | ||
| public T this[long index] => this.GetTensor(index); | ||
|
|
||
| // GetTensor is kept for compatibility. | ||
| // Perhaps we should remove that and make the indexer abstract later. | ||
|
|
||
| /// <summary> | ||
| /// Get tensor according to index | ||
| /// </summary> | ||
|
|
@@ -49,8 +64,31 @@ public void Dispose() | |
|
|
||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| IDataset<Dictionary<string, string>> a = null; | ||
| IDataset<IReadOnlyDictionary<string, string>> b = a; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The base interface for all Datasets. | ||
| /// </summary> | ||
| public interface IDataset<out T> : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Size of dataset | ||
| /// </summary> | ||
| long Count { get; } | ||
|
|
||
| /// <summary> | ||
| /// Get tensor according to index | ||
| /// </summary> | ||
| /// <param name="index">Index for tensor</param> | ||
| /// <returns>Tensors of index. DataLoader will catenate these tensors into batches.</returns> | ||
| [IndexerName("DatasetItems")] | ||
| T this[long index] { get; } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's using indexers instead of And... shall we remove |
||
|
|
||
| // TODO: support System.Index | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As commented in the codes below, due to covariation,
Datasetshould naturally beIDataset<IReadOnlyDictionary<string, Tensor>>(because it isIDataset<Dictionary<string, Tensor>>). HoweverFSharp.Examplescannot be complied without this. I don't know why this happens.I would suggest to remove this line, because it influences the detection of
torch.utils.data.ConcatDataset(IReadOnlyDictionaryorDictionaryare both ok, so it can't automatically choose one), but I failed to make the fsharp program work.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more radical idea is to remove
DatasetandIterableDatasetat all. Actually we don't need them.DataLoaders could work on anyIDataset<IReadOnlyDictionary<string, torch.Tensor>>andIDataset<IEnumerable<Tensor>>.By the way, our
IterableDatasetis occupying the position of PyTorchIterableDataset. #1353 That's also part of the reason why I suggest to remove them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F# samples have to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I suppose so... However it doesn't https://dev.azure.com/dotnet/TorchSharp/_build/results?buildId=107828&view=results
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So. just to check -- with the current commits, the F# examples build, correct? I don't see any errors in the latest build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I could be built by adding
: IDataset<IReadOnlyDictionary<string, Tensor>>.