Skip to content

Commit

Permalink
Merge branch 'develop' into ravil/atten-bias
Browse files Browse the repository at this point in the history
  • Loading branch information
umangyadav authored Mar 11, 2024
2 parents 2eae332 + 44df2d1 commit 6e7fed1
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 266 deletions.
7 changes: 7 additions & 0 deletions docs/reference/py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ program
:type ins_names: list[str]


.. py:function:: autocast_fp8(prog)
Auto-convert FP8 parameters and return values to Float for an MIGraphX program.

:param program prog: Program to auto-convert parameters/return values.


op
--
.. py::class:: op(name, kwargs)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ register_migraphx_ops(
undefined
unique
unknown
unpack_int4
unsqueeze
where
)
Expand Down
97 changes: 97 additions & 0 deletions src/include/migraphx/op/unpack_int4.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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_OPERATORS_UNPACK_INT4_HPP
#define MIGRAPHX_GUARD_OPERATORS_UNPACK_INT4_HPP

#include <cstdint>
#include <vector>
#include <string>
#include <algorithm>
#include <migraphx/check_shapes.hpp>
#include <migraphx/op/normalize_attribute.hpp>
#include <migraphx/shape.hpp>
#include <migraphx/value.hpp>
#include <migraphx/config.hpp>
#include <migraphx/par_for.hpp>
#include <migraphx/argument.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {
struct unpack_int4
{
int64_t axis = -1;

std::string name() const { return "unpack_int4"; }

value attributes() const
{
value normalize = value::object{};
normalize["axis"] = value::array{normalize_attribute::include_min};
return {{"normalize_axes", normalize}};
}

template <class Self, class F>
static auto reflect(Self& self, F f)
{
return pack(f(self.axis, "axis"));
}

migraphx::shape normalize_compute_shape(std::vector<migraphx::shape> inputs) const
{
check_shapes{inputs, *this}.same_dims().has(1);
auto in_shape = inputs.front();
if(in_shape.type() != migraphx::shape::uint8_type)
{
MIGRAPHX_THROW("UNPACK_INT4: Only Unsigned Int8 type is supported for unpacking");
}
auto new_lens = in_shape.lens();
new_lens[axis] *= 2;
return {migraphx::shape::uint8_type, new_lens};
}

argument compute(const shape& output_shape, std::vector<argument> args) const
{
argument result{output_shape};
auto in_shape = args.front().get_shape();
auto input = args.at(0).get<uint8_t>();
auto output = result.get<uint8_t>();
par_for(in_shape.elements(), [&](auto i) {
auto data_idx = in_shape.multi(i);
auto out_data_multi_idx = data_idx;
out_data_multi_idx[axis] *= 2;
auto input_val = input[data_idx];
// mask first 4 bits, packing is assumed to be little endian
output[out_data_multi_idx] = uint8_t(0x0F) & input_val;
out_data_multi_idx[axis] += 1;
output[out_data_multi_idx] = input_val >> 4; // NOLINT(hicpp-signed-bitwise)
});
return result;
}
};
} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
11 changes: 10 additions & 1 deletion src/py/migraphx_py.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 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 @@ -29,6 +29,7 @@
#include <migraphx/instruction_ref.hpp>
#include <migraphx/operation.hpp>
#include <migraphx/quantization.hpp>
#include <migraphx/autocast_fp8.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/ref/target.hpp>
Expand All @@ -41,6 +42,7 @@
#include <migraphx/make_op.hpp>
#include <migraphx/op/common.hpp>
#include <migraphx/float8.hpp>
#include <migraphx/pass_manager.hpp>
#ifdef HAVE_GPU
#include <migraphx/gpu/hip.hpp>
#endif
Expand Down Expand Up @@ -581,6 +583,13 @@ MIGRAPHX_PYBIND11_MODULE(migraphx, m)
py::arg("t"),
py::arg("calibration") = std::vector<migraphx::parameter_map>{},
py::arg("ins_names") = std::unordered_set<std::string>{"dot", "convolution"});
m.def(
"autocast_fp8",
[](migraphx::program& prog) {
migraphx::run_passes(*prog.get_main_module(), {migraphx::autocast_fp8_pass{}});
},
"Auto-convert FP8 parameters and return values to Float for MIGraphX Program",
py::arg("prog"));

#ifdef HAVE_GPU
m.def("allocate_gpu", &migraphx::gpu::allocate_gpu, py::arg("s"), py::arg("host") = false);
Expand Down
4 changes: 3 additions & 1 deletion src/targets/gpu/rocblas.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 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 @@ -35,6 +35,8 @@ namespace gpu {

rocblas_handle_ptr create_rocblas_handle_ptr()
{
// add a call to rocblas_initialize() to workaround a rocblas bug SWDEV-438929
rocblas_initialize();
rocblas_handle handle;
rocblas_create_handle(&handle);
return rocblas_handle_ptr{handle};
Expand Down
47 changes: 47 additions & 0 deletions test/op_shape_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,53 @@ TEST_CASE(pack_int4_odd_lengths)
throws_shape(migraphx::make_op("pack_int4", {{"axis", 0}}), input);
}

TEST_CASE(unpack_int4)
{
migraphx::shape input{migraphx::shape::uint8_type, {1, 4, 16, 8}};
migraphx::shape output{migraphx::shape::uint8_type, {1, 4, 16, 16}};
expect_shape(output, migraphx::make_op("unpack_int4"), input);
}

TEST_CASE(unpack_int4_axis1)
{
migraphx::shape input{migraphx::shape::uint8_type, {1, 2, 16, 16}};
migraphx::shape output{migraphx::shape::uint8_type, {1, 4, 16, 16}};
expect_shape(output, migraphx::make_op("unpack_int4", {{"axis", 1}}), input);
}

TEST_CASE(unpack_int4_axis2)
{
migraphx::shape input{migraphx::shape::uint8_type, {1, 2, 16, 16}};
migraphx::shape output{migraphx::shape::uint8_type, {1, 4, 16, 16}};
expect_shape(output, migraphx::make_op("unpack_int4", {{"axis", -3}}), input);
}

TEST_CASE(unpack_int4_invalid_axis)
{
migraphx::shape input{migraphx::shape::uint8_type, {1, 4, 16, 16}};
throws_shape(migraphx::make_op("unpack_int4", {{"axis", 4}}), input);
}

TEST_CASE(unpack_int4_nonstandard)
{
migraphx::shape input{migraphx::shape::uint8_type, {1, 16, 16, 4}, {1024, 16, 1, 256}};
migraphx::shape output{migraphx::shape::uint8_type, {1, 32, 16, 4}};
expect_shape(output, migraphx::make_op("unpack_int4", {{"axis", 1}}), input);
}

TEST_CASE(unpack_int4_invalid_dtype)
{
migraphx::shape input{migraphx::shape::float_type, {1, 4, 16, 16}};
throws_shape(migraphx::make_op("unpack_int4", {{"axis", 0}}), input);
}

TEST_CASE(unpack_int4_odd_lengths)
{
migraphx::shape input{migraphx::shape::uint8_type, {3, 4, 16, 16}};
migraphx::shape output{migraphx::shape::uint8_type, {6, 4, 16, 16}};
expect_shape(output, migraphx::make_op("unpack_int4", {{"axis", 0}}), input);
}

TEST_CASE(pad_shape0)
{
migraphx::shape input{migraphx::shape::float_type, {2, 3, 3, 3}};
Expand Down
1 change: 1 addition & 0 deletions test/py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ add_py_test(op test_op.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
add_py_test(shape test_shape.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
add_py_test(module_construct test_module_construct.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
add_py_test(literal test_literal.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
add_py_test(autocast_fp8 test_autocast_fp8.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
if(MIGRAPHX_ENABLE_GPU)
add_py_test(gpu_offload test_gpu_offload.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
add_py_test(gpu test_gpu.py common ${VENV} WORKING_DIRECTORY ${TEST_ONNX_DIR})
Expand Down
Loading

0 comments on commit 6e7fed1

Please sign in to comment.