rusttorch 0.4.0

Build, train, and run neural networks in Rust with LibTorch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
name: CI

on:
  pull_request:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  dco:
    name: DCO
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Check out complete pull-request history
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Check introduced commits
        env:
          BASE_SHA: ${{ github.event.pull_request.base.sha }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha }}
          TRUSTED_DEPENDABOT_ACTOR: ${{ github.actor == 'dependabot[bot]' }}
        run: |
          if [ "$TRUSTED_DEPENDABOT_ACTOR" = "true" ]; then
            python3 scripts/check-dco.py --base "$BASE_SHA" --head "$HEAD_SHA" \
              --trusted-dependabot-actor
          else
            python3 scripts/check-dco.py --base "$BASE_SHA" --head "$HEAD_SHA"
          fi

  dependency-review:
    name: Dependency review
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Review dependency changes
        uses: actions/dependency-review-action@595b5aeba73380359d98a5e087f648dbb0edce1b # v4.7.3
        with:
          comment-summary-in-pr: never

  quality:
    name: Stable quality
    runs-on: ubuntu-latest
    env:
      LIBTORCH_USE_PYTORCH: "1"
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: "3.14"

      - name: Validate Python lock artifacts
        run: python3 scripts/check-python-lock.py

      - name: Set up UV
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
        with:
          version: "0.12.3"
          enable-cache: false

      - name: Validate Python dependency lock
        run: uv lock --check --offline --no-cache

      - name: Synchronize locked Python environment
        run: uv sync --frozen --no-cache

      - name: Export project Python environment
        run: |
          echo "$PWD/.venv/bin" >> "$GITHUB_PATH"
          echo "VIRTUAL_ENV=$PWD/.venv" >> "$GITHUB_ENV"

      - name: Set up stable Rust
        uses: actions-rust-lang/setup-rust-toolchain@46268bd060767258de96ed93c1251119784f2ab6 # v1.16.1
        with:
          toolchain: stable
          cache: false
          components: rustfmt, clippy

      - name: Export LibTorch library path
        run: |
          torch_lib="$(.venv/bin/python -c 'from pathlib import Path; import torch; print(Path(torch.__file__).resolve().parent / "lib")')"
          echo "LD_LIBRARY_PATH=${torch_lib}" >> "$GITHUB_ENV"

      - name: Build and audit the pinned LGPL FFmpeg runtime
        run: |
          python3 scripts/build-ffmpeg.py --prefix "$RUNNER_TEMP/rusttorch-ffmpeg"
          echo "FFMPEG_LIBS_DIR=$RUNNER_TEMP/rusttorch-ffmpeg/lib" >> "$GITHUB_ENV"
          echo "FFMPEG_INCLUDE_DIR=$RUNNER_TEMP/rusttorch-ffmpeg/include" >> "$GITHUB_ENV"
          echo "FFMPEG_LINK_MODE=dynamic" >> "$GITHUB_ENV"
          echo "LD_LIBRARY_PATH=$RUNNER_TEMP/rusttorch-ffmpeg/lib:$LD_LIBRARY_PATH" >> "$GITHUB_ENV"

      - name: Run Python policy tests
        run: .venv/bin/python -m unittest discover -s tests -p 'test_*.py' -v

      - name: Validate API census offline
        run: .venv/bin/python scripts/sync-pytorch-inventory.py --check

      - name: Validate compatibility ledger
        run: .venv/bin/python scripts/check-compatibility.py --check

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Check workspace
        run: cargo check --workspace --all-targets --locked --features rusttorch/full

      - name: Check rusttorch-data feature combinations
        run: |
          cargo check -p rusttorch-data --all-targets --locked --no-default-features
          cargo check -p rusttorch-data --all-targets --locked --no-default-features --features doc-only
          cargo check -p rusttorch-data --all-targets --locked --no-default-features --features download-libtorch
          cargo check -p rusttorch-data --all-targets --locked --all-features

      - name: Lint workspace
        run: cargo clippy --workspace --all-targets --locked --features rusttorch/full -- -D warnings

      - name: Test workspace
        run: cargo test --workspace --all-targets --locked --features rusttorch/full

      - name: Test documentation examples
        run: cargo test --workspace --doc --locked --features rusttorch/full

      - name: Run training checkpoint example
        run: cargo run --example training_checkpoint --locked

      - name: Enforce Python parity
        run: scripts/run-python-parity.sh

      - name: Build workspace documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc --workspace --no-deps --locked --features rusttorch/full

      - name: Build rusttorch-cli documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc -p rusttorch-cli --no-deps --locked

      - name: Build rusttorch-core documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: >-
          cargo doc -p rusttorch-core --no-deps --locked --no-default-features
          --features doc-only

      - name: Build rusttorch-data documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: >-
          cargo doc -p rusttorch-data --no-deps --locked --no-default-features
          --features doc-only

      - name: Check docs.rs configuration
        env:
          DOCS_RS: "1"
        run: >-
          cargo check -p rusttorch --all-targets --locked --no-default-features
          --features doc-only,full

      - name: Build docs.rs documentation
        env:
          DOCS_RS: "1"
          RUSTDOCFLAGS: -D warnings
        run: >-
          cargo doc -p rusttorch --no-deps --locked --no-default-features
          --features doc-only,full

      - name: Inspect rusttorch-core package
        run: cargo package -p rusttorch-core --locked --list | tee "$RUNNER_TEMP/rusttorch-core-package-files.txt"

      - name: Inspect rusttorch-data package
        run: cargo package -p rusttorch-data --locked --list | tee "$RUNNER_TEMP/rusttorch-data-package-files.txt"

      - name: Inspect rusttorch-codec package
        run: cargo package -p rusttorch-codec --locked --list | tee "$RUNNER_TEMP/rusttorch-codec-package-files.txt"

      - name: Inspect rusttorch-vision package
        run: cargo package -p rusttorch-vision --locked --list | tee "$RUNNER_TEMP/rusttorch-vision-package-files.txt"

      - name: Inspect rusttorch-audio package
        run: cargo package -p rusttorch-audio --locked --list | tee "$RUNNER_TEMP/rusttorch-audio-package-files.txt"

      - name: Inspect rusttorch-text package
        run: cargo package -p rusttorch-text --locked --list | tee "$RUNNER_TEMP/rusttorch-text-package-files.txt"

      - name: Inspect rusttorch-tabular package
        run: cargo package -p rusttorch-tabular --locked --list | tee "$RUNNER_TEMP/rusttorch-tabular-package-files.txt"

      - name: Inspect rusttorch package
        run: cargo package -p rusttorch --locked --list | tee "$RUNNER_TEMP/rusttorch-package-files.txt"

      - name: Inspect rusttorch-cli package
        run: cargo package -p rusttorch-cli --locked --list | tee "$RUNNER_TEMP/rusttorch-cli-package-files.txt"

      - name: Reject unsafe package contents
        run: |
          if grep -E '(^|/)(\.venv|target|__pycache__|libtorch)(/|$)|(^|/)(pyproject\.toml|uv\.lock)$|\.(pyc|pyo|so|dylib|dll)$' \
            "$RUNNER_TEMP/rusttorch-core-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-data-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-codec-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-vision-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-audio-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-text-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-tabular-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-package-files.txt" \
            "$RUNNER_TEMP/rusttorch-cli-package-files.txt"; then
            echo "error: package contains Python tooling or a generated/native environment artifact" >&2
            exit 1
          fi

      # Packaging needs Cargo 1.90+; the crate MSRV remains Rust 1.88 and is check-only below.
      - name: Verify workspace archives
        run: cargo package --workspace --locked

  msrv:
    name: MSRV / ${{ matrix.package }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package:
          - rusttorch-core
          - rusttorch-data
          - rusttorch-codec
          - rusttorch-vision
          - rusttorch-audio
          - rusttorch-text
          - rusttorch-tabular
          - rusttorch
          - rusttorch-cli
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Rust 1.88
        uses: actions-rust-lang/setup-rust-toolchain@46268bd060767258de96ed93c1251119784f2ab6 # v1.16.1
        with:
          toolchain: "1.88"
          cache: false

      - name: Check library package on the MSRV
        if: matrix.package != 'rusttorch-cli'
        run: >-
          cargo check -p ${{ matrix.package }} --all-targets --locked
          --no-default-features --features doc-only

      - name: Check all optional library features on the MSRV
        if: matrix.package == 'rusttorch'
        env:
          DOCS_RS: "1"
        run: cargo check -p rusttorch --all-targets --locked --no-default-features --features doc-only,full

      - name: Check CLI package on the MSRV
        if: matrix.package == 'rusttorch-cli'
        run: cargo check -p rusttorch-cli --all-targets --locked

  cli:
    name: CLI / ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up stable Rust
        uses: actions-rust-lang/setup-rust-toolchain@46268bd060767258de96ed93c1251119784f2ab6 # v1.16.1
        with:
          toolchain: stable
          cache: false

      - name: Test the setup CLI
        run: cargo test -p rusttorch-cli --all-targets --locked

  loader-platform:
    name: Data loader / ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: "3.14"

      - name: Set up UV
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
        with:
          version: "0.12.3"
          enable-cache: false

      - name: Synchronize locked Python environment
        run: uv sync --frozen --no-cache

      - name: Set up stable Rust
        uses: actions-rust-lang/setup-rust-toolchain@46268bd060767258de96ed93c1251119784f2ab6 # v1.16.1
        with:
          toolchain: stable
          cache: false

      - name: Export Unix LibTorch library path
        if: runner.os != 'Windows'
        shell: bash
        run: |
          echo "VIRTUAL_ENV=$PWD/.venv" >> "$GITHUB_ENV"
          echo "$PWD/.venv/bin" >> "$GITHUB_PATH"
          torch_lib="$(.venv/bin/python -c 'from pathlib import Path; import torch; print(Path(torch.__file__).resolve().parent / "lib")')"
          echo "LIBTORCH_USE_PYTORCH=1" >> "$GITHUB_ENV"
          if [ "$RUNNER_OS" = "macOS" ]; then
            echo "DYLD_LIBRARY_PATH=${torch_lib}" >> "$GITHUB_ENV"
          else
            echo "LD_LIBRARY_PATH=${torch_lib}" >> "$GITHUB_ENV"
          fi

      - name: Export Windows LibTorch library path
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          # Keep torch-sys's C++17 mode: Clang supports the header extensions
          # without treating the binding's `module` typedef as C++20 syntax.
          "CXX=clang-cl" | Out-File -FilePath $env:GITHUB_ENV -Append
          "CXXFLAGS=$env:CXXFLAGS /EHsc" | Out-File -FilePath $env:GITHUB_ENV -Append
          # This upstream MSBuild-only option is ignored by cl.exe, not Clang.
          "CCC_OVERRIDE_OPTIONS=x/p:DefineConstants=GLOG_USE_GLOG_EXPORT" | Out-File -FilePath $env:GITHUB_ENV -Append
          "VIRTUAL_ENV=$PWD/.venv" | Out-File -FilePath $env:GITHUB_ENV -Append
          "$PWD/.venv/Scripts" | Out-File -FilePath $env:GITHUB_PATH -Append
          $torchLib = .venv/Scripts/python.exe -c "from pathlib import Path; import torch; print(Path(torch.__file__).resolve().parent / 'lib')"
          "LIBTORCH_USE_PYTORCH=1" | Out-File -FilePath $env:GITHUB_ENV -Append
          $torchLib | Out-File -FilePath $env:GITHUB_PATH -Append

      - name: Test loader runtime and conditional CUDA pinning
        run: cargo test -p rusttorch-data --tests --locked

      - name: Test RustTorch runtime on this platform
        run: cargo test --workspace --all-targets --locked --features rusttorch/vision,rusttorch/audio,rusttorch/text,rusttorch/columnar

      - name: Run loader examples
        run: |
          cargo run -p rusttorch-data --example loader --locked
          cargo run -p rusttorch-data --example checkpoint --locked

  loader-stress:
    name: Data loader stress
    runs-on: ubuntu-latest
    env:
      LIBTORCH_USE_PYTORCH: "1"
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: "3.14"

      - name: Set up UV
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
        with:
          version: "0.12.3"
          enable-cache: false

      - name: Synchronize locked Python environment
        run: uv sync --frozen --no-cache

      - name: Set up stable Rust
        uses: actions-rust-lang/setup-rust-toolchain@46268bd060767258de96ed93c1251119784f2ab6 # v1.16.1
        with:
          toolchain: stable
          cache: false

      - name: Export LibTorch library path
        run: |
          echo "VIRTUAL_ENV=$PWD/.venv" >> "$GITHUB_ENV"
          echo "$PWD/.venv/bin" >> "$GITHUB_PATH"
          torch_lib="$(.venv/bin/python -c 'from pathlib import Path; import torch; print(Path(torch.__file__).resolve().parent / "lib")')"
          echo "LD_LIBRARY_PATH=${torch_lib}" >> "$GITHUB_ENV"

      - name: Repeat deterministic lifecycle and checkpoint tests
        run: |
          for pass in 1 2 3; do
            cargo test -p rusttorch-data --locked --test worker_lifecycle --test checkpoint_map --test checkpoint_stream
          done

  required:
    name: required
    if: always()
    needs:
      - dco
      - dependency-review
      - quality
      - msrv
      - cli
      - loader-platform
      - loader-stress
    runs-on: ubuntu-latest
    steps:
      - name: Require every applicable check
        env:
          EVENT_NAME: ${{ github.event_name }}
          DCO_RESULT: ${{ needs.dco.result }}
          DEPENDENCY_REVIEW_RESULT: ${{ needs.dependency-review.result }}
          QUALITY_RESULT: ${{ needs.quality.result }}
          MSRV_RESULT: ${{ needs.msrv.result }}
          CLI_RESULT: ${{ needs.cli.result }}
          LOADER_PLATFORM_RESULT: ${{ needs.loader-platform.result }}
          LOADER_STRESS_RESULT: ${{ needs.loader-stress.result }}
        run: |
          test "$QUALITY_RESULT" = "success"
          test "$MSRV_RESULT" = "success"
          test "$CLI_RESULT" = "success"
          test "$LOADER_PLATFORM_RESULT" = "success"
          test "$LOADER_STRESS_RESULT" = "success"
          if [ "$EVENT_NAME" = "pull_request" ]; then
            test "$DCO_RESULT" = "success"
            test "$DEPENDENCY_REVIEW_RESULT" = "success"
          else
            test "$DCO_RESULT" = "skipped"
            test "$DEPENDENCY_REVIEW_RESULT" = "skipped"
          fi