rescale_stride Function and Check for Division by Zero
Author: akshit397a
Pull request: no
Labels: name: bug

name: Vision

### System Info
───────────────────────────────────────────────────────────────────────────────────────┐
│  SYSTEMINFORMATION                                                      Version: 5.23.5 │
└─────────────────────────────────────────────────────────────────────────────────────────┘

Operating System:
──────────────────────────────────────────────────────────────────────────────────────────
Platform         : Windows
Distro           : Microsoft Windows 11 Home Single Language
Release          : 10.0.22631
Codename         :
Kernel           : 10.0.22631
Arch             : x64
Hostname         : DESKTOP-FFR0VG0
Codepage         : 437
Build            : 22631
Hypervisor       : true
RemoteSession    :

System:
──────────────────────────────────────────────────────────────────────────────────────────
Manufacturer     : Dell Inc.
Model            : Inspiron 15 3525
Version          : 1.19.0
Virtual          :

CPU:
──────────────────────────────────────────────────────────────────────────────────────────
Manufacturer     : AMD
Brand            : Ryzen 5 5500U with Radeon Graphics
Family           : 23
Model            : 104
Stepping         : 1
Speed            : 2.1
Cores            : 12
PhysicalCores    : 6
PerformanceCores : 12
EfficiencyCores  :
Processors       : 1
Socket           : None

**Issue**

1.   **rescale_stride Function:** The rescale_stride function can be optimized for readability and performance.
The round function is called multiple times on the same variables (input_n and token_n). You can precompute certain values to avoid redundant calculations.
The variable names left and right should be clarified for better readability.

2. **Check for Division by Zero:**

There’s a division operation (left / input_n and right / input_n). Add a safeguard to check if input_n is not zero to avoid potential ZeroDivisionError.

**Improvements**
1. **Removed Redundant Calculations:** The round function for input_n is calculated once and used twice, so we eliminated the redundant call.
2. **Check for Division by Zero:** Added a check to ensure input_n is not zero, which prevents runtime errors.
3. **Clarified Variable Names:** Clarified that left_rescaled and right_rescaled are the rescaled versions of left and right.


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

from collections import defaultdict
from typing import TYPE_CHECKING, Dict, Optional, Union

import numpy as np
import requests

from ..tokenization_utils import PreTrainedTokenizer
from ..utils import is_torch_available, is_torchaudio_available, logging
from .audio_utils import ffmpeg_read
from .base import ChunkPipeline


if TYPE_CHECKING:
    from pyctcdecode import BeamSearchDecoderCTC

    from ..feature_extraction_sequence_utils import SequenceFeatureExtractor
    from ..modeling_utils import PreTrainedModel

logger = logging.get_logger(__name__)

if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES


def rescale_stride(stride, ratio):
    """
    Rescales the stride values from audio space to tokens/logits space.

    (160_000, 16_000, 16_000) -> (2000, 200, 200) for instance.
    """
    # Shape is [B, SEQ] for tokens
    # [B, SEQ, V] for logits

    new_strides = []
    for input_n, left, right in stride:
        token_n = int(round(input_n * ratio))
        left = int(round(left / input_n * token_n))
        right = int(round(right / input_n * token_n))
        new_stride = (token_n, left, right)
        new_strides.append(new_stride)

    return new_strides

### Expected behavior

1. Redundant Calculations
Expected Behavior:
The function rescale_stride is expected to rescale the stride values in an efficient and straightforward way. The goal is to scale from one space (audio) to another (tokens/logits), using simple ratio-based calculations. It should perform calculations only once per step for efficiency.

Actual Behavior:
The code was unnecessarily recalculating values, performing operations like dividing, rounding, and multiplying multiple times within the loop. This results in inefficient processing, especially for large inputs where multiple recalculations could become computationally expensive.

left = int(round(left / input_n * token_n))
right = int(round(right / input_n * token_n))

input_n and token_n are used multiple times, leading to redundant computations.


2. Potential Division by Zero
Expected Behavior:
The function is supposed to handle cases where input_n is zero safely. Ideally, the code should anticipate such edge cases and either skip processing or handle it gracefully (e.g., by throwing a clear error or providing a default value).

Actual Behavior:
The division involving input_n doesn't account for cases where input_n might be zero. In such cases, the division operation would result in a ZeroDivisionError, which would cause the program to crash unexpectedly.

left = int(round(left / input_n * token_n))

If input_n is zero, this would lead to a division by zero.



Comments:
Hey @akshit397a, I'm having trouble following, could you link the code you're reffering to?

Hey @LysandreJik 
the file is in src/transformers/feature_extraction_utils.py

Hey @LysandreJik
Any updates on the issue?


Would you like to open a PR to offer a fix for the issue you mention?

Hey @LysandreJik @akshit397a,

I'd like to contribute and work on this issue! I'll take a closer look at the code and propose a potential fix. This will be my first contribution to this repo, so any guidance or pointers would be greatly appreciated.

Thanks!

> Would you like to open a PR to offer a fix for the issue you mention?

Yes i want to open a PR.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.