Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Data V2 #3700

Merged
merged 59 commits into from
Feb 26, 2020
Merged

Data V2 #3700

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
0c42cb9
example for feedback
DeNeutoy Jan 30, 2020
5ffedfc
Merge branch 'master' into data-v2
DeNeutoy Feb 19, 2020
80049f8
remove all existing multiprocessing
DeNeutoy Feb 19, 2020
6f58c2a
sneak torch datasets inside DatasetReader
DeNeutoy Feb 19, 2020
1b3ad9a
lint
DeNeutoy Feb 19, 2020
effc445
trainer_v2, We Love To See It
DeNeutoy Feb 19, 2020
9d44ad6
datasets have index_with now, not iterators
DeNeutoy Feb 19, 2020
7e89ea6
use iter, custom collate function in allennlp wrapper
DeNeutoy Feb 19, 2020
883b6d7
we don't even need the data in the trainer anymore
DeNeutoy Feb 19, 2020
56d022a
all trainer tests passing
DeNeutoy Feb 20, 2020
01e12f5
black
DeNeutoy Feb 20, 2020
5aea291
make find learning rate work
DeNeutoy Feb 20, 2020
f026946
update test fixtures to new config
DeNeutoy Feb 20, 2020
5973b50
get train command tests mostly working
DeNeutoy Feb 20, 2020
a23f47a
lazily construct samplers, index lazy datasets
DeNeutoy Feb 20, 2020
a76ea0a
Merge branch 'master' into data-v2
DeNeutoy Feb 20, 2020
ebf3854
update some fixtures
DeNeutoy Feb 20, 2020
57a67e5
evaluate tests passing
DeNeutoy Feb 20, 2020
7d21ed8
all command tests passing
DeNeutoy Feb 20, 2020
24a500c
lint
DeNeutoy Feb 20, 2020
fb13769
update model test case, common and module tests passing
DeNeutoy Feb 20, 2020
ef5187f
fix test interdependence introduced by #3762
DeNeutoy Feb 21, 2020
b1ea845
more test interdependence
DeNeutoy Feb 21, 2020
0231616
tests tests tests
DeNeutoy Feb 21, 2020
01d76bb
remove unnecessary brackets
DeNeutoy Feb 21, 2020
12b6efb
Merge branch 'master' into data-v2
DeNeutoy Feb 21, 2020
859d3ca
update a chunk of the configs
DeNeutoy Feb 21, 2020
c22dee3
fix archival test, couple more configs
DeNeutoy Feb 21, 2020
fe5b470
rm pointless gan test
DeNeutoy Feb 21, 2020
7533c91
more tests passing
DeNeutoy Feb 21, 2020
ad45659
add current state of from params changes
DeNeutoy Feb 21, 2020
f944840
Revert "add current state of from params changes"
DeNeutoy Feb 21, 2020
3b12a2f
Merge branch 'master' into data-v2
DeNeutoy Feb 21, 2020
be1f58c
updated understanding of Lazy
DeNeutoy Feb 21, 2020
ebdabe0
add discussion of None comparison to Lazy
DeNeutoy Feb 21, 2020
8693739
lint
DeNeutoy Feb 21, 2020
b9b0650
it's a hard doc life
DeNeutoy Feb 21, 2020
88314c7
pull samplers into separate file
DeNeutoy Feb 21, 2020
14296a1
more docs updates
DeNeutoy Feb 22, 2020
8a08899
fold in #3812
DeNeutoy Feb 22, 2020
3520280
remove torch dataset
DeNeutoy Feb 22, 2020
0f1d8a4
add example to lazy
DeNeutoy Feb 22, 2020
93e1e89
rename to collate
DeNeutoy Feb 22, 2020
40dd695
no kwargs
DeNeutoy Feb 23, 2020
da3b1b4
Revert "fold in #3812"
DeNeutoy Feb 23, 2020
801a8f5
don't break up dataset
DeNeutoy Feb 23, 2020
007fd0c
add comment to iterable dataset len
DeNeutoy Feb 23, 2020
d00e1a9
Merge branch 'master' into data-v2
DeNeutoy Feb 23, 2020
c066804
improve docstrings, build dataloader using partial_objects
DeNeutoy Feb 23, 2020
61c7b14
flake
DeNeutoy Feb 23, 2020
2b56b14
give dataloader a default implementation
DeNeutoy Feb 24, 2020
354010a
safer default for DataLoader init
DeNeutoy Feb 24, 2020
568291d
more coherent dir structure
DeNeutoy Feb 24, 2020
a016103
update imports
DeNeutoy Feb 24, 2020
47db16a
Merge branch 'master' into data-v2
DeNeutoy Feb 24, 2020
04fdb70
add a test for the BucketBatchSampler
DeNeutoy Feb 24, 2020
d1d5c4a
split bucket sampler into own file, tests
DeNeutoy Feb 24, 2020
5f0c8db
PR comments
DeNeutoy Feb 26, 2020
6f63a53
Merge branch 'master' into data-v2
DeNeutoy Feb 26, 2020
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
Prev Previous commit
Next Next commit
improve docstrings, build dataloader using partial_objects
  • Loading branch information
DeNeutoy committed Feb 23, 2020
commit c066804cfc00ee5a46f8c7fdd5eb7d7b6ba512ef
67 changes: 60 additions & 7 deletions allennlp/data/samplers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import List
import logging
from torch.utils import data

from allennlp.common.registrable import Registrable
from allennlp.data.instance import Instance

from allennlp.common.lazy import Lazy
from allennlp.data.batch import Batch
Expand All @@ -19,19 +21,35 @@
logger = logging.getLogger(__name__)


def allennlp_collate(batch):
batch = Batch(batch)
def allennlp_collate(instances: List[Instance]):
batch = Batch(instances)
return batch.as_tensor_dict(batch.get_padding_lengths())


class DataLoader(Registrable, data.DataLoader):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need Registrable, or just FromParams? Do you imagine someone having to pick a subclass of these?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, given my comment below, yes, it needs to be Registrable. And you need to have a line like this after the class definition: DataLoader.register("default", constructor="from_partial_objects")(DataLoader). And you need a default_implementation = "default" line as a class variable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put this in __init__.py, instead of its own file? Because it's so small? It's just hard to discover things in __init__.py files, especially as your docs script doesn't include them, does it?

"""
A registrable version of the pytorch [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader).
The only reason this class exists is so that we can construct a DataLoader
from a configuration file. Instead of using this class directly in python code,
you should just use the pytorch dataloader with allennlp's custom collate function:

```
from torch.utils.data import DataLoader

from allennlp.data.samplers import allennlp_collate
# Construct a dataloader directly for a dataset which contains allennlp
# Instances which have _already_ been indexed.
my_loader = DataLoader(dataset, batch_size=32, collate_fn=allennlp_collate)
```
"""

def __init__(
self,
dataset: data.Dataset,
batch_size: int = 1,
shuffle: bool = False,
sampler: Lazy[Sampler] = None,
batch_sampler: Lazy[BatchSampler] = None,
sampler: Sampler = None,
batch_sampler: BatchSampler = None,
num_workers: int = 0,
collate_fn=None,
pin_memory: bool = False,
Expand All @@ -40,8 +58,37 @@ def __init__(
worker_init_fn=None,
multiprocessing_context: str = None,
):
super().__init__(
dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
sampler=sampler,
batch_sampler=batch_sampler,
num_workers=num_workers,
collate_fn=collate_fn,
pin_memory=pin_memory,
drop_last=drop_last,
timeout=timeout,
worker_init_fn=worker_init_fn,
multiprocessing_context=multiprocessing_context,
)

@classmethod
def from_partial_objects(
cls,
dataset: data.Dataset,
batch_size: int = 1,
shuffle: bool = False,
sampler: Lazy[Sampler] = None,
batch_sampler: Lazy[BatchSampler] = None,
num_workers: int = 0,
pin_memory: bool = False,
drop_last: bool = False,
timeout: int = 0,
worker_init_fn=None,
multiprocessing_context: str = None,
) -> "DataLoader":

collate_fn = allennlp_collate
if batch_sampler is not None:
batch_sampler_ = batch_sampler.construct(data_source=dataset)
else:
Expand All @@ -51,17 +98,23 @@ def __init__(
else:
sampler_ = None

super().__init__(
return cls(
dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
sampler=sampler_,
batch_sampler=batch_sampler_,
num_workers=num_workers,
collate_fn=collate_fn,
# NOTE: This default is different from the normal `None`.
# We assume that if you are using this class you are using an
# allennlp dataset of instances, which would require this.
collate_fn=allennlp_collate,
pin_memory=pin_memory,
drop_last=drop_last,
timeout=timeout,
worker_init_fn=worker_init_fn,
multiprocessing_context=multiprocessing_context,
)


DataLoader.register("default", "from_partial_objects")(DataLoader)
21 changes: 14 additions & 7 deletions allennlp/data/samplers/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ def __iter__(self) -> Iterable[List[int]]:

@Sampler.register("sequential")
class SequentialSampler(Sampler, data.SequentialSampler):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to have a docstring pointing to the pytorch classes for all of these.

"""
A registerable version of pytorch's [SequentialSampler](https://pytorch.org/docs/stable/data.html#torch.utils.data.SequentialSampler).
"""
def __init__(self, data_source: data.Dataset):
super().__init__(data_source)


@Sampler.register("random")
class RandomSampler(Sampler, data.RandomSampler):
"""
A registerable version of pytorch's [RandomSampler](https://pytorch.org/docs/stable/data.html#torch.utils.data.RandomSampler).
Samples elements randomly. If without replacement, then sample from a shuffled dataset.
If with replacement, then user can specify `num_samples` to draw.

Expand All @@ -64,6 +68,7 @@ def __init__(
@Sampler.register("subset_random")
class SubsetRandomSampler(Sampler, data.SubsetRandomSampler):
"""
A registerable version of pytorch's [SubsetRandomSampler](https://pytorch.org/docs/stable/data.html#torch.utils.data.SubsetRandomSampler).
Samples elements randomly from a given list of indices, without replacement.

# Parameters
Expand All @@ -78,7 +83,8 @@ def __init__(self, indices: List[int]):
@Sampler.register("weighted_random")
class WeightedRandomSampler(Sampler, data.WeightedRandomSampler):
"""
Samples elements from ``[0,..,len(weights)-1]`` with given probabilities (weights).
A registerable version of pytorch's [WeightedRandomSampler](https://pytorch.org/docs/stable/data.html#torch.utils.data.WeightedRandomSampler).
Samples elements from `[0,...,len(weights)-1]` with given probabilities (weights).

# Parameters:
weights : `List[float]`
Expand Down Expand Up @@ -106,6 +112,7 @@ def __init__(self, weights: List[float], num_samples: int, replacement: bool = T
@BatchSampler.register("basic")
class BasicBatchSampler(BatchSampler, data.BatchSampler):
"""
A registerable version of pytorch's [BatchSampler](https://pytorch.org/docs/stable/data.html#torch.utils.data.BatchSampler).
Wraps another sampler to yield a mini-batch of indices.

# Parameters
Expand Down Expand Up @@ -134,10 +141,10 @@ def __init__(self, sampler: Sampler, batch_size: int, drop_last: bool):
class BatchInstanceSampler(BatchSampler):
"""
An sampler which by default, argsorts batches with respect to the maximum input lengths `per
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argsorts instances before batching? Not sure why the second sentence says "additionally", because you can't do an argsort without those padding keys.

batch`. Additionally, you can provide a list of field names and padding keys which the dataset
will be sorted by before doing this batching, causing inputs with similar length to be batched
together, making computation more efficient (as less time is wasted on padded elements of the
batch).
batch`. You can provide a list of field names and padding keys (or pass none, in which case they
will be inferred) which the dataset will be sorted by before doing this batching, causing inputs
with similar length to be batched together, making computation more efficient (as less time is
wasted on padded elements of the batch).

# Parameters

Expand All @@ -154,15 +161,15 @@ class BatchInstanceSampler(BatchSampler):
When you need to specify this yourself, you can create an instance from your dataset and
call `Instance.get_padding_lengths()` to see a list of all keys used in your data. You
should give one or more of those as the sorting keys here.
batch_size : int, required.
The size of each batch of instances yielded when calling the dataloader.
padding_noise : float, optional (default=.1)
When sorting by padding length, we add a bit of noise to the lengths, so that the sorting
isn't deterministic. This parameter determines how much noise we add, as a percentage of
the actual padding value for each instance.

Note that if you specify `max_instances_in_memory`, the first batch will only be the
biggest from among the first "max instances in memory" instances.
batch_size : int, optional, (default = 32)
The size of each batch of instances yielded when calling the iterator.

"""

Expand Down