FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PaddleScience/ppsci/data/__init__.py at develop · PaddlePaddle/PaddleScience · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
PaddlePaddle
/
PaddleScience
Public
Notifications
You must be signed in to change notification settings
Fork
236
Star
450
Code
Issues
2
Pull requests
3
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PaddleScience
/
ppsci
/
data
/
__init__.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
207 lines (183 loc) · 8.27 KB
Breadcrumbs
PaddleScience
/
ppsci
/
data
/
__init__.py
Copy path
File metadata and controls
207 lines (183 loc) · 8.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
copy
import
random
from
functools
import
partial
from
typing
import
Callable
from
typing
import
Optional
import
numpy
as
np
import
paddle
.
distributed
as
dist
from
paddle
import
device
from
paddle
import
io
from
ppsci
.
data
import
dataloader
from
ppsci
.
data
import
dataset
from
ppsci
.
data
import
process
from
ppsci
.
data
.
dataset
import
register_to_dataset
from
ppsci
.
data
.
process
import
batch_transform
from
ppsci
.
data
.
process
import
transform
from
ppsci
.
utils
import
logger
__all__
=
[
"dataset"
,
"process"
,
"dataloader"
,
"build_dataloader"
,
"transform"
,
"batch_transform"
,
"register_to_dataset"
,
]
def
worker_init_fn
(
worker_id
:
int
,
num_workers
:
int
,
rank
:
int
,
base_seed
:
int
)
->
None
:
"""Callback function on each worker subprocess after seeding and before data loading.
Args:
worker_id (int): Worker id in [0, num_workers - 1].
num_workers (int): Number of subprocesses to use for data loading.
rank (int): Rank of process in distributed environment. If in non-distributed
environment, it is a constant number `0`.
base_seed (int): Base random seed.
"""
# The seed of each worker equals to: user_seed + num_worker * rank + worker_id
worker_seed
=
base_seed
+
num_workers
*
rank
+
worker_id
np
.
random
.
seed
(
worker_seed
)
random
.
seed
(
worker_seed
)
def
build_dataloader
(
_dataset
,
cfg
):
world_size
=
dist
.
get_world_size
()
# just return IterableDataset as dataloader
if
isinstance
(
_dataset
,
io
.
IterableDataset
):
return
_dataset
cfg
=
copy
.
deepcopy
(
cfg
)
# build sampler
sampler_cfg
=
cfg
.
pop
(
"sampler"
,
None
)
if
sampler_cfg
is
not
None
:
batch_sampler_cls
=
sampler_cfg
.
pop
(
"name"
)
if
batch_sampler_cls
==
"BatchSampler"
:
if
world_size
>
1
:
batch_sampler_cls
=
"DistributedBatchSampler"
logger
.
warning
(
f"Automatically use 'DistributedBatchSampler' instead of "
f"'BatchSampler' when world_size(
{
world_size
}
) > 1."
)
sampler_cfg
[
"batch_size"
]
=
cfg
[
"batch_size"
]
batch_sampler
=
getattr
(
io
,
batch_sampler_cls
)(
_dataset
,
**
sampler_cfg
)
else
:
batch_sampler_cls
=
"BatchSampler"
if
world_size
>
1
:
batch_sampler_cls
=
"DistributedBatchSampler"
logger
.
warning
(
f"Automatically use 'DistributedBatchSampler' instead of "
f"'BatchSampler' when world_size(
{
world_size
}
) > 1."
)
batch_sampler
=
getattr
(
io
,
batch_sampler_cls
)(
_dataset
,
batch_size
=
cfg
[
"batch_size"
],
shuffle
=
False
,
drop_last
=
False
,
)
logger
.
message
(
"'shuffle' and 'drop_last' are both set to False in default as sampler config is not specified."
)
# build collate_fn if specified
batch_transforms_cfg
=
cfg
.
pop
(
"batch_transforms"
,
None
)
collate_fn
:
Optional
[
Callable
]
=
cfg
.
pop
(
"collate_fn"
,
None
)
if
isinstance
(
batch_transforms_cfg
, (
list
,
tuple
)):
collate_fn
=
batch_transform
.
build_batch_transforms
(
batch_transforms_cfg
,
collate_fn
)
# build init function
_DEFAULT_NUM_WORKERS
=
1
_DEFAULT_SEED
=
42
init_fn
=
partial
(
worker_init_fn
,
num_workers
=
cfg
.
get
(
"num_workers"
,
_DEFAULT_NUM_WORKERS
),
rank
=
dist
.
get_rank
(),
base_seed
=
cfg
.
get
(
"seed"
,
_DEFAULT_SEED
),
)
# build dataloader
if
getattr
(
_dataset
,
"use_pgl"
,
False
):
# Use special dataloader from "Paddle Graph Learning" toolkit.
try
:
from
pgl
.
utils
import
data
as
pgl_data
except
ModuleNotFoundError
as
e
:
logger
.
error
(
"Please install pgl with `pip install pgl`."
)
raise
ModuleNotFoundError
(
str
(
e
))
if
collate_fn
is
None
:
collate_fn
=
batch_transform
.
default_collate_fn
dataloader_
=
pgl_data
.
Dataloader
(
dataset
=
_dataset
,
batch_size
=
cfg
[
"batch_size"
],
drop_last
=
sampler_cfg
.
get
(
"drop_last"
,
False
),
shuffle
=
sampler_cfg
.
get
(
"shuffle"
,
False
),
num_workers
=
cfg
.
get
(
"num_workers"
,
_DEFAULT_NUM_WORKERS
),
collate_fn
=
collate_fn
,
)
elif
getattr
(
_dataset
,
"use_graph_grid_mesh"
,
False
):
# Use special dataloader `GridMeshAtmosphericDataset`.
if
collate_fn
is
None
:
collate_fn
=
batch_transform
.
default_collate_fn
dataloader_
=
io
.
DataLoader
(
dataset
=
_dataset
,
places
=
device
.
get_device
(),
batch_sampler
=
batch_sampler
,
collate_fn
=
collate_fn
,
num_workers
=
cfg
.
get
(
"num_workers"
,
_DEFAULT_NUM_WORKERS
),
use_shared_memory
=
cfg
.
get
(
"use_shared_memory"
,
False
),
worker_init_fn
=
init_fn
,
)
else
:
if
(
cfg
.
get
(
"auto_collation"
,
not
getattr
(
_dataset
,
"batch_index"
,
False
))
is
False
):
if
"transforms"
in
cfg
[
"dataset"
]
and
"auto_collation"
not
in
cfg
:
logger
.
warning
(
"'transforms' and batch indexing(auto_collation=False) are both "
"enabled. If you do want to apply transforms to the batch samples, "
"please explicitly set 'auto_collation' to False in dataloader_cfg;"
" otherwise, the 'transforms' will be retained, but batch indexing "
"will be disabled."
)
else
:
# 1. wrap batch_sampler again into BatchSampler for disabling auto collation,
# which can speed up the process of batch samples indexing from dataset. See
# details at: https://discuss.pytorch.org/t/efficiency-of-dataloader-and-collate-for-large-array-like-datasets/59569/8
batch_sampler
=
io
.
BatchSampler
(
sampler
=
batch_sampler
,
batch_size
=
1
)
if
collate_fn
is
not
None
:
raise
NotImplementedError
(
"Detected collate_fn is not None for 'batch_transforms' might "
"be specified in 'dataloader_cfg', which is not supported yet "
"with 'auto_collation' is False at the same time"
)
# 2. disable auto collation by given identity collate_fn which return the first
# (also the only) batch data in batch list, or there will be a redundant
# axis at the first dimension returned by dataloader. This step is necessary
# because paddle do not support 'sampler' as instantiation argument of 'io.DataLoader'
collate_fn
=
lambda
batch
:
batch
[
0
]
# noqa: E731
_DEFAULT_NUM_WORKERS
=
0
logger
.
info
(
"Auto collation is disabled and set num_workers to "
f"
{
_DEFAULT_NUM_WORKERS
}
to speed up batch sampling."
)
dataloader_
=
io
.
DataLoader
(
dataset
=
_dataset
,
places
=
device
.
get_device
(),
batch_sampler
=
batch_sampler
,
collate_fn
=
collate_fn
,
num_workers
=
cfg
.
get
(
"num_workers"
,
_DEFAULT_NUM_WORKERS
),
use_shared_memory
=
cfg
.
get
(
"use_shared_memory"
,
False
),
worker_init_fn
=
init_fn
,
# TODO: Do not enable 'persistent_workers' below for
# 'IndexError: pop from empty list ...' will be raised in certain cases
# persistent_workers=cfg.get("num_workers", _DEFAULT_NUM_WORKERS) > 0,
)
if
len
(
dataloader_
)
==
0
:
raise
ValueError
(
f"batch_size(
{
sampler_cfg
[
'batch_size'
]
}
) should not bigger than number of "
f"samples(
{
len
(
_dataset
)
}
) when drop_last is
{
sampler_cfg
.
get
(
'drop_last'
,
False
)
}
."
)
return
dataloader_
Back
|
FazBrowse Home
|
New Git URL