* @brief Restores the loader's state from a snapshot. */
| 171 | * @brief Restores the loader's state from a snapshot. |
| 172 | */ |
| 173 | void RestoreStateFromSnapshot(const LoaderStateSnapshot& state) { |
| 174 | DALI_ENFORCE(IsCheckpointingEnabled(), |
| 175 | "Checkpointing was not enabled. Please make sure you set" |
| 176 | " enable_checkpointing to True when creating the pipeline."); |
| 177 | |
| 178 | RestoreEpochState(state); |
| 179 | SaveStateSnapshot(current_snapshot_); |
| 180 | |
| 181 | // Now fast forward the loader by `state.age` steps: |
| 182 | // |
| 183 | // 1. Run in dry mode to see which samples actually need to be read. |
| 184 | // We don't need to read samples that will leave the buffer during fast-forward. |
| 185 | for (Index i = 0; i < state.age; i++) { |
| 186 | Index pos_in_batch = (returned_sample_counter_ + i) % max_batch_size_; |
| 187 | ReadOne(pos_in_batch == 0, pos_in_batch == max_batch_size_ - 1, [](Index i){ return false; }); |
| 188 | } |
| 189 | auto missing = GetMissingSamples(); |
| 190 | |
| 191 | // 2. Restore the state and run reading again, this time reading the needed samples. |
| 192 | RestoreEpochState(state); |
| 193 | for (Index i = 0; i < state.age; i++) { |
| 194 | Index pos_in_batch = (returned_sample_counter_ + i) % max_batch_size_; |
| 195 | auto filter = [&missing](Index idx) { |
| 196 | return missing.find(idx) != missing.end(); |
| 197 | }; |
| 198 | ReadOne(pos_in_batch == 0, pos_in_batch == max_batch_size_ - 1, filter); |
| 199 | } |
| 200 | |
| 201 | DALI_ENFORCE(GetMissingSamples().empty(), "Internal error: reading missing samples failed"); |
| 202 | |
| 203 | // current_snapshot_.age was increased by `ReadOne` calls, reset it to correct value |
| 204 | current_snapshot_.age = state.age; |
| 205 | } |
| 206 | |
| 207 | bool ShouldPadBatch(bool is_new_batch) { |
| 208 | // If the reader has depleted samples from the given shard, but shards are not equal |