-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
236 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,79 @@ | ||
# ========================================================================= | ||
# Copyright (C) 2023-2024. FuxiCTR Authors. All rights reserved. | ||
# Copyright (C) 2022. Huawei Technologies Co., Ltd. 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 numpy as np | ||
from itertools import chain | ||
from torch.utils import data | ||
import glob | ||
import polars as pl | ||
import pandas as pd | ||
|
||
|
||
class IterDataPipe(data.IterDataPipe): | ||
def __init__(self, data_blocks, feature_map): | ||
self.feature_map = feature_map | ||
self.data_blocks = data_blocks | ||
|
||
def read_block(self, data_block): | ||
data_df = pd.read_parquet(data_block) | ||
for idx in range(len(data_df)): | ||
yield data_df.iloc[idx].to_dict() | ||
|
||
def __iter__(self): | ||
worker_info = data.get_worker_info() | ||
if worker_info is None: # single-process data loading | ||
block_list = self.data_blocks | ||
else: # in a worker process | ||
block_list = [ | ||
block | ||
for idx, block in enumerate(self.data_blocks) | ||
if idx % worker_info.num_workers == worker_info.id | ||
] | ||
return chain.from_iterable(map(self.read_block, block_list)) | ||
|
||
|
||
class ParquetBlockDataLoader(data.DataLoader): | ||
def __init__(self, feature_map, data_path, batch_size=32, shuffle=False, | ||
num_workers=1, buffer_size=100000, **kwargs): | ||
data_blocks = glob.glob(data_path + "/*.parquet") | ||
assert len(data_blocks) > 0, f"invalid data_path: {data_path}" | ||
if len(data_blocks) > 1: | ||
data_blocks.sort() # sort by part name | ||
self.data_blocks = data_blocks | ||
self.num_blocks = len(self.data_blocks) | ||
self.feature_map = feature_map | ||
self.batch_size = batch_size | ||
self.num_batches, self.num_samples = self.count_batches_and_samples() | ||
datapipe = IterDataPipe(self.data_blocks, feature_map) | ||
if shuffle: | ||
datapipe = datapipe.shuffle(buffer_size=buffer_size) | ||
else: | ||
num_workers = 1 # multiple workers cannot keep the order of data reading | ||
super().__init__(dataset=datapipe, batch_size=batch_size, | ||
num_workers=num_workers) | ||
|
||
def __len__(self): | ||
return self.num_batches | ||
|
||
def count_batches_and_samples(self): | ||
num_samples = 0 | ||
for data_block in self.data_blocks: | ||
df = pl.scan_parquet(data_block) | ||
num_samples += df.select(pl.count()).collect().item() | ||
num_batches = int(np.ceil(num_samples / self.batch_size)) | ||
return num_batches, num_samples |
This file contains 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,47 @@ | ||
# ========================================================================= | ||
# Copyright (C) 2024. FuxiCTR 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 numpy as np | ||
from torch.utils import data | ||
import pandas as pd | ||
|
||
|
||
class Dataset(data.Dataset): | ||
def __init__(self, feature_map, data_path): | ||
self.feature_map = feature_map | ||
self.data_df = pd.read_parquet(data_path) | ||
|
||
def __getitem__(self, index): | ||
return self.data_df.iloc[index].to_dict() | ||
|
||
def __len__(self): | ||
return len(self.data_df) | ||
|
||
|
||
class ParquetDataLoader(data.DataLoader): | ||
def __init__(self, feature_map, data_path, batch_size=32, shuffle=False, num_workers=1, **kwargs): | ||
if not data_path.endswith(".parquet"): | ||
data_path += ".parquet" | ||
self.dataset = Dataset(feature_map, data_path) | ||
super().__init__(dataset=self.dataset, batch_size=batch_size, | ||
shuffle=shuffle, num_workers=num_workers) | ||
self.num_samples = len(self.dataset) | ||
self.num_blocks = 1 | ||
self.num_batches = int(np.ceil(self.num_samples / self.batch_size)) | ||
|
||
def __len__(self): | ||
return self.num_batches |
This file contains 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
Oops, something went wrong.