MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / UniformRawKernel

Function UniformRawKernel

paddle/phi/kernels/legacy/cpu/uniform_kernel.cc:21–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19
20template <typename T, typename Context>
21void UniformRawKernel(const Context &dev_ctx,
22 const IntArray &shape,
23 DataType dtype UNUSED,
24 const Scalar &min,
25 const Scalar &max,
26 int seed,
27 int diag_num,
28 int diag_step,
29 float diag_val,
30 DenseTensor *out) {
31 out->Resize(shape.GetData());
32 T *data = dev_ctx.template Alloc<T>(out);
33 auto size = out->numel();
34 std::shared_ptr<std::mt19937_64> engine;
35 if (seed) {
36 engine = std::make_shared<std::mt19937_64>();
37 engine->seed(seed);
38 } else {
39 engine = dev_ctx.GetGenerator()->GetCPUEngine();
40 }
41 UniformRealDistribution<T>(
42 data, size, min.to<float>(), max.to<float>(), engine);
43 if (diag_num > 0) {
44 PADDLE_ENFORCE_GT(
45 size,
46 (diag_num - 1) * (diag_step + 1),
47 common::errors::InvalidArgument(
48 "ShapeInvalid: the diagonal's elements is equal (num-1) "
49 "* (step-1) with num %d, step %d,"
50 "It should be smaller than %d, but received %d",
51 diag_num,
52 diag_step,
53 (diag_num - 1) * (diag_step + 1),
54 size));
55 for (int64_t i = 0; i < diag_num; ++i) {
56 int64_t pos = i * diag_step + i;
57 data[pos] = diag_val;
58 }
59 }
60}
61
62} // namespace phi
63

Callers

nothing calls this directly

Calls 7

GetCPUEngineMethod · 0.80
to<float>Method · 0.80
ResizeMethod · 0.45
GetDataMethod · 0.45
numelMethod · 0.45
seedMethod · 0.45
GetGeneratorMethod · 0.45

Tested by

no test coverage detected