|
| 1 | +#include <ATen/native/vulkan/ops/Common.h> |
| 2 | +#include <torch/library.h> |
| 3 | + |
| 4 | +namespace at { |
| 5 | +namespace native { |
| 6 | +namespace vulkan { |
| 7 | +namespace ops { |
| 8 | +namespace { |
| 9 | + |
| 10 | +Tensor mul_scalar( |
| 11 | + const Tensor& self_arg, |
| 12 | + const Scalar other) { |
| 13 | + api::Context* const context = api::context(); |
| 14 | + |
| 15 | + const Tensor self = self_arg.is_vulkan() ? self_arg : self_arg.vulkan(); |
| 16 | + const vTensor& v_self = convert(self); |
| 17 | + |
| 18 | + vTensor v_output{ |
| 19 | + context, |
| 20 | + self.sizes(), |
| 21 | + self.options(), |
| 22 | + }; |
| 23 | + |
| 24 | + api::Command::Buffer command_buffer = context->command().pool.allocate(); |
| 25 | + command_buffer.begin(); |
| 26 | + { |
| 27 | + if (v_output.has_image() && v_self.has_image()) { |
| 28 | + const struct { |
| 29 | + uint32_t width, height, channels; |
| 30 | + float other; |
| 31 | + } block { |
| 32 | + v_output.extents().width, |
| 33 | + v_output.extents().height, |
| 34 | + v_output.extents().depth, |
| 35 | + other.to<float>(), |
| 36 | + }; |
| 37 | + |
| 38 | + context->dispatch( |
| 39 | + command_buffer, |
| 40 | + { |
| 41 | + VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, |
| 42 | + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 43 | + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 44 | + }, |
| 45 | + VK_KERNEL(mul_scalar), |
| 46 | + v_output.extents(), |
| 47 | + // Write-only access bypasses synchronization but inserts appropriate |
| 48 | + // barriers if necessary. |
| 49 | + v_output.image(command_buffer, vTensor::Access::Write), |
| 50 | + // Read-only access is implied on const tensors and triggers an async |
| 51 | + // synchronization if necessary. |
| 52 | + v_self.image(command_buffer), |
| 53 | + // Object lifetime is managed by the resource pool. |
| 54 | + // It is OK not to keep track of the handle. |
| 55 | + context->resource().pool.uniform(block).object); |
| 56 | + } |
| 57 | + else { |
| 58 | + TORCH_CHECK(false, "Not implemented!"); |
| 59 | + } |
| 60 | + } |
| 61 | + command_buffer.end(); |
| 62 | + command_buffer.submit(context->gpu().queue); |
| 63 | + |
| 64 | + return convert(v_output); |
| 65 | +} |
| 66 | + |
| 67 | +Tensor& mul_scalar_( |
| 68 | + Tensor& self_arg, |
| 69 | + const Scalar other) { |
| 70 | + api::Context* const context = api::context(); |
| 71 | + |
| 72 | + TORCH_CHECK( |
| 73 | + self_arg.is_vulkan(), |
| 74 | + "Vulkan: In-place add is only supported on Vulkan tensors."); |
| 75 | + |
| 76 | + vTensor& v_self = convert(self_arg); |
| 77 | + |
| 78 | + api::Command::Buffer command_buffer = context->command().pool.allocate(); |
| 79 | + command_buffer.begin(); |
| 80 | + { |
| 81 | + if (v_self.has_image()) { |
| 82 | + const struct { |
| 83 | + uint32_t width, height, channels; |
| 84 | + float other; |
| 85 | + } block { |
| 86 | + v_self.extents().width, |
| 87 | + v_self.extents().height, |
| 88 | + v_self.extents().depth, |
| 89 | + other.to<float>(), |
| 90 | + }; |
| 91 | + |
| 92 | + context->dispatch( |
| 93 | + command_buffer, |
| 94 | + { |
| 95 | + VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, |
| 96 | + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 97 | + }, |
| 98 | + VK_KERNEL(mul_scalar_), |
| 99 | + v_self.extents(), |
| 100 | + // Read-Write access triggers an async synchronization if necessory |
| 101 | + // and inserts appropriate barriers if hazards are detected. |
| 102 | + v_self.image(command_buffer, vTensor::Access::Read | vTensor::Access::Write), |
| 103 | + // Object lifetime is managed by the resource pool. |
| 104 | + // It is OK not to keep track of the handle. |
| 105 | + context->resource().pool.uniform(block).object); |
| 106 | + } |
| 107 | + else { |
| 108 | + TORCH_CHECK(false, "Not implemented!"); |
| 109 | + } |
| 110 | + } |
| 111 | + command_buffer.end(); |
| 112 | + command_buffer.submit(context->gpu().queue); |
| 113 | + |
| 114 | + return self_arg; |
| 115 | +} |
| 116 | + |
| 117 | +#ifdef USE_VULKAN_API |
| 118 | + |
| 119 | +TORCH_LIBRARY_IMPL(aten, Vulkan, m) { |
| 120 | + m.impl("mul.Scalar", TORCH_FN(mul_scalar)); |
| 121 | + m.impl("mul_.Scalar", TORCH_FN(mul_scalar_)); |
| 122 | +} |
| 123 | + |
| 124 | +#endif /* USE_VULKAN_API */ |
| 125 | + |
| 126 | +} // namespace |
| 127 | +} // namespace ops |
| 128 | +} // namespace vulkan |
| 129 | +} // namespace native |
| 130 | +} // namespace at |
0 commit comments