1 // Copyright 2020 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <math.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <xnnpack.h>
11 #include <xnnpack/log.h>
12 #include <xnnpack/params.h>
13 #include <xnnpack/subgraph.h>
14 
15 
xnn_define_leaky_relu(xnn_subgraph_t subgraph,float negative_slope,uint32_t input_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_leaky_relu(
17   xnn_subgraph_t subgraph,
18   float negative_slope,
19   uint32_t input_id,
20   uint32_t output_id,
21   uint32_t flags)
22 {
23   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
24     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
25       xnn_node_type_to_string(xnn_node_type_leaky_relu));
26     return xnn_status_uninitialized;
27   }
28 
29   if (!isfinite(negative_slope)) {
30     xnn_log_error(
31       "failed to create %s operator with %f negative slope: finite number expected",
32       xnn_node_type_to_string(xnn_node_type_leaky_relu),
33       negative_slope);
34     return xnn_status_invalid_parameter;
35   }
36 
37   if (input_id >= subgraph->num_values) {
38     xnn_log_error(
39       "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
40       xnn_node_type_to_string(xnn_node_type_leaky_relu), input_id);
41     return xnn_status_invalid_parameter;
42   }
43 
44   if (output_id >= subgraph->num_values) {
45     xnn_log_error(
46       "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
47       xnn_node_type_to_string(xnn_node_type_leaky_relu), output_id);
48     return xnn_status_invalid_parameter;
49   }
50 
51   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
52   if (node == NULL) {
53     return xnn_status_out_of_memory;
54   }
55 
56   node->type = xnn_node_type_leaky_relu;
57   node->params.leaky_relu.negative_slope = negative_slope;
58   node->num_inputs = 1;
59   node->inputs[0] = input_id;
60   node->num_outputs = 1;
61   node->outputs[0] = output_id;
62   node->flags = flags;
63 
64   return xnn_status_success;
65 }
66