Skip to content

Feature: sum / Tensor.sum: support multiple axes (tuple), or document workaround for spatial + channel layouts #325

@ternaus

Description

@ternaus

Describe what you are looking for

##Summary

Reductions like Tensor.sum and nk.sum only accept axis=None (global) or a single int. There is no NumPy-style axis=(0, 1, …) for collapsing several dimensions in one call.

Current behavior

  • Stubs: axis: Optional[int] only (numkong/__init__.pyiTensor.sum, nk.sum).
  • Runtime: non-int axis raises, e.g. TypeError: an integer is required.

Motivation

For arrays shaped like (H, W, C) or (N, H, W, C), per-channel sums require reducing all spatial axes while keeping the channel axis. In NumPy that is one reduction, e.g. np.sum(x, axis=tuple(range(x.ndim - 1))). With NumKong, callers must chain several sum(axis=…) calls (and handle keepdims carefully) or reshape, which is awkward and error-prone.

Request
Either:

  1. Extend axis to int | tuple[int, ...] | None with NumPy-compatible semantics (and keepdims), at least for sum, or
  2. Add a documented, first-class API for “sum over all axes except last” / multi-axis reductions if a tuple axis is intentionally out of scope.
import numpy as np import numkong as nk t = nk.Tensor(np.ones((2, 3, 4), dtype=np.float32)) t.sum(axis=(0, 1)) # TypeError: an integer is required

Can you contribute to the implementation?

  • I can contribute

Is your feature request specific to a certain interface?

It applies to everything

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions