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

Deprecate squeeze_labels option from MXNet iterator and enhance .squeeze function to match numpy style interface #2450

Merged
merged 3 commits into from
Nov 17, 2020

Conversation

jantonguirao
Copy link
Contributor

Signed-off-by: Joaquin Anton janton@nvidia.com

Why we need this PR?

Pick one, remove the rest

  • It fixes a bug in MxNet generic iterator, when batch_size is 1

What happened in this PR?

Fill relevant points, put NA otherwise. Replace anything inside []

  • What solution was applied:
    The option squeeze_labels=True caused the batch dimension to disappear when batch_size=1
  • Affected modules and functionalities:
    MXNet generic iterator
  • Key points relevant for the review:
    All
  • Validation and testing:
    Existing tests
  • Documentation (including examples):
    N/A

JIRA TASK: [Use DALI-XXXX or NA]

@jantonguirao jantonguirao changed the title Remove squeeze_labels option from MxNet iterator Remove squeeze_labels option from MXNet iterator Nov 10, 2020
@jantonguirao jantonguirao changed the title Remove squeeze_labels option from MXNet iterator Deprecate squeeze_labels option from MXNet iterator and enhance .squeeze function to resemble numpy Nov 16, 2020
@jantonguirao jantonguirao changed the title Deprecate squeeze_labels option from MXNet iterator and enhance .squeeze function to resemble numpy Deprecate squeeze_labels option from MXNet iterator and enhance .squeeze function to match numpy style interface Nov 16, 2020
Whether the iterator should squeeze the labels before
copying them to the ndarray.
This argument is deprecated and will be removed from future releases
without further notice.
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
without further notice.

(-2, (3, 1, 6)),
(None, (1, 1, 6)),
(1, (1, 1, 6)),
#(None, (1, 1, 1)), # Numpy produces a scalar in this case (probably we should too)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we address this now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We should.

Signed-off-by: Joaquin Anton <janton@nvidia.com>
*/
inline void Squeeze() {
std::vector<Index> shape(shape_.begin(), shape_.end());
SmallVector<int64_t, 6> shape(shape_.begin(), shape_.end());
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
SmallVector<int64_t, 6> shape(shape_.begin(), shape_.end());
auto &shape = shape_.shape;

shape.erase(std::remove(shape.begin(), shape.end(), 1), shape.end());
if (shape.empty()) {
shape.push_back(1);
shape_ = shape;
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is not necessary when you follow the suggestion above.

DALI_ENFORCE(dim >= -ndim && dim <= (ndim - 1),
make_string("axis ", dim, " is out of bound for a tensor with ", shape_.size(),
" dimensions."));
if (dim < 0)
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 we should remove corresponding dimension from the layout, if present.

Signed-off-by: Joaquin Anton <janton@nvidia.com>
shape.erase(std::remove(shape.begin(), shape.end(), 1), shape.end());
if (shape.empty()) {
shape.push_back(1);
SmallVector<int64_t, 6> out_shape;
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
SmallVector<int64_t, 6> out_shape;
DynamicTensorShapeContainer out_shape;

if (!in_layout.empty())
out_layout += in_layout[d];
}
shape_ = out_shape;
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
shape_ = out_shape;
shape_ = std::move(out_shape);

* equal to 1.
* @param dim Dimension to be squeezed. Negative indexing is also suppo9rted
*/
inline void Squeeze(int dim) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Following that definition (the function does or does not squeeze), maybe return a bool?

Suggested change
inline void Squeeze(int dim) {
inline bool Squeeze(int dim) {

auto layout = GetLayout();
if (!layout.empty()) {
SetLayout(layout.first(dim) + layout.sub(dim + 1));
}
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
}
}
return true;

}
shape_ = shape;
}
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
}
return false;
}

@@ -302,10 +312,29 @@ class TensorLayout {

DALI_HOST_DEV
friend constexpr TensorLayout operator+(const TensorLayout &a, const TensorLayout &b);
DALI_HOST_DEV
friend constexpr TensorLayout operator+(const TensorLayout &a, const char &b);
Copy link
Contributor

@mzient mzient Nov 16, 2020

Choose a reason for hiding this comment

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

Don't pass primitive types by reference

Suggested change
friend constexpr TensorLayout operator+(const TensorLayout &a, const char &b);
friend constexpr TensorLayout operator+(const TensorLayout &a, char b);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it is a leftover (I was trying to figure out a compile error, that turned out to be the lack of operator+=). I'll revert to pass by value

};

static_assert(sizeof(TensorLayout) == 16, "Tensor layout size should be exactly 16B");

/** @brief Appends a single element to the layout string */
DALI_HOST_DEV
constexpr TensorLayout operator+(const TensorLayout &a, const char &b) {
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
constexpr TensorLayout operator+(const TensorLayout &a, const char &b) {
constexpr TensorLayout operator+(const TensorLayout &a, char b) {

Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1806157]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1806157]: BUILD PASSED

@jantonguirao jantonguirao merged commit fc78b95 into NVIDIA:master Nov 17, 2020
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.

None yet

4 participants