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

Tensor concatenation and stacking #2350

Merged
merged 4 commits into from
Oct 13, 2020
Merged

Conversation

mzient
Copy link
Contributor

@mzient mzient commented Oct 12, 2020

Why we need this PR?

Pick one, remove the rest

  • It adds new feature: tensor concatenation and stacking operators

What happened in this PR?

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

  • What solution was applied:
    • Use TensorJoinCPU/TenosrJoinGPU kernels in the operator
    • Use a simple copy/reshaed copy when there's just 1 input
    • Use any to store type-specific data in the operator and avoid writing full-blown pImpl
    • Use function overloads to provide backend-specific RunImpl and have just one class
    • Register the operator with different template arguments (backend and new_axis) as Cat/Stack CPU/GPU
  • Affected modules and functionalities:
    • TensorJoin operator
    • Constant operator (minor fix)
  • Key points relevant for the review:
    • Axis handling
    • Use of any for inputs
  • Validation and testing:
    • Python tests
  • Documentation (including examples):
    • Docstrings
    • Jupyter

JIRA TASK: DALI-1620

@mzient mzient requested a review from a team October 12, 2020 19:51
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
Added tests.
Added special handling for a simple copy (cat) and reshaped copy (stack).
Added a jupyter notebook with examples.
Removed unnecessary assert from Constant op.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@mzient
Copy link
Contributor Author

mzient commented Oct 13, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1697570]: BUILD STARTED

"source": [
"# Tensor joining\n",
"\n",
"This notebook demonstrates two metbhods of joining tensors: stacking and concatenation.\n",
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
"This notebook demonstrates two metbhods of joining tensors: stacking and concatenation.\n",
"This notebook demonstrates two methods of joining tensors: stacking and concatenation.\n",

"\n",
"Both of these operations take multiple inputs and produce the output by joining the input tensors.\n",
"The difference between these methods is that concatenation joins the tensors along an existing axis, whereas stacking inserts a new axis.\n",
"Stacking can be used, for example, to combine separate combine separate coordinates into vectors, or to combine color planes into color images.\n",
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
"Stacking can be used, for example, to combine separate combine separate coordinates into vectors, or to combine color planes into color images.\n",
"Stacking can be used, for example, to combine separate coordinates into vectors, or to combine color planes into color images.\n",

src2 = dali.types.Constant(np.array(
[[],
[],
[]], dtype=np.int32))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it in purpose that only src2 has a dtype?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Others have it inferred from the values - here there are no values and it defaulted to float.

out.SetLayout(output_layout_);
TYPE_SWITCH(out.type().id(), type2id, T, TENSOR_JOIN_TYPES, (
RunTyped(view<T>(out), ws);
), (DALI_FAIL("Internal error: unsupported type reached RunImpl function"))); // NOLINT
Copy link
Contributor

Choose a reason for hiding this comment

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

print the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it was ever reached, then printing the type would shift attention from the real problem: that we've encountered a type that should have been rejected in Setup.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the user cares how fatal the error is, and the more debug info we get from the user the better.

This argument is mutually exclusive with ``axis``.
This argument requires that at least one input has a non-empty layout and that all non-empty
input layouts match.)", nullptr, false)
.NumInput(1, 999)
Copy link
Contributor

Choose a reason for hiding this comment

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

999? Just checking, did you mean 99 (I've seen it as a limit to other operators). By the way, why not 100 or 1000?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should have a define for an infinite number of inputs (999 + 1)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea, but maybe not for this PR...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure

.AddOptionalArg<int>("axis", R"(The axis in the output tensor along which the inputs are stacked.

The axis is inserted before a corresponding axis in the inputs. A value of 0 indicates that whole
tensors are stacked. Speicfying ``axis`` equal to the number of dimensions in the inputs causes
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
tensors are stacked. Speicfying ``axis`` equal to the number of dimensions in the inputs causes
tensors are stacked. Specifying ``axis`` equal to the number of dimensions in the inputs causes

the values from the inputs to be interleaved)", 0, false)
.AddOptionalArg<string>("axis_name", R"(Name of the new axis to be inserted.

A one-character that will denot the new axis in the output layout. The output layout will be
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
A one-character that will denot the new axis in the output layout. The output layout will be
A one-character that will denote the new axis in the output layout. The output layout will be

.AddOptionalArg<string>("axis_name", R"(Name of the new axis to be inserted.

A one-character that will denot the new axis in the output layout. The output layout will be
constructed by inserting that character into the input layout at position indicated by ``axis``.
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
constructed by inserting that character into the input layout at position indicated by ``axis``.
constructed by inserting that character into the input layout at the position indicated by ``axis``.

template <typename Backend, bool new_axis>
void TensorJoin<Backend, new_axis>::SetupAxis() {
// axis_name indicates the join axis for concatenation only;
// for stacking, it's the name of the new axiis
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
// for stacking, it's the name of the new axiis
// for stacking, it's the name of the new axis

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1697570]: BUILD FAILED

SetupAxis();
SetOutputLayout(ws);

// Run ove inputs and store them in a vector
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
// Run ove inputs and store them in a vector
// Run over inputs and store them in a vector

@@ -0,0 +1,404 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

metbhods -> methods

combine separate combine separate coordinates into vectors -> combine separate coordinates into vectors


Reply via ReviewNB

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@mzient
Copy link
Contributor Author

mzient commented Oct 13, 2020

!build

@mzient mzient changed the title Tensor join ops Tensor concatenation and stacking Oct 13, 2020
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1697830]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1697830]: BUILD PASSED

@mzient mzient merged commit 296663c into NVIDIA:master Oct 13, 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