Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.pipelines
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
bob
bob.pipelines
Commits
622b2570
Commit
622b2570
authored
5 years ago
by
Tiago de Freitas Pereira
Browse files
Options
Downloads
Patches
Plain Diff
Finished resource tags
parent
0ce7bce1
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!5
Make scikit operations daskable
Pipeline
#38092
passed
5 years ago
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bob/pipelines/mixins.py
+81
-17
81 additions, 17 deletions
bob/pipelines/mixins.py
bob/pipelines/test/test_mixins.py
+7
-3
7 additions, 3 deletions
bob/pipelines/test/test_mixins.py
with
88 additions
and
20 deletions
bob/pipelines/mixins.py
+
81
−
17
View file @
622b2570
...
...
@@ -12,29 +12,93 @@ from sklearn.pipeline import Pipeline
from
dask
import
delayed
import
dask.bag
def
dask_it
(
o
):
def
dask_it
(
o
,
fit_tag
=
None
,
transform_tag
=
None
):
"""
Mix up any :py:class:`sklearn.pipeline.Pipeline` or :py:class:`sklearn.estimator.Base with
Mix up any :py:class:`sklearn.pipeline.Pipeline` or :py:class:`sklearn.estimator.Base
`
with
:py:class`DaskEstimatorMixin`
Parameters
----------
o: :py:class:`sklearn.pipeline.Pipeline` or :py:class:`sklearn.estimator.Base`
Any :py:class:`sklearn.pipeline.Pipeline` or :py:class:`sklearn.estimator.Base` to be dask mixed
fit_tag: list(tuple()) or
"
str
"
Tag the `fit` method. This is useful to tag dask tasks to run in specific workers https://distributed.dask.org/en/latest/resources.html
If `o` is :py:class:`sklearn.pipeline.Pipeline`, this parameter should contain a list of tuples
containing the pipeline.step index and the `str` tag for `fit`.
If `o` is :py:class:`sklearn.estimator.Base`, this parameter should contain just the tag for `fit`
transform_tag: list(tuple()) or
"
str
"
Tag the `fit` method. This is useful to tag dask tasks to run in specific workers https://distributed.dask.org/en/latest/resources.html
If `o` is :py:class:`sklearn.pipeline.Pipeline`, this parameter should contain a list of tuples
containing the pipeline.step index and the `str` tag for `transform`.
If `o` is :py:class:`sklearn.estimator.Base`, this parameter should contain just the tag for `transform`
Examples
--------
Vanilla example
>>>
pipeline
=
dask_it
(
pipeline
)
# Take some pipeline and make the methods `fit`and `transform` run over dask
>>>
pipeline
.
fit
(
samples
).
compute
()
In this example we will
"
mark
"
the fit method with a particular tag
Hence, we can set the `dask.delayed.compute` method to place some
delayeds to be executed in particular resources
>>>
pipeline
=
dask_it
(
pipeline
,
fit_tag
=
[(
1
,
"
GPU
"
)])
# Take some pipeline and make the methods `fit`and `transform` run over dask
>>>
fit
=
pipeline
.
fit
(
samples
)
>>>
fit
.
compute
(
resources
=
pipeline
.
dask_tags
())
Taging estimator
>>>
estimator
=
dask_it
(
estimator
)
>>>
transf
=
estimator
.
transform
(
samples
)
>>>
transf
.
compute
(
resources
=
estimator
.
dask_tags
())
"""
def
_fetch_resource_tape
(
o
):
def
_fetch_resource_tape
(
self
):
"""
Get all the resources take
"""
resource_tape
=
dict
()
if
isinstance
(
o
,
Pipeline
):
for
o
in
range
(
1
,
len
(
o
.
steps
)):
resource_tape
+=
o
.
resource_tape
resource_tags
=
dict
()
if
isinstance
(
self
,
Pipeline
):
for
i
in
range
(
1
,
len
(
self
.
steps
)):
resource_tags
.
update
(
o
[
i
].
resource_tags
)
else
:
resource_tags
.
update
(
self
.
resource_tags
)
return
resource_ta
pe
return
resource_ta
gs
if
isinstance
(
o
,
Pipeline
):
#Adding a daskbag in the tail of the pipeline
o
.
steps
.
insert
(
0
,
(
'
0
'
,
DaskBagMixin
()))
# Patching dask_resources
dasked
=
mix_me_up
(
DaskEstimatorMixin
,
o
)
#dasked.dask_resources = _fetch_resource_tape(o)
# Tagging each element in a pipeline
if
isinstance
(
o
,
Pipeline
):
# Tagging each element for fitting and transforming
if
fit_tag
is
not
None
:
for
t
in
fit_tag
:
o
.
steps
[
t
[
0
]][
1
].
fit_tag
=
t
[
1
]
if
transform_tag
is
not
None
:
for
t
in
transform_tag
:
o
.
steps
[
t
[
0
]][
1
].
transform_tag
=
t
[
1
]
else
:
dasked
.
fit_tag
=
fit_tag
dasked
.
transform_tag
=
transform_tag
# Bounding the method
dasked
.
dask_tags
=
types
.
MethodType
(
_fetch_resource_tape
,
dasked
)
return
dasked
...
...
@@ -287,17 +351,17 @@ class DaskEstimatorMixin:
"""
def
__init__
(
self
,
fit_
resource
=
None
,
transform_
resource
=
None
,
**
kwargs
):
def
__init__
(
self
,
fit_
tag
=
None
,
transform_
tag
=
None
,
**
kwargs
):
super
().
__init__
(
**
kwargs
)
self
.
_dask_state
=
self
self
.
resource_ta
pe
=
dict
()
self
.
fit_
resource
=
fit_resource
self
.
transform_
resource
=
transform_
resource
self
.
resource_ta
gs
=
dict
()
self
.
fit_
tag
=
fit_tag
self
.
transform_
tag
=
transform_
tag
def
fit
(
self
,
X
,
y
=
None
,
**
fit_params
):
self
.
_dask_state
=
delayed
(
super
().
fit
)(
X
,
y
,
**
fit_params
)
if
self
.
fit_
resource
is
not
None
:
self
.
resource_ta
pe
[
self
.
_dask_state
]
=
self
.
fit_
resource
if
self
.
fit_
tag
is
not
None
:
self
.
resource_ta
gs
[
self
.
_dask_state
]
=
self
.
fit_
tag
return
self
...
...
@@ -306,8 +370,8 @@ class DaskEstimatorMixin:
return
super
(
DaskEstimatorMixin
,
dask_state
).
transform
(
X_line
)
map_partitions
=
X
.
map_partitions
(
_transf
,
self
.
_dask_state
)
if
self
.
transform_
resource
is
not
None
:
self
.
resource_ta
pe
[
map_partitions
]
=
self
.
transform_
resource
if
self
.
transform_
tag
is
not
None
:
self
.
resource_ta
gs
[
map_partitions
]
=
self
.
transform_
tag
return
map_partitions
...
...
This diff is collapsed.
Click to expand it.
bob/pipelines/test/test_mixins.py
+
7
−
3
View file @
622b2570
...
...
@@ -280,9 +280,12 @@ def test_checkpoint_fit_transform_pipeline():
fitter
=
(
"
0
"
,
_build_estimator
(
d
,
0
))
transformer
=
(
"
1
"
,
_build_transformer
(
d
,
1
))
pipeline
=
Pipeline
([
fitter
,
transformer
])
if
dask_enabled
:
pipeline
=
dask_it
(
pipeline
)
if
dask_enabled
:
pipeline
=
dask_it
(
pipeline
,
fit_tag
=
[(
1
,
"
GPU
"
)]
)
pipeline
=
pipeline
.
fit
(
samples
)
tags
=
pipeline
.
dask_tags
()
assert
len
(
tags
)
==
1
transformed_samples
=
pipeline
.
transform
(
samples_transform
)
transformed_samples
=
transformed_samples
.
compute
(
...
...
@@ -348,6 +351,7 @@ def test_dask_checkpoint_transform_pipeline():
samples_transform
=
[
Sample
(
data
,
key
=
str
(
i
))
for
i
,
data
in
enumerate
(
X
)]
with
tempfile
.
TemporaryDirectory
()
as
d
:
bag_transformer
=
DaskBagMixin
()
estimator
=
dask_it
(
_build_transformer
(
d
,
0
))
estimator
=
dask_it
(
_build_transformer
(
d
,
0
)
,
transform_tag
=
"
CPU
"
)
X_tr
=
estimator
.
transform
(
bag_transformer
.
transform
(
samples_transform
))
assert
len
(
estimator
.
dask_tags
())
==
1
assert
len
(
X_tr
.
compute
(
scheduler
=
"
single-threaded
"
))
==
10
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