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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
//! Utility functions and traits for cuTile Rust.
//!
//! This module provides helper utilities for testing, debugging, and working with
//! floating-point values and tensors.
//!
//! ## Floating-Point Comparisons
//!
//! The [`Float`] trait provides approximate equality comparisons for floating-point
//! types (`f16`, `f32`, `f64`). This is essential for testing GPU computations where
//! exact floating-point equality is often inappropriate due to rounding errors.
//!
//! ## Pretty Printing
//!
//! The [`pretty_print_matrix`] function formats 2D tensors (matrices) in a readable
//! table format for debugging and inspection.
use WithDType;
use f16;
use FloatCore;
/// Trait for approximate floating-point comparisons.
///
/// Provides methods to compare floating-point values with tolerance for rounding errors.
/// This is crucial for testing GPU computations where exact equality is often inappropriate.
///
/// Implemented for `f16`, `f32`, and `f64`.
///
/// ## Examples
///
/// ```rust,ignore
/// use cutile::utils::Float;
///
/// let a = 0.1f32 + 0.2f32;
/// let b = 0.3f32;
///
/// // Exact equality might fail due to rounding
/// assert!(a.close(b, 1e-6));
///
/// // Or use machine epsilon
/// assert!(a.epsilon_close(b));
/// ```
/// Prints a 2D tensor (matrix) in a formatted table.
///
/// Formats and prints a matrix with column indices as headers and values
/// formatted to one decimal place. Useful for debugging and visualizing
/// small matrices.
///
/// ## Parameters
///
/// - `mat`: A 2D `candle_core::Tensor` to print
///
/// ## Examples
///
/// ```rust,ignore
/// use cutile::api;
/// use cutile::utils::pretty_print_matrix;
///
/// let matrix = api::arange::<f32>(9).reshape([3, 3]).await;
/// let cpu_matrix = api::copy_to_host(&Arc::new(matrix)).await;
/// pretty_print_matrix::<f32>(&cpu_matrix);
///
/// // Output:
/// // 0 1 2
/// // | 0.0 | 1.0 | 2.0 |
/// // | 3.0 | 4.0 | 5.0 |
/// // | 6.0 | 7.0 | 8.0 |
/// ```
///
/// ## Note
///
/// This function is intended for small matrices and debugging purposes.
/// For large matrices, consider using other visualization tools.