Skip to content

Commit

Permalink
Test TestCalcNetworkLosses (#180)
Browse files Browse the repository at this point in the history
- Scale by 1e18
- Fix loss initialization logic
- Fix naive/combined variable switch
- test

---------

Co-authored-by: Kenny P <17100641+kpeluso@users.noreply.github.com>
  • Loading branch information
br4e and kpeluso authored Apr 24, 2024
1 parent b9d6f13 commit 5d10002
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 24 deletions.
13 changes: 13 additions & 0 deletions x/emissions/keeper/inference_synthesis/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ type Loss = alloraMath.Dec
type Weight = alloraMath.Dec
type InferenceValue = alloraMath.Dec
type Stake = cosmosMath.Uint

const (
oneE18 = "1000000000000000000"
)

func AlloraOneE18() (alloraMath.Dec, error) {
oneE18, err := alloraMath.NewDecFromString(oneE18)
return oneE18, err
}

func CosmosUintOneE18() cosmosMath.Uint {
return cosmosMath.NewUintFromString(oneE18)
}
65 changes: 49 additions & 16 deletions x/emissions/keeper/inference_synthesis/network_losses.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ func CalcNetworkLosses(
if err != nil {
return emissions.ValueBundle{}, err
}
oneE18, err := AlloraOneE18()
if err != nil {
return emissions.ValueBundle{}, err
}
stakeAmount, err = stakeAmount.Quo(oneE18)
if err != nil {
return emissions.ValueBundle{}, err
}

// Update combined loss with reputer reported loss and stake
nextCombinedLoss, err := RunningWeightedAvgUpdate(&runningWeightedCombinedLoss, stakeAmount, report.ValueBundle.CombinedValue, epsilon)
if err != nil {
Expand All @@ -123,9 +132,11 @@ func CalcNetworkLosses(
// Not all reputers may have reported losses on the same set of inferers => important that the code below doesn't assume that!
// Update inferer losses
for _, loss := range report.ValueBundle.InfererValues {
runningWeightedInfererLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
if runningWeightedInfererLosses[loss.Worker] == nil {
runningWeightedInfererLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
}
}

nextAvg, err := RunningWeightedAvgUpdate(runningWeightedInfererLosses[loss.Worker], stakeAmount, loss.Value, epsilon)
Expand All @@ -138,10 +149,13 @@ func CalcNetworkLosses(

// Update forecaster losses
for _, loss := range report.ValueBundle.ForecasterValues {
runningWeightedForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
if runningWeightedForecasterLosses[loss.Worker] == nil {
runningWeightedForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
}
}

nextAvg, err := RunningWeightedAvgUpdate(runningWeightedForecasterLosses[loss.Worker], stakeAmount, loss.Value, epsilon)
if err != nil {
fmt.Println("Error updating running weighted average for forecaster: ", err)
Expand All @@ -156,13 +170,15 @@ func CalcNetworkLosses(
fmt.Println("Error updating running weighted average for naive loss: ", err)
return emissions.ValueBundle{}, err
}
runningWeightedCombinedLoss = nextNaiveLoss
runningWeightedNaiveLoss = nextNaiveLoss

// Update one-out inferer losses
for _, loss := range report.ValueBundle.OneOutInfererValues {
runningWeightedOneOutInfererLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
if runningWeightedOneOutInfererLosses[loss.Worker] == nil {
runningWeightedOneOutInfererLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
}
}
nextAvg, err := RunningWeightedAvgUpdate(runningWeightedOneOutInfererLosses[loss.Worker], stakeAmount, loss.Value, epsilon)
if err != nil {
Expand All @@ -174,10 +190,13 @@ func CalcNetworkLosses(

// Update one-out forecaster losses
for _, loss := range report.ValueBundle.OneOutForecasterValues {
runningWeightedOneOutForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
if runningWeightedOneOutForecasterLosses[loss.Worker] == nil {
runningWeightedOneOutForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
}
}

nextAvg, err := RunningWeightedAvgUpdate(runningWeightedOneOutForecasterLosses[loss.Worker], stakeAmount, loss.Value, epsilon)
if err != nil {
fmt.Println("Error updating running weighted average for one-out forecaster: ", err)
Expand All @@ -188,10 +207,13 @@ func CalcNetworkLosses(

// Update one-in forecaster losses
for _, loss := range report.ValueBundle.OneOutForecasterValues {
runningWeightedOneInForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
if runningWeightedOneInForecasterLosses[loss.Worker] == nil {
runningWeightedOneInForecasterLosses[loss.Worker] = &WorkerRunningWeightedLoss{
SumWeight: alloraMath.MustNewDecFromString("0"),
Loss: alloraMath.MustNewDecFromString("0"),
}
}

nextAvg, err := RunningWeightedAvgUpdate(runningWeightedOneInForecasterLosses[loss.Worker], stakeAmount, loss.Value, epsilon)
if err != nil {
fmt.Println("Error updating running weighted average for one-in forecaster: ", err)
Expand Down Expand Up @@ -260,6 +282,17 @@ func CalcCombinedNetworkLoss(
fmt.Println("Error converting stake to Dec: ", err)
return Loss{}, err
}
if err != nil {
return Loss{}, err
}
oneE18, err := AlloraOneE18()
if err != nil {
return Loss{}, err
}
stakeAmount, err = stakeAmount.Quo(oneE18)
if err != nil {
return Loss{}, err
}
// Update combined loss with reputer reported loss and stake
nextCombinedLoss, err := RunningWeightedAvgUpdate(
&runningWeightedCombinedLoss,
Expand Down
Loading

0 comments on commit 5d10002

Please sign in to comment.