-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tensorflow 2.6 and pytorch 1.10 support (#212)
* added pytorch 1.10.x * added tensorflow 2.6 * bugfix on versions
- Loading branch information
Showing
10 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
RUN pip install "tensorflow>=2.6,<2.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"username": "__USER__", | ||
"algoname": "__ALGO__", | ||
"language": "python3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.]]} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"username": "__USER__", | ||
"algoname": "__ALGO__", | ||
"language": "python-3.x-1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.]]} |