philox.cuh 5.33 KB
Newer Older
1
// Adapted from https://github.com/NVIDIA/apex/blob/master/apex/contrib/csrc/multihead_attn/philox.cuh
Tri Dao's avatar
Tri Dao committed
2
3
4
5
6
7
8
9
10
11
12
// Pytorch also has an implementation of Philox RNG: https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/codegen/cuda/runtime/random_numbers.cu
#pragma once
// Philox CUDA.

namespace {

class Philox {
public:
  __device__ inline Philox(unsigned long long seed,
                           unsigned long long subsequence,
                           unsigned long long offset)
13
    : key(reinterpret_cast<const uint2&>(seed)) {
Tri Dao's avatar
Tri Dao committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    //key.x = (unsigned int)seed;
    //key.y = (unsigned int)(seed >> 32);
    //counter = make_uint4(0, 0, 0, 0);
    //counter.z = (unsigned int)(subsequence);
    //counter.w = (unsigned int)(subsequence >> 32);
    //STATE = 0;
    //incr_n(offset / 4);

    ull2 * tmp = reinterpret_cast<ull2*>(&counter);
    tmp->x = offset / 4;
    tmp->y = subsequence;
    // if ((threadIdx.x == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("Philox counter: %d, %d, %d, %d\n", counter.x, counter.y, counter.z, counter.w);
    // }
  }
29

Tri Dao's avatar
Tri Dao committed
30
  __device__ inline uint4 operator()() {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    uint4 counter_ = counter;
    uint2 key_ = key;
    // 7-round philox
    #pragma unroll
    for (int i = 0; i < 6; i++) {
      counter_ = single_round(counter_, key_);
      key_.x += (kPhilox10A);
      key_.y += (kPhilox10B);
    }
    uint4 output = single_round(counter_, key_);
    // if ((threadIdx.x == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("Philox counter: %u, %u, %u, %u\n", counter.x, counter.y, counter.z, counter.w);
    //     printf("Philox output: %u, %u, %u, %u\n", output.x, output.y, output.z, output.w);
    // }
    incr();
    return output;
  }

  __device__ inline uint4 operator()(const unsigned long long subsequence) {
    uint4 counter_ = counter;
    ull2 * tmp = reinterpret_cast<ull2*>(&counter_);
    tmp->y = subsequence;
    // if ((threadIdx.x % 32 == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("tidx = %d, counter_: %u, %u, %u, %u\n", threadIdx.x, counter_.x, counter_.y, counter_.z, counter_.w);
    // }
    uint2 key_ = key;
    // 7-round philox
    #pragma unroll
    for (int i = 0; i < 6; i++) {
      counter_ = single_round(counter_, key_);
      key_.x += (kPhilox10A);
      key_.y += (kPhilox10B);
    }
    uint4 output = single_round(counter_, key_);
    // if ((threadIdx.x == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("Philox counter: %u, %u, %u, %u\n", counter.x, counter.y, counter.z, counter.w);
    //     printf("Philox output: %u, %u, %u, %u\n", output.x, output.y, output.z, output.w);
Tri Dao's avatar
Tri Dao committed
68
69
70
71
72
73
74
75
76
77
78
79
    // }
    return output;
  }

private:
  struct ull2 {
      uint64_t x;
      uint64_t y;
  };
  uint4 counter;
  const uint2 key;

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  // __device__ inline void incr_n(unsigned long long n) {
  //   unsigned int nlo = (unsigned int)(n);
  //   unsigned int nhi = (unsigned int)(n >> 32);
  //   counter.x += nlo;
  //   if (counter.x < nlo)
  //     nhi++;
  //   counter.y += nhi;
  //   if (nhi <= counter.y)
  //     return;
  //   if (++counter.z)
  //     return;
  //   ++counter.w;
  // }

  __device__ uint4 incr(uint4 ctr) {
Tri Dao's avatar
Tri Dao committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    uint4 res;
    asm ("add.cc.u32      %0, %4, %8;\n\t"
         "addc.cc.u32     %1, %5, %9;\n\t"
         "addc.cc.u32     %2, %6, %10;\n\t"
         "addc.u32        %3, %7, %11;\n\t"
         : "=r"(res.x), "=r"(res.y), "=r"(res.z), "=r"(res.w)
         : "r"(ctr.x), "r"(ctr.y), "r"(ctr.z), "r"(ctr.w),
           "n"(1), "n"(0), "n"(0), "n"(0));
    return res;
  }

  __device__ inline void incr() {
    // if ((threadIdx.x == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("Counter before: %u, %u, %u, %u\n", counter.x, counter.y, counter.z, counter.w);
    // }
110
    counter = incr(counter);
Tri Dao's avatar
Tri Dao committed
111
112
113
114
    // if ((threadIdx.x == 0) && (blockIdx.x == 0) && (blockIdx.y == 0)) {
    //     printf("Counter after: %u, %u, %u, %u\n", counter.x, counter.y, counter.z, counter.w);
    // }
  }
115
116
117
118
119
120
121
122

  // __device__ unsigned int mulhilo32(unsigned int a, unsigned int b,
  //                                   unsigned int *result_high) {
  //   *result_high = __umulhi(a, b);
  //   return a * b;
  // }

  __device__ uint2 mulhilo32(const unsigned int a, const unsigned int b) {
Tri Dao's avatar
Tri Dao committed
123
124
125
    uint2 *res;
    unsigned long long tmp;
    asm ("mul.wide.u32      %0, %1, %2;\n\t"
126
127
          : "=l"(tmp)
          : "r"(a), "r"(b));
Tri Dao's avatar
Tri Dao committed
128
129
130
    res = (uint2*)(&tmp);
    return *res;
  }
131

Tri Dao's avatar
Tri Dao committed
132
133
134
135
136
137
  __device__ inline uint4 single_round(const uint4 ctr, const uint2 key) {
    //unsigned int hi0;
    //unsigned int hi1;
    //unsigned int lo0 = mulhilo32(kPhiloxSA, ctr.x, &hi0);
    //unsigned int lo1 = mulhilo32(kPhiloxSB, ctr.z, &hi1);
    //uint4 ret = {hi1 ^ ctr.y ^ key.x, lo1, hi0 ^ ctr.w ^ key.y, lo0};
138
139
    uint2 res0 = mulhilo32(kPhiloxSA, ctr.x);
    uint2 res1 = mulhilo32(kPhiloxSB, ctr.z);
Tri Dao's avatar
Tri Dao committed
140
141
142
    uint4 ret = {res1.y ^ ctr.y ^ key.x, res1.x, res0.y ^ ctr.w ^ key.y, res0.x};  
    return ret;
  }
143

Tri Dao's avatar
Tri Dao committed
144
145
146
147
148
  static const unsigned long kPhilox10A = 0x9E3779B9;
  static const unsigned long kPhilox10B = 0xBB67AE85;
  static const unsigned long kPhiloxSA = 0xD2511F53;
  static const unsigned long kPhiloxSB = 0xCD9E8D57;
};
149

Tri Dao's avatar
Tri Dao committed
150
151
152
153
154
155
156
157
// Inverse of 2^32.
constexpr float M_RAN_INVM32 = 2.3283064e-10f;
__device__ __inline__ float4 uniform4(const uint4 x) {
  return make_float4(x.x * M_RAN_INVM32, x.y * M_RAN_INVM32, x.z * M_RAN_INVM32,
                     x.w * M_RAN_INVM32);
}

} // namespace