提交 5b3d7c52 authored 作者: George Ho's avatar George Ho

Remove 'easy' references to theano.compat

上级 70ec78a1
......@@ -9,7 +9,7 @@ import theano
import theano.gpuarray
import theano.tensor as tt
from theano import config, gof
from theano.compat import exc_message
from theano.utils import exc_message
from theano.compile import UnusedInputError, function
from theano.compile.io import In, Out
from theano.gof import MissingInputError
......
......@@ -8,7 +8,6 @@ import tests.unittest_tools as utt
import theano
from tests.gpuarray.config import mode_with_gpu
from theano import config, function, tensor
from theano.compat import PY3
from theano.gpuarray.multinomial import (
GPUAChoiceFromUniform,
GPUAMultinomialFromUniform,
......@@ -378,9 +377,3 @@ def test_gpu_opt_wor():
def test_unpickle_legacy_op():
testfile_dir = os.path.dirname(os.path.realpath(__file__))
fname = "test_gpuarray_multinomial_wo_replacement.pkl"
if not PY3:
with open(os.path.join(testfile_dir, fname)) as fp:
u = Unpickler(fp)
m = u.load()
assert isinstance(m, GPUAChoiceFromUniform)
......@@ -8,8 +8,8 @@ import logging
import re
import traceback as tb
import warnings
from collections import OrderedDict
from theano import compat
from theano.compile.function_module import orig_function
from theano.compile.pfunc import pfunc
......@@ -291,7 +291,7 @@ def function(
if (
isinstance(updates, dict)
and not isinstance(updates, compat.OrderedDict)
and not isinstance(updates, OrderedDict)
and len(updates) > 1
):
warnings.warn(
......@@ -300,7 +300,7 @@ def function(
" got " + str(type(updates)) + ". Using "
"a standard dictionary here results in "
"non-deterministic behavior. You should use an OrderedDict"
" if you are using Python 2.7 (theano.compat.OrderedDict"
" if you are using Python 2.7 (collections.OrderedDict"
" for older python), or use a list of (shared, update)"
" pairs. Do not just convert your dictionary to this type before"
" the call as the conversion will still be non-deterministic.",
......
......@@ -12,7 +12,6 @@ from six.moves import StringIO
import theano
from theano import config
from theano.compat import PY3
from theano.gof import cmodule, graph, link, utils
from theano.gof.callcache import CallCache
from theano.gof.compilelock import get_lock, release_lock
......@@ -1648,8 +1647,7 @@ class CLinker(link.Linker):
# Static methods that can run and destroy the struct built by
# instantiate.
if PY3:
static = """
static = """
static int {struct_name}_executor({struct_name} *self) {{
return self->run();
}}
......@@ -1658,21 +1656,7 @@ class CLinker(link.Linker):
{struct_name} *self = ({struct_name} *)PyCapsule_GetContext(capsule);
delete self;
}}
""".format(
struct_name=self.struct_name
)
else:
static = """
static int %(struct_name)s_executor(%(struct_name)s* self) {
return self->run();
}
static void %(struct_name)s_destructor(void* executor, void* self) {
delete ((%(struct_name)s*)self);
}
""" % dict(
struct_name=self.struct_name
)
""".format(struct_name=self.struct_name)
# We add all the support code, compile args, headers and libs we need.
for support_code in self.support_code() + self.c_support_code_apply:
......@@ -1769,9 +1753,7 @@ class CLinker(link.Linker):
print(" delete struct_ptr;", file=code)
print(" return NULL;", file=code)
print(" }", file=code)
if PY3:
print(
"""\
print("""\
PyObject* thunk = PyCapsule_New((void*)(&{struct_name}_executor), NULL, {struct_name}_destructor);
if (thunk != NULL && PyCapsule_SetContext(thunk, struct_ptr) != 0) {{
PyErr_Clear();
......@@ -1779,16 +1761,10 @@ class CLinker(link.Linker):
thunk = NULL;
}}
""".format(
**locals()
),
file=code,
)
else:
print(
" PyObject* thunk = PyCObject_FromVoidPtrAndDesc((void*)(&%(struct_name)s_executor), struct_ptr, %(struct_name)s_destructor);"
% locals(),
file=code,
)
**locals()
),
file=code,
)
print(" return thunk; }", file=code)
return code.getvalue()
......
......@@ -149,38 +149,28 @@ class DynamicModule:
print("};", file=stream)
def print_init(self, stream):
if PY3:
print(
"""\
print(
"""\
static struct PyModuleDef moduledef = {{
PyModuleDef_HEAD_INIT,
"{name}",
NULL,
-1,
MyMethods,
PyModuleDef_HEAD_INIT,
"{name}",
NULL,
-1,
MyMethods,
}};
""".format(
name=self.hash_placeholder
),
file=stream,
)
print(
("PyMODINIT_FUNC PyInit_%s(void) {" % self.hash_placeholder),
file=stream,
)
for block in self.init_blocks:
print(" ", block, file=stream)
print(" PyObject *m = PyModule_Create(&moduledef);", file=stream)
print(" return m;", file=stream)
else:
print(("PyMODINIT_FUNC init%s(void){" % self.hash_placeholder), file=stream)
for block in self.init_blocks:
print(" ", block, file=stream)
print(
" ",
('(void) Py_InitModule("%s", MyMethods);' % self.hash_placeholder),
file=stream,
)
name=self.hash_placeholder
),
file=stream,
)
print(
("PyMODINIT_FUNC PyInit_%s(void) {" % self.hash_placeholder),
file=stream,
)
for block in self.init_blocks:
print(" ", block, file=stream)
print(" PyObject *m = PyModule_Create(&moduledef);", file=stream)
print(" return m;", file=stream)
print("}", file=stream)
def add_include(self, str):
......
......@@ -3,7 +3,6 @@ import os
import sys
from theano import config
from theano.compat import PY3
from theano.gof.compilelock import get_lock, release_lock
from . import cmodule
......@@ -49,35 +48,26 @@ def compile_cutils():
"Run a theano cthunk."},
{NULL, NULL, 0, NULL} /* Sentinel */
};"""
if PY3:
# This is not the most efficient code, but it is written this way to
# highlight the changes needed to make 2.x code compile under python 3.
code = code.replace("<Python.h>", '"numpy/npy_3kcompat.h"', 1)
code = code.replace("PyCObject", "NpyCapsule")
code += """
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"cutils_ext",
NULL,
-1,
CutilsExtMethods,
};
PyMODINIT_FUNC
PyInit_cutils_ext(void) {
return PyModule_Create(&moduledef);
}
}
"""
else:
code += """
PyMODINIT_FUNC
initcutils_ext(void)
{
(void) Py_InitModule("cutils_ext", CutilsExtMethods);
}
} //extern C
"""
# This is not the most efficient code, but it is written this way to
# highlight the changes needed to make 2.x code compile under python 3.
code = code.replace("<Python.h>", '"numpy/npy_3kcompat.h"', 1)
code = code.replace("PyCObject", "NpyCapsule")
code += """
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"cutils_ext",
NULL,
-1,
CutilsExtMethods,
};
PyMODINIT_FUNC
PyInit_cutils_ext(void) {
return PyModule_Create(&moduledef);
}
}
"""
loc = os.path.join(config.compiledir, "cutils_ext")
if not os.path.exists(loc):
......
......@@ -5,7 +5,7 @@ import sys
from six import StringIO
from theano import config
from theano.compat import DefaultOrderedDict
from theano.utils import DefaultOrderedDict
from theano.gof import opt
from theano.misc.ordered_set import OrderedSet
......
......@@ -6,7 +6,6 @@ import numpy as np
from six.moves import StringIO
from theano import config
from theano.compat import PY3
def simple_extract_stack(f=None, limit=None, skips=None):
......@@ -598,28 +597,16 @@ def remove(predicate, coll):
return [x for x in coll if not predicate(x)]
if PY3:
import hashlib
import hashlib
def hash_from_code(msg):
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return "m" + hashlib.sha256(msg).hexdigest()
else:
import hashlib
def hash_from_code(msg):
try:
return hashlib.sha256(msg).hexdigest()
except TypeError:
assert isinstance(msg, np.ndarray)
return hashlib.sha256(np.getbuffer(msg)).hexdigest()
def hash_from_code(msg):
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return "m" + hashlib.sha256(msg).hexdigest()
def hash_from_file(file_path):
......
......@@ -16,12 +16,12 @@ from copy import copy
from functools import partial
from itertools import chain
from textwrap import dedent
from collections.abc import Callable
import numpy as np
import theano
from theano import config, gof, printing
from theano.compat import Callable
from theano.gof import Apply, Constant, FunctionGraph, Op, Type, Variable, utils
from theano.gradient import DisconnectedType, grad_undefined
from theano.misc.safe_asarray import _asarray
......
......@@ -40,7 +40,7 @@ class OrderedUpdates(OrderedDict):
"non-ordered dictionary with 2+ elements could "
"make your code non-deterministic. You can use "
"an OrderedDict that is available at "
"theano.compat.OrderedDict for python 2.6+."
"collections.OrderedDict for python 2.6+."
)
super().__init__(*key, **kwargs)
for key in self:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论