1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
|
#!/usr/bin/python
from hypothesis import given, settings
import hypothesis.strategies as st
from multiprocessing import Process, Queue
import numpy as np
import os
import pickle
import tempfile
import shutil
from caffe2.python import core, workspace, dyndep
import caffe2.python.hypothesis_test_util as hu
from gloo.python import IoError
dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:file_store_handler_ops")
dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:redis_store_handler_ops")
dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:store_ops")
dyndep.InitOpsLibrary("@/caffe2/caffe2/contrib/gloo:gloo_ops")
dyndep.InitOpsLibrary("@/caffe2/caffe2/contrib/gloo:gloo_ops_gpu")
op_engine = 'GLOO'
class TemporaryDirectory:
def __enter__(self):
self.tmpdir = tempfile.mkdtemp()
return self.tmpdir
def __exit__(self, type, value, traceback):
shutil.rmtree(self.tmpdir)
class TestCase(hu.HypothesisTestCase):
test_counter = 0
sync_counter = 0
def run_test_locally(self, fn, device_option=None, **kwargs):
# Queue for assertion errors on subprocesses
queue = Queue()
# Capture any exception thrown by the subprocess
def run_fn(*args, **kwargs):
try:
with core.DeviceScope(device_option):
fn(*args, **kwargs)
workspace.ResetWorkspace()
queue.put(True)
except Exception as ex:
queue.put(ex)
# Start N processes in the background
procs = []
for i in range(kwargs['comm_size']):
kwargs['comm_rank'] = i
proc = Process(
target=run_fn,
kwargs=kwargs)
proc.start()
procs.append(proc)
# Test complete, join background processes
while len(procs) > 0:
proc = procs.pop(0)
while proc.is_alive():
proc.join(10)
# Raise exception if we find any. Otherwise each worker
# should put a True into the queue
# Note that the following is executed ALSO after
# the last process was joined, so if ANY exception
# was raised, it will be re-raised here.
self.assertFalse(queue.empty(), "Job failed without a result")
o = queue.get()
if isinstance(o, Exception):
raise o
else:
self.assertTrue(o)
def run_test_distributed(self, fn, device_option=None, **kwargs):
comm_rank = os.getenv('COMM_RANK')
self.assertIsNotNone(comm_rank)
comm_size = os.getenv('COMM_SIZE')
self.assertIsNotNone(comm_size)
kwargs['comm_rank'] = int(comm_rank)
kwargs['comm_size'] = int(comm_size)
with core.DeviceScope(device_option):
fn(**kwargs)
workspace.ResetWorkspace()
def create_common_world(self, comm_rank, comm_size, tmpdir=None, existing_cw=None):
store_handler = "store_handler"
# If REDIS_HOST is set, use RedisStoreHandler for rendezvous.
if existing_cw is None:
redis_host = os.getenv("REDIS_HOST")
redis_port = int(os.getenv("REDIS_PORT", 6379))
if redis_host is not None:
workspace.RunOperatorOnce(
core.CreateOperator(
"RedisStoreHandlerCreate",
[],
[store_handler],
prefix=str(TestCase.test_counter) + "/",
host=redis_host,
port=redis_port))
else:
workspace.RunOperatorOnce(
core.CreateOperator(
"FileStoreHandlerCreate",
[],
[store_handler],
path=tmpdir))
common_world = "common_world"
else:
common_world = str(existing_cw) + ".forked"
if existing_cw is not None:
workspace.RunOperatorOnce(
core.CreateOperator(
"CloneCommonWorld",
[existing_cw],
[common_world],
sync=True,
engine=op_engine))
else:
workspace.RunOperatorOnce(
core.CreateOperator(
"CreateCommonWorld",
[store_handler],
[common_world],
size=comm_size,
rank=comm_rank,
sync=True,
engine=op_engine))
return (store_handler, common_world)
def synchronize(self, store_handler, value, comm_rank=None):
TestCase.sync_counter += 1
blob = "sync_{}".format(TestCase.sync_counter)
if comm_rank == 0:
workspace.FeedBlob(blob, pickle.dumps(value))
workspace.RunOperatorOnce(
core.CreateOperator(
"StoreSet",
[store_handler, blob],
[]))
else:
workspace.RunOperatorOnce(
core.CreateOperator(
"StoreGet",
[store_handler],
[blob]))
return pickle.loads(workspace.FetchBlob(blob))
def _test_broadcast(self,
comm_rank=None,
comm_size=None,
blob_size=None,
num_blobs=None,
tmpdir=None,
use_float16=False,
):
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
blob_size = self.synchronize(
store_handler,
blob_size,
comm_rank=comm_rank)
num_blobs = self.synchronize(
store_handler,
num_blobs,
comm_rank=comm_rank)
for i in range(comm_size):
blobs = []
for j in range(num_blobs):
blob = "blob_{}".format(j)
offset = (comm_rank * num_blobs) + j
value = np.full(blob_size, offset,
np.float16 if use_float16 else np.float32)
workspace.FeedBlob(blob, value)
blobs.append(blob)
net = core.Net("broadcast")
net.Broadcast(
[common_world] + blobs,
blobs,
root=i,
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
for j in range(num_blobs):
np.testing.assert_array_equal(
workspace.FetchBlob(blobs[j]),
i * num_blobs)
# Run the net a few more times to check the operator
# works not just the first time it's called
for _tmp in range(4):
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
blob_size=st.integers(min_value=int(1e3), max_value=int(1e6)),
num_blobs=st.integers(min_value=1, max_value=4),
device_option=st.sampled_from([hu.cpu_do]),
use_float16=st.booleans())
@settings(deadline=10000)
def test_broadcast(self, comm_size, blob_size, num_blobs, device_option,
use_float16):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_broadcast,
blob_size=blob_size,
num_blobs=num_blobs,
use_float16=use_float16,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_broadcast,
comm_size=comm_size,
blob_size=blob_size,
num_blobs=num_blobs,
device_option=device_option,
tmpdir=tmpdir,
use_float16=use_float16)
def _test_allreduce(self,
comm_rank=None,
comm_size=None,
blob_size=None,
num_blobs=None,
tmpdir=None,
use_float16=False
):
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
blob_size = self.synchronize(
store_handler,
blob_size,
comm_rank=comm_rank)
num_blobs = self.synchronize(
store_handler,
num_blobs,
comm_rank=comm_rank)
blobs = []
for i in range(num_blobs):
blob = "blob_{}".format(i)
value = np.full(blob_size, (comm_rank * num_blobs) + i,
np.float16 if use_float16 else np.float32)
workspace.FeedBlob(blob, value)
blobs.append(blob)
net = core.Net("allreduce")
net.Allreduce(
[common_world] + blobs,
blobs,
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
for i in range(num_blobs):
np.testing.assert_array_equal(
workspace.FetchBlob(blobs[i]),
(num_blobs * comm_size) * (num_blobs * comm_size - 1) / 2)
# Run the net a few more times to check the operator
# works not just the first time it's called
for _tmp in range(4):
workspace.RunNet(net.Name())
def _test_allreduce_multicw(self,
comm_rank=None,
comm_size=None,
tmpdir=None
):
_store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
_, common_world2 = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir,
existing_cw=common_world)
blob_size = int(1e4)
num_blobs = 4
for cw in [common_world, common_world2]:
blobs = []
for i in range(num_blobs):
blob = "blob_{}".format(i)
value = np.full(blob_size, (comm_rank * num_blobs) + i, np.float32)
workspace.FeedBlob(blob, value)
blobs.append(blob)
net = core.Net("allreduce_multicw")
net.Allreduce(
[cw] + blobs,
blobs,
engine=op_engine)
workspace.RunNetOnce(net)
for i in range(num_blobs):
np.testing.assert_array_equal(
workspace.FetchBlob(blobs[i]),
(num_blobs * comm_size) * (num_blobs * comm_size - 1) / 2)
@given(comm_size=st.integers(min_value=2, max_value=8),
blob_size=st.integers(min_value=int(1e3), max_value=int(1e6)),
num_blobs=st.integers(min_value=1, max_value=4),
device_option=st.sampled_from([hu.cpu_do]),
use_float16=st.booleans())
@settings(deadline=10000)
def test_allreduce(self, comm_size, blob_size, num_blobs, device_option,
use_float16):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_allreduce,
blob_size=blob_size,
num_blobs=num_blobs,
use_float16=use_float16,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_allreduce,
comm_size=comm_size,
blob_size=blob_size,
num_blobs=num_blobs,
device_option=device_option,
tmpdir=tmpdir,
use_float16=use_float16)
def _test_reduce_scatter(self,
comm_rank=None,
comm_size=None,
blob_size=None,
num_blobs=None,
tmpdir=None,
use_float16=False
):
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
blob_size = self.synchronize(
store_handler,
blob_size,
comm_rank=comm_rank)
num_blobs = self.synchronize(
store_handler,
num_blobs,
comm_rank=comm_rank)
blobs = []
for i in range(num_blobs):
blob = "blob_{}".format(i)
value = np.full(blob_size, (comm_rank * num_blobs) + i,
np.float16 if use_float16 else np.float32)
workspace.FeedBlob(blob, value)
blobs.append(blob)
# Specify distribution among ranks i.e. number of elements
# scattered/distributed to each process.
recv_counts = np.zeros(comm_size, dtype=np.int32)
remaining = blob_size
chunk_size = (blob_size + comm_size - 1) / comm_size
for i in range(comm_size):
recv_counts[i] = min(chunk_size, remaining)
remaining = remaining - chunk_size if remaining > chunk_size else 0
recv_counts_blob = "recvCounts"
workspace.FeedBlob(recv_counts_blob, recv_counts)
blobs.append(recv_counts_blob)
net = core.Net("reduce_scatter")
net.ReduceScatter(
[common_world] + blobs,
blobs,
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
for i in range(num_blobs):
np.testing.assert_array_equal(
np.resize(workspace.FetchBlob(blobs[i]), recv_counts[comm_rank]),
(num_blobs * comm_size) * (num_blobs * comm_size - 1) / 2)
# Run the net a few more times to check the operator
# works not just the first time it's called
for _tmp in range(4):
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
blob_size=st.integers(min_value=int(1e3), max_value=int(1e6)),
num_blobs=st.integers(min_value=1, max_value=4),
device_option=st.sampled_from([hu.cpu_do]),
use_float16=st.booleans())
@settings(deadline=10000)
def test_reduce_scatter(self, comm_size, blob_size, num_blobs,
device_option, use_float16):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_reduce_scatter,
blob_size=blob_size,
num_blobs=num_blobs,
use_float16=use_float16,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_reduce_scatter,
comm_size=comm_size,
blob_size=blob_size,
num_blobs=num_blobs,
device_option=device_option,
tmpdir=tmpdir,
use_float16=use_float16)
def _test_allgather(self,
comm_rank=None,
comm_size=None,
blob_size=None,
num_blobs=None,
tmpdir=None,
use_float16=False
):
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
blob_size = self.synchronize(
store_handler,
blob_size,
comm_rank=comm_rank)
num_blobs = self.synchronize(
store_handler,
num_blobs,
comm_rank=comm_rank)
blobs = []
for i in range(num_blobs):
blob = "blob_{}".format(i)
value = np.full(blob_size, (comm_rank * num_blobs) + i,
np.float16 if use_float16 else np.float32)
workspace.FeedBlob(blob, value)
blobs.append(blob)
net = core.Net("allgather")
net.Allgather(
[common_world] + blobs,
["Gathered"],
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
# create expected output
expected_output = np.array([])
for i in range(comm_size):
for j in range(num_blobs):
value = np.full(blob_size, (i * num_blobs) + j,
np.float16 if use_float16 else np.float32)
expected_output = np.concatenate((expected_output, value))
np.testing.assert_array_equal(
workspace.FetchBlob("Gathered"), expected_output)
# Run the net a few more times to check the operator
# works not just the first time it's called
for _tmp in range(4):
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
blob_size=st.integers(min_value=int(1e3), max_value=int(1e6)),
num_blobs=st.integers(min_value=1, max_value=4),
device_option=st.sampled_from([hu.cpu_do]),
use_float16=st.booleans())
@settings(max_examples=10, deadline=None)
def test_allgather(self, comm_size, blob_size, num_blobs, device_option,
use_float16):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_allgather,
blob_size=blob_size,
num_blobs=num_blobs,
use_float16=use_float16,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_allgather,
comm_size=comm_size,
blob_size=blob_size,
num_blobs=num_blobs,
device_option=device_option,
tmpdir=tmpdir,
use_float16=use_float16)
@given(device_option=st.sampled_from([hu.cpu_do]))
@settings(deadline=10000)
def test_forked_cw(self, device_option):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_allreduce_multicw,
device_option=device_option)
else:
# Note: this test exercises the path where we fork a common world.
# We therefore don't need a comm size larger than 2. It used to be
# run with comm_size=8, which causes flaky results in a stress run.
# The flakiness was caused by too many listening sockets being
# created by Gloo context initialization (8 processes times
# 7 sockets times 20-way concurrency, plus TIME_WAIT).
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_allreduce_multicw,
comm_size=2,
device_option=device_option,
tmpdir=tmpdir)
def _test_barrier(
self,
comm_rank=None,
comm_size=None,
tmpdir=None,
):
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank, comm_size=comm_size, tmpdir=tmpdir
)
net = core.Net("barrier")
net.Barrier(
[common_world],
[],
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
# Run the net a few more times to check the operator
# works not just the first time it's called
for _tmp in range(4):
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
device_option=st.sampled_from([hu.cpu_do]))
@settings(deadline=10000)
def test_barrier(self, comm_size, device_option):
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_barrier,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_barrier,
comm_size=comm_size,
device_option=device_option,
tmpdir=tmpdir)
def _test_close_connection(
self,
comm_rank=None,
comm_size=None,
tmpdir=None,
):
'''
One node calls close connection, others wait it on barrier.
Test will check that all will exit eventually.
'''
# Caffe's for closers only:
# https://www.youtube.com/watch?v=QMFwFgG9NE8
closer = comm_rank == comm_size // 2,
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank, comm_size=comm_size, tmpdir=tmpdir
)
net = core.Net("barrier_or_close")
if not closer:
net.Barrier(
[common_world],
[],
engine=op_engine)
else:
net.DestroyCommonWorld(
[common_world], [common_world], engine=op_engine)
# Sleep a bit to ensure others start the barrier
import time
time.sleep(0.1)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
device_option=st.sampled_from([hu.cpu_do]))
@settings(deadline=10000)
def test_close_connection(self, comm_size, device_option):
import time
start_time = time.time()
TestCase.test_counter += 1
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_close_connection,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_close_connection,
comm_size=comm_size,
device_option=device_option,
tmpdir=tmpdir)
# Check that test finishes quickly because connections get closed.
# This assert used to check that the end to end runtime was less
# than 2 seconds, but this may not always be the case if there
# is significant overhead in starting processes. Ideally, this
# assert is replaced by one that doesn't depend on time but rather
# checks the success/failure status of the barrier that is run.
self.assertLess(time.time() - start_time, 20.0)
def _test_io_error(
self,
comm_rank=None,
comm_size=None,
tmpdir=None,
):
'''
Only one node will participate in allreduce, resulting in an IoError
'''
store_handler, common_world = self.create_common_world(
comm_rank=comm_rank,
comm_size=comm_size,
tmpdir=tmpdir)
if comm_rank == 0:
blob_size = 1000
num_blobs = 1
blobs = []
for i in range(num_blobs):
blob = "blob_{}".format(i)
value = np.full(
blob_size, (comm_rank * num_blobs) + i, np.float32
)
workspace.FeedBlob(blob, value)
blobs.append(blob)
net = core.Net("allreduce")
net.Allreduce(
[common_world] + blobs,
blobs,
engine=op_engine)
workspace.CreateNet(net)
workspace.RunNet(net.Name())
@given(comm_size=st.integers(min_value=2, max_value=8),
device_option=st.sampled_from([hu.cpu_do]))
@settings(deadline=10000)
def test_io_error(self, comm_size, device_option):
TestCase.test_counter += 1
with self.assertRaises(IoError):
if os.getenv('COMM_RANK') is not None:
self.run_test_distributed(
self._test_io_error,
device_option=device_option)
else:
with TemporaryDirectory() as tmpdir:
self.run_test_locally(
self._test_io_error,
comm_size=comm_size,
device_option=device_option,
tmpdir=tmpdir)
if __name__ == "__main__":
import unittest
unittest.main()
|