Skip to content

Commit

Permalink
Merge local and remote runs (#39)
Browse files Browse the repository at this point in the history
* few detected issues

* Removed env_logger which made logs not upload to heat

* fix endpoint for remote training

* Updated Burn and removed git patch for Burn

* Can now run for dev or prod.

Regenerated Cargo.lock to fix some vulnerabilities

Co-authored-by: Jonathan Richard <[email protected]>

* fix clippy

* Merged local/remote into same command

If the --runner flag is specified, the run will be remote on that runner

* Cleanup cli commands

* Do not permit to package project if repo is dirty (uncommited files)

* Changed version to latest commit hash

* update guide-cli readme

* fix lint

* fix lint with new rust version

* Add more doc in guide-cli

Fix problems linked to PR

---------

Co-authored-by: Jonathan Richard <[email protected]>
Co-authored-by: Jonathan Richard <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent eafdbfe commit 284a908
Show file tree
Hide file tree
Showing 27 changed files with 478 additions and 495 deletions.
44 changes: 43 additions & 1 deletion Cargo.lock

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

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ anyhow = "1.0.81"
clap = { version = "4.5.4", features = ["derive"] }
colored = "2.1.0"
derive-new = { version = "0.6.0", default-features = false }
derive_more = { version = "0.99.18", features = [
"display",
], default-features = false }
env_logger = "0.11.3"
log = "0.4.21"
once_cell = "1.19.0"
proc-macro2 = { version = "1.0.86" }
Expand Down
14 changes: 8 additions & 6 deletions crates/heat-sdk-cli-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ pub fn heat(args: TokenStream, item: TokenStream) -> TokenStream {
quote! {}
};

quote! {
let code = quote! {
#[allow(dead_code)]
#item

#flag_register
}
.into()
};

code.into()
}

#[proc_macro_attribute]
Expand Down Expand Up @@ -177,8 +178,9 @@ pub fn heat_cli_main(args: TokenStream, item: TokenStream) -> TokenStream {
}
};

quote! {
let code = quote! {
#item
}
.into()
};

code.into()
}
3 changes: 1 addition & 2 deletions crates/heat-sdk-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ flate2 = { version = "1.0.30", default-features = false, features = ["zlib"] }
tar = { version = "0.4.40", default-features = false }
walkdir = "2"
ignore = "0.4.22"
gix = { version = "0.66.0", default-features = false, features = ["dirwalk"]}
gix = { version = "0.66.0", default-features = false, features = ["dirwalk", "status"]}
unicase = "2.7.0"
itertools = "0.13.0"
lazycell = "1.3.0"
serde-untagged = "0.1.6"
serde_ignored = "0.1.1"
Expand Down
3 changes: 2 additions & 1 deletion crates/heat-sdk-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ pub struct CliArgs {
#[derive(Subcommand, Debug)]
#[command(arg_required_else_help = true)]
pub enum Commands {
/// {local|remote} : Run a training or inference locally or trigger a remote run.
/// Run a training or inference locally or trigger a remote run.
#[command(subcommand)]
Run(cli_commands::run::RunLocationType),

/// Package your project for running on a remote machine.
Package(cli_commands::package::PackageArgs),
// todo
// Ls(),
Expand Down
116 changes: 66 additions & 50 deletions crates/heat-sdk-cli/src/cli_commands/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
use crate::context::HeatCliContext;
use crate::registry::Flag;
use crate::{print_err, print_success};
use clap::Parser;
use heat_sdk::{
client::{HeatClient, HeatClientConfig, HeatCredentials},
schemas::{HeatCodeMetadata, ProjectPath, RegisteredHeatFunction},
};
use heat_sdk::client::{HeatClient, HeatClientConfig, HeatCredentials};
use heat_sdk::schemas::{HeatCodeMetadata, ProjectPath, RegisteredHeatFunction};
use quote::ToTokens;

#[derive(Parser, Debug)]
pub struct PackageArgs {
/// The Heat project ID
/// The Heat project path
// todo: support project name and creating a project if it doesn't exist
#[clap(
short = 'p',
long = "project",
required = true,
help = "<required> The Heat project ID."
help = "The Heat project path. Ex: test/Default-Project"
)]
project_path: String,
/// The Heat API key
#[clap(
short = 'k',
long = "key",
required = true,
help = "<required> The Heat API key."
)]
#[clap(short = 'k', long = "key", required = true, help = "The Heat API key.")]
key: String,
/// The Heat API endpoint
#[clap(
short = 'e',
long = "endpoint",
help = "The Heat API endpoint.",
default_value = "http://127.0.0.1:9001"
)]
pub heat_endpoint: String,
}

pub(crate) fn handle_command(args: PackageArgs, context: HeatCliContext) -> anyhow::Result<()> {
let last_commit_hash = get_last_commit_hash()?;

let heat_client = create_heat_client(
&args.key,
context.get_api_endpoint().as_str(),
&args.project_path,
);

let crates = crate::util::cargo::package::package(
&context.get_artifacts_dir_path(),
context.package_name(),
)?;

let flags = crate::registry::get_flags();
let registered_functions = get_registered_functions(&flags);

let heat_metadata = HeatCodeMetadata {
functions: registered_functions,
};

let project_version = heat_client.upload_new_project_version(
context.package_name(),
heat_metadata,
crates,
&last_commit_hash,
)?;

print_success!("New project version uploaded: {}", project_version);

Ok(())
}

fn create_heat_client(api_key: &str, url: &str, project_path: &str) -> HeatClient {
Expand All @@ -48,37 +68,33 @@ fn create_heat_client(api_key: &str, url: &str, project_path: &str) -> HeatClien
.expect("Should connect to the Heat server and create a client")
}

pub(crate) fn handle_command(args: PackageArgs, context: HeatCliContext) -> anyhow::Result<()> {
let heat_client = create_heat_client(&args.key, &args.heat_endpoint, &args.project_path);

let crates = crate::util::cargo::package::package(
&context.get_artifacts_dir_path(),
context.package_name(),
)?;

let flags = crate::registry::get_flags();
fn get_registered_functions(flags: &[Flag]) -> Vec<RegisteredHeatFunction> {
flags
.iter()
.map(|flag| {
// function token stream to readable string
let itemfn = syn_serde::json::from_slice::<syn::ItemFn>(flag.token_stream)
.expect("Should be able to parse token stream.");
let syn_tree: syn::File = syn::parse2(itemfn.into_token_stream())
.expect("Should be able to parse token stream.");
let code_str = prettyplease::unparse(&syn_tree);
RegisteredHeatFunction {
mod_path: flag.mod_path.to_string(),
fn_name: flag.fn_name.to_string(),
proc_type: flag.proc_type.to_string(),
code: code_str,
}
})
.collect()
}

let mut registered_functions = Vec::<RegisteredHeatFunction>::new();
for flag in flags {
// function token stream to readable string
let itemfn = syn_serde::json::from_slice::<syn::ItemFn>(flag.token_stream)
.expect("Should be able to parse token stream.");
let syn_tree: syn::File =
syn::parse2(itemfn.into_token_stream()).expect("Should be able to parse token stream.");
let code_str = prettyplease::unparse(&syn_tree);
registered_functions.push(RegisteredHeatFunction {
mod_path: flag.mod_path.to_string(),
fn_name: flag.fn_name.to_string(),
proc_type: flag.proc_type.to_string(),
code: code_str,
});
fn get_last_commit_hash() -> anyhow::Result<String> {
let repo = gix::discover(".")?;
let last_commit = repo.head()?.peel_to_commit_in_place()?.id();
if repo.is_dirty()? {
print_err!("Latest git commit: {}", last_commit);
anyhow::bail!("Repo is dirty. Please commit or stash your changes before packaging.");
}

let heat_metadata = HeatCodeMetadata {
functions: registered_functions,
};

heat_client.upload_new_project_version(context.package_name(), heat_metadata, crates)?;

Ok(())
Ok(last_commit.to_string())
}
15 changes: 15 additions & 0 deletions crates/heat-sdk-cli/src/cli_commands/run/inference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use clap::Parser;

use crate::context::HeatCliContext;

/// Run an inference locally.
/// Not yet supported.
#[derive(Parser, Debug)]
pub struct InferenceRunArgs {}

pub(crate) fn handle_command(
_args: InferenceRunArgs,
_context: HeatCliContext,
) -> anyhow::Result<()> {
todo!("Local inference is not yet supported")
}
27 changes: 0 additions & 27 deletions crates/heat-sdk-cli/src/cli_commands/run/local/inference.rs

This file was deleted.

31 changes: 0 additions & 31 deletions crates/heat-sdk-cli/src/cli_commands/run/local/mod.rs

This file was deleted.

Loading

0 comments on commit 284a908

Please sign in to comment.