Skip to content

Commit 7c759cf

Browse files
Xnnpack msvc (pytorch#15224)
### Summary Some fixes to make it compile on msvc ### Test plan will be adding full ci for msvc builds after all the changes are landed
1 parent a47b5a6 commit 7c759cf

File tree

4 files changed

+77
-67
lines changed

4 files changed

+77
-67
lines changed

backends/xnnpack/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ if(EXECUTORCH_XNNPACK_ENABLE_KLEIDI)
3535
add_definitions(-DENABLE_XNNPACK_KLEIDI)
3636
endif()
3737

38-
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
38+
set(_common_compile_options
39+
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
40+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations -fPIC>
41+
)
3942

4043
set(_xnnpack_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
4144
# Paths to headers generated from the .fbs files.

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,9 @@ Error defineGenericUnaryNode(
15391539
MAYBE_UNUSED(graph); \
15401540
auto graph_node = node->xnode_union_as_XNN##name(); \
15411541
std::pair<float, float> min_max = getOutputMinMax(node); \
1542-
union xnn_unary_params params = { \
1543-
.clamp = {.min = min_max.first, .max = min_max.second}}; \
1542+
union xnn_unary_params params; \
1543+
params.clamp.min = min_max.first; \
1544+
params.clamp.max = min_max.second; \
15441545
return defineGenericUnaryNode( \
15451546
subgraph_ptr, \
15461547
remapped_ids, \
@@ -1554,48 +1555,49 @@ Error defineGenericUnaryNode(
15541555
}
15551556

15561557
// Macro for unary operations with leaky_relu parameters
1557-
#define _DEFINE_UNARY_NODE_WITH_LEAKY_RELU(name) \
1558-
Error define##name##Node( \
1559-
xnn_subgraph_t subgraph_ptr, \
1560-
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1561-
const NodePtr node, \
1562-
const fb_xnnpack::XNNGraph* graph) noexcept { \
1563-
MAYBE_UNUSED(graph); \
1564-
auto graph_node = node->xnode_union_as_XNNLeakyReLU(); \
1565-
union xnn_unary_params params = { \
1566-
.leaky_relu = {.negative_slope = graph_node->negative_slope()}}; \
1567-
return defineGenericUnaryNode( \
1568-
subgraph_ptr, \
1569-
remapped_ids, \
1570-
graph_node->input_id(), \
1571-
graph_node->output_id(), \
1572-
graph_node->flags(), \
1573-
xnn_unary_leaky_relu, \
1574-
&params, \
1575-
node->xnode_union_type(), \
1576-
node->debug_handle()); \
1558+
#define _DEFINE_UNARY_NODE_WITH_LEAKY_RELU(name) \
1559+
Error define##name##Node( \
1560+
xnn_subgraph_t subgraph_ptr, \
1561+
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1562+
const NodePtr node, \
1563+
const fb_xnnpack::XNNGraph* graph) noexcept { \
1564+
MAYBE_UNUSED(graph); \
1565+
auto graph_node = node->xnode_union_as_XNNLeakyReLU(); \
1566+
union xnn_unary_params params; \
1567+
params.leaky_relu.negative_slope = graph_node->negative_slope(); \
1568+
return defineGenericUnaryNode( \
1569+
subgraph_ptr, \
1570+
remapped_ids, \
1571+
graph_node->input_id(), \
1572+
graph_node->output_id(), \
1573+
graph_node->flags(), \
1574+
xnn_unary_leaky_relu, \
1575+
&params, \
1576+
node->xnode_union_type(), \
1577+
node->debug_handle()); \
15771578
}
15781579

15791580
// Macro for unary operations with elu parameters
1580-
#define _DEFINE_UNARY_NODE_WITH_ELU(name) \
1581-
Error define##name##Node( \
1582-
xnn_subgraph_t subgraph_ptr, \
1583-
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1584-
const NodePtr node, \
1585-
const fb_xnnpack::XNNGraph* graph) noexcept { \
1586-
MAYBE_UNUSED(graph); \
1587-
auto graph_node = node->xnode_union_as_XNNELU(); \
1588-
union xnn_unary_params params = {.elu = {.alpha = graph_node->alpha()}}; \
1589-
return defineGenericUnaryNode( \
1590-
subgraph_ptr, \
1591-
remapped_ids, \
1592-
graph_node->input_id(), \
1593-
graph_node->output_id(), \
1594-
graph_node->flags(), \
1595-
xnn_unary_elu, \
1596-
&params, \
1597-
node->xnode_union_type(), \
1598-
node->debug_handle()); \
1581+
#define _DEFINE_UNARY_NODE_WITH_ELU(name) \
1582+
Error define##name##Node( \
1583+
xnn_subgraph_t subgraph_ptr, \
1584+
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1585+
const NodePtr node, \
1586+
const fb_xnnpack::XNNGraph* graph) noexcept { \
1587+
MAYBE_UNUSED(graph); \
1588+
auto graph_node = node->xnode_union_as_XNNELU(); \
1589+
union xnn_unary_params params; \
1590+
params.elu.alpha = graph_node->alpha(); \
1591+
return defineGenericUnaryNode( \
1592+
subgraph_ptr, \
1593+
remapped_ids, \
1594+
graph_node->input_id(), \
1595+
graph_node->output_id(), \
1596+
graph_node->flags(), \
1597+
xnn_unary_elu, \
1598+
&params, \
1599+
node->xnode_union_type(), \
1600+
node->debug_handle()); \
15991601
}
16001602

16011603
// Generic helper function for binary operations
@@ -1628,25 +1630,26 @@ Error defineGenericBinaryNode(
16281630
}
16291631

16301632
// Macro for binary operations with min/max parameters
1631-
#define _DEFINE_BINARY_NODE_WITH_MINMAX(name, op_type) \
1632-
Error define##name##Node( \
1633-
xnn_subgraph_t subgraph_ptr, \
1634-
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1635-
const NodePtr node, \
1636-
const fb_xnnpack::XNNGraph* graph) noexcept { \
1637-
MAYBE_UNUSED(graph); \
1638-
auto graph_node = node->xnode_union_as_XNN##name(); \
1639-
std::pair<float, float> min_max = getOutputMinMax(node); \
1640-
struct xnn_binary_params params = { \
1641-
.output_min = min_max.first, .output_max = min_max.second}; \
1642-
return defineGenericBinaryNode( \
1643-
subgraph_ptr, \
1644-
remapped_ids, \
1645-
graph_node, \
1646-
op_type, \
1647-
&params, \
1648-
node->xnode_union_type(), \
1649-
node->debug_handle()); \
1633+
#define _DEFINE_BINARY_NODE_WITH_MINMAX(name, op_type) \
1634+
Error define##name##Node( \
1635+
xnn_subgraph_t subgraph_ptr, \
1636+
const std::unordered_map<uint32_t, uint32_t>& remapped_ids, \
1637+
const NodePtr node, \
1638+
const fb_xnnpack::XNNGraph* graph) noexcept { \
1639+
MAYBE_UNUSED(graph); \
1640+
auto graph_node = node->xnode_union_as_XNN##name(); \
1641+
std::pair<float, float> min_max = getOutputMinMax(node); \
1642+
struct xnn_binary_params params; \
1643+
params.output_min = min_max.first; \
1644+
params.output_max = min_max.second; \
1645+
return defineGenericBinaryNode( \
1646+
subgraph_ptr, \
1647+
remapped_ids, \
1648+
graph_node, \
1649+
op_type, \
1650+
&params, \
1651+
node->xnode_union_type(), \
1652+
node->debug_handle()); \
16501653
}
16511654

16521655
// Macro for binary operations without parameters

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ class XnnpackBackend final
8282

8383
auto program_id =
8484
reinterpret_cast<uintptr_t>(context.get_runtime_allocator());
85-
auto workspace = ET_UNWRAP(get_or_create_workspace(program_id));
85+
auto workspace_result = get_or_create_workspace(program_id);
86+
if (!workspace_result.ok()) {
87+
return workspace_result.error();
88+
}
89+
auto workspace = workspace_result.get();
8690

8791
#ifdef ENABLE_XNNPACK_WEIGHTS_CACHE
8892
const std::lock_guard<std::mutex> lock_weight_cache(weights_cache_mutex_);

backends/xnnpack/runtime/XNNWeightsCache.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ size_t XNNWeightsCache::look_up_or_insert(
228228
weight_bias_name.append(bias_entry->second);
229229
}
230230
}
231-
PackedDataMeta packed_data_metadata = {
232-
.offset = next_offset,
233-
.ref_count =
234-
0, // ref_count is only incremented after finalizing for runtime
235-
.in_current_runtime = true};
231+
PackedDataMeta packed_data_metadata;
232+
packed_data_metadata.offset = next_offset;
233+
packed_data_metadata.ref_count =
234+
0; // ref_count is only incremented after finalizing for runtime
235+
packed_data_metadata.in_current_runtime = true;
236236
context->name_to_packed_data_metadata_[weight_bias_name] =
237237
packed_data_metadata;
238238
} else {

0 commit comments

Comments
 (0)