26 #ifndef MSHADOW_RANDOM_H_ 27 #define MSHADOW_RANDOM_H_ 41 #define rand_r(x) rand() 51 template<
typename Device,
typename DType MSHADOW_DEFAULT_DTYPE>
55 template<
typename DType>
72 inline void Seed(
int seed) {
74 rnd_engine_.seed(seed);
76 this->rseed_ =
static_cast<unsigned>(seed);
99 inline unsigned GetRandInt() {
100 return rnd_engine_();
107 std::generate_n(dst.
dptr_, dst.
size(0), [&](){
return rnd_engine_(); });
116 template<
int dim,
class Sampler>
119 std::generate_n(dst->
dptr_, dst->
shape_.Size(), sampler);
123 std::generate_n(mat[i].dptr_, mat.
size(1), sampler);
135 template<
int dim,
typename PType>
137 PType a = 0.0f , PType b = 1.0f ) {
139 typedef typename std::conditional<std::is_floating_point<DType>::value,
140 DType,
double>::type FType;
141 typedef typename std::conditional<std::is_integral<DType>::value,
142 std::uniform_int_distribution<DType>,
143 std::uniform_real_distribution<FType>>::type GType;
144 GType dist_uniform(a, b);
145 SampleDistribution(dst, [&](){
return dist_uniform(rnd_engine_);});
155 template<
int dim,
typename PType>
157 PType mu = 0.0f, PType sigma = 1.0f ) {
161 typedef typename std::conditional<std::is_floating_point<DType>::value,
162 DType,
double>::type GType;
163 std::normal_distribution<GType> dist_normal(mu, sigma);
164 SampleDistribution(dst, [&](){
return dist_normal(rnd_engine_);});
174 template<
int dim,
typename PType>
176 PType alpha, PType beta) {
177 typedef typename std::conditional<std::is_floating_point<DType>::value,
178 DType,
double>::type GType;
179 std::gamma_distribution<GType> dist_gamma(alpha, beta);
180 SampleDistribution(dst, [&](){
return dist_gamma(rnd_engine_);});
189 template<
int dim,
typename PType>
191 typedef typename std::conditional<std::is_floating_point<DType>::value,
192 DType,
double>::type GType;
193 std::exponential_distribution<GType> dist_exp(lambda);
194 SampleDistribution(dst, [&](){
return dist_exp(rnd_engine_);});
203 template<
int dim,
typename PType>
205 typedef typename std::conditional<std::is_integral<DType>::value, DType,
int>::type GType;
206 std::poisson_distribution<GType> dist_poisson(lambda);
207 SampleDistribution(dst, [&](){
return static_cast<DType
>(dist_poisson(rnd_engine_));});
217 template<
int dim,
typename PType1,
typename PType2>
219 typedef typename std::conditional<std::is_integral<DType>::value, DType,
int>::type GType;
220 std::negative_binomial_distribution<GType> dist_negbinomial(k, p);
221 SampleDistribution(dst, [&](){
return static_cast<DType
>(dist_negbinomial(rnd_engine_));});
232 template<
int dim,
typename PType>
234 PType mu, PType alpha) {
235 if (alpha == PType(0)) {
238 PType r(PType(1) / alpha);
239 PType beta = mu * alpha;
240 std::gamma_distribution<> dist_gamma(r, beta);
241 typedef typename std::conditional<std::is_integral<DType>::value, DType,
int>::type GType;
242 SampleDistribution(dst,
243 [&](){ std::poisson_distribution<GType> dist_poisson(dist_gamma(rnd_engine_));
244 return static_cast<DType
>(dist_poisson(rnd_engine_));});
279 inline expr::ReshapeExp<Tensor<cpu, 1, DType>, DType, dim, 1>
293 std::mt19937 rnd_engine_;
304 DType a = 0.0f, DType b = 1.0f) {
306 this->GenUniform(dst->
dptr_, dst->
shape_.Size(), a, b);
310 this->GenUniform(mat[i].dptr_, mat.
size(1), a, b);
316 DType mu = 0.0f, DType sigma = 1.0f) {
321 this->GenGaussian(dst->
dptr_, dst->
shape_.Size(), mu, sigma);
325 this->GenGaussian(mat[i].dptr_, mat.
size(1), mu, sigma);
329 inline void GenUniform(
float *dptr,
index_t size,
float a,
float b) {
330 for (
index_t j = 0; j < size; ++j) {
331 dptr[j] =
static_cast<float>(RandNext()) * (b - a) + a;
334 inline void GenUniform(
double *dptr,
index_t size,
double a,
double b) {
335 for (
index_t j = 0; j < size; ++j) {
336 dptr[j] =
static_cast<double>(RandNext()) * (b - a) + a;
339 inline void GenGaussian(
float *dptr,
index_t size,
float mu,
float sigma) {
340 this->GenGaussianX(dptr, size, mu, sigma);
342 inline void GenGaussian(
double *dptr,
index_t size,
double mu,
double sigma) {
343 this->GenGaussianX(dptr, size, mu, sigma);
345 inline void GenGaussianX(DType *dptr,
index_t size, DType mu, DType sigma) {
346 DType g1 = 0.0f, g2 = 0.0f;
347 for (
index_t j = 0; j < size; ++j) {
349 this->SampleNormal2D(&g1, &g2);
350 dptr[j] = mu + g1 * sigma;
352 dptr[j] = mu + g2 * sigma;
357 inline DType RandNext(
void) {
358 return static_cast<DType
>(rand_r(&rseed_)) /
359 (static_cast<DType>(RAND_MAX) + 1.0f);
362 inline DType RandNext2(
void) {
363 return (static_cast<DType>(rand_r(&rseed_)) + 1.0f) /
364 (
static_cast<DType
>(RAND_MAX) + 2.0f);
371 inline void SampleNormal2D(DType *xx_, DType *yy_) {
372 DType &xx = *xx_, &yy = *yy_;
375 x = 2.0f * RandNext2() - 1.0f;
376 y = 2.0f * RandNext2() - 1.0f;
378 }
while (s >= 1.0f || s == 0.0f);
379 DType t = std::sqrt(-2.0f * std::log(s) / s);
380 xx = x * t; yy = y * t;
390 template<
typename DType>
409 curandStatus_t status;
412 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"set_stream CURAND failed";
420 if (gen_ == NULL || (curandSetGeneratorOffset(gen_, 0ULL) != CURAND_STATUS_SUCCESS))
423 curandStatus_t status;
424 status = curandSetPseudoRandomGeneratorSeed(gen_, static_cast<uint64_t>(seed));
425 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"Set CURAND seed failed.";
431 curandStatus_t status = curandGenerate(gen_, dst.
dptr_, dst.
size(0));
432 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"CURAND Gen rand ints failed.";
443 DType a = 0.0f, DType b = 1.0f);
454 DType mu = 0.0f, DType sigma = 1.0f);
470 gaussian(
Shape<dim> shape, DType mu = 0.0f, DType sigma = 1.0f);
483 inline expr::ReshapeExp<Tensor<gpu, 1, DType>, DType, dim, 1>
487 inline void GenGaussian(
float *dptr,
size_t size,
float mu,
float sigma) {
488 curandStatus_t status;
489 status = curandGenerateNormal(gen_, dptr, size, mu, sigma);
490 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"CURAND Gen Normal float failed." 491 <<
" size = " << size
493 <<
",sigma = " << sigma;
495 inline void GenGaussian(
double *dptr,
size_t size,
double mu,
double sigma) {
496 curandStatus_t status;
497 status = curandGenerateNormalDouble(gen_, dptr, size, mu, sigma);
498 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"CURAND Gen Normal double failed." 499 <<
" size = " << size
501 <<
",sigma = " << sigma;
503 inline void GenUniform(
float *dptr,
size_t size) {
504 curandStatus_t status;
505 status = curandGenerateUniform(gen_, dptr, size);
506 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"CURAND Gen Uniform float failed." 507 <<
" size = " << size;
509 inline void GenUniform(
double *dptr,
size_t size) {
510 curandStatus_t status;
511 status = curandGenerateUniformDouble(gen_, dptr, size);
512 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"CURAND Gen Uniform double failed." 513 <<
" size = " << size;
515 inline void CreateGenerator() {
518 curandStatus_t status;
519 status = curandCreateGenerator(&gen_, CURAND_RNG_PSEUDO_DEFAULT);
520 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"Cannot create CURAND Generator";
522 inline void DeleteGenerator() {
524 curandStatus_t status;
525 status = curandDestroyGenerator(gen_);
526 CHECK_EQ(status, CURAND_STATUS_SUCCESS) <<
"Destory CURAND Gen failed";
531 curandGenerator_t gen_;
535 #endif // MSHADOW_USE_CUDA 539 template<
typename DType>
543 if (a == 0.0f && b == 1.0f) {
547 *dst = this->uniform(dst->
shape_);
550 *dst = this->uniform(dst->
shape_) * (b - a) + a;
553 template<
typename DType>
560 this->GenGaussian(dst->
dptr_, dst->
shape_.Size(), mu, sigma);
562 *dst = this->gaussian(dst->
shape_, mu, sigma);
566 template<
typename DType>
570 size_t aligned_sz = ((shape.
Size() + 1UL) >> 1) << 1;
572 buffer_.Resize(
Shape1(aligned_sz));
574 this->GenGaussian(buffer_.dptr_, aligned_sz, mu, sigma);
578 template<
typename DType>
580 inline expr::ReshapeExp<Tensor<gpu, 1, DType>, DType, dim, 1>
583 this->GenUniform(buffer_.dptr_, buffer_.size(0));
588 #endif // MSHADOW_RANDOM_H_ expr::ReshapeExp< Tensor< cpu, 1, DType >, DType, dim, 1 > gaussian(Shape< dim > shape)
return a temporal expression storing standard gaussian random variables the temporal tensor is only v...
Definition: random.h:262
random number generator
Definition: random.h:52
void Seed(int seed)
seed random number generator using this seed
Definition: random.h:418
DType * dptr_
pointer to the data
Definition: tensor.h:434
unsigned GetSeed() const
get random seed used in random generator
Definition: random.h:82
void Seed(int seed)
seed random number generator using this seed
Definition: random.h:72
Definition: stream_gpu-inl.h:37
~Random(void) MSHADOW_THROW_EXCEPTION
Definition: random.h:401
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:436
~Random(void)
Definition: random.h:66
expr::ReshapeExp< Tensor< cpu, 1, DType >, DType, dim, 1 > uniform(Shape< dim > shape)
return a temporal expression storing standard uniform [0,1) the temporal tensor is only valid before ...
Definition: random.h:280
header file of tensor data structure and functions This lib requires explicit memory allocation and d...
device name CPU
Definition: tensor.h:39
device name GPU
Definition: tensor.h:46
const unsigned kRandBufferSize
buffer size for each random number generator
Definition: base.h:336
void SampleExponential(real_t lambda, NDArray *out)
Sample exponential distribution for each elements of out.
int32_t index_t
type that will be used for index
Definition: base.h:343
ReshapeExp< SrcExp, DType, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, DType, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: reshape.h:66
void SampleGaussian(real_t mu, real_t sigma, NDArray *out)
Sample gaussian distribution for each elements of out.
MSHADOW_XINLINE Tensor< Device, 2, DType > FlatTo2D(void) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor.h:519
tensor container that does memory allocation and resize like STL, use it to save the lines of FreeSpa...
Definition: tensor_container.h:40
MSHADOW_XINLINE index_t size(int idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor.h:505
MSHADOW_XINLINE bool CheckContiguous(void) const
Definition: tensor.h:491
MSHADOW_XINLINE Shape< 1 > Shape1(index_t s0)
construct a one dimension shape, stride will equal s0
Definition: tensor.h:206
void GetRandInt(const Tensor< gpu, 1, unsigned > &dst)
get a set of random integers
Definition: random.h:430
void SampleUniform(real_t begin, real_t end, NDArray *out)
Sample uniform distribution for each elements of out.
reshape the content to another shape input: Tensor<Device,dimsrc>: ishape output: Tensor<Device...
Definition: reshape.h:39
std::mt19937 & GetRndEngine()
Definition: random.h:286
Random(int seed)
constructor of random engine
Definition: random.h:397
tensor container that does memory allocation and resize like STL
void set_stream(Stream< cpu > *stream)
set the stream of computation
Definition: random.h:89
Random(int seed)
constructor of random engine
Definition: random.h:62
overloaded + operator between half_t and bf16_t
Definition: base.h:334
#define MSHADOW_THROW_EXCEPTION
Definition: base.h:260
general tensor
Definition: tensor.h:420
void SamplePoisson(real_t lambda, NDArray *out)
Sample Poisson distribution for each elements of out.
void set_stream(Stream< gpu > *stream)
set the stream of computation
Definition: random.h:408
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:144
void SampleGamma(real_t alpha, real_t beta, NDArray *out)
Sample gamma distribution for each elements of out.
computaion stream structure, used for asynchronous computations
Definition: tensor.h:383