-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "hfqa" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
rust-bert = "0.20.0" | ||
clap = {version="4.0.32", features=["derive"]} | ||
anyhow = "1.0.51" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
format: | ||
cargo fmt --quiet | ||
|
||
lint: | ||
cargo clippy --quiet | ||
|
||
test: | ||
cargo test --quiet | ||
|
||
run: | ||
cargo run | ||
|
||
all: format lint test run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
```bash | ||
@noahgift ➜ /workspaces/rust-mlops-template/hfqa (main) $ cargo run -- question --help | ||
Finished dev [unoptimized + debuginfo] target(s) in 0.16s | ||
Running `target/debug/hfqa question --help` | ||
Usage: hfqa question --qname <QNAME> <CONTEXT> | ||
|
||
Arguments: | ||
<CONTEXT> | ||
|
||
Options: | ||
-q, --qname <QNAME> | ||
-h, --help Print help | ||
-V, --version Print version | ||
``` | ||
|
||
An example of it working: | ||
|
||
```cargo run -- question -q "What is my name" "I am a robot in the future"``` | ||
|
||
|
||
## Action Items | ||
|
||
Potential Solution for Static Linking? | ||
|
||
``` | ||
While building PyTorch from source, make sure to enable static linking by setting the USE_STATIC_DISPATCH and BUILD_SHARED_LIBS options to ON. You can use the following CMake options: | ||
-DBUILD_SHARED_LIBS=OFF -DUSE_STATIC_DISPATCH=ON | ||
``` | ||
|
||
Compile your Rust code with the PyTorch static libraries: | ||
In your Rust project, create a build.rs file to specify the location of the PyTorch static libraries and include files. Replace /path/to/pytorch with the actual path to your PyTorch source directory. | ||
|
||
```rust | ||
// build.rs | ||
use std::env; | ||
|
||
fn main() { | ||
let pytorch_path = "/path/to/pytorch"; | ||
let torch_lib_path = format!("{}/torch/lib", pytorch_path); | ||
let include_path = format!("{}/torch/include", pytorch_path); | ||
|
||
println!("cargo:rustc-link-search=native={}", torch_lib_path); | ||
println!("cargo:rustc-link-lib=static=torch"); | ||
println!("cargo:rustc-link-lib=static=torch_cpu"); | ||
println!("cargo:rustc-link-lib=static=c10"); | ||
env::set_var("C_INCLUDE_PATH", &include_path); | ||
} | ||
``` | ||
|
||
Then test it | ||
|
||
```rust | ||
use tch::nn; | ||
use tch::Tensor; | ||
|
||
fn main() { | ||
let vs = nn::VarStore::new(tch::Device::Cpu); | ||
let linear = nn::linear(vs.root(), 5, 2, Default::default()); | ||
let input = Tensor::randn(&[3, 5], ( | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//A command-line tool to do Question and Answer with Hugging Face | ||
use clap::Parser; | ||
extern crate anyhow; | ||
use rust_bert::pipelines::question_answering::{QaInput, QuestionAnsweringModel}; | ||
|
||
#[derive(Parser)] | ||
#[clap(version = "1.0", author = "Noah Gift", about = "Question and Answer with Hugging Face")] | ||
struct Cli { | ||
#[clap(subcommand)] | ||
command: Option<Commands>, | ||
} | ||
|
||
#[derive(Parser)] | ||
enum Commands { | ||
#[clap(version = "1.0", author = "Noah Gift")] | ||
Question { | ||
#[clap(short, long)] | ||
qname: String, | ||
context: String, | ||
}, | ||
} | ||
|
||
fn logic(qname: String, context: String) -> anyhow::Result<()> { | ||
let qa_model = QuestionAnsweringModel::new(Default::default())?; | ||
|
||
let question = qname; | ||
let context = context; | ||
let answers = qa_model.predict(&[QaInput { question, context }], 1, 32); | ||
for answer in answers { | ||
println!("Answer: {:?}", answer); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
let args = Cli::parse(); | ||
match args.command { | ||
Some(Commands::Question { qname, context }) => { | ||
logic(qname, context).unwrap(); | ||
} | ||
None => println!("No subcommand was used"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
# First stage: builder | ||
FROM rust:latest as builder | ||
|
||
# Set environment variables | ||
ENV APP pytorch-rust-docker | ||
|
||
# Set working directory | ||
WORKDIR /usr/src/$APP | ||
|
||
# Copy the content of the current directory into the working directory | ||
COPY . . | ||
RUN apt-get update && rm -rf /var/lib/apt/lists/* | ||
RUN cargo install --path . | ||
RUN cargo build -j 6 | ||
|
||
# Install required dependencies | ||
RUN apt-get update && \ | ||
apt-get install -y libtorch-dev && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# Build the application | ||
RUN cargo build --release | ||
|
||
# Second stage: distroless | ||
FROM gcr.io/distroless/cc | ||
|
||
# Copy the binary from the builder stage | ||
COPY --from=builder /usr/src/pytorch-rust-docker/target/release/pytorch_rust_docker /app/pytorch_rust_docker | ||
|
||
# Set the entrypoint for the container | ||
ENTRYPOINT ["/app/pytorch_rust_docker"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
install: | ||
cargo clean &&\ | ||
cargo build -j 1 | ||
cargo build -j 6 | ||
|
||
format: | ||
cargo fmt --quiet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters