Skip to content

[ET-VK][Ez] Introduce convenience constexpr for StorageTypes and GPUMemoryLayouts #2948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions backends/vulkan/runtime/VulkanBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,26 @@ api::StorageType get_storage_type(
const vkgraph::VkStorageType& vk_storage_type) {
switch (vk_storage_type) {
case vkgraph::VkStorageType::BUFFER:
return api::StorageType::BUFFER;
return api::kBuffer;
case vkgraph::VkStorageType::TEXTURE_3D:
return api::StorageType::TEXTURE_3D;
return api::kTexture3D;
case vkgraph::VkStorageType::TEXTURE_2D:
return api::StorageType::TEXTURE_2D;
return api::kTexture2D;
default:
break;
}
return api::StorageType::UNKNOWN;
VK_THROW("Invalid storage type encountered!");
}

api::GPUMemoryLayout get_memory_layout(
const vkgraph::VkMemoryLayout& vk_memory_layout) {
switch (vk_memory_layout) {
case vkgraph::VkMemoryLayout::TENSOR_WIDTH_PACKED:
return api::GPUMemoryLayout::TENSOR_WIDTH_PACKED;
return api::kWidthPacked;
case vkgraph::VkMemoryLayout::TENSOR_HEIGHT_PACKED:
return api::GPUMemoryLayout::TENSOR_HEIGHT_PACKED;
return api::kHeightPacked;
case vkgraph::VkMemoryLayout::TENSOR_CHANNELS_PACKED:
return api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED;
return api::kChannelsPacked;
default:
break;
}
Expand Down
23 changes: 2 additions & 21 deletions backends/vulkan/runtime/api/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,19 @@ ShaderInfo::ShaderInfo()
0u,
} {}

ShaderInfo::ShaderInfo(
std::string name,
const uint32_t* const spirv_bin,
const uint32_t size,
std::vector<VkDescriptorType> layout)
: src_code{
spirv_bin,
size,
},
kernel_name{std::move(name)},
kernel_layout{std::move(layout)} {}

ShaderInfo::ShaderInfo(
std::string name,
const uint32_t* const spirv_bin,
const uint32_t size,
std::vector<VkDescriptorType> layout,
const std::vector<uint32_t>& tile_size,
const StorageType bias_storage_type,
const StorageType weight_storage_type)
const utils::uvec3 tile_size)
: src_code{
spirv_bin,
size,
},
kernel_name{std::move(name)},
kernel_layout{std::move(layout)},
tile_size(tile_size),
bias_storage_type(bias_storage_type),
weight_storage_type(weight_storage_type) {
for (uint64_t i = 0; i < tile_size.size(); ++i) {
out_tile_size.data[i] = tile_size[i];
}
out_tile_size(tile_size) {
}

bool operator==(const ShaderInfo& _1, const ShaderInfo& _2) {
Expand Down
15 changes: 2 additions & 13 deletions backends/vulkan/runtime/api/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,14 @@ struct ShaderInfo final {
// Shader Metadata
utils::uvec3 out_tile_size{1u, 1u, 1u};

std::vector<uint32_t> tile_size;
StorageType bias_storage_type{StorageType::UNKNOWN};
StorageType weight_storage_type{StorageType::UNKNOWN};

explicit ShaderInfo();
explicit ShaderInfo(std::string, const char*);
explicit ShaderInfo(
std::string,
const uint32_t*,
const uint32_t,
std::vector<VkDescriptorType>);

explicit ShaderInfo(
std::string,
const uint32_t*,
const uint32_t,
std::vector<VkDescriptorType>,
const std::vector<uint32_t>& tile_size,
const StorageType bias_storage_type,
const StorageType weight_storage_type);
const utils::uvec3 tile_size);
};

bool operator==(const ShaderInfo& _1, const ShaderInfo& _2);
Expand Down
60 changes: 26 additions & 34 deletions backends/vulkan/runtime/api/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ std::vector<int64_t> calc_strides(
const api::GPUMemoryLayout memory_layout,
const api::StorageType storage_type) {
switch (storage_type) {
case api::StorageType::BUFFER:
case api::kBuffer:
switch (memory_layout) {
case api::GPUMemoryLayout::TENSOR_WIDTH_PACKED:
case api::kWidthPacked:
return calc_contiguous_strides(sizes);
break;
case api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED:
case api::kChannelsPacked:
return calc_channels_last_strides(sizes);
break;
default:
VK_THROW("Invalid memory format used to create vTensor!");
}
break;
case api::StorageType::TEXTURE_3D:
case api::StorageType::TEXTURE_2D:
case api::kTexture3D:
case api::kTexture2D:
return std::vector<int64_t>(sizes.size());
default:
VK_THROW("Invalid storage type used to create vTensor!");
Expand All @@ -99,10 +99,8 @@ std::vector<int64_t> calc_gpu_sizes(
const std::vector<int64_t>& sizes,
const api::GPUMemoryLayout memory_layout,
const api::StorageType storage_type) {
VK_CHECK_COND(storage_type != api::StorageType::UNKNOWN);

std::vector<int64_t> gpu_sizes;
if (storage_type == api::StorageType::BUFFER) {
if (storage_type == api::kBuffer) {
gpu_sizes.resize(sizes.size());
for (size_t i = 0; i < sizes.size(); i++) {
gpu_sizes.at(i) = sizes.at(i);
Expand All @@ -127,21 +125,21 @@ std::vector<int64_t> calc_gpu_sizes(

size_t ndim = gpu_sizes.size();
switch (memory_layout) {
case api::GPUMemoryLayout::TENSOR_WIDTH_PACKED:
case api::kWidthPacked:
if (ndim >= 1) {
gpu_sizes.at(ndim - 1) =
api::utils::align_up(api::utils::val_at(-1, sizes), INT64_C(4));
}
break;

case api::GPUMemoryLayout::TENSOR_HEIGHT_PACKED:
case api::kHeightPacked:
if (ndim >= 2) {
gpu_sizes.at(ndim - 2) =
api::utils::align_up(api::utils::val_at(-2, sizes), INT64_C(4));
}
break;

case api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED:
case api::kChannelsPacked:
if (ndim >= 3) {
gpu_sizes.at(ndim - 3) =
api::utils::align_up(api::utils::val_at(-3, sizes), INT64_C(4));
Expand All @@ -162,7 +160,7 @@ api::utils::uvec3 create_image_extents(
const api::GPUMemoryLayout memory_layout) {
size_t ndim = gpu_sizes.size();

if (storage_type == api::StorageType::BUFFER) {
if (storage_type == api::kBuffer) {
// image extents do not apply to buffer storage
return {0u, 0u, 0u};
} else {
Expand All @@ -177,15 +175,15 @@ api::utils::uvec3 create_image_extents(
uint32_t batch = safe_downcast<uint32_t>(val_at(-4, gpu_sizes));

switch (memory_layout) {
case api::GPUMemoryLayout::TENSOR_WIDTH_PACKED:
case api::kWidthPacked:
VK_CHECK_COND(width % 4 == 0, "Channels must be divisible by 4!");
width /= 4;
break;
case api::GPUMemoryLayout::TENSOR_HEIGHT_PACKED:
case api::kHeightPacked:
VK_CHECK_COND(height % 4 == 0, "Channels must be divisible by 4!");
height /= 4;
break;
case api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED:
case api::kChannelsPacked:
VK_CHECK_COND(channels % 4 == 0, "Channels must be divisible by 4!");
channels /= 4;
break;
Expand Down Expand Up @@ -326,41 +324,35 @@ std::shared_ptr<api::UniformParamsBuffer> vTensor::extents_ubo() {

VmaAllocationCreateInfo vTensor::get_allocation_create_info() const {
switch (storage_type()) {
case api::StorageType::BUFFER:
case api::kBuffer:
return view_->buffer_.allocation_create_info();
case api::StorageType::TEXTURE_2D:
case api::StorageType::TEXTURE_3D:
case api::kTexture2D:
case api::kTexture3D:
return view_->image_.allocation_create_info();
case api::StorageType::UNKNOWN:
break;
}
return {};
}

VkMemoryRequirements vTensor::get_memory_requirements() const {
switch (storage_type()) {
case api::StorageType::BUFFER:
case api::kBuffer:
return view_->buffer_.get_memory_requirements();
case api::StorageType::TEXTURE_2D:
case api::StorageType::TEXTURE_3D:
case api::kTexture2D:
case api::kTexture3D:
return view_->image_.get_memory_requirements();
case api::StorageType::UNKNOWN:
break;
}
return {};
}

void vTensor::bind_allocation(const api::MemoryAllocation& allocation) {
switch (storage_type()) {
case api::StorageType::BUFFER:
case api::kBuffer:
view_->buffer_.bind_allocation(allocation);
break;
case api::StorageType::TEXTURE_2D:
case api::StorageType::TEXTURE_3D:
case api::kTexture2D:
case api::kTexture3D:
view_->image_.bind_allocation(allocation);
break;
case api::StorageType::UNKNOWN:
break;
}
}

Expand Down Expand Up @@ -397,7 +389,7 @@ void vTensor::reallocate(const std::vector<int64_t>& new_sizes) {

void vTensor::virtual_resize(const std::vector<int64_t>& new_sizes) {
update_size_metadata(new_sizes);
if (storage_type() == api::StorageType::BUFFER) {
if (storage_type() == api::kBuffer) {
if (gpu_nbytes() > view_->buffer_.mem_size()) {
VK_THROW(
"Cannot virtual_resize a vTensor with sizes that require a larger "
Expand Down Expand Up @@ -446,11 +438,11 @@ api::VulkanImage allocate_image(
VkImageViewType image_view_type = VK_IMAGE_VIEW_TYPE_3D;

switch (storage_type) {
case api::StorageType::TEXTURE_3D:
case api::kTexture3D:
image_type = VK_IMAGE_TYPE_3D;
image_view_type = VK_IMAGE_VIEW_TYPE_3D;
break;
case api::StorageType::TEXTURE_2D:
case api::kTexture2D:
image_type = VK_IMAGE_TYPE_2D;
image_view_type = VK_IMAGE_VIEW_TYPE_2D;
break;
Expand Down Expand Up @@ -481,7 +473,7 @@ api::VulkanBuffer allocate_buffer(
api::Adapter* adapter_ptr = context_ptr->adapter_ptr();

switch (storage_type) {
case api::StorageType::BUFFER:
case api::kBuffer:
break;
default:
// Return an empty VulkanBuffer if Buffer storage is not used
Expand Down
10 changes: 4 additions & 6 deletions backends/vulkan/runtime/api/Tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ class vTensor final {
api::Context* context,
const std::vector<int64_t>& sizes,
const api::ScalarType dtype,
const api::StorageType storage_type = api::StorageType::TEXTURE_3D,
const api::GPUMemoryLayout memory_layout =
api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED,
const api::StorageType storage_type = api::kTexture3D,
const api::GPUMemoryLayout memory_layout = api::kChannelsPacked,
const bool allocate_memory = true);

// Default constructor for quantized vTensor
Expand All @@ -115,9 +114,8 @@ class vTensor final {
double q_scale,
int64_t q_zero_point,
const api::ScalarType dtype,
const api::StorageType storage_type = api::StorageType::TEXTURE_3D,
const api::GPUMemoryLayout memory_layout =
api::GPUMemoryLayout::TENSOR_CHANNELS_PACKED);
const api::StorageType storage_type = api::kTexture3D,
const api::GPUMemoryLayout memory_layout = api::kChannelsPacked);

// Copy Constructor and Assignment; Ideally copying would be disabled
// (see the reasoning for move assignment below) but it is required for
Expand Down
18 changes: 15 additions & 3 deletions backends/vulkan/runtime/api/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,16 @@ VK_FORALL_SCALAR_TYPES(SPECIALIZE_ScalarTypeToCType)
*
* UNKNOWN is not expected to be used.
*/
enum class StorageType {
enum class StorageType : uint8_t {
BUFFER,
TEXTURE_3D,
TEXTURE_2D,
UNKNOWN,
};

static constexpr StorageType kBuffer = StorageType::BUFFER;
static constexpr StorageType kTexture3D = StorageType::TEXTURE_3D;
static constexpr StorageType kTexture2D = StorageType::TEXTURE_2D;

/**
* The enum below is used to describe how tensor data is laid out when stored in
* GPU memory. The name of the enum describes which dimension is tightly packed;
Expand All @@ -182,11 +185,20 @@ enum class StorageType {
* strides of the tensor will be used instead to convert between logical tensor
* coordinates and linear access indices.
*/
enum class GPUMemoryLayout : uint32_t {
enum class GPUMemoryLayout : uint8_t {
TENSOR_WIDTH_PACKED = 0u,
TENSOR_HEIGHT_PACKED = 1u,
TENSOR_CHANNELS_PACKED = 2u,
};

static constexpr GPUMemoryLayout kWidthPacked =
GPUMemoryLayout::TENSOR_WIDTH_PACKED;

static constexpr GPUMemoryLayout kHeightPacked =
GPUMemoryLayout::TENSOR_HEIGHT_PACKED;

static constexpr GPUMemoryLayout kChannelsPacked =
GPUMemoryLayout::TENSOR_CHANNELS_PACKED;

} // namespace api
} // namespace vkcompute
11 changes: 1 addition & 10 deletions backends/vulkan/runtime/api/gen_vulkan_spv.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,6 @@ def findRegisterFor(lineStr: str) -> Tuple[str, List[str]]:
r"\buniform\b": "VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER",
}

storageTypeToEnum = {
"TEXTURE_2D": "api::StorageType::TEXTURE_2D",
"TEXTURE_3D": "api::StorageType::TEXTURE_3D",
"BUFFER": "api::StorageType::BUFFER",
"": "api::StorageType::UNKNOWN",
}


def determineDescriptorType(lineStr: str) -> str:
for identifier, typeNum in typeIdMapping.items():
Expand Down Expand Up @@ -632,7 +625,7 @@ def generateShaderInfoStr(shader_info: ShaderInfo, name: str, sizeBytes: int) ->
tile_size = (
f"{{{', '.join(str(x) for x in shader_info.tile_size)}}}"
if (len(shader_info.tile_size) > 0)
else "std::vector<uint32_t>()"
else "{1, 1, 1}"
)

shader_info_layouts = "{{{}}}".format(",\n ".join(shader_info.layouts))
Expand All @@ -643,8 +636,6 @@ def generateShaderInfoStr(shader_info: ShaderInfo, name: str, sizeBytes: int) ->
str(sizeBytes),
shader_info_layouts,
tile_size,
storageTypeToEnum[shader_info.weight_storage_type],
storageTypeToEnum[shader_info.bias_storage_type],
]

shader_info_str = textwrap.indent(
Expand Down
Loading