Skip to content

Commit

Permalink
updated tch-rs to 0.13.0 (guillaume-be#380)
Browse files Browse the repository at this point in the history
* updated tch-rs to 0.13.0
find replaced of_slice to from_slice as per
https://github.com/LaurentMazare/tch-rs/blob/008fff6cc0a91ecff274d1549c2dff25b8ca57a4/CHANGELOG.md

* fixed formatting

* Add download feature and update CI

* add build script, update CI

* updated chanelog, readme, convert script

* fixed wrong position for build script

* added libtorch download to dependencies download test script

* args reordering

---------

Co-authored-by: josephhajduk <[email protected]>
Co-authored-by: Guillaume Becquin <[email protected]>
  • Loading branch information
3 people authored May 21, 2023
1 parent 5f9500c commit 2bff63b
Show file tree
Hide file tree
Showing 50 changed files with 178 additions and 128 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --features download-libtorch

build-no-defaults:
name: Build no defaults
Expand All @@ -34,7 +35,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features
args: --no-default-features --features download-libtorch

build-windows:
name: Build Windows
Expand All @@ -49,6 +50,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --features download-libtorch

build-mac-os:
name: Build macOS
Expand All @@ -63,6 +65,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: build
args: --features download-libtorch

test-batch-0:
name: Integration tests (batch 0)
Expand All @@ -89,6 +92,7 @@ jobs:
--test fnet
--test deberta
--test deberta_v2
--features download-libtorch

test-batch-1:
name: Integration tests (batch 1)
Expand All @@ -114,6 +118,7 @@ jobs:
--test longformer
--test pegasus
--test gpt_neo
--features download-libtorch

test-batch-2:
name: Integration tests (batch 2)
Expand All @@ -133,6 +138,7 @@ jobs:
--test longt5
--test gpt_j
--test nllb
--features download-libtorch

convert-model:
name: Model conversion test
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ All notable changes to this project will be documented in this file. The format
## Changed
- Bumped the tokenizers dependency from 7.x to 8.x, exposing additional options for special token mapping and adding the NLLBTokenizer.
- (BREAKING) Simplified the generation traits (removal of LMHeadModel and elimination of unnecessary specification for LanguageGenerator)
- Upgraded to `torch` 2.0 (via `tch` 0.12.0).
- (BREAKING) Upgraded to `torch` 2.0 (via `tch` 0.13.0). The process to automatically download the dependencies have changed, it must now be enabled via the `download-libtorch` feature flag.

## Fixed
- MIN/MAX computation for float-like (was set to infinity instead of min/max)
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/guillaume-be/rust-bert"
documentation = "https://docs.rs/rust-bert"
license = "Apache-2.0"
readme = "README.md"
build = "build.rs"
keywords = [
"nlp",
"deep-learning",
Expand Down Expand Up @@ -64,13 +65,14 @@ default = ["remote"]
doc-only = ["tch/doc-only"]
all-tests = []
remote = ["cached-path", "dirs", "lazy_static"]
download-libtorch = ["torch-sys/download-libtorch"]

[package.metadata.docs.rs]
features = ["doc-only"]

[dependencies]
rust_tokenizers = "8.1"
tch = "0.12.0"
tch = "0.13.0"
serde_json = "1"
serde = { version = "1", features = ["derive"] }
ordered-float = "3"
Expand All @@ -88,6 +90,6 @@ anyhow = "1"
csv = "1"
criterion = "0.4"
tokio = { version = "1.24", features = ["sync", "rt-multi-thread", "macros"] }
torch-sys = "0.12.0"
torch-sys = "0.13.0"
tempfile = "3"
itertools = "0.10"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $Env:Path += ";X:\path\to\libtorch\lib"

### Automatic installation

Alternatively, you can let the `build` script automatically download the `libtorch` library for you.
Alternatively, you can let the `build` script automatically download the `libtorch` library for you. The `download-libtorch` feature flag needs to be enabled.
The CPU version of libtorch will be downloaded by default. To download a CUDA version, please set the environment variable `TORCH_CUDA_VERSION` to `cu118`.
Note that the libtorch library is large (order of several GBs for the CUDA-enabled version) and the first build may therefore take several minutes to complete.

Expand Down
29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Laurent Mazare
// https://github.com/LaurentMazare/diffusers-rs/blob/main/build.rs
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
let os = std::env::var("CARGO_CFG_TARGET_OS").expect("Unable to get TARGET_OS");
match os.as_str() {
"linux" | "windows" => {
if let Some(lib_path) = std::env::var_os("DEP_TCH_LIBTORCH_LIB") {
println!(
"cargo:rustc-link-arg=-Wl,-rpath={}",
lib_path.to_string_lossy()
);
}
println!("cargo:rustc-link-arg=-Wl,--no-as-needed");
println!("cargo:rustc-link-arg=-Wl,--copy-dt-needed-entries");
println!("cargo:rustc-link-arg=-ltorch");
}
_ => {}
}
}
2 changes: 1 addition & 1 deletion examples/natural_language_inference_deberta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() -> anyhow::Result<()> {
input.extend(vec![0; max_len - input.len()]);
input
})
.map(|input| Tensor::of_slice(&(input)))
.map(|input| Tensor::from_slice(&(input)))
.collect::<Vec<_>>();
let input_tensor = Tensor::stack(tokenized_input.as_slice(), 0).to(device);

Expand Down
4 changes: 2 additions & 2 deletions src/bart/bart_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ impl BartGenerator {
let impossible_tokens: Vec<i64> = (0..self.get_vocab_size())
.filter(|pos| !token_ids.contains(pos))
.collect();
let impossible_tokens = Tensor::of_slice(&impossible_tokens).to_device(scores.device());
let impossible_tokens = Tensor::from_slice(&impossible_tokens).to_device(scores.device());
let _ = scores.index_fill_(1, &impossible_tokens, f64::NEG_INFINITY);
}
}
Expand Down Expand Up @@ -1207,7 +1207,7 @@ impl PrivateLanguageGenerator for BartGenerator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/distilbert/embeddings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
);
}
}
let temp_vec = Tensor::of_slice(&temp_vec);
let temp_vec = Tensor::from_slice(&temp_vec);
sinusoidal_embedding.push(temp_vec);
}
let sinusoidal_embedding = Tensor::stack(&sinusoidal_embedding, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/gpt_neo/attention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl GptNeoSelfAttention {

let mut attention_weights = attention_weights.where_self(
causal_mask,
&Tensor::of_slice(&[-1e9f32]).to_device(attention_weights.device()),
&Tensor::from_slice(&[-1e9f32]).to_device(attention_weights.device()),
);
if let Some(attention_mask_value) = attention_mask {
attention_weights = attention_weights + attention_mask_value;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
//!
//! ### Automatic installation
//!
//! Alternatively, you can let the `build` script automatically download the `libtorch` library for you.
//! Alternatively, you can let the `build` script automatically download the `libtorch` library for you. The `download-libtorch` feature flag needs to be enabled.
//! The CPU version of libtorch will be downloaded by default. To download a CUDA version, please set the environment variable `TORCH_CUDA_VERSION` to `cu118`.
//! Note that the libtorch library is large (order of several GBs for the CUDA-enabled version) and the first build may therefore take several minutes to complete.
//!
Expand Down
2 changes: 1 addition & 1 deletion src/longt5/longt5_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ impl PrivateLanguageGenerator for LongT5Generator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/m2m_100/m2m_100_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl M2M100Generator {
let impossible_tokens: Vec<i64> = (0..self.get_vocab_size())
.filter(|pos| !token_ids.contains(pos))
.collect();
let impossible_tokens = Tensor::of_slice(&impossible_tokens).to_device(scores.device());
let impossible_tokens = Tensor::from_slice(&impossible_tokens).to_device(scores.device());
let _ = scores.index_fill_(1, &impossible_tokens, f64::NEG_INFINITY);
}
}
Expand Down Expand Up @@ -750,7 +750,7 @@ impl PrivateLanguageGenerator for M2M100Generator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
6 changes: 3 additions & 3 deletions src/marian/marian_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ impl MarianGenerator {
let impossible_tokens: Vec<i64> = (0..self.get_vocab_size())
.filter(|pos| !token_ids.contains(pos))
.collect();
let impossible_tokens = Tensor::of_slice(&impossible_tokens).to_device(scores.device());
let impossible_tokens = Tensor::from_slice(&impossible_tokens).to_device(scores.device());
let _ = scores.index_fill_(1, &impossible_tokens, f64::NEG_INFINITY);
}
}
Expand Down Expand Up @@ -895,7 +895,7 @@ impl PrivateLanguageGenerator for MarianGenerator {
) {
let _ = scores.index_fill_(
1,
&Tensor::of_slice(&[self.get_pad_id().unwrap()])
&Tensor::from_slice(&[self.get_pad_id().unwrap()])
.to_kind(Kind::Int64)
.to_device(scores.device()),
f64::NEG_INFINITY,
Expand Down Expand Up @@ -975,7 +975,7 @@ impl PrivateLanguageGenerator for MarianGenerator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/mbart/mbart_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ impl MBartGenerator {
let impossible_tokens: Vec<i64> = (0..self.get_vocab_size())
.filter(|pos| !token_ids.contains(pos))
.collect();
let impossible_tokens = Tensor::of_slice(&impossible_tokens).to_device(scores.device());
let impossible_tokens = Tensor::from_slice(&impossible_tokens).to_device(scores.device());
let _ = scores.index_fill_(1, &impossible_tokens, f64::NEG_INFINITY);
}
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl PrivateLanguageGenerator for MBartGenerator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/pegasus/embeddings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl SinusoidalPositionalEmbedding {
temp_vec.push(base_value.cos());
}
}
let temp_vec = Tensor::of_slice(&temp_vec);
let temp_vec = Tensor::from_slice(&temp_vec);

sinusoidal_embedding.push(temp_vec);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pegasus/pegasus_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ impl PegasusConditionalGenerator {
let impossible_tokens: Vec<i64> = (0..self.get_vocab_size())
.filter(|pos| !token_ids.contains(pos))
.collect();
let impossible_tokens = Tensor::of_slice(&impossible_tokens).to_device(scores.device());
let impossible_tokens = Tensor::from_slice(&impossible_tokens).to_device(scores.device());
let _ = scores.index_fill_(
1,
&impossible_tokens,
Expand Down Expand Up @@ -716,7 +716,7 @@ impl PrivateLanguageGenerator for PegasusConditionalGenerator {
input.extend(temp);
input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.get_var_store().device()))
.map(|tokens| Tensor::from_slice(&tokens).to(self.get_var_store().device()))
.collect::<Vec<Tensor>>();

Tensor::stack(&token_ids, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ impl ConversationModel {
padded_input.extend(input);
padded_input
})
.map(|tokens| Tensor::of_slice(&tokens).to(self.device))
.map(|tokens| Tensor::from_slice(&tokens).to(self.device))
.collect::<Vec<Tensor>>();

(Tensor::stack(&concatenated_inputs, 0), attention_mask)
Expand Down
Loading

0 comments on commit 2bff63b

Please sign in to comment.