Skip to content

Commit 9301dda

Browse files
author
Глеб Брыкин
authored
Add files via upload
1 parent c40e177 commit 9301dda

File tree

10 files changed

+250
-7
lines changed

10 files changed

+250
-7
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//***************************************************************************************************
2+
//* (C) ColorfulSoft corp., 2019 - 2021. All rights reserved.
3+
//* The code is available under the Apache-2.0 license. Read the License for details.
4+
//***************************************************************************************************
5+
6+
namespace System
7+
{
8+
9+
namespace AI
10+
{
11+
12+
public static partial class torch
13+
{
14+
15+
public static partial class nn
16+
{
17+
18+
public sealed class AvgPool2d : Module
19+
{
20+
21+
public Union<int, Tuple<int, int>> kernel_size;
22+
23+
public Union<int, Tuple<int, int>> stride;
24+
25+
public Union<int, Tuple<int, int>> padding;
26+
27+
public Union<int, Tuple<int, int>> dilation;
28+
29+
public AvgPool2d(Union<int, Tuple<int, int>> kernel_size, Union<int, Tuple<int, int>> stride = null, Union<int, Tuple<int, int>> padding = null, Union<int, Tuple<int, int>> dilation = null)
30+
{
31+
this.kernel_size = kernel_size;
32+
if(stride == null)
33+
{
34+
this.stride = 1;
35+
}
36+
else
37+
{
38+
this.stride = stride;
39+
}
40+
if(padding == null)
41+
{
42+
this.padding = 0;
43+
}
44+
else
45+
{
46+
this.padding = padding;
47+
}
48+
if(dilation == null)
49+
{
50+
this.dilation = 1;
51+
}
52+
else
53+
{
54+
this.dilation = dilation;
55+
}
56+
}
57+
58+
public override Tensor forward(Tensor x)
59+
{
60+
return functional.avg_pool2d(x, this.kernel_size, this.stride, this.padding, this.dilation);
61+
}
62+
63+
}
64+
65+
}
66+
67+
}
68+
69+
}
70+
71+
}

Implementation/src/torch/nn/Conv2d.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public Conv2d(int in_channels, int out_channels, Union<int, Tuple<int, int>> ker
124124
}
125125
}
126126

127-
public Tensor forward(Tensor x)
127+
public override Tensor forward(Tensor x)
128128
{
129129
return functional.conv2d(x, this.weight, this.bias, this.stride, this.padding, this.dilation, this.groups);
130130
}

Implementation/src/torch/nn/Dropout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Dropout(double p = 0.5)
2525
this.p = p;
2626
}
2727

28-
public Tensor forward(Tensor x)
28+
public override Tensor forward(Tensor x)
2929
{
3030
return functional.dropout(x, this.p);
3131
}

Implementation/src/torch/nn/MaxPool2d.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MaxPool2d(Union<int, Tuple<int, int>> kernel_size, Union<int, Tuple<int,
5555
}
5656
}
5757

58-
public Tensor forward(Tensor x)
58+
public override Tensor forward(Tensor x)
5959
{
6060
return functional.max_pool2d(x, this.kernel_size, this.stride, this.padding, this.dilation);
6161
}

Implementation/src/torch/nn/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void load_state_dict(Dictionary<string, Tensor> state_dict)
217217
}
218218
}
219219

220-
public Tensor forward(Tensor x)
220+
public virtual Tensor forward(Tensor x)
221221
{
222222
throw new NotImplementedException("Forward pass through the abstract module is not implemented.");
223223
}

Implementation/src/torch/nn/ReLU.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ReLU()
2828
{
2929
}
3030

31-
public Tensor forward(Tensor x)
31+
public override Tensor forward(Tensor x)
3232
{
3333
return functional.relu(x);
3434
}

Implementation/src/torch/nn/Sequential.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public Sequential(params Module[] args)
4040
}
4141
}
4242

43+
public Module this[int i]
44+
{
45+
get
46+
{
47+
return this.__modules[i];
48+
}
49+
set
50+
{
51+
this.__modules[i] = value;
52+
}
53+
}
54+
4355
private string __format_print(string s, int tab)
4456
{
4557
s += "Sequential(";

Implementation/src/torch/nn/Sigmoid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Sigmoid()
2828
{
2929
}
3030

31-
public Tensor forward(Tensor x)
31+
public override Tensor forward(Tensor x)
3232
{
3333
return functional.sigmoid(x);
3434
}

Implementation/src/torch/nn/Tanh.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Tanh()
2828
{
2929
}
3030

31-
public Tensor forward(Tensor x)
31+
public override Tensor forward(Tensor x)
3232
{
3333
return functional.tanh(x);
3434
}

Implementation/src/torch/nn/functional.cs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,166 @@ public static partial class nn
2020
public static class functional
2121
{
2222

23+
public static Tensor avg_pool2d(Tensor x, Union<int, Tuple<int, int>> kernel_size, Union<int, Tuple<int, int>> stride = null, Union<int, Tuple<int, int>> padding = null, Union<int, Tuple<int, int>> dilation = null)
24+
{
25+
if(x.dtype == torch.@bool)
26+
{
27+
throw new TorchException("TorchException: nn.functional.avg_pool2d is not implemented for bool tensors.");
28+
}
29+
if(x.__shape.Length != 4)
30+
{
31+
throw new TorchException("TorchException: nn.functional.avg_pool2d requires 4D input, but " + x.__shape.Length.ToString() + "D given.");
32+
}
33+
Tuple<int, int> kernel_size__;
34+
if(kernel_size == null)
35+
{
36+
throw new TorchException("TorchException: kernel_size is not an optional parameter.");
37+
}
38+
else
39+
{
40+
if((Tuple<int, int>)kernel_size != null)
41+
{
42+
kernel_size__ = (Tuple<int, int>)kernel_size;
43+
}
44+
else
45+
{
46+
kernel_size__ = new Tuple<int, int>((int)kernel_size, (int)kernel_size);
47+
}
48+
}
49+
Tuple<int, int> stride__;
50+
if(stride == null)
51+
{
52+
stride__ = kernel_size__;
53+
}
54+
else
55+
{
56+
if((Tuple<int, int>)stride != null)
57+
{
58+
stride__ = (Tuple<int, int>)stride;
59+
}
60+
else
61+
{
62+
stride__ = new Tuple<int, int>((int)stride, (int)stride);
63+
}
64+
}
65+
Tuple<int, int> padding__;
66+
if(padding == null)
67+
{
68+
padding__ = new Tuple<int, int>(0, 0);
69+
}
70+
else
71+
{
72+
if((Tuple<int, int>)padding != null)
73+
{
74+
padding__ = (Tuple<int, int>)padding;
75+
}
76+
else
77+
{
78+
padding__ = new Tuple<int, int>((int)padding, (int)padding);
79+
}
80+
}
81+
Tuple<int, int> dilation__;
82+
if(dilation == null)
83+
{
84+
dilation__ = new Tuple<int, int>(1, 1);
85+
}
86+
else
87+
{
88+
if((Tuple<int, int>)dilation != null)
89+
{
90+
dilation__ = (Tuple<int, int>)dilation;
91+
}
92+
else
93+
{
94+
dilation__ = new Tuple<int, int>((int)dilation, (int)dilation);
95+
}
96+
}
97+
if((kernel_size__.Item1 < 1) || (kernel_size__.Item2 < 1))
98+
{
99+
throw new TorchException("TorchException: kernel size should be >= 1.");
100+
}
101+
if((kernel_size__.Item1 == 1) || (kernel_size__.Item2 == 1))
102+
{
103+
__Warnings.warn("AvgPool2d with kernel size = 1 is useless.");
104+
}
105+
if((stride__.Item1 < 1) || (stride__.Item2 < 1))
106+
{
107+
throw new TorchException("TorchException: stride should be >= 1.");
108+
}
109+
if((padding__.Item1 < 0) || (padding__.Item2 < 0))
110+
{
111+
throw new TorchException("TorchException: padding should be >= 0.");
112+
}
113+
if((dilation__.Item1 < 1) || (dilation__.Item2 < 1))
114+
{
115+
throw new TorchException("TorchException: dilation should be >= 1.");
116+
}
117+
var y = new Tensor(x.__shape[0], x.__shape[1],
118+
(x.__shape[2] + 2 * padding__.Item1 - dilation__.Item1 * (kernel_size__.Item1 - 1) - 1) / stride__.Item1 + 1,
119+
(x.__shape[3] + 2 * padding__.Item2 - dilation__.Item2 * (kernel_size__.Item2 - 1) - 1) / stride__.Item2 + 1,
120+
dtype: x.dtype, requires_grad: (!torch.autograd.grad_mode.no_grad.prev) && x.requires_grad);
121+
switch(x.dtype)
122+
{
123+
case torch.float16:
124+
{
125+
MKL.AvgPool2d(x.__half, x.__shape, kernel_size__, stride__, padding__, dilation__, y.__half, y.__shape);
126+
if(y.requires_grad)
127+
{
128+
y.__backward_fn = () =>
129+
{
130+
MKL.dAvgPool2d(x.grad.__half, x.grad.__shape, kernel_size__, stride__, padding__, dilation__, y.grad.__half, y.__shape);
131+
if(x.__backward_fn != null)
132+
{
133+
x.__backward_fn();
134+
}
135+
};
136+
}
137+
break;
138+
}
139+
case torch.float32:
140+
{
141+
MKL.AvgPool2d(x.__float, x.__shape, kernel_size__, stride__, padding__, dilation__, y.__float, y.__shape);
142+
if(y.requires_grad)
143+
{
144+
y.__backward_fn = () =>
145+
{
146+
MKL.dAvgPool2d(x.grad.__float, x.grad.__shape, kernel_size__, stride__, padding__, dilation__, y.grad.__float, y.__shape);
147+
if(x.__backward_fn != null)
148+
{
149+
x.__backward_fn();
150+
}
151+
};
152+
}
153+
break;
154+
}
155+
case torch.float64:
156+
{
157+
MKL.AvgPool2d(x.__double, x.__shape, kernel_size__, stride__, padding__, dilation__, y.__double, y.__shape);
158+
if(y.requires_grad)
159+
{
160+
y.__backward_fn = () =>
161+
{
162+
MKL.dAvgPool2d(x.grad.__double, x.grad.__shape, kernel_size__, stride__, padding__, dilation__, y.grad.__double, y.__shape);
163+
if(x.__backward_fn != null)
164+
{
165+
x.__backward_fn();
166+
}
167+
};
168+
}
169+
break;
170+
}
171+
case torch.int8:
172+
case torch.uint8:
173+
case torch.int16:
174+
case torch.int32:
175+
case torch.int64:
176+
{
177+
throw new TorchException("TorchException: nn.functional.avg_pool2d is not implemented for integer tensors.");
178+
}
179+
}
180+
return y;
181+
}
182+
23183
public static Tuple<Tensor, Tensor> max_pool2d_with_indices(Tensor x, Union<int, Tuple<int, int>> kernel_size, Union<int, Tuple<int, int>> stride = null, Union<int, Tuple<int, int>> padding = null, Union<int, Tuple<int, int>> dilation = null)
24184
{
25185
if(x.dtype == torch.@bool)

0 commit comments

Comments
 (0)