Spell out where views are allowed

オープン
#331 コメント 15 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python, pytorch
領域
documentation

調査の方向性

コピー、ビュー、ミューテーションに関するリンク先のセクションから始め、特に sum の例に注目して array_api_compat PR 298 を確認してください。ビューを生成する一覧中の関数を、issue で取り上げられている他の操作と比較し、ビューをいつ許可するかについて、明示的で合意された範囲を文書化してください。標準がこれらのケースを明確に解決していれば完了です。

索引モデルが issue の本文から書いたものです。

説明

In https://data-apis.org/array-api/latest/design_topics/copies_views_and_mutation.html, the Standard says

Array API consumers are strongly advised to avoid any mutating operations when an array object may [...] be a “view” [...] It is not always clear, however, when a library will return a view and when it will return a copy. This standard does not attempt to specify this—libraries may do either.

The above is fine after __getitem__ , asarray(..., copy=None), astype(..., copy=False), and similar functions that are explicitly explained by the standard to potentially return views.

However, there are a few corner cases where views could be possible but a normal user is very unlikely to think about them.
I just stumbled on one in https://github.com/data-apis/array-api-compat/pull/298, where array_api_compat.torch.sum(x, dtype=x.dtype, axis=()) was accidentally returning x instead of a copy of it.

There are a few more cases where a library could try to be smart; for example

  • search functions (min, max, other?) could return a view to the minimum/maximum point
  • replacement functions (minimum, maximum, clip, where) could return one of the input arrays when there is nothing to do
  • same for arithmetic functions (__add__ / __sub__ vs. 0, __mul__ / __div__ vs. 1, etc.)
  • same for sort functions when they realise the input is already sorted
  • possibly more

In real life, I expect end users to assume that the above functions will always return a copy.
I think the standard should spell this out, limiting the possibily of views to an explicit list of allowed functions:

  • __getitem__
  • asarray
  • astype
  • __dlpack__
  • from_dlpack
  • reshape
  • broacast_to
  • broadcast_arrays
  • ...more?
主要言語
Python
スター
131
フォーク
49
平均マージ
2日 8時間
マージ済み PR(30日)
6

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

data-apis/array-api-compat のほかの issue

data-apis/array-api-compat の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。