-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable grain and tfrecord for mlperf dataset in SD base_2_base traini…
…ng (#130)
- Loading branch information
Showing
9 changed files
with
242 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2023 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
# Description: | ||
# bash setup_gcsfuse.sh DATASET_GCS_BUCKET=maxdiffusion-github-runner-test-assets MOUNT_PATH=/tmp/gcsfuse | ||
|
||
set -e -x | ||
|
||
# Set environment variables | ||
for ARGUMENT in "$@"; do | ||
IFS='=' read -r KEY VALUE <<< "$ARGUMENT" | ||
export "$KEY"="$VALUE" | ||
echo "$KEY"="$VALUE" | ||
done | ||
|
||
if [[ -z ${DATASET_GCS_BUCKET} || -z ${MOUNT_PATH} ]]; then | ||
echo "Please set arguments: DATASET_GCS_BUCKET and MOUNT_PATH" | ||
exit 1 | ||
fi | ||
|
||
if [[ "$DATASET_GCS_BUCKET" =~ gs:\/\/ ]] ; then | ||
DATASET_GCS_BUCKET="${DATASET_GCS_BUCKET/gs:\/\//}" | ||
echo "Removed gs:// from GCS bucket name, GCS bucket is $DATASET_GCS_BUCKET" | ||
fi | ||
|
||
if [[ -d ${MOUNT_PATH} ]]; then | ||
echo "$MOUNT_PATH exists, removing..." | ||
fusermount -u $MOUNT_PATH || rm -rf $MOUNT_PATH | ||
fi | ||
|
||
mkdir -p $MOUNT_PATH | ||
|
||
# see https://cloud.google.com/storage/docs/gcsfuse-cli for all configurable options of gcsfuse CLI | ||
# Grain uses _PROCESS_MANAGEMENT_MAX_THREADS = 64 (https://github.com/google/grain/blob/main/grain/_src/python/grain_pool.py) | ||
# Please make sure max-conns-per-host > grain_worker_count * _PROCESS_MANAGEMENT_MAX_THREADS | ||
|
||
gcsfuse -o ro --implicit-dirs --http-client-timeout=5s --max-conns-per-host=2000 \ | ||
--debug_fuse_errors --debug_fuse --debug_gcs --debug_invariants --debug_mutex \ | ||
--log-file=$HOME/gcsfuse.json "$DATASET_GCS_BUCKET" "$MOUNT_PATH" |
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 2024 Google LLC | ||
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 | ||
https://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 dataclasses | ||
import glob | ||
import tensorflow as tf | ||
import numpy as np | ||
import grain.python as grain | ||
|
||
from maxdiffusion import multihost_dataloading | ||
|
||
|
||
def make_grain_iterator( | ||
config, | ||
dataloading_host_index, | ||
dataloading_host_count, | ||
mesh, | ||
global_batch_size, | ||
): | ||
"""Use Grain data input pipeline with ArrayRecord data format""" | ||
data_files = glob.glob(config.grain_train_files) | ||
data_source = grain.ArrayRecordDataSource(data_files) | ||
|
||
operations = [] | ||
operations.append(ParseFeatures()) | ||
operations.append(grain.Batch(batch_size=global_batch_size // dataloading_host_count, drop_remainder=True)) | ||
|
||
index_sampler = grain.IndexSampler( | ||
num_records=len(data_source), | ||
num_epochs=None, | ||
shard_options=grain.ShardOptions( | ||
shard_index=dataloading_host_index, shard_count=dataloading_host_count, drop_remainder=True | ||
), | ||
shuffle=True, | ||
seed=config.seed, | ||
) | ||
|
||
dataloader = grain.DataLoader( | ||
data_source=data_source, | ||
operations=operations, | ||
sampler=index_sampler, | ||
worker_count=config.grain_worker_count, | ||
) | ||
|
||
data_iter = multihost_dataloading.MultiHostDataLoadIterator(dataloader, mesh) | ||
return data_iter | ||
|
||
|
||
@dataclasses.dataclass | ||
class ParseFeatures(grain.MapTransform): | ||
"""Parse serialized example""" | ||
|
||
def __init__(self): | ||
self.feature_description = { | ||
"moments": tf.io.FixedLenFeature([], tf.string), | ||
"clip_embeddings": tf.io.FixedLenFeature([], tf.string), | ||
} | ||
|
||
def map(self, example): | ||
def _parse(example): | ||
features = tf.io.parse_single_example(example, self.feature_description) | ||
moments = tf.io.parse_tensor(np.asarray(features["moments"]), out_type=tf.float32) | ||
clip_embeddings = tf.io.parse_tensor(np.asarray(features["clip_embeddings"]), out_type=tf.float32) | ||
return {"pixel_values": moments, "input_ids": clip_embeddings} | ||
|
||
return _parse(example) |
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