Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
evt_fugx1
dcu_megatron
Commits
5d997698
Commit
5d997698
authored
Mar 26, 2025
by
dongcl
Browse files
megatron patch
parent
950d42b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
32 deletions
+32
-32
dcu_megatron/adaptor/patch_utils.py
dcu_megatron/adaptor/patch_utils.py
+32
-32
No files found.
dcu_megatron/adaptor/patch_utils.py
View file @
5d997698
...
@@ -17,62 +17,62 @@ def dummy_function_wrapper(func_name):
...
@@ -17,62 +17,62 @@ def dummy_function_wrapper(func_name):
class
Patch
:
class
Patch
:
def
__init__
(
self
,
orig_func_name
,
new_func
,
create_dummy
,
apply_wrapper
=
False
):
def
__init__
(
self
,
orig_func_
or_cls_
name
,
new_func
_or_cls
,
create_dummy
,
apply_wrapper
=
False
):
split_name
=
orig_func_name
.
rsplit
(
'.'
,
1
)
split_name
=
orig_func_
or_cls_
name
.
rsplit
(
'.'
,
1
)
if
len
(
split_name
)
==
1
:
if
len
(
split_name
)
==
1
:
self
.
orig_module_name
,
self
.
orig_func_name
=
orig_func_name
,
None
self
.
orig_module_name
,
self
.
orig_func_
or_cls_
name
=
orig_func_
or_cls_
name
,
None
else
:
else
:
self
.
orig_module_name
,
self
.
orig_func_name
=
split_name
self
.
orig_module_name
,
self
.
orig_func_
or_cls_
name
=
split_name
self
.
orig_module
=
None
self
.
orig_module
=
None
self
.
orig_func
=
None
self
.
orig_func
_or_cls
=
None
self
.
patch_func
=
None
self
.
patch_func
_or_cls
=
None
self
.
wrappers
=
[]
self
.
wrappers
=
[]
if
new_func
is
None
:
if
new_func
_or_cls
is
None
:
new_func
=
dummy_function_wrapper
(
orig_func_name
)
new_func
_or_cls
=
dummy_function_wrapper
(
orig_func_
or_cls_
name
)
self
.
set_patch_func
(
new_func
,
apply_wrapper
=
apply_wrapper
)
self
.
set_patch_func
(
new_func
_or_cls
,
apply_wrapper
=
apply_wrapper
)
self
.
is_applied
=
False
self
.
is_applied
=
False
self
.
create_dummy
=
create_dummy
self
.
create_dummy
=
create_dummy
@
property
@
property
def
orig_func_id
(
self
):
def
orig_func_
or_cls_
id
(
self
):
return
id
(
self
.
orig_func
)
return
id
(
self
.
orig_func
_or_cls
)
@
property
@
property
def
patch_func_id
(
self
):
def
patch_func_id
(
self
):
return
id
(
self
.
patch_func
)
return
id
(
self
.
patch_func
_or_cls
)
def
set_patch_func
(
self
,
new_func
,
force_patch
=
False
,
apply_wrapper
=
False
):
def
set_patch_func
(
self
,
new_func
_or_cls
,
force_patch
=
False
,
apply_wrapper
=
False
):
if
(
if
(
apply_wrapper
apply_wrapper
or
(
hasattr
(
new_func
,
'__name__'
)
and
new_func
.
__name__
.
endswith
((
'wrapper'
,
'decorator'
)))
or
(
hasattr
(
new_func
_or_cls
,
'__name__'
)
and
new_func
_or_cls
.
__name__
.
endswith
((
'wrapper'
,
'decorator'
)))
):
):
self
.
wrappers
.
append
(
new_func
)
self
.
wrappers
.
append
(
new_func
_or_cls
)
else
:
else
:
if
self
.
patch_func
and
not
force_patch
:
if
self
.
patch_func
_or_cls
and
not
force_patch
:
raise
RuntimeError
(
'the patch of {} exist !'
.
format
(
self
.
orig_func_name
))
raise
RuntimeError
(
'the patch of {} exist !'
.
format
(
self
.
orig_func_
or_cls_
name
))
self
.
patch_func
=
new_func
self
.
patch_func
_or_cls
=
new_func
_or_cls
self
.
is_applied
=
False
self
.
is_applied
=
False
def
apply_patch
(
self
):
def
apply_patch
(
self
):
if
self
.
is_applied
:
if
self
.
is_applied
:
return
return
self
.
orig_module
,
self
.
orig_func
=
Patch
.
parse_path
(
self
.
orig_module_name
,
self
.
orig_func_name
,
self
.
create_dummy
)
self
.
orig_module
,
self
.
orig_func
_or_cls
=
Patch
.
parse_path
(
self
.
orig_module_name
,
self
.
orig_func_
or_cls_
name
,
self
.
create_dummy
)
final_patch_func
=
self
.
orig_func
final_patch_func
_or_cls
=
self
.
orig_func
_or_cls
if
self
.
patch_func
is
not
None
:
if
self
.
patch_func
_or_cls
is
not
None
:
final_patch_func
=
self
.
patch_func
final_patch_func
_or_cls
=
self
.
patch_func
_or_cls
for
wrapper
in
self
.
wrappers
:
for
wrapper
in
self
.
wrappers
:
final_patch_func
=
wrapper
(
final_patch_func
)
final_patch_func
_or_cls
=
wrapper
(
final_patch_func
_or_cls
)
if
self
.
orig_func_name
is
not
None
:
if
self
.
orig_func_
or_cls_
name
is
not
None
:
setattr
(
self
.
orig_module
,
self
.
orig_func_name
,
final_patch_func
)
setattr
(
self
.
orig_module
,
self
.
orig_func_
or_cls_
name
,
final_patch_func
_or_cls
)
for
key
,
value
in
sys
.
modules
.
copy
().
items
():
for
key
,
value
in
sys
.
modules
.
copy
().
items
():
if
self
.
orig_func_name
is
not
None
and
hasattr
(
value
,
self
.
orig_func_name
)
\
if
self
.
orig_func_
or_cls_
name
is
not
None
and
hasattr
(
value
,
self
.
orig_func_
or_cls_
name
)
\
and
id
(
getattr
(
value
,
self
.
orig_func_name
))
==
self
.
orig_func_id
:
and
id
(
getattr
(
value
,
self
.
orig_func_
or_cls_
name
))
==
self
.
orig_func_
or_cls_
id
:
setattr
(
value
,
self
.
orig_func_name
,
final_patch_func
)
setattr
(
value
,
self
.
orig_func_
or_cls_
name
,
final_patch_func
_or_cls
)
self
.
is_applied
=
True
self
.
is_applied
=
True
@
staticmethod
@
staticmethod
...
@@ -111,11 +111,11 @@ class MegatronPatchesManager:
...
@@ -111,11 +111,11 @@ class MegatronPatchesManager:
patches_info
=
{}
patches_info
=
{}
@
staticmethod
@
staticmethod
def
register_patch
(
orig_func_name
,
new_func
=
None
,
force_patch
=
False
,
create_dummy
=
False
):
def
register_patch
(
orig_func_
or_cls_
name
,
new_func
_or_cls
=
None
,
force_patch
=
False
,
create_dummy
=
False
):
if
orig_func_name
not
in
MegatronPatchesManager
.
patches_info
:
if
orig_func_
or_cls_
name
not
in
MegatronPatchesManager
.
patches_info
:
MegatronPatchesManager
.
patches_info
[
orig_func_name
]
=
Patch
(
orig_func_name
,
new_func
,
create_dummy
)
MegatronPatchesManager
.
patches_info
[
orig_func_
or_cls_
name
]
=
Patch
(
orig_func_
or_cls_
name
,
new_func
_or_cls
,
create_dummy
)
else
:
else
:
MegatronPatchesManager
.
patches_info
.
get
(
orig_func_name
).
set_patch_func
(
new_func
,
force_patch
)
MegatronPatchesManager
.
patches_info
.
get
(
orig_func_
or_cls_
name
).
set_patch_func
(
new_func
_or_cls
,
force_patch
)
@
staticmethod
@
staticmethod
def
apply_patches
():
def
apply_patches
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment