From 98c0c7a3e83160d0655feaea7a496a0875a8bfe3 Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Fri, 1 Mar 2019 13:57:11 +0100 Subject: [PATCH] [advanced][algorithms] Add basic GPU using algorithm --- advanced/algorithms/username/gpu_test/1.json | 30 +++++++++ advanced/algorithms/username/gpu_test/1.py | 61 +++++++++++++++++++ advanced/algorithms/username/gpu_test/1.rst | 22 +++++++ .../integers_add_torch_autonomous/1.json | 2 +- .../username/integers_add_torch_legacy/1.json | 2 +- .../integers_add_torch_sequential/1.json | 2 +- 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 advanced/algorithms/username/gpu_test/1.json create mode 100644 advanced/algorithms/username/gpu_test/1.py create mode 100644 advanced/algorithms/username/gpu_test/1.rst diff --git a/advanced/algorithms/username/gpu_test/1.json b/advanced/algorithms/username/gpu_test/1.json new file mode 100644 index 0000000..3744b51 --- /dev/null +++ b/advanced/algorithms/username/gpu_test/1.json @@ -0,0 +1,30 @@ +{ + "type": "sequential", + "groups": [ + { + "outputs": { + "scores": { + "type": "{{ user.username }}/probe_scores/1" + } + }, + "inputs": { + "client_id": { + "type": "{{ system_user.username }}/uint64/1" + } + }, + "name": "main" + } + ], + "parameters": { + "offset": { + "type": "uint32", + "default": 10, + "description": "Offset" + } + }, + "schema_version": 2, + "api_version": 2, + "description": "", + "splittable": false, + "language": "python" +} diff --git a/advanced/algorithms/username/gpu_test/1.py b/advanced/algorithms/username/gpu_test/1.py new file mode 100644 index 0000000..149dc2e --- /dev/null +++ b/advanced/algorithms/username/gpu_test/1.py @@ -0,0 +1,61 @@ +############################################################################### +# # +# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ # +# Contact: beat.support@idiap.ch # +# # +# This file is part of the beat.examples module of the BEAT platform. # +# # +# Commercial License Usage # +# Licensees holding valid commercial BEAT licenses may use this file in # +# accordance with the terms contained in a written agreement between you # +# and Idiap. For further information contact tto@idiap.ch # +# # +# Alternatively, this file may be used under the terms of the GNU Affero # +# Public License version 3 as published by the Free Software and appearing # +# in the file LICENSE.AGPL included in the packaging of this file. # +# The BEAT platform is distributed in the hope that it will be useful, but # +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # +# or FITNESS FOR A PARTICULAR PURPOSE. # +# # +# You should have received a copy of the GNU Affero Public License along # +# with the BEAT platform. If not, see http://www.gnu.org/licenses/. # +# # +############################################################################### + +import numpy +import torch + +class Algorithm: + + def __init__(self): + self.offset = 1 + + def setup(self, parameters): + self.offset = numpy.asscalar(parameters['offset']) + return torch.cuda.is_available() + + def process(self, inputs, data_loaders, outputs): + client_id = inputs['client_id'].data.value + + x = torch.FloatTensor(1, 1).fill_(numpy.asscalar(client_id)).cuda() + y = torch.FloatTensor(1, 1).fill_(self.offset).cuda() + z = x + y + score = z.cpu().numpy().astype(numpy.int32)[0][0] + + scores = [ + { + 'template_identity': client_id, + 'score': score if client_id % 2 else -score + }, + { + 'template_identity': 0, + 'score': -score if client_id % 2 else score + } + ] + + outputs['scores'].write({ + 'client_identity': client_id, + 'scores': scores + }) + + return True diff --git a/advanced/algorithms/username/gpu_test/1.rst b/advanced/algorithms/username/gpu_test/1.rst new file mode 100644 index 0000000..3d94c66 --- /dev/null +++ b/advanced/algorithms/username/gpu_test/1.rst @@ -0,0 +1,22 @@ +.. Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ .. +.. Contact: beat.support@idiap.ch .. +.. .. +.. This file is part of the beat.examples module of the BEAT platform. .. +.. .. +.. Commercial License Usage .. +.. Licensees holding valid commercial BEAT licenses may use this file in .. +.. accordance with the terms contained in a written agreement between you .. +.. and Idiap. For further information contact tto@idiap.ch .. +.. .. +.. Alternatively, this file may be used under the terms of the GNU Affero .. +.. Public License version 3 as published by the Free Software and appearing .. +.. in the file LICENSE.AGPL included in the packaging of this file. .. +.. The BEAT platform is distributed in the hope that it will be useful, but .. +.. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY .. +.. or FITNESS FOR A PARTICULAR PURPOSE. .. +.. .. +.. You should have received a copy of the GNU Affero Public License along .. +.. with the BEAT platform. If not, see http://www.gnu.org/licenses/. .. + + +Minimal algorithm validating GPU setup. \ No newline at end of file diff --git a/test/algorithms/username/integers_add_torch_autonomous/1.json b/test/algorithms/username/integers_add_torch_autonomous/1.json index 09b49e7..adec321 100644 --- a/test/algorithms/username/integers_add_torch_autonomous/1.json +++ b/test/algorithms/username/integers_add_torch_autonomous/1.json @@ -20,7 +20,7 @@ ], "parameters": { "offset": { - "default": "1", + "default": 1, "type": "int32" } }, diff --git a/test/algorithms/username/integers_add_torch_legacy/1.json b/test/algorithms/username/integers_add_torch_legacy/1.json index e59a0ec..578d04b 100644 --- a/test/algorithms/username/integers_add_torch_legacy/1.json +++ b/test/algorithms/username/integers_add_torch_legacy/1.json @@ -18,7 +18,7 @@ ], "parameters": { "offset": { - "default": "1", + "default": 1, "type": "int32" } }, diff --git a/test/algorithms/username/integers_add_torch_sequential/1.json b/test/algorithms/username/integers_add_torch_sequential/1.json index 74bca6f..67a0153 100644 --- a/test/algorithms/username/integers_add_torch_sequential/1.json +++ b/test/algorithms/username/integers_add_torch_sequential/1.json @@ -20,7 +20,7 @@ ], "parameters": { "offset": { - "default": "1", + "default": 1, "type": "int32" }, "sync": { -- GitLab