@@ -16,20 +16,77 @@ using namespace torch::autograd::utils;
1616
1717namespace torch { namespace autograd {
1818
19- static PyObject * THPVariable_detach (PyObject* self, PyObject* args)
19+ static Tensor dispatch_clamp (const Tensor & self, Scalar min, Scalar max) {
20+ AutoNoGIL no_gil;
21+ AutoGPU auto_gpu (self);
22+ return self.clamp (min, max);
23+ }
24+ static Tensor dispatch_clamp_min (const Tensor & self, Scalar min) {
25+ AutoNoGIL no_gil;
26+ AutoGPU auto_gpu (self);
27+ return self.clamp_min (min);
28+ }
29+ static Tensor dispatch_clamp_max (const Tensor & self, Scalar max) {
30+ AutoNoGIL no_gil;
31+ AutoGPU auto_gpu (self);
32+ return self.clamp_max (max);
33+ }
34+
35+ PyObject * THPVariable_clamp (PyObject* self, PyObject* args, PyObject* kwargs)
2036{
2137 HANDLE_TH_ERRORS
38+ static PythonArgParser parser ({
39+ " clamp(Scalar min=None, Scalar max=None)" ,
40+ });
2241 auto & self_ = reinterpret_cast <THPVariable*>(self)->cdata ;
23- return THPVariable_Wrap (self_.detach ());
42+ PyObject* parsed_args[2 ];
43+ auto r = parser.parse (args, kwargs, parsed_args);
44+ if (!r.isNone (0 ) && !r.isNone (1 )) {
45+ return THPVariable_Wrap (dispatch_clamp (self_, r.scalar (0 ), r.scalar (1 )));
46+ } else if (!r.isNone (0 )) {
47+ return THPVariable_Wrap (dispatch_clamp_min (self_, r.scalar (0 )));
48+ } else if (!r.isNone (1 )) {
49+ return THPVariable_Wrap (dispatch_clamp_max (self_, r.scalar (1 )));
50+ } else {
51+ throw std::runtime_error (" At least one of 'min' or 'max' must not be None" );
52+ }
2453 END_HANDLE_TH_ERRORS
2554}
2655
27- static PyObject * THPVariable_detach_ (PyObject* self, PyObject* args)
56+ static Tensor & dispatch_clamp_ (Tensor & self, Scalar min, Scalar max) {
57+ AutoNoGIL no_gil;
58+ AutoGPU auto_gpu (self);
59+ return self.clamp_ (min, max);
60+ }
61+ static Tensor & dispatch_clamp_min_ (Tensor & self, Scalar min) {
62+ AutoNoGIL no_gil;
63+ AutoGPU auto_gpu (self);
64+ return self.clamp_min_ (min);
65+ }
66+ static Tensor & dispatch_clamp_max_ (Tensor & self, Scalar max) {
67+ AutoNoGIL no_gil;
68+ AutoGPU auto_gpu (self);
69+ return self.clamp_max_ (max);
70+ }
71+
72+ PyObject * THPVariable_clamp_ (PyObject* self, PyObject* args, PyObject* kwargs)
2873{
2974 HANDLE_TH_ERRORS
30- reinterpret_cast <THPVariable*>(self)->cdata .detach_ ();
31- Py_INCREF (self);
32- return self;
75+ static PythonArgParser parser ({
76+ " clamp_(Scalar min=None, Scalar max=None)" ,
77+ });
78+ auto & self_ = reinterpret_cast <THPVariable*>(self)->cdata ;
79+ PyObject* parsed_args[2 ];
80+ auto r = parser.parse (args, kwargs, parsed_args);
81+ if (!r.isNone (0 ) && !r.isNone (1 )) {
82+ return THPVariable_Wrap (dispatch_clamp_ (self_, r.scalar (0 ), r.scalar (1 )));
83+ } else if (!r.isNone (0 )) {
84+ return THPVariable_Wrap (dispatch_clamp_min_ (self_, r.scalar (0 )));
85+ } else if (!r.isNone (1 )) {
86+ return THPVariable_Wrap (dispatch_clamp_max_ (self_, r.scalar (1 )));
87+ } else {
88+ throw std::runtime_error (" At least one of 'min' or 'max' must not be None" );
89+ }
3390 END_HANDLE_TH_ERRORS
3491}
3592
@@ -52,6 +109,23 @@ static PyObject * THPVariable_contiguous(PyObject* self, PyObject* args)
52109 END_HANDLE_TH_ERRORS
53110}
54111
112+ static PyObject * THPVariable_detach (PyObject* self, PyObject* args)
113+ {
114+ HANDLE_TH_ERRORS
115+ auto & self_ = reinterpret_cast <THPVariable*>(self)->cdata ;
116+ return THPVariable_Wrap (self_.detach ());
117+ END_HANDLE_TH_ERRORS
118+ }
119+
120+ static PyObject * THPVariable_detach_ (PyObject* self, PyObject* args)
121+ {
122+ HANDLE_TH_ERRORS
123+ reinterpret_cast <THPVariable*>(self)->cdata .detach_ ();
124+ Py_INCREF (self);
125+ return self;
126+ END_HANDLE_TH_ERRORS
127+ }
128+
55129static PyObject * THPVariable_element_size (PyObject* self, PyObject* args)
56130{
57131 HANDLE_TH_ERRORS
@@ -61,7 +135,6 @@ static PyObject * THPVariable_element_size(PyObject* self, PyObject* args)
61135 END_HANDLE_TH_ERRORS
62136}
63137
64-
65138// generated methods start here
66139
67140${py_methods}
@@ -79,6 +152,8 @@ PyMethodDef variable_methods[] = {
79152 {" __truediv__" , (PyCFunction)THPVariable_div, METH_VARARGS | METH_KEYWORDS, NULL },
80153 {" __idiv__" , (PyCFunction)THPVariable_div_, METH_VARARGS | METH_KEYWORDS, NULL },
81154 {" __mod__" , (PyCFunction)THPVariable_remainder, METH_VARARGS | METH_KEYWORDS, NULL },
155+ {" clamp" , (PyCFunction)THPVariable_clamp, METH_VARARGS | METH_KEYWORDS, NULL },
156+ {" clamp_" , (PyCFunction)THPVariable_clamp_, METH_VARARGS | METH_KEYWORDS, NULL },
82157 {" contiguous" , (PyCFunction)THPVariable_contiguous, METH_NOARGS, NULL },
83158 {" detach" , (PyCFunction)THPVariable_detach, METH_NOARGS, NULL },
84159 {" detach_" , (PyCFunction)THPVariable_detach_, METH_NOARGS, NULL },
0 commit comments