Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support checkpointing in Caffe reader #5181

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dali/operators/reader/caffe2_reader_op.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2017-2018, 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,13 @@

namespace dali {

class Caffe2Reader : public DataReader<CPUBackend, Tensor<CPUBackend>> {
class Caffe2Reader : public DataReader<CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true> {
public:
explicit Caffe2Reader(const OpSpec& spec)
: DataReader<CPUBackend, Tensor<CPUBackend>>(spec) {
: DataReader<CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true>(spec) {
loader_ = InitLoader<LMDBLoader>(spec);
parser_.reset(new Caffe2Parser(spec));
this->SetInitialSnapshot();
}

void RunImpl(SampleWorkspace &ws) override {
Expand All @@ -35,7 +36,7 @@ class Caffe2Reader : public DataReader<CPUBackend, Tensor<CPUBackend>> {
}

protected:
USE_READER_OPERATOR_MEMBERS(CPUBackend, Tensor<CPUBackend>);
USE_READER_OPERATOR_MEMBERS(CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true);
};

} // namespace dali
Expand Down
9 changes: 5 additions & 4 deletions dali/operators/reader/caffe_reader_op.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2017-2018, 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,13 @@

namespace dali {

class CaffeReader : public DataReader<CPUBackend, Tensor<CPUBackend>> {
class CaffeReader : public DataReader<CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true> {
public:
explicit CaffeReader(const OpSpec& spec)
: DataReader<CPUBackend, Tensor<CPUBackend>>(spec) {
: DataReader<CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true>(spec) {
loader_ = InitLoader<LMDBLoader>(spec);
parser_.reset(new CaffeParser(spec));
this->SetInitialSnapshot();
}

void RunImpl(SampleWorkspace &ws) override {
Expand All @@ -35,7 +36,7 @@ class CaffeReader : public DataReader<CPUBackend, Tensor<CPUBackend>> {
}

protected:
USE_READER_OPERATOR_MEMBERS(CPUBackend, Tensor<CPUBackend>);
USE_READER_OPERATOR_MEMBERS(CPUBackend, Tensor<CPUBackend>, Tensor<CPUBackend>, true);
};

} // namespace dali
Expand Down
12 changes: 8 additions & 4 deletions dali/operators/reader/loader/lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static int find_lower_bound(const std::vector<Index>& a, Index x) {
return -1;
}

class LMDBLoader : public Loader<CPUBackend, Tensor<CPUBackend>> {
class LMDBLoader : public Loader<CPUBackend, Tensor<CPUBackend>, true> {
public:
explicit LMDBLoader(const OpSpec& options)
: Loader(options) {
Expand Down Expand Up @@ -204,6 +204,10 @@ class LMDBLoader : public Loader<CPUBackend, Tensor<CPUBackend>> {
value.mv_size * sizeof(uint8_t));
}

void Skip() override {
MoveToNextShard(++current_index_);
}

protected:
Index SizeImpl() override {
return offsets_.size() > 0 ? offsets_.back() : 0;
Expand All @@ -224,7 +228,7 @@ class LMDBLoader : public Loader<CPUBackend, Tensor<CPUBackend>> {
void Reset(bool wrap_to_shard) override {
// work out how many entries to move forward to handle sharding
if (wrap_to_shard) {
current_index_ = start_index(shard_id_, num_shards_, SizeImpl());
current_index_ = start_index(virtual_shard_id_, num_shards_, SizeImpl());
} else {
current_index_ = 0;
}
Expand All @@ -233,8 +237,8 @@ class LMDBLoader : public Loader<CPUBackend, Tensor<CPUBackend>> {

mdb_[file_index].SeekByIndex(local_index);
}
using Loader<CPUBackend, Tensor<CPUBackend>>::shard_id_;
using Loader<CPUBackend, Tensor<CPUBackend>>::num_shards_;
using Loader<CPUBackend, Tensor<CPUBackend>, true>::virtual_shard_id_;
using Loader<CPUBackend, Tensor<CPUBackend>, true>::num_shards_;

std::vector<IndexedLMDB> mdb_;

Expand Down
56 changes: 56 additions & 0 deletions dali/test/python/checkpointing/test_dali_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,62 @@ def test_coco_reader(
image_ids=True)


@params(
(1, 3, 0, 1, True, True, True, 1),
(5, 5, 1, 3, True, True, False, 2),
(6, 7, 2, 3, True, False, True, 3),
(5, 3, 0, 1, True, False, False, 1),
(7, 5, 2, 3, False, True, True, None),
(4, 1, 1, 2, False, True, False, 2),
(0, 3, 3, 4, False, False, True, None),
(1, 4, 2, 3, False, False, False, 3),
)
def test_caffe_reader(
num_epochs, batch_size, shard_id, num_shards,
random_shuffle, stick_to_shard, pad_last_batch,
iters_into_epoch=None, initial_fill=1024):

caffe_dir = os.path.join(data_root, 'db', 'lmdb')

check_reader_checkpointing(
fn.readers.caffe, num_epochs, batch_size, iters_into_epoch,
path=caffe_dir,
pad_last_batch=pad_last_batch,
random_shuffle=random_shuffle,
shard_id=shard_id,
num_shards=num_shards,
stick_to_shard=stick_to_shard,
initial_fill=initial_fill)


@params(
(1, 2, 0, 2, True, True, True, 1),
(4, 4, 1, 2, True, True, False, 2),
(5, 6, 0, 2, True, False, True, None),
(6, 2, 1, 3, True, False, False, 1),
(3, 4, 3, 4, False, True, True, 2),
(8, 1, 2, 3, False, True, False, None),
(0, 2, 4, 5, False, False, True, None),
(3, 3, 1, 3, False, False, False, 2),
)
def test_caffe2_reader(
num_epochs, batch_size, shard_id, num_shards,
random_shuffle, stick_to_shard, pad_last_batch,
iters_into_epoch=None, initial_fill=1024):

caffe2_dir = os.path.join(data_root, 'db', 'c2lmdb')

check_reader_checkpointing(
fn.readers.caffe2, num_epochs, batch_size, iters_into_epoch,
path=caffe2_dir,
pad_last_batch=pad_last_batch,
random_shuffle=random_shuffle,
shard_id=shard_id,
num_shards=num_shards,
stick_to_shard=stick_to_shard,
initial_fill=initial_fill)


@attr('pytorch')
@params(
(1, 3, 0, 1, True, False, False),
Expand Down