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

Fix access to unitialized memory in RNG ops #5394

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions dali/operators/random/choice.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct ChoiceSampleDist {
using DistType =
std::conditional_t<uniform, std::uniform_int_distribution<>, std::discrete_distribution<>>;

DALI_HOST_DEV explicit ChoiceSampleDist() {}
DALI_HOST_DEV ChoiceSampleDist() = default;

template <bool uniform_ = uniform, typename = std::enable_if_t<!uniform_>>
DALI_HOST_DEV ChoiceSampleDist(const T *elements, const float *p_first, const float *p_last)
Expand Down Expand Up @@ -76,15 +76,14 @@ struct ChoiceSampleDist<T, uniform, false> {
using DistType =
std::conditional_t<uniform, std::uniform_int_distribution<T>, std::discrete_distribution<T>>;

DALI_HOST_DEV explicit ChoiceSampleDist() {}
DALI_HOST_DEV ChoiceSampleDist() = default;

template <bool uniform_ = uniform, typename = std::enable_if_t<!uniform_>>
DALI_HOST_DEV ChoiceSampleDist(const float *p_first, const float *p_last)
: dist_{p_first, p_last} {
static_assert(!uniform_, "This is non-uniform variant");
}


template <bool uniform_ = uniform, typename = std::enable_if_t<uniform_>>
DALI_HOST_DEV ChoiceSampleDist(int64_t element_count) : dist_(0, element_count - 1) {
static_assert(uniform_, "This is uniform variant");
Expand Down
8 changes: 2 additions & 6 deletions dali/operators/random/rng_base_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ namespace rng {
template<>
struct OperatorWithRngFields<CPUBackend> {
OperatorWithRngFields(int64_t seed, int nsamples) {}

std::vector<uint8_t> dists_cpu_;
};

template <bool IsNoiseGen>
Expand Down Expand Up @@ -125,10 +123,8 @@ void RNGBase<Backend, Impl, IsNoiseGen>::RunImplTyped(Workspace &ws, CPUBackend)

// TODO(janton): set layout explicitly from the user for RNG

auto &dists_cpu = backend_data_.dists_cpu_;
dists_cpu.resize(sizeof(Dist) * nsamples); // memory was already reserved in the constructor
Dist* dists = reinterpret_cast<Dist*>(dists_cpu.data());
bool use_default_dist = !This().template SetupDists<T>(dists, ws, nsamples);
std::vector<Dist> dists(nsamples);
JanuszL marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

@mzient mzient Mar 26, 2024

Choose a reason for hiding this comment

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

This is captured by value in task lambdas (see line 162 and 180).

bool use_default_dist = !This().template SetupDists<T>(dists.data(), ws, nsamples);

int channel_dim = -1;
auto layout = output.GetLayout();
Expand Down
Loading