@@ -211,10 +211,227 @@ pub unsafe extern "C" fn oxideon_tensor_relu(x: *const OxideonTensor) -> *mut Ox
211211 guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . relu ( ) } ) )
212212}
213213
214+ /// Elementwise GELU (exact, erf-based).
215+ ///
216+ /// # Safety
217+ /// `x` must be a valid `OxideonTensor` pointer.
218+ #[ unsafe( no_mangle) ]
219+ pub unsafe extern "C" fn oxideon_tensor_gelu ( x : * const OxideonTensor ) -> * mut OxideonTensor {
220+ if x. is_null ( ) {
221+ set_err ( "null operand in oxideon_tensor_gelu" ) ;
222+ return ptr:: null_mut ( ) ;
223+ }
224+ guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . gelu ( ) } ) )
225+ }
226+
227+ /// Elementwise SiLU (swish) `x * sigmoid(x)`.
228+ ///
229+ /// # Safety
230+ /// `x` must be a valid `OxideonTensor` pointer.
231+ #[ unsafe( no_mangle) ]
232+ pub unsafe extern "C" fn oxideon_tensor_silu ( x : * const OxideonTensor ) -> * mut OxideonTensor {
233+ if x. is_null ( ) {
234+ set_err ( "null operand in oxideon_tensor_silu" ) ;
235+ return ptr:: null_mut ( ) ;
236+ }
237+ guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . silu ( ) } ) )
238+ }
239+
240+ /// Softmax along `axis` (negative values count from the last dimension).
241+ ///
242+ /// # Safety
243+ /// `x` must be a valid `OxideonTensor` pointer.
244+ #[ unsafe( no_mangle) ]
245+ pub unsafe extern "C" fn oxideon_tensor_softmax ( x : * const OxideonTensor , axis : i64 ) -> * mut OxideonTensor {
246+ if x. is_null ( ) {
247+ set_err ( "null operand in oxideon_tensor_softmax" ) ;
248+ return ptr:: null_mut ( ) ;
249+ }
250+ guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . softmax ( axis as isize ) } ) )
251+ }
252+
253+ /// Reshapes `x` to `shape[ndim]` (the element count must be unchanged).
254+ ///
255+ /// # Safety
256+ /// `x` must be a valid `OxideonTensor` pointer and `shape` must point to `ndim` valid `usize` values.
257+ #[ unsafe( no_mangle) ]
258+ pub unsafe extern "C" fn oxideon_tensor_reshape (
259+ x : * const OxideonTensor ,
260+ shape : * const usize ,
261+ ndim : usize ,
262+ ) -> * mut OxideonTensor {
263+ if x. is_null ( ) || ( ndim > 0 && shape. is_null ( ) ) {
264+ set_err ( "null pointer passed to oxideon_tensor_reshape" ) ;
265+ return ptr:: null_mut ( ) ;
266+ }
267+ let shp = if ndim == 0 { & [ ] [ ..] } else { unsafe { std:: slice:: from_raw_parts ( shape, ndim) } } ;
268+ guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . reshape ( shp) } ) )
269+ }
270+
271+ /// Swaps dimensions `dim0` and `dim1` (negative values count from the last dimension).
272+ ///
273+ /// # Safety
274+ /// `x` must be a valid `OxideonTensor` pointer.
275+ #[ unsafe( no_mangle) ]
276+ pub unsafe extern "C" fn oxideon_tensor_transpose ( x : * const OxideonTensor , dim0 : i64 , dim1 : i64 ) -> * mut OxideonTensor {
277+ if x. is_null ( ) {
278+ set_err ( "null operand in oxideon_tensor_transpose" ) ;
279+ return ptr:: null_mut ( ) ;
280+ }
281+ guard ( ptr:: null_mut ( ) , || boxed ( unsafe { ( * x) . inner . transpose ( dim0 as isize , dim1 as isize ) } ) )
282+ }
283+
284+ /// Layer normalization over the trailing `normalized_ndim` dimensions, whose sizes are `normalized_shape[normalized_ndim]`.
285+ /// `weight` and `bias` are optional (pass NULL to skip them).
286+ ///
287+ /// # Safety
288+ /// `x` must be a valid `OxideonTensor` pointer, `normalized_shape` must point to `normalized_ndim` valid `usize` values,
289+ /// and `weight` / `bias` must each be NULL or a valid `OxideonTensor` pointer.
290+ #[ unsafe( no_mangle) ]
291+ pub unsafe extern "C" fn oxideon_tensor_layer_norm (
292+ x : * const OxideonTensor ,
293+ normalized_shape : * const usize ,
294+ normalized_ndim : usize ,
295+ weight : * const OxideonTensor ,
296+ bias : * const OxideonTensor ,
297+ eps : f64 ,
298+ ) -> * mut OxideonTensor {
299+ if x. is_null ( ) || normalized_shape. is_null ( ) || normalized_ndim == 0 {
300+ set_err ( "null pointer or empty normalized_shape passed to oxideon_tensor_layer_norm" ) ;
301+ return ptr:: null_mut ( ) ;
302+ }
303+ let shp = unsafe { std:: slice:: from_raw_parts ( normalized_shape, normalized_ndim) } ;
304+ let ( w, b) = unsafe { ( weight. as_ref ( ) . map ( |t| & t. inner ) , bias. as_ref ( ) . map ( |t| & t. inner ) ) } ;
305+ guard ( ptr:: null_mut ( ) , || boxed ( crate :: nn:: functional_ext:: layer_norm ( unsafe { & ( * x) . inner } , shp, w, b, eps) ) )
306+ }
307+
308+ /// 2D convolution of `input: [N, C_in, H, W]` with `weight: [C_out, C_in, kH, kW]` and an optional `bias: [C_out]` (NULL to skip).
309+ ///
310+ /// # Safety
311+ /// `input` and `weight` must be valid `OxideonTensor` pointers and `bias` must be NULL or a valid `OxideonTensor` pointer.
312+ #[ unsafe( no_mangle) ]
313+ pub unsafe extern "C" fn oxideon_tensor_conv2d (
314+ input : * const OxideonTensor ,
315+ weight : * const OxideonTensor ,
316+ bias : * const OxideonTensor ,
317+ stride_h : usize ,
318+ stride_w : usize ,
319+ pad_h : usize ,
320+ pad_w : usize ,
321+ dilation_h : usize ,
322+ dilation_w : usize ,
323+ ) -> * mut OxideonTensor {
324+ if input. is_null ( ) || weight. is_null ( ) {
325+ set_err ( "null operand in oxideon_tensor_conv2d" ) ;
326+ return ptr:: null_mut ( ) ;
327+ }
328+ let b = unsafe { bias. as_ref ( ) . map ( |t| & t. inner ) } ;
329+ guard ( ptr:: null_mut ( ) , || {
330+ boxed ( unsafe {
331+ ( * input) . inner . conv2d ( & ( * weight) . inner , b, ( stride_h, stride_w) , ( pad_h, pad_w) , ( dilation_h, dilation_w) )
332+ } )
333+ } )
334+ }
335+
214336#[ cfg( test) ]
215337mod tests {
216338 use super :: * ;
217339
340+ unsafe fn make ( data : & [ f32 ] , shape : & [ usize ] ) -> * mut OxideonTensor {
341+ unsafe { oxideon_tensor_from_f32 ( data. as_ptr ( ) , data. len ( ) , shape. as_ptr ( ) , shape. len ( ) ) }
342+ }
343+
344+ unsafe fn read ( t : * const OxideonTensor ) -> Vec < f32 > {
345+ let mut out = vec ! [ 0.0f32 ; unsafe { oxideon_tensor_numel( t) } ] ;
346+ assert_eq ! ( unsafe { oxideon_tensor_copy_f32( t, out. as_mut_ptr( ) , out. len( ) ) } , 0 ) ;
347+ out
348+ }
349+
350+ fn assert_close ( a : & [ f32 ] , b : & [ f32 ] ) {
351+ assert_eq ! ( a. len( ) , b. len( ) ) ;
352+ for ( x, y) in a. iter ( ) . zip ( b) {
353+ assert ! ( ( x - y) . abs( ) < 1e-5 , "{a:?} vs {b:?}" ) ;
354+ }
355+ }
356+
357+ #[ test]
358+ fn c_api_activations_softmax_and_layer_norm ( ) {
359+ unsafe {
360+ let x = make ( & [ -1.0 , 0.0 , 1.0 , 2.0 ] , & [ 2 , 2 ] ) ;
361+ let g = oxideon_tensor_gelu ( x) ;
362+ assert_close ( & read ( g) , & [ -0.158_655_26 , 0.0 , 0.841_344_7 , 1.954_499_7 ] ) ;
363+ let s = oxideon_tensor_silu ( x) ;
364+ assert_close ( & read ( s) , & [ -0.268_941_4 , 0.0 , 0.731_058_6 , 1.761_594_2 ] ) ;
365+ let sm = oxideon_tensor_softmax ( x, -1 ) ;
366+ assert_close ( & read ( sm) , & [ 0.268_941_4 , 0.731_058_6 , 0.268_941_4 , 0.731_058_6 ] ) ;
367+ assert ! ( oxideon_tensor_softmax( x, 5 ) . is_null( ) , "out-of-range axis is an error" ) ;
368+
369+ // layer_norm over the last dim: each row [a, b] -> [-1, 1] (eps = 0), then weight 2 / bias 1 -> [-1, 3]
370+ let w = make ( & [ 2.0 , 2.0 ] , & [ 2 ] ) ;
371+ let b = make ( & [ 1.0 , 1.0 ] , & [ 2 ] ) ;
372+ let ln = oxideon_tensor_layer_norm ( x, [ 2usize ] . as_ptr ( ) , 1 , std:: ptr:: null ( ) , std:: ptr:: null ( ) , 0.0 ) ;
373+ assert_close ( & read ( ln) , & [ -1.0 , 1.0 , -1.0 , 1.0 ] ) ;
374+ let lnwb = oxideon_tensor_layer_norm ( x, [ 2usize ] . as_ptr ( ) , 1 , w, b, 0.0 ) ;
375+ assert_close ( & read ( lnwb) , & [ -1.0 , 3.0 , -1.0 , 3.0 ] ) ;
376+ assert ! ( oxideon_tensor_layer_norm( x, [ 3usize ] . as_ptr( ) , 1 , w, b, 1e-5 ) . is_null( ) , "mismatched normalized_shape" ) ;
377+ assert ! ( oxideon_tensor_layer_norm( x, std:: ptr:: null( ) , 1 , w, b, 1e-5 ) . is_null( ) ) ;
378+
379+ for t in [ x, g, s, sm, w, b, ln, lnwb] {
380+ oxideon_tensor_free ( t) ;
381+ }
382+ }
383+ }
384+
385+ #[ test]
386+ fn c_api_reshape_and_transpose ( ) {
387+ unsafe {
388+ let x = make ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] , & [ 2 , 3 ] ) ;
389+ let r = oxideon_tensor_reshape ( x, [ 3usize , 2 ] . as_ptr ( ) , 2 ) ;
390+ let mut shp = [ 0usize ; 2 ] ;
391+ assert_eq ! ( oxideon_tensor_shape( r, shp. as_mut_ptr( ) , 2 ) , 0 ) ;
392+ assert_eq ! ( shp, [ 3 , 2 ] ) ;
393+ assert_eq ! ( read( r) , [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] ) ;
394+ assert ! ( oxideon_tensor_reshape( x, [ 4usize ] . as_ptr( ) , 1 ) . is_null( ) , "element count must match" ) ;
395+ assert ! ( oxideon_tensor_reshape( x, std:: ptr:: null( ) , 2 ) . is_null( ) ) ;
396+
397+ let t = oxideon_tensor_transpose ( x, 0 , 1 ) ;
398+ assert_eq ! ( oxideon_tensor_shape( t, shp. as_mut_ptr( ) , 2 ) , 0 ) ;
399+ assert_eq ! ( shp, [ 3 , 2 ] ) ;
400+ assert_eq ! ( read( t) , [ 1.0 , 4.0 , 2.0 , 5.0 , 3.0 , 6.0 ] ) ;
401+ let t_neg = oxideon_tensor_transpose ( x, -1 , -2 ) ;
402+ assert_eq ! ( read( t_neg) , [ 1.0 , 4.0 , 2.0 , 5.0 , 3.0 , 6.0 ] ) ;
403+ assert ! ( oxideon_tensor_transpose( x, 0 , 7 ) . is_null( ) ) ;
404+
405+ for p in [ x, r, t, t_neg] {
406+ oxideon_tensor_free ( p) ;
407+ }
408+ }
409+ }
410+
411+ #[ test]
412+ fn c_api_conv2d ( ) {
413+ unsafe {
414+ // 1x1x3x3 input, 1x1x2x2 all-ones kernel, bias 0.5: each output is a 2x2 window sum + 0.5
415+ let x = make ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0 ] , & [ 1 , 1 , 3 , 3 ] ) ;
416+ let w = make ( & [ 1.0 ; 4 ] , & [ 1 , 1 , 2 , 2 ] ) ;
417+ let b = make ( & [ 0.5 ] , & [ 1 ] ) ;
418+ let y = oxideon_tensor_conv2d ( x, w, b, 1 , 1 , 0 , 0 , 1 , 1 ) ;
419+ assert_close ( & read ( y) , & [ 12.5 , 16.5 , 24.5 , 28.5 ] ) ;
420+ let y_nb = oxideon_tensor_conv2d ( x, w, std:: ptr:: null ( ) , 1 , 1 , 0 , 0 , 1 , 1 ) ;
421+ assert_close ( & read ( y_nb) , & [ 12.0 , 16.0 , 24.0 , 28.0 ] ) ;
422+ // padding 1 grows the output to 4x4
423+ let y_pad = oxideon_tensor_conv2d ( x, w, std:: ptr:: null ( ) , 1 , 1 , 1 , 1 , 1 , 1 ) ;
424+ assert_eq ! ( oxideon_tensor_numel( y_pad) , 16 ) ;
425+ // a 3D input is an error, not a crash
426+ let flat = make ( & [ 1.0 ; 9 ] , & [ 3 , 3 , 1 ] ) ;
427+ assert ! ( oxideon_tensor_conv2d( flat, w, std:: ptr:: null( ) , 1 , 1 , 0 , 0 , 1 , 1 ) . is_null( ) ) ;
428+ assert ! ( oxideon_tensor_conv2d( std:: ptr:: null( ) , w, std:: ptr:: null( ) , 1 , 1 , 0 , 0 , 1 , 1 ) . is_null( ) ) ;
429+ for p in [ x, w, b, y, y_nb, y_pad, flat] {
430+ oxideon_tensor_free ( p) ;
431+ }
432+ }
433+ }
434+
218435 #[ test]
219436 fn c_api_roundtrip_matmul_and_relu ( ) {
220437 let a_data = [ 1.0f32 , -2.0 , 3.0 , 4.0 ] ;
0 commit comments