ferrotensor 0.2.1__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/Cargo.lock +11 -11
  2. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/Cargo.toml +1 -1
  3. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/PKG-INFO +1 -1
  4. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/Cargo.toml +1 -1
  5. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/backend/compute.rs +11 -2
  6. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/backend/kernels.rs +145 -0
  7. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/functional.rs +5 -4
  8. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/lib.rs +3 -0
  9. ferrotensor-0.3.0/ferrotensor/src/random.rs +77 -0
  10. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/core.rs +4 -2
  11. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/cpu.rs +54 -0
  12. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/ops.rs +3 -27
  13. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/raw.rs +108 -0
  14. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/tests.rs +93 -0
  15. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/lib.rs +1 -0
  16. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/tensor.rs +6 -0
  17. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_tensor.py +14 -0
  18. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_train.py +3 -0
  19. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/__init__.py +2 -0
  20. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/_ferrotensor.pyi +6 -0
  21. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/README.md +0 -0
  22. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/README.md +0 -0
  23. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/README.md +0 -0
  24. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/char_transformer.rs +0 -0
  25. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/data/sonnets.txt +0 -0
  26. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/gpu_matmul_attention.rs +0 -0
  27. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/mlp_two_moons.rs +0 -0
  28. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/quickstart.rs +0 -0
  29. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/examples/similarity_search.rs +0 -0
  30. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/backend/mod.rs +0 -0
  31. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/data.rs +0 -0
  32. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/device.rs +0 -0
  33. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/activation.rs +0 -0
  34. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/attention.rs +0 -0
  35. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/container.rs +0 -0
  36. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/dropout.rs +0 -0
  37. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/embedding.rs +0 -0
  38. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/linear.rs +0 -0
  39. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/mod.rs +0 -0
  40. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/module.rs +0 -0
  41. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/normalization.rs +0 -0
  42. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/nn/tests.rs +0 -0
  43. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/adam.rs +0 -0
  44. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/mod.rs +0 -0
  45. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/optimizer.rs +0 -0
  46. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/scheduler.rs +0 -0
  47. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/sgd.rs +0 -0
  48. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/optim/tests.rs +0 -0
  49. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/serialize.rs +0 -0
  50. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/autograd.rs +0 -0
  51. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/mod.rs +0 -0
  52. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor/src/tensor/shape.rs +0 -0
  53. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/.gitignore +0 -0
  54. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/Cargo.toml +0 -0
  55. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/README.md +0 -0
  56. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/build.rs +0 -0
  57. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/examples/mlp.py +0 -0
  58. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/device.rs +0 -0
  59. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/error.rs +0 -0
  60. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/functional.rs +0 -0
  61. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/nn.rs +0 -0
  62. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/optim.rs +0 -0
  63. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/src/serialize.rs +0 -0
  64. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/conftest.py +0 -0
  65. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_autograd.py +0 -0
  66. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_errors.py +0 -0
  67. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_functional.py +0 -0
  68. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_gpu.py +0 -0
  69. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_nn.py +0 -0
  70. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_optim.py +0 -0
  71. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/ferrotensor-py/tests/test_serialize.py +0 -0
  72. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/pyproject.toml +0 -0
  73. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/nn/__init__.py +0 -0
  74. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/nn/functional.py +0 -0
  75. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/optim.py +0 -0
  76. {ferrotensor-0.2.1 → ferrotensor-0.3.0}/python/ferrotensor/py.typed +0 -0
@@ -606,7 +606,7 @@ dependencies = [
606
606
 
607
607
  [[package]]
608
608
  name = "ferrotensor"
609
- version = "0.2.1"
609
+ version = "0.3.0"
610
610
  dependencies = [
611
611
  "bytemuck",
612
612
  "naga",
@@ -619,7 +619,7 @@ dependencies = [
619
619
 
620
620
  [[package]]
621
621
  name = "ferrotensor-autograd"
622
- version = "0.2.1"
622
+ version = "0.3.0"
623
623
  dependencies = [
624
624
  "ferrotensor-backend",
625
625
  "pollster",
@@ -628,7 +628,7 @@ dependencies = [
628
628
 
629
629
  [[package]]
630
630
  name = "ferrotensor-backend"
631
- version = "0.2.1"
631
+ version = "0.3.0"
632
632
  dependencies = [
633
633
  "bytemuck",
634
634
  "env_logger",
@@ -641,7 +641,7 @@ dependencies = [
641
641
 
642
642
  [[package]]
643
643
  name = "ferrotensor-benchmarks"
644
- version = "0.2.1"
644
+ version = "0.3.0"
645
645
  dependencies = [
646
646
  "ferrotensor",
647
647
  "ferrotensor-backend",
@@ -652,7 +652,7 @@ dependencies = [
652
652
 
653
653
  [[package]]
654
654
  name = "ferrotensor-cli"
655
- version = "0.2.1"
655
+ version = "0.3.0"
656
656
  dependencies = [
657
657
  "clap",
658
658
  "env_logger",
@@ -670,7 +670,7 @@ dependencies = [
670
670
 
671
671
  [[package]]
672
672
  name = "ferrotensor-graph"
673
- version = "0.2.1"
673
+ version = "0.3.0"
674
674
  dependencies = [
675
675
  "bytemuck",
676
676
  "env_logger",
@@ -685,7 +685,7 @@ dependencies = [
685
685
 
686
686
  [[package]]
687
687
  name = "ferrotensor-hub"
688
- version = "0.2.1"
688
+ version = "0.3.0"
689
689
  dependencies = [
690
690
  "ferrotensor-autograd",
691
691
  "ferrotensor-backend",
@@ -698,7 +698,7 @@ dependencies = [
698
698
 
699
699
  [[package]]
700
700
  name = "ferrotensor-loader"
701
- version = "0.2.1"
701
+ version = "0.3.0"
702
702
  dependencies = [
703
703
  "bytemuck",
704
704
  "env_logger",
@@ -716,7 +716,7 @@ dependencies = [
716
716
 
717
717
  [[package]]
718
718
  name = "ferrotensor-monitor"
719
- version = "0.2.1"
719
+ version = "0.3.0"
720
720
  dependencies = [
721
721
  "anyhow",
722
722
  "chrono",
@@ -730,7 +730,7 @@ dependencies = [
730
730
 
731
731
  [[package]]
732
732
  name = "ferrotensor-py"
733
- version = "0.2.1"
733
+ version = "0.3.0"
734
734
  dependencies = [
735
735
  "ferrotensor",
736
736
  "pyo3",
@@ -739,7 +739,7 @@ dependencies = [
739
739
 
740
740
  [[package]]
741
741
  name = "ferrotensor-trainer"
742
- version = "0.2.1"
742
+ version = "0.3.0"
743
743
  dependencies = [
744
744
  "bytemuck",
745
745
  "ferrotensor-autograd",
@@ -4,7 +4,7 @@ resolver = "2"
4
4
 
5
5
  [workspace.package]
6
6
  edition = "2021"
7
- version = "0.2.1"
7
+ version = "0.3.0"
8
8
  license = "MIT OR Apache-2.0"
9
9
  repository = "https://github.com/puranikyashaswin/ferrotensor"
10
10
  authors = ["Yashaswin Puranik <puranikyashaswinsharma@gmail.com>"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ferrotensor
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: Implementation :: CPython
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "ferrotensor"
3
- version = "0.2.1"
3
+ version = "0.3.0"
4
4
  edition = "2021"
5
5
  authors.workspace = true
6
6
  license.workspace = true
@@ -9,10 +9,12 @@ use wgpu::{BindGroup, BindGroupLayout, BindingResource, CommandEncoder, ComputeP
9
9
  use super::KERNELS_WGSL;
10
10
 
11
11
  /// Entry points compiled from [`KERNELS_WGSL`].
12
- pub(crate) const KERNEL_NAMES: [&str; 7] = [
12
+ pub(crate) const KERNEL_NAMES: [&str; 9] = [
13
13
  "unary",
14
14
  "binary",
15
15
  "reduce",
16
+ "reduce_all",
17
+ "arg_reduce",
16
18
  "softmax",
17
19
  "matmul",
18
20
  "permute",
@@ -129,7 +131,14 @@ impl ComputeEngine {
129
131
  /// Spills into the y dimension past 65535 workgroups; the kernels rebuild
130
132
  /// the flat index as `x + y * nx * 256`.
131
133
  pub fn workgroups_1d(n: usize) -> (u32, u32, u32) {
132
- let groups = (n as u32).div_ceil(WORKGROUP_1D).max(1);
134
+ Self::workgroup_grid(n.div_ceil(WORKGROUP_1D as usize))
135
+ }
136
+
137
+ /// Grid of at least `groups` workgroups (at least one), spilling into the
138
+ /// y dimension past 65535; the kernels rebuild the workgroup index as
139
+ /// `x + y * nx`.
140
+ pub fn workgroup_grid(groups: usize) -> (u32, u32, u32) {
141
+ let groups = u32::try_from(groups.max(1)).expect("too many workgroups");
133
142
  if groups <= MAX_GROUPS_PER_DIM {
134
143
  (groups, 1, 1)
135
144
  } else {
@@ -243,6 +243,151 @@ fn reduce(@builtin(global_invocation_id) gid: vec3<u32>,
243
243
  red_out[i] = acc;
244
244
  }
245
245
 
246
+ // --- Full reductions (two stages) ---------------------------------------------
247
+ // Workgroup w adds elements w*256 + lid, stepping by the whole grid, then
248
+ // combines its 256 partials in a tree and writes red_part_out[w]. The first
249
+ // dispatch leaves one partial per workgroup, a one-workgroup dispatch over
250
+ // those partials gives the result.
251
+
252
+ struct AllParams {
253
+ op: u32,
254
+ n: u32,
255
+ _pad0: u32,
256
+ _pad1: u32,
257
+ };
258
+
259
+ @group(0) @binding(0) var<storage, read> all_in: array<f32>;
260
+ @group(0) @binding(1) var<storage, read_write> all_out: array<f32>;
261
+ @group(0) @binding(2) var<uniform> all_p: AllParams;
262
+
263
+ var<workgroup> all_part: array<f32, 256>;
264
+
265
+ fn reduce_combine(op: u32, a: f32, b: f32) -> f32 {
266
+ switch op {
267
+ case 1u: { return max(a, b); }
268
+ case 2u: { return min(a, b); }
269
+ default: { return a + b; }
270
+ }
271
+ }
272
+
273
+ @compute @workgroup_size(256)
274
+ fn reduce_all(@builtin(local_invocation_index) lid: u32,
275
+ @builtin(workgroup_id) wid: vec3<u32>,
276
+ @builtin(num_workgroups) nwg: vec3<u32>) {
277
+ let op = all_p.op;
278
+ // Identity: 0 for sum, -inf for max, +inf for min.
279
+ var acc = 0.0;
280
+ if (op == 1u) { acc = bitcast<f32>(0xff800000u); }
281
+ if (op == 2u) { acc = bitcast<f32>(0x7f800000u); }
282
+ let step = nwg.x * 256u;
283
+ for (var i = wid.x * 256u + lid; i < all_p.n; i = i + step) {
284
+ acc = reduce_combine(op, acc, all_in[i]);
285
+ }
286
+ all_part[lid] = acc;
287
+ workgroupBarrier();
288
+ for (var half = 128u; half > 0u; half = half >> 1u) {
289
+ if (lid < half) {
290
+ all_part[lid] = reduce_combine(op, all_part[lid], all_part[lid + half]);
291
+ }
292
+ workgroupBarrier();
293
+ }
294
+ if (lid == 0u) {
295
+ all_out[wid.x] = all_part[0];
296
+ }
297
+ }
298
+
299
+ // --- Argmax / argmin along an axis ------------------------------------------
300
+ // The input is viewed as [outer, dim, inner] and each of the outer * inner
301
+ // outputs is split into `splits` workgroups, each taking every
302
+ // (splits * 256)-th element of its share of dim. Values travel as u32 bits so
303
+ // nothing is flushed or canonicalised on the way.
304
+ // meta = [flags, outputs, dim, inner, splits]; flags bit 0: argmin,
305
+ // bit 1: the input is (value bits, index) pairs from an earlier pass (inner
306
+ // must be 1), bit 2: write (value bits, index) pairs for workgroup w at
307
+ // [2w, 2w + 1] instead of the f32 index.
308
+ // Ties go to the lower index; for argmax NaN beats every number, for argmin
309
+ // every number beats NaN, as in the CPU path.
310
+
311
+ @group(0) @binding(0) var<storage, read> arg_in: array<u32>;
312
+ @group(0) @binding(1) var<storage, read_write> arg_out: array<u32>;
313
+ @group(0) @binding(2) var<storage, read> arg_meta: array<u32>;
314
+
315
+ var<workgroup> arg_val: array<u32, 256>;
316
+ var<workgroup> arg_idx: array<u32, 256>;
317
+
318
+ const ARG_NONE: u32 = 0xffffffffu;
319
+
320
+ fn is_nan_bits(v: u32) -> bool {
321
+ return (v & 0x7fffffffu) > 0x7f800000u;
322
+ }
323
+
324
+ // Whether candidate (v, i) replaces the current best (bv, bi).
325
+ fn arg_better(smallest: bool, v: u32, i: u32, bv: u32, bi: u32) -> bool {
326
+ if (i == ARG_NONE) { return false; }
327
+ if (bi == ARG_NONE) { return true; }
328
+ let vn = is_nan_bits(v);
329
+ let bn = is_nan_bits(bv);
330
+ if (vn && bn) { return i < bi; }
331
+ if (vn || bn) { return vn != smallest; }
332
+ let a = bitcast<f32>(v);
333
+ let b = bitcast<f32>(bv);
334
+ if (a == b) { return i < bi; }
335
+ return select(a > b, a < b, smallest);
336
+ }
337
+
338
+ @compute @workgroup_size(256)
339
+ fn arg_reduce(@builtin(local_invocation_index) lid: u32,
340
+ @builtin(workgroup_id) wid: vec3<u32>,
341
+ @builtin(num_workgroups) nwg: vec3<u32>) {
342
+ let flags = arg_meta[0];
343
+ let dim = arg_meta[2];
344
+ let inner = arg_meta[3];
345
+ let splits = arg_meta[4];
346
+ let smallest = (flags & 1u) != 0u;
347
+ let pairs_in = (flags & 2u) != 0u;
348
+ let w = wid.x + wid.y * nwg.x;
349
+ let live = w < arg_meta[1] * splits;
350
+ let o_flat = w / splits;
351
+ let base = (o_flat / inner) * dim * inner + o_flat % inner;
352
+ var bv = 0u;
353
+ var bi = ARG_NONE;
354
+ if (live) {
355
+ for (var j = (w % splits) * 256u + lid; j < dim; j = j + splits * 256u) {
356
+ var v = 0u;
357
+ var i = j;
358
+ if (pairs_in) {
359
+ v = arg_in[2u * (o_flat * dim + j)];
360
+ i = arg_in[2u * (o_flat * dim + j) + 1u];
361
+ } else {
362
+ v = arg_in[base + j * inner];
363
+ }
364
+ if (arg_better(smallest, v, i, bv, bi)) {
365
+ bv = v;
366
+ bi = i;
367
+ }
368
+ }
369
+ }
370
+ arg_val[lid] = bv;
371
+ arg_idx[lid] = bi;
372
+ workgroupBarrier();
373
+ for (var half = 128u; half > 0u; half = half >> 1u) {
374
+ if (lid < half && arg_better(smallest, arg_val[lid + half], arg_idx[lid + half],
375
+ arg_val[lid], arg_idx[lid])) {
376
+ arg_val[lid] = arg_val[lid + half];
377
+ arg_idx[lid] = arg_idx[lid + half];
378
+ }
379
+ workgroupBarrier();
380
+ }
381
+ if (lid == 0u && live) {
382
+ if ((flags & 4u) != 0u) {
383
+ arg_out[2u * w] = arg_val[0];
384
+ arg_out[2u * w + 1u] = arg_idx[0];
385
+ } else {
386
+ arg_out[o_flat] = bitcast<u32>(f32(arg_idx[0]));
387
+ }
388
+ }
389
+ }
390
+
246
391
  // --- Softmax / log-softmax along an axis -------------------------------------
247
392
  // op 0 = softmax, op 1 = log_softmax.
248
393
 
@@ -106,11 +106,12 @@ pub fn dropout(x: &Tensor, p: f32, training: bool) -> Tensor {
106
106
  return x.clone();
107
107
  }
108
108
  use rand::Rng;
109
- let mut rng = rand::thread_rng();
110
109
  let scale = 1.0 / (1.0 - p);
111
- let mask: Vec<f32> = (0..x.numel())
112
- .map(|_| if rng.gen::<f32>() < p { 0.0 } else { scale })
113
- .collect();
110
+ let mask: Vec<f32> = crate::random::with_rng(|rng| {
111
+ (0..x.numel())
112
+ .map(|_| if rng.gen::<f32>() < p { 0.0 } else { scale })
113
+ .collect()
114
+ });
114
115
  x.mul(&Tensor::from_vec(mask, &x.shape, x.device()))
115
116
  }
116
117
 
@@ -51,18 +51,21 @@ pub mod data;
51
51
  pub mod functional;
52
52
  pub mod nn;
53
53
  pub mod optim;
54
+ pub mod random;
54
55
  pub mod serialize;
55
56
  pub mod tensor;
56
57
 
57
58
  mod device;
58
59
 
59
60
  pub use device::Device;
61
+ pub use random::manual_seed;
60
62
  pub use tensor::{no_grad, Tensor};
61
63
 
62
64
  /// Prelude module - import everything you need with `use ferrotensor::prelude::*`
63
65
  pub mod prelude {
64
66
  pub use crate::data::{DataLoader, Dataset};
65
67
  pub use crate::functional as F;
68
+ pub use crate::manual_seed;
66
69
  pub use crate::nn;
67
70
  pub use crate::nn::Module;
68
71
  pub use crate::optim;
@@ -0,0 +1,77 @@
1
+ //! The process-wide random number generator, seedable for reproducible runs.
2
+ //!
3
+ //! [`Tensor::randn`](crate::Tensor::randn), [`Tensor::rand`](crate::Tensor::rand),
4
+ //! [`Tensor::uniform`](crate::Tensor::uniform), weight initialisation in [`nn`](crate::nn)
5
+ //! and [`functional::dropout`](crate::functional::dropout) all draw from it. Until
6
+ //! [`manual_seed`] is called it is the thread-local OS-seeded generator; after it,
7
+ //! every draw comes from one seeded generator, so the same program produces the
8
+ //! same numbers on every run.
9
+
10
+ use rand::rngs::StdRng;
11
+ use rand::{RngCore, SeedableRng};
12
+ use std::sync::{Mutex, MutexGuard};
13
+
14
+ static SEEDED: Mutex<Option<StdRng>> = Mutex::new(None);
15
+
16
+ fn seeded() -> MutexGuard<'static, Option<StdRng>> {
17
+ // A panic while holding the lock leaves the generator usable.
18
+ SEEDED
19
+ .lock()
20
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
21
+ }
22
+
23
+ /// Seed the generator behind every random tensor, initialiser and dropout mask.
24
+ ///
25
+ /// Like `torch.manual_seed`: call it once at the start of a program. Calling it
26
+ /// again with the same seed restarts the same sequence.
27
+ ///
28
+ /// ```
29
+ /// use ferrotensor::{manual_seed, Device, Tensor};
30
+ ///
31
+ /// let device = Device::cpu();
32
+ /// manual_seed(42);
33
+ /// let a = Tensor::randn(&[4], &device).to_vec();
34
+ /// manual_seed(42);
35
+ /// let b = Tensor::randn(&[4], &device).to_vec();
36
+ /// assert_eq!(a, b);
37
+ /// ```
38
+ pub fn manual_seed(seed: u64) {
39
+ *seeded() = Some(StdRng::seed_from_u64(seed));
40
+ }
41
+
42
+ /// Run `f` with the seeded generator if [`manual_seed`] was called, else with
43
+ /// the thread-local OS-seeded one.
44
+ pub(crate) fn with_rng<T>(f: impl FnOnce(&mut dyn RngCore) -> T) -> T {
45
+ let mut guard = seeded();
46
+ match guard.as_mut() {
47
+ Some(rng) => f(rng),
48
+ None => {
49
+ drop(guard);
50
+ f(&mut rand::thread_rng())
51
+ }
52
+ }
53
+ }
54
+
55
+ #[cfg(test)]
56
+ mod tests {
57
+ use super::*;
58
+ use crate::{Device, Tensor};
59
+
60
+ // One test, so parallel tests can't interleave draws from the shared generator.
61
+ #[test]
62
+ fn manual_seed_makes_every_source_reproducible() {
63
+ let d = Device::cpu();
64
+ let draw = || {
65
+ let r = Tensor::randn(&[8], &d).to_vec();
66
+ let u = Tensor::uniform(&[8], -1.0, 1.0, &d).to_vec();
67
+ let m = crate::functional::dropout(&Tensor::ones(&[64], &d), 0.5, true).to_vec();
68
+ (r, u, m)
69
+ };
70
+ manual_seed(7);
71
+ let first = draw();
72
+ manual_seed(7);
73
+ assert_eq!(first, draw());
74
+ manual_seed(8);
75
+ assert_ne!(first.0, draw().0);
76
+ }
77
+ }
@@ -139,8 +139,10 @@ impl Tensor {
139
139
  }
140
140
 
141
141
  /// Create a tensor with random normal values (mean 0, std 1).
142
+ ///
143
+ /// Draws from the generator [`manual_seed`](crate::manual_seed) seeds.
142
144
  pub fn randn(shape: &[usize], device: &Device) -> Self {
143
- Self::randn_with(shape, &mut rand::thread_rng(), device)
145
+ crate::random::with_rng(|mut rng| Self::randn_with(shape, &mut rng, device))
144
146
  }
145
147
 
146
148
  /// Like [`Tensor::randn`], drawing from `rng` (for reproducible runs).
@@ -164,7 +166,7 @@ impl Tensor {
164
166
 
165
167
  /// Create a tensor with random uniform values in [low, high).
166
168
  pub fn uniform(shape: &[usize], low: f32, high: f32, device: &Device) -> Self {
167
- Self::uniform_with(shape, low, high, &mut rand::thread_rng(), device)
169
+ crate::random::with_rng(|mut rng| Self::uniform_with(shape, low, high, &mut rng, device))
168
170
  }
169
171
 
170
172
  /// Like [`Tensor::uniform`], drawing from `rng`.
@@ -166,6 +166,46 @@ pub(crate) fn reduce(op: u32, x: &[f32], outer: usize, dim: usize, inner: usize)
166
166
  out
167
167
  }
168
168
 
169
+ /// Index of the first largest element of `x` (with `smallest`, the first
170
+ /// smallest); NaN counts as larger than every number. 0 when `x` is empty.
171
+ pub(crate) fn arg_extreme(smallest: bool, x: &[f32]) -> usize {
172
+ let better = |a: f32, b: f32| {
173
+ if smallest {
174
+ a < b || (b.is_nan() && !a.is_nan())
175
+ } else {
176
+ a > b || (a.is_nan() && !b.is_nan())
177
+ }
178
+ };
179
+ let mut best = 0;
180
+ for (i, &v) in x.iter().enumerate().skip(1) {
181
+ if better(v, x[best]) {
182
+ best = i;
183
+ }
184
+ }
185
+ best
186
+ }
187
+
188
+ /// The `arg_reduce` kernel over a `[outer, dim, inner]` view: the index along
189
+ /// `dim` of each extreme, as `f32`.
190
+ pub(crate) fn arg_reduce(
191
+ smallest: bool,
192
+ x: &[f32],
193
+ outer: usize,
194
+ dim: usize,
195
+ inner: usize,
196
+ ) -> Vec<f32> {
197
+ let mut out = Vec::with_capacity(outer * inner);
198
+ let mut column = Vec::with_capacity(dim);
199
+ for o in 0..outer {
200
+ for k in 0..inner {
201
+ column.clear();
202
+ column.extend((0..dim).map(|j| x[(o * dim + j) * inner + k]));
203
+ out.push(arg_extreme(smallest, &column) as f32);
204
+ }
205
+ }
206
+ out
207
+ }
208
+
169
209
  /// The `softmax` kernel (or log-softmax) over a `[outer, dim, inner]` view.
170
210
  pub(crate) fn softmax(x: &[f32], outer: usize, dim: usize, inner: usize, log: bool) -> Vec<f32> {
171
211
  let mut out = vec![0.0; x.len()];
@@ -299,6 +339,20 @@ mod tests {
299
339
  assert!((ls[0].exp() - s[0]).abs() < 1e-6);
300
340
  }
301
341
 
342
+ #[test]
343
+ fn arg_reduce_takes_first_extreme() {
344
+ let x = [1.0, 3.0, 3.0, f32::NAN, 1.0, 0.0];
345
+ // NaN counts as larger than every number.
346
+ assert_eq!(arg_extreme(false, &x), 3);
347
+ assert_eq!(arg_extreme(true, &x), 5);
348
+ assert_eq!(arg_extreme(false, &x[..3]), 1);
349
+ assert_eq!(arg_extreme(true, &x[..5]), 0);
350
+ assert_eq!(arg_extreme(true, &[f32::NAN, f32::NAN]), 0);
351
+ // [3, 2] along dim 0.
352
+ assert_eq!(arg_reduce(false, &x, 1, 3, 2), vec![1.0, 1.0]);
353
+ assert_eq!(arg_reduce(true, &x, 1, 3, 2), vec![0.0, 2.0]);
354
+ }
355
+
302
356
  #[test]
303
357
  fn reduce_over_middle_axis() {
304
358
  // [2, 2, 2] summed over dim 1.
@@ -507,32 +507,18 @@ impl Tensor {
507
507
 
508
508
  /// Index of the first largest element (NaN counts as largest).
509
509
  pub fn argmax(&self) -> usize {
510
- arg_extreme(&self.to_vec(), |a, b| a > b || (a.is_nan() && !b.is_nan()))
510
+ self.arg_index_raw(false)
511
511
  }
512
512
 
513
513
  /// Index of the first smallest element (NaN counts as largest).
514
514
  pub fn argmin(&self) -> usize {
515
- arg_extreme(&self.to_vec(), |a, b| a < b || (b.is_nan() && !a.is_nan()))
515
+ self.arg_index_raw(true)
516
516
  }
517
517
 
518
518
  /// Indices of the largest elements along `dim`, as an `f32` tensor with
519
519
  /// `dim` removed.
520
520
  pub fn argmax_dim(&self, dim: isize) -> Tensor {
521
- let d = normalize_dim(dim, self.ndim());
522
- let (outer, size, inner) = super::shape::split_at_dim(&self.shape, d);
523
- let data = self.to_vec();
524
- let mut out = Vec::with_capacity(outer * inner);
525
- for o in 0..outer {
526
- for k in 0..inner {
527
- let column: Vec<f32> = (0..size)
528
- .map(|j| data[(o * size + j) * inner + k])
529
- .collect();
530
- out.push(arg_extreme(&column, |a, b| a > b || (a.is_nan() && !b.is_nan())) as f32);
531
- }
532
- }
533
- let mut shape = self.shape.clone();
534
- shape.remove(d);
535
- self.host_output(out, &shape)
521
+ self.arg_reduce_raw(normalize_dim(dim, self.ndim()), false)
536
522
  }
537
523
 
538
524
  // ========================================================================
@@ -859,16 +845,6 @@ fn index_add(grad: &Tensor, dim: usize, indices: &[usize], shape: &[usize]) -> T
859
845
  grad.host_output(out, shape)
860
846
  }
861
847
 
862
- fn arg_extreme(data: &[f32], better: impl Fn(f32, f32) -> bool) -> usize {
863
- let mut best = 0;
864
- for (i, &v) in data.iter().enumerate().skip(1) {
865
- if better(v, data[best]) {
866
- best = i;
867
- }
868
- }
869
- best
870
- }
871
-
872
848
  // ============================================================================
873
849
  // OPERATOR OVERLOADS
874
850
  // ============================================================================
@@ -33,6 +33,77 @@ fn as_u32(values: &[usize]) -> Vec<u32> {
33
33
  .collect()
34
34
  }
35
35
 
36
+ /// Threads per workgroup of `reduce_all` and `arg_reduce`.
37
+ const GROUP: usize = 256;
38
+ /// Workgroups the first pass of a full reduction is spread over.
39
+ const FIRST_PASS_GROUPS: usize = 256;
40
+
41
+ /// Reduce the first `n` elements of `input` into `out[0]`: one pass of
42
+ /// `FIRST_PASS_GROUPS` workgroups leaves a partial per workgroup and a
43
+ /// one-workgroup pass combines them.
44
+ fn reduce_all(ctx: &GpuContext, input: &Buffer, out: &Buffer, op: u32, n: usize) {
45
+ let params = |n: usize| Arg::Params([op, n as u32, 0, 0]);
46
+ let groups = n.div_ceil(GROUP).min(FIRST_PASS_GROUPS);
47
+ if groups == 1 {
48
+ ctx.dispatch(
49
+ "reduce_all",
50
+ &[Arg::Buffer(input), Arg::Buffer(out), params(n)],
51
+ (1, 1, 1),
52
+ );
53
+ return;
54
+ }
55
+ let partials = alloc(ctx, groups);
56
+ ctx.dispatch(
57
+ "reduce_all",
58
+ &[Arg::Buffer(input), Arg::Buffer(&partials), params(n)],
59
+ (groups as u32, 1, 1),
60
+ );
61
+ ctx.dispatch(
62
+ "reduce_all",
63
+ &[Arg::Buffer(&partials), Arg::Buffer(out), params(groups)],
64
+ (1, 1, 1),
65
+ );
66
+ }
67
+
68
+ /// The `arg_reduce` kernel over `input` viewed as `[outputs / inner, dim,
69
+ /// inner]`. Gives one `f32` index per output, or with `pairs` a (value bits,
70
+ /// index) pair per output. When there are too few outputs to fill the GPU,
71
+ /// each one is split over several workgroups and a second pass combines them.
72
+ fn arg_reduce(
73
+ ctx: &GpuContext,
74
+ input: &Buffer,
75
+ smallest: bool,
76
+ pairs: bool,
77
+ outputs: usize,
78
+ dim: usize,
79
+ inner: usize,
80
+ ) -> GpuBuffer {
81
+ const MIN: u32 = 1;
82
+ const PAIRS_IN: u32 = 2;
83
+ const PAIRS_OUT: u32 = 4;
84
+ let flags = if smallest { MIN } else { 0 };
85
+ let last = if pairs { flags | PAIRS_OUT } else { flags };
86
+ let out = alloc(ctx, if pairs { 2 * outputs } else { outputs });
87
+ let run =
88
+ |input: &Buffer, out: &Buffer, flags: u32, dim: usize, inner: usize, splits: usize| {
89
+ let meta = as_u32(&[flags as usize, outputs, dim, inner, splits]);
90
+ ctx.dispatch(
91
+ "arg_reduce",
92
+ &[Arg::Buffer(input), Arg::Buffer(out), Arg::Meta(&meta)],
93
+ ComputeEngine::workgroup_grid(outputs * splits),
94
+ );
95
+ };
96
+ let splits = (FIRST_PASS_GROUPS / outputs).clamp(1, dim.div_ceil(GROUP));
97
+ if splits == 1 {
98
+ run(input, &out, last, dim, inner, 1);
99
+ } else {
100
+ let partials = alloc(ctx, 2 * outputs * splits);
101
+ run(input, &partials, flags | PAIRS_OUT, dim, inner, splits);
102
+ run(&partials, &out, last | PAIRS_IN, splits, 1, 1);
103
+ }
104
+ out
105
+ }
106
+
36
107
  impl Tensor {
37
108
  /// The GPU context and buffer, when this tensor lives on a GPU.
38
109
  fn gpu(&self) -> Option<(&GpuContext, &Buffer)> {
@@ -142,6 +213,11 @@ impl Tensor {
142
213
  }
143
214
  let n = outer * inner;
144
215
  match self.gpu() {
216
+ Some((ctx, input)) if n == 1 => {
217
+ let out = alloc(ctx, 1);
218
+ reduce_all(ctx, input, &out, op, size);
219
+ self.gpu_output(out, out_shape)
220
+ }
145
221
  Some((ctx, input)) if n > 0 => {
146
222
  let out = alloc(ctx, n);
147
223
  let params = Arg::Params([op, outer as u32, size as u32, inner as u32]);
@@ -159,6 +235,38 @@ impl Tensor {
159
235
  }
160
236
  }
161
237
 
238
+ /// Index along `dim` (removed from the shape) of the first largest
239
+ /// element, or with `smallest` the first smallest, as `f32`.
240
+ pub(crate) fn arg_reduce_raw(&self, dim: usize, smallest: bool) -> Tensor {
241
+ let (outer, size, inner) = split_at_dim(&self.shape, dim);
242
+ let mut out_shape = self.shape.clone();
243
+ out_shape.remove(dim);
244
+ match self.gpu() {
245
+ Some((ctx, input)) if outer * inner > 0 && size > 0 => {
246
+ let out = arg_reduce(ctx, input, smallest, false, outer * inner, size, inner);
247
+ self.gpu_output(out, out_shape)
248
+ }
249
+ _ => {
250
+ let data = cpu::arg_reduce(smallest, &self.to_vec(), outer, size, inner);
251
+ self.host_output(data, &out_shape)
252
+ }
253
+ }
254
+ }
255
+
256
+ /// Flat index of the first largest element, or with `smallest` the first
257
+ /// smallest; 0 for an empty tensor. On a GPU only the index is read back.
258
+ pub(crate) fn arg_index_raw(&self, smallest: bool) -> usize {
259
+ let n = self.numel();
260
+ match self.gpu() {
261
+ Some((ctx, input)) if n > 0 => {
262
+ let out = arg_reduce(ctx, input, smallest, true, 1, n, 1);
263
+ let pair = self.gpu_output(out, vec![2]).to_vec();
264
+ pair[1].to_bits() as usize
265
+ }
266
+ _ => cpu::arg_extreme(smallest, &self.to_vec()),
267
+ }
268
+ }
269
+
162
270
  /// Softmax (or log-softmax) over `dim`.
163
271
  pub(crate) fn softmax_raw(&self, dim: usize, log: bool) -> Tensor {
164
272
  let (outer, size, inner) = split_at_dim(&self.shape, dim);
@@ -470,3 +470,96 @@ fn incompatible_shapes_panic() {
470
470
  let d = Device::cpu();
471
471
  Tensor::zeros(&[2, 3], &d).add(&Tensor::zeros(&[2], &d));
472
472
  }
473
+
474
+ // ============================================================================
475
+ // GPU REDUCTIONS
476
+ // ============================================================================
477
+
478
+ /// The GPU when `FERROTENSOR_RUN_GPU_TESTS=1`.
479
+ fn test_gpu() -> Option<Device> {
480
+ if std::env::var("FERROTENSOR_RUN_GPU_TESTS").as_deref() != Ok("1") {
481
+ eprintln!("skipping GPU reductions; set FERROTENSOR_RUN_GPU_TESTS=1 to enable");
482
+ return None;
483
+ }
484
+ Some(Device::gpu().expect("FERROTENSOR_RUN_GPU_TESTS=1 but no GPU is available"))
485
+ }
486
+
487
+ #[test]
488
+ fn full_reductions_match_cpu_on_gpu() {
489
+ let Some(gpu) = test_gpu() else { return };
490
+ let cpu = Device::cpu();
491
+ let mut rng = StdRng::seed_from_u64(11);
492
+ for n in [1, 255, 256, 257, 65_537, 1_000_003, 4096 * 4096] {
493
+ let data: Vec<f32> = (0..n).map(|_| rng.gen_range(-2.0..2.0)).collect();
494
+ let (c, g) = (
495
+ Tensor::from_data(&data, &[n], &cpu),
496
+ Tensor::from_data(&data, &[n], &gpu),
497
+ );
498
+ // max and min do not depend on the order of the elements.
499
+ assert_eq!(g.max().item(), c.max().item(), "max of {n}");
500
+ assert_eq!(g.min().item(), c.min().item(), "min of {n}");
501
+ // The GPU adds in a different order than the CPU's left-to-right loop;
502
+ // each order's rounding error is a few f32 epsilons per element of
503
+ // sum(|x|) at most, so allow 1e-6 of sum(|x|) between them.
504
+ let abs: f64 = data.iter().map(|v| v.abs() as f64).sum();
505
+ let (gs, cs) = (g.sum().item(), c.sum().item());
506
+ assert!(
507
+ ((gs - cs) as f64).abs() <= 1e-6 * abs,
508
+ "sum of {n}: gpu {gs} vs cpu {cs}"
509
+ );
510
+ }
511
+ }
512
+
513
+ /// Values with many ties, and NaN at `nans`.
514
+ fn tied(n: usize, nans: &[usize]) -> Vec<f32> {
515
+ let mut data: Vec<f32> = (0..n).map(|i| ((i * 7919) % 13) as f32 - 6.0).collect();
516
+ for &i in nans {
517
+ data[i] = f32::NAN;
518
+ }
519
+ data
520
+ }
521
+
522
+ #[test]
523
+ fn arg_reductions_match_cpu_on_gpu() {
524
+ let Some(gpu) = test_gpu() else { return };
525
+ let cpu = Device::cpu();
526
+ let cases: [(&[usize], &[usize]); 6] = [
527
+ (&[1_000_003], &[]),
528
+ (&[1_000_003], &[500_000, 900_000]),
529
+ (&[3, 5, 4], &[]),
530
+ (&[3, 5, 4], &[7, 9, 30, 31, 59]),
531
+ (&[2, 100_000], &[150_000]),
532
+ (&[70_000, 3], &[4, 5, 100]),
533
+ ];
534
+ for (shape, nans) in cases {
535
+ let data = tied(shape.iter().product(), nans);
536
+ let (c, g) = (
537
+ Tensor::from_data(&data, shape, &cpu),
538
+ Tensor::from_data(&data, shape, &gpu),
539
+ );
540
+ assert_eq!(g.argmax(), c.argmax(), "argmax {shape:?}");
541
+ assert_eq!(g.argmin(), c.argmin(), "argmin {shape:?}");
542
+ for d in 0..shape.len() {
543
+ for smallest in [false, true] {
544
+ assert_eq!(
545
+ g.arg_reduce_raw(d, smallest).to_vec(),
546
+ c.arg_reduce_raw(d, smallest).to_vec(),
547
+ "arg_reduce {shape:?} dim {d} smallest {smallest}"
548
+ );
549
+ }
550
+ }
551
+ }
552
+ }
553
+
554
+ #[test]
555
+ fn gpu_argmax_index_is_exact_past_f32_integers() {
556
+ let Some(gpu) = test_gpu() else { return };
557
+ // 2^24 + 1 is the first integer f32 cannot hold.
558
+ let n = (1 << 24) + 2;
559
+ let mut data = vec![0.0; n];
560
+ data[n - 1] = 1.0;
561
+ data[n - 2] = -1.0;
562
+ let x = Tensor::from_vec(data, &[n], &gpu);
563
+ assert_eq!(x.argmax(), n - 1);
564
+ assert_eq!(x.argmin(), n - 2);
565
+ }
@@ -27,6 +27,7 @@ fn _ferrotensor(m: &Bound<'_, PyModule>) -> PyResult<()> {
27
27
  m.add_function(wrap_pyfunction!(tensor::zeros_like, m)?)?;
28
28
  m.add_function(wrap_pyfunction!(tensor::ones_like, m)?)?;
29
29
  m.add_function(wrap_pyfunction!(tensor::full_like, m)?)?;
30
+ m.add_function(wrap_pyfunction!(tensor::manual_seed, m)?)?;
30
31
  m.add_function(wrap_pyfunction!(tensor::randn, m)?)?;
31
32
  m.add_function(wrap_pyfunction!(tensor::rand, m)?)?;
32
33
  m.add_function(wrap_pyfunction!(tensor::arange, m)?)?;
@@ -1253,6 +1253,12 @@ pub(crate) fn ones(
1253
1253
  create(size, device, requires_grad, Tensor::ones)
1254
1254
  }
1255
1255
 
1256
+ /// Seed the generator behind `randn`, `rand`, layer initialisation and dropout.
1257
+ #[pyfunction]
1258
+ pub(crate) fn manual_seed(seed: u64) {
1259
+ ferrotensor::manual_seed(seed);
1260
+ }
1261
+
1256
1262
  /// Standard-normal samples.
1257
1263
  #[pyfunction]
1258
1264
  #[pyo3(signature = (*size, device = None, requires_grad = false))]
@@ -227,3 +227,17 @@ def test_device_basics():
227
227
  assert best.is_gpu == ft.Device.gpu_available()
228
228
  assert ft.zeros(1).to("cpu").device == cpu
229
229
  assert ft.__version__
230
+
231
+
232
+ def test_manual_seed_makes_random_tensors_and_init_reproducible():
233
+ from ferrotensor import nn
234
+
235
+ def draw():
236
+ return ft.randn(5).tolist(), nn.Linear(3, 2).weight.tolist()
237
+
238
+ ft.manual_seed(123)
239
+ first = draw()
240
+ ft.manual_seed(123)
241
+ assert draw() == first
242
+ ft.manual_seed(124)
243
+ assert draw()[0] != first[0]
@@ -30,6 +30,9 @@ class MLP(nn.Module):
30
30
 
31
31
 
32
32
  def test_mlp_loss_drops_and_fits(tmp_path):
33
+ # Without a seed the random initialisation occasionally lands where 300
34
+ # steps reach only ~78% accuracy, which made this test flaky.
35
+ ft.manual_seed(0)
33
36
  x, y = make_spirals()
34
37
  model = MLP()
35
38
  opt = optim.AdamW(model.parameters(), lr=0.02, weight_decay=0.0)
@@ -26,6 +26,7 @@ from ferrotensor._ferrotensor import (
26
26
  is_grad_enabled,
27
27
  linspace,
28
28
  load,
29
+ manual_seed,
29
30
  ones,
30
31
  ones_like,
31
32
  rand,
@@ -80,6 +81,7 @@ __all__ = [
80
81
  "is_grad_enabled",
81
82
  "linspace",
82
83
  "load",
84
+ "manual_seed",
83
85
  "nn",
84
86
  "no_grad",
85
87
  "ones",
@@ -233,6 +233,12 @@ def full(
233
233
  def zeros_like(input: Tensor, *, requires_grad: bool = False) -> Tensor: ...
234
234
  def ones_like(input: Tensor, *, requires_grad: bool = False) -> Tensor: ...
235
235
  def full_like(input: Tensor, fill_value: float, *, requires_grad: bool = False) -> Tensor: ...
236
+ def manual_seed(seed: int) -> None:
237
+ """Seed the generator behind ``randn``, ``rand``, layer initialisation and dropout.
238
+
239
+ Like ``torch.manual_seed``: the same seed gives the same numbers on every run.
240
+ """
241
+
236
242
  def arange(
237
243
  start: float,
238
244
  end: float | None = None,
File without changes
File without changes