Skip to content

Commit

Permalink
Split tf tests (#3112)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddieliao authored May 28, 2024
1 parent d7520dd commit 263509b
Show file tree
Hide file tree
Showing 122 changed files with 2,650 additions and 1,095 deletions.
8 changes: 4 additions & 4 deletions test/api/test_tf_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

TEST_CASE(load_tf)
{
auto p = migraphx::parse_tf("add_test.pb");
auto p = migraphx::parse_tf("models/add_test.pb");
auto shapes = p.get_output_shapes();
CHECK(shapes.size() == 1);
}
Expand All @@ -38,7 +38,7 @@ TEST_CASE(load_tf_default_dim)
size_t batch = 2;
tf_options.set_default_dim_value(batch);
tf_options.set_nhwc();
auto p = migraphx::parse_tf("conv_batch_test.pb", tf_options);
auto p = migraphx::parse_tf("models/conv_batch_test.pb", tf_options);
auto shapes = p.get_output_shapes();
CHECK(shapes.size() == 1);
CHECK(shapes.front().lengths().front() == batch);
Expand All @@ -50,7 +50,7 @@ TEST_CASE(load_tf_param_shape)
std::vector<size_t> new_shape{1, 3};
tf_options.set_input_parameter_shape("0", new_shape);
tf_options.set_input_parameter_shape("1", new_shape);
auto p = migraphx::parse_tf("add_test.pb", tf_options);
auto p = migraphx::parse_tf("models/add_test.pb", tf_options);
auto shapes = p.get_output_shapes();
CHECK(shapes.size() == 1);
CHECK(shapes.front().lengths() == new_shape);
Expand All @@ -60,7 +60,7 @@ TEST_CASE(load_tf_multi_outputs)
{
migraphx::tf_options tf_options;
tf_options.set_output_names({"relu", "tanh"});
auto p = migraphx::parse_tf("multi_output_test.pb", tf_options);
auto p = migraphx::parse_tf("models/multi_output_test.pb", tf_options);
auto shapes = p.get_output_shapes();
CHECK(shapes.size() == 2);
}
Expand Down
18 changes: 12 additions & 6 deletions test/tf/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
# THE SOFTWARE.
#####################################################################################

function(add_tf_test TEST_NAME)
rocm_add_test_executable(${TEST_NAME} ${ARGN})
rocm_clang_tidy_check(${TEST_NAME})
target_link_libraries(${TEST_NAME} migraphx_tf pb_files)
target_include_directories(${TEST_NAME} PUBLIC ../include include)
endfunction()

include(Embed)
file(GLOB_RECURSE PB_FILES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.pb)
add_embed_library(pb_files ${PB_FILES} RELATIVE ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB_RECURSE PB_FILES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/models/*.pb)
add_embed_library(pb_files ${PB_FILES} RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/models)

file(GLOB TF_TESTS CONFIGRE_DEPENDS tests/*.cpp)

rocm_add_test_executable(test_tf tf_test.cpp)
rocm_clang_tidy_check(test_tf)
target_link_libraries(test_tf migraphx_tf pb_files)
target_include_directories(test_tf PUBLIC ../include)
add_tf_test(test_tf ${TF_TESTS})
4 changes: 2 additions & 2 deletions test/tf/gen_tf_pb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#####################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
# Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,7 +32,7 @@ def run_test():
g1 = tf.Graph()
op_test(g1)
tf.io.write_graph(g1,
'.',
'./models',
'{}.pb'.format(op_test.__name__),
as_text=False)

Expand Down
51 changes: 51 additions & 0 deletions test/tf/include/tf_conv_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#ifndef MIGRAPHX_GUARD_TEST_TF_TF_CONV_UTILS_HPP
#define MIGRAPHX_GUARD_TEST_TF_TF_CONV_UTILS_HPP

inline migraphx::program create_conv()
{
migraphx::program p;

auto* mm = p.get_main_module();

auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
std::vector<float> weight_data(3 * 3 * 3 * 32);
std::fill(weight_data.begin(), weight_data.end(), 1.0f);
auto l1 =
mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 32}}, weight_data);

migraphx::op::convolution op;
op.padding = {1, 1, 1, 1};
op.stride = {1, 1};
op.dilation = {1, 1};
auto l2 =
mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {3, 2, 0, 1}}}), l1);
mm->add_instruction(op, l0, l2);
return p;
}

#endif
96 changes: 96 additions & 0 deletions test/tf/include/tf_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MIGRAPHX_GUARD_TEST_TF_TF_TEST_HPP
#define MIGRAPHX_GUARD_TEST_TF_TF_TEST_HPP

#include <iostream>
#include <vector>
#include <unordered_map>
#include <pb_files.hpp>
#include <migraphx/common.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/tf.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/op/convolution.hpp>
#include <migraphx/op/reduce_mean.hpp>
#include <migraphx/op/pooling.hpp>

#include <migraphx/serialize.hpp>

#include "test.hpp"

inline migraphx::program read_pb_file(const std::string& name, const migraphx::tf_options& options)
{
static auto pb_files{::pb_files()};
if(pb_files.find(name) == pb_files.end())
{
std::cerr << "Can not find TensorFlow Protobuf file by name: " << name
<< " , aborting the program\n"
<< std::endl;
std::abort();
}
return migraphx::parse_tf_buffer(std::string{pb_files.at(name)}, options);
}

inline migraphx::program
parse_tf(const std::string& name,
bool is_nhwc,
const std::unordered_map<std::string, std::vector<std::size_t>>& dim_params = {},
const std::vector<std::string>& output_node_names = {})
{

return read_pb_file(name, migraphx::tf_options{is_nhwc, 1, dim_params, output_node_names});
}

inline migraphx::program optimize_tf(const std::string& name, bool is_nhwc)
{
auto prog = read_pb_file(name, migraphx::tf_options{is_nhwc, 1});
auto* mm = prog.get_main_module();
if(is_nhwc)
migraphx::run_passes(*mm,
{migraphx::simplify_reshapes{},
migraphx::dead_code_elimination{},
migraphx::eliminate_identity{}});

// remove the last return instruction

if(mm->size() > 0)
{
auto last_ins = std::prev(mm->end());
if(last_ins->name() == "@return")
{
mm->remove_instruction(last_ins);
}
}
return prog;
}

#endif
File renamed without changes.
6 changes: 3 additions & 3 deletions test/tf/add_test.pb → test/tf/models/add_test.pb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
1 Placeholder*
dtype0*
shape:

add1Add01*
T0"�

add1AddV201*
T0"�
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions test/tf/tests/add_bcast_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#include <tf_test.hpp>

TEST_CASE(add_bcast_test)
{

migraphx::program p;

auto* mm = p.get_main_module();
migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
auto l0 = mm->add_parameter("0", s0);
auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 1}});
auto l2 =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s0.lens()}}), l1);
mm->add_instruction(migraphx::make_op("add"), l0, l2);
auto prog = optimize_tf("add_bcast_test.pb", false);

EXPECT(p == prog);
}
39 changes: 39 additions & 0 deletions test/tf/tests/add_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#include <tf_test.hpp>

TEST_CASE(add_test)
{
migraphx::program p;

auto* mm = p.get_main_module();
auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
mm->add_instruction(migraphx::make_op("add"), l0, l1);
auto prog = optimize_tf("add_test.pb", false);

EXPECT(p == prog);
}
38 changes: 38 additions & 0 deletions test/tf/tests/addv2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#include <tf_test.hpp>

TEST_CASE(addv2_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
mm->add_instruction(migraphx::make_op("add"), l0, l1);
auto prog = optimize_tf("addv2_test.pb", false);

EXPECT(p == prog);
}
Loading

0 comments on commit 263509b

Please sign in to comment.