APP下载

Ubuntu | AI: Candle实践(基础篇)

原创

AILinux/Ubuntu

得心之想,艰难,则失之不易;得之既易,则失之亦然;难易一线,全凭己心;因即是果,果即使因。

Environment Information

  1. OS: Linux [username] 6.8.0-47-generic #47-Ubuntu SMP PREEMPT_DYNAMIC Fri Sep 27 21:40:26 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

  2. rust version: rustc 1.82.0 (f6e511eec 2024-10-15)

  3. candle version:

What is Candle?

  1. About: Minimalist ML framework for Rust

  2. Github: https://github.com/huggingface/candle , Stars: ~16k

  3. docs: https://huggingface.github.io/candle/index.html  (WIP: Work In Process)

  4. hf-hub: https://github.com/huggingface/hf-hub 

Exceptions

1. Getting Start

               
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
use hf_hub::api::sync::Api; use candle_core::Device; let api = Api::new().unwrap(); let repo = api.model("bert-base-uncased".to_string()); let weights = repo.get("model.safetensors").unwrap(); let weights = candle_core::safetensors::load(weights, &Device::Cpu); COPY

2. Getting the SSL errors when download the model file as follows:

               
  • 1
  • 2
  • 3
thread 'main' panicked at src/main.rs:8:45: called `Result::unwrap()` on an `Err` value: RequestError(Transport(Transport { kind: ConnectionFailed, message: Some("tls connection init failed"), url: Some(Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("huggingface.co")), port: None, path: "/bert-base-uncased/resolve/main/model.safetensors", query: None, fragment: None }), source: Some(Kind(UnexpectedEof)) })) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace COPY

At first, I resort to the Claude and the replay as follows:

STEP 1:


Based on the error message, it appears there's an issue with the TLS connection to the Hugging Face Hub. This is a common problem that can occur due to various reasons, especially on Ubuntu. Let me help you troubleshoot this step by step.

  1. First, let's verify that you have the necessary SSL/TLS dependencies installed on Ubuntu:
               
  • 1
  • 2
sudo apt-get update sudo apt-get install pkg-config libssl-dev ca-certificates COPY
  1. Make sure your Rust project has the required dependencies in your Cargo.toml. Here's what you should have:
               
  • 1
  • 2
  • 3
  • 4
[dependencies] hf-hub = "0.3.2" # or the latest version candle-core = "0.3.1" # or the latest version reqwest = { version = "0.11", features = ["rustls-tls"] } # Add this COPY
  1. You might want to try setting the HTTPS_PROXY environment variable if you're behind a proxy:
               
  • 1
export HTTPS_PROXY=http://proxy.example.com:port # Replace with your proxy if needed COPY
  1. Here's a modified version of your code with better error handling and proxy support:
               
  • 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
use hf_hub::api::sync::Api; use candle_core::Device; fn main() -> Result<(), Box<dyn std::error::Error>> { // Create API with more detailed error handling let api = Api::new().map_err(|e| { eprintln!("Failed to create API: {:?}", e); e })?; // Set token if you have one (optional but recommended) // let api = Api::new()?.with_token("your_token_here"); let repo = api.model("bert-base-uncased".to_string()); // Add more detailed error handling for the file download let weights = repo.get("model.safetensors").map_err(|e| { eprintln!("Failed to download weights: {:?}", e); e })?; // Load the weights with error handling let weights = candle_core::safetensors::load(&weights, &Device::Cpu)?; println!("Successfully loaded weights!"); Ok(()) } COPY
  1. You can also try running with verbose logging to see more details about the error:
               
  • 1
RUST_LOG=debug cargo run COPY
  1. If the issue persists, you can try downloading the model manually first:
               
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
use std::path::PathBuf; fn main() -> Result<(), Box<dyn std::error::Error>> { let api = Api::new()?; // Set a local cache directory let cache_dir = PathBuf::from("./model_cache"); std::fs::create_dir_all(&cache_dir)?; let api = api.with_cache_dir(cache_dir); // Rest of your code... } COPY
  1. If you're still having issues, you might want to try using a different TLS backend. Add this to your Cargo.toml:
               
  • 1
  • 2
[dependencies] reqwest = { version = "0.11", features = ["native-tls"] } # Instead of rustls-tls COPY

If none of these solutions work, you can try:

  1. Checking your firewall settings:

                   
    • 1
    sudo ufw status COPY
  2. Verifying your system's root certificates:

                   
    • 1
    update-ca-certificates COPY
  3. Testing basic HTTPS connectivity:

                   
    • 1
    curl -v https://huggingface.co COPY

Let me know if you're still experiencing issues after trying these solutions, and I can help you troubleshoot further based on any new error messages you receive.


STEP 2: I pasted the errors to feed the Claude, then got below response:

prompts:

when I using the command: "curl -v https://huggingface.co" (not need visit), got below errors:

               
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
* Host huggingface.co:443 was resolved. * IPv6: 2a03:2880:f10d:83:face:b00c:0:25de * IPv4: 115.126.100.160 * Trying 115.126.100.160:443... * Connected to huggingface.co (115.126.100.160) port 443 * ALPN: curl offers h2,http/1.1 * TLSv1.3 (OUT), TLS handshake, Client hello (1): * CAfile: /etc/ssl/certs/ca-certificates.crt * CApath: /etc/ssl/certs * OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to huggingface.co:443 * Closing connection curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to huggingface.co:443 COPY

I notice the error SSL_ERROR_SYSCALL when connecting to huggingface.co, and the IP address (115.126.100.160) seems unusual for Hugging Face. This suggests there might be DNS hijacking or network interference issues. Let's fix this step by step:

  1. First, let's verify the correct IP for huggingface.co. Run this command:
               
  • 1
nslookup huggingface.co 8.8.8.8 COPY
  1. Check your /etc/hosts file to ensure there are no incorrect entries:
               
  • 1
sudo cat /etc/hosts COPY
  1. Try clearing your DNS cache:
               
  • 1
sudo systemd-resolve --flush-caches COPY
  1. Temporarily use Google's DNS servers to test if it's a DNS issue. Edit /etc/resolv.conf:
               
  • 1
sudo nano /etc/resolv.conf COPY

Add these lines at the top:

               
  • 1
  • 2
nameserver 8.8.8.8 nameserver 8.8.4.4 COPY
  1. Try curl with specific SSL/TLS version and verbose output:
               
  • 1
curl -v --tlsv1.2 https://huggingface.co COPY
  1. Check if you can ping the correct IP:
               
  • 1
ping huggingface.co COPY
  1. If you're using Ubuntu 24.04, ensure your OpenSSL is up to date:
               
  • 1
  • 2
sudo apt update sudo apt install --only-upgrade openssl libssl-dev COPY
  1. You can modify your Rust code to use a custom DNS resolver and specific TLS configuration:
               
  • 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
use hf_hub::api::sync::Api; use candle_core::Device; use reqwest::Client; use std::time::Duration; fn main() -> Result<(), Box<dyn std::error::Error>> { // Create a custom client with specific DNS and TLS configurations let client = Client::builder() .dns_resolver(Box::new(reqwest::dns::TrustDnsResolver::new().unwrap())) .use_rustls_tls() // Use rustls instead of native-tls .timeout(Duration::from_secs(30)) .build()?; // Create API with custom client let api = Api::new()? .with_client(client); let repo = api.model("bert-base-uncased".to_string()); let weights = repo.get("model.safetensors").map_err(|e| { eprintln!("Failed to download weights: {:?}", e); e })?; let weights = candle_core::safetensors::load(&weights, &Device::Cpu)?; println!("Successfully loaded weights!"); Ok(()) } COPY
  1. Update your Cargo.toml to include the necessary dependencies:
               
  • 1
  • 2
  • 3
  • 4
  • 5
[dependencies] hf-hub = "0.3.2" candle-core = "0.3.1" reqwest = { version = "0.11", features = ["rustls-tls", "trust-dns"] } tokio = { version = "1.0", features = ["full"] } COPY
  1. If you're behind a corporate network or in a region with internet restrictions, you might want to try using a VPN or configure a proxy:
               
  • 1
  • 2
# Set environment variables for proxy export HTTPS_PROXY=http://your-proxy:port COPY
  1. You can also try to explicitly set the SSL certificate path in your environment:
               
  • 1
  • 2
export SSL_CERT_DIR=/etc/ssl/certs export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt COPY
  1. If nothing else works, you can download the model manually using a browser or another machine and then load it locally in your code:
               
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
use candle_core::Device; use std::path::PathBuf; fn main() -> Result<(), Box<dyn std::error::Error>> { let model_path = PathBuf::from("./downloaded_model.safetensors"); let weights = candle_core::safetensors::load_file(&model_path, &Device::Cpu)?; println!("Successfully loaded weights!"); Ok(()) } COPY

Let me know if you need any clarification or if you're still experiencing issues after trying these solutions!


**Result: **

I resolve the issue due to the DNS's configurations

Continue...

评论区

写评论

登录

所以,就随便说点什么吧...

这里什么都没有,快来评论吧...