Repository navigation
[Train][Data] Fix double step count with TF MultiWorkerMirroredStrategy and pre-sharded dataset - #66189
[Train][Data] Fix double step count with TF MultiWorkerMirroredStrategy and pre-sharded dataset#66189lorenzozanee wants to merge 1 commit into
Conversation
…gy and pre-sharded dataset
There was a problem hiding this comment.
Code Review
This pull request introduces the write_orc method to Dataset to support writing datasets to ORC files, and updates to_tf to return a prefetched dataset with disabled auto-sharding to prevent double-sharding issues when using TensorFlow's MultiWorkerMirroredStrategy. Feedback on these changes includes a recommendation to replace a lambda assignment with a local function definition in write_orc to comply with PEP 8 (E731), as well as minor suggestions to correct a typo and remove redundant phrasing in the write_orc docstrings.
| if arrow_orc_args_fn is None: | ||
| arrow_orc_args_fn = lambda: {} # noqa: E731 |
There was a problem hiding this comment.
According to PEP 8 (E731), do not assign a lambda expression to an identifier. Use a local function definition (def) instead to improve readability and traceback support.
| if arrow_orc_args_fn is None: | |
| arrow_orc_args_fn = lambda: {} # noqa: E731 | |
| if arrow_orc_args_fn is None: | |
| def arrow_orc_args_fn(): | |
| return {} |
References
- PEP 8: Do not assign a lambda expression, use a def. (link)
| """Writes the :class:`~ray.data.Dataset` to ORC files. | ||
|
|
||
| The number of files is determined by the number of blocks in the dataset. | ||
| To control the number of number of blocks, call |
| try_create_dir: If ``True``, attempts to create all directories in the | ||
| destination path if ``True``. Does nothing if all directories already |
There was a problem hiding this comment.
The description for try_create_dir is redundant ("if True ... if True"). Let's simplify it for clarity.
| try_create_dir: If ``True``, attempts to create all directories in the | |
| destination path if ``True``. Does nothing if all directories already | |
| try_create_dir: If ``True``, attempts to create all directories in the | |
| destination path. Does nothing if all directories already |
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
|
This pull request has been automatically closed because there has been no more activity in the 14 days Please feel free to reopen or open a new pull request if you'd still like this to be addressed. Again, you can always ask for help on our discussion forum or Ray's public slack channel. Thanks again for your contribution! |
…es (#68006) ## Description With `tf.distribute.MultiWorkerMirroredStrategy`, the batch size of the dataset passed to `model.fit()` is the global batch size, and TensorFlow splits each batch evenly across the workers. Several Ray Train TensorFlow examples passed the per-worker batch size to `Dataset.to_tf()` or `iter_tf_batches()` instead. With 2 workers, every step then processes half the configured batch per worker, so Keras reports twice as many steps per epoch as expected (#61838). Ray's data split isn't actually the reason; each worker's shard has the expected number of batches. The Train guide's Step 2 already says to set the dataset's batch size to the per-worker batch size times the number of workers, and `tensorflow_mnist_example.py` already does so. These didn't: - `doc/source/train/doc_code/tf_starter.py`, the code shown on the TensorFlow guide page - `python/ray/train/examples/tf/tensorflow_regression_example.py` - `python/ray/train/examples/tf/tensorflow_autoencoder_example.py` Each now computes `global_batch_size = per_worker_batch_size * train.get_context().get_world_size()` and passes it to the dataset. In `doc/source/train/distributed-tensorflow-keras.md`, the Step 2 diff uses the same variable names as the example, and the inline sample, which hard-coded `batch_size=64`, uses the global batch size too. A new test, `test_tensorflow_dataset_batch_size_is_global` in `python/ray/train/tests/test_tensorflow_trainer.py`, runs a `TensorflowTrainer` with 1 and 2 workers and checks that Keras runs `rows_per_worker // per_worker_batch_size` steps in an epoch when the dataset uses the global batch size. Measured effect (200 rows for `tf_starter.py`, 2 workers, per-worker batch size 32; regression example with `--smoke-test`): | Example | Keras steps per epoch before | after | |---|---|---| | `tf_starter.py` | 8 | 4 (= ⌈100 ÷ 32⌉) | | `tensorflow_regression_example.py` | 400 | 200 | ## Related issues Fixes #61838 ## Additional information **Requested on the issue:** `pseudo-rnd-thoughts` asked for a doc PR with a test to confirm this works as expected. **Not a duplicate:** no open PR references #61838 or changes these files. The two earlier attempts, #65762 and #66189 (both closed as stale), changed Ray Train's data configuration. This PR changes only the examples, the guide, and adds a test, because the step count comes from how TensorFlow treats the batch size, not from how Ray splits the data. **Tests** (Ray 2.59.0, macOS, Python 3.12, TensorFlow 2.21 with `tf-keras`, `TF_USE_LEGACY_KERAS=1`): ``` python -m pytest test_tensorflow_trainer.py -k batch_size_is_global 2 passed # num_workers = 1 and 2 ``` As a negative control, passing the per-worker batch size instead (the old examples' behavior) fails the 2-worker case with `assert 20 == (160 // 16)`: twice the expected steps. The 1-worker case passes either way. I ran the examples before and after the change to get the table above. The test file and the regression example skip themselves on Python 3.12, because TensorFlow isn't installed there in CI. I ran copies with that guard removed, so CI is the first run of the actual files. I didn't run `tensorflow_autoencoder_example.py`, which downloads image data and has no CI test target; I checked that it compiles and passes the linters. The inline sample in the guide is a `{testcode}` block that only defines `train_func`, so the new `get_world_size()` call doesn't execute during the docs doctest. `pre-commit run --files` on all five changed files passes. AI assistance (Claude Code) was used for this change. --------- Signed-off-by: miggle711 <[email protected]>
Description
When using
TensorflowTrainerwithMultiWorkerMirroredStrategyand a pre-sharded Ray Dataset, the per-epoch step count was double the expected value. This change ensures the TF dataset is consistently marked as pre-sharded to prevent re-partitioning by the distribution strategy.Related issues
Fixes #61838
Additional information
N/A
Supercedes #65762(原PR分支已删,按原提交重建)