Skip to content
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

Add image sampler. #45037

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 69 additions & 10 deletions aten/src/ATen/native/vulkan/api/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,23 @@ void release_buffer(const Resource::Buffer& buffer) {
// Safe to pass null as buffer or allocation.
vmaDestroyBuffer(
buffer.memory.allocator,
buffer.handle,
buffer.object.handle,
buffer.memory.allocation);
}

void release_image(const Resource::Image& image) {
if (VK_NULL_HANDLE != image.view) {
// Sampler lifetime managed through the sampler cache.

if (VK_NULL_HANDLE != image.object.view) {
VmaAllocatorInfo allocator_info{};
vmaGetAllocatorInfo(image.memory.allocator, &allocator_info);
vkDestroyImageView(allocator_info.device, image.view, nullptr);
vkDestroyImageView(allocator_info.device, image.object.view, nullptr);
}

// Safe to pass null as image or allocation.
vmaDestroyImage(
image.memory.allocator,
image.handle,
image.object.handle,
image.memory.allocation);
}

Expand Down Expand Up @@ -127,16 +129,65 @@ void Resource::Memory::Scope::operator()(const void* const data) const {
}
}

Resource::Image::Sampler::Factory::Factory(const GPU& gpu)
: device_(gpu.device) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_,
"Invalid Vulkan device!");
}

typename Resource::Image::Sampler::Factory::Handle
Resource::Image::Sampler::Factory::operator()(
const Descriptor& descriptor) const {
const VkSamplerCreateInfo sampler_create_info{
VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
nullptr,
0u,
descriptor.filter,
descriptor.filter,
descriptor.mipmap_mode,
descriptor.address_mode,
descriptor.address_mode,
descriptor.address_mode,
0.0f,
VK_FALSE,
0.0f,
VK_FALSE,
VK_COMPARE_OP_NEVER,
0.0f,
0.0f,
descriptor.border,
VK_FALSE,
};

VkSampler sampler{};
VK_CHECK(vkCreateSampler(
device_,
&sampler_create_info,
nullptr,
&sampler));

TORCH_CHECK(
sampler,
"Invalid Vulkan image sampler!");

return Handle{
sampler,
Deleter(device_),
};
}

Resource::Pool::Pool(const GPU& gpu)
: device_(gpu.device),
allocator_(
create_allocator(
gpu.adapter->runtime->instance(),
gpu.adapter->handle,
device_),
vmaDestroyAllocator) {
buffers_.reserve(Configuration::kReserve);
images_.reserve(Configuration::kReserve);
vmaDestroyAllocator),
sampler_(gpu) {
buffers_.reserve(Configuration::kReserve);
images_.reserve(Configuration::kReserve);
}

Resource::Buffer Resource::Pool::allocate(
Expand Down Expand Up @@ -172,7 +223,11 @@ Resource::Buffer Resource::Pool::allocate(

buffers_.emplace_back(
Buffer{
buffer,
Buffer::Object{
buffer,
0u,
descriptor.size,
},
Memory{
allocator_.get(),
allocation,
Expand Down Expand Up @@ -257,8 +312,12 @@ Resource::Image Resource::Pool::allocate(

images_.emplace_back(
Image{
image,
view,
Image::Object{
image,
VK_IMAGE_LAYOUT_UNDEFINED,
view,
sampler_.cache.retrieve(descriptor.sampler),
},
Memory{
allocator_.get(),
allocation,
Expand Down
127 changes: 111 additions & 16 deletions aten/src/ATen/native/vulkan/api/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

#include <ATen/native/vulkan/api/Common.h>
#include <ATen/native/vulkan/api/Allocator.h>
#include <ATen/native/vulkan/api/Cache.h>
#include <c10/util/hash.h>

namespace at {
namespace native {
namespace vulkan {
namespace api {

struct Resource final {
/*
Memory
*/
//
// Memory
//

struct Memory final {
VmaAllocator allocator;
Expand Down Expand Up @@ -46,9 +48,9 @@ struct Resource final {
Data<Pointer> map() && = delete;
};

/*
Buffer
*/
//
// Buffer
//

struct Buffer final {
/*
Expand All @@ -64,17 +66,74 @@ struct Resource final {
} usage;
};

VkBuffer handle;
struct Object final {
VkBuffer handle;
VkDeviceSize offset;
VkDeviceSize range;

operator bool() const;
} object;

Memory memory;

operator bool() const;
};

/*
Image
*/
//
// Image
//

struct Image final {
//
// Sampler
//

struct Sampler final {
/*
Descriptor
*/

struct Descriptor final {
VkFilter filter;
VkSamplerMipmapMode mipmap_mode;
VkSamplerAddressMode address_mode;
VkBorderColor border;
};

/*
Factory
*/

class Factory final {
public:
explicit Factory(const GPU& gpu);

typedef Sampler::Descriptor Descriptor;
typedef VK_DELETER(Sampler) Deleter;
typedef Handle<VkSampler, Deleter> Handle;

struct Hasher {
size_t operator()(const Descriptor& descriptor) const;
};

Handle operator()(const Descriptor& descriptor) const;

private:
VkDevice device_;
};

/*
Cache
*/

typedef api::Cache<Factory> Cache;
Cache cache;

explicit Sampler(const GPU& gpu)
: cache(Factory(gpu)) {
}
};

/*
Descriptor
*/
Expand All @@ -93,18 +152,27 @@ struct Resource final {
VkImageViewType type;
VkFormat format;
} view;

Sampler::Descriptor sampler;
};

VkImage handle;
VkImageView view;
struct Object final {
VkImage handle;
VkImageLayout layout;
VkImageView view;
VkSampler sampler;

operator bool() const;
} object;

Memory memory;

operator bool() const;
};

/*
Pool
*/
//
// Pool
//

class Pool final {
public:
Expand All @@ -123,6 +191,7 @@ struct Resource final {
Handle<VmaAllocator, void(*)(VmaAllocator)> allocator_;
std::vector<Handle<Buffer, void(*)(const Buffer&)>> buffers_;
std::vector<Handle<Image, void(*)(const Image&)>> images_;
Image::Sampler sampler_;
} pool;

explicit Resource(const GPU& gpu)
Expand Down Expand Up @@ -170,12 +239,38 @@ inline Resource::Memory::Data<Pointer> Resource::Memory::map() & {
};
}

inline Resource::Buffer::Object::operator bool() const {
return VK_NULL_HANDLE != handle;
}

inline Resource::Buffer::operator bool() const {
return object;
}

inline bool operator==(
const Resource::Image::Sampler::Descriptor& _1,
const Resource::Image::Sampler::Descriptor& _2) {
return (_1.filter == _2.filter) &&
(_1.mipmap_mode == _2.mipmap_mode) &&
(_1.address_mode == _2.address_mode) &&
(_1.border == _2.border);
}

inline size_t Resource::Image::Sampler::Factory::Hasher::operator()(
const Descriptor& descriptor) const {
return c10::get_hash(
descriptor.filter,
descriptor.mipmap_mode,
descriptor.address_mode,
descriptor.border);
}

inline Resource::Image::Object::operator bool() const {
return VK_NULL_HANDLE != handle;
}

inline Resource::Image::operator bool() const {
return VK_NULL_HANDLE != handle;
return object;
}

} // namespace api
Expand Down