Xilinx / brevitas

Brevitas: neural network quantization in PyTorch
https://xilinx.github.io/brevitas/
Other
1.14k stars 189 forks source link

Docstrings with backslashes are not set as raw strings #983

Open andrei-stoian-zama opened 1 month ago

andrei-stoian-zama commented 1 month ago

Two docstrings in core/scaling/pre_scaling.py contain backslashes which, when brevitas is imported during a pytest, issue errors:

  File "/__w/concrete-ml/concrete-ml/.venv/lib/python3.8/site-packages/brevitas/core/scaling/__init__.py", line 9, in <module>
    from .pre_scaling import AccumulatorAwareParameterPreScaling
  File "/__w/concrete-ml/concrete-ml/.venv/lib/python3.8/site-packages/brevitas/core/scaling/pre_scaling.py", line 198
    """Takes weights, input bit-width, and input sign as input and returns the pre-clipping
    ^
SyntaxError: invalid escape sequence \c

The solution is to make them raw strings:

    @brevitas.jit.script_method
    def forward(self, weights: Tensor, input_bit_width: Tensor, input_is_signed: bool) -> Tensor:
        r"""Takes weights, input bit-width, and input sign as input and returns the pre-clipping
        scaling factor per-channel, which is $s \cdot \Vert v - \mu_v \Vert_1 / g$"""

Two occurrences found for now: pre_scaling.py: 198 and 265.

Weirdly simply importing from brevitas.core.scaling import AccumulatorAwareParameterPreScaling (not in pytest) works fine. It seems to be something related to doctest but it's not very clear.

Giuseppe5 commented 1 month ago

The only thing I'm wondering about is how these would look like in the documentation after the change to raw string. I will look into that. Thanks for spotting this