Skip to content

Commit 2498862

Browse files
authored
Add week 08 materials (#10)
1 parent 1d2adbc commit 2498862

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2917
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM gcc:latest as build
2+
3+
WORKDIR /gtest_build
4+
5+
RUN apt-get update && \
6+
apt-get install -y cmake
7+
8+
WORKDIR /
9+
RUN wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip && \
10+
unzip libtorch-shared-with-deps-latest.zip && \
11+
rm libtorch-shared-with-deps-latest.zip
12+
13+
ADD ./src /app/src
14+
15+
WORKDIR /app/build
16+
17+
RUN cmake -DCMAKE_PREFIX_PATH=/libtorch ../src && \
18+
cmake --build . --config Release
19+
20+
WORKDIR /app
21+
COPY vgg16.pt .
22+
RUN cp /app/build/vgg_cpp .
23+
24+
ENTRYPOINT ["./vgg_cpp"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
To run Docker
2+
3+
```bash
4+
docker build -t torchcpp:latest .
5+
docker run torchcpp:latest
6+
```
7+
8+
9+
More
10+
11+
* TorchScript - https://pytorch.org/docs/stable/jit.html
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
project(custom_ops)
3+
4+
find_package(Torch REQUIRED)
5+
6+
add_executable(vgg_cpp main.cpp)
7+
target_link_libraries(vgg_cpp "${TORCH_LIBRARIES}")
8+
set_property(TARGET vgg_cpp PROPERTY CXX_STANDARD 14)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <torch/script.h> // One-stop header.
2+
3+
#include <iostream>
4+
#include <memory>
5+
6+
int main(int argc, const char* argv[]) {
7+
torch::jit::script::Module module;
8+
try {
9+
module = torch::jit::load("./vgg16.pt");
10+
11+
std::vector<torch::jit::IValue> inputs;
12+
inputs.push_back(torch::ones({1, 3, 224, 224}));
13+
14+
at::Tensor output = module.forward(inputs).toTensor();
15+
16+
float* itr = (float*)output.data_ptr();
17+
std::cout << output.sizes() << std::endl;
18+
19+
int argmax = 0;
20+
float value = 0;
21+
for (int i =0; i < output.sizes()[0] * output.sizes()[1]; i++) {
22+
if(*itr > value) {
23+
value = *itr;
24+
argmax = i;
25+
}
26+
itr++;
27+
}
28+
29+
std::cout << "Answer = " << argmax << " with logit = " << value << std::endl;
30+
}
31+
catch (const c10::Error& e) {
32+
std::cerr << "error loading the model\n";
33+
return -1;
34+
}
35+
36+
std::cout << "ok\n";
37+
}

0 commit comments

Comments
 (0)