MCPcopy Create free account
hub / github.com/davisking/dlib / softmax_gradient

Function softmax_gradient

dlib/cuda/cudnn_dlibapi.cpp:1754–1814  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1752 }
1753
1754 void softmax_gradient(
1755 tensor& grad,
1756 const tensor& dest,
1757 const tensor& gradient_input,
1758 operation_mode mode
1759 )
1760 {
1761 DLIB_CASSERT(
1762 have_same_dimensions(dest, gradient_input) == true &&
1763 have_same_dimensions(dest, grad) == true);
1764 if (dest.size() == 0) return;
1765
1766 const float alpha = 1;
1767 const float beta = is_same_object(grad, gradient_input) ? 0 : 1;
1768
1769 if (mode == operation_mode::CHANNEL_WISE)
1770 {
1771 CHECK_CUDNN(cudnnSoftmaxBackward(context(),
1772 CUDNN_SOFTMAX_ACCURATE,
1773 CUDNN_SOFTMAX_MODE_CHANNEL,
1774 &alpha,
1775 descriptor(dest),
1776 dest.device(),
1777 descriptor(gradient_input),
1778 gradient_input.device(),
1779 &beta,
1780 descriptor(grad),
1781 grad.device()));
1782 }
1783 else if (mode == operation_mode::PLANE_WISE)
1784 {
1785 const long num_samples = dest.num_samples();
1786 const long num_channels = dest.k();
1787 const size_t plane_size = dest.nr() * dest.nc();
1788
1789 for (long s = 0; s < num_samples; ++s)
1790 {
1791 for (long k = 0; k < num_channels; ++k)
1792 {
1793 auto dest_slice = dest.device() + (s * num_channels + k) * plane_size;
1794 auto gi_slice = gradient_input.device() + (s * num_channels + k) * plane_size;
1795 auto grad_slice = grad.device() + (s * num_channels + k) * plane_size;
1796 auto a_dest_slice = alias_tensor(dest.nr(), dest.nc())(dest, (s * num_channels + k) * plane_size);
1797 auto a_gi_slice = alias_tensor(gradient_input.nr(), gradient_input.nc())(gradient_input, (s * num_channels + k) * plane_size);
1798 auto a_grad_slice = alias_tensor(grad.nr(), grad.nc())(grad, (s * num_channels + k) * plane_size);
1799
1800 CHECK_CUDNN(cudnnSoftmaxBackward(context(),
1801 CUDNN_SOFTMAX_ACCURATE,
1802 CUDNN_SOFTMAX_MODE_CHANNEL,
1803 &alpha,
1804 descriptor(a_dest_slice),
1805 dest_slice,
1806 descriptor(a_gi_slice),
1807 gi_slice,
1808 &beta,
1809 descriptor(a_grad_slice),
1810 grad_slice));
1811 }

Callers

nothing calls this directly

Calls 11

descriptorFunction · 0.85
have_same_dimensionsFunction · 0.70
contextFunction · 0.70
alias_tensorClass · 0.70
is_same_objectFunction · 0.50
sizeMethod · 0.45
deviceMethod · 0.45
num_samplesMethod · 0.45
kMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45

Tested by

no test coverage detected