Skip to content

Commit

Permalink
feat: prover metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Apr 26, 2024
1 parent 28e2291 commit aaf5350
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 112 deletions.
26 changes: 25 additions & 1 deletion yarn-project/circuit-types/src/stats/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ export const Metrics = [
events: ['note-processor-caught-up'],
},
{
name: 'circuit_simulation_time_in_ms',
name: 'circuit_wasm_simulation_time_in_ms',
groupBy: 'circuit-name',
description: 'Time to run a circuit simulation using WASM.',
events: ['circuit-simulation'],
},
{
name: 'circuit_native_simulation_time_in_ms',
groupBy: 'circuit-name',
description: 'Time to run a circuit simulation.',
events: ['circuit-simulation'],
Expand All @@ -128,6 +134,24 @@ export const Metrics = [
description: 'Size of the outputs (ie public inputs) from a circuit simulation.',
events: ['circuit-simulation'],
},
{
name: 'circuit_proving_time_in_ms',
groupBy: 'circuit-name',
description: 'Time to prove circuit execution.',
events: ['circuit-proving'],
},
{
name: 'circuit_proof_size_in_bytes',
groupBy: 'circuit-name',
description: 'Size of the proof produced by a circuit.',
events: ['circuit-proving'],
},
{
name: 'proof_verification_time_in_ms',
groupBy: 'circuit-name',
description: 'Time to verify proof.',
events: ['proof-verification'],
},
{
name: 'tx_size_in_bytes',
groupBy: 'classes-registered',
Expand Down
52 changes: 39 additions & 13 deletions yarn-project/circuit-types/src/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,54 @@ export type NodeSyncedChainHistoryStats = {
dbSize: number;
};

export type CircuitName =
| 'base-parity'
| 'root-parity'
| 'base-rollup'
| 'private-kernel-init'
| 'private-kernel-ordering'
| 'root-rollup'
| 'merge-rollup'
| 'private-kernel-inner'
| 'public-kernel-setup'
| 'public-kernel-app-logic'
| 'public-kernel-teardown'
| 'public-kernel-tail';

/** Stats for circuit simulation. */
export type CircuitSimulationStats = {
/** name of the event. */
eventName: 'circuit-simulation';
/** Name of the circuit. */
circuitName:
| 'base-parity'
| 'root-parity'
| 'base-rollup'
| 'private-kernel-init'
| 'private-kernel-ordering'
| 'root-rollup'
| 'merge-rollup'
| 'private-kernel-inner'
| 'public-kernel-setup'
| 'public-kernel-app-logic'
| 'public-kernel-teardown'
| 'public-kernel-tail';
circuitName: CircuitName;
/** Duration in ms. */
duration: number;
/** Size in bytes of circuit inputs. */
inputSize: number;
/** Size in bytes of circuit outputs (aka public inputs). */
outputSize: number;
};

export type CircuitProvingStats = {
/** Name of the event. */
eventName: 'circuit-proving';
/** Name of the circuit. */
circuitName: CircuitName;
/** Duration in ms. */
duration: number;
/** Size in bytes of circuit inputs. */
inputSize: number;
/** Size in bytes of circuit outputs (aka public inputs). */
outputSize: number;
/** Size in bytes of the proof. */
proofSize: number;
};

export type ProofVerificationStats = {
eventName: 'proof-verification';
circuitName: CircuitName;
duration: number;
proofSize: number;
};

/** Stats for an L2 block built by a sequencer. */
Expand Down Expand Up @@ -206,6 +230,8 @@ export type Stats =
| L1PublishStats
| NodeSyncedChainHistoryStats
| CircuitSimulationStats
| CircuitProvingStats
| ProofVerificationStats
| L2BlockBuiltStats
| L2BlockHandledStats
| NoteProcessorCaughtUpStats
Expand Down
94 changes: 8 additions & 86 deletions yarn-project/prover-client/src/orchestrator/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
L2Block,
MerkleTreeId,
type ProcessedTx,
type PublicKernelRequest,
PublicKernelType,
type TxEffect,
toTxEffect,
Expand All @@ -14,7 +13,6 @@ import {
type ProvingResult,
type ProvingTicket,
} from '@aztec/circuit-types/interfaces';
import { type CircuitSimulationStats } from '@aztec/circuit-types/stats';
import {
type BaseOrMergeRollupPublicInputs,
BaseParityInputs,
Expand All @@ -39,7 +37,6 @@ import { createDebugLogger } from '@aztec/foundation/log';
import { promiseWithResolvers } from '@aztec/foundation/promise';
import { type Tuple } from '@aztec/foundation/serialize';
import { sleep } from '@aztec/foundation/sleep';
import { Timer } from '@aztec/foundation/timer';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { inspect } from 'util';
Expand Down Expand Up @@ -309,7 +306,7 @@ export class ProvingOrchestrator {
private deferredProving<T>(
provingState: ProvingState | undefined,
request: (signal: AbortSignal) => Promise<T>,
callback: (result: T, durationMs: number) => void | Promise<void>,
callback: (result: T) => void | Promise<void>,
) {
if (!provingState?.verifyState()) {
logger.debug(`Not enqueuing job, state no longer valid`);
Expand All @@ -328,10 +325,7 @@ export class ProvingOrchestrator {
return;
}

const timer = new Timer();
const result = await request(controller.signal);
const duration = timer.ms();

if (!provingState?.verifyState()) {
logger.debug(`State no longer valid, discarding result`);
return;
Expand All @@ -343,7 +337,7 @@ export class ProvingOrchestrator {
return;
}

await callback(result, duration);
await callback(result);
} catch (err) {
if (err instanceof AbortedError) {
// operation was cancelled, probably because the block was cancelled
Expand All @@ -365,39 +359,6 @@ export class ProvingOrchestrator {
setImmediate(safeJob);
}

private emitCircuitSimulationStats(
circuitName: CircuitSimulationStats['circuitName'] | null,
inputSize: number,
outputSize: number,
duration: number,
) {
const stats: CircuitSimulationStats | undefined = circuitName
? {
eventName: 'circuit-simulation',
circuitName,
duration,
inputSize,
outputSize,
}
: undefined;
logger.debug(`Simulated ${circuitName} circuit duration=${duration}ms`, stats);
}

private getPublicKernelCircuitName(request: PublicKernelRequest) {
switch (request.type) {
case PublicKernelType.SETUP:
return 'public-kernel-setup';
case PublicKernelType.APP_LOGIC:
return 'public-kernel-app-logic';
case PublicKernelType.TEARDOWN:
return 'public-kernel-teardown';
case PublicKernelType.TAIL:
return 'public-kernel-tail';
default:
return null;
}
}

// Updates the merkle trees for a transaction. The first enqueued job for a transaction
private async prepareBaseRollupInputs(
provingState: ProvingState | undefined,
Expand Down Expand Up @@ -473,14 +434,7 @@ export class ProvingOrchestrator {
this.deferredProving(
provingState,
signal => this.prover.getBaseRollupProof(tx.baseRollupInputs, signal),
([publicInputs, proof], duration) => {
this.emitCircuitSimulationStats(
'base-rollup',
tx.baseRollupInputs.toBuffer().length,
publicInputs.toBuffer().length,
duration,
);

([publicInputs, proof]) => {
validatePartialState(publicInputs.end, tx.treeSnapshots);
const currentLevel = provingState.numMergeLevels + 1n;
this.storeAndExecuteNextMergeLevel(provingState, currentLevel, index, [publicInputs, proof]);
Expand All @@ -504,13 +458,7 @@ export class ProvingOrchestrator {
this.deferredProving(
provingState,
signal => this.prover.getMergeRollupProof(inputs, signal),
([publicInputs, proof], duration) => {
this.emitCircuitSimulationStats(
'merge-rollup',
inputs.toBuffer().length,
publicInputs.toBuffer().length,
duration,
);
([publicInputs, proof]) => {
this.storeAndExecuteNextMergeLevel(provingState, level, index, [publicInputs, proof]);
},
);
Expand Down Expand Up @@ -540,14 +488,7 @@ export class ProvingOrchestrator {
this.deferredProving(
provingState,
signal => this.prover.getRootRollupProof(inputs, signal),
([publicInputs, proof], duration) => {
this.emitCircuitSimulationStats(
'root-rollup',
inputs.toBuffer().length,
publicInputs.toBuffer().length,
duration,
);

([publicInputs, proof]) => {
provingState.rootRollupPublicInputs = publicInputs;
provingState.finalProof = proof;

Expand All @@ -565,13 +506,7 @@ export class ProvingOrchestrator {
this.deferredProving(
provingState,
signal => this.prover.getBaseParityProof(inputs, signal),
([publicInputs, proof], duration) => {
this.emitCircuitSimulationStats(
'base-parity',
inputs.toBuffer().length,
publicInputs.toBuffer().length,
duration,
);
([publicInputs, proof]) => {
const rootInput = new RootParityInput(proof, publicInputs);
provingState.setRootParityInputs(rootInput, index);
if (provingState.areRootParityInputsReady()) {
Expand All @@ -590,13 +525,7 @@ export class ProvingOrchestrator {
this.deferredProving(
provingState,
signal => this.prover.getRootParityProof(inputs, signal),
async ([publicInputs, proof], duration) => {
this.emitCircuitSimulationStats(
'root-parity',
inputs.toBuffer().length,
publicInputs.toBuffer().length,
duration,
);
async ([publicInputs, proof]) => {
const rootInput = new RootParityInput(proof, publicInputs);
provingState!.finalRootParityInput = rootInput;
await this.checkAndEnqueueRootRollup(provingState);
Expand Down Expand Up @@ -711,14 +640,7 @@ export class ProvingOrchestrator {
return this.prover.getPublicKernelProof(request, signal);
}
},
([_, proof], duration) => {
this.emitCircuitSimulationStats(
this.getPublicKernelCircuitName(request),
request.inputs.toBuffer().length,
0,
duration,
);

([_, proof]) => {
const nextKernelRequest = txProvingState.getNextPublicKernelFromKernelProof(functionIndex, proof);
// What's the status of the next kernel?
if (nextKernelRequest.code === TX_PROVING_CODE.NOT_READY) {
Expand Down
29 changes: 29 additions & 0 deletions yarn-project/prover-client/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
Fr,
type KernelCircuitPublicInputs,
type MergeRollupInputs,
type ParityPublicInputs,
Expand All @@ -17,6 +18,7 @@ import {
} from '@aztec/circuits.js';
import { randomBytes } from '@aztec/foundation/crypto';
import { createDebugLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
import {
ServerCircuitArtifacts,
type ServerProtocolArtifact,
Expand All @@ -39,6 +41,12 @@ import { type WitnessMap } from '@noir-lang/types';
import * as fs from 'fs/promises';

import { BB_RESULT, generateKeyForNoirCircuit, generateProof, verifyProof } from '../bb/execute.js';
import {
circuitTypeToCircuitName,
emitCircuitProvingStats,
emitCircuitSimulationStats,
emitProofVerificationStats,
} from '../util.js';
import { type CircuitProver, KernelArtifactMapping } from './interface.js';

const logger = createDebugLogger('aztec:bb-prover');
Expand Down Expand Up @@ -214,7 +222,15 @@ export class BBNativeRollupProver implements CircuitProver {

logger.debug(`Generating witness data for ${circuitType}`);

const simTimer = new Timer();
const outputWitness = await simulator.simulateCircuit(witnessMap, artifact);
emitCircuitSimulationStats(
circuitTypeToCircuitName(circuitType),
simTimer.ms(),
witnessMap.size * Fr.SIZE_IN_BYTES,
outputWitness.size * Fr.SIZE_IN_BYTES,
logger,
);

// Now prove the circuit from the generated witness
logger.debug(`Proving ${circuitType}...`);
Expand All @@ -236,6 +252,16 @@ export class BBNativeRollupProver implements CircuitProver {
// Read the proof and then cleanup up our temporary directory
const proofBuffer = await fs.readFile(provingResult.path!);

// does not include reading the proof from disk above because duration comes from the bb wrapper
emitCircuitProvingStats(
circuitTypeToCircuitName(circuitType),
provingResult.duration,
witnessMap.size * Fr.SIZE_IN_BYTES,
outputWitness.size * Fr.SIZE_IN_BYTES,
proofBuffer.length,
logger,
);

await fs.rm(bbWorkingDirectory, { recursive: true, force: true });

logger.info(
Expand Down Expand Up @@ -268,6 +294,9 @@ export class BBNativeRollupProver implements CircuitProver {
throw new Error(errorMessage);
}

// does not include the fs.rm above because duration comes from the bb wrapper
emitProofVerificationStats(circuitTypeToCircuitName(circuitType), result.duration, proof.buffer.length, logger);

logger.info(`Successfully verified ${circuitType} proof in ${result.duration} ms`);
}

Expand Down
Loading

0 comments on commit aaf5350

Please sign in to comment.