Hello, I’m running metaflow through AWS batch, do ...
# ask-metaflow
r
Hello, I’m running metaflow through AWS batch, do you know how I can get the vcpu count from within my step if I restrict it through the resource decorator? e.g. if I use os.cpu_count() within my step I get the machine cpu count instead of what was restricted by the resource decorator. also when I try to read
/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us
I get -1.
1
s
great question! It’s actually quite tricky since Batch allows bursting to all available cores on the instance
r
so does this mean there isn’t a reliable way to get the cpu_count at the container level if I use batch?
v
you can get the minimum cpu_count reliably, maximum is available indirectly, but the actual number is variable
r
a second questions would be, if I define the cpu count through the resource decorator, can I look up that value within the step and pass it to my function?
v
yes, let me give you an example..
r
how would the minimum cpu_count relate to what i’ve potentially defined in the resource decorator? would it be the same?
v
minimum equals to what's specified in
@resources
r
oh icic. i guess from my first question, how can I get the minimum cpu count as well
v
try this:
Copy code
from metaflow import FlowSpec, current, resources, Parameter, step, Task

class CPUCountFlow(FlowSpec):

    @resources(cpu=8)
    @step
    def start(self):
        print('cpu count', self.cpu_count())
        self.next(self.end)

    @step
    def end(self):
        print("done!")

    def cpu_count(self):
        decos = self._graph_info['steps'][current.step_name]['decorators']
        for deco in decos:
            if deco['name'] == 'resources':
                return deco['attributes']['cpu'] 

if __name__ == '__main__':
    CPUCountFlow()
it gives you the minimum cpu count when running on
@batch
(caveats: it works only if you have specified
@resources
explicitly and this simple example doesn't grab the count from
@batch
but that's easy to fix)
r
that makes sense. going back to the first question, this is more of a batch or ecs question then but do you know of if I can look something up within the container to get the constricted cpu count? Something like looking at
/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us
i have a library that does multiprocessing and uses
os.cpu_count()
to determine how many parallel processes to run. when I execute this on batch, I run more processes than whats been constricted by
@resource
v
yep, that's the issue: Python's
os.cpu_count()
and
multiprocessing.cpu_count()
return all available cores on Batch, which is often too high
I looked into workarounds a while back but couldn't find an easy answer
your safest bet is to set cpu_count to the minimum but you are then potentially underutilizing resources that would be available through bursting
r
i’d be ok with setting the cpu_count to be the minimum
👍 1
v
another solution is construct your compute environment so that
@resources
match instance sizes, so min==max, which effectively disables bursting
r
but how can i create a
cpu_count()
function to grab the minimum amount in batch?
r
oh, i meant is there a solution to get the cpu count without using information from the flow. like in docker if I constrict the resources with
--cpu
I can look at
/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us
to figure out how much cpu I have. this would probably be more of a batch/ecs question but do you know of a way that I can determine the constrained cpu count from within the container if I’m using batch. when I look at
/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us
from batch, I get
-1
v
right, I don't know if there's a way to do it on Batch. Let me know if you find one 🙂
there are more knobs to control/query CPU limits on
@kubernetes
if you need more control over it
r
haha gotcha. thanks for the help!
👍 1
v
sorry that we don't have a better answer today. Definitely follow up here if you find something