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

Add squeeze operator #2792

Merged
merged 24 commits into from
Mar 16, 2021
Merged

Add squeeze operator #2792

merged 24 commits into from
Mar 16, 2021

Conversation

majra20
Copy link
Contributor

@majra20 majra20 commented Mar 15, 2021

Why we need this PR?

  • It adds new feature needed because it add new functionality

What happened in this PR?

  • What solution was applied:
    • Added squeeze operator
  • Affected modules and functionalities:
    • Squeeze operator which allows to squeeze dimensions of size one
  • Key points relevant for the review:
    • Squeeze operator code, does it do everything we wanted
  • Validation and testing:
    • Python tests: test_operator_squeeze.py
  • Documentation (including examples):
    NA

JIRA TASK: DALI-1851

Rafal Maj added 14 commits March 16, 2021 09:02
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/operators/generic/squeeze.cc Show resolved Hide resolved
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/operators/generic/squeeze.cc Outdated Show resolved Hide resolved
dali/test/python/test_operator_squeeze.py Outdated Show resolved Hide resolved
dali/test/python/test_operator_squeeze.py Outdated Show resolved Hide resolved
dali/test/python/test_operator_squeeze.py Show resolved Hide resolved
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Rafal Maj added 2 commits March 16, 2021 10:00
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
namespace dali {

DALI_SCHEMA(Squeeze)
.DocStr(R"code(Collapses the dimensions given as axes or axis_names.
Copy link
Contributor

@mzient mzient Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.DocStr(R"code(Collapses the dimensions given as axes or axis_names.
.DocStr(R"code(Removes the dimensions given as ``axes`` or ``axis_names``.

"Collapse" could be misunderstood as "flatten" or "combine" - which, I believe, would make for a very useful operator, but it's not this one.

DALI_SCHEMA(Squeeze)
.DocStr(R"code(Collapses the dimensions given as axes or axis_names.

It's an error to collapse a dimension that would cause the total volume to change.)code")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It's an error to collapse a dimension that would cause the total volume to change.)code")
It's an error to remove a dimension that would cause the total volume to change.)code")

Rafal Maj added 2 commits March 16, 2021 13:40
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
All indices must be in the range of valid dimensions of the input)code", std::vector<int>(), true)
.AddOptionalArg("axis_names", R"code(Layout columns which should be removed.

All squeezed dimensions should have size 1 unless the total volume of the tensor is 0 before and after squeeze.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
All squeezed dimensions should have size 1 unless the total volume of the tensor is 0 before and after squeeze.
All squeezed dimensions should have size 1, unless the total volume of the tensor is 0 before and after squeeze.

nitpick

Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Comment on lines 52 to 53
DALI_ENFORCE(spec.HasArgument("axes") + spec.HasArgument("axis_names") == 1,
make_string("Provided both axes and axis_names argument"));
Copy link
Contributor

@mzient mzient Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DALI_ENFORCE(spec.HasArgument("axes") + spec.HasArgument("axis_names") == 1,
make_string("Provided both axes and axis_names argument"));
DALI_ENFORCE(spec.HasArgument("axes") + spec.HasArgument("axis_names") == 1,
spec.HasArgument("axes") ? "Provided both ``axes`` and ``axis_names`` arguments"
: "Missing argument ``axes`` or ``axis_names``." );

this->SetOutputType(ws);

GenerateSrcDims(ws);
Reshape<Backend>::CalculateOutputShape(ws);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would invoke one from the base class in case you overrode it - and you wouldn't even know. Use this-> to enable dependent name lookup.

Suggested change
Reshape<Backend>::CalculateOutputShape(ws);
this->CalculateOutputShape(ws);

Comment on lines 79 to 81
DALI_ENFORCE(in_layout.size() == ndim || in_layout.empty(),
make_string("Layout for data has ",
in_layout.size(), " elements but data has ", ndim, " dimensions."));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DALI_ENFORCE(in_layout.size() == ndim || in_layout.empty(),
make_string("Layout for data has ",
in_layout.size(), " elements but data has ", ndim, " dimensions."));

This is checked in the executor.

private:
void GenerateSrcDims(const Workspace &ws);

std::vector<int> axes_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<int> axes_;
SmallVector<int, 6> axes_;

This will avoid calling to_vector and a needless dynamic allocation.

in_layout.size(), " elements but data has ", ndim, " dimensions."));

this->src_dims_.clear();
auto axes = axis_names_.empty() ? axes_ : GetDimIndices(in_layout, axis_names_).to_vector();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you convert axes_ to SmallVector, you can skip to_vector and avoid allocating a handful of numbers from the heap.

Suggested change
auto axes = axis_names_.empty() ? axes_ : GetDimIndices(in_layout, axis_names_).to_vector();
auto axes = axis_names_.empty() ? axes_ : GetDimIndices(in_layout, axis_names_);

this->src_dims_.clear();
auto axes = axis_names_.empty() ? axes_ : GetDimIndices(in_layout, axis_names_).to_vector();
std::sort(axes.begin(), axes.end());
axes.erase(std::unique(axes.begin(), axes.end()), axes.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that specifying an axis multiple times should be an error, not something swept under the carpet.

Signed-off-by: Rafal Maj <rmaj@nvidia.com>
Comment on lines +96 to +100
if (!in_layout.empty()) {
out_layout += in_layout[d];
}
}
this->layout_ = out_layout;
Copy link
Contributor

@mzient mzient Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather a nitpick:

Suggested change
if (!in_layout.empty()) {
out_layout += in_layout[d];
}
}
this->layout_ = out_layout;
}
if (!in_layout.empty())
this->layout_ = permute(in_layout, this->src_dims_);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does something different then what I meant. I will skip it.

@majra20
Copy link
Contributor Author

majra20 commented Mar 16, 2021

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2171679]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2171679]: BUILD FAILED

Signed-off-by: Rafal Maj <rmaj@nvidia.com>
@majra20
Copy link
Contributor Author

majra20 commented Mar 16, 2021

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2171766]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2171766]: BUILD PASSED

@majra20 majra20 merged commit 58623e0 into NVIDIA:master Mar 16, 2021
@majra20 majra20 deleted the squeeze_operator branch March 17, 2021 08:14
@JanuszL JanuszL mentioned this pull request May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants