Skip to content

Fix broken slice_scatter example in basics.rs#3062

Merged
ivarflakstad merged 2 commits into
huggingface:mainfrom
davenpi:fix/core-basics-example
Sep 9, 2025
Merged

Fix broken slice_scatter example in basics.rs#3062
ivarflakstad merged 2 commits into
huggingface:mainfrom
davenpi:fix/core-basics-example

Conversation

@davenpi

@davenpi davenpi commented Aug 22, 2025

Copy link
Copy Markdown
Contributor

Problem

The basics.rs example in candle-core/examples fails to run due to a shape mismatch error:

$ cargo run --example basics -p candle-core
Error: shape mismatch in slice-scatter (self, src), lhs: [3, 2], rhs: [2, 1]

because

let a = Tensor::new(&[[0.0f32, 1.0, 2.0], [3.0, 4.0, 5.0]], &Device::Cpu)?;  // [2, 3]
let b = Tensor::new(&[[88.0f32, 99.0]], &Device::Cpu)?;                      // [1, 2] ❌
let new_a = a.slice_scatter(&b, 1, 2)?;  // Trying to scatter into column 2

Fix

// Before
let b = Tensor::new(&[[88.0f32, 99.0]], &Device::Cpu)?;  // [1, 2] row vector

// After  
let b = Tensor::new(&[[88.0f32], [99.0]], &Device::Cpu)?;  // [2, 1] column vector

Also updated the assertion to match the expected result after column replacement.

- Change tensor b from [1,2] row vector to [2,1] column vector
- Fix assertion to match expected result after column replacement
- Resolves shape mismatch error that prevented example from running

@ivarflakstad ivarflakstad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

@ivarflakstad ivarflakstad merged commit b1dbce0 into huggingface:main Sep 9, 2025
9 checks passed
john-sharratt pushed a commit to john-sharratt/candle that referenced this pull request May 7, 2026
Fix broken slice_scatter example in basics.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants