Make random_ exclusive and make generator kwarg only in all random
functions
diff --git a/tools/cwrap/plugins/KwargsPlugin.py b/tools/cwrap/plugins/KwargsPlugin.py
index 79995c3..9542f5c 100644
--- a/tools/cwrap/plugins/KwargsPlugin.py
+++ b/tools/cwrap/plugins/KwargsPlugin.py
@@ -24,6 +24,16 @@
for option in declaration['options']:
for arg in option['arguments']:
arg['no_kwargs'] = True
+ # we need to use offsets for arg position in *arg if kwarg_only args
+ # are not at the end
+ for declaration in declarations:
+ for option in declaration['options']:
+ offset = 0
+ for arg in option['arguments']:
+ if arg.get('kwarg_only') and not arg.get('ignore_check', False):
+ offset += 1
+ else:
+ arg['kwarg_offset'] = offset
return declarations
def get_arg_accessor(self, arg, option):
@@ -31,14 +41,14 @@
return
if arg.get('kwarg_only'):
return self.KWARG_ONLY_ACCESSOR_TEMPLATE.substitute(name=arg['name'])
- return self.ACCESSOR_TEMPLATE.substitute(idx=arg['idx'], name=arg['name'])
+ return self.ACCESSOR_TEMPLATE.substitute(idx=arg['idx'] - arg['kwarg_offset'], name=arg['name'])
def process_single_check(self, code, arg, arg_accessor):
if arg.get('no_kwargs'):
return code
if arg.get('kwarg_only'):
return self.KWARG_ONLY_CHECK_TEMPLATE.substitute(name=arg['name'], code=code)
- return self.CHECK_TEMPLATE.substitute(idx=arg['idx'], name=arg['name'], code=code)
+ return self.CHECK_TEMPLATE.substitute(idx=arg['idx'] - arg['kwarg_offset'], name=arg['name'], code=code)
def process_wrapper(self, code, declaration):
if declaration.get('no_kwargs'):
diff --git a/torch/_tensor_docs.py b/torch/_tensor_docs.py
index 691363c..cf5282c 100644
--- a/torch/_tensor_docs.py
+++ b/torch/_tensor_docs.py
@@ -221,7 +221,7 @@
add_docstr(torch._C.FloatTensorBase.cauchy_,
"""
-cauchy_(generator=None, median=0, sigma=1) -> Tensor
+cauchy_(median=0, sigma=1, *, generator=None) -> Tensor
Fills the tensor with numbers drawn from the Cauchy distribution:
@@ -445,7 +445,7 @@
add_docstr(torch._C.FloatTensorBase.exponential_,
"""
-exponential_(generator=None, lambd=1) -> Tensor
+exponential_(lambd=1, *, generator=None) -> Tensor
Fills this tensor with elements drawn from the exponential distribution:
@@ -533,7 +533,7 @@
add_docstr(torch._C.FloatTensorBase.geometric_,
"""
-geometric_(generator=None, p) -> Tensor
+geometric_(p, *, generator=None) -> Tensor
Fills this tensor with elements drawn from the geometric distribution:
@@ -761,7 +761,7 @@
""")
add_docstr(torch._C.FloatTensorBase.log_normal_, u"""
-log_normal_(generator=None, mean=1, std=2)
+log_normal_(mean=1, std=2, *, generator=None)
Fills this tensor with numbers samples from the log-normal distribution
parameterized by the given mean (\u00B5) and standard deviation (\u03C3). Note that
@@ -895,7 +895,7 @@
add_docstr(torch._C.FloatTensorBase.multinomial,
"""
-multinomial(generator=None, num_samples, replacement=False)
+multinomial(num_samples, replacement=False, *, generator=None)
See :func:`torch.multinomial`
""")
@@ -991,7 +991,7 @@
add_docstr(torch._C.FloatTensorBase.normal_,
"""
-normal_(generator=None, mean=0, std=1)
+normal_(mean=0, std=1, *, generator=None)
Fills this tensor with elements samples from the normal distribution
parameterized by :attr:`mean` and :attr:`std`.
@@ -1085,11 +1085,11 @@
add_docstr(torch._C.FloatTensorBase.random_,
"""
-random_(generator=None, from=0, to=None)
+random_(from=0, to=None, *, generator=None)
Fills this tensor with numbers sampled from the uniform distribution or
-discrete uniform distribution over [from, to]. If not specified, :attr:`to`
-defaults to the largest value representable by this tensor's data type.
+discrete uniform distribution over [from, to - 1]. If not specified, the
+values are only bounded by this tensor's data type.
""")
add_docstr(torch._C.FloatTensorBase.reciprocal,
diff --git a/torch/csrc/generic/methods/TensorRandom.cwrap b/torch/csrc/generic/methods/TensorRandom.cwrap
index a18491f..b444940 100644
--- a/torch/csrc/generic/methods/TensorRandom.cwrap
+++ b/torch/csrc/generic/methods/TensorRandom.cwrap
@@ -8,19 +8,20 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- long n
]]
#if !defined(TH_REAL_IS_HALF) && !IS_CUDA
static void THTensor_(random2__)(THTensor *self, THGenerator *gen, long a, long b)
{
- THArgCheck(b >= a, 2, "upper bound must be greater or equal than lower bound");
+ THArgCheck(b > a, 2, "upper bound must be greater than lower bound");
TH_TENSOR_APPLY(real, self, *self_data = ((THRandom_random(gen) % (b+1-a)) + a);)
}
static void THTensor_(random1__)(THTensor *self, THGenerator *gen, long b)
{
- THArgCheck(b >= 0, 1, "upper bound must be positive");
+ THArgCheck(b > 0, 1, "upper bound must be positive");
TH_TENSOR_APPLY(real, self, *self_data = (THRandom_random(gen) % b);)
}
#endif
@@ -35,17 +36,20 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- cname: random1__
arguments:
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- long to
- cname: random2__
arguments:
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- long from
- long to
]]
@@ -60,6 +64,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THTensor* self
- long num_samples
- arg: bool replacement
@@ -75,6 +80,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real from
default: 0
- arg: real to
@@ -118,6 +124,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THTensor* means
- arg: real std
default: 1
@@ -127,6 +134,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real mean
default: 0
- THTensor* std
@@ -136,6 +144,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THTensor* means
- THTensor* std
]]
@@ -149,6 +158,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real mean
default: 0
- arg: real std
@@ -164,6 +174,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real median
default: 0
- arg: real sigma
@@ -180,6 +191,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real mean
default: 1
- arg: real std
@@ -195,6 +207,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: real lambd
default: 1
]]
@@ -209,6 +222,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: THSize* size
long_args: True
]]
@@ -223,6 +237,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: THSize* size
long_args: True
]]
@@ -399,6 +414,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- double p
]]
@@ -418,6 +434,7 @@
output: True
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THTensor* self
]]
@@ -434,6 +451,7 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- arg: double p
default: 0.5
- cname: bernoulli_FloatTensor
@@ -441,12 +459,14 @@
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THFloatTensor* float_p
- cname: bernoulli_DoubleTensor
arguments:
- THTensor* self
- arg: THGenerator* generator
default: THPDefaultGenerator->cdata
+ kwarg_only: True
- THDoubleTensor* float_p
]]