1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//! Unified error handling system for RusTorch
//! RusTorch用統一エラーハンドリングシステム
use std::error::Error;
pub mod context;
/// Main error type for RusTorch operations
/// RusTorch操作のメインエラー型
#[derive(Debug, thiserror::Error)]
pub enum RusTorchError {
/// Tensor operation failed
/// テンソル操作失敗
#[error("Tensor operation failed: {message}")]
TensorOp {
/// Error message describing the tensor operation failure
/// テンソル操作失敗を説明するエラーメッセージ
message: String,
/// Optional underlying error cause
/// オプションの根本的なエラー原因
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Shape mismatch between tensors
/// テンソル間の形状不一致
#[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
/// Expected tensor shape
/// 期待されるテンソル形状
expected: Vec<usize>,
/// Actual tensor shape that was provided
/// 提供された実際のテンソル形状
actual: Vec<usize>,
},
/// Device operation failed
/// デバイス操作失敗
#[error("Device error on {device}: {message}")]
Device {
/// Device identifier where the error occurred
/// エラーが発生したデバイス識別子
device: String,
/// Error message describing the device issue
/// デバイスの問題を説明するエラーメッセージ
message: String,
},
/// Backend not available
/// バックエンドが利用不可
#[error("Backend not available: {backend}")]
BackendUnavailable {
/// Name of the unavailable backend
/// 利用不可なバックエンド名
backend: String,
},
/// Memory allocation failed
/// メモリ割り当て失敗
#[error("Memory allocation failed: {size} bytes on {device}")]
MemoryAllocation {
/// Size of the failed allocation in bytes
/// 失敗したアロケーションのサイズ(バイト)
size: usize,
/// Device where allocation failed
/// アロケーションが失敗したデバイス
device: String,
},
/// Invalid operation parameters
/// 無効な操作パラメータ
#[error("Invalid parameters for {operation}: {message}")]
InvalidParameters {
/// Name of the operation with invalid parameters
/// 無効なパラメータを持つ操作名
operation: String,
/// Description of the parameter issue
/// パラメータの問題の説明
message: String,
},
/// Invalid operation
/// 無効な操作
#[error("{operation}: {message}")]
InvalidOperation {
/// Name of the invalid operation
/// 無効な操作名
operation: String,
/// Detailed error message
/// 詳細なエラーメッセージ
message: String,
},
/// Neural network layer error
/// ニューラルネットワークレイヤーエラー
#[error("Neural network error in {layer}: {message}")]
NeuralNetwork {
/// Name of the neural network layer
/// ニューラルネットワークレイヤー名
layer: String,
/// Error message from the layer
/// レイヤーからのエラーメッセージ
message: String,
},
/// Automatic differentiation error
/// 自動微分エラー
#[error("Autograd error: {message}")]
Autograd {
/// Autograd error description
/// 自動微分エラーの説明
message: String,
},
/// Model import/export error
/// モデルインポート/エクスポートエラー
#[error("Model I/O error: {message}")]
ModelIo {
/// Model I/O error description
/// モデルI/Oエラーの説明
message: String,
/// Optional underlying I/O error
/// オプションの根本的なI/Oエラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Import-specific error
/// インポート固有エラー
#[error("Import error: {message}")]
Import {
/// Import operation error message
/// インポート操作エラーメッセージ
message: String,
/// Optional underlying import error
/// オプションの根本的なインポートエラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Data loading error
/// データ読み込みエラー
#[error("Data loading error: {message}")]
DataLoading {
/// Data loading error description
/// データ読み込みエラーの説明
message: String,
/// Optional underlying data loading error
/// オプションの根本的なデータ読み込みエラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// GPU-specific errors
/// GPU固有エラー
#[error("GPU error: {message}")]
Gpu {
/// GPU operation error description
/// GPU操作エラーの説明
message: String,
/// Optional underlying GPU error
/// オプションの根本的なGPUエラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Vision processing errors
/// 視覚処理エラー
#[error("Vision processing error: {message}")]
Vision {
/// Vision processing error description
/// 視覚処理エラーの説明
message: String,
/// Optional underlying vision error
/// オプションの根本的な視覚エラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Distributed computing errors
/// 分散コンピューティングエラー
#[error("Distributed computing error: {message}")]
Distributed {
/// Distributed computing error description
/// 分散コンピューティングエラーの説明
message: String,
/// Optional underlying distributed error
/// オプションの根本的な分散エラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Visualization errors
/// 可視化エラー
#[error("Visualization error: {message}")]
Visualization {
/// Visualization error description
/// 可視化エラーの説明
message: String,
/// Optional underlying visualization error
/// オプションの根本的な可視化エラー
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
/// Profiling and performance analysis errors
/// プロファイリング・パフォーマンス分析エラー
#[error("Profiling error: {message}")]
Profiling {
/// Profiling error description
/// プロファイリングエラーの説明
message: String,
},
/// Data validation and quality assurance errors
/// データ検証・品質保証エラー
#[error("Validation error: {message}")]
Validation {
/// Data validation error description
/// データ検証エラーの説明
message: String,
},
/// Debug system and logging errors
/// デバッグシステム・ログエラー
#[error("Debug system error: {message}")]
Debug {
/// Debug system error description
/// デバッグシステムエラーの説明
message: String,
},
/// Kernel compilation error for GPU operations
/// GPU操作のカーネルコンパイルエラー
#[error("Kernel compilation error: {message}")]
KernelCompilation {
/// Kernel compilation error description
/// カーネルコンパイルエラーの説明
message: String,
},
/// DataLoader system errors (Phase 5)
/// DataLoaderシステムエラー(フェーズ5)
#[error("DataLoader error: {message}")]
Dataloader {
/// DataLoader error description
/// DataLoaderエラーの説明
message: String,
},
/// Serialization and model I/O errors (Phase 9)
/// シリアライゼーション・モデルI/Oエラー(フェーズ9)
#[error("Serialization error in {operation}: {message}")]
SerializationError {
/// Operation that caused the serialization error
/// シリアライゼーションエラーを引き起こした操作
operation: String,
/// Serialization error description
/// シリアライゼーションエラーの説明
message: String,
},
/// Input/Output and serialization errors
/// 入出力・シリアライゼーションエラー
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
/// Feature not implemented yet
/// 機能未実装
#[error("Not implemented: {feature}")]
NotImplemented {
/// Feature that is not yet implemented
/// 未実装の機能
feature: String,
},
/// Out of memory error
/// メモリ不足エラー
#[error("Out of memory: requested {requested} bytes, available {available} bytes")]
OutOfMemory {
/// Requested memory in bytes
/// 要求されたメモリ(バイト)
requested: usize,
/// Available memory in bytes
/// 利用可能メモリ(バイト)
available: usize,
},
/// Serialization error
/// シリアライゼーションエラー
#[error("Serialization error: {message}")]
Serialization {
/// Serialization error description
/// シリアライゼーションエラーの説明
message: String,
},
}
// All individual error types removed - only RusTorchError is used now
// 個別エラー型は全て削除 - RusTorchErrorのみ使用
/// Unified Result type for all RusTorch operations - the ONLY Result type used
/// 全RusTorch操作用統一Result型 - 唯一使用されるResult型
pub type RusTorchResult<T> = Result<T, RusTorchError>;
// All Display implementations and From traits removed - RusTorchError handles everything directly
// 全DisplayとFromトレイト削除 - RusTorchErrorが直接全てを処理
// Comprehensive convenience constructors for unified error handling
#[allow(non_snake_case)] // Allow PascalCase for error constructor methods
impl RusTorchError {
// === Tensor Operations ===
/// Create a tensor operation error
pub fn tensor_op(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: message.into(),
source: None,
}
}
/// Create a shape mismatch error
pub fn shape_mismatch(expected: &[usize], actual: &[usize]) -> Self {
RusTorchError::ShapeMismatch {
expected: expected.to_vec(),
actual: actual.to_vec(),
}
}
/// Create empty tensor error
pub fn empty_tensor() -> Self {
RusTorchError::TensorOp {
message: "Operation on empty tensor".into(),
source: None,
}
}
/// Create index out of bounds error
pub fn index_out_of_bounds(index: &[usize], shape: &[usize]) -> Self {
RusTorchError::TensorOp {
message: format!("Index {:?} out of bounds for shape {:?}", index, shape),
source: None,
}
}
/// Create invalid dimension error
pub fn invalid_dimension(dim: usize, max_dim: usize) -> Self {
RusTorchError::TensorOp {
message: format!("Invalid dimension {} (max: {})", dim, max_dim),
source: None,
}
}
// === Device & GPU Operations ===
/// Create a device error
pub fn device(device: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::Device {
device: device.into(),
message: message.into(),
}
}
/// Create a GPU error
pub fn gpu(message: impl Into<String>) -> Self {
RusTorchError::Gpu {
message: message.into(),
source: None,
}
}
/// Create device not available error
pub fn device_not_available(device: impl Into<String>) -> Self {
RusTorchError::Device {
device: device.into(),
message: "Device not available".into(),
}
}
/// Create memory allocation error
pub fn memory_alloc(size: usize, device: impl Into<String>) -> Self {
RusTorchError::MemoryAllocation {
size,
device: device.into(),
}
}
/// Create kernel compilation error
pub fn kernel_compilation(message: impl Into<String>) -> Self {
RusTorchError::KernelCompilation {
message: message.into(),
}
}
// === Neural Network Operations ===
/// Create neural network error
pub fn neural_network(layer: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::NeuralNetwork {
layer: layer.into(),
message: message.into(),
}
}
/// Create invalid configuration error
pub fn invalid_config(layer: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::NeuralNetwork {
layer: layer.into(),
message: format!("Invalid configuration: {}", message.into()),
}
}
/// Create forward pass error
pub fn forward_error(layer: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::NeuralNetwork {
layer: layer.into(),
message: format!("Forward pass error: {}", message.into()),
}
}
/// Create backward pass error
pub fn backward_error(layer: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::NeuralNetwork {
layer: layer.into(),
message: format!("Backward pass error: {}", message.into()),
}
}
// === Data Operations ===
/// Create data loading error
pub fn data_loading(message: impl Into<String>) -> Self {
RusTorchError::DataLoading {
message: message.into(),
source: None,
}
}
/// Create dataset error
pub fn dataset_error(message: impl Into<String>) -> Self {
RusTorchError::DataLoading {
message: format!("Dataset error: {}", message.into()),
source: None,
}
}
// === Vision Operations ===
/// Create vision processing error
pub fn vision(message: impl Into<String>) -> Self {
RusTorchError::Vision {
message: message.into(),
source: None,
}
}
/// Create invalid image format error
pub fn invalid_image_format(format: impl Into<String>) -> Self {
RusTorchError::Vision {
message: format!("Invalid image format: {}", format.into()),
source: None,
}
}
/// Create invalid image shape error
pub fn invalid_image_shape(shape: &[usize]) -> Self {
RusTorchError::Vision {
message: format!("Invalid image shape: {:?}", shape),
source: None,
}
}
// === Distributed Operations ===
/// Create distributed error
pub fn distributed(message: impl Into<String>) -> Self {
RusTorchError::Distributed {
message: message.into(),
source: None,
}
}
/// Create process group error
pub fn process_group_error(message: impl Into<String>) -> Self {
RusTorchError::Distributed {
message: format!("Process group error: {}", message.into()),
source: None,
}
}
// === Visualization Operations ===
/// Create visualization error
pub fn visualization(message: impl Into<String>) -> Self {
RusTorchError::Visualization {
message: message.into(),
source: None,
}
}
/// Create plotting error
pub fn plotting_error(message: impl Into<String>) -> Self {
RusTorchError::Visualization {
message: format!("Plotting error: {}", message.into()),
source: None,
}
}
// === Autograd Operations ===
/// Create autograd error
pub fn autograd(message: impl Into<String>) -> Self {
RusTorchError::Autograd {
message: message.into(),
}
}
/// Create gradient computation error
pub fn gradient_error(message: impl Into<String>) -> Self {
RusTorchError::Autograd {
message: format!("Gradient error: {}", message.into()),
}
}
// === Model I/O Operations ===
/// Create model I/O error
pub fn model_io(message: impl Into<String>) -> Self {
RusTorchError::ModelIo {
message: message.into(),
source: None,
}
}
/// Create import error
pub fn import_error(message: impl Into<String>) -> Self {
RusTorchError::Import {
message: message.into(),
source: None,
}
}
/// Create unsupported format error
pub fn unsupported_format(format: impl Into<String>) -> Self {
RusTorchError::Import {
message: format!("Unsupported format: {}", format.into()),
source: None,
}
}
// === Parameter Operations ===
/// Create invalid parameter error (simple)
pub fn invalid_parameter(message: impl Into<String>) -> Self {
RusTorchError::InvalidParameters {
operation: "parameter".into(),
message: message.into(),
}
}
/// Create invalid parameters error
pub fn invalid_params(operation: impl Into<String>, message: impl Into<String>) -> Self {
RusTorchError::InvalidParameters {
operation: operation.into(),
message: message.into(),
}
}
/// Create backend unavailable error
pub fn backend_unavailable(backend: impl Into<String>) -> Self {
RusTorchError::BackendUnavailable {
backend: backend.into(),
}
}
// === Type and Numeric Operations ===
/// Create type error
pub fn type_error(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: message.into(),
source: None,
}
}
/// Create numeric error
pub fn numeric(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: message.into(),
source: None,
}
}
// === Additional Helper Functions ===
/// Create unsupported device error
pub fn UnsupportedDevice(message: impl Into<String>) -> Self {
RusTorchError::device("gpu", message)
}
/// Create parallel error
pub fn parallel(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Parallel error: {}", message.into()),
source: None,
}
}
/// Create domain error
pub fn DomainError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Domain error: {}", message.into()),
source: None,
}
}
/// Create overflow error
pub fn OverflowError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Overflow error: {}", message.into()),
source: None,
}
}
/// Create invalid dimensions error
pub fn InvalidDimensions(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Invalid dimensions: {}", message.into()),
source: None,
}
}
/// Create invalid data format error
pub fn InvalidDataFormat(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Invalid data format: {}", message.into()),
source: None,
}
}
/// Create invalid operation error
pub fn InvalidOperation(message: impl Into<String>) -> Self {
RusTorchError::invalid_params("operation", message)
}
/// Create kernel execution error
pub fn KernelExecutionError(message: impl Into<String>) -> Self {
RusTorchError::gpu(format!("Kernel execution error: {}", message.into()))
}
/// Create communication error
pub fn CommunicationError(message: impl Into<String>) -> Self {
RusTorchError::distributed(format!("Communication error: {}", message.into()))
}
// === More Helper Functions ===
/// Create unsupported operation error
pub fn UnsupportedOperation(message: impl Into<String>) -> Self {
RusTorchError::invalid_params(
"operation",
format!("Unsupported operation: {}", message.into()),
)
}
/// Create IO error
pub fn IoError(message: impl Into<String>) -> Self {
RusTorchError::model_io(message)
}
/// Create kernel error
pub fn KernelError(message: impl Into<String>) -> Self {
RusTorchError::gpu(format!("Kernel error: {}", message.into()))
}
/// Create invalid transform params error
pub fn InvalidTransformParams(message: impl Into<String>) -> Self {
RusTorchError::vision(format!("Invalid transform params: {}", message.into()))
}
/// Create process group error
pub fn ProcessGroupError(message: impl Into<String>) -> Self {
RusTorchError::process_group_error(message)
}
/// Create memory error
pub fn MemoryError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Memory error: {}", message.into()),
source: None,
}
}
/// Create plotting error
pub fn PlottingError(message: impl Into<String>) -> Self {
RusTorchError::plotting_error(message)
}
// === Final Helper Functions ===
/// Create memory error (simple)
pub fn memory(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Memory: {}", message.into()),
source: None,
}
}
/// Create invalid model error
pub fn InvalidModel(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Invalid model: {}", message.into()))
}
/// Create dataset error
pub fn DatasetError(message: impl Into<String>) -> Self {
RusTorchError::dataset_error(message)
}
/// Create configuration error
pub fn ConfigurationError(message: impl Into<String>) -> Self {
RusTorchError::invalid_params("config", format!("Configuration error: {}", message.into()))
}
// === Broadcasting and Shape Operations ===
/// Create incompatible shapes error
pub fn IncompatibleShapes(_message: impl Into<String>) -> Self {
RusTorchError::shape_mismatch(&[], &[])
}
/// Create invalid dimension error
pub fn InvalidDimension(message: impl Into<String>) -> Self {
RusTorchError::InvalidDimensions(message)
}
/// Create reshape error
pub fn ReshapeError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Reshape error: {}", message.into()),
source: None,
}
}
/// Create not singleton dimension error
pub fn NotSingletonDimension(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Not singleton dimension: {}", message.into()),
source: None,
}
}
/// Create mismatched dimensions error
pub fn MismatchedDimensions(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Mismatched dimensions: {}", message.into()),
source: None,
}
}
/// Create computation error
pub fn ComputationError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Computation error: {}", message.into()),
source: None,
}
}
// === Additional Missing Functions ===
/// Create config error
pub fn ConfigError(message: impl Into<String>) -> Self {
RusTorchError::invalid_params("config", message)
}
/// Create invalid image shape error
pub fn InvalidImageShape(_message: impl Into<String>) -> Self {
RusTorchError::invalid_image_shape(&[])
}
/// Create convergence error
pub fn ConvergenceError(message: impl Into<String>) -> Self {
RusTorchError::TensorOp {
message: format!("Convergence error: {}", message.into()),
source: None,
}
}
/// Create serialization error
pub fn SerializationError(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Serialization error: {}", message.into()))
}
/// Create parse error
pub fn ParseError(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Parse error: {}", message.into()))
}
/// Create validation error
pub fn ValidationError(message: impl Into<String>) -> Self {
RusTorchError::tensor_op(format!("Validation error: {}", message.into()))
}
/// Create device not available error
pub fn DeviceNotAvailable(message: impl Into<String>) -> Self {
RusTorchError::device_not_available(message.into())
}
/// Create file not found error
pub fn FileNotFound(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("File not found: {}", message.into()))
}
/// Create cluster error
pub fn ClusterError(message: impl Into<String>) -> Self {
RusTorchError::distributed(format!("Cluster error: {}", message.into()))
}
/// Create invalid rank error
pub fn InvalidRank(message: impl Into<String>) -> Self {
RusTorchError::distributed(format!("Invalid rank: {}", message.into()))
}
/// Create deserialization error
pub fn DeserializationError(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Deserialization error: {}", message.into()))
}
/// Create download error
pub fn DownloadError(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Download error: {}", message.into()))
}
/// Create model not found error
pub fn ModelNotFound(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Model not found: {}", message.into()))
}
/// Create verification error
pub fn VerificationError(message: impl Into<String>) -> Self {
RusTorchError::model_io(format!("Verification error: {}", message.into()))
}
/// Create not implemented error
pub fn not_implemented(feature: impl Into<String>) -> Self {
RusTorchError::NotImplemented {
feature: feature.into(),
}
}
/// Create out of memory error
pub fn out_of_memory(requested: usize, available: usize) -> Self {
RusTorchError::OutOfMemory {
requested,
available,
}
}
/// Create serialization error
pub fn serialization_error(message: impl Into<String>) -> Self {
RusTorchError::Serialization {
message: message.into(),
}
}
// === Quantization-Specific Errors (Phase 11) ===
/// Create invalid quantization parameters error
pub fn quantization_invalid_params(
scale: f32,
zero_point: i32,
message: impl Into<String>,
) -> Self {
RusTorchError::InvalidParameters {
operation: "quantization".into(),
message: format!(
"Invalid quantization parameters: scale={}, zero_point={}, {}",
scale,
zero_point,
message.into()
),
}
}
/// Create quantization range overflow error
pub fn quantization_range_overflow(value: f32, min: i32, max: i32) -> Self {
RusTorchError::TensorOp {
message: format!(
"Quantization range overflow: value={}, range=({}, {})",
value, min, max
),
source: None,
}
}
/// Create incompatible quantization schemes error
pub fn quantization_incompatible_schemes(
expected: impl Into<String>,
actual: impl Into<String>,
) -> Self {
RusTorchError::InvalidOperation {
operation: "quantization".into(),
message: format!(
"Incompatible quantization schemes: expected={}, actual={}",
expected.into(),
actual.into()
),
}
}
/// Create quantization hardware not supported error
pub fn quantization_hardware_not_supported(feature: impl Into<String>) -> Self {
RusTorchError::BackendUnavailable {
backend: format!("quantization-{}", feature.into()),
}
}
/// Create quantization calibration failed error
pub fn quantization_calibration_failed(reason: impl Into<String>) -> Self {
RusTorchError::InvalidOperation {
operation: "quantization-calibration".into(),
message: format!("Calibration failed: {}", reason.into()),
}
}
}
// From trait implementations for common error types
impl From<String> for RusTorchError {
fn from(message: String) -> Self {
RusTorchError::model_io(message)
}
}
impl From<&str> for RusTorchError {
fn from(message: &str) -> Self {
RusTorchError::model_io(message.to_string())
}
}
impl From<serde_json::Error> for RusTorchError {
fn from(err: serde_json::Error) -> Self {
RusTorchError::model_io(format!("Serialization error: {}", err))
}
}
impl From<crate::models::serialization::SaveError> for RusTorchError {
fn from(err: crate::models::serialization::SaveError) -> Self {
RusTorchError::model_io(format!("Save error: {}", err))
}
}
impl From<crate::models::serialization::LoadError> for RusTorchError {
fn from(err: crate::models::serialization::LoadError) -> Self {
RusTorchError::model_io(format!("Load error: {}", err))
}
}
impl From<ndarray::ShapeError> for RusTorchError {
fn from(err: ndarray::ShapeError) -> Self {
RusTorchError::TensorOp {
message: format!("Shape error: {}", err),
source: Some(Box::new(err)),
}
}
}