inference_synthesis

package
v0.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

Inference Synthesis

This package contains the logic from the Inference Synthesis Section of the litepaper, which combine current inferences, forecasts, previous losses and current regrets into current network losses and new latest regrets per worker or combination of workers (and topic).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccumulateWeights

func AccumulateWeights(
	inference *emissions.Inference,
	weight alloraMath.Dec,
	noPriorRegret bool,
	allPeersAreNew bool,
	runningUnnormalizedI_i alloraMath.Dec,
	sumWeights alloraMath.Dec,
) (alloraMath.Dec, alloraMath.Dec, error)

func CalcNetworkLosses

func CalcNetworkLosses(
	stakesByReputer map[Worker]Stake,
	reputerReportedLosses emissions.ReputerValueBundles,
	epsilon alloraMath.Dec,
) (emissions.ValueBundle, error)

Assumes stakes are all positive

func CalcWeightFromNormalizedRegret

func CalcWeightFromNormalizedRegret(
	normalizedRegret alloraMath.Dec,
	maxNormalizedRegret alloraMath.Dec,
	pNorm alloraMath.Dec,
	cNorm alloraMath.Dec,
) (alloraMath.Dec, error)

func ComputeAndBuildEMRegret

func ComputeAndBuildEMRegret(
	lossA Loss,
	lossB Loss,
	previousRegret Regret,
	alpha alloraMath.Dec,
	blockHeight BlockHeight,
	noPreviousRegret bool,
) (emissions.TimestampedValue, error)

func ConvertForecastImpliedInferencesToArrays added in v0.2.7

func ConvertForecastImpliedInferencesToArrays(
	forecastImpliedInferenceByWorker map[string]*types.Inference,
) []*types.WorkerAttributedValue

func ConvertValueBundleToNetworkLossesByWorker

func ConvertValueBundleToNetworkLossesByWorker(
	valueBundle emissions.ValueBundle,
) networkLossesByWorker

Convert a ValueBundle to a networkLossesByWorker

func ConvertWeightsToArrays added in v0.2.7

func ConvertWeightsToArrays(weights map[Worker]Weight) []*types.RegretInformedWeight

func CosmosIntOneE18

func CosmosIntOneE18() cosmosMath.Int

func FilterNoncesWithinEpochLength

func FilterNoncesWithinEpochLength(n emissions.Nonces, blockHeight, epochLength int64) emissions.Nonces

Filter nonces that are within the epoch length of the current block height

func GetCalcSetNetworkRegrets

func GetCalcSetNetworkRegrets(
	ctx sdk.Context,
	k keeper.Keeper,
	topicId TopicId,
	networkLosses emissions.ValueBundle,
	nonce emissions.Nonce,
	alpha alloraMath.Dec,
) error

Calculate the new network regrets by taking EMAs between the previous network regrets and the new regrets admitted by the inputted network losses It is assumed the workers are uniquely represented in the network losses

func GetLatestNetworkInference added in v0.2.8

func GetLatestNetworkInference(
	ctx sdk.Context,
	k keeper.Keeper,
	topicId TopicId,
) (
	*emissions.ValueBundle,
	map[string]*emissions.Inference,
	map[string]alloraMath.Dec,
	map[string]alloraMath.Dec,
	error,
)

func GetNetworkInferencesAtBlock

func GetNetworkInferencesAtBlock(
	ctx sdk.Context,
	k keeper.Keeper,
	topicId TopicId,
	inferencesNonce BlockHeight,
	previousLossNonce BlockHeight,
) (
	*emissions.ValueBundle,
	map[string]*emissions.Inference,
	map[string]alloraMath.Dec,
	map[string]alloraMath.Dec,
	error,
)

Calculates all network inferences in the set I_i given historical state (e.g. regrets) and data from workers (e.g. inferences, forecast-implied inferences) as of a specified block height

func Logger

func Logger(ctx sdk.Context) log.Logger

func MakeMapFromForecasterToTheirForecast

func MakeMapFromForecasterToTheirForecast(forecasts []*emissions.Forecast) map[Worker]*emissions.Forecast

Create a map from worker address to their inference or forecast-implied inference

func MakeMapFromInfererToTheirInference

func MakeMapFromInfererToTheirInference(inferences []*emissions.Inference) map[Worker]*emissions.Inference

Create a map from worker address to their inference or forecast-implied inference

func SelectTopNReputerNonces

func SelectTopNReputerNonces(reputerRequestNonces *emissions.ReputerRequestNonces, N int, currentBlockHeight int64, groundTruthLag int64) []*emissions.ReputerRequestNonce

Select the top N latest reputer nonces

func SelectTopNWorkerNonces

func SelectTopNWorkerNonces(workerNonces emissions.Nonces, N int) []*emissions.Nonce

Select the top N latest worker nonces

func SortByBlockHeight

func SortByBlockHeight(r []*emissions.ReputerRequestNonce)

Types

type BlockHeight

type BlockHeight = int64

type InferenceValue

type InferenceValue = alloraMath.Dec

type InferersNewStatus

type InferersNewStatus int
const (
	InferersAllNew InferersNewStatus = iota
	InferersAllNewExceptOne
	InferersNotNew
)

type Loss

type Loss = alloraMath.Dec

type NetworkInferenceBuilder

type NetworkInferenceBuilder struct {
	// contains filtered or unexported fields
}

func NewNetworkInferenceBuilderFromSynthRequest

func NewNetworkInferenceBuilderFromSynthRequest(
	req SynthRequest,
) (*NetworkInferenceBuilder, error)

func (*NetworkInferenceBuilder) Build

Calculates all network inferences in the set I_i given historical state (e.g. regrets) and data from workers (e.g. inferences, forecast-implied inferences). Could improve this with Builder pattern, as for other instances of generated ValueBundles.

func (*NetworkInferenceBuilder) CalcAndSetNetworkInferences

func (b *NetworkInferenceBuilder) CalcAndSetNetworkInferences() *NetworkInferenceBuilder

func (*NetworkInferenceBuilder) SetCombinedValue

func (b *NetworkInferenceBuilder) SetCombinedValue() *NetworkInferenceBuilder

Calculates the network combined inference I_i, Equation 9

func (*NetworkInferenceBuilder) SetForecasterValues

func (b *NetworkInferenceBuilder) SetForecasterValues() *NetworkInferenceBuilder

Map forecast-implied inferences to a WorkerAttributedValue array and set

func (*NetworkInferenceBuilder) SetInfererValues

func (b *NetworkInferenceBuilder) SetInfererValues() *NetworkInferenceBuilder

Map inferences to a WorkerAttributedValue array and set

func (*NetworkInferenceBuilder) SetNaiveValue

Calculates the network naive inference I^-_i

func (*NetworkInferenceBuilder) SetOneInValues

func (b *NetworkInferenceBuilder) SetOneInValues() *NetworkInferenceBuilder

Set all one-in inferences that are possible given the provided input Assumed that there is at most 1 inference per worker. Also assume that there is at most 1 forecast-implied inference per worker.

func (*NetworkInferenceBuilder) SetOneOutForecasterValues

func (b *NetworkInferenceBuilder) SetOneOutForecasterValues() *NetworkInferenceBuilder

Set all one-out-forecaster inferences that are possible given the provided input Assume that there is at most 1 forecast-implied inference per forecaster Loop over all forecast-implied inferences and withold one, then calculate the network inference less that witheld value

func (*NetworkInferenceBuilder) SetOneOutInfererValues

func (b *NetworkInferenceBuilder) SetOneOutInfererValues() *NetworkInferenceBuilder

Set all one-out-inferer inferences that are possible given the provided input Assumed that there is at most 1 inference per inferer Loop over all inferences and withold one, then calculate the network inference less that witheld inference This involves recalculating the forecast-implied inferences for each withheld inferer

type Regret

type Regret = alloraMath.Dec

type RegretInformedWeights

type RegretInformedWeights struct {
	// contains filtered or unexported fields
}

Need to differentiate between the two types of regrets because workers may complete tasks for both roles and may have different regrets for those different roles

type RunningWeightedLoss

type RunningWeightedLoss struct {
	UnnormalizedWeightedLoss Loss
	SumWeight                Weight
}

func RunningWeightedAvgUpdate

func RunningWeightedAvgUpdate(
	runningWeightedAvg *RunningWeightedLoss,
	nextWeight Weight,
	nextValue Weight,
) (RunningWeightedLoss, error)

Update the running information needed to calculate weighted loss per worker

type Stake

type Stake = cosmosMath.Int

type StatefulRegret

type StatefulRegret struct {
	// contains filtered or unexported fields
}

type StdDevRegrets

type StdDevRegrets struct {
	// contains filtered or unexported fields
}

type SynthPalette

type SynthPalette struct {
	Ctx     sdk.Context
	K       keeper.Keeper
	Logger  log.Logger
	TopicId TopicId
	// Should use this as a source of truth regarding for which inferers to have data calculated
	// i.e. if an inferer is not present here, calculate a network inference without their data
	// Must be unique values
	Inferers          []Worker
	InferenceByWorker map[Worker]*emissions.Inference
	// Must respect the order of sister `inferers` property
	InfererRegrets map[Worker]*StatefulRegret
	// Should use this as a source of truth regarding for which forecasters to have data calculated
	// i.e. if an forecaster is not present here, calculate a network inference without their data
	// Must be unique values
	Forecasters                      []Worker
	ForecastByWorker                 map[Worker]*emissions.Forecast
	ForecastImpliedInferenceByWorker map[Worker]*emissions.Inference
	// Must respect the order of sister `forecasters` property
	ForecasterRegrets   map[Worker]*StatefulRegret
	InferersNewStatus   InferersNewStatus
	SingleNotNewInferer Worker
	NetworkCombinedLoss Loss
	Epsilon             alloraMath.Dec
	Tolerance           alloraMath.Dec
	PNorm               alloraMath.Dec
	CNorm               alloraMath.Dec
}

func (*SynthPalette) BootstrapRegretData

func (p *SynthPalette) BootstrapRegretData() error

Bootstraps xRegrets, allxsAreNew (x="inferer"|"forecasters") for the inferers and forecasters in the palette Just requires these props:: ctx, k, topicId, inferers, forecasts

func (*SynthPalette) CalcForecastImpliedInferences

func (p *SynthPalette) CalcForecastImpliedInferences() (map[Worker]*emissionstypes.Inference, error)

Calculate the forecast-implied inferences I_ik given inferences, forecasts and network losses. Calculates R_ijk, w_ijk, and I_ik for each forecast k and forecast element (forcast of worker loss) j

Forecast without inference => weight in calculation of I_ik and I_i set to 0. Use latest available regret R_i-1,l Inference without forecast => only weight in calculation of I_ik set to 0 A value of 0 => no inference corresponded to any of the forecasts from a forecaster

Requires: forecasts, inferenceByWorker, allInferersAreNew, networkCombinedLoss, epsilon, pNorm, cNorm Updates: forecastImpliedInferenceByWorker

func (*SynthPalette) CalcWeightedInference

func (p *SynthPalette) CalcWeightedInference(weights RegretInformedWeights) (InferenceValue, error)

Calculates network combined inference I_i, network per worker regret R_i-1,l, and weights w_il from the litepaper: I_i = Σ_l w_il I_il / Σ_l w_il w_il = φ'_p(\hatR_i-1,l) \hatR_i-1,l = R_i-1,l / |max_{l'}(R_i-1,l')| given inferences, forecast-implied inferences, and network regrets

func (*SynthPalette) CalcWeightsGivenWorkers

func (p *SynthPalette) CalcWeightsGivenWorkers() (RegretInformedWeights, error)

Given the current set of inferers and forecasters in the palette, calculate their weights using the current regrets

func (SynthPalette) Clone

func (p SynthPalette) Clone() SynthPalette

Clone creates a deep copy of the SynthPalette.

func (*SynthPalette) GetForecasterRegretsSlice

func (p *SynthPalette) GetForecasterRegretsSlice() []alloraMath.Dec

func (*SynthPalette) GetInfererRegretsSlice

func (p *SynthPalette) GetInfererRegretsSlice() []alloraMath.Dec

func (*SynthPalette) UpdateForecastImpliedInferences

func (p *SynthPalette) UpdateForecastImpliedInferences() error

Calculate the forecast-implied inferences I_ik given inferences, forecasts and network losses. See docs of CalcForecastImpliedInferences for more details on calculation. This calculates and then sets the forecastImpliedInferenceByWorker property of the palette. Requires: forecasts, inferenceByWorker, allInferersAreNew, networkCombinedLoss, epsilon, pNorm, cNorm Updates: forecastImpliedInferenceByWorker

type SynthPaletteFactory

type SynthPaletteFactory struct{}

func (*SynthPaletteFactory) BuildPaletteFromRequest

func (f *SynthPaletteFactory) BuildPaletteFromRequest(req SynthRequest) (SynthPalette, error)

Could use Builder pattern in the future to make this cleaner

type SynthRequest

type SynthRequest struct {
	Ctx                 sdk.Context
	K                   keeper.Keeper
	TopicId             TopicId
	Inferences          *emissions.Inferences
	Forecasts           *emissions.Forecasts
	NetworkCombinedLoss Loss
	Epsilon             alloraMath.Dec
	Tolerance           alloraMath.Dec
	PNorm               alloraMath.Dec
	CNorm               alloraMath.Dec
}

type TopicId

type TopicId = uint64

type Weight

type Weight = alloraMath.Dec

type Worker

type Worker = string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL