huggingface/candle · error
step cannot be zero
Error message
step cannot be zero
What it means
Tensor::arange_step generates a range from start to end with a given step; a step of zero would produce an infinite loop, so it is rejected up front with this bail. D::is_zero is checked before any data is generated.
Source
Thrown at candle-core/src/tensor.rs:501
}
/// Creates a new 1D tensor with values from the interval `[start, end)` taken with a common
/// difference `step` from `start`.
///```rust
/// use candle_core::{Tensor, Device};
/// let a = Tensor::arange_step(2.0, 4.0, 0.5, &Device::Cpu)?;
///
/// assert_eq!(a.to_vec1::<f64>()?, &[2.0, 2.5, 3.0, 3.5]);
/// # Ok::<(), candle_core::Error>(())
/// ```
pub fn arange_step<D: crate::WithDType>(
start: D,
end: D,
step: D,
device: &Device,
) -> Result<Self> {
if D::is_zero(&step) {
bail!("step cannot be zero")
}
let mut data = vec![];
let mut current = start;
if step >= D::zero() {
while current < end {
data.push(current);
current += step;
}
} else {
while current > end {
data.push(current);
current += step;
}
}
let len = data.len();
Self::from_vec_impl(data, len, device, false)
}
View on GitHub (pinned to d5fee525bf)
Solutions
- Pass a nonzero step, matching the sign of (end - start): positive for ascending ranges, negative for descending.
- Validate user/config-supplied step values before calling arange_step.
- Use Tensor::arange(start, end, &dev) if you just want unit steps.
Example fix
// before
let t = Tensor::arange_step(0f32, 10., cfg.step, &dev)?; // cfg.step may be 0
// after
let step = if cfg.step == 0. { 1. } else { cfg.step };
let t = Tensor::arange_step(0f32, 10., step, &dev)?; Defensive patterns
Strategy: validation
Validate before calling
if step == 0.0 {
return Err(anyhow!("arange_step requires nonzero step"));
}
let t = Tensor::arange_step(start, end, step, &dev)?; Try / catch
let t = Tensor::arange_step(start, end, step, &dev)
.map_err(|e| anyhow::anyhow!("bad arange step {step}: {e"))?; Prevention
- Validate config/CLI-provided step values before use.
- When step is derived as (end-start)/n, assert n > 0 and the quotient != 0.
- Use Tensor::arange for unit-step ranges.
When it happens
Trigger: Calling Tensor::arange_step(start, end, 0., &dev) with a literal or computed step of zero, e.g. a step read from config/CLI that defaulted to 0 or was divided to 0.
Common situations: Config files where the 'step' field was left at 0 or missing and defaulted to zero; dynamic step computed as (end-start)/n where n was huge or start==end leading to 0.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/7c69f312dbd992ba.
Report an issue: GitHub.