Skip to content

Commit d4d608e

Browse files
AbrilRBSczoido
andauthored
Libtorch regression example (#213)
* Sketch libtorch test * Readme for libtorch example * cppstd 17 * Testing * It's gnu... * It's gnu..., but not in windows! * DOn't build for windows * Use update --------- Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
1 parent 722968b commit d4d608e

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

examples/libraries/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
### [Using libcurl to download an image and stb to read it](libcurl/download_image)
1010

1111
### [Using libcurl to download an image and stb to read it (color version with fmt)](libcurl/ascii_art_color)
12+
13+
### [Using libtorch](libtorch/regression)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Libtorch example with Conan
2+
3+
This example shows how to use Libtorch with Conan package manager to build a simple regression model.
4+
5+
The regression source code is not hosted in this repository,
6+
we use [the official PyTorch examples repository](https://github.com/pytorch/examples/tree/main/cpp/regression),
7+
so make sure to clone it first and then navigate to the `cpp/regression` folder:
8+
9+
```bash
10+
$ git clone https://github.com/pytorch/examples/tree/main/cpp/regression
11+
$ cd cpp/regression
12+
```
13+
14+
Then, copy the `conanfile.txt` from this directory to the `cpp/regression` folder,
15+
and finally run Conan and CMake as usual:
16+
17+
```bash
18+
$ conan install --build=missing
19+
$ cmake --preset conan-release # conan-default for Windows
20+
$ cmake --build --preset conan-release
21+
$ ./build/Release/regression
22+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import platform
2+
import shutil
3+
4+
from test.examples_tools import run, chdir
5+
6+
print("Libtorch regression example with Conan")
7+
8+
run("git clone --depth 1 https://github.com/pytorch/examples.git")
9+
shutil.copy("conanfile.txt", "examples/cpp/regression/conanfile.txt")
10+
11+
with chdir("examples/cpp/regression"):
12+
cppstd = "17" if platform.system() == "Windows" else "gnu17"
13+
run(f"conan install -b=missing -s compiler.cppstd={cppstd} --update")
14+
15+
if platform.system() == "Windows":
16+
run("cmake --preset conan-default")
17+
# Don't build on Windows. CI's msvc can't build this
18+
else:
19+
run("cmake --preset conan-release")
20+
run("cmake --build --preset conan-release")
21+
22+
# Only execute this in macos in the CI, there are some issues with the generated binary and the
23+
# GitHub images
24+
if platform.system() == "Darwin":
25+
run("./build/Release/regression")
26+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[requires]
2+
libtorch/[*]
3+
4+
[generators]
5+
CMakeToolchain
6+
CMakeDeps
7+
8+
[layout]
9+
cmake_layout

0 commit comments

Comments
 (0)