-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fdy/support hf lora amp #308
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
53b0aa4
add mem_get_info
fandaoyi 25d5df4
fix extra device stub err
fandaoyi 06a5a9d
dipu device as cuda
fandaoyi ebb3173
Merge branch 'main' into fdy/support_hf_lora_amp
fandaoyi 26e2e9b
fix mem_get_info flaw
fandaoyi 8207acc
fix storage ci err
fandaoyi 96ddd0c
unset cudnn
fandaoyi 3b7a27f
fix storage construct err
fandaoyi 2402b77
fix storage type err
fandaoyi 0a76c83
fix linejie comment
fandaoyi b75ef66
fix camb ci
fandaoyi 88190db
fix ci device err
fandaoyi c8f912c
fix lingjie comments 2
fandaoyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2023, DeepLink. | ||
|
||
#include <torch/csrc/Export.h> | ||
#include <torch/csrc/python_headers.h> | ||
|
||
#include <torch/csrc/Exceptions.h> | ||
#include <torch/csrc/utils/object_ptr.h> | ||
#include <torch/csrc/utils/pybind.h> | ||
#include <torch/csrc/utils/python_arg_parser.h> | ||
#include <torch/csrc/utils/python_numbers.h> | ||
#include <torch/csrc/utils/python_strings.h> | ||
|
||
#include <ATen/Device.h> | ||
#include <c10/util/Exception.h> | ||
#include <torch/csrc/Device.h> | ||
|
||
#include <structmember.h> | ||
#include <cstring> | ||
#include <limits> | ||
#include <sstream> | ||
|
||
#include "exportapi.h" | ||
|
||
namespace dipu { | ||
|
||
static bool PythonDeviceAsCuda = false; | ||
|
||
static at::DeviceType _get_dipu_python_type(const at::Device& device) { | ||
if (device.type() == DIPU_DEVICE_TYPE && PythonDeviceAsCuda) { | ||
return at::DeviceType::CUDA; | ||
} | ||
return device.type(); | ||
} | ||
|
||
PyObject* _THPDevice_type(THPDevice* self, PyObject* noargs) { | ||
HANDLE_TH_ERRORS | ||
std::ostringstream oss; | ||
oss << _get_dipu_python_type(self->device); | ||
return THPUtils_packString(oss.str().c_str()); | ||
Py_RETURN_NONE; | ||
END_HANDLE_TH_ERRORS | ||
} | ||
|
||
PyObject* _THPDevice_index(THPDevice* self, PyObject* noargs) { | ||
HANDLE_TH_ERRORS | ||
if (self->device.has_index()) { | ||
return THPUtils_packInt64(self->device.index()); | ||
} else { | ||
Py_RETURN_NONE; | ||
} | ||
END_HANDLE_TH_ERRORS | ||
} | ||
|
||
PyObject* DIPU_THPDevice_repr(THPDevice* self) { | ||
std::ostringstream oss; | ||
oss << "device(type=\'" << _get_dipu_python_type(self->device) << "\'"; | ||
if (self->device.has_index()) { | ||
// `self->device.index()` returns uint8_t which is treated as ascii while | ||
// printing, hence casting it to uint16_t. | ||
// https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout | ||
oss << ", index=" << static_cast<uint16_t>(self->device.index()); | ||
} | ||
oss << ")"; | ||
return THPUtils_packString(oss.str().c_str()); | ||
} | ||
|
||
|
||
PyObject* DIPU_THPDevice_str(THPDevice* self) { | ||
std::ostringstream oss; | ||
oss << _get_dipu_python_type(self->device); | ||
return THPUtils_packString(oss.str().c_str()); | ||
} | ||
|
||
static struct PyGetSetDef DIPU_THPDevice_properties[] = { | ||
{"type", (getter)_THPDevice_type, nullptr, nullptr, nullptr}, | ||
{"index", (getter)_THPDevice_index, nullptr, nullptr, nullptr}, | ||
{nullptr}}; | ||
|
||
|
||
/* | ||
why use this method to patch csrc.Device: because | ||
1. csrc.Device is a final cpython class which not support attributes mock in python layer. | ||
2. rewrite a new DeviceType to replace THPDeviceType is not work because torch::PythonArgParser | ||
will check the type of THPDeviceType when parse Device parameter(see csrc/utils/python_arg_parer.cpp | ||
FunctionParameter::check() -> THPDevice_Check()) | ||
so we replace some attributes of THPDeviceType class in c-python layer | ||
*/ | ||
void patchTorchCsrcDevice(PyObject* module) { | ||
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dict | ||
THPDeviceType.tp_dict = nullptr; | ||
// change Type properties | ||
THPDeviceType.tp_getset = DIPU_THPDevice_properties; | ||
THPDeviceType.tp_repr = (reprfunc)DIPU_THPDevice_repr; | ||
THPDeviceType.tp_str = (reprfunc)DIPU_THPDevice_str; | ||
|
||
// change THPDeviceType as an overriable class need add some other prperties in PyTypeObject, | ||
// It may cause problems and seem un-necessary, so we keep the THPDeviceType as immutable. | ||
THPDeviceType.tp_flags = Py_TPFLAGS_DEFAULT; // | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; | ||
|
||
if (PyType_Ready(&THPDeviceType) < 0) { | ||
throw python_error(); | ||
} | ||
Py_INCREF(&THPDeviceType); | ||
|
||
auto m = py::handle(module).cast<py::module>(); | ||
|
||
m.def("_get_python_device_as_cuda", []() -> bool { | ||
return PythonDeviceAsCuda; | ||
}); | ||
|
||
m.def ("_set_python_device_as_cuda", [](bool as_cuda) -> void { | ||
PythonDeviceAsCuda = as_cuda; | ||
}); | ||
|
||
// not really 'export' new type but change original THPDeviceType is enough | ||
// if (PyModule_AddObject(module, "device", (PyObject*)&THPDeviceType) != 0) { | ||
// throw python_error(); | ||
// } | ||
} | ||
} // namespace dipu |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该不能用 const, 后面要赋值给 tp_getset, 那个不是 const的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这居然是个数组- -看起来更可怕了...
好像也没啥好改法,要不然加个注释说这玩意儿传给 pytorch 随他用去了