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
1bac78e3
Commit
1bac78e3
authored
2 years ago
by
Milos Vujasinovic
Browse files
Options
Downloads
Patches
Plain Diff
Added utilities for handling random states
parent
0c1ab13e
No related branches found
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
+57
-0
57 additions, 0 deletions
src/decentralizepy/random.py
with
57 additions
and
0 deletions
src/decentralizepy/random.py
0 → 100644
+
57
−
0
View file @
1bac78e3
import
contextlib
import
torch
import
numpy
as
np
@contextlib.contextmanager
def
temp_seed
(
seed
):
"""
Creates a context with seeds set to given value. Returns to the
previous seed afterwards.
Note: Based on torch implementation there might be issues with CUDA
causing troubles with the correctness of this function. Function
torch.rand() work fine from testing as their results are generated
on CPU regardless if CUDA is used for other things.
"""
np_old_state
=
np
.
random
.
get_state
()
torch_old_state
=
torch
.
random
.
get_rng_state
()
torch
.
random
.
manual_seed
(
seed
)
np
.
random
.
seed
(
seed
)
try
:
yield
finally
:
np
.
random
.
set_state
(
np_old_state
)
torch
.
random
.
set_rng_state
(
torch_old_state
)
class
RandomState
:
"""
Creates a state that affects random number generation on
torch and numpy and whose context can be activated at will
"""
def
__init__
(
self
,
seed
):
with
temp_seed
(
seed
):
self
.
__np_state
=
np
.
random
.
get_state
()
self
.
__torch_state
=
torch
.
random
.
get_rng_state
()
@contextlib.contextmanager
def
activate
(
self
):
"""
Activates this state in the given context for torch and
numpy. The previous state is restored when the context
is finished
"""
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
)
try
:
yield
finally
:
self
.
__np_state
=
np
.
random
.
get_state
()
self
.
__torch_state
=
torch
.
random
.
get_rng_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