Skip to content

Commit

Permalink
Fix edge case zero combined loss (#277)
Browse files Browse the repository at this point in the history
- Fallback stake-weighted combined loss to `epsilon` if it's equal to
zero

Ticket:
https://linear.app/upshot/issue/ORA-1391/no-weights-in-one-reputerlosses-breaks-the-calcnetworklosses
  • Loading branch information
guilherme-brandao authored May 20, 2024
1 parent e5ed2aa commit eb79d58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions x/emissions/keeper/inference_synthesis/network_losses.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func CalcNetworkLosses(
fmt.Println("Error updating running weighted average for next combined loss: ", err)
return emissions.ValueBundle{}, err
}
// If the combined loss is zero, set it to epsilon to avoid divide by zero
if nextCombinedLoss.Loss.IsZero() {
nextCombinedLoss.Loss = epsilon
}
runningWeightedCombinedLoss = nextCombinedLoss

// Not all reputers may have reported losses on the same set of inferers => important that the code below doesn't assume that!
Expand Down Expand Up @@ -245,11 +249,14 @@ func CalcCombinedNetworkLoss(
)
if err != nil {
fmt.Println("Error updating running weighted average for combined loss: ", err)
return alloraMath.ZeroDec(), err
return Loss{}, err
}
runningWeightedCombinedLoss = nextCombinedLoss
}
}

// If the combined loss is zero, set it to epsilon to avoid divide by zero
if runningWeightedCombinedLoss.Loss.IsZero() {
runningWeightedCombinedLoss.Loss = epsilon
}
return runningWeightedCombinedLoss.Loss, nil
}
19 changes: 19 additions & 0 deletions x/emissions/keeper/inference_synthesis/network_losses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ func (s *InferenceSynthesisTestSuite) TestCalcCombinedNetworkLoss() {
expectedLoss: alloraMath.MustNewDecFromString(".000015456633"),
expectedErr: nil,
},
{
name: "One reputer reporting a zero combined loss",
stakesByReputer: map[inference_synthesis.Worker]cosmosMath.Uint{
"worker1": inference_synthesis.CosmosUintOneE18(), // 1 token
},
reportedLosses: &emissions.ReputerValueBundles{
ReputerValueBundles: []*emissions.ReputerValueBundle{
{
ValueBundle: &emissions.ValueBundle{
Reputer: "worker1",
CombinedValue: alloraMath.MustNewDecFromString("0"),
},
},
},
},
epsilon: alloraMath.MustNewDecFromString("1e-4"),
expectedLoss: alloraMath.MustNewDecFromString("1e-4"), // Should be equal to epsilon
expectedErr: nil,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit eb79d58

Please sign in to comment.