#[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
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 の入力を検出した
際に構築する。
ElementCountMismatch
shape の要素数積とデータ長が一致しない
(Tensor::new/from_slice が data.len() と shape の要素数積を
突き合わせる際に返す)。
AxisOutOfRange
軸番号(dim)がテンソルの rank 範囲外
(transpose/narrow/permute の軸引数)。
DuplicateAxis
permute の perm に同一軸が複数回指定された(順列として不正)。
permute(イシュー #274)専用の検査で構築する。
NarrowOutOfBounds
narrow の [start, start+len) が対象軸のサイズを超える。
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 として構築する。
MatmulDimMismatch
matmul(ops_shape::matmul_out_shape)の内部次元(lhs の最終軸と
rhs の先頭軸)が一致しない。rank 検査(RankMismatch)を通過した
2 次元 shape 同士でのみ構築される(TASK-1.4c・#13。
docs/public-api-design.md §3.2)。
ShapeMismatch
厳密一致を要求する演算(ops_shape::require_same_shape。
例: mse_loss の予測値と target)で shape が一致しない
(TASK-1.4c・#13。docs/public-api-design.md §3.2)。
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 指摘)。
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
事前検査を経由しない直接呼び出し経路でも過大確保・無検査切り
詰めを防ぐ独立検査)。
Trait Implementations§
Source§impl Clone for ShapeError
impl Clone for ShapeError
Source§fn clone(&self) -> ShapeError
fn clone(&self) -> ShapeError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ShapeError
impl Debug for ShapeError
Source§impl Display for ShapeError
impl Display for ShapeError
impl Eq for ShapeError
Source§impl Error for ShapeError
impl Error for ShapeError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<ShapeError> for CreationError
impl From<ShapeError> for CreationError
Source§fn from(err: ShapeError) -> CreationError
fn from(err: ShapeError) -> CreationError
Source§impl From<ShapeError> for DataError
impl From<ShapeError> for DataError
Source§fn from(err: ShapeError) -> DataError
fn from(err: ShapeError) -> DataError
Source§impl From<ShapeError> for NpyError
impl From<ShapeError> for NpyError
Source§fn from(e: ShapeError) -> NpyError
fn from(e: ShapeError) -> NpyError
Source§impl From<ShapeError> for RngError
impl From<ShapeError> for RngError
Source§fn from(err: ShapeError) -> RngError
fn from(err: ShapeError) -> RngError
Source§impl From<ShapeError> for AutodiffError
impl From<ShapeError> for AutodiffError
Source§fn from(err: ShapeError) -> AutodiffError
fn from(err: ShapeError) -> AutodiffError
Source§impl PartialEq for ShapeError
impl PartialEq for ShapeError
impl StructuralPartialEq for ShapeError
Auto Trait Implementations§
impl Freeze for ShapeError
impl RefUnwindSafe for ShapeError
impl Send for ShapeError
impl Sync for ShapeError
impl Unpin for ShapeError
impl UnsafeUnpin for ShapeError
impl UnwindSafe for ShapeError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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