• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_fully_connected(xnn_subgraph_t subgraph,float output_min,float output_max,uint32_t input_id,uint32_t filter_id,uint32_t bias_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_fully_connected(
17   xnn_subgraph_t subgraph,
18   float output_min,
19   float output_max,
20   uint32_t input_id,
21   uint32_t filter_id,
22   uint32_t bias_id,
23   uint32_t output_id,
24   uint32_t flags)
25 {
26   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
27     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
28       xnn_node_type_to_string(xnn_node_type_fully_connected));
29     return xnn_status_uninitialized;
30   }
31 
32   if (isnan(output_min)) {
33     xnn_log_error(
34       "failed to define %s operator with NaN output lower bound: lower bound must be non-NaN",
35       xnn_node_type_to_string(xnn_node_type_fully_connected));
36     return xnn_status_invalid_parameter;
37   }
38 
39   if (isnan(output_max)) {
40     xnn_log_error(
41       "failed to define %s operator with NaN output upper bound: upper bound must be non-NaN",
42       xnn_node_type_to_string(xnn_node_type_fully_connected));
43     return xnn_status_invalid_parameter;
44   }
45 
46   if (output_min >= output_max) {
47     xnn_log_error(
48       "failed to define %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
49       xnn_node_type_to_string(xnn_node_type_fully_connected), output_min, output_max);
50     return xnn_status_invalid_parameter;
51   }
52 
53   if (input_id >= subgraph->num_values) {
54     xnn_log_error(
55       "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
56       xnn_node_type_to_string(xnn_node_type_fully_connected), input_id);
57     return xnn_status_invalid_parameter;
58   }
59 
60   if (filter_id >= subgraph->num_values) {
61     xnn_log_error(
62       "failed to define %s operator with filter ID #%" PRIu32 ": invalid Value ID",
63       xnn_node_type_to_string(xnn_node_type_fully_connected), filter_id);
64     return xnn_status_invalid_parameter;
65   }
66 
67   if (bias_id >= subgraph->num_values) {
68     xnn_log_error(
69       "failed to define %s operator with bias ID #%" PRIu32 ": invalid Value ID",
70       xnn_node_type_to_string(xnn_node_type_fully_connected), bias_id);
71     return xnn_status_invalid_parameter;
72   }
73 
74   if (output_id >= subgraph->num_values) {
75     xnn_log_error(
76       "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
77       xnn_node_type_to_string(xnn_node_type_fully_connected), output_id);
78     return xnn_status_invalid_parameter;
79   }
80 
81   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
82   if (node == NULL) {
83     return xnn_status_out_of_memory;
84   }
85 
86   node->type = xnn_node_type_fully_connected;
87   node->activation.output_min = output_min;
88   node->activation.output_max = output_max;
89   node->num_inputs = 3;
90   node->inputs[0] = input_id;
91   node->inputs[1] = filter_id;
92   node->inputs[2] = bias_id;
93   node->num_outputs = 1;
94   node->outputs[0] = output_id;
95   node->flags = flags;
96 
97   return xnn_status_success;
98 }
99