| 338 | static constexpr auto* SbpBToP = DECORATE(&RawSbpBToP, ThreadLocalCached); |
| 339 | |
| 340 | static PyObject* PyTensorObject_zero_grad(PyObject* self, PyObject* args, PyObject* kwargs) { |
| 341 | HANDLE_ERRORS |
| 342 | int set_to_none = 0; |
| 343 | static const char* keywords[2] = {"set_to_none", NULL}; |
| 344 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|p:_zero_grad_", const_cast<char**>(keywords), |
| 345 | &set_to_none)) { |
| 346 | return NULL; |
| 347 | } |
| 348 | const auto& t = PyTensor_Unpack(self); |
| 349 | const auto acc_grad = ASSERT_PTR(t->acc_grad()); |
| 350 | if (acc_grad) { |
| 351 | if (set_to_none) { |
| 352 | ASSERT(t->set_acc_grad(NULL)); |
| 353 | } else { |
| 354 | ASSERT(EagerLocalTensorZeros(acc_grad)); |
| 355 | if (acc_grad->is_global() && acc_grad->is_eager()) { |
| 356 | const auto local_tensor = ASSERT_PTR(functional::GlobalToLocal(acc_grad, false)); |
| 357 | const auto p = ASSERT_PTR(functional::LocalToGlobal( |
| 358 | local_tensor, ASSERT(acc_grad->parallel_desc()), SbpBToP(ASSERT(acc_grad->nd_sbp())), |
| 359 | *acc_grad->shape(), acc_grad->dtype(), false, false)); |
| 360 | ASSERT(acc_grad->set_data(p)); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | Py_XINCREF(self); |
| 365 | return self; |
| 366 | END_HANDLE_ERRORS |
| 367 | } |
| 368 | |
| 369 | static PyObject* PyTensorObject_register_hook(PyObject* self, PyObject* hook) { |
| 370 | HANDLE_ERRORS |
nothing calls this directly
no test coverage detected