Skip to content

Commit

Permalink
Tensorflow 2.6 and pytorch 1.10 support (#212)
Browse files Browse the repository at this point in the history
* added pytorch 1.10.x

* added tensorflow 2.6

* bugfix on versions
  • Loading branch information
zeryx authored Nov 13, 2021
1 parent 0e54c26 commit 285f611
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libraries/pytorch-1.10.x/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RUN pip install numpy==1.16.0
RUN pip install "torch>=1.10.0,<1.11.0"
1 change: 1 addition & 0 deletions libraries/tensorflow-gpu-2.6/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUN pip install "tensorflow>=2.6,<2.7"
5 changes: 5 additions & 0 deletions templates/pytorch-1.10.x/algorithmia.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"username": "__USER__",
"algoname": "__ALGO__",
"language": "python3"
}
6 changes: 6 additions & 0 deletions templates/pytorch-1.10.x/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
algorithmia>=1.0.0,<2.0
six
numpy>=1.16.0,<2.0

# you can use a different pytorch version, however please bear in mind that it may take longer to compile and load.
torch>=1.10.0,<1.11.0
58 changes: 58 additions & 0 deletions templates/pytorch-1.10.x/src/__ALGO__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Algorithmia
import torch as th

"""
Example Input:
{
"matrix_a": [[0, 1], [1, 0]],
"matrix_b": [[25, 25], [11, 11]]
}
Expected Output:
{
"product": [[11, 11], [25, 25]]
}
"""

class InputObject:
def __init__(self, input_dict):
"""
Creates an instance of the InputObject, which checks the format of data and throws exceptions if anything is
missing.
"matrix_a" and "matrix_b" must be the same shape.
:param A - Matrix A, converted from a json list into a torch cuda Tensor.
:param B - Matrix B, converted from a json list into a torch cuda Tensor.
"""
if isinstance(input_dict, dict):
if {'matrix_a', 'matrix_b'} <= input_dict.keys():
self.A = convert(input_dict['matrix_a'])
self.B = convert(input_dict['matrix_b'])
else:
raise Exception("'matrix_a' and 'matrix_b' must be defined.")
else:
raise Exception('input must be a json object.')
if self.A.shape[-1] != self.B.shape[0]:
raise Exception('inner dimensions between A and B must be the same.\n A: {} B: {}'
.format(self.A.shape[-1], self.B.shape[0]))


def convert(list_array):
"""
Converts a json list into a torch Tensor object.
"""
th_tensor = th.tensor(list_array).float()
gpu_tensor = th_tensor.cuda()
return gpu_tensor


def apply(input):
"""
Calculates the dot product of two matricies using pytorch, with a cudnn backend.
Returns the product as the output.
"""
print(th.__version__)
input = InputObject(input)
C = th.mm(input.A, input.B)
z = C.cpu().numpy().tolist()
output = {'product': z}
return output
7 changes: 7 additions & 0 deletions templates/pytorch-1.10.x/src/__ALGO___test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __ALGO__ import apply

def test_algorithm():
input = {"matrix_a": [[0, 1], [1, 0]], "matrix_b": [[25, 25], [11, 11]]}
result = apply(input)
assert result == {"product": [[11., 11.], [25., 25.]]}

5 changes: 5 additions & 0 deletions templates/tensorflow-gpu-2.6/algorithmia.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"username": "__USER__",
"algoname": "__ALGO__",
"language": "python-3.x-1"
}
5 changes: 5 additions & 0 deletions templates/tensorflow-gpu-2.6/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
algorithmia>=1.0.0,<2.0
six

# if you need a different version, please change this value - bear in mind that it may take longer to compile.
tensorflow>=2.6,<2.7
81 changes: 81 additions & 0 deletions templates/tensorflow-gpu-2.6/src/__ALGO__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Algorithmia
import tensorflow.keras.backend as K
from tensorflow import convert_to_tensor
import tensorflow as tf
import numpy as np

"""
Example Input:
{
"matrix_a": [[0, 1], [1, 0]],
"matrix_b": [[25, 25], [11, 11]]
}
Expected Output:
{
"product": [[11, 11], [25, 25]]
}
"""

# Print TensorFlow info
print(tf.__version__)
print(tf.test.is_built_with_cuda())
print(tf.sysconfig.get_build_info()["cuda_version"])
print(tf.test.is_built_with_gpu_support())
print(tf.config.list_physical_devices("GPU"))
print(tf.test.gpu_device_name())


# Configure Tensorflow to only use up to 30% of the GPU.
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3432)])


class InputObject:
def __init__(self, input_dict):
"""
Creates an instance of the InputObject, which checks the format of data and throws exceptions if anything is
missing.
"matrix_a" and "matrix_b" must be the same shape.
:param A - Matrix A, converted from a json list into a keras Tensor.
:param B - Matrix B, converted from a json list into a keras Tensor.
"""
if isinstance(input_dict, dict):
if {'matrix_a', 'matrix_b'} <= input_dict.keys():
self.A = convert(input_dict['matrix_a'])
self.B = convert(input_dict['matrix_b'])
else:
raise Exception("'matrix_a' and 'matrix_b' must be defined.")
else:
raise Exception('input must be a json object.')
if self.A.shape[-1] != self.B.shape[0]:
raise Exception('inner dimensions between A and B must be the same.\n A: {} B: {}'.format(self.A.shape[-1],
self.B.shape[0]))


def convert(list_array):
"""
Converts a json list into a keras Tensor object.
"""
numpy_object = np.asarray(list_array, dtype=np.float)
tensor_object = convert_to_tensor(numpy_object)
return tensor_object


def apply(input):
"""
Calculates the dot product of two matricies using keras, with a tensorflow-gpu backend.
Returns the product as the output.
"""

input = InputObject(input)

z = K.dot(input.A, input.B)
# Here you need to use K.eval() instead of z.eval() because this uses the backend session
K.eval(z)
z = K.get_value(z)
output = {'product': z.tolist()}
return output

7 changes: 7 additions & 0 deletions templates/tensorflow-gpu-2.6/src/__ALGO___test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .__ALGO__ import apply


def test_algorithm():
input = {"matrix_a": [[0, 1], [1, 0]], "matrix_b": [[25, 25], [11, 11]]}
result = apply(input)
assert result == {"product": [[11., 11.], [25., 25.]]}

0 comments on commit 285f611

Please sign in to comment.