|
24 | 24 | namespace dali::c_api::test { |
25 | 25 |
|
26 | 26 | std::unique_ptr<Pipeline> |
27 | | -ReaderDecoderPipe(std::string_view decoder_device, StorageDevice output_device) { |
| 27 | +ReaderDecoderPipe( |
| 28 | + std::string_view decoder_device, |
| 29 | + StorageDevice output_device, |
| 30 | + PipelineParams params = {}) { |
28 | 31 | std::string file_root = testing::dali_extra_path() + "/db/single/jpeg/"; |
29 | 32 | std::string file_list = file_root + "image_list.txt"; |
30 | | - auto pipe = std::make_unique<Pipeline>(4, 1, 0, 12345, true, 2, true, true); |
| 33 | + if (!params.max_batch_size) params.max_batch_size = 4; |
| 34 | + if (!params.num_threads) params.num_threads = 1; |
| 35 | + if (!params.seed) params.seed = 12345; |
| 36 | + if (!params.executor_type) params.executor_type = ExecutorType::Dynamic; |
| 37 | + auto pipe = std::make_unique<Pipeline>(params); |
31 | 38 | pipe->AddOperator(OpSpec("FileReader") |
32 | 39 | .AddArg("device", "cpu") |
33 | 40 | .AddArg("file_root", file_root) |
@@ -81,4 +88,79 @@ TEST(CAPI2_PipelineTest, ReaderDecoderMixed2CPU) { |
81 | 88 | TestReaderDecoder("mixed", StorageDevice::CPU); |
82 | 89 | } |
83 | 90 |
|
| 91 | +TEST(CAPI2_PipelineTest, Checkpointing) { |
| 92 | + // This test creates three pipelines - a C++ pipeline (ref) and two C pipelines (pipe1, pipe2), |
| 93 | + // created by deserializing the serialized representation of the C++ pipeline. |
| 94 | + // |
| 95 | + // (pipe1) advances 3 iterations and then a checkpoint is taken and restored in (ref), |
| 96 | + // after which 5 iterations of outputs are compared. |
| 97 | + // Then a checkpoint is taken in (ref) and restored in (pipe2), after which another 5 iterations |
| 98 | + // are compared. |
| 99 | + PipelineParams params{}; |
| 100 | + params.enable_checkpointing = true; |
| 101 | + params.seed = 1234; |
| 102 | + auto ref = ReaderDecoderPipe("cpu", StorageDevice::GPU, params); |
| 103 | + ref->Build(); |
| 104 | + auto pipe_str = ref->SerializeToProtobuf(); // serialize the ref... |
| 105 | + auto pipe1 = Deserialize(pipe_str, {}); // ...and create pipe1 |
| 106 | + auto pipe2 = Deserialize(pipe_str, {}); // ...and pipe2 from serialized ref |
| 107 | + CHECK_DALI(daliPipelineBuild(pipe1)); |
| 108 | + CHECK_DALI(daliPipelineBuild(pipe2)); |
| 109 | + |
| 110 | + // Advance a few iterations... |
| 111 | + CHECK_DALI(daliPipelinePrefetch(pipe1)); |
| 112 | + daliPipelineOutputs_h out1_h{}; |
| 113 | + CHECK_DALI(daliPipelinePopOutputs(pipe1, &out1_h)); |
| 114 | + CHECK_DALI(daliPipelineOutputsDestroy(out1_h)); |
| 115 | + CHECK_DALI(daliPipelineRun(pipe1)); |
| 116 | + CHECK_DALI(daliPipelinePopOutputs(pipe1, &out1_h)); |
| 117 | + CHECK_DALI(daliPipelineOutputsDestroy(out1_h)); |
| 118 | + CHECK_DALI(daliPipelineRun(pipe1)); |
| 119 | + CHECK_DALI(daliPipelinePopOutputs(pipe1, &out1_h)); |
| 120 | + CHECK_DALI(daliPipelineOutputsDestroy(out1_h)); |
| 121 | + |
| 122 | + const char pipeline_data[] = "A rose by any other name would smell as sweet"; |
| 123 | + size_t pipeline_data_size = strlen(pipeline_data); |
| 124 | + |
| 125 | + daliCheckpointExternalData_t ext{}; |
| 126 | + ext.iterator_data.data = "ITER"; |
| 127 | + ext.iterator_data.size = 4; |
| 128 | + ext.pipeline_data.data = pipeline_data; |
| 129 | + ext.pipeline_data.size = strlen(ext.pipeline_data.data); |
| 130 | + |
| 131 | + daliCheckpoint_h checkpoint_h{}; |
| 132 | + // Take a checkpoint... |
| 133 | + CHECK_DALI(daliPipelineGetCheckpoint(pipe1, &checkpoint_h, &ext)); |
| 134 | + CheckpointHandle checkpoint(checkpoint_h); |
| 135 | + |
| 136 | + const char *data = nullptr; |
| 137 | + size_t size = 0; |
| 138 | + CHECK_DALI(daliPipelineSerializeCheckpoint(pipe1, checkpoint, &data, &size)); |
| 139 | + ASSERT_NE(data, nullptr); |
| 140 | + |
| 141 | + // ...restore... |
| 142 | + ref->RestoreFromSerializedCheckpoint(std::string(data, size)); |
| 143 | + // ...run and compare. |
| 144 | + ComparePipelineOutputs(*ref, pipe1, 5, false); |
| 145 | + |
| 146 | + // Now take another checkpoint... |
| 147 | + auto chk_str = ref->GetSerializedCheckpoint({ ext.pipeline_data.data, ext.iterator_data.data }); |
| 148 | + |
| 149 | + // ...deserialize... |
| 150 | + CHECK_DALI(daliPipelineDeserializeCheckpoint( |
| 151 | + pipe2, &checkpoint_h, chk_str.data(), chk_str.length())); |
| 152 | + CheckpointHandle checkpoint2(checkpoint_h); |
| 153 | + |
| 154 | + daliCheckpointExternalData_t ext2{}; |
| 155 | + CHECK_DALI(daliCheckpointGetExternalData(checkpoint2, &ext2)); |
| 156 | + EXPECT_EQ(ext2.iterator_data.size, 4); |
| 157 | + EXPECT_STREQ(ext2.iterator_data.data, "ITER"); |
| 158 | + EXPECT_EQ(ext2.pipeline_data.size, pipeline_data_size); |
| 159 | + EXPECT_STREQ(ext2.pipeline_data.data, pipeline_data); |
| 160 | + // ...restore... |
| 161 | + CHECK_DALI(daliPipelineRestoreCheckpoint(pipe2, checkpoint2)); |
| 162 | + // ...run and compare. |
| 163 | + ComparePipelineOutputs(*ref, pipe2, 5, false); |
| 164 | +} |
| 165 | + |
84 | 166 | } // namespace dali::c_api::test |
0 commit comments