Skip to content

Commit

Permalink
adding hugging face
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgift committed Mar 27, 2023
1 parent c477c88 commit 8a12643
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,12 @@ Code here: https://github.com/nogibjj/assimilate-openai/tree/main/rust-curl-ope
* Verified GCP Cloud Run works, code here: https://github.com/nogibjj/rust-mlops-template/tree/main/webdocker
![Screenshot 2023-03-07 at 4 31 02 PM](https://user-images.githubusercontent.com/58792/223558711-efedd835-0ddf-4727-854c-1928bb0a57de.png)
#### ONNX Series
* Next session try to build a GPT 2 CLI from ORT: https://github.com/pykeio/ort
#### References
Expand Down
1 change: 0 additions & 1 deletion distroless-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM rust:1.67.1 as build-env
ENV RUSTFLAGS="-C link-arg=-s -C lto -C codegen-units=1"
WORKDIR /app
COPY . /app
RUN cargo build --release
Expand Down
11 changes: 11 additions & 0 deletions hfqa/Cargo.toml
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"
13 changes: 13 additions & 0 deletions hfqa/Makefile
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
64 changes: 64 additions & 0 deletions hfqa/README.md
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], (
```



43 changes: 43 additions & 0 deletions hfqa/src/main.rs
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"),
}
}
3 changes: 1 addition & 2 deletions pytorch-rust-docker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rust-bert = "0.19.0"
anyhow = "1.0.40"
tch = "~0.8.0"
tch = "0.11.0"
27 changes: 24 additions & 3 deletions pytorch-rust-docker/Dockerfile
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"]
2 changes: 1 addition & 1 deletion pytorch-rust-docker/Makefile
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
Expand Down
44 changes: 43 additions & 1 deletion pytorch-rust-docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,50 @@

### Docker Examples

The size is HUGE: `6.06GB`

You can also run inside a docker container:

* `docker build -t pytorch-rust-docker .`
* `docker run -it pytorch-rust-docker`
* Next inside the container run: `cargo run -- resnet18.ot Walking_tiger_female.jpg`
* Next inside the container run: `cargo run -- resnet18.ot Walking_tiger_female.jpg`

One possibility is to try this, but a key issue is the `libtorch_cpu.so`

```bash
# 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 . .

# 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"]
```

** Warning Broken for Now **

To run this, you could do something like this:
`docker run -it --rm 61503cf1e0b3 ./app/pytorch_rust_docker resnet18.ot Walking_tiger_female.jpg`


0 comments on commit 8a12643

Please sign in to comment.