Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
decentralizepy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Milos Vujasinovic
decentralizepy
Commits
65f1b43a
Commit
65f1b43a
authored
2 years ago
by
Milos Vujasinovic
Browse files
Options
Downloads
Patches
Plain Diff
Added support for native seed to random states
parent
1bac78e3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/decentralizepy/random.py
+21
-7
21 additions, 7 deletions
src/decentralizepy/random.py
with
21 additions
and
7 deletions
src/decentralizepy/random.py
+
21
−
7
View file @
65f1b43a
import
random
import
contextlib
import
torch
import
numpy
as
np
...
...
@@ -14,13 +16,16 @@ def temp_seed(seed):
on CPU regardless if CUDA is used for other things.
"""
random_state
=
random
.
getstate
()
np_old_state
=
np
.
random
.
get_state
()
torch_old_state
=
torch
.
random
.
get_rng_state
()
torch
.
random
.
manual_
seed
(
seed
)
random
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
torch
.
random
.
manual_seed
(
seed
)
try
:
yield
finally
:
random
.
setstate
(
random_state
)
np
.
random
.
set_state
(
np_old_state
)
torch
.
random
.
set_rng_state
(
torch_old_state
)
...
...
@@ -33,8 +38,17 @@ class RandomState:
"""
def
__init__
(
self
,
seed
):
with
temp_seed
(
seed
):
self
.
__np_state
=
np
.
random
.
get_state
()
self
.
__torch_state
=
torch
.
random
.
get_rng_state
()
self
.
__refresh_states
()
def
__refresh_states
(
self
):
self
.
__random_state
=
random
.
getstate
()
self
.
__np_state
=
np
.
random
.
get_state
()
self
.
__torch_state
=
torch
.
random
.
get_rng_state
()
def
__set_states
(
self
):
random
.
setstate
(
self
.
__random_state
)
np
.
random
.
set_state
(
self
.
__np_state
)
torch
.
random
.
set_rng_state
(
self
.
__torch_state
)
@contextlib.contextmanager
def
activate
(
self
):
...
...
@@ -44,14 +58,14 @@ class RandomState:
is finished
"""
random_state
=
random
.
getstate
()
np_old_state
=
np
.
random
.
get_state
()
torch_old_state
=
torch
.
random
.
get_rng_state
()
np
.
random
.
set_state
(
self
.
__np_state
)
torch
.
random
.
set_rng_state
(
self
.
__torch_state
)
self
.
__set_states
()
try
:
yield
finally
:
self
.
__
np_state
=
np
.
random
.
get
_state
()
self
.
__torch_state
=
torch
.
random
.
get_rng
_state
(
)
self
.
__
refresh
_state
s
()
random
.
setstate
(
random
_state
)
np
.
random
.
set_state
(
np_old_state
)
torch
.
random
.
set_rng_state
(
torch_old_state
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment