@@ -106,7 +106,7 @@ fn convert_tensor_to_ndarray_f32() {
106106}
107107
108108#[ test]
109- fn convert_ndarray_u8_to_tensor ( ) {
109+ fn convert_ndarray_f64_to_tensor ( ) {
110110 let n = ndarray:: array![ [ 1. , 2. , 3. ] , [ 4. , 5. , 6. ] ] ;
111111 let t = TensorData :: try_from ( n) . unwrap ( ) ;
112112
@@ -125,6 +125,97 @@ fn convert_ndarray_slice_to_tensor() {
125125 assert_eq ! ( t. shape( ) , & [ TensorDimension :: unnamed( 2 ) ] ) ;
126126}
127127
128+ #[ test]
129+ fn convert_ndarray_to_tensor_both_layouts ( ) {
130+ #[ rustfmt:: skip]
131+ let row_major_vec = vec ! [
132+ 1 , 2 , 3 ,
133+ 4 , 5 , 6 ,
134+ 7 , 8 , 9
135+ ] ;
136+ #[ rustfmt:: skip]
137+ let col_major_vec = vec ! [
138+ 1 , 4 , 7 ,
139+ 2 , 5 , 8 ,
140+ 3 , 6 , 9
141+ ] ;
142+
143+ let shape = ndarray:: Ix2 ( 3 , 3 ) ;
144+
145+ let row_major = ndarray:: Array :: from_vec ( row_major_vec)
146+ . into_shape_with_order ( ( shape, ndarray:: Order :: RowMajor ) )
147+ . unwrap ( ) ;
148+
149+ let col_major = ndarray:: Array :: from_vec ( col_major_vec)
150+ . into_shape_with_order ( ( shape, ndarray:: Order :: ColumnMajor ) )
151+ . unwrap ( ) ;
152+
153+ assert ! ( row_major. is_standard_layout( ) ) ;
154+ assert ! ( !col_major. is_standard_layout( ) ) ;
155+
156+ // make sure that the offset is in fact zero, in case ndarray behavior changes
157+ let rm = row_major. clone ( ) ;
158+ let cm = col_major. clone ( ) ;
159+ let ( _, rm_offset) = rm. into_raw_vec_and_offset ( ) ;
160+ let ( _, cm_offset) = cm. into_raw_vec_and_offset ( ) ;
161+ assert_eq ! ( rm_offset. unwrap( ) , 0 ) ;
162+ assert_eq ! ( cm_offset. unwrap( ) , 0 ) ;
163+
164+ let tensor_row_major = TensorData :: try_from ( row_major) . unwrap ( ) ;
165+ let tensor_col_major = TensorData :: try_from ( col_major) . unwrap ( ) ;
166+
167+ assert_eq ! ( tensor_row_major, tensor_col_major) ;
168+ }
169+
170+ #[ test]
171+ fn convert_ndarray_to_tensor_both_layouts_nonzero_offset ( ) {
172+ #[ rustfmt:: skip]
173+ let row_major_vec = vec ! [
174+ 1 , 2 , 3 ,
175+ 4 , 5 , 6 ,
176+ 7 , 8 , 9
177+ ] ;
178+ #[ rustfmt:: skip]
179+ let col_major_vec = vec ! [
180+ 1 , 4 , 7 ,
181+ 2 , 5 , 8 ,
182+ 3 , 6 , 9
183+ ] ;
184+
185+ let shape = ndarray:: Ix2 ( 3 , 3 ) ;
186+
187+ let row_major = ndarray:: Array :: from_vec ( row_major_vec)
188+ . into_shape_with_order ( ( shape, ndarray:: Order :: RowMajor ) )
189+ . unwrap ( ) ;
190+ assert ! ( row_major. is_standard_layout( ) ) ;
191+ let row_major_nonzero_offset = row_major. slice_move ( ndarray:: s![ 1 .., ..] ) ;
192+
193+ let col_major = ndarray:: Array :: from_vec ( col_major_vec)
194+ . into_shape_with_order ( ( shape, ndarray:: Order :: ColumnMajor ) )
195+ . unwrap ( ) ;
196+ assert ! ( !col_major. is_standard_layout( ) ) ;
197+ let col_major_nonzero_offset = col_major. slice_move ( ndarray:: s![ 1 .., ..] ) ;
198+
199+ assert ! ( row_major_nonzero_offset. is_standard_layout( ) ) ;
200+ assert ! ( !col_major_nonzero_offset. is_standard_layout( ) ) ;
201+
202+ // make sure that the offset is in fact non-zero, in case ndarray behavior changes
203+ let rmno = row_major_nonzero_offset. clone ( ) ;
204+ let cmno = col_major_nonzero_offset. clone ( ) ;
205+ let ( _, rm_offset) = rmno. into_raw_vec_and_offset ( ) ;
206+ let ( _, cm_offset) = cmno. into_raw_vec_and_offset ( ) ;
207+ assert ! ( rm_offset. unwrap( ) > 0 ) ;
208+ assert ! ( cm_offset. unwrap( ) > 0 ) ;
209+
210+ let tensor_row_major_nonzero_offset = TensorData :: try_from ( row_major_nonzero_offset) . unwrap ( ) ;
211+ let tensor_col_major_nonzero_offset = TensorData :: try_from ( col_major_nonzero_offset) . unwrap ( ) ;
212+
213+ assert_eq ! (
214+ tensor_row_major_nonzero_offset,
215+ tensor_col_major_nonzero_offset
216+ ) ;
217+ }
218+
128219#[ test]
129220fn check_slices ( ) {
130221 let t = TensorData :: new (
0 commit comments