Skip to main content

ShapeError

Enum ShapeError 

Source
#[non_exhaustive]
pub enum ShapeError { RankMismatch { expected: usize, actual: usize, }, ElementCountMismatch { expected: usize, actual: usize, }, AxisOutOfRange { axis: usize, rank: usize, }, DuplicateAxis { axis: usize, }, NarrowOutOfBounds { dim: usize, start: usize, len: usize, dim_size: usize, }, ElementCountOverflow, NonContiguousReshape, BroadcastIncompatible { lhs: Vec<usize>, rhs: Vec<usize>, }, MatmulDimMismatch { lhs: Vec<usize>, rhs: Vec<usize>, }, ShapeMismatch { lhs: Vec<usize>, rhs: Vec<usize>, }, IndexOutOfRange { dim: usize, index: i64, dim_size: usize, }, IndexRangeOverflow { index: usize, }, }
Expand description

テンソル生成・view 操作・reshape の shape 不整合を表す。

#[non_exhaustive] を付す理由: 公開 API 非破壊はガードレール条件 (.claude/rules/security.md)であり、後続タスクで検査項目が増えても 呼び出し側の網羅的 match を破壊しないため。

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

RankMismatch

要求される次元数(rank)と実際の次元数が一致しない。

TASK-1.4a(#11)では型定義のみを提供し構築しなかった。本 variant は ops_shape::gemm_out_shape(TASK-1.4c・#13。2 次元カーネル入口用) が rank ≠ 2 の入力を検出した際、または ops_shape::matmul_out_shape (イシュー #1715 でバッチ次元対応へ一般化。docs/public-api-design.md §3.2・docs/compat-api-scope.md §1.2)が rank < 2 の入力を検出した 際に構築する。

Fields

§expected: usize
§actual: usize
§

ElementCountMismatch

shape の要素数積とデータ長が一致しない (Tensor::new/from_slice が data.len() と shape の要素数積を 突き合わせる際に返す)。

Fields

§expected: usize
§actual: usize
§

AxisOutOfRange

軸番号(dim)がテンソルの rank 範囲外 (transpose/narrow/permute の軸引数)。

Fields

§axis: usize
§rank: usize
§

DuplicateAxis

permute の perm に同一軸が複数回指定された(順列として不正)。 permute(イシュー #274)専用の検査で構築する。

Fields

§axis: usize
§

NarrowOutOfBounds

narrow の [start, start+len) が対象軸のサイズを超える。

Fields

§dim: usize
§start: usize
§len: usize
§dim_size: usize
§

ElementCountOverflow

shape の要素数積が usize の範囲でオーバーフローする、または 要素型込みのバイトサイズが Vec の allocation 上限 (isize::MAX バイト)を超えアロケーション不能な shape (zeros/ones/full/Tensor::new/from_slice がアロケーション 前に検査する。randn/rand/randint〈rng.rs〉・ arange/linspace/eye/zeros_like/ones_like〈creation.rs。 イシュー #1726〉は checked_numel_for 経由でバイトサイズ側も 検査する。イシュー #1725・PR #1815 codex-review P1 是正)。

§

NonContiguousReshape

非 contiguous なテンソルに対して reshape が呼ばれた。

docs/public-api-design.md §2.2.1 は reshape の非 contiguous ケースの扱いを「案 A(エラー)」「案 B(暗黙コピー)」の 2 案で 未決事項としている。本イシュー(TASK-1.4a)では安全側(設計書が 推奨する案 A)を採用し、この variant で通知する。最終決定は ユーザー承認が必要な採否論点として残っており、案 B へ変更する 場合は Tensor::reshape 内の分岐 1 箇所の差し替えで足りるよう 局所化している。設計書の 5 variant には明示されていないが、 #[non_exhaustive] は後続タスクでの variant 追加を織り込み済みで あるため追加する。

§

BroadcastIncompatible

2 つの shape が NumPy 互換のブロードキャスト規則で両立しない (末尾軸から比較し「両者同一」または「片方が 1」を満たせない)。

broadcast_shape(broadcast.rs)・Tensor::broadcast_to/ Tensor::broadcast_with(tensor.rs)が構築する (#12・TASK-1.4b、docs/public-api-design.md §2.1 の stride 0 ブロードキャスト方針)。broadcast_to 呼び出し時は lhs = 自身の shape・rhs = target shape として構築する。

Fields

§lhs: Vec<usize>
§rhs: Vec<usize>
§

MatmulDimMismatch

matmul(ops_shape::matmul_out_shape)の内部次元(lhs の最終軸と rhs の先頭軸)が一致しない。rank 検査(RankMismatch)を通過した 2 次元 shape 同士でのみ構築される(TASK-1.4c・#13。 docs/public-api-design.md §3.2)。

Fields

§lhs: Vec<usize>
§rhs: Vec<usize>
§

ShapeMismatch

厳密一致を要求する演算(ops_shape::require_same_shape。 例: mse_loss の予測値と target)で shape が一致しない (TASK-1.4c・#13。docs/public-api-design.md §3.2)。

Fields

§lhs: Vec<usize>
§rhs: Vec<usize>
§

IndexOutOfRange

gather/scatter/scatter_add(イシュー #1776)の index 添字値が対象軸の範囲 [0, dim_size) を外れている。

呼び出し元(fandhe_ai_autodiff::var::Var::gather/scatter/ scatter_add)は forward 時点で同じ検査を行うが、 BackendOps::gather/scatter(CPU 実装 backend-cpu::gather_scatter)を直接呼び出す経路(Var を 経由しない)でも誤書き込みを防ぐため、実装側でも独立に検査 する(.claude/rules/security.md A08。判定迂回経路を作らない。 codex-review 指摘)。

Fields

§dim: usize
§index: i64
§dim_size: usize
§

IndexRangeOverflow

sort/topk(イシュー #1733)が出力添字(Tensor<i32>)へ 書き戻す元の usize 添字が i32 の表現範囲(i32::MAX)を 超える。

backend-cpu::sort_topk(BackendOps::sort/topk の CPU 実装本体)・fandhe_ai_autodiff::eval::sort/eval::topk (BackendOps が Unsupported を返したときのみ使われる ホスト参照実装。CUDA/Metal は #1741 未実装のため現状は こちらが実行される)はいずれも orig_idx as i32 のような 無検査キャストではなく i32::try_from(orig_idx) で検査し、 失敗時に本 variant を返す(codex-review 指摘・PR #1818。 .claude/rules/security.md A08 の「判定迂回経路を作らない」 に基づき、両実装で同一の添字契約を満たす)。dim 軸のサイズが i32::MAX を超える巨大テンソルでのみ構築されうる。

backend-cpu::unique::unique_ext(BackendOps::unique_ext の CPU 実装本体。イシュー #2153)も同じ契約を再利用する: inverse(Tensor<i32>)へ書き戻す対象要素数(dim=None は numel・dim=Some(d) は shape[d])が i32 の表現範囲を 超える場合、vec![0i32; ...] の確保前に i32::try_from で 検査し本 variant を返す(codex-review P1 是正・PR #2270。 Var/autodiff 層の topk_unique_ops::ensure_target_len_fits_i32 事前検査を経由しない直接呼び出し経路でも過大確保・無検査切り 詰めを防ぐ独立検査)。

Fields

§index: usize

Trait Implementations§

Source§

impl Clone for ShapeError

Source§

fn clone(&self) -> ShapeError

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShapeError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for ShapeError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for ShapeError

Source§

impl Error for ShapeError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<ShapeError> for CreationError

Source§

fn from(err: ShapeError) -> CreationError

Converts to this type from the input type.
Source§

impl From<ShapeError> for DataError

Source§

fn from(err: ShapeError) -> DataError

Converts to this type from the input type.
Source§

impl From<ShapeError> for NpyError

Source§

fn from(e: ShapeError) -> NpyError

Converts to this type from the input type.
Source§

impl From<ShapeError> for RngError

Source§

fn from(err: ShapeError) -> RngError

Converts to this type from the input type.
Source§

impl From<ShapeError> for AutodiffError

Source§

fn from(err: ShapeError) -> AutodiffError

Converts to this type from the input type.
Source§

impl PartialEq for ShapeError

Source§

fn eq(&self, other: &ShapeError) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ShapeError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V