### Run Mistral Example in Release Mode Source: https://github.com/oxiglade/mlx-rs/blob/main/examples/mistral/README.md Execute the Mistral text generation example in release mode. This command assumes you have the mlx-rs project set up. ```bash cargo run --release ``` -------------------------------- ### Run Mistral Example with Custom Prompt and Seed Source: https://github.com/oxiglade/mlx-rs/blob/main/examples/mistral/README.md Run the Mistral text generation example with a custom prompt and seed. This command is executed in release mode. ```bash cargo run --release -- --prompt "Hello, world!" --seed 1 ``` -------------------------------- ### Updating Arrays in MLX-RS Training Loop Source: https://github.com/oxiglade/mlx-rs/blob/main/mlx-rs/README.md This example shows how to update a weight array within a training loop by calculating the gradient and applying it to the corresponding input array. Ensure `eval()` is called after updates. ```rust let mut inputs = vec![w, x, y]; for _ in 0..num_iterations { let grad = transforms::grad(loss_fn, argnums)(&inputs)?; inputs[0] = &inputs[0] - Array::from_f32(learning_rate) * grad; inputs[0].eval()?; } ``` -------------------------------- ### Correct Closure for Automatic Differentiation Source: https://github.com/oxiglade/mlx-rs/blob/main/README.md This Rust code shows the recommended approach for automatic differentiation by explicitly passing all required arrays as inputs to the closure, ensuring proper tracing of the compute graph. ```rust let loss_fn = |inputs: &[Array]| -> Result { let w = &inputs[0]; let x = &inputs[1]; let y = &inputs[2]; let y_pred = x.matmul(w)?; let loss = Array::from_f32(0.5) * ops::mean(&ops::square(y_pred - y)?, None, None)?; Ok(loss) }; let argnums = &[0]; let mut inputs = vec![w, x, y]; let grad = transforms::grad(loss_fn, argnums)(&inputs)?; ``` -------------------------------- ### Add MLX-RS Dependency to Cargo.toml Source: https://github.com/oxiglade/mlx-rs/blob/main/mlx-rs/README.md Include the mlx-rs crate in your Rust project by adding this entry to your Cargo.toml file. ```toml [dependencies] mlx-rs = "0.21.0" ``` -------------------------------- ### Correct Automatic Differentiation Closure in Rust Source: https://github.com/oxiglade/mlx-rs/blob/main/mlx-rs/README.md This closure correctly passes all required arrays as explicit inputs, ensuring proper tracing for automatic differentiation in mlx-rs. The `argnums` parameter specifies which input array to differentiate with respect to. ```rust let loss_fn = |inputs: &[Array]| -> Result { let w = &inputs[0]; let x = &inputs[1]; let y = &inputs[2]; let y_pred = x.matmul(w)?; let loss = Array::from_f32(0.5) * ops::mean(&ops::square(y_pred - y)?, None, None)?; Ok(loss) }; let argnums = &[0]; let mut inputs = vec![w, x, y]; grad = transforms::grad(loss_fn, argnums)(&inputs)?; ``` -------------------------------- ### Incorrect Automatic Differentiation Closure in Rust Source: https://github.com/oxiglade/mlx-rs/blob/main/mlx-rs/README.md This closure attempts to capture arrays from the outer scope, which can lead to segfaults in mlx-rs. Avoid this pattern. ```rust let x = random::normal::(&[num_examples, num_features], None, None, None)?; let y = x.matmul(&w_star)? + eps; let loss_fn = |w: &Array| -> Result { let y_pred = x.matmul(w)?; let loss = Array::from_f32(0.5) * ops::mean(&ops::square(&(y_pred - &y))?, None, None)?; Ok(loss) }; grad_fn = transforms::grad(loss_fn, &[0]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.