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

Pad operator: Add support for per-sample shape and alignment requirements #2432

Merged
merged 4 commits into from
Nov 5, 2020

Conversation

jantonguirao
Copy link
Contributor

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

Why we need this PR?

  • It adds new feature needed to support different Pad shape/align requirements per sample

What happened in this PR?

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

  • What solution was applied:
    Pad operator: shape/align to support argument inputs
    (Bonus, needed for testing) Uniform operator: shape to support argument inputs
  • Affected modules and functionalities:
    Pad, Uniform
  • Key points relevant for the review:
    N/A
  • Validation and testing:
    Tests added
  • Documentation (including examples):
    N/A

JIRA TASK: [DALI-1669]

@@ -31,6 +31,8 @@ bool Pad<GPUBackend>::SetupImpl(std::vector<OutputDesc> &output_desc,
int ndim = in_shape.sample_dim();
int nsamples = in_shape.num_samples();

this->ReadArguments(spec_, ws);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

gave me an error without this->

@jantonguirao jantonguirao force-pushed the pad_arg_input_shape branch 3 times, most recently from ee671f3 to 8e04c95 Compare November 3, 2020 13:39
…ents

Signed-off-by: Joaquin Anton <janton@nvidia.com>
if (remainder > 0)
extent += alignment - remainder;
return extent;
void ReadShapeListArg(std::vector<TensorShape<>> &out, const std::string &arg_name,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you use GetShapeArgument from operator/common.h?

output_desc[0].type = TypeTable::GetTypeInfo(DALI_FLOAT);
auto& sh = output_desc[0].shape;
if (spec_.HasTensorArgument("shape")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise - use GetShapeArgument?

Signed-off-by: Joaquin Anton <janton@nvidia.com>
out_tls.resize(out_tls.shape().num_samples / ndim, ndim);
ndim = GetShapeLikeArgument<ArgumentType>(out_tls.shapes, spec, argument_name,
ws, ndim, batch_size);
out_tls.resize(ndim == 0 ? 0 : out_tls.shapes.size() / ndim, ndim);
Copy link
Contributor

@mzient mzient Nov 3, 2020

Choose a reason for hiding this comment

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

This is wrong (and it's my fault). Please change semantics of GetShapeLikeArgument to return number of samples, not dimensions (or both, perhaps). The return value is not used anywhere except tests, so it won't break anything.

@@ -75,7 +75,7 @@ This argument is mutually exclusive with ``values``.
This argument is mutually exclusive with ``range``.
)code", std::vector<float>({}))
.AddOptionalArg("shape",
R"code(Shape of the samples.)code", std::vector<int>{1});
R"code(Shape of the samples.)code", std::vector<int>{1}, true);
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
R"code(Shape of the samples.)code", std::vector<int>{1}, true);
R"code(Shape of the samples.)code", std::vector<int>{}, true);

The default has been changed to generate scalars, not 1-element 1D tensors. It should stay like this!

@jantonguirao jantonguirao force-pushed the pad_arg_input_shape branch 4 times, most recently from fc34aa9 to ec781ee Compare November 4, 2020 14:59
template <typename It,
typename = std::enable_if<is_integer_iterator<It>::value>>
TensorShape(It first, It last) {
int sz = last - first;
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
int sz = last - first;
std::ptrdiff_t sz = last - first;

although I don't know if we need to require contiguity at all

Comment on lines 203 to 204
std::pair<int, int> GetShapeArgument(TensorListShape<out_ndim> &out_tls, const OpSpec &spec,
const std::string &argument_name, const ArgumentWorkspace &ws,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the formatting change? This grouping seems random.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CTRL+F did that. I can restore

Comment on lines 140 to 145
std::pair<int, int> GetShapeLikeArgument(std::vector<ExtentType> &out_shape, const OpSpec &spec,
const std::string &argument_name,
const ArgumentWorkspace &ws, int ndim = -1,
int batch_size = -1 /* -1 = get from "batch_size" arg */) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Random grouping of arguments.

if (spec_.ArgumentDefined("shape")) {
GetShapeArgument(output_desc[0].shape, spec_, "shape", ws, -1, batch_size_);
} else {
output_desc[0].shape = uniform_list_shape(batch_size_, TensorShape<>{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
output_desc[0].shape = uniform_list_shape(batch_size_, TensorShape<>{1});
output_desc[0].shape = uniform_list_shape(batch_size_, TensorShape<0>());

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bug. Please fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true

Comment on lines 255 to 257
template <typename It,
typename = std::enable_if<is_integer_iterator<It>::value>>
TensorShape(It first, It last) : Base() {
Copy link
Contributor

Choose a reason for hiding this comment

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

missing:
DALI_NO_EXEC_CHECK
DALI_HOST_DEV
Unnecessary explicit invocation of base's default constructor.

Comment on lines 269 to 277
template <typename Collection,
typename = std::enable_if_t<is_integer_collection<Collection>::value>>
TensorShape(const Collection &c) // NOLINT(runtime/explicit)
: TensorShape(dali::begin(c), dali::end(c)) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

DALI_NO_EXEC_CHECK
DALI_HOST_DEV

@jantonguirao jantonguirao force-pushed the pad_arg_input_shape branch 2 times, most recently from 3560aa2 to 17287f5 Compare November 4, 2020 15:51
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1762284]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1762284]: BUILD FAILED

@@ -197,19 +198,20 @@ int GetShapeLikeArgument(std::vector<ExtentType> &out_shape,
out_shape[i * ndim + d] = to_extent(tsvec[d]);
}

return ndim;
return {batch_size, ndim};
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to return pair?
Shape it fill is accepted as an argument, so we can do that with batch_size and ndim as well.
ret.first, ret.second is not very self descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that there are already input ndim and batch size arguments that can be omitted or in the case of ndim, also inferred from the type (TensorShape<ndim>). IMHO it would be too messy to use those both as optional inputs and also as outputs, that's why I am using return

Copy link
Contributor

Choose a reason for hiding this comment

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

I just wonder if this is not too pythonic. But sure.

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

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1765042]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1765042]: BUILD PASSED

@jantonguirao jantonguirao merged commit db2df9f into NVIDIA:master Nov 5, 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.

4 participants