Skip to content

Commit

Permalink
[GPU] Use ov element type and float16 inside plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-paramuzov committed Oct 2, 2023
1 parent affceaa commit 78f91a4
Show file tree
Hide file tree
Showing 134 changed files with 4,134 additions and 4,548 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct generate_proposals
int64_t post_nms_count = 0;
bool normalized = false;
float nms_eta = 0.0f;
data_types roi_num_type = data_types::bin;
data_types roi_num_type = data_types::undefined;

size_t hash() const override {
size_t seed = primitive::hash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ struct primitive {
return false;

for (size_t i = 0; i < output_data_types.size(); ++i) {
if (output_data_types[i].value_or(data_types::bin) != rhs.output_data_types[i].value_or(data_types::bin))
if (output_data_types[i].value_or(data_types::undefined) != rhs.output_data_types[i].value_or(data_types::undefined))
return false;
}

Expand Down
57 changes: 0 additions & 57 deletions src/plugins/intel_gpu/include/intel_gpu/runtime/half.hpp

This file was deleted.

110 changes: 20 additions & 90 deletions src/plugins/intel_gpu/include/intel_gpu/runtime/layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include "tensor.hpp"
#include "half.hpp"

#include <cmath>
#include <cstdlib>
Expand Down Expand Up @@ -34,41 +33,14 @@ constexpr size_t uint_type_mask = 0x40;
constexpr size_t bin_type_mask = 0x20;

/// @brief Possible data types could be stored in memory.
enum class data_types : size_t {
bin = sizeof(int32_t) | bin_type_mask,
u8 = sizeof(uint8_t) | uint_type_mask,
i8 = sizeof(int8_t),
f16 = sizeof(int16_t) | float_type_mask,
f32 = sizeof(float) | float_type_mask,
i32 = sizeof(int32_t),
i64 = sizeof(int64_t)
};


/// Converts C++ type to @ref data_types .
template <typename T>
struct type_to_data_type;
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <>
struct type_to_data_type<int8_t> { static constexpr data_types value = data_types::i8; };
template <>
struct type_to_data_type<uint8_t> { static constexpr data_types value = data_types::u8; };
template <>
struct type_to_data_type<int32_t> { static constexpr data_types value = data_types::i32; };
template <>
struct type_to_data_type<int64_t> { static constexpr data_types value = data_types::i64; };
template <>
struct type_to_data_type<half_t> { static constexpr data_types value = data_types::f16; };
template <>
struct type_to_data_type<float> { static constexpr data_types value = data_types::f32; };
#endif
using data_types = ov::element::Type_t;

/// Converts @ref data_types to C++ type.
template <data_types Data_Type>
struct data_type_to_type;
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <>
struct data_type_to_type<data_types::bin> { typedef uint32_t type; };
struct data_type_to_type<data_types::u1> { typedef uint32_t type; };
template <>
struct data_type_to_type<data_types::u8> { typedef uint8_t type; };
template <>
Expand All @@ -78,29 +50,32 @@ struct data_type_to_type<data_types::i32> { typedef int32_t type; };
template <>
struct data_type_to_type<data_types::i64> { typedef int64_t type; };
template <>
struct data_type_to_type<data_types::f16> { typedef half_t type; };
struct data_type_to_type<data_types::f16> { typedef ov::float16 type; };
template <>
struct data_type_to_type<data_types::f32> { typedef float type; };
#endif

/// Helper class to identify key properties for data_types.
struct data_type_traits {
static size_t size_of(data_types data_type) {
return (static_cast<uint32_t>(data_type) & ~(float_type_mask | uint_type_mask | bin_type_mask));
auto et = ov::element::Type(data_type);
OPENVINO_ASSERT(et.bitwidth() >= 8, "[GPU] Unexpected data_type_traits::size_of call for type with bitwidth < 8 (", et.get_type_name(), ")");
return et.size();
}

static bool is_floating_point(data_types data_type) {
return (static_cast<uint32_t>(data_type) & float_type_mask) != 0;
return ov::element::Type(data_type).is_real();
}

static bool is_i8_u8(data_types data_type) {
return data_type == data_types::i8 || data_type == data_types::u8;
auto et = ov::element::Type(data_type);
return et.is_quantized() && et.bitwidth() == 8;
}

static size_t align_of(data_types data_type) {
switch (data_type) {
case data_types::bin:
return alignof(data_type_to_type<data_types::bin>::type);
case data_types::u1:
return alignof(data_type_to_type<data_types::u1>::type);
case data_types::i8:
return alignof(data_type_to_type<data_types::i8>::type);
case data_types::u8:
Expand All @@ -119,32 +94,14 @@ struct data_type_traits {
}

static std::string name(data_types data_type) {
switch (data_type) {
case data_types::bin:
return "bin";
case data_types::i8:
return "i8";
case data_types::u8:
return "u8";
case data_types::i32:
return "i32";
case data_types::i64:
return "i64";
case data_types::f16:
return "f16";
case data_types::f32:
return "f32";
default:
assert(0);
return "unknown (" + std::to_string(typename std::underlying_type<data_types>::type(data_type)) + ")";
}
return ov::element::Type(data_type).get_type_name();
}

static data_types max_type(data_types dt1, data_types dt2) {
if (dt1 == data_types::bin)
if (dt1 == data_types::u1)
return dt2;

if (dt2 == data_types::bin)
if (dt2 == data_types::u1)
return dt1;

if (size_of(dt1) < size_of(dt2))
Expand All @@ -160,7 +117,7 @@ struct data_type_traits {
}

static bool is_quantized(data_types dt) {
return dt == data_types::u8 || dt == data_types::i8;
return is_i8_u8(dt);
}

template <typename T>
Expand Down Expand Up @@ -206,13 +163,7 @@ struct data_type_traits {
};

inline ::std::ostream& operator<<(::std::ostream& os, const data_types& dt) {
return os << data_type_traits::name(dt);
}

/// Helper function to check if C++ type matches @p data_type.
template <typename T>
bool data_type_match(data_types data_type) {
return data_type == type_to_data_type<T>::value;
return os << ov::element::Type(dt);
}

inline data_types element_type_to_data_type(ov::element::Type t) {
Expand All @@ -237,33 +188,12 @@ inline data_types element_type_to_data_type(ov::element::Type t) {
case ov::element::Type_t::boolean:
return cldnn::data_types::u8;
case ov::element::Type_t::u1:
return cldnn::data_types::bin;
return cldnn::data_types::u1;
default:
throw std::runtime_error("Can't convert " + t.get_type_name() + " element type");
}
}

inline ov::element::Type data_type_to_element_type(data_types t) {
switch (t) {
case cldnn::data_types::f32:
return ov::element::Type_t::f32;
case cldnn::data_types::f16:
return ov::element::Type_t::f16;
case cldnn::data_types::u8:
return ov::element::Type_t::u8;
case cldnn::data_types::i8:
return ov::element::Type_t::i8;
case cldnn::data_types::i32:
return ov::element::Type_t::i32;
case cldnn::data_types::i64:
return ov::element::Type_t::i64;
case cldnn::data_types::bin:
return ov::element::Type_t::u1;
default:
throw std::runtime_error("Can't convert " + data_type_traits::name(t) + " precision");
}
}

/// Helper function to get both data_types and format::type in a single, unique value. Useable in 'case' statement.
constexpr auto fuse(data_types dt, cldnn::format::type fmt) -> decltype(static_cast<std::underlying_type<data_types>::type>(dt) |
static_cast<std::underlying_type<format::type>::type>(fmt)) {
Expand Down Expand Up @@ -425,7 +355,7 @@ struct layout {
layout(const layout& other) = default;

layout()
: data_type(cldnn::data_types::bin)
: data_type(cldnn::data_types::undefined)
, format(cldnn::format::any)
, data_padding(padding())
, size(ov::PartialShape()) { }
Expand Down Expand Up @@ -489,7 +419,7 @@ struct layout {
layout with_padding(padding const& padd) const;

/// Data type stored in @ref memory (see. @ref data_types)
data_types data_type;
ov::element::Type_t data_type;

/// Format stored in @ref memory (see. @ref format)
cldnn::format format;
Expand All @@ -498,7 +428,7 @@ struct layout {
padding data_padding;

/// Number of bytes needed to store this layout
size_t bytes_count() const { return data_type_traits::size_of(data_type) * get_linear_size(); }
size_t bytes_count() const { return (ov::element::Type(data_type).bitwidth() * get_linear_size() + 7) >> 3; }

size_t get_rank() const;

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/intel_gpu/include/intel_gpu/runtime/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct memory {
return true;
}

if (_bytes_count == (l.data_type == data_types::bin ? ceil_div(l.count(), 32) : l.count()) * data_type_traits::size_of(l.data_type)) {
if (_bytes_count == l.bytes_count()) {
return false;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ template<typename T>
inline std::vector<T> read_vector(cldnn::memory::ptr mem, const cldnn::stream& stream) {
cldnn::data_types mem_dtype = mem->get_layout().data_type;
if (mem_dtype == data_types::f16 || mem_dtype == data_types::f32) {
if (!std::is_floating_point<T>::value && !std::is_same<T, half_t>::value) {
if (!std::is_floating_point<T>::value && !std::is_same<T, ov::float16>::value) {
OPENVINO_ASSERT(false, "[GPU] read_vector: attempt to convert floating point memory to non-floating point memory");
}
}
Expand All @@ -211,7 +211,7 @@ inline std::vector<T> read_vector(cldnn::memory::ptr mem, const cldnn::stream& s
case data_types::f16: {
auto p_mem = reinterpret_cast<uint16_t*>(mem->buffer_ptr());
for (size_t i = 0; i < mem->count(); ++i) {
out_vecs.push_back(static_cast<T>(half_to_float(p_mem[i])));
out_vecs.push_back(static_cast<T>(ov::float16::from_bits(p_mem[i])));
}
break;
}
Expand All @@ -237,7 +237,7 @@ inline std::vector<T> read_vector(cldnn::memory::ptr mem, const cldnn::stream& s
break;
}
case data_types::f16: {
mem_lock<half_t, mem_lock_type::read> lock{mem, stream};
mem_lock<ov::float16, mem_lock_type::read> lock{mem, stream};
out_vecs = std::move(std::vector<T>(lock.begin(), lock.end()));
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
namespace cldnn {

inline ov::Tensor make_tensor(const layout& l, void* memory_pointer) {
ov::element::Type et = data_type_to_element_type(l.data_type);

return ov::Tensor(et, l.get_shape(), memory_pointer);
return ov::Tensor(l.data_type, l.get_shape(), memory_pointer);
}

struct TensorsContainer final {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/intel_gpu/src/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ov_install_static_lib(${TARGET_NAME} ${OV_CPACK_COMP_CORE})

if(ENABLE_SSE42)
ie_sse42_optimization_flags(sse4_2_flags)
set_source_files_properties(impls/cpu/detection_output.cpp half.cpp PROPERTIES
set_source_files_properties(impls/cpu/detection_output.cpp PROPERTIES
COMPILE_FLAGS "${sse4_2_flags}"
COMPILE_DEFINITIONS "HAVE_SSE")
endif()
4 changes: 2 additions & 2 deletions src/plugins/intel_gpu/src/graph/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool condition_inst::get_pred_from_memory(memory::ptr mem, stream& stream) {
case cldnn::data_types::f32:
return convert_data<float>(mem, stream);
case cldnn::data_types::f16:
return convert_data<half_t>(mem, stream);
return convert_data<ov::float16>(mem, stream);
case cldnn::data_types::i64:
return convert_data<int64_t>(mem, stream);
case cldnn::data_types::i32:
Expand All @@ -94,7 +94,7 @@ bool condition_inst::get_pred_from_memory(memory::ptr mem, stream& stream) {
return convert_data<int8_t>(mem, stream);
case cldnn::data_types::u8:
return convert_data<uint8_t>(mem, stream);
case cldnn::data_types::bin:
case cldnn::data_types::u1:
default:
return convert_data<uint32_t>(mem, stream);
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/intel_gpu/src/graph/error_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ void error_on_mismatching_data_types(const std::string& file,
(data_format_1 == data_types::u8 && data_format_2 == data_types::i8))) {
std::stringstream error_msg;
error_msg << "Data formats are incompatible." << std::endl;
error_msg << data_format_1_id << " format is: " << data_type_traits::name(data_format_1) << ", "
<< data_format_2_id << " is: " << data_type_traits::name(data_format_2) << std::endl;
error_msg << data_format_1_id << " format is: " << ov::element::Type(data_format_1) << ", "
<< data_format_2_id << " is: " << ov::element::Type(data_format_2) << std::endl;
error_msg << "Data formats should be the same!" << std::endl;
err_details::cldnn_print_error_message(file, line, instance_id, error_msg, additional_message);
}
Expand Down Expand Up @@ -217,8 +217,8 @@ void error_on_mismatch_layout(const std::string& file,
}
if (layout_1.data_type != layout_2.data_type) {
error_msg << layout_1_id << " data type mismatch: " << layout_2_id << " data type." << std::endl;
error_msg << layout_1_id << " data type: " << data_type_traits::name(layout_1.data_type) << ", "
<< layout_2_id << " data type: " << data_type_traits::name(layout_2.data_type) << std::endl;
error_msg << layout_1_id << " data type: " << ov::element::Type(layout_1.data_type) << ", "
<< layout_2_id << " data type: " << ov::element::Type(layout_2.data_type) << std::endl;
}
if (layout_1.format != layout_2.format) {
error_msg << layout_1_id << " format mismatch: " << layout_2_id << " format." << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ void add_required_reorders::run(program& p) {
OPENVINO_ASSERT(correct_layout_selected,
"[GPU] No layout format available for ", usr->id(), ", impl_type: ", usr->get_preferred_impl_type(),
" (format: ", original_layout.format.to_string(),
", data_type: ", data_type_traits::name(original_layout.data_type), ") ",
", data_type: ", ov::element::Type(original_layout.data_type), ") ",
"compatible with ", node.first->id(),
" (format: ", node.first->get_output_layout().format.to_string(),
", data_type: ", data_type_traits::name(node.first->get_output_layout().data_type), ")");
", data_type: ", ov::element::Type(node.first->get_output_layout().data_type), ")");
}
}

Expand Down
Loading

0 comments on commit 78f91a4

Please sign in to comment.