gpt4 book ai didi

python - 使用 Dask 进行大规模并行搜索操作,分布式

转载 作者:行者123 更新时间:2023-11-30 22:30:36 25 4
gpt4 key购买 nike

我在 Kubernetes 和 AWS 上测试自动扩展 Dask 分布式实现时创建了一个演示问题,但我不确定是否正确解决了该问题。

我的场景是给定一个字符串(代表密码)的 md5 哈希值,找到原始字符串。我遇到了三个主要问题。

A) 参数空间很大,尝试创建一个包含 2.8211099e+12 个成员的 dask bag 会导致内存问题(因此您将在下面的示例代码中看到“explode”函数)。

B) 尽早发现时干净退出。我认为使用 take(1, npartitions=-1) 可以实现这一点,但我不确定。最初我提出了一个异常 raise Exception("%s is your answer' % test_str) 它有效但感觉“肮脏”

C) 鉴于这是长时间运行的,有时工作人员或 AWS 机器会死机,那么如何最好地存储进度?

示例代码:

import distributed
import math
import dask.bag as db
import hashlib
import dask
import os

if os.environ.get('SCHED_URL', False):
sched_url = os.environ['SCHED_URL']
client = distributed.Client(sched_url)
versions = client.get_versions(True)
dask.set_options(get=client.get)

difficulty = 'easy'

settings = {
'hard': (hashlib.md5('welcome1'.encode('utf-8')).hexdigest(),'abcdefghijklmnopqrstuvwxyz1234567890', 8),
'mid-hard': (hashlib.md5('032abgh'.encode('utf-8')).hexdigest(),'abcdefghijklmnop1234567890', 7),
'mid': (hashlib.md5('b08acd'.encode('utf-8')).hexdigest(),'0123456789abcdef', 6),
'easy': (hashlib.md5('0812'.encode('utf-8')).hexdigest(),'0123456789', 4)
}

hashed_pw, keyspace, max_guess_length = settings[difficulty]

def is_pw(guess):
return hashlib.md5(guess.encode('utf-8')).hexdigest() == hashed_pw

def guess(n):
guess = ''
size = len(keyspace)
while n>0 :
n -= 1
guess += keyspace[n % size];
n = math.floor(n / size);
return guess

def make_exploder(num_partitions, max_val):
"""Creates a function that maps a int to a range based on the number maximum value aimed for
and the number of partitions that are expected.
Used in this code used with map and flattent to take a short list
i.e 1->1e6 to a large one 1->1e20 in dask rather than on the host machine."""
steps = math.ceil(max_val / num_partitions)
def explode(partition):
return range(partition * steps, partition * steps + steps)
return explode


max_val = len(keyspace) ** max_guess_length # How many possiable password permutation
partitions = math.floor(max_val / 100)
partitions = partitions if partitions < 100000 else 100000 # split in to a maximum of 10000 partitions. Too many partitions caused issues, memory I think.
exploder = make_exploder(partitions, max_val) # Sort of the opposite of a reduce. make_exploder(10, 100)(3) => [30, 31, ..., 39]. Expands the problem back in to the full problem space.

print("max val: %s, partitions:%s" % (max_val, partitions))

search = db.from_sequence(range(partitions), npartitions=partitions).map(exploder).flatten().filter(lambda i: i <= max_val).map(guess).filter(is_pw)

search.take(1,npartitions=-1)

我发现“简单”在本地效果很好,“中难”在我们的 6 到 8 * m4.2xlarge AWS 集群上效果很好。但到目前为止还没有很难工作。

最佳答案

A) the parameter space is massive and trying to create a dask bag with 2.8211099e+12 members caused memory issues (hence the 'explode' function you'll see in the sample code below).

这很大程度上取决于您如何将元素放入包中。如果每个元素都在自己的分区中,那么是的,这肯定会杀死所有内容。 1e12 分区非常昂贵。我建议将分区数量保持在数千或数万。

B) Clean exit on early find. I think using take(1, npartitions=-1) will achieve this but I wasn't sure. Originally I raised an exception raise Exception("%s is your answer' % test_str) which worked but felt "dirty"

如果您想要这个,那么我建议不要使用 dask.bag,而是使用 concurrent.futures interface特别是 as_completed迭代器。

C) Given this is long running and sometimes workers or AWS boxes die, how would it be best to store progress?

只要您能保证调度程序能够生存,Dask 就应该能够适应这种情况。如果您使用并发 futures 接口(interface)而不是 dask bag,那么您还可以跟踪客户端进程上的中间结果。

关于python - 使用 Dask 进行大规模并行搜索操作,分布式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com