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

Update documentation of random.uniform to reflect data type conversion behavior #3013

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion dali/operators/random/rng_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ It should be added as parent to all RNG operators.)code")
.AddOptionalArg<std::vector<int>>("shape",
R"code(Shape of the output data.)code", nullptr, true)
.AddOptionalArg<DALIDataType>("dtype",
R"code(Output data type.)code", nullptr);
R"code(Output data type.

.. note::
The generated numbers are converted to the output data type, rounding and clamping if necessary.
)code", nullptr);

} // namespace dali
26 changes: 22 additions & 4 deletions dali/operators/random/uniform_distribution_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ namespace dali {
DALI_SCHEMA(random__Uniform)
.DocStr(R"code(Generates random numbers following a uniform distribution.

It can be configured to produce a continuous uniform distribution in the ``range`` [min, max),
or a discrete uniform distribution where any of the specified ``values`` [v0, v1, ..., vn] occur
with equal probability.

The shape of the generated data can be either specified explicitly with a ``shape`` argument,
or chosen to match the shape of the input, if provided. If none are present, a scalar is
generated.
Expand All @@ -30,10 +34,15 @@ generated.
.AddOptionalArg("range",
R"code(Range ``[min, max)`` of a continuous uniform distribution.

This argument is mutually exclusive with ``values``.)code",
This argument is mutually exclusive with ``values``.

.. warning::
When specifying an integer type as ``dtype``, the generated numbers can go outside
the specified range, due to rounding.
Copy link
Contributor

Choose a reason for hiding this comment

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

the generated numbers can go outside the specified range

Can't we clamp? It is a bit unexpected.

Copy link
Contributor Author

@jantonguirao jantonguirao Jun 2, 2021

Choose a reason for hiding this comment

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

So the RNG work like that, you generate the number in whatever is the natural type for the distribution, in this case float. Then if the user request the output data type to be a different type, we just ConvertSat(...). It's equivalent to doing fn.cast( fn.random.uniform(...), dtype=...), the numbers when converted to an integer data type, can go outside of the float range that was specified for the distribution. If the user needs a continuous distribution, it doesn't make much sense to request dtype to be an integer data type, I'd say.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense.

)code",
std::vector<float>{-1.0f, 1.0f}, true)
.AddOptionalArg<std::vector<float>>("values",
R"code(The discrete values produced by a discrete uniform distribution.
R"code(The discrete values [v0, v1, ..., vn] produced by a discrete uniform distribution.

This argument is mutually exclusive with ``range``.)code",
nullptr, true)
Expand All @@ -45,6 +54,10 @@ DALI_REGISTER_OPERATOR(random__Uniform, UniformDistribution<CPUBackend>, CPU);
DALI_SCHEMA(Uniform)
.DocStr(R"code(Generates random numbers following a uniform distribution.

It can be configured to produce a continuous uniform distribution in the ``range`` [min, max),
or a discrete uniform distribution where any of the specified ``values`` [v0, v1, ..., vn] occur
with equal probability.

The shape of the generated data can be either specified explicitly with a ``shape`` argument,
or chosen to match the shape of the input, if provided. If none are present, a scalar is
generated.
Expand All @@ -54,10 +67,15 @@ generated.
.AddOptionalArg("range",
R"code(Range ``[min, max)`` of a continuous uniform distribution.

This argument is mutually exclusive with ``values``.)code",
This argument is mutually exclusive with ``values``.

.. warning::
When specifying an integer type as ``dtype``, the generated numbers can go outside
the specified range, due to rounding.
)code",
std::vector<float>{-1.0f, 1.0f}, true)
.AddOptionalArg<std::vector<float>>("values",
R"code(The discrete values produced by a discrete uniform distribution.
R"code(The discrete values [v0, v1, ..., vn] produced by a discrete uniform distribution.

This argument is mutually exclusive with ``range``.)code",
nullptr, true)
Expand Down