1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <iostream>
17
18 #include "minddata/dataset/engine/datasetops/epoch_ctrl_op.h"
19
20 #include "minddata/dataset/util/log_adapter.h"
21
22 namespace mindspore {
23 namespace dataset {
24 // Constructor
EpochCtrlOp(int32_t num_epoch)25 EpochCtrlOp::EpochCtrlOp(int32_t num_epoch) : RepeatOp(num_epoch) { MS_LOG(INFO) << "Welcome to Epoch Ctrl Op."; }
26
27 // Destructor
~EpochCtrlOp()28 EpochCtrlOp::~EpochCtrlOp() {}
29
30 // A print method typically used for debugging
Print(std::ostream & out,bool show_all) const31 void EpochCtrlOp::Print(std::ostream &out, bool show_all) const {
32 if (!show_all) {
33 // Call the super class for displaying any common 1-liner info
34 PipelineOp::Print(out, show_all);
35 // Then show any custom derived-internal 1-liner info for this op
36 out << " [epochs: " << num_repeats_ << "]\n";
37 } else {
38 // Call the super class for displaying any common detailed info
39 RepeatOp::Print(out, show_all);
40 }
41 }
42
GetNextRow(TensorRow * row,int32_t worker_id,bool retry_if_eoe)43 Status EpochCtrlOp::GetNextRow(TensorRow *row, int32_t worker_id, bool retry_if_eoe) {
44 if (child_.empty()) {
45 RETURN_STATUS_UNEXPECTED("EpochCtrlOp can't be the leaf node(first operator) of pipeline.");
46 }
47
48 // `retry_if_eoe` is false because EpochCtrlOp does not eat EOE.
49 RETURN_IF_NOT_OK(child_[0]->GetNextRow(row, worker_id, false));
50
51 // Only intercept EOE for EoeReceived processing, after that the EOE is forwarded to next op.
52 // Other TensorRows containing data or EOF will simply be forwarded.
53 // EOF can simply be forwarded because this op does not spawn any thread, thus does not require clean up.
54 if (row->eoe()) {
55 RETURN_IF_NOT_OK(EoeReceived(worker_id));
56 }
57
58 return Status::OK();
59 }
60
EoeReceived(int32_t worker_id)61 Status EpochCtrlOp::EoeReceived(int32_t worker_id) {
62 UpdateRepeatAndEpochCounter();
63 repeat_count_++;
64 MS_LOG(DEBUG) << "Epoch Control operator received end of epoch. Epoch count is now: " << repeat_count_
65 << ". Max epochs: " << num_repeats_;
66
67 // This will allow GetNextInput in DatasetOp class to pass EOE row instead of eating it.
68 state_ = OpState::kDeOpIdle;
69
70 if (repeat_count_ != num_repeats_) {
71 for (auto &eoe_op : eoe_ops_) {
72 MS_LOG(DEBUG) << "Epoch Control driving reset to op: " << eoe_op->id();
73 RETURN_IF_NOT_OK(eoe_op->Reset());
74 }
75 }
76
77 return Status::OK();
78 }
79
GetTreeRepeatCount()80 int64_t EpochCtrlOp::GetTreeRepeatCount() { return child_[0]->GetTreeRepeatCount(); }
81 } // namespace dataset
82 } // namespace mindspore
83