mirror of
https://github.com/wassname/ray.git
synced 2026-08-07 11:27:43 +08:00
change remote function invocation from func() to func.remote() (#328)
This commit is contained in:
committed by
Philipp Moritz
parent
92f1976e94
commit
0e5b858324
@@ -72,7 +72,7 @@ of object references, where the first object reference in each pair refers to a
|
||||
batch of images and the second refers to the corresponding batch of labels.
|
||||
|
||||
```python
|
||||
batches = [load_tarfile_from_s3(bucket, s3_key, size) for s3_key in s3_keys]
|
||||
batches = [load_tarfile_from_s3.remote(bucket, s3_key, size) for s3_key in s3_keys]
|
||||
```
|
||||
|
||||
By default, this will only fetch objects whose keys have prefix
|
||||
@@ -104,5 +104,5 @@ gradient_refs = []
|
||||
for i in range(num_workers):
|
||||
# Choose a random batch and use it to compute the gradient of the loss.
|
||||
x_ref, y_ref = batches[np.random.randint(len(batches))]
|
||||
gradient_refs.append(compute_grad(x_ref, y_ref, mean_ref, weights_ref))
|
||||
gradient_refs.append(compute_grad.remote(x_ref, y_ref, mean_ref, weights_ref))
|
||||
```
|
||||
|
||||
@@ -74,7 +74,7 @@ def load_tarfiles_from_s3(bucket, s3_keys, size=[]):
|
||||
np.ndarray: Contains object references to the chunks of the images (see load_chunk).
|
||||
"""
|
||||
|
||||
return [load_tarfile_from_s3(bucket, s3_key, size) for s3_key in s3_keys]
|
||||
return [load_tarfile_from_s3.remote(bucket, s3_key, size) for s3_key in s3_keys]
|
||||
|
||||
def setup_variables(params, placeholders, assigns, kernelshape, biasshape):
|
||||
"""Creates the variables for each layer and adds the variables and the components needed to feed them to various lists
|
||||
@@ -239,7 +239,7 @@ def num_images(batches):
|
||||
Returns:
|
||||
int: The number of images
|
||||
"""
|
||||
shape_refs = [ra.shape(batch) for batch in batches]
|
||||
shape_refs = [ra.shape.remote(batch) for batch in batches]
|
||||
return sum([ray.get(shape_ref)[0] for shape_ref in shape_refs])
|
||||
|
||||
@ray.remote([List], [np.ndarray])
|
||||
@@ -254,9 +254,9 @@ def compute_mean_image(batches):
|
||||
"""
|
||||
if len(batches) == 0:
|
||||
raise Exception("No images were passed into `compute_mean_image`.")
|
||||
sum_image_refs = [ra.sum(batch, axis=0) for batch in batches]
|
||||
sum_image_refs = [ra.sum.remote(batch, axis=0) for batch in batches]
|
||||
sum_images = [ray.get(ref) for ref in sum_image_refs]
|
||||
n_images = num_images(batches)
|
||||
n_images = num_images.remote(batches)
|
||||
return np.sum(sum_images, axis=0).astype("float64") / ray.get(n_images)
|
||||
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray, np.ndarray], [np.ndarray, np.ndarray, np.ndarray, np.ndarray])
|
||||
@@ -303,7 +303,7 @@ def shuffle_pair(first_batch, second_batch):
|
||||
Tuple[ObjRef, Objref]: The first batch of shuffled data.
|
||||
Tuple[ObjRef, Objref]: Two second bach of shuffled data.
|
||||
"""
|
||||
images1, labels1, images2, labels2 = shuffle_arrays(first_batch[0], first_batch[1], second_batch[0], second_batch[1])
|
||||
images1, labels1, images2, labels2 = shuffle_arrays.remote(first_batch[0], first_batch[1], second_batch[0], second_batch[1])
|
||||
return (images1, labels1), (images2, labels2)
|
||||
|
||||
@ray.remote([list, dict], [np.ndarray])
|
||||
|
||||
@@ -44,10 +44,10 @@ if __name__ == "__main__":
|
||||
imagenet_data = alexnet.load_tarfiles_from_s3(args.s3_bucket, image_tar_files, [256, 256])
|
||||
|
||||
# Convert the parsed filenames to integer labels and create batches.
|
||||
batches = [(images, alexnet.filenames_to_labels(filenames, filename_label_dict_ref)) for images, filenames in imagenet_data]
|
||||
batches = [(images, alexnet.filenames_to_labels.remote(filenames, filename_label_dict_ref)) for images, filenames in imagenet_data]
|
||||
|
||||
# Compute the mean image.
|
||||
mean_ref = alexnet.compute_mean_image([images for images, labels in batches])
|
||||
mean_ref = alexnet.compute_mean_image.remote([images for images, labels in batches])
|
||||
|
||||
# The data does not start out shuffled. Images of the same class all appear
|
||||
# together, so we shuffle it ourselves here. Each shuffle pairs up the batches
|
||||
@@ -71,14 +71,14 @@ if __name__ == "__main__":
|
||||
|
||||
# Compute the accuracy on a random training batch.
|
||||
x_ref, y_ref = batches[np.random.randint(len(batches))]
|
||||
accuracy = alexnet.compute_accuracy(x_ref, y_ref, weights_ref)
|
||||
accuracy = alexnet.compute_accuracy.remote(x_ref, y_ref, weights_ref)
|
||||
|
||||
# Launch tasks in parallel to compute the gradients for some batches.
|
||||
gradient_refs = []
|
||||
for i in range(num_workers - 1):
|
||||
# Choose a random batch and use it to compute the gradient of the loss.
|
||||
x_ref, y_ref = batches[np.random.randint(len(batches))]
|
||||
gradient_refs.append(alexnet.compute_grad(x_ref, y_ref, mean_ref, weights_ref))
|
||||
gradient_refs.append(alexnet.compute_grad.remote(x_ref, y_ref, mean_ref, weights_ref))
|
||||
|
||||
# Print the accuracy on a random training batch.
|
||||
print "Iteration {}: accuracy = {:.3}%".format(iteration, 100 * ray.get(accuracy))
|
||||
|
||||
@@ -105,7 +105,7 @@ computation. Instead, it simply submits a number of tasks to the scheduler.
|
||||
result_refs = []
|
||||
for _ in range(100):
|
||||
params = generate_random_params()
|
||||
results.append((params, train_cnn_and_compute_accuracy(params, epochs)))
|
||||
results.append((params, train_cnn_and_compute_accuracy.remote(params, epochs)))
|
||||
```
|
||||
|
||||
If we wish to wait until the results have all been retrieved, we can retrieve
|
||||
|
||||
@@ -37,7 +37,7 @@ if __name__ == "__main__":
|
||||
dropout = np.random.uniform(0, 1)
|
||||
stddev = 10 ** np.random.uniform(-5, 5)
|
||||
params = {"learning_rate": learning_rate, "batch_size": batch_size, "dropout": dropout, "stddev": stddev}
|
||||
results.append((params, hyperopt.train_cnn_and_compute_accuracy(params, epochs, train_images, train_labels, validation_images, validation_labels)))
|
||||
results.append((params, hyperopt.train_cnn_and_compute_accuracy.remote(params, epochs, train_images, train_labels, validation_images, validation_labels)))
|
||||
|
||||
# Fetch the results of the tasks and print the results.
|
||||
for i in range(trials):
|
||||
|
||||
@@ -112,12 +112,12 @@ gradient.
|
||||
```python
|
||||
def full_loss(theta):
|
||||
theta_ref = ray.put(theta)
|
||||
loss_refs = [loss(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
loss_refs = [loss.remote(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
return sum([ray.get(loss_ref) for loss_ref in loss_refs])
|
||||
|
||||
def full_grad(theta):
|
||||
theta_ref = ray.put(theta)
|
||||
grad_refs = [grad(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
grad_refs = [grad.remote(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
return sum([ray.get(grad_ref) for grad_ref in grad_refs]).astype("float64") # This conversion is necessary for use with fmin_l_bfgs_b.
|
||||
```
|
||||
|
||||
@@ -125,14 +125,14 @@ Note that we turn `theta` into a remote object with the line `theta_ref =
|
||||
ray.put(theta)` before passing it into the remote functions. If we had written
|
||||
|
||||
```python
|
||||
[loss(theta, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
[loss.remote(theta, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
```
|
||||
|
||||
instead of
|
||||
|
||||
```python
|
||||
theta_ref = ray.put(theta)
|
||||
[loss(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
[loss.remote(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
```
|
||||
|
||||
then each task that got sent to the scheduler (one for every element of
|
||||
|
||||
@@ -79,13 +79,13 @@ if __name__ == "__main__":
|
||||
# Compute the loss on the entire dataset.
|
||||
def full_loss(theta):
|
||||
theta_ref = ray.put(theta)
|
||||
loss_refs = [loss(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
loss_refs = [loss.remote(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
return sum([ray.get(loss_ref) for loss_ref in loss_refs])
|
||||
|
||||
# Compute the gradient of the loss on the entire dataset.
|
||||
def full_grad(theta):
|
||||
theta_ref = ray.put(theta)
|
||||
grad_refs = [grad(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
grad_refs = [grad.remote(theta_ref, xs_ref, ys_ref) for (xs_ref, ys_ref) in batch_refs]
|
||||
return sum([ray.get(grad_ref) for grad_ref in grad_refs]).astype("float64") # This conversion is necessary for use with fmin_l_bfgs_b.
|
||||
|
||||
# From the perspective of scipy.optimize.fmin_l_bfgs_b, full_loss is simply a
|
||||
|
||||
@@ -54,7 +54,7 @@ model_ref = ray.put(model)
|
||||
grads, reward_sums = [], []
|
||||
# Launch tasks to compute gradients from multiple rollouts in parallel.
|
||||
for i in range(10):
|
||||
grad_ref, reward_sum_ref = compute_gradient(model_ref)
|
||||
grad_ref, reward_sum_ref = compute_gradient.remote(model_ref)
|
||||
grads.append(grad_ref)
|
||||
reward_sums.append(reward_sum_ref)
|
||||
```
|
||||
|
||||
@@ -127,7 +127,7 @@ if __name__ == "__main__":
|
||||
grads, reward_sums = [], []
|
||||
# Launch tasks to compute gradients from multiple rollouts in parallel.
|
||||
for i in range(batch_size):
|
||||
grad_ref, reward_sum_ref = compute_gradient(model_ref)
|
||||
grad_ref, reward_sum_ref = compute_gradient.remote(model_ref)
|
||||
grads.append(grad_ref)
|
||||
reward_sums.append(reward_sum_ref)
|
||||
for i in range(batch_size):
|
||||
|
||||
Reference in New Issue
Block a user