1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
#include "caffe2/operators/conv_transpose_op.h"
#include <vector>
#include "caffe2/core/context_gpu.h"
#include "caffe2/core/cudnn_wrappers.h"
#include "caffe2/operators/conv_op_cache_cudnn.h"
#include "caffe2/operators/op_utils_cudnn.h"
namespace caffe2 {
class CudnnConvTransposeOpBase : public ConvTransposeUnpoolBase<CUDAContext> {
public:
template <class... Args>
explicit CudnnConvTransposeOpBase(Args&&... args)
: ConvTransposeUnpoolBase<CUDAContext>(std::forward<Args>(args)...),
cudnn_wrapper_(&context_),
cudnn_ws_nbytes_limit_(OperatorBase::GetSingleArgument<size_t>(
"ws_nbytes_limit",
kCONV_CUDNN_WORKSPACE_LIMIT_BYTES)),
exhaustive_search_(
OperatorBase::GetSingleArgument<int>("exhaustive_search", 0)),
deterministic_(
OperatorBase::GetSingleArgument<int>("deterministic", 0)),
cudnn_state_(OperatorBase::GetSingleArgument<int>("cudnn_state", 0)),
force_algo_(OperatorBase::GetRepeatedArgument<int>(
"force_algo",
vector<int>{-1, -1, -1})),
enable_tensor_core_(
OperatorBase::GetSingleArgument<bool>("enable_tensor_core", 1)) {
CAFFE_ENFORCE(!deterministic_ || !exhaustive_search_);
bool individual_force_algo = OperatorBase::HasArgument("force_algo_fwd") ||
OperatorBase::HasArgument("force_algo_dgrad") ||
OperatorBase::HasArgument("force_algo_wgrad");
if (OperatorBase::HasArgument("force_algo")) {
CAFFE_ENFORCE(
!individual_force_algo,
"Cannot specify both force_algo and any of",
"force_algo_fwd, force_algo_dgrad, force_algo_wgrad");
} else {
force_algo_ = std::vector<int>{-1, -1, -1};
force_algo_[ALGO_FWD] =
OperatorBase::GetSingleArgument<int>("force_algo_fwd", -1);
force_algo_[ALGO_DGRAD] =
OperatorBase::GetSingleArgument<int>("force_algo_dgrad", -1);
force_algo_[ALGO_WGRAD] =
OperatorBase::GetSingleArgument<int>("force_algo_wgrad", -1);
}
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&bottom_desc_));
CUDNN_ENFORCE(cudnnCreateFilterDescriptor(&filter_desc_));
if (InputSize() == 3) {
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&bias_desc_));
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&top_desc_for_bias_));
}
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&top_desc_));
CUDNN_ENFORCE(cudnnCreateConvolutionDescriptor(&conv_desc_));
}
~CudnnConvTransposeOpBase() override {
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(bottom_desc_));
CUDNN_ENFORCE(cudnnDestroyFilterDescriptor(filter_desc_));
if (InputSize() == 3) {
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(bias_desc_));
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(top_desc_for_bias_));
}
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(top_desc_));
CUDNN_ENFORCE(cudnnDestroyConvolutionDescriptor(conv_desc_));
}
protected:
void SetTensor4DDescriptorWithGroup(
const cudnnDataType_t data_type,
const int N,
const int C,
const int H,
const int W,
cudnnTensorDescriptor_t* desc) const {
#if CUDNN_VERSION_MIN(7, 0, 0)
const int CC = C;
#else
const int CC = C / group_;
#endif
switch (order_) {
case StorageOrder::NCHW: {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptorEx(
*desc, data_type, N, CC, H, W, C * H * W, H * W, W, 1));
break;
}
case StorageOrder::NHWC: {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptorEx(
*desc, data_type, N, CC, H, W, H * W * C, 1, W * C, C));
break;
}
default: {
LOG(FATAL) << "Unknown storage order: " << order_;
}
}
}
std::vector<std::int64_t> cudnn_input_dims_;
std::vector<std::int64_t> cudnn_filter_dims_;
CuDNNWrapper cudnn_wrapper_;
cudnnTensorDescriptor_t bottom_desc_;
cudnnFilterDescriptor_t filter_desc_;
cudnnTensorDescriptor_t bias_desc_;
cudnnTensorDescriptor_t top_desc_;
cudnnTensorDescriptor_t top_desc_for_bias_;
cudnnConvolutionDescriptor_t conv_desc_;
const size_t cudnn_ws_nbytes_limit_;
size_t cudnn_ws_nbytes_;
bool exhaustive_search_;
bool deterministic_;
size_t cudnn_state_;
std::vector<int> force_algo_; // stored as FWD, dFILTER, dDATA
bool enable_tensor_core_;
};
template <typename T>
class CudnnConvTransposeOp final : public CudnnConvTransposeOpBase {
public:
template <class... Args>
explicit CudnnConvTransposeOp(Args&&... args)
: CudnnConvTransposeOpBase(std::forward<Args>(args)...) {}
~CudnnConvTransposeOp() override {}
bool RunOnDevice() override;
private:
AlgorithmsCache<cudnnConvolutionBwdDataAlgo_t> data_algo_cache_;
cudnnConvolutionBwdDataAlgo_t bwd_data_algo_;
// Input: X, W, b
// Output: Y
INPUT_TAGS(INPUT, FILTER, BIAS);
};
template <typename T>
class CudnnConvTransposeGradientOp final : public CudnnConvTransposeOpBase {
public:
template <class... Args>
explicit CudnnConvTransposeGradientOp(Args&&... args)
: CudnnConvTransposeOpBase(std::forward<Args>(args)...),
no_bias_(OperatorBase::GetSingleArgument<bool>("no_bias", false)) {
CAFFE_ENFORCE(
!(no_bias_ && OutputSize() == 3),
"If bias is not present, you should not have 3 grad output.");
}
~CudnnConvTransposeGradientOp() override {}
bool RunOnDevice() override;
private:
cudnnConvolutionFwdAlgo_t algo_;
cudnnConvolutionBwdFilterAlgo_t bwd_filter_algo_;
AlgorithmsCache<cudnnConvolutionFwdAlgo_t> forward_algo_cache_;
AlgorithmsCache<cudnnConvolutionBwdFilterAlgo_t> filter_algo_cache_;
const bool no_bias_;
// input: X, W, dY
// output: dW, optionally db and dX
INPUT_TAGS(INPUT, FILTER, OUTPUT_GRAD);
OUTPUT_TAGS(FILTER_GRAD, BIAS_OR_INPUT_GRAD, INPUT_GRAD);
};
////////////////////////////////////////////////////////////////////////////////
// Implementations
////////////////////////////////////////////////////////////////////////////////
template <typename T>
bool CudnnConvTransposeOp<T>::RunOnDevice() {
auto& X = Input(INPUT);
auto& filter = Input(FILTER);
int C = 0;
switch (order_) {
case StorageOrder::NHWC:
C = filter.dim32(3) * group_;
break;
case StorageOrder::NCHW:
C = filter.dim32(1) * group_;
break;
default:
LOG(FATAL) << "Unknown storage order: " << order_;
}
auto sizes = ConvTransposeUnpoolBase<CUDAContext>::GetOutputSize(X, C);
auto* Y = Output(0, sizes, at::dtype<T>());
if (X.numel() == 0) {
VLOG(2) << "Number on elements is 0 in CudnnConvTransposeOp";
return true;
}
int N = 0, M = 0, H = 0, W = 0, H_out = 0, W_out = 0;
switch (order_) {
case StorageOrder::NHWC:
N = X.dim32(0);
H = X.dim32(1);
W = X.dim32(2);
M = X.dim32(3);
H_out = Y->dim32(1);
W_out = Y->dim32(2);
CAFFE_ENFORCE_EQ(filter.dim32(1), kernel_h());
CAFFE_ENFORCE_EQ(filter.dim32(2), kernel_w());
CAFFE_ENFORCE_EQ(filter.dim32(3), C / group_);
break;
case StorageOrder::NCHW:
N = X.dim32(0);
M = X.dim32(1);
H = X.dim32(2);
W = X.dim32(3);
H_out = Y->dim32(2);
W_out = Y->dim32(3);
CAFFE_ENFORCE_EQ(filter.dim32(1), C / group_);
CAFFE_ENFORCE_EQ(filter.dim32(2), kernel_h());
CAFFE_ENFORCE_EQ(filter.dim32(3), kernel_w());
break;
default:
LOG(FATAL) << "Unknown storage order: " << order_;
}
CAFFE_ENFORCE_EQ(M % group_, 0);
if (InputSize() == 3) {
auto& bias = Input(BIAS);
CAFFE_ENFORCE_EQ(bias.dim(), 1);
CAFFE_ENFORCE_EQ(bias.dim32(0), C);
}
// Set up the cudnn algorithms & workspace if necessary
const bool input_changed = (X.sizes() != cudnn_input_dims_);
const bool filter_changed = (filter.sizes() != cudnn_filter_dims_);
if (input_changed || filter_changed) {
VLOG(1) << "Changing the cudnn descriptor configurations.";
if (input_changed) {
cudnn_input_dims_ = X.sizes().vec();
SetTensor4DDescriptorWithGroup(
cudnnTypeWrapper<T>::type, N, M, H, W, &bottom_desc_);
}
if (filter_changed) {
cudnn_filter_dims_ = filter.sizes().vec();
#if CUDNN_VERSION_MIN(7, 0, 0)
const int MM = M;
#else
const int MM = M / group_;
#endif
CUDNN_ENFORCE(cudnnSetFilter4dDescriptor(
filter_desc_,
cudnnTypeWrapper<T>::type,
GetCudnnTensorFormat(order_),
MM,
C / group_,
kernel_h(),
kernel_w()));
if (InputSize() == 3) {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
bias_desc_,
GetCudnnTensorFormat(order_),
cudnnTypeWrapper<T>::type,
1,
C,
1,
1));
}
}
// Set the output
SetTensor4DDescriptorWithGroup(
cudnnTypeWrapper<T>::type, N, C, H_out, W_out, &top_desc_);
if (InputSize() == 3) {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
top_desc_for_bias_,
GetCudnnTensorFormat(order_),
cudnnTypeWrapper<T>::type,
N,
C,
H_out,
W_out));
}
// Set the convolution descriptor
CAFFE_ENFORCE_EQ(
pad_t(),
pad_b(),
"The current padding scheme leads to unequal padding on the top and "
"bottom, which is not supported by cudnn.");
CAFFE_ENFORCE_EQ(
pad_l(),
pad_r(),
"The current padding scheme leads to unequal padding on the left "
"and right, which is not supported by cudnn.");
// Set the convolution descriptor
#if CUDNN_VERSION_MIN(6, 0, 0)
CUDNN_ENFORCE(cudnnSetConvolution2dDescriptor(
conv_desc_,
pad_t(),
pad_l(),
stride_h(),
stride_w(),
1,
1,
CUDNN_CROSS_CORRELATION,
cudnnTypeWrapper<T>::type));
#else
CUDNN_ENFORCE(cudnnSetConvolution2dDescriptor(
conv_desc_,
pad_t(),
pad_l(),
stride_h(),
stride_w(),
1,
1,
CUDNN_CROSS_CORRELATION));
#endif
#if CUDNN_VERSION_MIN(7, 0, 0)
// enable TensorCore math if desired
enable_tensor_core_ &= TensorCoreAvailable();
if (enable_tensor_core_) {
CUDNN_ENFORCE(
cudnnSetConvolutionMathType(conv_desc_, CUDNN_TENSOR_OP_MATH));
}
// set cuDNN groups if appropriate
CUDNN_ENFORCE(cudnnSetConvolutionGroupCount(conv_desc_, group_));
#endif
if (force_algo_[ALGO_DGRAD] >= 0) {
bwd_data_algo_ = (cudnnConvolutionBwdDataAlgo_t)force_algo_[ALGO_DGRAD];
} else if (deterministic_) {
bwd_data_algo_ = CUDNN_CONVOLUTION_BWD_DATA_ALGO_1;
} else if (exhaustive_search_) {
bwd_data_algo_ =
data_algo_cache_.getAlgorithm(X.sizes(), filter.sizes(), 0, [&]() {
int returned_algo_count;
std::array<
cudnnConvolutionBwdDataAlgoPerf_t,
kNUM_CUDNN_BWD_DATA_ALGS>
data_perf_stat;
cudnn_wrapper_.with_cudnn_state(
cudnn_state_, [&](CuDNNState* state) {
state->workspace().reset();
CUDNN_ENFORCE(cudnnFindConvolutionBackwardDataAlgorithm(
state->cudnn_handle(),
filter_desc_,
bottom_desc_,
conv_desc_,
top_desc_,
kNUM_CUDNN_BWD_DATA_ALGS,
&returned_algo_count,
data_perf_stat.data()));
});
LogCuDNNPerfStats(data_perf_stat, returned_algo_count);
return data_perf_stat[0].algo;
});
} else {
constexpr int nalgo = CUDNN_CONVOLUTION_BWD_DATA_ALGO_COUNT;
int valid_algos;
cudnnConvolutionBwdDataAlgoPerf_t algos[nalgo];
CUDNN_ENFORCE(cudnnGetConvolutionBackwardDataAlgorithm_v7(
cudnn_wrapper_.inline_cudnn_handle(),
filter_desc_,
bottom_desc_,
conv_desc_,
top_desc_,
nalgo,
&valid_algos,
algos));
bool found = false;
for (int i = 0; i < valid_algos; i++) {
auto a = algos[i];
if (a.memory <= cudnn_ws_nbytes_limit_) {
bwd_data_algo_ = a.algo;
found = true;
break;
}
}
CAFFE_ENFORCE(found, "Unable to find algorithms for cuDNN backward data");
}
size_t bwd_data_ws_size;
CUDNN_ENFORCE(cudnnGetConvolutionBackwardDataWorkspaceSize(
cudnn_wrapper_.inline_cudnn_handle(),
filter_desc_,
bottom_desc_,
conv_desc_,
top_desc_,
bwd_data_algo_,
&bwd_data_ws_size));
cudnn_ws_nbytes_ = bwd_data_ws_size;
VLOG(1) << "CuDNN algorithm: " << bwd_data_algo_;
VLOG(1) << "CuDNN workspace size: " << bwd_data_ws_size;
}
const T* X_data = X.template data<T>();
const T* filter_data = filter.template data<T>();
T* Y_data = Y->template mutable_data<T>();
// Now, actually run the computation.
// Filter
#if CUDNN_VERSION_MIN(7, 0, 0)
cudnn_wrapper_.with_cudnn_state(cudnn_state_, [&](CuDNNState* state) {
CUDNN_ENFORCE(cudnnConvolutionBackwardData(
state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
filter_desc_,
filter_data,
bottom_desc_,
X_data,
conv_desc_,
bwd_data_algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T>::kZero(),
top_desc_,
Y_data));
});
#else
const int X_HxW = H * W;
const int Y_HxW = H_out * W_out;
const int group_offset_X =
order_ == StorageOrder::NCHW ? M / group_ * X_HxW : M / group_;
const int group_offset_Y =
order_ == StorageOrder::NCHW ? C / group_ * Y_HxW : C / group_;
const int group_offset_filter = filter.numel() / group_;
for (int i = 0; i < group_; ++i) {
cudnn_wrapper_.with_cudnn_state(cudnn_state_, [&](CuDNNState* state) {
CUDNN_ENFORCE(
cudnnConvolutionBackwardData(state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
filter_desc_,
filter_data + i * group_offset_filter,
bottom_desc_,
X_data + i * group_offset_X;
conv_desc_,
bwd_data_algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T_DX>::kZero(),
top_desc_,
Y_data + i * group_offset_Y));
});
}
#endif
// Bias
if (InputSize() == 3) {
CUDNN_ENFORCE(cudnnAddTensor(
cudnn_wrapper_.inline_cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
bias_desc_,
Input(BIAS).template data<T>(),
cudnnTypeWrapper<T>::kOne(),
top_desc_for_bias_,
Y->template mutable_data<T>()));
}
// Done.
return true;
}
// TODO(Yangqing): a lot of the function contents are very similar. Consider
// consolidating them.
template <typename T>
bool CudnnConvTransposeGradientOp<T>::RunOnDevice() {
const auto& X = Input(INPUT);
const auto& filter = Input(FILTER);
const auto& dY = Input(OUTPUT_GRAD);
CAFFE_ENFORCE_EQ(X.dim(), 4);
CAFFE_ENFORCE_EQ(filter.dim(), 4);
int C = 0;
switch (order_) {
case StorageOrder::NHWC:
C = filter.dim32(3) * group_;
break;
case StorageOrder::NCHW:
C = filter.dim32(1) * group_;
break;
default:
LOG(FATAL) << "Unknown storage order: " << order_;
}
int N = 0, M = 0, H = 0, W = 0, H_out = 0, W_out = 0;
switch (order_) {
case StorageOrder::NHWC:
N = X.dim32(0);
H = X.dim32(1);
W = X.dim32(2);
M = X.dim32(3);
H_out = dY.dim32(1);
W_out = dY.dim32(2);
CAFFE_ENFORCE_EQ(filter.dim32(1), kernel_h());
CAFFE_ENFORCE_EQ(filter.dim32(1), kernel_h());
CAFFE_ENFORCE_EQ(filter.dim32(2), kernel_w());
CAFFE_ENFORCE_EQ(filter.dim32(3), C / group_);
break;
case StorageOrder::NCHW:
N = X.dim32(0);
M = X.dim32(1);
H = X.dim32(2);
W = X.dim32(3);
H_out = dY.dim32(2);
W_out = dY.dim32(3);
CAFFE_ENFORCE_EQ(filter.dim32(1), C / group_);
CAFFE_ENFORCE_EQ(filter.dim32(2), kernel_h());
CAFFE_ENFORCE_EQ(filter.dim32(3), kernel_w());
break;
default:
LOG(FATAL) << "Unknown storage order: " << order_;
}
CAFFE_ENFORCE_EQ(M % group_, 0);
// Since we only handle LegacyPadding::NOTSET, we don't need to
// compute padding.
auto* dfilter = Output(FILTER_GRAD, filter.sizes(), at::dtype<T>());
// Set up the cudnn algorithms & workspace if necessary
const bool input_changed = (X.sizes() != cudnn_input_dims_);
const bool filter_changed = (filter.sizes() != cudnn_filter_dims_);
if (input_changed || filter_changed) {
VLOG(1) << "Changing the cudnn descriptor configurations.";
if (input_changed) {
cudnn_input_dims_ = X.sizes().vec();
SetTensor4DDescriptorWithGroup(
cudnnTypeWrapper<T>::type, N, M, H, W, &bottom_desc_);
}
if (filter_changed) {
cudnn_filter_dims_ = filter.sizes().vec();
#if CUDNN_VERSION_MIN(7, 0, 0)
const int MM = M;
#else
const int MM = M / group_;
#endif
CUDNN_ENFORCE(cudnnSetFilter4dDescriptor(
filter_desc_,
cudnnTypeWrapper<T>::type,
GetCudnnTensorFormat(order_),
MM,
C / group_,
kernel_h(),
kernel_w()));
if (!no_bias_) {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
bias_desc_,
GetCudnnTensorFormat(order_),
cudnnTypeWrapper<T>::type,
1,
C,
1,
1));
}
}
// Set the output
SetTensor4DDescriptorWithGroup(
cudnnTypeWrapper<T>::type, N, C, H_out, W_out, &top_desc_);
if (!no_bias_) {
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
top_desc_for_bias_,
GetCudnnTensorFormat(order_),
cudnnTypeWrapper<T>::type,
N,
C,
H_out,
W_out));
}
// Set the convolution descriptor
CAFFE_ENFORCE_EQ(
pad_t(),
pad_b(),
"The current padding scheme leads to unequal padding on the top and "
"bottom, which is not supported by cudnn.");
CAFFE_ENFORCE_EQ(
pad_l(),
pad_r(),
"The current padding scheme leads to unequal padding on the left "
"and right, which is not supported by cudnn.");
#if CUDNN_VERSION_MIN(6, 0, 0)
CUDNN_ENFORCE(cudnnSetConvolution2dDescriptor(
conv_desc_,
pad_t(),
pad_l(),
stride_h(),
stride_w(),
1,
1,
CUDNN_CROSS_CORRELATION,
cudnnTypeWrapper<T>::type));
#else
CUDNN_ENFORCE(cudnnSetConvolution2dDescriptor(
conv_desc_,
pad_t(),
pad_l(),
stride_h(),
stride_w(),
1,
1,
CUDNN_CROSS_CORRELATION));
#endif
#if CUDNN_VERSION_MIN(7, 0, 0)
// enable TensorCore math if desired
enable_tensor_core_ &= TensorCoreAvailable();
if (enable_tensor_core_) {
CUDNN_ENFORCE(
cudnnSetConvolutionMathType(conv_desc_, CUDNN_TENSOR_OP_MATH));
}
// set cuDNN groups if appropriate
CUDNN_CHECK(cudnnSetConvolutionGroupCount(conv_desc_, group_));
#endif
if (force_algo_[ALGO_WGRAD] >= 0) {
bwd_filter_algo_ =
(cudnnConvolutionBwdFilterAlgo_t)force_algo_[ALGO_WGRAD];
} else if (deterministic_) {
algo_ = CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM;
bwd_filter_algo_ = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1;
} else if (exhaustive_search_) {
bwd_filter_algo_ =
filter_algo_cache_.getAlgorithm(X.sizes(), filter.sizes(), 0, [&]() {
LOG(INFO) << "CUDNN Convolution bwd: doing exhaustive search.";
// When we do an exhaustive search, we will ignore the workspace
// size
// limit and simply go for the fastest algorithm. If you happen to
// run
// out of memory later, you will be on your own...
int returned_algo_count;
// We clean up the current workspace memory so that the forward
// algorithm
// is free to allocate memory.
// Actually run the search.
std::array<
cudnnConvolutionBwdFilterAlgoPerf_t,
kNUM_CUDNN_BWD_FILTER_ALGS>
filter_perf_stat;
cudnn_wrapper_.with_cudnn_state(
cudnn_state_, [&](CuDNNState* state) {
state->workspace().reset();
CUDNN_ENFORCE(cudnnFindConvolutionBackwardFilterAlgorithm(
state->cudnn_handle(),
top_desc_,
bottom_desc_,
conv_desc_,
filter_desc_,
kNUM_CUDNN_BWD_FILTER_ALGS,
&returned_algo_count,
filter_perf_stat.data()));
});
LogCuDNNPerfStats(filter_perf_stat, returned_algo_count);
return filter_perf_stat[0].algo;
});
algo_ =
forward_algo_cache_.getAlgorithm(X.sizes(), filter.sizes(), 0, [&]() {
int returned_algo_count;
std::array<cudnnConvolutionFwdAlgoPerf_t, kNUM_CUDNN_FWD_ALGS>
fwd_perf_stat;
cudnn_wrapper_.with_cudnn_state(
cudnn_state_, [&](CuDNNState* state) {
state->workspace().reset();
CUDNN_ENFORCE(cudnnFindConvolutionForwardAlgorithm(
state->cudnn_handle(),
top_desc_,
filter_desc_,
conv_desc_,
bottom_desc_,
kNUM_CUDNN_BWD_DATA_ALGS,
&returned_algo_count,
fwd_perf_stat.data()));
});
LogCuDNNPerfStats(fwd_perf_stat, returned_algo_count);
return fwd_perf_stat[0].algo;
});
} else {
// choose backward algorithm for filter
{
constexpr int nalgo = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_COUNT;
int valid_algos;
cudnnConvolutionBwdFilterAlgoPerf_t algos[nalgo];
CUDNN_ENFORCE(cudnnGetConvolutionBackwardFilterAlgorithm_v7(
cudnn_wrapper_.inline_cudnn_handle(),
top_desc_,
bottom_desc_,
conv_desc_,
filter_desc_,
nalgo,
&valid_algos,
algos));
bool found = false;
for (int i = 0; i < valid_algos; i++) {
auto a = algos[i];
if (a.memory <= cudnn_ws_nbytes_limit_) {
bwd_filter_algo_ = a.algo;
found = true;
break;
}
}
CAFFE_ENFORCE(found, "Unable to find algorithms for cuDNN backward filter");
}
// choose backward algo for data
{
constexpr int nalgo = CUDNN_CONVOLUTION_FWD_ALGO_COUNT;
int valid_algos;
cudnnConvolutionFwdAlgoPerf_t algos[nalgo];
CUDNN_ENFORCE(cudnnGetConvolutionForwardAlgorithm_v7(
cudnn_wrapper_.inline_cudnn_handle(),
top_desc_,
filter_desc_,
conv_desc_,
bottom_desc_,
nalgo,
&valid_algos,
algos));
bool found = false;
for (int i = 0; i < valid_algos; i++) {
auto a = algos[i];
if (a.memory <= cudnn_ws_nbytes_limit_) {
algo_ = a.algo;
found = true;
break;
}
}
CAFFE_ENFORCE(found, "Unable to find algorithms for cuDNN forward");
}
}
// get workspace for backwards filter algorithm
size_t bwd_filter_ws_size, fwd_ws_size;
CUDNN_ENFORCE(cudnnGetConvolutionBackwardFilterWorkspaceSize(
cudnn_wrapper_.inline_cudnn_handle(),
top_desc_,
bottom_desc_,
conv_desc_,
filter_desc_,
bwd_filter_algo_,
&bwd_filter_ws_size));
// get workspace for backwards data algorithm
CUDNN_ENFORCE(cudnnGetConvolutionForwardWorkspaceSize(
cudnn_wrapper_.inline_cudnn_handle(),
top_desc_,
filter_desc_,
conv_desc_,
bottom_desc_,
algo_,
&fwd_ws_size));
cudnn_ws_nbytes_ = std::max(bwd_filter_ws_size, fwd_ws_size);
VLOG(1) << "CuDNN bwd algorithm: " << bwd_filter_algo_ << ", " << algo_;
VLOG(1) << "CuDNN workspace size: " << cudnn_ws_nbytes_;
}
// Now, actually run the computation.
if (!no_bias_) {
auto* dbias = Output(BIAS_OR_INPUT_GRAD, {C}, at::dtype<T>());
CUDNN_ENFORCE(cudnnConvolutionBackwardBias(
cudnn_wrapper_.inline_cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
top_desc_for_bias_,
dY.template data<T>(),
cudnnTypeWrapper<T>::kZero(),
bias_desc_,
dbias->template mutable_data<T>()));
}
#if CUDNN_VERSION_MIN(7, 0, 0)
cudnn_wrapper_.with_cudnn_state(cudnn_state_, [&](CuDNNState* state) {
CUDNN_ENFORCE(cudnnConvolutionBackwardFilter(
state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
top_desc_,
dY.template data<T>(),
bottom_desc_,
X.template data<T>(),
conv_desc_,
bwd_filter_algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T>::kZero(),
filter_desc_,
dfilter->template mutable_data<T>()));
if (OutputSize() == 3 || (no_bias_ && (OutputSize() == 2))) {
// Compute the gradient w.r.t. the input.
auto* dX = Output(
no_bias_ ? BIAS_OR_INPUT_GRAD : INPUT_GRAD,
X.sizes(),
at::dtype<T>());
CUDNN_ENFORCE(cudnnConvolutionForward(
state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
top_desc_,
dY.template data<T>(),
filter_desc_,
filter.template data<T>(),
conv_desc_,
algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T>::kZero(),
bottom_desc_,
dX->template mutable_data<T>()));
}
});
#else
const int X_HxW = H * W;
const int Y_HxW = H_out * W_out;
const int group_offset_X =
order_ == StorageOrder::NCHW ? M / group_ * X_HxW : M / group_;
const int group_offset_Y =
order_ == StorageOrder::NCHW ? C / group_ * Y_HxW : C / group_;
const int group_offset_filter = filter.numel() / group_;
for (int i = 0; i < group_; ++i) {
cudnn_wrapper_.with_cudnn_state(cudnn_state_, [&](CuDNNState* state) {
CUDNN_ENFORCE(cudnnConvolutionBackwardFilter(
state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
top_desc_,
dY.template data<T>() + i * group_offset_Y,
bottom_desc_,
X.template data<T>() + i * group_offset_X,
conv_desc_,
bwd_filter_algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T>::kZero(),
filter_desc_,
dfilter->template mutable_data<T>() + i * group_offset_filter));
if (OutputSize() == 3 || (no_bias_ && (OutputSize() == 2))) {
// Compute the gradient w.r.t. the input.
auto* dX = Output(
no_bias_ ? BIAS_OR_INPUT_GRAD : INPUT_GRAD,
X.sizes(),
at::dtype<T>());
cudnn_wrapper_.with_cudnn_state(cudnn_state_, [&](CuDNNState* state) {
CUDNN_ENFORCE(cudnnConvolutionForward(
state->cudnn_handle(),
cudnnTypeWrapper<T>::kOne(),
top_desc_,
dY.template data<T>() + i * group_offset_Y,
filter_desc_,
filter.template data<T>() + i * group_offset_filter,
conv_desc_,
algo_,
state->workspace().get(cudnn_ws_nbytes_),
cudnn_ws_nbytes_,
cudnnTypeWrapper<T>::kZero(),
bottom_desc_,
dX->template mutable_data<T>() + i * group_offset_X));
});
}
}
#endif
return true;
}
REGISTER_CUDNN_OPERATOR(ConvTranspose, CudnnConvTransposeOp<float>);
REGISTER_CUDNN_OPERATOR(
ConvTransposeGradient,
CudnnConvTransposeGradientOp<float>);
} // namespace caffe2
|