Skip to content
10 changes: 5 additions & 5 deletions cpp/include/raft/linalg/detail/reduce_rows_by_key.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ __launch_bounds__(SUM_ROWS_SMALL_K_DIMX, 4)
block_offset_irow < nrows; // we will syncthreads() inside the loop, no CTA divergence
block_offset_irow += blockDim.x * gridDim.x) {
IdxT irow = block_offset_irow + threadIdx.x;
DataType val = (irow < nrows) ? d_A[irow * lda + idim] : 0.0;
DataType val = (irow < nrows) ? d_A[irow * lda + idim] : DataType(0);
if (d_weights && irow < nrows) { val = val * d_weights[irow]; }
// we are not reusing the keys - after profiling
// d_keys is mainly loaded from L2, and this kernel is DRAM BW bounded
// (experimentation gave a 10% speed up - not worth the many code lines added)
IdxT row_key = (irow < nrows) ? d_keys[irow] : std::numeric_limits<IdxT>::max();

thread_sums.x += (row_key == 0) ? static_cast<SumsT>(val) : 0.0;
thread_sums.y += (row_key == 1) ? static_cast<SumsT>(val) : 0.0;
thread_sums.z += (row_key == 2) ? static_cast<SumsT>(val) : 0.0;
thread_sums.w += (row_key == 3) ? static_cast<SumsT>(val) : 0.0;
thread_sums.x += (row_key == 0) ? static_cast<SumsT>(val) : SumsT(0);
thread_sums.y += (row_key == 1) ? static_cast<SumsT>(val) : SumsT(0);
thread_sums.z += (row_key == 2) ? static_cast<SumsT>(val) : SumsT(0);
thread_sums.w += (row_key == 3) ? static_cast<SumsT>(val) : SumsT(0);
}

// End of column
Expand Down
4 changes: 2 additions & 2 deletions cpp/include/raft/random/detail/multi_variable_gaussian.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ RAFT_KERNEL combined_dot_product(int rows, int cols, const T* W, T* matrix, int*
int m_i = threadIdx.x + blockDim.x * blockIdx.x;
int Wi = m_i / cols;
if (m_i < cols * rows) {
if (W[Wi] >= 0.0)
matrix[m_i] = pow(W[Wi], 0.5) * (matrix[m_i]);
if (W[Wi] >= T(0))
matrix[m_i] = raft::sqrt(W[Wi]) * (matrix[m_i]);
else
check[0] = Wi; // reports Wi'th eigen values is negative.
}
Expand Down
21 changes: 14 additions & 7 deletions cpp/include/raft/random/detail/rng_device.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,24 @@ HDI void custom_next(GenType& gen,
LenType idx = 0,
LenType stride = 0)
{
double res1, res2;
// Draw a zero-mean deviate in the narrowest type that can carry it, then shift by mu in the
// output type. Folding mu into the transform would round mu itself and, worse, quantize the
// deviate away entirely once |mu| exceeds the mantissa of the compute type (2^24 for float,
// 2^53 for double): at mu = 2e9 the float spacing is 128, so a sigma of 10 vanishes.
// With mu applied separately, float carries any deviate an integer output can represent, and
// double is only needed for the wider deviates of 64-bit outputs. Double is 1/64 rate on
// consumer GPUs, so this matters.
using compute_t = std::conditional_t<(sizeof(IntType) > 4), double, float>;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
compute_t res1, res2;
do {
gen.next(res1);
} while (res1 == double(0.0));
} while (res1 == compute_t(0.0));

gen.next(res2);
double mu = static_cast<double>(params.mu);
double sigma = static_cast<double>(params.sigma);
box_muller_transform<double>(res1, res2, sigma, mu);
*val = static_cast<IntType>(res1);
*(val + 1) = static_cast<IntType>(res2);
compute_t sigma = static_cast<compute_t>(params.sigma);
box_muller_transform<compute_t>(res1, res2, sigma, compute_t(0));
*val = static_cast<IntType>(res1) + params.mu;
*(val + 1) = static_cast<IntType>(res2) + params.mu;
}

template <typename GenType, typename OutType, typename LenType>
Expand Down
62 changes: 62 additions & 0 deletions cpp/tests/random/rng_int.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <gtest/gtest.h>

#include <vector>

namespace raft {
namespace random {

Expand Down Expand Up @@ -287,5 +289,65 @@ TEST_P(RngMdspanTestS64, Result)
}
INSTANTIATE_TEST_SUITE_P(RngMdspanTests, RngMdspanTestS64, ::testing::ValuesIn(inputs_s64));

/**
* normalInt draws the deviate in a narrower type than the output when it can, so the shift by mu
* has to be applied in the output type. If mu is folded into the Box-Muller transform instead,
* everything below the mantissa of the compute type is lost: at mu = 2e9 the float spacing is 128,
* which quantizes a sigma of 10 away completely and collapses the sample to a single value.
*
* The deviate does not depend on mu, so drawing with mu and with 0 from the same seed must give
* the same deviates. That is exact, so it needs no statistical tolerance.
*/
template <typename T>
void testNormalIntLargeMu(T mu, T sigma, GeneratorType gtype)
{
raft::resources handle;
auto stream = resource::get_cuda_stream(handle);
constexpr int len = 32 * 1024;

rmm::device_uvector<T> shifted(len, stream);
rmm::device_uvector<T> centered(len, stream);

RngState r_shifted(1234ULL, gtype);
normalInt(handle, r_shifted, shifted.data(), len, mu, sigma);
RngState r_centered(1234ULL, gtype);
normalInt(handle, r_centered, centered.data(), len, T(0), sigma);

std::vector<T> h_shifted(len);
std::vector<T> h_centered(len);
update_host(h_shifted.data(), shifted.data(), len, stream);
update_host(h_centered.data(), centered.data(), len, stream);
resource::sync_stream(handle, stream);

bool all_zero = true;
for (int i = 0; i < len; ++i) {
// Subtract in the integer type: converting values of this magnitude to a floating point type
// would lose the very precision being tested for.
ASSERT_EQ(static_cast<T>(h_shifted[i] - mu), h_centered[i])
<< "deviate " << i << " differs once shifted by mu=" << mu;
all_zero = all_zero && (h_centered[i] == T(0));
}
ASSERT_FALSE(all_zero) << "every deviate was zero for sigma=" << sigma;
}

TEST(RngNormalIntLargeMu, S32)
{
for (auto gtype : {GenPhilox, GenPC}) {
testNormalIntLargeMu<int32_t>(16777217, 10, gtype); // 2^24 + 1, past float's mantissa
testNormalIntLargeMu<int32_t>(100000000, 10, gtype);
testNormalIntLargeMu<int32_t>(2000000000, 10, gtype); // near the int32_t limit
testNormalIntLargeMu<int32_t>(-2000000000, 10, gtype);
}
}

TEST(RngNormalIntLargeMu, S64)
{
for (auto gtype : {GenPhilox, GenPC}) {
// 2^53 + 1, past double's mantissa
testNormalIntLargeMu<int64_t>(9007199254740993LL, 10, gtype);
testNormalIntLargeMu<int64_t>(4000000000000000000LL, 10, gtype);
}
}

} // namespace random
} // namespace raft
Loading