|
1 | 1 | #include <vector> |
| 2 | +#include <random> |
2 | 3 | #include <ctime> |
| 4 | +#include <chrono> |
| 5 | +#include <atomic> |
| 6 | +#include <numeric> |
3 | 7 | #include "../Tensor.h" |
4 | 8 | #include "../../Util/utils.h" |
5 | 9 |
|
@@ -48,3 +52,72 @@ GradTensor* GradTensor::eye(size_t n, size_t bidx, size_t pidx) { |
48 | 52 | return new GradTensor(storage, shape, bidx, pidx); |
49 | 53 | } |
50 | 54 |
|
| 55 | +GradTensor* GradTensor::gaussian(std::vector<size_t> shape, double mean, double stddev, size_t bidx, size_t pidx) { |
| 56 | + // Create a unique seed by combining high-resolution time and a counter |
| 57 | + static std::atomic<unsigned long long> seed_counter{0}; |
| 58 | + |
| 59 | + auto now = std::chrono::high_resolution_clock::now(); |
| 60 | + auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count(); |
| 61 | + unsigned long long unique_seed = nanos ^ (seed_counter.fetch_add(1, std::memory_order_relaxed) << 32); |
| 62 | + |
| 63 | + // Create a generator with the unique seed |
| 64 | + std::mt19937 generator(unique_seed); |
| 65 | + |
| 66 | + // Create a distribution |
| 67 | + std::normal_distribution<double> distribution(mean, stddev); |
| 68 | + |
| 69 | + // Calculate total number of elements |
| 70 | + int length = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>()); |
| 71 | + |
| 72 | + // Create and fill the vector |
| 73 | + std::vector<double> result(length); |
| 74 | + for (int i = 0; i < length; ++i) { |
| 75 | + result[i] = distribution(generator); |
| 76 | + } |
| 77 | + |
| 78 | + return new GradTensor(result, shape, bidx, pidx); |
| 79 | +} |
| 80 | + |
| 81 | +GradTensor* GradTensor::gaussian_like(GradTensor* input, double mean, double stddev) { |
| 82 | + return GradTensor::gaussian(input->shape(), mean, stddev, input->bidx, input->pidx()); |
| 83 | +} |
| 84 | + |
| 85 | +GradTensor* GradTensor::uniform(std::vector<size_t> shape, double min, double max, size_t bidx, size_t pidx) { |
| 86 | + // (Use the same unique seeding method as in the gaussian function) |
| 87 | + static std::atomic<unsigned long long> seed_counter{0}; |
| 88 | + |
| 89 | + auto now = std::chrono::high_resolution_clock::now(); |
| 90 | + auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count(); |
| 91 | + unsigned long long unique_seed = nanos ^ (seed_counter.fetch_add(1, std::memory_order_relaxed) << 32); |
| 92 | + |
| 93 | + std::mt19937 generator(unique_seed); |
| 94 | + std::uniform_real_distribution<double> distribution(min, max); |
| 95 | + |
| 96 | + int length = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>()); |
| 97 | + std::vector<double> result(length); |
| 98 | + for (int i = 0; i < length; ++i) { |
| 99 | + result[i] = distribution(generator); |
| 100 | + } |
| 101 | + |
| 102 | + return new GradTensor(result, shape, bidx, pidx); |
| 103 | +} |
| 104 | + |
| 105 | +GradTensor* GradTensor::uniform_like(GradTensor* input, double min, double max) { |
| 106 | + return GradTensor::uniform(input->shape(), min, max, input->bidx, input->pidx()); |
| 107 | +} |
| 108 | + |
| 109 | +GradTensor* GradTensor::ones(std::vector<size_t> shape, size_t bidx, size_t pidx) { |
| 110 | + return new GradTensor(std::vector<double> (CIntegrity::prod(shape), 1.0), shape, bidx, pidx); |
| 111 | +} |
| 112 | + |
| 113 | +GradTensor* GradTensor::ones_like(GradTensor* input) { |
| 114 | + return GradTensor::ones(input->shape(), input->bidx, input->pidx()); |
| 115 | +} |
| 116 | + |
| 117 | +GradTensor* GradTensor::zeros(std::vector<size_t> shape, size_t bidx, size_t pidx) { |
| 118 | + return new GradTensor(std::vector<double> (CIntegrity::prod(shape), 0.0), shape, bidx, pidx); |
| 119 | +} |
| 120 | + |
| 121 | +GradTensor* GradTensor::zeros_like(GradTensor* input) { |
| 122 | + return GradTensor::zeros(input->shape(), input->bidx, input->pidx()); |
| 123 | +} |
0 commit comments