Skip to content

Commit e7e6265

Browse files
authored
Merge branch 'main' into main
2 parents 3818397 + 8c88fd0 commit e7e6265

File tree

17 files changed

+438
-63
lines changed

17 files changed

+438
-63
lines changed

RELEASENOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ __Bug Fixes__:
77

88
#1426 Sequential.eval() does not put model into eval mode<br/>
99
`torch.optim.lr_scheduler.LinearLR` `end_factor` default has been corrected, is now 1.0.<br/>
10+
11+
__API Changes__:
12+
13+
#1374 Add accumulate to index_put_<br/>
1014
`torch.optim.lr_scheduler.PolynomialLR` `power` type has been corrected, is now double.<br/>
15+
Returning an input tensor has been corrected, is now `alias()`.<br/>
16+
Add `torchvision.transforms.Resize` `interpolation` and `antialias`.<br />
1117

1218
# NuGet Version 0.105.0
1319

build/BranchInfo.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<MajorVersion>0</MajorVersion>
44
<MinorVersion>105</MinorVersion>
5-
<PatchVersion>0</PatchVersion>
6-
<PreviousPackageVersion>0.104.0</PreviousPackageVersion>
5+
<PatchVersion>1</PatchVersion>
6+
<PreviousPackageVersion>0.105.0</PreviousPackageVersion>
77
</PropertyGroup>
88
</Project>

src/Native/LibTorchSharp/THSNN.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ void ApplyInterpolateMode(T& opts, const int8_t mode)
109109
opts = opts.mode(torch::kTrilinear);
110110
if (mode == 5)
111111
opts = opts.mode(torch::kArea);
112+
if (mode == 6)
113+
opts = opts.mode(torch::kNearestExact);
112114
}
113115

114116
template<typename T>
@@ -176,13 +178,14 @@ Tensor THSNN_affine_grid(const Tensor theta, const int64_t* size, const int size
176178
}
177179

178180

179-
EXPORT_API(Tensor) THSNN_interpolate(const Tensor input, const int64_t* size, const int size_len, const double* scale_factor, const int scale_factor_len, const int8_t mode, const int8_t align_corners, const bool recompute_scale_factor, NNAnyModule* outAsAnyModule)
181+
EXPORT_API(Tensor) THSNN_interpolate(const Tensor input, const int64_t* size, const int size_len, const double* scale_factor, const int scale_factor_len, const int8_t mode, const int8_t align_corners, const bool recompute_scale_factor, const bool antialias, NNAnyModule* outAsAnyModule)
180182
{
181183
auto opts = torch::nn::functional::InterpolateFuncOptions().recompute_scale_factor(recompute_scale_factor);
182184
// align_corners -- 0=None, 1=true, 2=false
183185
if (align_corners != 0)
184186
opts.align_corners(align_corners == 1);
185187
ApplyInterpolateMode(opts, mode);
188+
opts.antialias(antialias);
186189

187190
if (size_len > 0) {
188191
std::vector<int64_t> sizes;

src/Native/LibTorchSharp/THSNN.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ EXPORT_API(Tensor) THSNN_pixel_unshuffle(const Tensor tensor, const int64_t do
7171
// Vision -- Functions
7272

7373
EXPORT_API(Tensor) THSNN_pad(const Tensor input, const int64_t* pad, const int pad_length, const int8_t mode, const double value);
74-
EXPORT_API(Tensor) THSNN_interpolate(const Tensor input, const int64_t* size, const int size_len, const double* scale_factor, const int scale_factor_len, const int8_t mode, const int8_t align_corners, const bool recompute_scale_factor, NNAnyModule* outAsAnyModule);
74+
EXPORT_API(Tensor) THSNN_interpolate(const Tensor input, const int64_t* size, const int size_len, const double* scale_factor, const int scale_factor_len, const int8_t mode, const int8_t align_corners, const bool recompute_scale_factor, const bool antialias, NNAnyModule* outAsAnyModule);
7575
EXPORT_API(Tensor) THSNN_grid_sample(const Tensor input, const Tensor grid, const int8_t mode, const int8_t padding_mode, const int8_t align_corners);
7676
EXPORT_API(Tensor) THSNN_affine_grid(const Tensor theta, const int64_t* size, const int size_len, const bool align_corners);
7777

src/Native/LibTorchSharp/THSTensor.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,31 @@ void THSTensor_index_put_(Tensor tensor,
837837
CATCH(tensor->index_put_(indices, *value););
838838
}
839839

840+
void THSTensor_index_put_(Tensor tensor,
841+
const int64_t* indexStarts,
842+
const int64_t* indexEnds,
843+
const int64_t* indexSteps,
844+
const Tensor* indexTensors,
845+
const int indicesLength,
846+
const Tensor value,
847+
const bool accumulate)
848+
{
849+
at::indexing::TensorIndex* indicesArray = (at::indexing::TensorIndex*)alloca(indicesLength * sizeof(at::indexing::TensorIndex));
850+
memset(indicesArray, 0, indicesLength * sizeof(at::indexing::TensorIndex));
851+
completeTensorIndices(indexStarts, indexEnds, indexSteps, indexTensors, indicesArray, indicesLength);
852+
auto indices = at::ArrayRef<at::indexing::TensorIndex>(indicesArray, indicesLength);
853+
if (accumulate) {
854+
c10::List<std::optional<at::Tensor>> indicesList = c10::List<std::optional<at::Tensor>>();
855+
for (int i = 0; i < indicesLength; i++) {
856+
indicesList.push_back(c10::optional<at::Tensor>(*indexTensors[i]));
857+
}
858+
CATCH(tensor->index_put_(indicesList, *value, accumulate););
859+
}
860+
else {
861+
CATCH(tensor->index_put_(indices, *value););
862+
}
863+
}
864+
840865
void THSTensor_index_put_scalar_(Tensor tensor,
841866
const int64_t* indexStarts,
842867
const int64_t* indexEnds,

src/Native/LibTorchSharp/THSTensor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ EXPORT_API(void) THSTensor_index_put_(Tensor tensor,
683683
const int64_t* indexSteps,
684684
const Tensor* indexTensors,
685685
const int indicesLength,
686-
const Tensor value);
686+
const Tensor value,
687+
const bool accumulate = false);
687688

688689
EXPORT_API(Tensor) THSTensor_index_select(Tensor tensor, int64_t dim, Tensor index);
689690

src/TorchSharp/LinearAlgebra.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public static Tensor multi_dot(IList<Tensor> tensors)
440440
throw new ArgumentException(nameof(tensors));
441441
}
442442
if (tensors.Count == 1) {
443-
return tensors[0];
443+
return tensors[0].alias();
444444
}
445445

446446
using (var parray = new PinnedArray<IntPtr>()) {

src/TorchSharp/NN/Vision.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum InterpolationMode
2323
Bilinear = 2,
2424
Bicubic = 3,
2525
Trilinear = 4,
26-
Area = 5
26+
Area = 5,
27+
NearestExact = 6
2728
}
2829

2930
public enum GridSampleMode
@@ -194,7 +195,7 @@ public static Tensor affine_grid(Tensor theta, long[]? size = null, bool align_c
194195
/// <param name="x">The input tensor</param>
195196
/// <param name="size">Output spatial size</param>
196197
/// <param name="scale_factor">Multiplier for spatial size. Has to match input size if it is a tuple.</param>
197-
/// <param name="mode">The algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' | 'bicubic' | 'trilinear' | 'area'</param>
198+
/// <param name="mode">The algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' | 'bicubic' | 'trilinear' | 'area' | 'nearest-exact'</param>
198199
/// <param name="align_corners">Geometrically, we consider the pixels of the input and output as squares rather than points.
199200
/// If set to true, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels.
200201
/// If set to false, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size when scale_factor is kept the same.</param>
@@ -205,14 +206,19 @@ public static Tensor affine_grid(Tensor theta, long[]? size = null, bool align_c
205206
/// Otherwise, a new scale_factor will be computed based on the output and input sizes for use in the interpolation computation
206207
/// (i.e. the computation will be identical to if the computed output_size were passed-in explicitly).
207208
/// </param>
209+
/// <param name="antialias">
210+
/// Flag to apply anti-aliasing. Using anti-alias
211+
/// option together with align_corners = false, interpolation result would match Pillow
212+
/// result for downsampling operation. Supported modes: 'bilinear', 'bicubic'.
213+
/// </param>
208214
/// <returns></returns>
209-
public static Tensor interpolate(Tensor x, long[]? size = null, double[]? scale_factor = null, InterpolationMode mode = InterpolationMode.Nearest, bool? align_corners = null, bool recompute_scale_factor = false)
215+
public static Tensor interpolate(Tensor x, long[]? size = null, double[]? scale_factor = null, InterpolationMode mode = InterpolationMode.Nearest, bool? align_corners = null, bool recompute_scale_factor = false, bool antialias = false)
210216
{
211217
unsafe {
212218
fixed (long* psize = size) {
213219
fixed (double* pSF = scale_factor) {
214220
byte ac = (byte)((align_corners.HasValue) ? (align_corners.Value ? 1 : 2) : 0);
215-
var res = THSNN_interpolate(x.Handle, (IntPtr)psize, size is null ? 0 : size.Length, (IntPtr)pSF, scale_factor is null ? 0 : scale_factor.Length, (byte)mode, ac, recompute_scale_factor);
221+
var res = THSNN_interpolate(x.Handle, (IntPtr)psize, size is null ? 0 : size.Length, (IntPtr)pSF, scale_factor is null ? 0 : scale_factor.Length, (byte)mode, ac, recompute_scale_factor, antialias);
216222
if (res == IntPtr.Zero) { torch.CheckForErrors(); }
217223
return new Tensor(res);
218224
}

src/TorchSharp/PInvoke/LibTorchSharp.THSNN.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal static extern IntPtr THSNN_custom_module(
4444

4545
[DllImport("LibTorchSharp")]
4646
// align_corners -- 0=None, 1=true, 2=false
47-
internal static extern IntPtr THSNN_interpolate(IntPtr input, IntPtr size, int size_len, IntPtr scale_factor, int scale_factor_len, byte mode, byte align_corners, [MarshalAs(UnmanagedType.U1)] bool recompute_scale_factor);
47+
internal static extern IntPtr THSNN_interpolate(IntPtr input, IntPtr size, int size_len, IntPtr scale_factor, int scale_factor_len, byte mode, byte align_corners, [MarshalAs(UnmanagedType.U1)] bool recompute_scale_factor, [MarshalAs(UnmanagedType.U1)] bool antialias);
4848

4949
[DllImport("LibTorchSharp")]
5050
// align_corners -- 0=None, 1=true, 2=false

src/TorchSharp/PInvoke/LibTorchSharp.THSTensor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ internal static extern IntPtr THSTensor_upsample_nearest3d(IntPtr input,
410410
internal static extern void THSTensor_index_put_scalar_(IntPtr tensor, IntPtr indexStarts, IntPtr indexEnds, IntPtr indexSteps, IntPtr indexTensors, int indicesLength, IntPtr value);
411411

412412
[DllImport("LibTorchSharp")]
413-
internal static extern void THSTensor_index_put_(IntPtr tensor, IntPtr indexStarts, IntPtr indexEnds, IntPtr indexSteps, IntPtr indexTensors, int indicesLength, IntPtr value);
413+
internal static extern void THSTensor_index_put_(IntPtr tensor, IntPtr indexStarts, IntPtr indexEnds, IntPtr indexSteps, IntPtr indexTensors, int indicesLength, IntPtr value, [MarshalAs(UnmanagedType.U1)] bool accumulate);
414414

415415
[DllImport("LibTorchSharp")]
416416
internal static extern IntPtr THSTensor_get1(IntPtr handle, long i1);

0 commit comments

Comments
 (0)