Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a few Rust doc examples to Executor and ExecutorEnv #572

Merged
merged 12 commits into from May 24, 2023

Conversation

3lkn
Copy link
Contributor

@3lkn 3lkn commented May 19, 2023

No description provided.

@3lkn 3lkn requested review from tzerrell and flaub May 19, 2023 03:00
@github-actions
Copy link

Benchmark for Linux-cuda 955df88

Click to hide benchmark
Test Base PR %
fib/100/execute 18.2±0.98ms 18.2±1.36ms 0.00%
fib/100/prove 1033.5±28.36ms 896.6±12.60ms -13.25%
fib/100/total 1050.9±20.12ms 993.6±68.81ms -5.45%
fib/1000/execute 32.3±1.16ms 31.8±1.29ms -1.55%
fib/1000/prove 1063.2±34.50ms 1017.4±93.47ms -4.31%
fib/1000/total 1127.2±40.49ms 1118.3±33.88ms -0.79%
fib/10000/execute 172.8±2.89ms 170.0±3.40ms -1.62%
fib/10000/prove 3.9±0.09s 3.7±0.27s -5.13%
fib/10000/total 4.0±0.14s 4.0±0.17s 0.00%

Benchmark for Linux-default 955df88

Click to hide benchmark
Test Base PR %
fib/100/execute 55.6±0.29ms 55.5±0.22ms -0.18%
fib/100/prove 5.1±0.04s 5.1±0.02s 0.00%
fib/100/total 5.2±0.02s 5.2±0.03s 0.00%
fib/1000/execute 77.8±0.83ms 76.7±0.43ms -1.41%
fib/1000/prove 5.2±0.03s 5.1±0.02s -1.92%
fib/1000/total 5.2±0.02s 5.2±0.03s 0.00%
fib/10000/execute 291.8±1.06ms 289.2±1.27ms -0.89%
fib/10000/prove 21.5±0.23s 21.3±0.11s -0.93%
fib/10000/total 21.7±0.13s 21.6±0.12s -0.46%

Benchmark for macOS-default 955df88

Click to hide benchmark
Test Base PR %
fib/100/execute 29.0±0.05ms 28.5±0.09ms -1.72%
fib/100/prove 3.7±0.04s 3.7±0.05s 0.00%
fib/100/total 3.7±0.06s 3.7±0.04s 0.00%
fib/1000/execute 38.4±0.09ms 37.4±0.08ms -2.60%
fib/1000/prove 3.7±0.04s 3.7±0.06s 0.00%
fib/1000/total 3.8±0.03s 3.8±0.07s 0.00%
fib/10000/execute 132.6±0.11ms 129.2±0.17ms -2.56%
fib/10000/prove 15.3±0.14s 15.3±0.13s 0.00%
fib/10000/total 15.4±0.07s 15.4±0.08s 0.00%

Benchmark for macOS-metal 955df88

Click to hide benchmark
Test Base PR %
fib/100/execute 28.3±0.22ms 28.3±0.17ms 0.00%
fib/100/prove 847.4±4.18ms 843.4±4.28ms -0.47%
fib/100/total 898.2±7.62ms 897.2±5.16ms -0.11%
fib/1000/execute 37.5±0.13ms 37.4±0.09ms -0.27%
fib/1000/prove 868.2±5.47ms 863.8±3.76ms -0.51%
fib/1000/total 927.5±7.19ms 924.3±8.04ms -0.35%
fib/10000/execute 129.8±0.23ms 129.3±0.31ms -0.39%
fib/10000/prove 3.3±0.01s 3.3±0.01s 0.00%
fib/10000/total 3.5±0.01s 3.5±0.01s 0.00%

@@ -151,6 +213,39 @@ impl<'a> ExecutorEnvBuilder<'a> {
}

/// Add initial input that can be read by the guest from stdin.
/// This step must occur before the executor environment is built.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment because structurally, it's not possible to add_input after the env has been built.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is trivially true, pulling it out.

Comment on lines 161 to 162
/// # ExecutorEnv,
/// # ExecutorEnvBuilder};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these commented out? Seems useful to have in the example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right -- going off of TimZ's feedback, I'm going to un-comment a majority of the omitted code, including includes.

risc0/zkvm/src/exec/env.rs Outdated Show resolved Hide resolved
/// # use risc0_zkvm::{
/// # ExecutorEnv,
/// # ExecutorEnvBuilder};
/// # use std::collections::HashMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed

Copy link
Contributor Author

@3lkn 3lkn May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Copied and pasted from prev example without pulling that out.

risc0/zkvm/src/exec/env.rs Outdated Show resolved Hide resolved
/// # use risc0_zkvm::{
/// # ExecutorEnv,
/// # ExecutorEnvBuilder,
/// serde::to_vec};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed

///
/// let a: u64 = 400;
///
/// let env = ExecutorEnv::builder().build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't seem especially useful

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of an example, add a redirection to other parts of the doc, e.g. "Combine with [ExecutorEnvBuilder] functions like [...add_input] to set up the environment and I/O data your guest needs. Finalize with [ExecutorEnvBuilder::build] to get a configured [ExecutorEnv]."

/// The executor environment holds configuration details that inform how the
/// guest environment is set up prior to guest program execution. This
/// includes initial guest program inputs, session size limits, segment size
/// limits, and environment variables.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding more context is good, but enumerating the fields in this description isn't good for maintenance and it doesn't actually help understand what this thing does any better (IMHO).

Copy link
Contributor Author

@3lkn 3lkn May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes a lot of sense, outdated explanatory text is still an update cost

Comment on lines 231 to 234
/// Most of the time, the best practice is to combine guest program inputs
/// into a single struct. For cases where you want to add inputs that
/// the guest will access using consecutive reads, you can call
/// `ExecutorEnvBuilder::add_input()` iteratively:
Copy link
Member

@flaub flaub May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making a prescription, I'd just say what the behavior of calling add_input multiple times is, which is to concatenate the inputs together

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the prescriptive first line but making note that guest can access concatenated input with consecutive reads, can take it out if that's still too prescriptive.

risc0/zkvm/src/exec/mod.rs Outdated Show resolved Hide resolved
/// deciding where a zkVM program should be split into [Segment]s and what
/// work will be done in each segment. This is the execution phase:
/// the guest program is executed to determine how its proof should be
/// divided into subparts.
pub fn new(env: ExecutorEnv<'a>, image: MemoryImage, pc: u32) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the PC is part of the MemoryImage, we should drop the pc argument here

Comment on lines 165 to 171
/// # use risc0_zkvm::{serde::to_vec, Executor, ExecutorEnv, Session};
/// # use risc0_zkvm_methods::{BENCH_ELF, bench::{BenchmarkSpec, SpecWithIters}};
/// #
/// # let spec = SpecWithIters(BenchmarkSpec::SimpleLoop, 1);
/// # let env = ExecutorEnv::builder()
/// # .add_input(&to_vec(&spec).unwrap())
/// # .build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think showing this is useful for this example

Copy link
Member

@tzerrell tzerrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad to see examples for these, thanks!

@flaub pointed out specific places where this would be helpful, but in general with code snippets I think showing context is one of the main benefits. More concretely, when documenting a function, the automatically generated signature documentation gives the reader a pretty good sense of what the exact line where you call the function looks like. What isn't generated that we need to manually write is what happens around that line: are there expectations or common techniques for other functions to be called ahead of time? Is there a standard or common way to construct one or more of the inputs? What sorts of things can you do with the result of the function? Is any cleanup needed? What do you need to import to be able to use the function in typical ways?

There's definitely a tradeoff here, for the general reasons that concision is good (readers won't bother to read, or read carefully, snippets that are too long). Nevertheless, in general I would hide less of the supporting code relative to what you're doing here. This is especially true when you only give one snippet: if you have three snippets and they each use the same set up code, you sometimes only need to show it on the first snippet, but if you only have one snippet -- well you still have to show it on the first snippet, but that's the only snippet.

/// # use risc0_zkvm::{
/// # ExecutorEnv,
/// # ExecutorEnvBuilder,
/// serde::to_vec};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use is dangling. Probably just un-hide the entire use block, but if you really want to show just to_vec, make it a separate statement, e.g.

# use risc0_zkvm::{
#   ExecutorEnv,
#   ExecutorEnvBuilder
# };
use risc0_zkvm::serde::to_vec;

Comment on lines 188 to 191
/// # let env = ExecutorEnv::builder()
/// # .add_input(&to_vec(&spec).unwrap())
/// # .build();
/// # let mut exec = Executor::from_elf(env, BENCH_ELF).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think showing the env & exec gives useful context. If the spec and BENCH_ELF names seem confusing (and they probably are), this could be further upgraded by hidden lines like let EXAMPLE_ELF = BENCH_ELF and naming spec to instead by example_input or something similar.

3lkn and others added 4 commits May 19, 2023 14:39
Co-authored-by: Frank Laub <flaub@risc0.com>
Co-authored-by: Frank Laub <flaub@risc0.com>
Co-authored-by: Frank Laub <flaub@risc0.com>
@3lkn
Copy link
Contributor Author

3lkn commented May 20, 2023

Thank you for writing this out, these are excellent guidelines.

@github-actions
Copy link

Benchmark for Linux-cuda

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for Linux-default ae6ff3b

Click to hide benchmark
Test Base PR %
fib/100/execute 57.1±0.31ms 56.6±0.25ms -0.88%
fib/100/prove 5.1±0.03s 5.1±0.02s 0.00%
fib/100/total 5.1±0.03s 5.1±0.02s 0.00%
fib/1000/execute 78.9±1.56ms 77.6±0.62ms -1.65%
fib/1000/prove 5.1±0.02s 5.1±0.02s 0.00%
fib/1000/total 5.2±0.03s 5.2±0.02s 0.00%
fib/10000/execute 289.8±1.99ms 289.0±0.97ms -0.28%
fib/10000/prove 21.3±0.15s 21.3±0.13s 0.00%
fib/10000/total 21.6±0.11s 21.4±0.13s -0.93%

Benchmark for macOS-default ae6ff3b

Click to hide benchmark
Test Base PR %
fib/100/execute 29.1±0.11ms 28.2±0.04ms -3.09%
fib/100/prove 3.7±0.04s 3.7±0.06s 0.00%
fib/100/total 3.7±0.06s 3.7±0.05s 0.00%
fib/1000/execute 37.9±0.05ms 37.5±0.08ms -1.06%
fib/1000/prove 3.7±0.07s 3.7±0.06s 0.00%
fib/1000/total 3.8±0.05s 3.7±0.04s -2.63%
fib/10000/execute 132.3±0.14ms 129.8±0.11ms -1.89%
fib/10000/prove 15.3±0.18s 15.2±0.07s -0.65%
fib/10000/total 15.6±0.17s 15.4±0.22s -1.28%

Benchmark for macOS-metal ae6ff3b

Click to hide benchmark
Test Base PR %
fib/100/execute 28.4±0.03ms 28.2±0.04ms -0.70%
fib/100/prove 849.0±3.14ms 846.4±3.01ms -0.31%
fib/100/total 904.0±9.31ms 901.4±5.89ms -0.29%
fib/1000/execute 37.6±0.15ms 37.4±0.10ms -0.53%
fib/1000/prove 867.5±4.19ms 867.1±4.31ms -0.05%
fib/1000/total 934.1±5.59ms 926.4±4.87ms -0.82%
fib/10000/execute 129.5±0.31ms 128.9±0.32ms -0.46%
fib/10000/prove 3.4±0.01s 3.3±0.01s -2.94%
fib/10000/total 3.5±0.01s 3.5±0.01s 0.00%

@github-actions
Copy link

Benchmark for Linux-cuda af58725

Click to hide benchmark
Test Base PR %
fib/100/execute 18.1±0.29ms 17.9±0.46ms -1.10%
fib/100/prove 1085.4±43.23ms 1051.8±65.65ms -3.10%
fib/100/total 1170.8±83.37ms 1076.9±35.40ms -8.02%
fib/1000/execute 35.8±0.87ms 35.2±0.35ms -1.68%
fib/1000/prove 1164.5±105.10ms 1096.8±45.99ms -5.81%
fib/1000/total 1139.2±21.18ms 1132.6±49.73ms -0.58%
fib/10000/execute 211.3±2.31ms 206.8±1.98ms -2.13%
fib/10000/prove 4.6±0.28s 3.9±0.12s -15.22%
fib/10000/total 4.3±0.21s 4.2±0.30s -2.33%

Benchmark for Linux-default

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for macOS-default af58725

Click to hide benchmark
Test Base PR %
fib/100/execute 30.0±0.15ms 29.0±0.03ms -3.33%
fib/100/prove 3.7±0.07s 3.7±0.08s 0.00%
fib/100/total 3.7±0.07s 3.7±0.09s 0.00%
fib/1000/execute 40.3±0.10ms 39.1±0.08ms -2.98%
fib/1000/prove 3.7±0.04s 3.7±0.06s 0.00%
fib/1000/total 3.8±0.07s 3.7±0.07s -2.63%
fib/10000/execute 144.1±0.42ms 139.6±0.20ms -3.12%
fib/10000/prove 15.3±0.13s 15.3±0.11s 0.00%
fib/10000/total 15.5±0.17s 15.4±0.12s -0.65%

Benchmark for macOS-metal

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

@github-actions
Copy link

Benchmark for Linux-cuda

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for Linux-default

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for macOS-default b93e304

Click to hide benchmark
Test Base PR %
fib/100/execute 29.8±0.07ms 29.0±0.03ms -2.68%
fib/100/prove 3.7±0.04s 3.7±0.04s 0.00%
fib/100/total 3.8±0.05s 3.7±0.08s -2.63%
fib/1000/execute 40.2±0.10ms 39.1±0.11ms -2.74%
fib/1000/prove 3.7±0.04s 3.7±0.06s 0.00%
fib/1000/total 3.8±0.05s 3.7±0.04s -2.63%
fib/10000/execute 143.6±0.30ms 140.0±0.18ms -2.51%
fib/10000/prove 15.2±0.09s 15.2±0.11s 0.00%
fib/10000/total 15.5±0.06s 15.4±0.16s -0.65%

Benchmark for macOS-metal

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

@3lkn 3lkn requested review from tzerrell and flaub May 23, 2023 20:58
Copy link
Member

@tzerrell tzerrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! One suggested revision, one typesetting suggestion

risc0/zkvm/src/exec/env.rs Outdated Show resolved Hide resolved
///
/// let a: u64 = 400;
///
/// let env = ExecutorEnv::builder().build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of an example, add a redirection to other parts of the doc, e.g. "Combine with [ExecutorEnvBuilder] functions like [...add_input] to set up the environment and I/O data your guest needs. Finalize with [ExecutorEnvBuilder::build] to get a configured [ExecutorEnv]."

3lkn and others added 2 commits May 23, 2023 17:17
@3lkn 3lkn requested review from tzerrell and removed request for flaub May 24, 2023 00:22
@3lkn 3lkn enabled auto-merge (squash) May 24, 2023 00:28
@3lkn 3lkn merged commit 2d04249 into main May 24, 2023
12 checks passed
@3lkn 3lkn deleted the 3lkn/executor-examples branch May 24, 2023 00:37
@github-actions
Copy link

Benchmark for Linux-cuda

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for Linux-default

    <details open>
      <summary>Click to hide benchmark</summary>
      Benchmarks have changed between the two branches, unable to diff.
    </details>

Benchmark for macOS-default 0c3eaa8

Click to hide benchmark
Test Base PR %
fib/100/execute 29.9±0.04ms 29.3±0.08ms -2.01%
fib/100/prove 3.7±0.04s 3.7±0.07s 0.00%
fib/100/total 3.7±0.05s 3.7±0.06s 0.00%
fib/1000/execute 40.3±0.23ms 39.4±0.13ms -2.23%
fib/1000/prove 3.7±0.06s 3.7±0.08s 0.00%
fib/1000/total 3.8±0.04s 3.7±0.06s -2.63%
fib/10000/execute 146.1±0.22ms 143.1±0.23ms -2.05%
fib/10000/prove 15.3±0.20s 15.2±0.13s -0.65%
fib/10000/total 15.4±0.09s 15.4±0.16s 0.00%

Benchmark for macOS-metal 0c3eaa8

Click to hide benchmark
Test Base PR %
fib/100/execute 29.2±0.10ms 28.9±0.04ms -1.03%
fib/100/prove 852.0±3.80ms 851.3±4.92ms -0.08%
fib/100/total 905.0±5.58ms 900.3±5.07ms -0.52%
fib/1000/execute 39.2±0.09ms 39.1±0.07ms -0.26%
fib/1000/prove 867.3±4.98ms 867.0±2.60ms -0.03%
fib/1000/total 933.5±4.92ms 932.5±5.48ms -0.11%
fib/10000/execute 139.8±0.25ms 139.8±0.26ms 0.00%
fib/10000/prove 3.3±0.01s 3.3±0.01s 0.00%
fib/10000/total 3.5±0.01s 3.5±0.02s 0.00%

flaub added a commit that referenced this pull request May 24, 2023
* adding examples to ExecutorEnv and Executor

* Update risc0/zkvm/src/exec/env.rs

Co-authored-by: Frank Laub <flaub@risc0.com>

* Update risc0/zkvm/src/exec/env.rs

Co-authored-by: Frank Laub <flaub@risc0.com>

* Update risc0/zkvm/src/exec/mod.rs

Co-authored-by: Frank Laub <flaub@risc0.com>

* updating executor/executor env doc examples after code review

* Update risc0/zkvm/src/exec/env.rs

Co-authored-by: Tim Zerrell <tim.zerrell@risczero.com>

---------

Co-authored-by: Frank Laub <flaub@risc0.com>
Co-authored-by: Tim Zerrell <tim.zerrell@risczero.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants