__init__.py 1.66 KB
Newer Older
1
import importlib
2
import os
3
import sys
4

5
6
import numpy as np

7
8
9
10
11
12
from dgl.backend import *
from dgl.nn import *

from . import backend_unittest

mod = importlib.import_module(".%s" % backend_name, __name__)
13
14
15
thismod = sys.modules[__name__]

for api in backend_unittest.__dict__.keys():
16
    if api.startswith("__"):
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        continue
    elif callable(mod.__dict__[api]):
        # Tensor APIs used in unit tests MUST be supported across all backends
        globals()[api] = mod.__dict__[api]

# Tensor creation with default dtype and context

_zeros = zeros
_ones = ones
_randn = randn
_tensor = tensor
_arange = arange
_full = full
_full_1d = full_1d
31
_softmax = softmax
32
_default_context_str = os.getenv("DGLTESTDEV", "cpu")
33
_context_dict = {
34
35
36
    "cpu": cpu(),
    "gpu": cuda(),
}
37
38
_default_context = _context_dict[_default_context_str]

39

40
41
42
def ctx():
    return _default_context

43

44
def gpu_ctx():
45
46
    return _default_context_str == "gpu"

47

48
49
50
def zeros(shape, dtype=float32, ctx=_default_context):
    return _zeros(shape, dtype, ctx)

51

52
53
54
def ones(shape, dtype=float32, ctx=_default_context):
    return _ones(shape, dtype, ctx)

55

56
57
58
def randn(shape):
    return copy_to(_randn(shape), _default_context)

59

60
61
62
def tensor(data, dtype=None):
    return copy_to(_tensor(data, dtype), _default_context)

63

64
def arange(start, stop, dtype=int64, ctx=None):
65
66
67
68
    return _arange(
        start, stop, dtype, ctx if ctx is not None else _default_context
    )

69
70
71
72

def full(shape, fill_value, dtype, ctx=_default_context):
    return _full(shape, fill_value, dtype, ctx)

73

74
75
def full_1d(length, fill_value, dtype, ctx=_default_context):
    return _full_1d(length, fill_value, dtype, ctx)
76

77

78
def softmax(x, dim):
79
    return _softmax(x, dim)