-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: #617 Extract out the next-batch fetching logic from the loop script (train/evaluate/predict) to a method on Unit/AutoUnit Reviewed By: JKSenthil Differential Revision: D51162751 fbshipit-source-id: 67302adfd1ffce91f4942218e442ddfd16731c16
- Loading branch information
1 parent
8b258a6
commit f8e5313
Showing
8 changed files
with
219 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
from typing import Iterator | ||
|
||
import torch | ||
from torchtnt.framework._test_utils import get_dummy_train_state | ||
from torchtnt.framework.state import State | ||
from torchtnt.framework.unit import EvalUnit, PredictUnit, TrainUnit | ||
|
||
|
||
class TestUnit( | ||
EvalUnit[Iterator[torch.Tensor]], PredictUnit[torch.Tensor], TrainUnit[torch.Tensor] | ||
): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def train_step(self, state: State, data: torch.Tensor) -> None: | ||
return | ||
|
||
def eval_step(self, state: State, data: Iterator[torch.Tensor]) -> None: | ||
return | ||
|
||
def predict_step(self, state: State, data: torch.Tensor) -> None: | ||
return | ||
|
||
|
||
class UnitTest(unittest.TestCase): | ||
def test_initialization_and_get_next_batch(self) -> None: | ||
unit = TestUnit() | ||
self.assertIsNotNone(unit.train_progress) | ||
self.assertIsNotNone(unit.eval_progress) | ||
self.assertIsNotNone(unit.predict_progress) | ||
|
||
tensor_1 = torch.ones(1) | ||
tensor_2 = torch.zeros(1) | ||
state = get_dummy_train_state() | ||
|
||
# test train next batch - exepct to return the elements within the iterable | ||
train_data_iter = iter([tensor_1, tensor_2]) | ||
self.assertEqual(unit.get_next_train_batch(state, train_data_iter), tensor_1) | ||
self.assertEqual(unit.get_next_train_batch(state, train_data_iter), tensor_2) | ||
|
||
# test predict next batch - exepct to return the elements within the iterable | ||
self.assertEqual( | ||
unit.get_next_predict_batch(state, iter([tensor_1, tensor_2])), tensor_1 | ||
) | ||
|
||
# test eval next batch - exepct to return the iterable | ||
data_iter = iter([tensor_1, tensor_2]) | ||
next_eval_batch = unit.get_next_eval_batch(state, data_iter) | ||
self.assertEqual(next_eval_batch, data_iter) |
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
Oops, something went wrong.