1 /**
2 * Copyright 2022 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cstdint>
18 #include <random>
19 #include "include/common/random.h"
20 #include "include/common/pybind_api/api_register.h"
21 #include "utils/log_adapter.h"
22
23 namespace mindspore::initializer {
24 //
25 // Generate float random numbers into a python buffer.
26 //
27 template <typename Generator, typename Distribution, typename... Args>
GenerateFloatRandoms(std::uint64_t seed,const py::buffer & py_buf,Args...args)28 void GenerateFloatRandoms(std::uint64_t seed, const py::buffer &py_buf, Args... args) {
29 // Check buffer info.
30 py::buffer_info info = py_buf.request();
31 if (info.format != py::format_descriptor<float>::format()) {
32 MS_LOG(EXCEPTION) << "Unsupported data type '" << info.format << "'.";
33 }
34 // Get buffer pointer and size.
35 if (info.size < 0) {
36 MS_LOG(EXCEPTION) << "Negative buffer size: " << info.size << ".";
37 }
38 const size_t buf_size = static_cast<size_t>(info.size);
39 float *buf = reinterpret_cast<float *>(info.ptr);
40 MS_EXCEPTION_IF_NULL(buf);
41
42 // Parallel generate randoms into buffer.
43 random::GenerateRandomsParallel<float, Generator, Distribution>(seed, buf, buf_size, args...);
44 }
45
RandomUniform(std::uint64_t seed,const py::buffer & py_buf,float a,float b)46 void RandomUniform(std::uint64_t seed, const py::buffer &py_buf, float a, float b) {
47 using Generator = random::Philox;
48 using Distribution = random::UniformDistribution<double>;
49 GenerateFloatRandoms<Generator, Distribution>(seed, py_buf, a, b);
50 }
51
RandomNormal(std::uint64_t seed,const py::buffer & py_buf,float mean,float sigma)52 void RandomNormal(std::uint64_t seed, const py::buffer &py_buf, float mean, float sigma) {
53 using Generator = random::Philox;
54 using Distribution = random::NormalDistribution<double>;
55 GenerateFloatRandoms<Generator, Distribution>(seed, py_buf, mean, sigma);
56 }
57
TruncatedNormal(std::uint64_t seed,const py::buffer & py_buf,float a,float b,float mean,float sigma)58 void TruncatedNormal(std::uint64_t seed, const py::buffer &py_buf, float a, float b, float mean, float sigma) {
59 using Generator = random::Philox;
60 using Distribution = random::TruncatedNormal<double>;
61 GenerateFloatRandoms<Generator, Distribution>(seed, py_buf, a, b, mean, sigma);
62 }
63
RegRandomNormal(py::module * m)64 void RegRandomNormal(py::module *m) {
65 (void)m->def("_random_uniform", RandomUniform);
66 (void)m->def("_random_normal", RandomNormal);
67 (void)m->def("_truncated_normal", TruncatedNormal);
68 }
69 } // namespace mindspore::initializer
70