Skip to content

Commit 4d93fc8

Browse files
jantonguiraoJanuszL
authored andcommitted
Fix nvJPEGDecoder cache when using new nvJPEG decoupled API (#748)
Signed-off-by: Joaquin Anton <janton@nvidia.com>
1 parent e9e9390 commit 4d93fc8

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

dali/pipeline/operators/decoder/nvjpeg_decoder_decoupled_api.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,16 @@ class nvJPEGDecoder : public Operator<MixedBackend>, CachedDecoderImpl {
224224

225225
const auto &in = ws->Input<CPUBackend>(0, i);
226226
const auto file_name = in.GetSourceInfo();
227-
cudaStream_t stream = ws->stream();
228227
auto *output_data = output.mutable_tensor<uint8_t>(i);
229228
auto dims = output_shape_[i];
230229
ImageCache::ImageShape shape = {dims[0], dims[1], dims[2]};
231230
thread_pool_.DoWorkWithID(
232-
[this, i, file_name, stream, &in, output_data, shape](int tid) {
233-
if (CacheLoad(file_name, shape, output_data, stream))
231+
[this, i, file_name, &in, output_data, shape](int tid) {
232+
if (CacheLoad(file_name, shape, output_data, streams_[tid]))
234233
return;
235-
236234
SampleWorker(i, file_name, in.size(), tid,
237235
in.data<uint8_t>(), output_data);
238-
239-
CacheStore(file_name, output_data, shape, stream);
236+
CacheStore(file_name, output_data, shape, streams_[tid]);
240237
});
241238
}
242239

dali/test/python/test_pipeline.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
caffe_db_folder = "/data/imagenet/train-lmdb-256x256"
2424

25+
def check_batch(batch1, batch2, batch_size, eps = 0.0000001):
26+
for i in range(batch_size):
27+
err = np.mean( np.abs(batch1.at(i) - batch2.at(i)) )
28+
assert(err < eps)
29+
2530
def test_tensor_multiple_uses():
2631
batch_size = 128
2732
class HybridPipe(Pipeline):
@@ -772,3 +777,38 @@ def iter_setup(self):
772777
assert out2.shape == out4.shape
773778
np.testing.assert_array_equal( expected_last, out2 )
774779
np.testing.assert_array_equal( expected_last, out4 )
780+
781+
def test_nvjpegdecoder_cached_vs_non_cached():
782+
"""
783+
Checking that cached nvJPEGDecoder produces the same output as non cached version
784+
"""
785+
batch_size = 26
786+
787+
class ComparePipeline(Pipeline):
788+
def __init__(self, batch_size=batch_size, num_threads=1, device_id=0, num_gpus=10000):
789+
super(ComparePipeline, self).__init__(batch_size, num_threads, device_id, prefetch_queue_depth = 1)
790+
self.input = ops.CaffeReader(path = caffe_db_folder, shard_id = device_id, num_shards = num_gpus, stick_to_shard = True)
791+
self.decode_non_cached = ops.nvJPEGDecoder(device = "mixed", output_type = types.RGB)
792+
self.decode_cached = ops.nvJPEGDecoder(device = "mixed", output_type = types.RGB,
793+
cache_size=8000,
794+
cache_threshold=0,
795+
cache_type='threshold',
796+
cache_debug=False)
797+
798+
def define_graph(self):
799+
self.jpegs, self.labels = self.input()
800+
images_non_cached = self.decode_non_cached(self.jpegs)
801+
images_cached = self.decode_cached(self.jpegs)
802+
return (images_non_cached, images_cached)
803+
804+
def iter_setup(self):
805+
pass
806+
807+
pipe = ComparePipeline()
808+
pipe.build()
809+
N_iterations = 100
810+
for k in range(N_iterations):
811+
pipe_out = pipe.run()
812+
non_cached_data = pipe_out[0].as_cpu()
813+
cached_data = pipe_out[1].as_cpu()
814+
check_batch(non_cached_data, cached_data, batch_size)

0 commit comments

Comments
 (0)