forked from opentensor/subtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepoch.rs
796 lines (677 loc) · 33.3 KB
/
epoch.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use super::*;
use crate::math::*;
use frame_support::IterableStorageDoubleMap;
use sp_std::vec;
use substrate_fixed::types::{I32F32, I64F64, I96F32};
impl<T: Config> Pallet<T> {
/// Calculates reward consensus and returns the emissions for uids/hotkeys in a given `netuid`.
/// (Dense version used only for testing purposes.)
#[allow(clippy::indexing_slicing)]
pub fn epoch_dense(netuid: u16, rao_emission: u64) -> Vec<(T::AccountId, u64, u64)> {
// Get subnetwork size.
let n: u16 = Self::get_subnetwork_n(netuid);
log::trace!("n:\n{:?}\n", n);
// ======================
// == Active & updated ==
// ======================
// Get current block.
let current_block: u64 = Self::get_current_block_as_u64();
log::trace!("current_block:\n{:?}\n", current_block);
// Get activity cutoff.
let activity_cutoff: u64 = Self::get_activity_cutoff(netuid) as u64;
log::trace!("activity_cutoff:\n{:?}\n", activity_cutoff);
// Last update vector.
let last_update: Vec<u64> = Self::get_last_update(netuid);
log::trace!("Last update:\n{:?}\n", &last_update);
// Inactive mask.
let inactive: Vec<bool> = last_update
.iter()
.map(|updated| *updated + activity_cutoff < current_block)
.collect();
log::trace!("Inactive:\n{:?}\n", inactive.clone());
// Logical negation of inactive.
let active: Vec<bool> = inactive.iter().map(|&b| !b).collect();
// Block at registration vector (block when each neuron was most recently registered).
let block_at_registration: Vec<u64> = Self::get_block_at_registration(netuid);
log::trace!("Block at registration:\n{:?}\n", &block_at_registration);
// Outdated matrix, updated_ij=True if i has last updated (weights) after j has last registered.
let outdated: Vec<Vec<bool>> = last_update
.iter()
.map(|updated| {
block_at_registration
.iter()
.map(|registered| updated <= registered)
.collect()
})
.collect();
log::trace!("Outdated:\n{:?}\n", &outdated);
// ===========
// == Stake ==
// ===========
let hotkeys: Vec<(u16, T::AccountId)> =
<Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid)
.collect();
log::trace!("hotkeys: {:?}", &hotkeys);
// Access network stake as normalized vector.
let mut stake_64: Vec<I64F64> = vec![I64F64::from_num(0.0); n as usize];
for (uid_i, hotkey) in &hotkeys {
stake_64[*uid_i as usize] = I64F64::from_num(Self::get_total_stake_for_hotkey(hotkey));
}
inplace_normalize_64(&mut stake_64);
let stake: Vec<I32F32> = vec_fixed64_to_fixed32(stake_64);
log::trace!("S:\n{:?}\n", &stake);
// =======================
// == Validator permits ==
// =======================
// Get validator permits.
let validator_permits: Vec<bool> = Self::get_validator_permit(netuid);
log::trace!("validator_permits: {:?}", validator_permits);
// Logical negation of validator_permits.
let validator_forbids: Vec<bool> = validator_permits.iter().map(|&b| !b).collect();
// Get max allowed validators.
let max_allowed_validators: u16 = Self::get_max_allowed_validators(netuid);
log::trace!("max_allowed_validators: {:?}", max_allowed_validators);
// Get new validator permits.
let new_validator_permits: Vec<bool> = is_topk(&stake, max_allowed_validators as usize);
log::trace!("new_validator_permits: {:?}", new_validator_permits);
// ==================
// == Active Stake ==
// ==================
let mut active_stake: Vec<I32F32> = stake.clone();
// Remove inactive stake.
inplace_mask_vector(&inactive, &mut active_stake);
// Remove non-validator stake.
inplace_mask_vector(&validator_forbids, &mut active_stake);
// Normalize active stake.
inplace_normalize(&mut active_stake);
log::trace!("S:\n{:?}\n", &active_stake);
// =============
// == Weights ==
// =============
// Access network weights row unnormalized.
let mut weights: Vec<Vec<I32F32>> = Self::get_weights(netuid);
log::trace!("W:\n{:?}\n", &weights);
// Mask weights that are not from permitted validators.
inplace_mask_rows(&validator_forbids, &mut weights);
// log::trace!( "W (permit): {:?}", &weights );
// Remove self-weight by masking diagonal.
inplace_mask_diag(&mut weights);
// log::trace!( "W (permit+diag):\n{:?}\n", &weights );
// Mask outdated weights: remove weights referring to deregistered neurons.
inplace_mask_matrix(&outdated, &mut weights);
// log::trace!( "W (permit+diag+outdate):\n{:?}\n", &weights );
// Normalize remaining weights.
inplace_row_normalize(&mut weights);
// log::trace!( "W (mask+norm):\n{:?}\n", &weights );
// ================================
// == Consensus, Validator Trust ==
// ================================
// Compute preranks: r_j = SUM(i) w_ij * s_i
let preranks: Vec<I32F32> = matmul(&weights, &active_stake);
// Clip weights at majority consensus
let kappa: I32F32 = Self::get_float_kappa(netuid); // consensus majority ratio, e.g. 51%.
let consensus: Vec<I32F32> = weighted_median_col(&active_stake, &weights, kappa);
inplace_col_clip(&mut weights, &consensus);
let validator_trust: Vec<I32F32> = row_sum(&weights);
// ====================================
// == Ranks, Server Trust, Incentive ==
// ====================================
// Compute ranks: r_j = SUM(i) w_ij * s_i
let mut ranks: Vec<I32F32> = matmul(&weights, &active_stake);
// Compute server trust: ratio of rank after vs. rank before.
let trust: Vec<I32F32> = vecdiv(&ranks, &preranks);
inplace_normalize(&mut ranks);
let incentive: Vec<I32F32> = ranks.clone();
log::trace!("I:\n{:?}\n", &incentive);
// =========================
// == Bonds and Dividends ==
// =========================
// Access network bonds.
let mut bonds: Vec<Vec<I32F32>> = Self::get_bonds(netuid);
inplace_mask_matrix(&outdated, &mut bonds); // mask outdated bonds
inplace_col_normalize(&mut bonds); // sum_i b_ij = 1
// log::trace!( "B:\n{:?}\n", &bonds );
// Compute bonds delta column normalized.
let mut bonds_delta: Vec<Vec<I32F32>> = row_hadamard(&weights, &active_stake); // ΔB = W◦S
inplace_col_normalize(&mut bonds_delta); // sum_i b_ij = 1
// log::trace!( "ΔB:\n{:?}\n", &bonds_delta );
// Compute bonds moving average.
let bonds_moving_average: I64F64 =
I64F64::from_num(Self::get_bonds_moving_average(netuid)) / I64F64::from_num(1_000_000);
let alpha: I32F32 = I32F32::from_num(1) - I32F32::from_num(bonds_moving_average);
let mut ema_bonds: Vec<Vec<I32F32>> = mat_ema(&bonds_delta, &bonds, alpha);
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
// log::trace!( "emaB:\n{:?}\n", &ema_bonds );
// Compute dividends: d_i = SUM(j) b_ij * inc_j
let mut dividends: Vec<I32F32> = matmul_transpose(&ema_bonds, &incentive);
inplace_normalize(&mut dividends);
log::trace!("D:\n{:?}\n", ÷nds);
// =================================
// == Emission and Pruning scores ==
// =================================
// Compute emission scores.
// Compute normalized emission scores. range: I32F32(0, 1)
// Compute normalized emission scores. range: I32F32(0, 1)
let combined_emission: Vec<I32F32> = incentive
.iter()
.zip(dividends.clone())
.map(|(ii, di)| ii + di)
.collect();
let emission_sum: I32F32 = combined_emission.iter().sum();
let mut normalized_server_emission: Vec<I32F32> = incentive.clone(); // Servers get incentive.
let mut normalized_validator_emission: Vec<I32F32> = dividends.clone(); // Validators get dividends.
let mut normalized_combined_emission: Vec<I32F32> = combined_emission.clone();
// Normalize on the sum of incentive + dividends.
inplace_normalize_using_sum(&mut normalized_server_emission, emission_sum);
inplace_normalize_using_sum(&mut normalized_validator_emission, emission_sum);
inplace_normalize(&mut normalized_combined_emission);
// If emission is zero, replace emission with normalized stake.
if emission_sum == I32F32::from(0) {
// no weights set | outdated weights | self_weights
if is_zero(&active_stake) {
// no active stake
normalized_validator_emission.clone_from(&stake); // do not mask inactive, assumes stake is normalized
normalized_combined_emission.clone_from(&stake);
} else {
normalized_validator_emission.clone_from(&active_stake); // emission proportional to inactive-masked normalized stake
normalized_combined_emission.clone_from(&active_stake);
}
}
// Compute rao based emission scores. range: I96F32(0, rao_emission)
let float_rao_emission: I96F32 = I96F32::from_num(rao_emission);
let server_emission: Vec<I96F32> = normalized_server_emission
.iter()
.map(|se: &I32F32| I96F32::from_num(*se) * float_rao_emission)
.collect();
let server_emission: Vec<u64> = server_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
let validator_emission: Vec<I96F32> = normalized_validator_emission
.iter()
.map(|ve: &I32F32| I96F32::from_num(*ve) * float_rao_emission)
.collect();
let validator_emission: Vec<u64> = validator_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
// Used only to track combined emission in the storage.
let combined_emission: Vec<I96F32> = normalized_combined_emission
.iter()
.map(|ce: &I32F32| I96F32::from_num(*ce) * float_rao_emission)
.collect();
let combined_emission: Vec<u64> = combined_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
log::trace!("nSE: {:?}", &normalized_server_emission);
log::trace!("SE: {:?}", &server_emission);
log::trace!("nVE: {:?}", &normalized_validator_emission);
log::trace!("VE: {:?}", &validator_emission);
log::trace!("nCE: {:?}", &normalized_combined_emission);
log::trace!("CE: {:?}", &combined_emission);
// Set pruning scores using combined emission scores.
let pruning_scores: Vec<I32F32> = normalized_combined_emission.clone();
log::trace!("P: {:?}", &pruning_scores);
// ===================
// == Value storage ==
// ===================
let cloned_emission: Vec<u64> = combined_emission.clone();
let cloned_ranks: Vec<u16> = ranks
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_trust: Vec<u16> = trust
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_consensus: Vec<u16> = consensus
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_incentive: Vec<u16> = incentive
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_dividends: Vec<u16> = dividends
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_pruning_scores: Vec<u16> = vec_max_upscale_to_u16(&pruning_scores);
let cloned_validator_trust: Vec<u16> = validator_trust
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
Active::<T>::insert(netuid, active.clone());
Emission::<T>::insert(netuid, cloned_emission);
Rank::<T>::insert(netuid, cloned_ranks);
Trust::<T>::insert(netuid, cloned_trust);
Consensus::<T>::insert(netuid, cloned_consensus);
Incentive::<T>::insert(netuid, cloned_incentive);
Dividends::<T>::insert(netuid, cloned_dividends);
PruningScores::<T>::insert(netuid, cloned_pruning_scores);
ValidatorTrust::<T>::insert(netuid, cloned_validator_trust);
ValidatorPermit::<T>::insert(netuid, new_validator_permits.clone());
// Column max-upscale EMA bonds for storage: max_i w_ij = 1.
inplace_col_max_upscale(&mut ema_bonds);
new_validator_permits
.iter()
.zip(validator_permits)
.zip(ema_bonds)
.enumerate()
.for_each(|(i, ((new_permit, validator_permit), ema_bond))| {
// Set bonds only if uid retains validator permit, otherwise clear bonds.
if *new_permit {
let new_bonds_row: Vec<(u16, u16)> = (0..n)
.zip(vec_fixed_proportions_to_u16(ema_bond.clone()))
.collect();
Bonds::<T>::insert(netuid, i as u16, new_bonds_row);
} else if validator_permit {
// Only overwrite the intersection.
let new_empty_bonds_row: Vec<(u16, u16)> = vec![];
Bonds::<T>::insert(netuid, i as u16, new_empty_bonds_row);
}
});
hotkeys
.into_iter()
.map(|(uid_i, hotkey)| {
(
hotkey,
server_emission[uid_i as usize],
validator_emission[uid_i as usize],
)
})
.collect()
}
/// Calculates reward consensus values, then updates rank, trust, consensus, incentive, dividend, pruning_score, emission and bonds, and
/// returns the emissions for uids/hotkeys in a given `netuid`.
///
/// # Args:
/// * 'netuid': ( u16 ):
/// - The network to distribute the emission onto.
///
/// * 'rao_emission': ( u64 ):
/// - The total emission for the epoch.
///
/// * 'debug' ( bool ):
/// - Print debugging outputs.
///
#[allow(clippy::indexing_slicing)]
pub fn epoch(netuid: u16, rao_emission: u64) -> Vec<(T::AccountId, u64, u64)> {
// Get subnetwork size.
let n: u16 = Self::get_subnetwork_n(netuid);
log::trace!("n: {:?}", n);
// ======================
// == Active & updated ==
// ======================
// Get current block.
let current_block: u64 = Self::get_current_block_as_u64();
log::trace!("current_block: {:?}", current_block);
// Get activity cutoff.
let activity_cutoff: u64 = Self::get_activity_cutoff(netuid) as u64;
log::trace!("activity_cutoff: {:?}", activity_cutoff);
// Last update vector.
let last_update: Vec<u64> = Self::get_last_update(netuid);
log::trace!("Last update: {:?}", &last_update);
// Inactive mask.
let inactive: Vec<bool> = last_update
.iter()
.map(|updated| *updated + activity_cutoff < current_block)
.collect();
log::trace!("Inactive: {:?}", inactive.clone());
// Logical negation of inactive.
let active: Vec<bool> = inactive.iter().map(|&b| !b).collect();
// Block at registration vector (block when each neuron was most recently registered).
let block_at_registration: Vec<u64> = Self::get_block_at_registration(netuid);
log::trace!("Block at registration: {:?}", &block_at_registration);
// ===========
// == Stake ==
// ===========
let hotkeys: Vec<(u16, T::AccountId)> =
<Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid)
.collect();
log::trace!("hotkeys: {:?}", &hotkeys);
// Access network stake as normalized vector.
let mut stake_64: Vec<I64F64> = vec![I64F64::from_num(0.0); n as usize];
for (uid_i, hotkey) in &hotkeys {
stake_64[*uid_i as usize] = I64F64::from_num(Self::get_total_stake_for_hotkey(hotkey));
}
inplace_normalize_64(&mut stake_64);
let stake: Vec<I32F32> = vec_fixed64_to_fixed32(stake_64);
// range: I32F32(0, 1)
log::trace!("S: {:?}", &stake);
// =======================
// == Validator permits ==
// =======================
// Get current validator permits.
let validator_permits: Vec<bool> = Self::get_validator_permit(netuid);
log::trace!("validator_permits: {:?}", validator_permits);
// Logical negation of validator_permits.
let validator_forbids: Vec<bool> = validator_permits.iter().map(|&b| !b).collect();
// Get max allowed validators.
let max_allowed_validators: u16 = Self::get_max_allowed_validators(netuid);
log::trace!("max_allowed_validators: {:?}", max_allowed_validators);
// Get new validator permits.
let new_validator_permits: Vec<bool> = is_topk(&stake, max_allowed_validators as usize);
log::trace!("new_validator_permits: {:?}", new_validator_permits);
// ==================
// == Active Stake ==
// ==================
let mut active_stake: Vec<I32F32> = stake.clone();
// Remove inactive stake.
inplace_mask_vector(&inactive, &mut active_stake);
// Remove non-validator stake.
inplace_mask_vector(&validator_forbids, &mut active_stake);
// Normalize active stake.
inplace_normalize(&mut active_stake);
log::trace!("S:\n{:?}\n", &active_stake);
// =============
// == Weights ==
// =============
// Access network weights row unnormalized.
let mut weights: Vec<Vec<(u16, I32F32)>> = Self::get_weights_sparse(netuid);
// log::trace!( "W: {:?}", &weights );
// Mask weights that are not from permitted validators.
weights = mask_rows_sparse(&validator_forbids, &weights);
// log::trace!( "W (permit): {:?}", &weights );
// Remove self-weight by masking diagonal.
weights = mask_diag_sparse(&weights);
// log::trace!( "W (permit+diag): {:?}", &weights );
// Remove weights referring to deregistered neurons.
weights = vec_mask_sparse_matrix(
&weights,
&last_update,
&block_at_registration,
&|updated, registered| updated <= registered,
);
// log::trace!( "W (permit+diag+outdate): {:?}", &weights );
// Normalize remaining weights.
inplace_row_normalize_sparse(&mut weights);
// log::trace!( "W (mask+norm): {:?}", &weights );
// ================================
// == Consensus, Validator Trust ==
// ================================
// Compute preranks: r_j = SUM(i) w_ij * s_i
let preranks: Vec<I32F32> = matmul_sparse(&weights, &active_stake, n);
// log::trace!( "R (before): {:?}", &preranks );
// Clip weights at majority consensus
let kappa: I32F32 = Self::get_float_kappa(netuid); // consensus majority ratio, e.g. 51%.
let consensus: Vec<I32F32> = weighted_median_col_sparse(&active_stake, &weights, n, kappa);
log::trace!("C: {:?}", &consensus);
weights = col_clip_sparse(&weights, &consensus);
// log::trace!( "W: {:?}", &weights );
let validator_trust: Vec<I32F32> = row_sum_sparse(&weights);
log::trace!("Tv: {:?}", &validator_trust);
// =============================
// == Ranks, Trust, Incentive ==
// =============================
// Compute ranks: r_j = SUM(i) w_ij * s_i.
let mut ranks: Vec<I32F32> = matmul_sparse(&weights, &active_stake, n);
// log::trace!( "R (after): {:?}", &ranks );
// Compute server trust: ratio of rank after vs. rank before.
let trust: Vec<I32F32> = vecdiv(&ranks, &preranks); // range: I32F32(0, 1)
log::trace!("T: {:?}", &trust);
inplace_normalize(&mut ranks); // range: I32F32(0, 1)
let incentive: Vec<I32F32> = ranks.clone();
log::trace!("I (=R): {:?}", &incentive);
// =========================
// == Bonds and Dividends ==
// =========================
// Access network bonds.
let mut bonds: Vec<Vec<(u16, I32F32)>> = Self::get_bonds_sparse(netuid);
// log::trace!( "B: {:?}", &bonds );
// Remove bonds referring to deregistered neurons.
bonds = vec_mask_sparse_matrix(
&bonds,
&last_update,
&block_at_registration,
&|updated, registered| updated <= registered,
);
// log::trace!( "B (outdatedmask): {:?}", &bonds );
// Normalize remaining bonds: sum_i b_ij = 1.
inplace_col_normalize_sparse(&mut bonds, n);
// log::trace!( "B (mask+norm): {:?}", &bonds );
// Compute bonds delta column normalized.
let mut bonds_delta: Vec<Vec<(u16, I32F32)>> = row_hadamard_sparse(&weights, &active_stake); // ΔB = W◦S (outdated W masked)
// log::trace!( "ΔB: {:?}", &bonds_delta );
// Normalize bonds delta.
inplace_col_normalize_sparse(&mut bonds_delta, n); // sum_i b_ij = 1
// log::trace!( "ΔB (norm): {:?}", &bonds_delta );
// Compute bonds moving average.
let bonds_moving_average: I64F64 =
I64F64::from_num(Self::get_bonds_moving_average(netuid)) / I64F64::from_num(1_000_000);
let alpha: I32F32 = I32F32::from_num(1) - I32F32::from_num(bonds_moving_average);
let mut ema_bonds: Vec<Vec<(u16, I32F32)>> = mat_ema_sparse(&bonds_delta, &bonds, alpha);
// Normalize EMA bonds.
inplace_col_normalize_sparse(&mut ema_bonds, n); // sum_i b_ij = 1
// log::trace!( "emaB: {:?}", &ema_bonds );
// Compute dividends: d_i = SUM(j) b_ij * inc_j.
// range: I32F32(0, 1)
let mut dividends: Vec<I32F32> = matmul_transpose_sparse(&ema_bonds, &incentive);
inplace_normalize(&mut dividends);
log::trace!("D: {:?}", ÷nds);
// =================================
// == Emission and Pruning scores ==
// =================================
// Compute normalized emission scores. range: I32F32(0, 1)
let combined_emission: Vec<I32F32> = incentive
.iter()
.zip(dividends.clone())
.map(|(ii, di)| ii + di)
.collect();
let emission_sum: I32F32 = combined_emission.iter().sum();
let mut normalized_server_emission: Vec<I32F32> = incentive.clone(); // Servers get incentive.
let mut normalized_validator_emission: Vec<I32F32> = dividends.clone(); // Validators get dividends.
let mut normalized_combined_emission: Vec<I32F32> = combined_emission.clone();
// Normalize on the sum of incentive + dividends.
inplace_normalize_using_sum(&mut normalized_server_emission, emission_sum);
inplace_normalize_using_sum(&mut normalized_validator_emission, emission_sum);
inplace_normalize(&mut normalized_combined_emission);
// If emission is zero, replace emission with normalized stake.
if emission_sum == I32F32::from(0) {
// no weights set | outdated weights | self_weights
if is_zero(&active_stake) {
// no active stake
normalized_validator_emission.clone_from(&stake); // do not mask inactive, assumes stake is normalized
normalized_combined_emission.clone_from(&stake);
} else {
normalized_validator_emission.clone_from(&active_stake); // emission proportional to inactive-masked normalized stake
normalized_combined_emission.clone_from(&active_stake);
}
}
// Compute rao based emission scores. range: I96F32(0, rao_emission)
let float_rao_emission: I96F32 = I96F32::from_num(rao_emission);
let server_emission: Vec<I96F32> = normalized_server_emission
.iter()
.map(|se: &I32F32| I96F32::from_num(*se) * float_rao_emission)
.collect();
let server_emission: Vec<u64> = server_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
let validator_emission: Vec<I96F32> = normalized_validator_emission
.iter()
.map(|ve: &I32F32| I96F32::from_num(*ve) * float_rao_emission)
.collect();
let validator_emission: Vec<u64> = validator_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
// Only used to track emission in storage.
let combined_emission: Vec<I96F32> = normalized_combined_emission
.iter()
.map(|ce: &I32F32| I96F32::from_num(*ce) * float_rao_emission)
.collect();
let combined_emission: Vec<u64> = combined_emission
.iter()
.map(|e: &I96F32| e.to_num::<u64>())
.collect();
log::trace!("nSE: {:?}", &normalized_server_emission);
log::trace!("SE: {:?}", &server_emission);
log::trace!("nVE: {:?}", &normalized_validator_emission);
log::trace!("VE: {:?}", &validator_emission);
log::trace!("nCE: {:?}", &normalized_combined_emission);
log::trace!("CE: {:?}", &combined_emission);
// Set pruning scores using combined emission scores.
let pruning_scores: Vec<I32F32> = normalized_combined_emission.clone();
log::trace!("P: {:?}", &pruning_scores);
// ===================
// == Value storage ==
// ===================
let cloned_emission: Vec<u64> = combined_emission.clone();
let cloned_ranks: Vec<u16> = ranks
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_trust: Vec<u16> = trust
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_consensus: Vec<u16> = consensus
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_incentive: Vec<u16> = incentive
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_dividends: Vec<u16> = dividends
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
let cloned_pruning_scores: Vec<u16> = vec_max_upscale_to_u16(&pruning_scores);
let cloned_validator_trust: Vec<u16> = validator_trust
.iter()
.map(|xi| fixed_proportion_to_u16(*xi))
.collect::<Vec<u16>>();
Active::<T>::insert(netuid, active.clone());
Emission::<T>::insert(netuid, cloned_emission);
Rank::<T>::insert(netuid, cloned_ranks);
Trust::<T>::insert(netuid, cloned_trust);
Consensus::<T>::insert(netuid, cloned_consensus);
Incentive::<T>::insert(netuid, cloned_incentive);
Dividends::<T>::insert(netuid, cloned_dividends);
PruningScores::<T>::insert(netuid, cloned_pruning_scores);
ValidatorTrust::<T>::insert(netuid, cloned_validator_trust);
ValidatorPermit::<T>::insert(netuid, new_validator_permits.clone());
// Column max-upscale EMA bonds for storage: max_i w_ij = 1.
inplace_col_max_upscale_sparse(&mut ema_bonds, n);
new_validator_permits
.iter()
.zip(validator_permits)
.zip(ema_bonds)
.enumerate()
.for_each(|(i, ((new_permit, validator_permit), ema_bond))| {
// Set bonds only if uid retains validator permit, otherwise clear bonds.
if *new_permit {
let new_bonds_row: Vec<(u16, u16)> = ema_bond
.iter()
.map(|(j, value)| (*j, fixed_proportion_to_u16(*value)))
.collect();
Bonds::<T>::insert(netuid, i as u16, new_bonds_row);
} else if validator_permit {
// Only overwrite the intersection.
let new_empty_bonds_row: Vec<(u16, u16)> = vec![];
Bonds::<T>::insert(netuid, i as u16, new_empty_bonds_row);
}
});
// Emission tuples ( hotkeys, server_emission, validator_emission )
hotkeys
.into_iter()
.map(|(uid_i, hotkey)| {
(
hotkey,
server_emission[uid_i as usize],
validator_emission[uid_i as usize],
)
})
.collect()
}
pub fn get_float_rho(netuid: u16) -> I32F32 {
I32F32::from_num(Self::get_rho(netuid))
}
pub fn get_float_kappa(netuid: u16) -> I32F32 {
I32F32::from_num(Self::get_kappa(netuid)) / I32F32::from_num(u16::MAX)
}
pub fn get_normalized_stake(netuid: u16) -> Vec<I32F32> {
let n = Self::get_subnetwork_n(netuid);
let mut stake_64: Vec<I64F64> = (0..n)
.map(|neuron_uid| {
I64F64::from_num(Self::get_stake_for_uid_and_subnetwork(netuid, neuron_uid))
})
.collect();
inplace_normalize_64(&mut stake_64);
let stake: Vec<I32F32> = vec_fixed64_to_fixed32(stake_64);
stake
}
pub fn get_block_at_registration(netuid: u16) -> Vec<u64> {
let n = Self::get_subnetwork_n(netuid);
let block_at_registration: Vec<u64> = (0..n)
.map(|neuron_uid| {
if Keys::<T>::contains_key(netuid, neuron_uid) {
Self::get_neuron_block_at_registration(netuid, neuron_uid)
} else {
0
}
})
.collect();
block_at_registration
}
/// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_weights_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, weights_i) in
<Weights<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
.filter(|(uid_i, _)| *uid_i < n as u16)
{
for (uid_j, weight_ij) in weights_i.iter().filter(|(uid_j, _)| *uid_j < n as u16) {
weights[uid_i as usize].push((*uid_j, I32F32::from_num(*weight_ij)));
}
}
weights
}
/// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_weights(netuid: u16) -> Vec<Vec<I32F32>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<I32F32>> = vec![vec![I32F32::from_num(0.0); n]; n];
for (uid_i, weights_i) in
<Weights<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
{
for (uid_j, weight_ij) in weights_i {
weights[uid_i as usize][uid_j as usize] = I32F32::from_num(weight_ij);
}
}
weights
}
/// Output unnormalized sparse bonds, input bonds are assumed to be column max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_bonds_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, bonds_i) in
<Bonds<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
{
for (uid_j, bonds_ij) in bonds_i {
bonds[uid_i as usize].push((uid_j, I32F32::from_num(bonds_ij)));
}
}
bonds
}
/// Output unnormalized bonds in [n, n] matrix, input bonds are assumed to be column max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_bonds(netuid: u16) -> Vec<Vec<I32F32>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<I32F32>> = vec![vec![I32F32::from_num(0.0); n]; n];
for (uid_i, bonds_i) in
<Bonds<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
{
for (uid_j, bonds_ij) in bonds_i {
bonds[uid_i as usize][uid_j as usize] = I32F32::from_num(bonds_ij);
}
}
bonds
}
}