1313# New operator library with a custom namespace to allow fusion etc.
1414lib = Library ("cortex_m" , "DEF" )
1515
16+ # Import these for the cadence function signatures.
17+ import executorch .backends .cortex_m .cortex_m_ops_lib # noqa: F401
18+
19+ ###
20+ # add.Tensor
21+ ###
22+
23+ lib .define (
24+ "add.Tensor(Tensor self, Tensor other, ScalarType dtype) -> (Tensor Z)"
25+ )
26+
27+ lib .define (
28+ "add_Tensor.out(Tensor self, Tensor other, ScalarType dtype, Tensor(a!) out) -> Tensor(a!)"
29+ )
30+
31+ @impl (lib , "add.Tensor" , "CompositeExplicitAutograd" )
32+ def aten_add_tensor_impl (
33+ input1 : torch .Tensor ,
34+ input2 : torch .Tensor ,
35+ dtype : torch .dtype ,
36+ out : torch .Tensor ,
37+ ) -> torch .Tensor :
38+ """
39+ The implementation of aten add.Tensor.
40+ """
41+ return exir_ops .edge .cortex_m .add .Tensor (input1 , input2 , dtype )
42+
43+ ###
44+ # add.out
45+ ###
46+
47+ lib .define (
48+ "add(Tensor input1, Tensor input2, ScalarType dtype) -> (Tensor Z)"
49+ )
50+
51+ lib .define (
52+ "add.out(Tensor input1, Tensor input2, ScalarType dtype, Tensor(a!) out) -> Tensor(a!)"
53+ )
54+
55+ @impl (lib , "add.out" , "CompositeExplicitAutograd" )
56+ def add_out_impl (
57+ input1 : torch .Tensor ,
58+ input2 : torch .Tensor ,
59+ dtype : torch .dtype ,
60+ out : torch .Tensor ,
61+ ) -> torch .Tensor :
62+ """
63+ The implementation of cmsis-nn add.out.
64+ """
65+
66+ return exir_ops .edge .cortex_m .add .default (
67+ input1 , input2 , dtype , dtype
68+ )
69+
1670###
1771# dequantize_per_tensor
1872###
2579 "quantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)"
2680)
2781
28-
2982@register_fake ("cortex_m::quantize_per_tensor" )
3083def quantize_per_tensor_meta (
3184 input : torch .Tensor ,
@@ -37,7 +90,6 @@ def quantize_per_tensor_meta(
3790) -> torch .Tensor :
3891 return torch .empty_like (input , dtype = dtype )
3992
40-
4193@impl (lib , "quantize_per_tensor" , "CompositeExplicitAutograd" )
4294def quantize_per_tensor_impl (
4395 input : torch .Tensor ,
@@ -96,3 +148,19 @@ def dequantize_per_tensor_impl(
96148 return exir_ops .edge .quantized_decomposed .dequantize_per_tensor .default (
97149 input , scale , zero_point , quant_min , quant_max , dtype
98150 )
151+
152+ lib .define (
153+ "softmax(Tensor self, int dim, bool half_to_float) -> Tensor"
154+ )
155+ lib .define (
156+ "softmax.out(Tensor self, int dim, bool half_to_float, *, Tensor(a!) out) -> Tensor(a!)"
157+ )
158+ @impl (lib , "softmax" , "CompositeExplicitAutograd" )
159+ def softmax_impl (self : torch .Tensor , dim : int , half_to_float : bool ) -> torch .Tensor :
160+ # Call your custom edge op or fallback
161+ # return exir_ops.edge.cortex_m.softmax(self, dim, half_to_float)
162+ # ctx = get_kernel_ctx() # gets KernelRuntimeContext*
163+ return {}
164+ @impl (lib , "softmax.out" , "CompositeExplicitAutograd" )
165+ def softmax_out_impl (self : torch .Tensor , dim : int , half_to_float : bool , out : torch .Tensor ) -> torch .Tensor :
166+ return exir_ops .edge .cortex_m .softmax_out (self , dim , half_to_float , out )
0 commit comments