Skip to content

Commit

Permalink
Merge pull request #7612 from chainer-ci/bp-7508-v12-cythonlint
Browse files Browse the repository at this point in the history
[backport] Introduce `cython-lint`
  • Loading branch information
kmaehashi authored Jun 6, 2023
2 parents 291b649 5e47645 commit 7d2a869
Show file tree
Hide file tree
Showing 41 changed files with 104 additions and 177 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,11 @@ repos:
args: [--config, ".flake8.cython", "--force-check"]
additional_dependencies: [flake8-force]

- repo: https://github.com/MarcoGorelli/cython-lint
rev: v0.15.0
hooks:
- id: cython-lint

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
hooks:
Expand Down
4 changes: 0 additions & 4 deletions cupy/_core/_carray.pyx
Original file line number Diff line number Diff line change
@@ -1,7 1,3 @@
import os

from libcpp cimport vector

from cupy.cuda cimport function
from cupy._core cimport internal

Expand Down
5 changes: 1 addition & 4 deletions cupy/_core/_cub_reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 263,6 @@ cdef str _get_cub_header_include():
return _cub_header

assert _cub_path is not None
cdef str rocm_path = None
if _cub_path == '<bundle>':
_cub_header = '''
#include <cupy/cuda_workaround.h>
Expand Down Expand Up @@ -292,7 291,7 @@ cpdef inline tuple _can_use_cub_block_reduction(
parameters, otherwise returns None.
'''
cdef tuple axis_permutes_cub
cdef _ndarray_base in_arr, out_arr
cdef _ndarray_base in_arr
cdef Py_ssize_t contiguous_size = 1
cdef str order

Expand All @@ -307,7 306,6 @@ cpdef inline tuple _can_use_cub_block_reduction(
return None

in_arr = in_args[0]
out_arr = out_args[0]

# the axes might not be sorted when we arrive here...
reduce_axis = tuple(sorted(reduce_axis))
Expand Down Expand Up @@ -565,7 563,6 @@ def _get_cub_optimized_params(
self, optimize_config, in_args, out_args, in_shape, out_shape,
type_map, map_expr, reduce_expr, post_map_expr, reduce_type,
stream, full_reduction, out_block_num, contiguous_size, params):
out_size = internal.prod(out_shape)
in_args = [_reduction._optimizer_copy_arg(a) for a in in_args]
out_args = [_reduction._optimizer_copy_arg(a) for a in out_args]

Expand Down
5 changes: 0 additions & 5 deletions cupy/_core/_fusion_kernel.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,6 @@ import itertools
import string

from libcpp cimport vector
import numpy

from cupy._core cimport _carray
from cupy._core.core cimport _ndarray_init
Expand All @@ -11,16 10,13 @@ from cupy._core.core cimport _ndarray_base
from cupy._core cimport internal
from cupy._core cimport _routines_manipulation as _manipulation
from cupy_backends.cuda.api cimport driver
from cupy.cuda cimport function
from cupy_backends.cuda.api cimport runtime
from cupy._core cimport _reduction

import cupy as _cupy
from cupy._core import _dtype
from cupy import _util
from cupy._core import _codeblock
from cupy._core import _fusion_op
from cupy._core._fusion_variable import _AbstractDim
from cupy._core._fusion_variable import _TraceVariable
from cupy._core._fusion_variable import _TraceScalar
from cupy._core._fusion_variable import _TraceArray
Expand Down Expand Up @@ -291,7 287,6 @@ cdef class FusedKernel:
"""
cdef list params = []
cdef list indexers = []
cdef list block_strides = []
cdef _carray.Indexer indexer

for i in range(len(self._params)):
Expand Down
8 changes: 2 additions & 6 deletions cupy/_core/_kernel.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,6 @@ import cupy
from cupy.cuda import compiler
from cupy import _util

cimport cpython # NOQA
cimport cython # NOQA

from libcpp cimport vector
Expand Down Expand Up @@ -369,7 368,7 @@ cdef shape_t _reduce_dims(list args, tuple params, const shape_t& shape):

cdef shape_t _reduced_view_core(list args, tuple params, const shape_t& shape):
cdef int i, ax, last_ax, ndim
cdef Py_ssize_t x, total_size
cdef Py_ssize_t total_size
cdef shape_t vecshape, newshape, newstrides
cdef vector.vector[int] array_indexes, axes
cdef vector.vector[int] strides_indexes
Expand Down Expand Up @@ -677,8 676,6 @@ cdef list _get_out_args_with_params(
tuple out_params, bint is_size_specified):
cdef ParameterInfo p
cdef _ndarray_base arr
cdef shape_t shape
cdef Py_ssize_t x
if not out_args:
for p in out_params:
if p.raw and not is_size_specified:
Expand Down Expand Up @@ -847,7 844,7 @@ cdef class ElementwiseKernel:
cdef function.Function kern
cdef Py_ssize_t size, i
cdef list in_args, out_args
cdef tuple in_types, out_types, types
cdef tuple in_types, out_types
cdef shape_t shape

size = kwargs.pop('size', -1)
Expand Down Expand Up @@ -1243,7 1240,6 @@ cdef class ufunc:
cdef function.Function kern
cdef list broad_values
cdef shape_t shape
cdef Py_ssize_t s

out = kwargs.pop('out', None)
where = kwargs.pop('_where', None)
Expand Down
2 changes: 0 additions & 2 deletions cupy/_core/_memory_range.pyx
Original file line number Diff line number Diff line change
@@ -1,9 1,7 @@
from cupy._core.core cimport _ndarray_base
from cupy.cuda cimport memory

from libc.stdint cimport intptr_t
from libcpp.pair cimport pair
from libcpp.vector cimport vector


cpdef pair[Py_ssize_t, Py_ssize_t] get_bound(_ndarray_base array):
Expand Down
5 changes: 0 additions & 5 deletions cupy/_core/_reduction.pyx
Original file line number Diff line number Diff line change
@@ -1,7 1,5 @@
from cpython cimport sequence

from libcpp cimport vector

from cupy._core cimport _carray
from cupy._core cimport _accelerator
from cupy._core._carray cimport shape_t
Expand All @@ -11,7 9,6 @@ from cupy._core cimport _kernel
from cupy._core._kernel cimport _broadcast
from cupy._core._kernel cimport _check_peer_access
from cupy._core._kernel cimport _get_arginfos
from cupy._core._kernel cimport _get_kernel_params
from cupy._core._kernel cimport _get_out_args_from_optionals
from cupy._core._kernel cimport _get_out_args_with_params
from cupy._core._kernel cimport _preprocess_args
Expand All @@ -28,7 25,6 @@ from cupy._core.core cimport _ndarray_base
from cupy._core cimport internal
from cupy.cuda cimport device
from cupy.cuda cimport function
from cupy.cuda cimport memory
from cupy_backends.cuda.api cimport runtime

import math
Expand Down Expand Up @@ -312,7 308,6 @@ cdef class _AbstractReductionKernel:
cdef tuple reduce_axis, out_axis, axis_permutes
cdef tuple params, opt_params
cdef tuple shape_and_strides
cdef Py_ssize_t i
cdef Py_ssize_t contiguous_size = -1
cdef Py_ssize_t block_size, block_stride, out_block_num = 0
cdef shape_t in_shape, out_shape
Expand Down
1 change: 0 additions & 1 deletion cupy/_core/_routines_binary.pyx
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
from cupy._core._kernel import create_ufunc
from cupy._core._reduction import create_reduction_func


cdef _create_bit_op(name, op, no_bool, doc='', scatter_op=None):
Expand Down
20 changes: 8 additions & 12 deletions cupy/_core/_routines_indexing.pyx
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
# distutils: language = c
import sys
import warnings
import string

Expand All @@ -12,7 11,6 @@ from cupy._core._ufuncs import elementwise_copy

from libcpp cimport vector

from cupy_backends.cuda.api cimport runtime
from cupy._core._carray cimport shape_t
from cupy._core._carray cimport strides_t
from cupy._core cimport core
Expand All @@ -28,7 26,7 @@ from cupy._core cimport internal
cdef _ndarray_base _ndarray_getitem(_ndarray_base self, slices):
cdef Py_ssize_t axis
cdef list slice_list
cdef _ndarray_base a, mask
cdef _ndarray_base a

slice_list = _prepare_slice_list(slices)
a, adv = _view_getitem(self, slice_list)
Expand Down Expand Up @@ -301,7 299,7 @@ cdef tuple _view_getitem(_ndarray_base a, list slice_list):
cdef strides_t strides
cdef _ndarray_base v
cdef Py_ssize_t ndim_a, axis_a, ndim_v, axis_v, ndim_ellipsis
cdef Py_ssize_t i, k, offset, start
cdef Py_ssize_t i, k, offset
cdef Py_ssize_t s_start, s_stop, s_step, dim, ind
cdef slice ss
cdef list index_list, axes
Expand Down Expand Up @@ -708,9 706,9 @@ cdef _check_mask_shape(_ndarray_base a, _ndarray_base mask, Py_ssize_t axis):

cpdef _prepare_mask_indexing_single(
_ndarray_base a, _ndarray_base mask, Py_ssize_t axis):
cdef _ndarray_base mask_scanned, mask_br, mask_br_scanned
cdef _ndarray_base mask_scanned, mask_br
cdef int n_true
cdef tuple lshape, rshape, out_shape, a_shape
cdef tuple lshape, rshape, a_shape
cdef Py_ssize_t a_ndim, mask_ndim

a_ndim = a._shape.size()
Expand Down Expand Up @@ -779,7 777,7 @@ cdef _ndarray_base _take(
_ndarray_base a, indices, int start, int stop, _ndarray_base out=None):
# Take along (flattened) axes from start to stop.
# When start 1 == stop this function behaves similarly to np.take
cdef tuple out_shape, ind_shape, indices_shape
cdef tuple out_shape, indices_shape
cdef int i, ndim = a._shape.size()
cdef Py_ssize_t ldim, cdim, rdim, index_range

Expand Down Expand Up @@ -844,12 842,10 @@ cdef _scatter_op_single(
# slices = (slice(None),) * start indices \
# (slice(None),) * (a.ndim - stop)
# a[slices] = value
cdef Py_ssize_t ndim, adim, cdim, rdim
cdef Py_ssize_t adim, cdim, rdim
cdef tuple a_shape, indices_shape, lshape, rshape, v_shape
cdef _ndarray_base v

ndim = a._shape.size()

if not isinstance(value, _ndarray_base):
v = core.array(value, dtype=a.dtype)
else:
Expand Down Expand Up @@ -992,8 988,8 @@ cdef _scatter_op_mask_single(


cdef _scatter_op(_ndarray_base a, slices, value, op):
cdef Py_ssize_t i, start, stop, axis
cdef _ndarray_base v, x, y, reduced_idx, mask
cdef Py_ssize_t start, stop, axis
cdef _ndarray_base x, y, reduced_idx
cdef list slice_list

slice_list = _prepare_slice_list(slices)
Expand Down
4 changes: 1 addition & 3 deletions cupy/_core/_routines_linalg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 545,7 @@ cpdef _ndarray_base tensordot_core(
Py_ssize_t m, Py_ssize_t k, const shape_t& ret_shape):
# out, if specified, must be C-contiguous and have correct shape.
cdef shape_t shape
cdef Py_ssize_t inca, incb, transa, transb, lda, ldb
cdef Py_ssize_t mode
cdef Py_ssize_t transa, transb, lda, ldb
cdef intptr_t handle
cdef _ndarray_base copy_to_out = None
cdef str dtype = a.dtype.char
Expand Down Expand Up @@ -799,7 798,6 @@ cpdef _ndarray_base _mat_ptrs(_ndarray_base a):
"""
cdef int ndim = a._shape.size()
assert ndim > 2
cdef Py_ssize_t sh_, st_
cdef _ndarray_base idx
idx = _mat_ptrs_kernel(
a.data.ptr, a._strides[0],
Expand Down
7 changes: 1 addition & 6 deletions cupy/_core/_routines_manipulation.pyx
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
# distutils: language = c
import functools
import sys

import numpy

Expand All @@ -13,7 12,6 @@ cimport cython # NOQA
from libcpp cimport vector

from cupy._core._dtype cimport get_dtype, _raise_if_invalid_cast
from cupy._core cimport _routines_indexing as _indexing
from cupy._core cimport core
from cupy._core.core cimport _ndarray_base
from cupy._core cimport internal
Expand Down Expand Up @@ -417,7 415,7 @@ cpdef _ndarray_base _transpose(


cpdef array_split(_ndarray_base ary, indices_or_sections, Py_ssize_t axis):
cdef Py_ssize_t i, ndim, size, each_size, index, prev, offset, stride
cdef Py_ssize_t i, ndim, size, each_size, index, prev, stride
cdef Py_ssize_t num_large
cdef shape_t shape

Expand Down Expand Up @@ -590,7 588,6 @@ cpdef _ndarray_base concatenate_method(
cdef int ndim0
cdef int i
cdef _ndarray_base a, a0
cdef shape_t shape

if dtype is not None:
dtype = get_dtype(dtype)
Expand Down Expand Up @@ -764,8 761,6 @@ cdef _get_strides_for_nocopy_reshape(

ndim = shape.size()
dim = 0
sh = shape[0]
st = strides[0]
last_stride = shape[0] * strides[0]
for i in range(newshape.size()):
size = newshape[i]
Expand Down
1 change: 0 additions & 1 deletion cupy/_core/_routines_math.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,6 @@ from cupy import _util
from cupy_backends.cuda.api cimport runtime
from cupy._core cimport _accelerator
from cupy._core._dtype cimport get_dtype
from cupy._core cimport _kernel
from cupy._core.core cimport _ndarray_init
from cupy._core.core cimport compile_with_cache
from cupy._core.core cimport _ndarray_base
Expand Down
13 changes: 3 additions & 10 deletions cupy/_core/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 5,13 @@ import functools
import os
import pickle
import re
import sys
import warnings

import numpy

import cupy
from cupy._core._kernel import create_ufunc
from cupy._core._kernel import ElementwiseKernel
from cupy._core._kernel import ufunc # NOQA
from cupy._core._ufuncs import elementwise_copy
from cupy._core import flags
from cupy._core import syncdetect
Expand All @@ -25,9 23,7 @@ from cupy.cuda import stream as stream_mod
from cupy_backends.cuda.api.runtime import CUDARuntimeError
from cupy import _util

cimport cpython # NOQA
cimport cython # NOQA
from libcpp cimport vector
from libc.stdint cimport int64_t, intptr_t

from cupy._core cimport _carray
Expand Down Expand Up @@ -194,7 190,7 @@ cdef class _ndarray_base:

# Check for erroneous shape
if len(s) > _carray.MAX_NDIM:
msg = f'maximum supported dimension for an ndarray is '
msg = 'maximum supported dimension for an ndarray is '
msg = f'{_carray.MAX_NDIM}, found {len(s)}'
raise ValueError(msg)
self._shape.reserve(len(s))
Expand Down Expand Up @@ -232,7 228,7 @@ cdef class _ndarray_base:
""" For internal ndarray creation. """
cdef Py_ssize_t itemsize
if shape.size() > _carray.MAX_NDIM:
msg = f'maximum supported dimension for an ndarray is '
msg = 'maximum supported dimension for an ndarray is '
msg = f'{_carray.MAX_NDIM}, found {shape.size()}'
raise ValueError(msg)
self._shape = shape
Expand Down Expand Up @@ -541,7 537,6 @@ cdef class _ndarray_base:
"""
cdef strides_t strides
cdef Py_ssize_t stride

# TODO(beam2d): Support casting and subok option
if casting is not None:
Expand Down Expand Up @@ -1983,7 1978,7 @@ cdef class _ndarray_base:
if shape.size() != strides.size():
raise ValueError('len(shape) != len(strides)')
if shape.size() > _carray.MAX_NDIM:
msg = f'maximum supported dimension for an ndarray is '
msg = 'maximum supported dimension for an ndarray is '
msg = f'{_carray.MAX_NDIM}, found {shape.size()}'
raise ValueError(msg)
self._shape = shape
Expand Down Expand Up @@ -2483,7 2478,6 @@ cdef _ndarray_base _array_from_nested_numpy_sequence(
# https://github.com/cupy/cupy/pull/5155#discussion_r621808782
cdef pinned_memory.PinnedMemoryPointer mem = (
_alloc_async_transfer_buffer(nbytes))
cdef size_t offset, length
if mem is not None:
# write concatenated arrays to the pinned memory directly
src_cpu = (
Expand Down Expand Up @@ -2714,7 2708,6 @@ cpdef _ndarray_base ascontiguousarray(_ndarray_base a, dtype=None):

cpdef _ndarray_base asfortranarray(_ndarray_base a, dtype=None):
cdef _ndarray_base newarray
cdef int m, n
cdef bint same_dtype = False
zero_dim = a._shape.size() == 0

Expand Down
Loading

0 comments on commit 7d2a869

Please sign in to comment.