Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas committed Aug 11, 2024
1 parent 5337558 commit 166fda1
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 73 deletions.
41 changes: 21 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"build",
"cli",
"core",
"cuda",
"derive",
"eval",
"helper",
Expand All @@ -15,7 +16,6 @@ members = [
"recursion/gnark-cli",
"recursion/gnark-ffi",
"recursion/program",
"server",
"sdk",
"zkvm/*",
]
Expand Down Expand Up @@ -46,6 +46,7 @@ sp1-build = { path = "build", version = "1.1.1" }
sp1-derive = { path = "derive", version = "1.1.1" }
sp1-core = { path = "core", version = "1.1.1" }
sp1-cli = { path = "cli", version = "1.1.1", default-features = false }
sp1-cuda = { path = "cuda", version = "1.1.1", default-features = false }
sp1-eval = { path = "eval", version = "1.1.0", default-features = false }
sp1-helper = { path = "helper", version = "1.1.1", default-features = false }
sp1-primitives = { path = "primitives", version = "1.1.1" }
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion server/Cargo.toml → cuda/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "sp1-server"
name = "sp1-cuda"
description = "SP1 is a performant, 100% open-source, contributor-friendly zkVM."
readme = "../README.md"
version = { workspace = true }
Expand Down
File renamed without changes.
File renamed without changes.
71 changes: 45 additions & 26 deletions server/src/lib.rs → cuda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub mod proto {
}

use core::time::Duration;
use std::io::BufReader;
use std::io::Read;
use std::io::Write;
use std::process::Command;
use std::process::Stdio;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -32,7 +35,7 @@ use twirp::Client;
/// This is currently used to provide experimental support for GPU hardware acceleration.
///
/// **WARNING**: This is an experimental feature and may not work as expected.
pub struct SP1ProverServer {
pub struct SP1CudaProver {
/// The gRPC client to communicate with the container.
client: Client,
/// The name of the container.
Expand Down Expand Up @@ -81,7 +84,7 @@ pub struct WrapRequestPayload {
pub reduced_proof: SP1ReduceProof<InnerSC>,
}

impl SP1ProverServer {
impl SP1CudaProver {
/// Creates a new [SP1Prover] that runs inside a Docker container and returns a
/// [SP1ProverClient] that can be used to communicate with the container.
pub fn new() -> Self {
Expand All @@ -95,30 +98,45 @@ impl SP1ProverServer {
// Pull the docker image if it's not present.
Command::new("sudo")
.args(["docker", "pull", image_name])
.status()
.output()
.expect("failed to pull docker image");

// Start the docker container.
let rust_log_level = std::env::var("RUST_LOG").unwrap_or("none".to_string());
let mut child = Command::new("sudo")
.args([
"docker",
"run",
"-e",
format!("RUST_LOG={}", rust_log_level).as_str(),
"-p",
"3000:3000",
"--rm",
"--runtime=nvidia",
"--gpus",
"all",
"--name",
container_name,
image_name,
])
.stdout(Stdio::piped())
.spawn()
.expect("failed to start Docker container");

let stdout = child.stdout.take().unwrap();
std::thread::spawn(move || {
Command::new("sudo")
.args([
"docker",
"run",
"-e",
format!("RUST_LOG={}", rust_log_level).as_str(),
"-p",
"3000:3000",
"--rm",
"--runtime=nvidia",
"--gpus",
"all",
"--name",
container_name,
image_name,
])
.status()
.expect("failed to start Docker container");
let mut reader = BufReader::new(stdout);
let mut buffer = [0; 1024];
loop {
match reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => {
std::io::stdout().write_all(&buffer[..n]).unwrap();
std::io::stdout().flush().unwrap();
}
Err(_) => break,
}
}
});

// Kill the container on control-c.
Expand Down Expand Up @@ -157,7 +175,7 @@ impl SP1ProverServer {
}
});

SP1ProverServer {
SP1CudaProver {
client: Client::from_base_url(
Url::parse("http://localhost:3000/twirp/").expect("failed to parse url"),
)
Expand Down Expand Up @@ -269,13 +287,13 @@ impl SP1ProverServer {
}
}

impl Default for SP1ProverServer {
impl Default for SP1CudaProver {
fn default() -> Self {
Self::new()
}
}

impl Drop for SP1ProverServer {
impl Drop for SP1CudaProver {
fn drop(&mut self) {
if !self.cleaned_up.load(Ordering::SeqCst) {
tracing::debug!("dropping SP1ProverClient, cleaning up...");
Expand All @@ -289,6 +307,7 @@ impl Drop for SP1ProverServer {
fn cleanup_container(container_name: &str) {
if let Err(e) = Command::new("sudo")
.args(["docker", "rm", "-f", container_name])
.stdin(Stdio::null())
.status()
{
eprintln!("failed to remove container: {}", e);
Expand All @@ -307,14 +326,14 @@ mod tests {

use crate::SP1Stdin;
use crate::{proto::api::ProverServiceClient, ProveCoreRequestPayload};
use crate::{CompressRequestPayload, SP1ProverServer};
use crate::{CompressRequestPayload, SP1CudaProver};

#[test]
fn test_client() {
utils::setup_logger();

let prover = SP1Prover::<DefaultProverComponents>::new();
let client = SP1ProverServer::new();
let client = SP1CudaProver::new();
let (pk, vk) = prover.setup(FIBONACCI_ELF);

println!("proving core");
Expand Down
File renamed without changes.
Loading

0 comments on commit 166fda1

Please sign in to comment.