Esempio n. 1
0
 def _get_version(self, spec):
     spec = self._parse_spec(spec)
     version = spec['version']
     version_sign = spec['version_sign']
     resource_name = spec['resource_name']
     if version_sign == '==':
         return os.path.join(self.fpath, spec['resource_name'], version)
     found = self.iter_contents(resource_name)
     if version is None:
         sc = semantic_version.compare
         sorted_vers = sorted(found,
                              cmp=lambda a, b: sc(a['version'],
                                                  b['version']),
                              reverse=True)
         if not sorted_vers:
             raise ResourceNotFound(spec)
         version = sorted_vers[0]['version']
     else:
         version = '{}{}'.format(version_sign, version)
         matched = filter(lambda x: semantic_version.match(version,
                                                           x['version']),
                          found)
         sorted_vers = sorted(matched,
                              cmp=lambda a, b: semantic_version.compare(
                                  a['version'],
                                  b['version']),
                              reverse=True)
         version = next((x['version'] for x in sorted_vers
                         if semantic_version.match(version, x['version'])),
                        None)
     if version is None:
         raise ResourceNotFound(spec)
     return version
Esempio n. 2
0
 def _get_version(self, spec):
     spec = self._parse_spec(spec)
     version = spec['version']
     version_sign = spec['version_sign']
     resource_name = spec['resource_name']
     if version_sign == '==':
         return os.path.join(self.fpath, spec['resource_name'], version)
     found = self.iter_contents(resource_name)
     if version is None:
         sc = semantic_version.compare
         sorted_vers = sorted(found,
                              cmp=lambda a, b: sc(a['version'],
                                                  b['version']),
                              reverse=True)
         if not sorted_vers:
             raise ResourceNotFound(spec)
         version = sorted_vers[0]['version']
     else:
         version = '{}{}'.format(version_sign, version)
         matched = filter(lambda x: semantic_version.match(version,
                                                           x['version']),
                          found)
         sorted_vers = sorted(matched,
                              cmp=lambda a, b: semantic_version.compare(
                                  a['version'],
                                  b['version']),
                              reverse=True)
         version = next((x['version'] for x in sorted_vers
                         if semantic_version.match(version, x['version'])),
                        None)
     if version is None:
         raise ResourceNotFound(spec)
     return version
Esempio n. 3
0
def create_message(msg_data):
    try:
        version = get_version(msg_data)

        if semantic_version.match('<0.2.0', version):
            return Message(msg_data)

        if semantic_version.match('<0.2.1', version):
            return MessageV2(msg_data)

        return MessageV2_1(msg_data)
    except Exception:
        msg = Message(msg_data)
        msg.log.exception('Failed to parse message version')
        return msg
Esempio n. 4
0
def _get_test_details(topic, message):
    """
    Returns test details according to the version of the spec.
    """
    result = None

    # version 0.1.x
    if semantic_version.match('<0.2.0', message['version']):
        category = message['category']
        namespace = message['namespace']
        test_type = message['type']
        xunit = message.get('xunit', None)
        result = message.get('status', None)

    # version >= 0.2.0
    else:
        test = message['test']
        category = test['category']
        namespace = test['namespace']
        test_type = test['type']

        # result is required for complete messages only
        if topic.endswith('.complete'):
            result = test['result']

        xunit = message.get('xunit', None)

    return FedoraCiTestArray(
        category=category,
        namespace=namespace,
        result=result,
        type=test_type,
        xunit=xunit
    )
 def test_match(self):
     for spec_txt, versions in self.matches.items():
         spec = semantic_version.Spec(spec_txt)
         self.assertNotEqual(spec, spec_txt)
         for version_txt in versions:
             version = semantic_version.Version(version_txt)
             self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec))
             self.assertTrue(semantic_version.match(spec_txt, version_txt))
             self.assertTrue(version in spec, "%r not in %r" % (version, spec))
 def test_match(self):
     for spec_txt, versions in self.matches.items():
         spec = semantic_version.Spec(spec_txt)
         self.assertNotEqual(spec, spec_txt)
         for version_txt in versions:
             version = semantic_version.Version(version_txt)
             self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec))
             self.assertTrue(semantic_version.match(spec_txt, version_txt))
             self.assertTrue(version in spec, "%r not in %r" % (version, spec))
Esempio n. 7
0
 def test_match(self):
     for spec_text, versions in self.matches.items():
         for version_text in versions:
             with self.subTest(spec=spec_text, version=version_text):
                 spec = semantic_version.Spec(spec_text)
                 self.assertNotEqual(spec, spec_text)
                 version = semantic_version.Version(version_text)
                 self.assertIn(version, spec)
                 self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec))
                 self.assertTrue(semantic_version.match(spec_text, version_text))
Esempio n. 8
0
def match(version, spec):
    if spec == '*' or spec == 'master':
        return True
    if X_RANGE.search(spec):
        spec = '~' + X_RANGE.sub('', spec)
    # semantic_version fails with '~1'...
    if BAD_TILDE.search(spec):
        main = spec[1:]
        spec = '>=%s,<%d' % (main, int(main) + 1)
    if version[0] == 'v':
        version = version[1:]
    try:
        return semantic_version.match(spec, version)
    except ValueError:
        # If the spec is malformed or we don't support it.
        return False
Esempio n. 9
0
def match(version, spec):
  if spec == '*' or spec == 'master':
    return True
  if X_RANGE.search(spec):
    spec = '~' + X_RANGE.sub('', spec)
  # semantic_version fails with '~1'...
  if BAD_TILDE.search(spec):
    main = spec[1:]
    spec = '>=%s,<%d' % (main, int(main) + 1)
  if version[0] == 'v':
    version = version[1:]
  try:
    return semantic_version.match(spec, version)
  except ValueError:
    # If the spec is malformed or we don't support it.
    return False
Esempio n. 10
0
# limitations under the License.
"""This module contains a TensorFlow implementation of the :class:`~.DefaultQubit`
reference plugin.
"""
import numpy as np
import semantic_version

from pennylane.operation import DiagonalOperation

try:
    import tensorflow as tf

    if tf.__version__[0] == "1":
        raise ImportError("default.qubit.tf device requires TensorFlow>=2.0")

    SUPPORTS_APPLY_OPS = semantic_version.match(">=2.3.0", tf.__version__)

except ImportError as e:
    raise ImportError("default.qubit.tf device requires TensorFlow>=2.0") from e


# With TF 2.1+, the legacy tf.einsum was renamed to _einsum_v1, while
# the replacement tf.einsum introduced the bug. This try-except block
# will dynamically patch TensorFlow versions where _einsum_v1 exists, to make it the
# default einsum implementation.
#
# For more details, see https://github.com/tensorflow/tensorflow/issues/37307
try:
    from tensorflow.python.ops.special_math_ops import _einsum_v1

    tf.einsum = _einsum_v1
Esempio n. 11
0
# limitations under the License.
"""
This module contains the mixin interface class for creating differentiable quantum tapes with
PyTorch.
"""
# pylint: disable=protected-access, attribute-defined-outside-init, arguments-differ, no-member, import-self
import numpy as np
import semantic_version
import torch

from pennylane import QuantumFunctionError
from pennylane.interfaces.torch import args_to_numpy

from pennylane.tape.queuing import AnnotatedQueue

COMPLEX_SUPPORT = semantic_version.match(">=1.6.0", torch.__version__)


class _TorchInterface(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input_kwargs, *input_):
        """Implements the forward pass QNode evaluation"""
        # detach all input tensors, convert to NumPy array
        ctx.args = args_to_numpy(input_)
        ctx.kwargs = input_kwargs
        ctx.save_for_backward(*input_)

        tape = ctx.kwargs["tape"]
        device = ctx.kwargs["device"]

        # unwrap constant parameters
Esempio n. 12
0
    return x.detach().cpu().numpy()


ar.register_function("torch", "to_numpy", _to_numpy_torch)
ar.register_function(
    "torch", "asarray", lambda x, device=None: _i("torch").as_tensor(x, device=device)
)
ar.register_function("torch", "diag", lambda x, k=0: _i("torch").diag(x, diagonal=k))
ar.register_function("torch", "expand_dims", lambda x, axis: _i("torch").unsqueeze(x, dim=axis))
ar.register_function("torch", "shape", lambda x: tuple(x.shape))
ar.register_function("torch", "gather", lambda x, indices: x[indices])
ar.register_function("torch", "gather", lambda x, indices: x[indices])

try:
    if semantic_version.match(">=1.10", _i("torch").__version__):
        # Autoray uses the deprecated torch.symeig as an alias for eigh, however this has
        # been deprecated in favour of torch.linalg.eigh.
        # autoray.py:84: UserWarning: torch.symeig is deprecated in favor of torch.linalg.eigh
        # and will be removed in a future PyTorch release.
        del ar.autoray._FUNCS["torch", "linalg.eigh"]
except ImportError:
    pass


ar.register_function(
    "torch",
    "sqrt",
    lambda x: _i("torch").sqrt(
        x.to(_i("torch").float64) if x.dtype in (_i("torch").int64, _i("torch").int32) else x
    ),
Esempio n. 13
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This module contains a PyTorch implementation of the :class:`~.DefaultQubit`
reference plugin.
"""
import warnings
import semantic_version

try:
    import torch

    VERSION_SUPPORT = semantic_version.match(">=1.8.1", torch.__version__)
    if not VERSION_SUPPORT:
        raise ImportError("default.qubit.torch device requires Torch>=1.8.1")

except ImportError as e:
    raise ImportError(
        "default.qubit.torch device requires Torch>=1.8.1") from e

import numpy as np
from pennylane.ops.qubit.attributes import diagonal_in_z_basis
from . import DefaultQubit


class DefaultQubitTorch(DefaultQubit):
    """Simulator plugin based on ``"default.qubit"``, written using PyTorch.
Esempio n. 14
0
def get_contact(msg_body):
    if semantic_version.match('<0.2.1', msg_body['version']):
        return msg_body['ci']
    return msg_body['contact']
Esempio n. 15
0
def handle_ci_umb(msg):
    #
    # Handle messages in Fedora CI messages format
    #
    # https://pagure.io/fedora-ci/messages
    #

    msg_body = msg['body']['msg']

    # check if required version is provided in the message
    if 'version' not in msg_body:
        LOGGER.error((
            'The message "{0}" does not contain required version information'
            .format(msg_body),
            ', cannot continue'
        ))
        return

    topic = msg['topic']

    item_type = msg_body['artifact']['type']
    test_run_url = msg_body['run']['url']

    test = _get_test_details(topic, msg_body)

    outcome = _test_result_outcome(topic, test.result)

    # variables to be passed to create_result
    groups = [{
        'uuid': str(uuid.uuid4()),
        'url': test_run_url
    }]

    system = msg_body.get('system', {})

    # Oddly, sometimes people pass us a sytem dict but other times a
    # list of one system dict.  Try to handle those two situation here.
    if isinstance(system, list):
        system = system[0] if system else {}

    contact = get_contact(msg_body)

    if item_type == 'productmd-compose':
        architecture = system['architecture']
        variant = system.get('variant')
        # Field compose_id in artifacts is deprecated.
        compose_id = msg_body['artifact'].get('id') or msg_body['artifact']['compose_id']
        item = '{0}/{1}/{2}'.format(compose_id, variant or 'unknown', architecture)
        result_data = {
            key: value for key, value in (
                ('item', item),

                ('ci_name', contact['name']),
                ('ci_team', contact['team']),
                ('ci_url', contact.get('url', 'not available')),
                ('ci_irc', contact.get('irc', 'not available')),
                ('ci_email', contact['email']),

                ('log', msg_body['run']['log']),

                ('type', item_type),
                ('productmd.compose.id', compose_id),

                ('system_provider', system['provider']),
                ('system_architecture', architecture),
                ('system_variant', variant),

                ('category', test.category),
            ) if value is not None
        }

    elif item_type == 'component-version':
        component = msg_body['artifact']['component']
        version = msg_body['artifact']['version']
        item = '{0}-{1}'.format(component, version)
        result_data = {
            key: value for key, value in (
                ('item', item),

                ('ci_name', contact['name']),
                ('ci_team', contact['team']),
                ('ci_url', contact.get('url', 'not available')),
                ('ci_irc', contact.get('irc', 'not available')),
                ('ci_email', contact['email']),

                ('log', msg_body['run']['log']),

                ('type', item_type),
                ('component', component),
                ('version', version),

                ('category', test.category),
            ) if value is not None
        }

    elif item_type == 'container-image':
        repo = msg_body['artifact']['repository']
        digest = msg_body['artifact']['digest']
        item = '{0}@{1}'.format(repo, digest)
        result_data = {
            key: value for key, value in (
                ('item', item),

                ('ci_name', contact['name']),
                ('ci_team', contact['team']),
                ('ci_url', contact.get('url', 'not available')),
                ('ci_irc', contact.get('irc', 'not available')),
                ('ci_email', contact['email']),

                ('log', msg_body['run']['log']),
                ('rebuild', msg_body['run'].get('rebuild')),
                ('xunit', test.xunit),

                ('type', item_type),
                ('repository', msg_body['artifact'].get('repository')),
                ('digest', msg_body['artifact'].get('digest')),
                ('format', msg_body['artifact'].get('format')),
                ('pull_ref', msg_body['artifact'].get('pull_ref')),
                ('scratch', msg_body['artifact'].get('scratch')),
                ('nvr', msg_body['artifact'].get('nvr')),
                ('issuer', msg_body['artifact'].get('issuer')),

                ('system_os', system.get('os')),
                ('system_provider', system.get('provider')),
                ('system_architecture', system.get('architecture')),

                ('category', test.category),
            ) if value is not None
        }

    elif item_type == 'redhat-module':
        # The pagure.io/messages spec defines the NSVC delimited with ':' and the stream name can
        # contain '-', which MBS changes to '_' when importing to koji.
        # See https://github.com/release-engineering/resultsdb-updater/pull/73
        nsvc_regex = re.compile('^(.*):(.*):(.*):(.*)')
        try:
            name, stream, version, context = re.match(
                nsvc_regex, msg_body['artifact']['nsvc']).groups()
            stream = stream.replace('-', '_')
        except AttributeError:
            LOGGER.error("Invalid nsvc '{}' encountered, ignoring result".format(
                msg_body['artifact']['nsvc']))
            return

        nsvc = '{}-{}-{}.{}'.format(name, stream, version, context)

        result_data = {
            'item': nsvc,
            'type': item_type,
            'mbs_id': msg_body['artifact'].get('id'),
            'category': test.category,
            'context': msg_body['artifact']['context'],
            'name': msg_body['artifact']['name'],
            'nsvc': nsvc,
            'stream': msg_body['artifact']['stream'],
            'version': msg_body['artifact']['version'],
            'issuer': msg_body['artifact'].get('issuer'),
            'rebuild': msg_body['run'].get('rebuild'),
            'log': msg_body['run']['log'],
            'system_os': system.get('os'),
            'system_provider': system.get('provider'),
            'ci_name': contact.get('name'),
            'ci_url': contact.get('url', 'not available'),
            'ci_team': contact.get('team'),
            'ci_irc': contact.get('irc', 'not available'),
            'ci_email': contact.get('email'),
        }
    # used as a default
    elif item_type == 'brew-build':
        item = msg_body['artifact']['nvr']
        component = msg_body['artifact']['component']
        scratch = msg_body['artifact'].get('scratch', '')
        brew_task_id = msg_body['artifact'].get('id')

        # scratch is supposed to be a bool but some messages in the wild
        # use a string instead
        if not isinstance(scratch, bool):
            scratch = scratch.lower() == 'true'

        # we need to differentiate between scratch and non-scratch builds
        if scratch:
            item_type += '_scratch'

        result_data = {
            'item': item,
            'type': item_type,
            'brew_task_id': brew_task_id,
            'category': test.category,
            'component': component,
            'scratch': scratch,
            'issuer': msg_body['artifact'].get('issuer'),
            'rebuild': msg_body['run'].get('rebuild'),
            'log': msg_body['run']['log'],  # required
            'system_os': system.get('os'),
            'system_provider': system.get('provider'),
            'ci_name': contact.get('name'),
            'ci_url': contact.get('url', 'not available'),
            'ci_team': contact.get('team'),
            'ci_irc': contact.get('irc', 'not available'),
            'ci_email': contact.get('email'),
        }

    # an unknown artifact type
    else:
        LOGGER.error("Artifact type '{}' handling not implemented".format(item_type))
        return

    # add optional recipients field, according to the version
    if semantic_version.match('<0.2.0', msg_body['version']):
        result_data['recipients'] = msg_body.get('recipients', [])

    else:
        notification = msg_body.get('notification', None)

        if notification:
            result_data['recipients'] = notification.get('recipients', [])

    update_publisher_id(data=result_data, msg=msg)

    # construct resultsdb testcase dict
    testcase = {
        'name': '.'.join([test.namespace, test.type, test.category]),
        'ref_url': msg_body['run']['url'],
    }

    if not verify_topic_and_testcase_name(msg['topic'], testcase['name']):
        return

    create_result(testcase, outcome, test_run_url, result_data, groups)
Esempio n. 16
0
def _tensordot_torch(tensor1, tensor2, axes):
    torch = _i("torch")
    if not semantic_version.match(">=1.10.0", torch.__version__) and axes == 0:
        return torch.outer(tensor1, tensor2)
    return torch.tensordot(tensor1, tensor2, axes)