|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "xla/service/gpu/runtime/resize_bicubic.h" |
| 17 | + |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#include <cstddef> |
| 21 | + |
| 22 | +#include "absl/status/status.h" |
| 23 | +#include "absl/types/span.h" |
| 24 | +#include "xla/runtime/custom_call.h" |
| 25 | +#include "xla/runtime/executable.h" |
| 26 | +#include "xla/service/gpu/runtime/resize_bicubic_kernel.h" |
| 27 | +// #include "xla/runtime/custom_call_registry.h" |
| 28 | + |
| 29 | +#include "xla/service/gpu/runtime/support.h" |
| 30 | +#include "xla/service/service_executable_run_options.h" |
| 31 | +#include "xla/xla_data.pb.h" |
| 32 | + |
| 33 | +namespace xla::gpu { |
| 34 | +using ::xla::runtime::CustomCall; |
| 35 | +using ::xla::runtime::StridedMemrefView; |
| 36 | + |
| 37 | +static absl::Status ResizeBicubicImpl( |
| 38 | + const ServiceExecutableRunOptions* run_options, StridedMemrefView input, |
| 39 | + StridedMemrefView output, bool align_corners) { |
| 40 | + float scales_h = |
| 41 | + static_cast<float>(output.sizes[2]) / static_cast<float>(input.sizes[2]); |
| 42 | + float scales_w = |
| 43 | + static_cast<float>(output.sizes[3]) / static_cast<float>(input.sizes[3]); |
| 44 | + se::StreamExecutor* executor = run_options->stream()->parent(); |
| 45 | + return RunResizeBicubicImpl( |
| 46 | + se::gpu::AsGpuStreamValue(run_options->stream()), |
| 47 | + executor->GetDeviceDescription().threads_per_block_limit(), input, output, |
| 48 | + align_corners, scales_h, scales_w); |
| 49 | +} |
| 50 | + |
| 51 | +static absl::Status ResizeBicubicGradImpl( |
| 52 | + const ServiceExecutableRunOptions* run_options, |
| 53 | + StridedMemrefView grad_output, StridedMemrefView grad_input, |
| 54 | + bool align_corners) { |
| 55 | + float scales_h = static_cast<float>(grad_output.sizes[2]) / |
| 56 | + static_cast<float>(grad_input.sizes[2]); |
| 57 | + float scales_w = static_cast<float>(grad_output.sizes[3]) / |
| 58 | + static_cast<float>(grad_input.sizes[3]); |
| 59 | + se::StreamExecutor* executor = run_options->stream()->parent(); |
| 60 | + return RunResizeBicubicGradImpl( |
| 61 | + se::gpu::AsGpuStreamValue(run_options->stream()), |
| 62 | + executor->GetDeviceDescription().threads_per_block_limit(), grad_input, |
| 63 | + grad_output, align_corners, scales_h, scales_w); |
| 64 | +} |
| 65 | + |
| 66 | +XLA_RUNTIME_DEFINE_CUSTOM_CALL( |
| 67 | + ResizeBicubic, FunctionWrapper<ResizeBicubicImpl>(), checks, |
| 68 | + CustomCall::Bind("__gpu$ResizeBicubic") |
| 69 | + .UserData<const ServiceExecutableRunOptions*>() |
| 70 | + .Arg<StridedMemrefView>() // input |
| 71 | + .Arg<StridedMemrefView>() // output |
| 72 | + .Attr<bool>("align_corners")); |
| 73 | + |
| 74 | +XLA_RUNTIME_DEFINE_CUSTOM_CALL( |
| 75 | + ResizeBicubicGrad, FunctionWrapper<ResizeBicubicGradImpl>(), checks, |
| 76 | + CustomCall::Bind("__gpu$ResizeBicubicGrad") |
| 77 | + .UserData<const ServiceExecutableRunOptions*>() |
| 78 | + .Arg<StridedMemrefView>() // grad_output |
| 79 | + .Arg<StridedMemrefView>() // grad_input |
| 80 | + .Attr<bool>("align_corners")); |
| 81 | + |
| 82 | +void RegisterResizeBicubicCustomCall( |
| 83 | + runtime::DirectCustomCallRegistry& registry) { |
| 84 | + registry.Register("__gpu$ResizeBicubic", ResizeBicubic); |
| 85 | + registry.Register("__gpu$ResizeBicubicGrad", ResizeBicubicGrad); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace xla::gpu |
0 commit comments